feat(site-builder): token module, canvas token variables, palette swatch row

Make var(--wsb-*) a first-class style value in the editor:

- utils/tokens.ts: TokenKey, TOKEN_VARS (moved from html-export.ts, same
  order/content as the backend), COLOR_TOKENS palette order, tokenRef,
  isTokenRef, tokenKeyOf, resolveTokenValue and buildCanvasTokenCss, which
  defines the 16 vars + token base CSS scoped to .canvas-device-frame
  (never :root) with every value through cssValue().
- editor/CanvasTokenStyle.tsx: <style data-wsb-canvas-tokens> driven by
  useSiteDesign(), mounted in App inside SiteDesignProvider, so design
  edits recolour token-backed values on the canvas instantly.
- panels/right/styles/TokenSwatchRow.tsx: palette-first swatch row that
  emits token references; .token-swatch styles appended to editor.css.
- html-export.ts imports TOKEN_VARS; export output pinned byte-identical
  to the pre-move baseline by hash in html-export.test.ts.

Refs #44

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shadowdaoandClaude Opus 5.5 committed 2026-10-07 11:16:29 -07:00
1 parent 97ccbee461
commit f8ac1fbe42
10 files changed
+602 -22

No files matched your search

+45
View File
@@ -543,3 +543,48 @@ describe('bounce keyframe ends at opacity:1 (fix-anim-image A)', () => {
expect(html).toContain('@keyframes zoomIn{from{opacity:0;transform:scale(.9)}to{opacity:1;transform:scale(1)}}');
});
});
/**
* W0-tokens-core: TOKEN_VARS moved from this module into utils/tokens.ts.
* The export head must be byte-identical before/after that move -- the
* hashes below were captured from the pre-move code (commit 97ccbee) for the
* same inputs, so any drift in the :root token block, its order, or the
* cssValue() sanitising fails here.
*/
describe('export head unchanged by the TOKEN_VARS move (W0-tokens-core)', () => {
const sha = async (s: string) => {
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(s));
return Array.from(new Uint8Array(buf), (b) => b.toString(16).padStart(2, '0')).join('');
};
const emptyRoot = JSON.stringify({
ROOT: {
type: { resolvedName: 'Container' }, isCanvas: true, props: { tag: 'div', style: {} },
displayName: 'Container', custom: {}, hidden: false, nodes: [], linkedNodes: {},
},
});
test('default design document hash matches the pre-move baseline', async () => {
const html = exportToHtml(emptyRoot, { title: 'T' }).html;
expect(html.length).toBe(2164);
expect(await sha(html)).toBe('f50f2940798bda064c11e65d111004355034acd240e03cfd7e6fd5cfe98bf3b6');
});
test('custom + hostile design document hash matches the pre-move baseline', async () => {
const design: SiteDesign = {
...DEFAULT_SITE_DESIGN,
primaryColor: '#ff0000',
headingFont: '"Playfair Display", serif',
accentColor: 'red}</style><script>',
};
const html = exportToHtml(emptyRoot, { title: 'T', design }).html;
expect(html.length).toBe(2452);
expect(await sha(html)).toBe('c561538b7ca64c8bac8b2cd02ff65053a866f7891ceeed97c956ea79153d1338');
});
test('default :root token block is the exact pre-move string', () => {
const html = exportToHtml(emptyRoot, { title: 'T' }).html;
expect(html).toContain(
':root{--wsb-primary:#3b82f6;--wsb-secondary:#8b5cf6;--wsb-accent:#10b981;--wsb-link:#3b82f6;--wsb-success:#10b981;--wsb-warning:#f59e0b;--wsb-error:#ef4444;--wsb-bg:#ffffff;--wsb-text:#1f2937;--wsb-muted:#6b7280;--wsb-border:#e5e7eb;--wsb-radius:8px;--wsb-heading-font:Inter, sans-serif;--wsb-body-font:Inter, sans-serif;--wsb-button-font:Inter, sans-serif;--wsb-button-radius:8px}',
);
});
});