Skip to content

Share one memoized label text color helper across canvas and previews - #2118

Merged
kmcginnes merged 1 commit into
schema-view-style-perffrom
share-label-text-color-helper
Aug 14, 2026
Merged

Share one memoized label text color helper across canvas and previews#2118
kmcginnes merged 1 commit into
schema-view-style-perffrom
share-label-text-color-helper

Conversation

@kmcginnes

Copy link
Copy Markdown
Collaborator

Description

LabelPreview inlined its own new Color(labelStyle.labelColor).isDark() with no guard for the empty labelColor an imported styling file can carry, so the preview could throw where the canvas does not. Both surfaces now call labelTextColorFor, 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:

  • The empty-labelColor fallback reads appDefaultEdgeStyle.labelColor instead of repeating the hex, so changing the default can't leave the contrast calculation comparing against the old one.
  • LINE_PATTERN becomes a Map. As an object literal, a lineStyle colliding with Object.prototype ("constructor", "toString") resolved to a function, which passed the !== undefined gate 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.

labelTextColorFor returns uppercase #FFFFFF, where the preview previously produced lowercase, so one assertion in LabelPreview.test.tsx moves with it. Same rendered colour.

How to read

  1. graphElementStyleData.ts — the helper, the default reference, and the Map.
  2. 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.

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant