refactor: unify theme color resolution across display paths#272
Open
refactor: unify theme color resolution across display paths#272
Conversation
Adds `resolveColorToHex` as the single shared helper for converting any `ColorValue` (rgb or themeColor+tint/shade) to a display-ready hex string, with consistent handling of transparent/auto and missing-theme cases. Applies it at every display-side call site so `themeFill`/`themeColor` with `themeTint`/`themeShade` is honored everywhere, not just on table cells: - `toProseDoc.ts` — replaces the inline resolution added in #270. - `toFlowBlocks.ts` — paragraph shading now resolves themed fills (previously read `.fill.rgb` directly, silently dropping themed paragraph backgrounds). - `clipboard.ts` — HTML copy now honors themed text color and shading when a theme is threaded through `ClipboardOptions`/`useClipboard` options. Tests: +20 new pass (357 → 377 total), covering helper edge cases, paragraph themed-shading end-to-end (Document → PM → flow blocks), and clipboard HTML with themed runs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…tches Addresses review feedback on #272. **fromProseDoc round-trip fix** — themed cells (`themeFill` + `themeFillTint`/`themeFillShade`) no longer lose their theme refs on save. Stores the parse-time resolved hex in a new `_originalResolvedFill` attr; if it still matches `attrs.backgroundColor` at save time, the user hasn't changed the color so `orig.shading` is preserved intact. If they have changed it, we write plain `{ fill: { rgb } }` as before. **Helper simplification** — `resolveColorToHex` now uses `slice(1)` instead of a second `.replace(/^#/, '')` in the themed-color branch (`resolveColor` already guarantees the `#` prefix). Dedups the rgb-without-theme fallback by folding it into the single `rgb` branch below. Trims the docstring. **Unify remaining sites** — three sites were hand-rolling the same `resolveColor(...).replace(/^#/, '')` pattern; all now use the helper: - `DocxEditor.tsx:1474` (cell border color) - `FormattingBar.tsx:597` (table border color picker value) - `AdvancedColorPicker.tsx:222` (selected-cell hex comparison) Plus two sites that read `.color?.rgb` directly and dropped themed text colors: - `DocxEditor.tsx` text-color swatch readout - `toolbarUtils.ts` `getSelectionFormatting` (new optional `theme` param) **Tests** — 3 new round-trip tests in `toProseDoc.test.ts` proving themed `tint`/`shade` survive toProseDoc → fromProseDoc unchanged, and that user-initiated color changes still override theme refs with plain rgb. 380 pass (was 377), typecheck clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Follow-up to #270. Extracts a single shared helper —
resolveColorToHex(color, theme)— and routes every display-side call site through it sothemeFill/themeColorwiththemeTint/themeShadeis honored everywhere, not just on table cells.Background
#270 fixed the OOXML tint/shade math and table-cell theme resolution, but the same bug pattern (direct
.fill.rgbreads that silently drop themed fills) remained in two other places:layout-bridge/toFlowBlocks.ts— paragraphs withw:shd w:themeFill=\"accent1\"rendered with no background.utils/clipboard.ts— themed text color and shading were dropped when copy-pasting to external apps (Gmail, Word, etc.).Changes
New helper —
resolveColorToHexpackages/core/src/utils/colorResolver.tsColorValue(rgb, themeColor + tint/shade, or auto).#, orundefinedwhen unresolvable (transparent, auto, or theme ref with no theme).rgbif present, elseundefined.Unified call sites
prosemirror/conversion/toProseDoc.ts— replaces the inline resolution added in fix: correct OOXML theme color tint/shade resolution #270 (table cells).layout-bridge/toFlowBlocks.ts— paragraph shading (new fix).utils/clipboard.ts—runToHtmlandparagraphsToHtmlnow thread an optionalthemeand resolve both text color and shading via the shared helper.ClipboardOptionsgainstheme?: Theme | null;UseClipboardOptionsinuseClipboardfollows suit.Backwards compatible —
themeis optional everywhere; omitting it preserves the previous rgb-only behavior.Known follow-up
fromProseDoc.ts:1508writesattrs.backgroundColorback as plain{ fill: { rgb } }, which can stripthemeFill/themeFillTint/themeFillShadeon round-trip after #270 started populatingbackgroundColorfor themed cells. Documented here as a separate concern — fix requires either threading the theme intofromProseDocor stashing a "resolved fill" sentinel on_originalFormattingto detect user edits. Left for a follow-up since it touches a different seam.Tests
colorResolver.test.ts— 10 new cases covering helper edge cases (undefined, auto, rgb, themeColor w/ and w/o tint/shade, missing-theme fallback, leading-#normalization).layout-bridge/__tests__/toFlowBlocks-shading.test.ts(new) — end-to-end: Document model →toProseDoc→toFlowBlocks, verifies paragraphattrs.shadingresolves themed fills.clipboard.test.ts— 5 new cases covering themed text color and shading in HTML output, plus graceful no-theme degradation.Test plan
bun run typecheckpasses (all 4 packages)bun test— 377 pass, 0 fail (was 357 on main, +20 new tests)bun run formatcleannpx playwright test tests/demo-docx.spec.ts— 44/45 pass (1 pre-existing failure on main, unrelated to colors)🤖 Generated with Claude Code