@glw907/cairn-cms 0.8.0 → 0.10.0

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 (72) hide show
  1. package/dist/components/ComponentForm.svelte +178 -0
  2. package/dist/components/ComponentForm.svelte.d.ts +20 -0
  3. package/dist/components/ComponentForm.svelte.d.ts.map +1 -0
  4. package/dist/components/ComponentInsertDialog.svelte +92 -0
  5. package/dist/components/ComponentInsertDialog.svelte.d.ts +20 -0
  6. package/dist/components/ComponentInsertDialog.svelte.d.ts.map +1 -0
  7. package/dist/components/EditPage.svelte +9 -8
  8. package/dist/components/EditPage.svelte.d.ts +4 -3
  9. package/dist/components/EditPage.svelte.d.ts.map +1 -1
  10. package/dist/components/EditorToolbar.svelte +61 -0
  11. package/dist/components/EditorToolbar.svelte.d.ts +15 -0
  12. package/dist/components/EditorToolbar.svelte.d.ts.map +1 -0
  13. package/dist/components/IconPicker.svelte +51 -0
  14. package/dist/components/IconPicker.svelte.d.ts +20 -0
  15. package/dist/components/IconPicker.svelte.d.ts.map +1 -0
  16. package/dist/components/MarkdownEditor.svelte +96 -57
  17. package/dist/components/MarkdownEditor.svelte.d.ts +5 -6
  18. package/dist/components/MarkdownEditor.svelte.d.ts.map +1 -1
  19. package/dist/components/index.d.ts +3 -1
  20. package/dist/components/index.d.ts.map +1 -1
  21. package/dist/components/index.js +3 -1
  22. package/dist/components/markdown-format.d.ts +13 -0
  23. package/dist/components/markdown-format.d.ts.map +1 -0
  24. package/dist/components/markdown-format.js +23 -0
  25. package/dist/content/compose.d.ts.map +1 -1
  26. package/dist/content/compose.js +1 -0
  27. package/dist/content/types.d.ts +5 -0
  28. package/dist/content/types.d.ts.map +1 -1
  29. package/dist/index.d.ts +8 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +5 -1
  32. package/dist/render/component-grammar.d.ts +10 -0
  33. package/dist/render/component-grammar.d.ts.map +1 -0
  34. package/dist/render/component-grammar.js +140 -0
  35. package/dist/render/component-insert.d.ts +14 -0
  36. package/dist/render/component-insert.d.ts.map +1 -0
  37. package/dist/render/component-insert.js +9 -0
  38. package/dist/render/component-reference.d.ts +11 -0
  39. package/dist/render/component-reference.d.ts.map +1 -0
  40. package/dist/render/component-reference.js +34 -0
  41. package/dist/render/component-validate.d.ts +10 -0
  42. package/dist/render/component-validate.d.ts.map +1 -0
  43. package/dist/render/component-validate.js +30 -0
  44. package/dist/render/pipeline.d.ts +1 -1
  45. package/dist/render/pipeline.js +1 -1
  46. package/dist/render/registry.d.ts +45 -1
  47. package/dist/render/registry.d.ts.map +1 -1
  48. package/dist/render/registry.js +13 -0
  49. package/dist/render/sanitize.js +2 -2
  50. package/package.json +8 -3
  51. package/src/lib/components/ComponentForm.svelte +178 -0
  52. package/src/lib/components/ComponentInsertDialog.svelte +92 -0
  53. package/src/lib/components/EditPage.svelte +9 -8
  54. package/src/lib/components/EditorToolbar.svelte +61 -0
  55. package/src/lib/components/IconPicker.svelte +51 -0
  56. package/src/lib/components/MarkdownEditor.svelte +96 -57
  57. package/src/lib/components/index.ts +3 -1
  58. package/src/lib/components/markdown-format.ts +39 -0
  59. package/src/lib/content/compose.ts +1 -0
  60. package/src/lib/content/types.ts +5 -0
  61. package/src/lib/index.ts +16 -2
  62. package/src/lib/render/component-grammar.ts +167 -0
  63. package/src/lib/render/component-insert.ts +15 -0
  64. package/src/lib/render/component-reference.ts +38 -0
  65. package/src/lib/render/component-validate.ts +36 -0
  66. package/src/lib/render/pipeline.ts +1 -1
  67. package/src/lib/render/registry.ts +61 -1
  68. package/src/lib/render/sanitize.ts +2 -2
  69. package/dist/components/ComponentPalette.svelte +0 -50
  70. package/dist/components/ComponentPalette.svelte.d.ts +0 -16
  71. package/dist/components/ComponentPalette.svelte.d.ts.map +0 -1
  72. package/src/lib/components/ComponentPalette.svelte +0 -50
@@ -0,0 +1,34 @@
1
+ import { serializeComponent } from './component-grammar.js';
2
+ import { emptyValues } from './registry.js';
3
+ /** Build a self-contained markdown reference (the llms-full.txt shape) for a component registry, for
4
+ * authors and for pointing an LLM at one curated file. */
5
+ export function generateComponentReference(registry, opts) {
6
+ const sections = registry.defs.map((def) => componentSection(def));
7
+ return `# ${opts.title}\n\n> ${opts.summary}\n\n${sections.join('\n\n')}\n`;
8
+ }
9
+ function componentSection(def) {
10
+ const lines = [`## ${def.label} (\`:::${def.name}\`)`, '', def.description ?? ''];
11
+ if (def.use)
12
+ lines.push('', `**When to use:** ${def.use}`);
13
+ lines.push('', '```', serializeComponent(def, exampleValues(def)), '```');
14
+ return lines.join('\n');
15
+ }
16
+ /** Seed example values that show every declared field: an ellipsis for strings, one sample list item. */
17
+ function exampleValues(def) {
18
+ const values = emptyValues(def);
19
+ for (const field of def.attributes ?? []) {
20
+ if (field.type === 'boolean')
21
+ values.attributes[field.key] = false;
22
+ else
23
+ values.attributes[field.key] = field.options?.[0] ?? '…';
24
+ }
25
+ for (const slot of def.slots ?? []) {
26
+ if (slot.kind === 'repeatable')
27
+ values.slots[slot.name] = ['…'];
28
+ else if (slot.name === 'title')
29
+ values.slots[slot.name] = 'Title';
30
+ else
31
+ values.slots[slot.name] = '…';
32
+ }
33
+ return values;
34
+ }
@@ -0,0 +1,10 @@
1
+ import type { ComponentDef } from './registry.js';
2
+ /** A validation verdict: ok, or field-keyed error messages. */
3
+ export type ComponentValidation = {
4
+ ok: true;
5
+ } | {
6
+ ok: false;
7
+ errors: Record<string, string>;
8
+ };
9
+ export declare function validateComponent(markdown: string, def: ComponentDef): Promise<ComponentValidation>;
10
+ //# sourceMappingURL=component-validate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"component-validate.d.ts","sourceRoot":"","sources":["../../src/lib/render/component-validate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,+DAA+D;AAC/D,MAAM,MAAM,mBAAmB,GAAG;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,GAAG;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,CAAC;AAE/F,wBAAsB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,YAAY,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA6BzG"}
@@ -0,0 +1,30 @@
1
+ import { parseComponent, parseRawAttributeKeys } from './component-grammar.js';
2
+ export async function validateComponent(markdown, def) {
3
+ const values = await parseComponent(markdown, def);
4
+ const errors = {};
5
+ const declared = new Set((def.attributes ?? []).map((f) => f.key));
6
+ for (const field of def.attributes ?? []) {
7
+ const v = values.attributes[field.key];
8
+ const filled = field.type === 'boolean' ? true : typeof v === 'string' && v !== '';
9
+ if (field.required && !filled) {
10
+ errors[field.key] = `${field.label} is required.`;
11
+ continue;
12
+ }
13
+ if (field.type === 'select' && typeof v === 'string' && v !== '' && !(field.options ?? []).includes(v)) {
14
+ errors[field.key] = `${field.label} must be one of: ${(field.options ?? []).join(', ')}.`;
15
+ }
16
+ }
17
+ for (const key of parseRawAttributeKeys(markdown, def)) {
18
+ if (!declared.has(key))
19
+ errors[key] = `Unknown attribute "${key}".`;
20
+ }
21
+ for (const slot of def.slots ?? []) {
22
+ if (!slot.required)
23
+ continue;
24
+ const v = values.slots[slot.name];
25
+ const filled = Array.isArray(v) ? v.length > 0 : typeof v === 'string' && v !== '';
26
+ if (!filled)
27
+ errors[slot.name] = `${slot.label} is required.`;
28
+ }
29
+ return Object.keys(errors).length ? { ok: false, errors } : { ok: true };
30
+ }
@@ -8,7 +8,7 @@ export interface RendererOptions {
8
8
  }
9
9
  /** Compose a site's render pipeline from its component registry: directive syntax to
10
10
  * stamped markers to registry-built hast. Returns `renderMarkdown` plus the remark/
11
- * rehype plugin arrays (so the Carta editor preview can reuse the exact same set). */
11
+ * rehype plugin arrays (so the admin editor preview can reuse the exact same set). */
12
12
  export declare function createRenderer(registry: ComponentRegistry, options?: RendererOptions): {
13
13
  remarkPlugins: PluggableList;
14
14
  rehypePlugins: PluggableList;
@@ -10,7 +10,7 @@ import { remarkDirectiveStamp } from './remark-directives.js';
10
10
  import { rehypeDispatch } from './rehype-dispatch.js';
11
11
  /** Compose a site's render pipeline from its component registry: directive syntax to
12
12
  * stamped markers to registry-built hast. Returns `renderMarkdown` plus the remark/
13
- * rehype plugin arrays (so the Carta editor preview can reuse the exact same set). */
13
+ * rehype plugin arrays (so the admin editor preview can reuse the exact same set). */
14
14
  export function createRenderer(registry, options = {}) {
15
15
  const remarkPlugins = [remarkDirective, [remarkDirectiveStamp, registry]];
16
16
  const rehypePlugins = [rehypeRaw, [rehypeDispatch, registry, options.stagger], rehypeSlug];
@@ -1,4 +1,33 @@
1
1
  import type { Element } from 'hast';
2
+ /** The input types a component attribute or repeatable item field can take. */
3
+ export type FieldType = 'text' | 'select' | 'icon' | 'boolean';
4
+ /** One `{key="value"}` attribute on a component directive, or one field of a repeatable item. */
5
+ export interface AttributeField {
6
+ /** The attribute name as it appears in the directive, e.g. `icon`. */
7
+ key: string;
8
+ /** The form label. */
9
+ label: string;
10
+ type: FieldType;
11
+ required?: boolean;
12
+ /** Initial value; a string for text/select/icon, a boolean for boolean. */
13
+ default?: string | boolean;
14
+ /** Allowed values for `type: 'select'`. */
15
+ options?: string[];
16
+ /** Helper text shown under the field. */
17
+ help?: string;
18
+ }
19
+ export type SlotKind = 'markdown' | 'inline' | 'repeatable';
20
+ /** One named content region of a component. The slots named `title` and `body` are special: `title`
21
+ * serializes to the directive `[label]` and `body` to the unmarked content (see the canonical grammar). */
22
+ export interface SlotDef {
23
+ name: string;
24
+ label: string;
25
+ kind: SlotKind;
26
+ required?: boolean;
27
+ help?: string;
28
+ /** For `kind: 'repeatable'`: the fields composing each list item (v1 uses the first field). */
29
+ itemFields?: AttributeField[];
30
+ }
2
31
  /** A site component: how it inserts (editor) and how it renders (rehype). */
3
32
  export interface ComponentDef {
4
33
  /** Directive name, e.g. 'card' (matches `:::card`). */
@@ -8,13 +37,19 @@ export interface ComponentDef {
8
37
  /** Palette description. */
9
38
  description: string;
10
39
  /** Markdown scaffold inserted at the cursor by the editor palette. */
11
- insertTemplate: string;
40
+ insertTemplate?: string;
12
41
  /** Build the final hast element from the stamped directive element. The engine
13
42
  * stamps the entrance-stagger ordinal (`data-rise`) on the top-level result, so a
14
43
  * build fn stays free of any motion concern. */
15
44
  build: (node: Element) => Element;
16
45
  /** Optional role-to-default-icon, e.g. `{ caution: 'warning' }`. */
17
46
  defaultIconByRole?: Record<string, string>;
47
+ /** One line on when to reach for this component; feeds the picker and the reference file. */
48
+ use?: string;
49
+ /** The `{key="value"}` attributes this component accepts. */
50
+ attributes?: AttributeField[];
51
+ /** The named content regions this component accepts. */
52
+ slots?: SlotDef[];
18
53
  }
19
54
  export interface ComponentRegistry {
20
55
  defs: ComponentDef[];
@@ -29,4 +64,13 @@ export interface ComponentRegistry {
29
64
  export declare function defineRegistry({ components }: {
30
65
  components: ComponentDef[];
31
66
  }): ComponentRegistry;
67
+ /** Guided-form values for one component: attribute values keyed by attribute key, slot values keyed
68
+ * by slot name (a string, or a string list for a repeatable slot). */
69
+ export interface ComponentValues {
70
+ attributes: Record<string, string | boolean>;
71
+ slots: Record<string, string | string[]>;
72
+ }
73
+ /** Seed an empty {@link ComponentValues} from a component's schema: attribute defaults (or '' / false)
74
+ * and empty slot values ([] for repeatable, '' otherwise). */
75
+ export declare function emptyValues(def: ComponentDef): ComponentValues;
32
76
  //# sourceMappingURL=registry.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/lib/render/registry.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAEpC,6EAA6E;AAC7E,MAAM,WAAW,YAAY;IAC3B,uDAAuD;IACvD,IAAI,EAAE,MAAM,CAAC;IACb,qBAAqB;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,2BAA2B;IAC3B,WA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,YAAY,EAAE,CAAA;CAAE,GAAG,iBAAiB,CAQhG;AAED;uEACuE;AACvE,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC;IAC7C,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC,CAAC;CAC1C;AAED;+DAC+D;AAC/D,wBAAgB,WAAW,CAAC,GAAG,EAAE,YAAY,GAAG,eAAe,CAU9D"}
@@ -11,3 +11,16 @@ export function defineRegistry({ components }) {
11
11
  defaultIcon: (name, role) => (role ? byName.get(name)?.defaultIconByRole?.[role] : undefined),
12
12
  };
13
13
  }
14
+ /** Seed an empty {@link ComponentValues} from a component's schema: attribute defaults (or '' / false)
15
+ * and empty slot values ([] for repeatable, '' otherwise). */
16
+ export function emptyValues(def) {
17
+ const attributes = {};
18
+ for (const field of def.attributes ?? []) {
19
+ attributes[field.key] = field.default ?? (field.type === 'boolean' ? false : '');
20
+ }
21
+ const slots = {};
22
+ for (const slot of def.slots ?? []) {
23
+ slots[slot.name] = slot.kind === 'repeatable' ? [] : '';
24
+ }
25
+ return { attributes, slots };
26
+ }
@@ -1,5 +1,5 @@
1
- // The live preview's sanitize floor. Carta runs with `sanitizer: false` behind the MarkdownEditor
2
- // seam, so the admin preview pane is the one barrier between editor-authored markdown and the DOM.
1
+ // The live preview's sanitize floor. The MarkdownEditor edits raw markdown and never sanitizes,
2
+ // so the admin preview pane is the one barrier between editor-authored markdown and the DOM.
3
3
  // DOMPurify needs a DOM, and the preview renders only in the browser after mount, so DOMPurify
4
4
  // loads through a dynamic import: the module never evaluates a DOM library on the Worker, and a
5
5
  // server import of this file pulls in nothing.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@glw907/cairn-cms",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Embedded, magic-link, GitHub-committing CMS for SvelteKit/Cloudflare sites.",
5
5
  "type": "module",
6
6
  "sideEffects": [
@@ -56,13 +56,18 @@
56
56
  ],
57
57
  "peerDependencies": {
58
58
  "@sveltejs/kit": "^2",
59
- "carta-md": "^4.11",
60
59
  "svelte": "^5.0.0"
61
60
  },
62
61
  "dependencies": {
62
+ "@codemirror/commands": "^6.10.3",
63
+ "@codemirror/lang-markdown": "^6.5.0",
64
+ "@codemirror/language": "^6.12.3",
65
+ "@codemirror/state": "^6.6.0",
66
+ "@codemirror/view": "^6.43.0",
63
67
  "@rodrigodagostino/svelte-sortable-list": "^2.1.17",
64
68
  "@types/hast": "^3.0.4",
65
69
  "@types/mdast": "^4.0.4",
70
+ "codemirror": "^6.0.2",
66
71
  "dompurify": "^3.4.7",
67
72
  "gray-matter": "^4",
68
73
  "hastscript": "^9.0.1",
@@ -74,6 +79,7 @@
74
79
  "remark-gfm": "^4",
75
80
  "remark-parse": "^11.0.0",
76
81
  "remark-rehype": "^11.1.2",
82
+ "remark-stringify": "^11.0.0",
77
83
  "unified": "^11.0.5",
78
84
  "unist-util-visit": "^5.1.0",
79
85
  "yaml": "^2"
@@ -88,7 +94,6 @@
88
94
  "@types/node": "^22.19.19",
89
95
  "@vitest/browser": "^4.1.7",
90
96
  "@vitest/browser-playwright": "^4.1.7",
91
- "carta-md": "^4.11",
92
97
  "playwright": "^1.60.0",
93
98
  "publint": "^0.3.21",
94
99
  "svelte": "^5.55",
@@ -0,0 +1,178 @@
1
+ <!--
2
+ @component
3
+ The schema-driven fill form for one component. It holds the working ComponentValues, seeded from
4
+ emptyValues(def), and renders attribute fields and the title/body and other non-repeatable slots.
5
+ Submit (Task 6) serializes and validates through buildComponentInsert and calls onInsert with the
6
+ markdown. Back returns to the picker. This is not a nested HTML form; Insert calls a callback.
7
+ -->
8
+ <script lang="ts">
9
+ import { untrack } from 'svelte';
10
+ import { emptyValues, type ComponentDef } from '../render/registry.js';
11
+ import { buildComponentInsert } from '../render/component-insert.js';
12
+ import type { IconSet } from '../render/glyph.js';
13
+ import IconPicker from './IconPicker.svelte';
14
+
15
+ interface Props {
16
+ def: ComponentDef;
17
+ icons?: IconSet;
18
+ /** Called with the serialized markdown when the form validates. */
19
+ onInsert: (markdown: string) => void;
20
+ /** Return to the picker. */
21
+ onBack: () => void;
22
+ }
23
+
24
+ let { def, icons, onInsert, onBack }: Props = $props();
25
+
26
+ // Working values, seeded once from the schema. $state makes the nested records deeply reactive.
27
+ // untrack marks the seed as a deliberate one-time read of the initial def, not a reactive miss.
28
+ let values = $state(untrack(() => emptyValues(def)));
29
+
30
+ const attributes = $derived(def.attributes ?? []);
31
+ // Non-repeatable slots render here; the repeatable list is handled separately.
32
+ const flatSlots = $derived((def.slots ?? []).filter((s) => s.kind !== 'repeatable'));
33
+ const repeatableSlots = $derived((def.slots ?? []).filter((s) => s.kind === 'repeatable'));
34
+
35
+ // The live $state proxy array for a repeatable slot, so push/splice stay reactive.
36
+ function slotItems(name: string): string[] {
37
+ const v = values.slots[name];
38
+ return Array.isArray(v) ? v : [];
39
+ }
40
+
41
+ // Typed accessors over the unions so explicit value targets stay sound.
42
+ function asString(key: string): string {
43
+ const v = values.attributes[key];
44
+ return typeof v === 'string' ? v : '';
45
+ }
46
+ function asBool(key: string): boolean {
47
+ return values.attributes[key] === true;
48
+ }
49
+ function slotString(name: string): string {
50
+ const v = values.slots[name];
51
+ return typeof v === 'string' ? v : '';
52
+ }
53
+
54
+ // Field-keyed validation errors from the last submit, keyed by attribute key or slot name.
55
+ let errors = $state<Record<string, string>>({});
56
+
57
+ // Serialize and validate through the pure helper. On success clear errors and emit the markdown;
58
+ // on failure keep the field-keyed errors so each field can show its message and insert nothing.
59
+ async function submit() {
60
+ const result = await buildComponentInsert(def, values);
61
+ if (result.ok) {
62
+ errors = {};
63
+ onInsert(result.markdown);
64
+ } else {
65
+ errors = result.errors;
66
+ }
67
+ }
68
+ </script>
69
+
70
+ <div class="flex flex-col gap-3">
71
+ <div class="flex items-center justify-between">
72
+ <h3 class="text-sm font-semibold">{def.label}</h3>
73
+ <button type="button" class="btn btn-ghost btn-xs" onclick={onBack}>Back</button>
74
+ </div>
75
+
76
+ {#each attributes as field (field.key)}
77
+ {#if field.type === 'boolean'}
78
+ <label class="label cursor-pointer justify-start gap-2">
79
+ <input
80
+ class="checkbox checkbox-sm"
81
+ type="checkbox"
82
+ aria-label={field.label}
83
+ aria-invalid={Boolean(errors[field.key])}
84
+ aria-describedby={errors[field.key] ? `err-${field.key}` : undefined}
85
+ checked={asBool(field.key)}
86
+ onchange={(e) => (values.attributes[field.key] = e.currentTarget.checked)}
87
+ />
88
+ <span class="text-sm">{field.label}</span>
89
+ </label>
90
+ {:else if field.type === 'select'}
91
+ <label class="flex flex-col gap-1">
92
+ <span class="text-sm font-medium">{field.label}</span>
93
+ <select
94
+ class="select"
95
+ aria-label={field.label}
96
+ aria-invalid={Boolean(errors[field.key])}
97
+ aria-describedby={errors[field.key] ? `err-${field.key}` : undefined}
98
+ value={asString(field.key)}
99
+ onchange={(e) => (values.attributes[field.key] = e.currentTarget.value)}
100
+ >
101
+ {#if !field.required}<option value="">—</option>{/if}
102
+ {#each field.options ?? [] as opt (opt)}<option value={opt}>{opt}</option>{/each}
103
+ </select>
104
+ </label>
105
+ {:else if field.type === 'icon' && icons}
106
+ <div class="flex flex-col gap-1">
107
+ <span class="text-sm font-medium">{field.label}</span>
108
+ <IconPicker
109
+ {icons}
110
+ value={asString(field.key)}
111
+ required={field.required ?? false}
112
+ onChange={(name) => (values.attributes[field.key] = name)}
113
+ />
114
+ </div>
115
+ {:else}
116
+ <label class="flex flex-col gap-1">
117
+ <span class="text-sm font-medium">{field.label}</span>
118
+ <input
119
+ class="input"
120
+ aria-label={field.label}
121
+ aria-invalid={Boolean(errors[field.key])}
122
+ aria-describedby={errors[field.key] ? `err-${field.key}` : undefined}
123
+ value={asString(field.key)}
124
+ oninput={(e) => (values.attributes[field.key] = e.currentTarget.value)}
125
+ />
126
+ </label>
127
+ {/if}
128
+ {#if errors[field.key]}<span id={`err-${field.key}`} role="alert" class="text-error text-xs">{errors[field.key]}</span>{/if}
129
+ {/each}
130
+
131
+ {#each flatSlots as slot (slot.name)}
132
+ {#if slot.kind === 'markdown'}
133
+ <label class="flex flex-col gap-1">
134
+ <span class="text-sm font-medium">{slot.label}</span>
135
+ <textarea
136
+ class="textarea"
137
+ aria-label={slot.label}
138
+ aria-invalid={Boolean(errors[slot.name])}
139
+ aria-describedby={errors[slot.name] ? `err-${slot.name}` : undefined}
140
+ rows={3}
141
+ value={slotString(slot.name)}
142
+ oninput={(e) => (values.slots[slot.name] = e.currentTarget.value)}
143
+ ></textarea>
144
+ </label>
145
+ {:else}
146
+ <label class="flex flex-col gap-1">
147
+ <span class="text-sm font-medium">{slot.label}</span>
148
+ <input
149
+ class="input"
150
+ aria-label={slot.label}
151
+ aria-invalid={Boolean(errors[slot.name])}
152
+ aria-describedby={errors[slot.name] ? `err-${slot.name}` : undefined}
153
+ value={slotString(slot.name)}
154
+ oninput={(e) => (values.slots[slot.name] = e.currentTarget.value)}
155
+ />
156
+ </label>
157
+ {/if}
158
+ {#if errors[slot.name]}<span id={`err-${slot.name}`} role="alert" class="text-error text-xs">{errors[slot.name]}</span>{/if}
159
+ {/each}
160
+
161
+ {#each repeatableSlots as slot (slot.name)}
162
+ {@const items = slotItems(slot.name)}
163
+ <fieldset class="rounded-box border border-base-300 flex flex-col gap-2 p-2">
164
+ <legend class="text-sm font-medium">{slot.label}</legend>
165
+ <!-- Index key is deliberate: items are bare strings with no stable id, so the value bindings and splice/push are index-based by design. -->
166
+ {#each items as _, i (i)}
167
+ <div class="flex items-center gap-2">
168
+ <input class="input input-sm flex-1" aria-label={`${slot.label} item`} bind:value={items[i]} />
169
+ <button type="button" class="btn btn-ghost btn-sm" aria-label={`Remove item ${i + 1}`} onclick={() => items.splice(i, 1)}>✕</button>
170
+ </div>
171
+ {/each}
172
+ <button type="button" class="btn btn-sm self-start" onclick={() => items.push('')}>Add item</button>
173
+ {#if errors[slot.name]}<span id={`err-${slot.name}`} role="alert" class="text-error text-xs">{errors[slot.name]}</span>{/if}
174
+ </fieldset>
175
+ {/each}
176
+
177
+ <button type="button" class="btn btn-primary btn-sm mt-2" onclick={submit}>Insert</button>
178
+ </div>
@@ -0,0 +1,92 @@
1
+ <!--
2
+ @component
3
+ The Insert control and its modal. The picker lists each actionable component with its description and
4
+ intended use. A component with a schema opens the guided ComponentForm; a template-only component
5
+ inserts directly; a component with neither is not listed. Built on a native <dialog> for focus
6
+ trapping and Escape, following the dropdown's a11y conventions used elsewhere in the admin.
7
+ -->
8
+ <script lang="ts">
9
+ import type { ComponentRegistry, ComponentDef } from '../render/registry.js';
10
+ import type { IconSet } from '../render/glyph.js';
11
+ import ComponentForm from './ComponentForm.svelte';
12
+
13
+ interface Props {
14
+ /** The site's component registry. */
15
+ registry?: ComponentRegistry;
16
+ /** Insert markdown at the editor cursor. */
17
+ insert: (text: string) => void;
18
+ /** The site's icon set, for icon fields. */
19
+ icons?: IconSet;
20
+ }
21
+
22
+ let { registry, insert, icons }: Props = $props();
23
+
24
+ let dialog = $state<HTMLDialogElement | null>(null);
25
+ let picked = $state<ComponentDef | null>(null);
26
+
27
+ function hasSchema(def: ComponentDef): boolean {
28
+ return (def.attributes?.length ?? 0) > 0 || (def.slots?.length ?? 0) > 0;
29
+ }
30
+ function actionable(def: ComponentDef): boolean {
31
+ return hasSchema(def) || Boolean(def.insertTemplate);
32
+ }
33
+
34
+ const defs = $derived((registry?.defs ?? []).filter(actionable));
35
+
36
+ function open() {
37
+ picked = null;
38
+ dialog?.showModal();
39
+ }
40
+ function close() {
41
+ picked = null;
42
+ dialog?.close();
43
+ }
44
+ function choose(def: ComponentDef) {
45
+ if (hasSchema(def)) {
46
+ picked = def;
47
+ } else {
48
+ insert(def.insertTemplate ?? '');
49
+ close();
50
+ }
51
+ }
52
+ function onInsert(markdown: string) {
53
+ insert(markdown);
54
+ close();
55
+ }
56
+ </script>
57
+
58
+ {#if defs.length > 0}
59
+ <button type="button" class="btn btn-sm btn-ghost" aria-haspopup="dialog" aria-label="Insert component" onclick={open}>Insert</button>
60
+
61
+ <dialog class="modal" aria-labelledby="cairn-insert-dialog-title" bind:this={dialog} onclose={() => (picked = null)}>
62
+ <div class="modal-box">
63
+ <div class="mb-3 flex items-center justify-between">
64
+ <h2 id="cairn-insert-dialog-title" class="text-base font-semibold">Insert component</h2>
65
+ <button type="button" class="btn btn-ghost btn-sm" aria-label="Close" onclick={close}>✕</button>
66
+ </div>
67
+
68
+ {#if picked}
69
+ {#key picked}
70
+ <ComponentForm def={picked} {icons} {onInsert} onBack={() => (picked = null)} />
71
+ {/key}
72
+ {:else}
73
+ <ul class="menu w-full">
74
+ {#each defs as def (def.name)}
75
+ <li>
76
+ <button type="button" onclick={() => choose(def)}>
77
+ <span class="flex flex-col items-start">
78
+ <span class="font-medium">{def.label}</span>
79
+ {#if def.description}<span class="text-xs text-[var(--color-muted)]">{def.description}</span>{/if}
80
+ {#if def.use}<span class="text-xs text-[var(--color-muted)]">{def.use}</span>{/if}
81
+ </span>
82
+ </button>
83
+ </li>
84
+ {/each}
85
+ </ul>
86
+ {/if}
87
+ </div>
88
+ <form method="dialog" class="modal-backdrop">
89
+ <button tabindex="-1" aria-label="Close">close</button>
90
+ </form>
91
+ </dialog>
92
+ {/if}
@@ -1,14 +1,15 @@
1
1
  <!--
2
2
  @component
3
- The differentiated editor: the per-concept frontmatter form (from `data.fields`) beside the Carta
3
+ The differentiated editor: the per-concept frontmatter form (from `data.fields`) beside the
4
4
  markdown editor and a live, design-accurate preview. The whole surface is one form posting to the
5
5
  `?/save` action; the preview toggle persists per user in localStorage (spec §7.6).
6
6
  -->
7
7
  <script lang="ts">
8
8
  import { untrack } from 'svelte';
9
9
  import MarkdownEditor from './MarkdownEditor.svelte';
10
- import ComponentPalette from './ComponentPalette.svelte';
10
+ import ComponentInsertDialog from './ComponentInsertDialog.svelte';
11
11
  import type { ComponentRegistry } from '../render/registry.js';
12
+ import type { IconSet } from '../render/glyph.js';
12
13
  import type { EditData } from '../sveltekit/content-routes.js';
13
14
  import type { TextareaField, TagsField, FreeTagsField } from '../content/types.js';
14
15
  import { sanitizePreviewHtml } from '../render/sanitize.js';
@@ -18,13 +19,13 @@ markdown editor and a live, design-accurate preview. The whole surface is one fo
18
19
  data: EditData & { siteName: string };
19
20
  /** The site's component registry, for the insert palette. */
20
21
  registry?: ComponentRegistry;
21
- /** Carta preview plugins from the adapter, for the design-accurate preview. */
22
- preview?: unknown[];
23
22
  /** The site's design-accurate render pipeline; the preview pane sanitizes its output. */
24
23
  render?: (md: string, opts?: { stagger?: boolean }) => string | Promise<string>;
24
+ /** The site's icon set, for the guided form's icon fields. */
25
+ icons?: IconSet;
25
26
  }
26
27
 
27
- let { data, registry, preview = [], render }: Props = $props();
28
+ let { data, registry, render, icons }: Props = $props();
28
29
 
29
30
  // `body` is local editor state seeded once from the prop; it diverges as the user types.
30
31
  // untrack() captures the initial value without subscribing to future prop changes.
@@ -46,7 +47,7 @@ markdown editor and a live, design-accurate preview. The whole surface is one fo
46
47
  }
47
48
 
48
49
  // Render the design-accurate preview as the body changes, debounced, and sanitize before the DOM.
49
- // The sanitize is the one barrier between editor-authored markdown and the page (Carta is unsanitized).
50
+ // The sanitize is the one barrier between editor-authored markdown and the page (the editor is unsanitized).
50
51
  // previewRun is a plain counter (not reactive state) used as a latest-wins guard: if a slow earlier
51
52
  // async render call resolves after a newer one has started, the stale result is discarded.
52
53
  let previewRun = 0;
@@ -78,7 +79,7 @@ markdown editor and a live, design-accurate preview. The whole surface is one fo
78
79
  <p class="text-xs text-[var(--color-muted)]">{data.label}: {data.id}</p>
79
80
  </div>
80
81
  <div class="flex items-center gap-2">
81
- <ComponentPalette {registry} {insert} />
82
+ <ComponentInsertDialog {registry} {insert} {icons} />
82
83
  <button
83
84
  type="button"
84
85
  class="btn btn-sm btn-ghost"
@@ -103,7 +104,7 @@ markdown editor and a live, design-accurate preview. The whole surface is one fo
103
104
 
104
105
  <div class="lg:order-1">
105
106
  <div class="rounded-box border border-base-300 bg-base-100 overflow-hidden">
106
- <MarkdownEditor bind:value={body} name="body" plugins={preview} registerInsert={(fn) => (insert = fn)} />
107
+ <MarkdownEditor bind:value={body} name="body" registerInsert={(fn) => (insert = fn)} />
107
108
  </div>
108
109
  {#if showPreview}
109
110
  <section
@@ -0,0 +1,61 @@
1
+ <!--
2
+ @component
3
+ The editor's formatting toolbar: bold, italic, heading, link, bulleted list, quote, code. Each button
4
+ asks the host to apply a markdown transform to the current selection. Carta supplied this row before;
5
+ cairn owns it now so the edit surface stays swappable. The glyphs are stroke SVG icons in the admin's
6
+ house style (24x24 viewBox, `currentColor`, round caps), so the row matches the rest of the surface.
7
+ -->
8
+ <script lang="ts">
9
+ import type { FormatKind } from './markdown-format.js';
10
+
11
+ interface Props {
12
+ /** Apply a markdown transform to the editor's current selection. */
13
+ format: (kind: FormatKind) => void;
14
+ }
15
+
16
+ let { format }: Props = $props();
17
+
18
+ // Each icon is a set of stroke `<path>` d-strings rendered into the shared 24x24 svg below, so the
19
+ // markup stays declarative (no per-icon raw html). Paths follow the house outline style.
20
+ const buttons: { kind: FormatKind; label: string; paths: string[] }[] = [
21
+ { kind: 'bold', label: 'Bold', paths: ['M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8'] },
22
+ { kind: 'italic', label: 'Italic', paths: ['M19 4h-9', 'M14 20H5', 'M15 4 9 20'] },
23
+ { kind: 'heading', label: 'Heading', paths: ['M6 4v16', 'M18 4v16', 'M6 12h12'] },
24
+ {
25
+ kind: 'link',
26
+ label: 'Link',
27
+ paths: [
28
+ 'M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71',
29
+ 'M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71',
30
+ ],
31
+ },
32
+ { kind: 'ul', label: 'Bulleted list', paths: ['M8 6h13', 'M8 12h13', 'M8 18h13', 'M3 6h.01', 'M3 12h.01', 'M3 18h.01'] },
33
+ {
34
+ kind: 'quote',
35
+ label: 'Quote',
36
+ paths: [
37
+ 'M16 3a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2 1 1 0 0 1 1 1v1a2 2 0 0 1-2 2 1 1 0 0 0-1 1 1 1 0 0 0 1 1 4 4 0 0 0 4-4V5a2 2 0 0 0-2-2z',
38
+ 'M5 3a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2 1 1 0 0 1 1 1v1a2 2 0 0 1-2 2 1 1 0 0 0-1 1 1 1 0 0 0 1 1 4 4 0 0 0 4-4V5a2 2 0 0 0-2-2z',
39
+ ],
40
+ },
41
+ { kind: 'code', label: 'Code', paths: ['M16 18l6-6-6-6', 'M8 6l-6 6 6 6'] },
42
+ ];
43
+ </script>
44
+
45
+ <div class="border-base-300 bg-base-200 flex gap-1 border-b p-1" role="toolbar" aria-label="Formatting">
46
+ {#each buttons as button (button.kind)}
47
+ <button
48
+ type="button"
49
+ class="btn btn-ghost btn-sm btn-square"
50
+ aria-label={button.label}
51
+ title={button.label}
52
+ onclick={() => format(button.kind)}
53
+ >
54
+ <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
55
+ {#each button.paths as d (d)}
56
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d={d} />
57
+ {/each}
58
+ </svg>
59
+ </button>
60
+ {/each}
61
+ </div>