create-nextblock 0.14.2 → 0.14.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/package.json +1 -1
  2. package/templates/nextblock-template/app/[slug]/page.tsx +7 -2
  3. package/templates/nextblock-template/app/[slug]/page.utils.ts +8 -3
  4. package/templates/nextblock-template/app/actions/postActions.ts +3 -0
  5. package/templates/nextblock-template/app/actions/visibilityActions.ts +210 -0
  6. package/templates/nextblock-template/app/actions/visualEditingActions.test.ts +83 -3
  7. package/templates/nextblock-template/app/actions/visualEditingActions.ts +34 -14
  8. package/templates/nextblock-template/app/api/ai/global-agent/route.ts +45 -0
  9. package/templates/nextblock-template/app/api/cron/reset-sandbox/sandboxResetSql.ts +736 -117
  10. package/templates/nextblock-template/app/api/view/route.ts +114 -0
  11. package/templates/nextblock-template/app/article/[slug]/page.utils.ts +1 -2
  12. package/templates/nextblock-template/app/cms/CmsClientLayout.tsx +2 -2
  13. package/templates/nextblock-template/app/cms/blocks/components/ColumnEditor.tsx +17 -22
  14. package/templates/nextblock-template/app/cms/blocks/components/EditableBlock.tsx +13 -19
  15. package/templates/nextblock-template/app/cms/blocks/editors/HeadingBlockEditor.tsx +45 -34
  16. package/templates/nextblock-template/app/cms/components/DraftStatusActions.tsx +10 -0
  17. package/templates/nextblock-template/app/cms/components/VisibilityBadge.tsx +62 -0
  18. package/templates/nextblock-template/app/cms/components/VisibilityControl.tsx +528 -0
  19. package/templates/nextblock-template/app/cms/pages/[id]/edit/EditPageClient.tsx +33 -17
  20. package/templates/nextblock-template/app/cms/pages/[id]/edit/page.tsx +19 -7
  21. package/templates/nextblock-template/app/cms/pages/actions.ts +17 -10
  22. package/templates/nextblock-template/app/cms/pages/components/PageForm.tsx +7 -29
  23. package/templates/nextblock-template/app/cms/pages/page.tsx +6 -19
  24. package/templates/nextblock-template/app/cms/posts/[id]/edit/page.tsx +42 -18
  25. package/templates/nextblock-template/app/cms/posts/actions.ts +16 -27
  26. package/templates/nextblock-template/app/cms/posts/components/PostForm.tsx +3 -60
  27. package/templates/nextblock-template/app/cms/posts/page.tsx +6 -13
  28. package/templates/nextblock-template/app/cms/products/[id]/edit/page.tsx +63 -9
  29. package/templates/nextblock-template/app/cms/revisions/RevisionHistoryButton.tsx +66 -32
  30. package/templates/nextblock-template/app/cms/revisions/actions.ts +332 -285
  31. package/templates/nextblock-template/app/cms/revisions/service.test.ts +498 -0
  32. package/templates/nextblock-template/app/cms/revisions/service.ts +549 -471
  33. package/templates/nextblock-template/app/cms/revisions/utils.ts +304 -132
  34. package/templates/nextblock-template/app/cms/settings/global-css/components/ThemeEditor.tsx +382 -0
  35. package/templates/nextblock-template/app/cms/settings/global-css/components/ThemeManager.tsx +267 -0
  36. package/templates/nextblock-template/app/cms/settings/global-css/page.tsx +40 -24
  37. package/templates/nextblock-template/app/cms/settings/global-css/theme-actions.ts +259 -0
  38. package/templates/nextblock-template/app/layout.tsx +49 -0
  39. package/templates/nextblock-template/app/lib/sitemap-utils.ts +6 -4
  40. package/templates/nextblock-template/app/lib/ucp/server.ts +4 -1
  41. package/templates/nextblock-template/app/page.tsx +6 -3
  42. package/templates/nextblock-template/app/product/[slug]/page.tsx +27 -3
  43. package/templates/nextblock-template/app/providers.tsx +16 -4
  44. package/templates/nextblock-template/components/blocks/renderers/HeadingBlockRenderer.tsx +7 -10
  45. package/templates/nextblock-template/components/theme-icon.tsx +78 -0
  46. package/templates/nextblock-template/components/theme-switcher.tsx +85 -90
  47. package/templates/nextblock-template/components/visual-editing/NextblockVisualEditing.tsx +4 -1
  48. package/templates/nextblock-template/context/ThemeCatalogContext.tsx +44 -0
  49. package/templates/nextblock-template/docs/03-CMS-AND-EDITOR.md +77 -0
  50. package/templates/nextblock-template/docs/04-DATABASE-AND-AUTH.md +7 -2
  51. package/templates/nextblock-template/lib/blocks/blockColors.test.ts +114 -0
  52. package/templates/nextblock-template/lib/blocks/blockColors.ts +132 -0
  53. package/templates/nextblock-template/lib/blocks/blockRegistry.ts +17 -1
  54. package/templates/nextblock-template/lib/cms-transfer/server.ts +13 -0
  55. package/templates/nextblock-template/lib/full-backup/server.ts +1 -0
  56. package/templates/nextblock-template/lib/publishing/viewUrl.ts +26 -0
  57. package/templates/nextblock-template/lib/search/server.ts +3 -0
  58. package/templates/nextblock-template/lib/setup/migrations-bundle.ts +102 -87
  59. package/templates/nextblock-template/lib/themes/buildThemeCss.test.ts +163 -0
  60. package/templates/nextblock-template/lib/themes/buildThemeCss.ts +124 -0
  61. package/templates/nextblock-template/lib/themes/tokenColor.ts +31 -0
  62. package/templates/nextblock-template/lib/themes/tokens.ts +143 -0
  63. package/templates/nextblock-template/lib/visual-editing/mutations.ts +4 -1
  64. package/templates/nextblock-template/lib/visual-editing/product-drafts.ts +46 -1
  65. package/templates/nextblock-template/next-env.d.ts +2 -2
  66. package/templates/nextblock-template/package.json +1 -1
  67. package/templates/nextblock-template/scripts/verify-site-themes.ts +63 -0
  68. package/templates/nextblock-template/tsconfig.tsbuildinfo +1 -1
@@ -1,5 +1,6 @@
1
1
  import React from "react";
2
2
  import type { HeadingBlockContent } from '../../../lib/blocks/blockRegistry';
3
+ import { resolveTextAlign, resolveTextColor } from '../../../lib/blocks/blockColors';
3
4
  import type { VisualEditAttributes } from "../../../lib/visual-editing/types";
4
5
 
5
6
  interface HeadingBlockRendererProps {
@@ -23,19 +24,15 @@ const HeadingBlockRenderer: React.FC<HeadingBlockRendererProps> = ({
23
24
  : 2;
24
25
  const Tag: React.ElementType = `h${level}`;
25
26
 
26
- let alignmentClass = "";
27
- if (content.textAlign) {
28
- alignmentClass = `text-${content.textAlign}`;
29
- }
27
+ const alignmentClass = resolveTextAlign(content.textAlign);
28
+ const { className: colorClass, style: colorStyle } = resolveTextColor(content.textColor);
30
29
 
31
- let colorClass = "";
32
- if (content.textColor) {
33
- colorClass = `text-${content.textColor}`;
34
- }
30
+ const combinedClasses = ["my-6 font-bold container mx-auto", alignmentClass, colorClass]
31
+ .filter(Boolean)
32
+ .join(" ");
35
33
 
36
- const combinedClasses = `my-6 font-bold container mx-auto ${alignmentClass} ${colorClass}`.trim();
37
34
  return (
38
- <Tag className={combinedClasses} {...visualEditAttributes}>
35
+ <Tag className={combinedClasses} style={colorStyle} {...visualEditAttributes}>
39
36
  {content.text_content}
40
37
  </Tag>
41
38
  );
@@ -0,0 +1,78 @@
1
+ "use client";
2
+
3
+ import {
4
+ Aperture,
5
+ Atom,
6
+ Blend,
7
+ Brush,
8
+ Cloud,
9
+ Contrast,
10
+ Droplet,
11
+ Feather,
12
+ Flame,
13
+ Gem,
14
+ Ghost,
15
+ Leaf,
16
+ Moon,
17
+ Mountain,
18
+ Palette,
19
+ Rocket,
20
+ Snowflake,
21
+ Sparkles,
22
+ Star,
23
+ Sun,
24
+ Sunrise,
25
+ Sunset,
26
+ Waves,
27
+ Zap,
28
+ type LucideIcon,
29
+ } from "lucide-react";
30
+
31
+ /**
32
+ * Curated icon set offered to themes.
33
+ *
34
+ * A fixed map rather than a dynamic lucide lookup: the icon name comes from the
35
+ * database, and importing lucide dynamically by string would either pull the
36
+ * whole icon set into the bundle or break tree-shaking.
37
+ */
38
+ export const THEME_ICONS: Record<string, LucideIcon> = {
39
+ Sun,
40
+ Moon,
41
+ Zap,
42
+ Sparkles,
43
+ Palette,
44
+ Flame,
45
+ Droplet,
46
+ Leaf,
47
+ Star,
48
+ Cloud,
49
+ Snowflake,
50
+ Waves,
51
+ Mountain,
52
+ Sunrise,
53
+ Sunset,
54
+ Contrast,
55
+ Blend,
56
+ Brush,
57
+ Aperture,
58
+ Atom,
59
+ Feather,
60
+ Gem,
61
+ Ghost,
62
+ Rocket,
63
+ };
64
+
65
+ export const THEME_ICON_NAMES = Object.keys(THEME_ICONS);
66
+
67
+ export function ThemeIcon({
68
+ name,
69
+ size = 16,
70
+ className,
71
+ }: {
72
+ name: string;
73
+ size?: number;
74
+ className?: string;
75
+ }) {
76
+ const Icon = THEME_ICONS[name] ?? Palette;
77
+ return <Icon size={size} className={className} />;
78
+ }
@@ -1,90 +1,85 @@
1
- "use client";
2
-
3
- import { Button } from "@nextblock-cms/ui";
4
- import {
5
- DropdownMenu,
6
- DropdownMenuContent,
7
- DropdownMenuRadioGroup,
8
- DropdownMenuRadioItem,
9
- DropdownMenuTrigger,
10
- } from "@nextblock-cms/ui";
11
- import { Laptop, Moon, Sun, Zap } from "lucide-react";
12
- import { useTheme } from "next-themes";
13
- import { useEffect, useState } from "react";
14
- import { useTranslations } from "@nextblock-cms/utils";
15
-
16
- const ThemeSwitcher = () => {
17
- const [mounted, setMounted] = useState(false);
18
- const { theme, setTheme } = useTheme();
19
- const { t } = useTranslations();
20
-
21
- // useEffect only runs on the client, so now we can safely show the UI
22
- useEffect(() => {
23
- setMounted(true);
24
- }, []);
25
-
26
- if (!mounted) {
27
- return null;
28
- }
29
-
30
- const ICON_SIZE = 16;
31
-
32
- return (
33
- <DropdownMenu>
34
- <DropdownMenuTrigger asChild>
35
- <Button variant="ghost" size={"sm"} aria-label={t('theme_switcher')}>
36
- {theme === "light" ? (
37
- <Sun
38
- key="light"
39
- size={ICON_SIZE}
40
- className={"text-muted-foreground"}
41
- />
42
- ) : theme === "dark" ? (
43
- <Moon
44
- key="dark"
45
- size={ICON_SIZE}
46
- className={"text-muted-foreground"}
47
- />
48
- ) : theme === "vibrant" ? (
49
- <Zap
50
- key="vibrant"
51
- size={ICON_SIZE}
52
- className={"text-muted-foreground"}
53
- />
54
- ) : (
55
- <Laptop
56
- key="system"
57
- size={ICON_SIZE}
58
- className={"text-muted-foreground"}
59
- />
60
- )}
61
- </Button>
62
- </DropdownMenuTrigger>
63
- <DropdownMenuContent className="w-content" align="start">
64
- <DropdownMenuRadioGroup
65
- value={theme}
66
- onValueChange={(e) => setTheme(e)}
67
- >
68
- <DropdownMenuRadioItem className="flex gap-2" value="light">
69
- <Sun size={ICON_SIZE} className="text-muted-foreground" />{" "}
70
- <span>{t('theme_light')}</span>
71
- </DropdownMenuRadioItem>
72
- <DropdownMenuRadioItem className="flex gap-2" value="dark">
73
- <Moon size={ICON_SIZE} className="text-muted-foreground" />{" "}
74
- <span>{t('theme_dark')}</span>
75
- </DropdownMenuRadioItem>
76
- <DropdownMenuRadioItem className="flex gap-2" value="vibrant">
77
- <Zap size={ICON_SIZE} className="text-muted-foreground" />{" "}
78
- <span>{t('theme_vibrant')}</span>
79
- </DropdownMenuRadioItem>
80
- <DropdownMenuRadioItem className="flex gap-2" value="system">
81
- <Laptop size={ICON_SIZE} className="text-muted-foreground" />{" "}
82
- <span>{t('theme_system')}</span>
83
- </DropdownMenuRadioItem>
84
- </DropdownMenuRadioGroup>
85
- </DropdownMenuContent>
86
- </DropdownMenu>
87
- );
88
- };
89
-
90
- export { ThemeSwitcher };
1
+ "use client";
2
+
3
+ import { Button } from "@nextblock-cms/ui";
4
+ import {
5
+ DropdownMenu,
6
+ DropdownMenuContent,
7
+ DropdownMenuRadioGroup,
8
+ DropdownMenuRadioItem,
9
+ DropdownMenuTrigger,
10
+ } from "@nextblock-cms/ui";
11
+ import { Laptop } from "lucide-react";
12
+ import { useTheme } from "next-themes";
13
+ import { useEffect, useState } from "react";
14
+ import { useTranslations } from "@nextblock-cms/utils";
15
+ import { useThemeCatalog } from "../context/ThemeCatalogContext";
16
+ import { ThemeIcon } from "./theme-icon";
17
+
18
+ const ICON_SIZE = 16;
19
+
20
+ const ThemeSwitcher = () => {
21
+ const [mounted, setMounted] = useState(false);
22
+ const { theme, setTheme } = useTheme();
23
+ const { t } = useTranslations();
24
+ const themes = useThemeCatalog();
25
+
26
+ // useEffect only runs on the client, so now we can safely show the UI
27
+ useEffect(() => {
28
+ setMounted(true);
29
+ }, []);
30
+
31
+ if (!mounted) {
32
+ return null;
33
+ }
34
+
35
+ const active = themes.find((entry) => entry.slug === theme);
36
+
37
+ /**
38
+ * Built-in slugs keep their existing translation keys so no copy is lost;
39
+ * admin-created themes fall back to the name stored on the row.
40
+ */
41
+ const labelFor = (slug: string, name: string) => {
42
+ const builtin: Record<string, string> = {
43
+ light: "theme_light",
44
+ dark: "theme_dark",
45
+ vibrant: "theme_vibrant",
46
+ };
47
+ const key = builtin[slug];
48
+ if (!key) return name;
49
+ const translated = t(key);
50
+ return translated === key ? name : translated;
51
+ };
52
+
53
+ return (
54
+ <DropdownMenu>
55
+ <DropdownMenuTrigger asChild>
56
+ <Button variant="ghost" size={"sm"} aria-label={t('theme_switcher')}>
57
+ {active ? (
58
+ <ThemeIcon name={active.icon} size={ICON_SIZE} className="text-muted-foreground" />
59
+ ) : (
60
+ <Laptop size={ICON_SIZE} className="text-muted-foreground" />
61
+ )}
62
+ </Button>
63
+ </DropdownMenuTrigger>
64
+ <DropdownMenuContent className="w-content" align="start">
65
+ <DropdownMenuRadioGroup
66
+ value={theme}
67
+ onValueChange={(e) => setTheme(e)}
68
+ >
69
+ {themes.map((entry) => (
70
+ <DropdownMenuRadioItem key={entry.slug} className="flex gap-2" value={entry.slug}>
71
+ <ThemeIcon name={entry.icon} size={ICON_SIZE} className="text-muted-foreground" />{" "}
72
+ <span>{labelFor(entry.slug, entry.name)}</span>
73
+ </DropdownMenuRadioItem>
74
+ ))}
75
+ <DropdownMenuRadioItem className="flex gap-2" value="system">
76
+ <Laptop size={ICON_SIZE} className="text-muted-foreground" />{" "}
77
+ <span>{t('theme_system')}</span>
78
+ </DropdownMenuRadioItem>
79
+ </DropdownMenuRadioGroup>
80
+ </DropdownMenuContent>
81
+ </DropdownMenu>
82
+ );
83
+ };
84
+
85
+ export { ThemeSwitcher };
@@ -1018,7 +1018,10 @@ function VisualEditingToolbar() {
1018
1018
  return;
1019
1019
  }
1020
1020
 
1021
- setMessage("Draft published.");
1021
+ // A warning means the content is live but the revision didn't record — still a
1022
+ // publish, so the editor closes, but don't report it as a clean one.
1023
+ const warning = result && "success" in result ? result.warning : undefined;
1024
+ setMessage(warning ?? "Draft published.");
1022
1025
  hasSavedSinceOpenRef.current = false;
1023
1026
  closeVisualEditor();
1024
1027
  router.refresh();
@@ -0,0 +1,44 @@
1
+ "use client";
2
+
3
+ import { createContext, useContext, useMemo, type ReactNode } from "react";
4
+
5
+ /** The switcher-facing shape of a row in `site_themes`. */
6
+ export interface ThemeCatalogEntry {
7
+ slug: string;
8
+ name: string;
9
+ /** lucide-react icon name. */
10
+ icon: string;
11
+ colorScheme: "light" | "dark";
12
+ }
13
+
14
+ /**
15
+ * Themes are read once on the server in app/layout.tsx and handed to the client
16
+ * through this context, so the switcher never fetches and never hardcodes a
17
+ * list. Falls back to the palette shipped in libs/ui/src/styles/theme.css when
18
+ * the table is empty or unreachable.
19
+ */
20
+ export const FALLBACK_THEME_CATALOG: ThemeCatalogEntry[] = [
21
+ { slug: "light", name: "Light", icon: "Sun", colorScheme: "light" },
22
+ { slug: "dark", name: "Dark", icon: "Moon", colorScheme: "dark" },
23
+ { slug: "vibrant", name: "Vibrant", icon: "Zap", colorScheme: "dark" },
24
+ ];
25
+
26
+ const ThemeCatalogContext = createContext<ThemeCatalogEntry[]>(FALLBACK_THEME_CATALOG);
27
+
28
+ export function ThemeCatalogProvider({
29
+ themes,
30
+ children,
31
+ }: {
32
+ themes?: ThemeCatalogEntry[];
33
+ children: ReactNode;
34
+ }) {
35
+ const value = useMemo(
36
+ () => (Array.isArray(themes) && themes.length > 0 ? themes : FALLBACK_THEME_CATALOG),
37
+ [themes],
38
+ );
39
+ return <ThemeCatalogContext.Provider value={value}>{children}</ThemeCatalogContext.Provider>;
40
+ }
41
+
42
+ export function useThemeCatalog(): ThemeCatalogEntry[] {
43
+ return useContext(ThemeCatalogContext);
44
+ }
@@ -200,3 +200,80 @@ contract lives in `libs/sdk` and is documented in
200
200
 
201
201
  If you are changing how blocks work inside the CMS, start here. If you are
202
202
  designing a reusable third-party block contract, start with the SDK doc.
203
+
204
+ ## Themes and Colour
205
+
206
+ ### Editable site themes
207
+
208
+ Themes are rows in `site_themes`, edited at **/cms/settings/global-css**. An
209
+ ADMIN can recolour any theme, create new ones, duplicate, reorder, hide, set the
210
+ site default, and delete non-system themes.
211
+
212
+ The pipeline is:
213
+
214
+ 1. `app/layout.tsx` reads the table through a cached `getCachedSiteThemes()`
215
+ (tag `public-layout-site-themes`).
216
+ 2. `lib/themes/buildThemeCss.ts` renders each row to a `:root.<slug> { ... }`
217
+ rule, injected as a `<style id="nb-theme-tokens">` in `<head>` — before the
218
+ Global CSS box, so custom CSS can still override a theme.
219
+ 3. `app/providers.tsx` feeds the slug list and default to `next-themes`, and
220
+ `context/ThemeCatalogContext.tsx` carries names + icons to the switcher.
221
+
222
+ `libs/ui/src/styles/theme.css` remains as the fallback palette for standalone
223
+ consumers of the published `@nextblock-cms/ui` package. Generated rules use
224
+ `:root.<slug>` (specificity 0,2,0) so they always beat that file's `.dark` /
225
+ `.vibrant` rules (0,1,0) regardless of stylesheet order.
226
+
227
+ ### Token storage
228
+
229
+ Colour tokens are stored as **bare HSL triplets** (`"222 47% 11%"`), not hex,
230
+ because `libs/ui/tailwind.config.js` composes them as `hsl(var(--primary))` and
231
+ appends alpha as `hsl(var(--primary) / 0.5)` — only a bare triplet supports
232
+ that. `lib/themes/tokenColor.ts` converts to and from the hex the colour picker
233
+ speaks. The allowed token list lives in `lib/themes/tokens.ts`; anything not on
234
+ it is dropped on save, and values are shape-checked, because the result is
235
+ interpolated into a `<style>` tag.
236
+
237
+ Per-theme `extra_css` is emitted **nested inside** the theme rule, so authors
238
+ write `& h1 { ... }` and scoping is automatic. `sanitizeExtraCss()` strips `<`
239
+ and unbalanced `}` so it cannot escape the rule or close the style element.
240
+
241
+ ### Known limitation: `dark:` utilities under custom themes
242
+
243
+ Tailwind's dark variant compiles to `.dark`, and `darkMode: ['class']` is fixed
244
+ at build time. A custom theme with `color_scheme: 'dark'` recolours every token
245
+ and sets CSS `color-scheme`, but does **not** activate `dark:` utilities — the
246
+ same behaviour the shipped `.vibrant` theme has always had.
247
+
248
+ It cannot be fixed by mapping the theme to two classes: `next-themes` applies
249
+ its value with a single `classList.add(value)`, and `DOMTokenList.add` throws
250
+ `InvalidCharacterError` on a string containing a space. Wiring `dark:` to a data
251
+ attribute would require a pre-hydration script mirroring next-themes' own.
252
+
253
+ Verify the database → CSS path without booting the app:
254
+
255
+ ```bash
256
+ npm run verify:site-themes
257
+ ```
258
+
259
+ ### Block text colour
260
+
261
+ `apps/nextblock/lib/blocks/blockColors.ts` is the single source of truth for
262
+ block text colour, shared by `HeadingBlockRenderer`, `EditableBlock` and
263
+ `ColumnEditor` so the CMS preview cannot drift from the live page.
264
+
265
+ A stored value is either a **theme token** keyword (`"primary"` — follows the
266
+ theme) or a **literal CSS colour** (`"#FF8800"` — fixed). Tokens render as a
267
+ static Tailwind class; literals render as an inline `style`. `resolveTextColor()`
268
+ returns `{ className?, style? }` and falls back to `{}` for anything it does not
269
+ recognise, so a hand-edited value degrades to the inherited colour instead of
270
+ emitting a bogus class.
271
+
272
+ Class names here are written as **complete literals**. Tailwind v4 ignores the
273
+ `safelist` key in the legacy JS config — `libs/ui/tailwind.config.js` still
274
+ carries one, but v4 never reads it, so a class exists only if the scanner finds
275
+ the whole string in a source file.
276
+
277
+ The editor control is `ColorField` from `libs/ui`, which offers the theme tokens
278
+ and a custom picker (hex, alpha, screen eyedropper, recent colours) with a live
279
+ WCAG contrast badge measured against the section background.
@@ -100,6 +100,7 @@ Defined primarily in `00000000000002_setup_content_tables.sql`:
100
100
  - `navigation_items`
101
101
  - `page_revisions`
102
102
  - `post_revisions`
103
+ - `product_revisions` (added in `00000000000016`, alongside `products.version`)
103
104
 
104
105
  ### Commerce tables
105
106
 
@@ -179,8 +180,12 @@ tree. The current sequence is:
179
180
 
180
181
  Every file is fully idempotent. Existing databases already have versions
181
182
  `000`–`003` recorded, so both appliers skip the baseline — it only runs on a
182
- fresh/empty database. **The next new migration is `00000000000004`**, appended
183
- forward-only.
183
+ fresh/empty database.
184
+
185
+ `00000000000004` was the first migration appended after that re-baseline, not the
186
+ one still to be written — the folder has grown well past it. **To find the next
187
+ number, list `libs/db/src/supabase/migrations` and take the one after the highest
188
+ file on disk.** Never copy a hardcoded "next is N" out of a doc.
184
189
 
185
190
  ### Production migration policy
186
191
 
@@ -0,0 +1,114 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ TEXT_COLOR_CLASSES,
4
+ TEXT_COLOR_TOKENS,
5
+ TEXT_COLOR_TOKEN_OPTIONS,
6
+ isCustomCssColor,
7
+ isTextColorToken,
8
+ resolveTextAlign,
9
+ resolveTextColor,
10
+ } from './blockColors';
11
+ import { HeadingBlockSchema } from './blockRegistry';
12
+
13
+ describe('resolveTextColor', () => {
14
+ it('maps theme tokens to a static class and no inline style', () => {
15
+ expect(resolveTextColor('primary')).toEqual({ className: 'text-primary' });
16
+ expect(resolveTextColor('foreground')).toEqual({ className: 'text-foreground' });
17
+ });
18
+
19
+ it('maps "muted" to the readable foreground pair, not the surface colour', () => {
20
+ // Regression: the renderer used to emit `text-muted` (hsl(var(--muted)),
21
+ // a near-white surface) while both CMS previews used muted-foreground.
22
+ expect(resolveTextColor('muted')).toEqual({ className: 'text-muted-foreground' });
23
+ });
24
+
25
+ it('renders custom colours as an inline style', () => {
26
+ expect(resolveTextColor('#FF8800')).toEqual({ style: { color: '#FF8800' } });
27
+ expect(resolveTextColor('rgba(255, 136, 0, 0.8)')).toEqual({ style: { color: 'rgba(255, 136, 0, 0.8)' } });
28
+ expect(resolveTextColor('hsl(28, 100%, 50%)')).toEqual({ style: { color: 'hsl(28, 100%, 50%)' } });
29
+ expect(resolveTextColor('#f80')).toEqual({ style: { color: '#f80' } });
30
+ expect(resolveTextColor('#FF880080')).toEqual({ style: { color: '#FF880080' } });
31
+ });
32
+
33
+ it('trims incidental whitespace', () => {
34
+ expect(resolveTextColor(' primary ')).toEqual({ className: 'text-primary' });
35
+ expect(resolveTextColor(' #FF8800 ')).toEqual({ style: { color: '#FF8800' } });
36
+ });
37
+
38
+ it('degrades to inherited colour rather than emitting a bogus class', () => {
39
+ expect(resolveTextColor(undefined)).toEqual({});
40
+ expect(resolveTextColor(null)).toEqual({});
41
+ expect(resolveTextColor('')).toEqual({});
42
+ expect(resolveTextColor('chartreuse')).toEqual({});
43
+ expect(resolveTextColor('javascript:alert(1)')).toEqual({});
44
+ expect(resolveTextColor('url(https://evil.test/x.png)')).toEqual({});
45
+ expect(resolveTextColor('red; background: url(x)')).toEqual({});
46
+ });
47
+
48
+ it('never emits an interpolated class name', () => {
49
+ for (const token of TEXT_COLOR_TOKENS) {
50
+ const { className } = resolveTextColor(token);
51
+ expect(className).toBe(TEXT_COLOR_CLASSES[token]);
52
+ expect(className?.startsWith('text-')).toBe(true);
53
+ }
54
+ });
55
+ });
56
+
57
+ describe('resolveTextAlign', () => {
58
+ it('covers every alignment the schema allows', () => {
59
+ expect(resolveTextAlign('left')).toBe('text-left');
60
+ expect(resolveTextAlign('center')).toBe('text-center');
61
+ expect(resolveTextAlign('right')).toBe('text-right');
62
+ expect(resolveTextAlign('justify')).toBe('text-justify');
63
+ });
64
+
65
+ it('ignores unknown values', () => {
66
+ expect(resolveTextAlign(undefined)).toBeUndefined();
67
+ expect(resolveTextAlign('sideways')).toBeUndefined();
68
+ });
69
+ });
70
+
71
+ describe('guards', () => {
72
+ it('distinguishes tokens from custom colours', () => {
73
+ expect(isTextColorToken('accent')).toBe(true);
74
+ expect(isTextColorToken('#FFF')).toBe(false);
75
+ expect(isCustomCssColor('#FFF')).toBe(true);
76
+ expect(isCustomCssColor('accent')).toBe(false);
77
+ });
78
+ });
79
+
80
+ describe('token options', () => {
81
+ it('exposes a label and swatch for every token', () => {
82
+ expect(TEXT_COLOR_TOKEN_OPTIONS).toHaveLength(TEXT_COLOR_TOKENS.length);
83
+ for (const option of TEXT_COLOR_TOKEN_OPTIONS) {
84
+ expect(option.label).toBeTruthy();
85
+ expect(option.cssColor).toMatch(/^hsl\(var\(--[a-z-]+\)\)$/);
86
+ }
87
+ });
88
+ });
89
+
90
+ describe('HeadingBlockSchema.textColor', () => {
91
+ const base = { level: 2 as const, text_content: 'Hello' };
92
+
93
+ it('still accepts every previously stored token', () => {
94
+ for (const token of ['primary', 'secondary', 'accent', 'muted', 'destructive', 'background']) {
95
+ expect(HeadingBlockSchema.safeParse({ ...base, textColor: token }).success).toBe(true);
96
+ }
97
+ });
98
+
99
+ it('accepts custom colours from the picker', () => {
100
+ for (const color of ['#FF8800', '#f80', '#FF880080', 'rgb(1,2,3)', 'rgba(1,2,3,0.5)', 'hsl(28, 100%, 50%)']) {
101
+ expect(HeadingBlockSchema.safeParse({ ...base, textColor: color }).success).toBe(true);
102
+ }
103
+ });
104
+
105
+ it('rejects values that are neither a token nor a colour', () => {
106
+ for (const bad of ['nonsense', 'url(x)', 'red', '']) {
107
+ expect(HeadingBlockSchema.safeParse({ ...base, textColor: bad }).success).toBe(false);
108
+ }
109
+ });
110
+
111
+ it('stays optional', () => {
112
+ expect(HeadingBlockSchema.safeParse(base).success).toBe(true);
113
+ });
114
+ });