Share one memoized label text color helper across canvas and previews - #2118
Merged
Merged
Conversation
`LabelPreview` inlined its own `new Color(...).isDark()` with no guard for the empty `labelColor` an imported style file can carry, so the preview could throw where the canvas does not. Both now call `labelTextColorFor`, memoized because parsing a color is the only non-trivial work here. Also reads the fallback from `appDefaultEdgeStyle.labelColor` rather than repeating the hex, and looks up line dash patterns through a `Map` so a `lineStyle` colliding with `Object.prototype` cannot resolve to a function.
kmcginnes
marked this pull request as ready for review
August 14, 2026 20:51
This was referenced Aug 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
LabelPreviewinlined its ownnew Color(labelStyle.labelColor).isDark()with no guard for the emptylabelColoran imported styling file can carry, so the preview could throw where the canvas does not. Both surfaces now calllabelTextColorFor, which already has that guard — a near-duplicate of the crash the helper exists to prevent is gone, and a preview can no longer drift from what gets drawn.Two smaller hardening changes ride along in the same file:
labelColorfallback readsappDefaultEdgeStyle.labelColorinstead of repeating the hex, so changing the default can't leave the contrast calculation comparing against the old one.LINE_PATTERNbecomes aMap. As an object literal, alineStylecolliding withObject.prototype("constructor","toString") resolved to a function, which passed the!== undefinedgate and was written onto every edge's cytoscape data. Reaching it needs same-origin IndexedDB write access, so this is hardening rather than a live bug.labelTextColorForreturns uppercase#FFFFFF, where the preview previously produced lowercase, so one assertion inLabelPreview.test.tsxmoves with it. Same rendered colour.How to read
graphElementStyleData.ts— the helper, the default reference, and theMap.LabelPreview.tsx— now a caller rather than a second implementation.Stack
Targets
schema-view-style-perf(#2112), which is left exactly as it is. Nine further PRs stack on this one so each change can be merged individually.