uncial 2.0.3 → 4.0.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 (51) hide show
  1. package/README.md +6 -1
  2. package/dist/core/defineBlock.js +5 -2
  3. package/dist/core/index.d.ts +2 -2
  4. package/dist/core/index.js +1 -1
  5. package/dist/core/registry.d.ts +8 -1
  6. package/dist/core/registry.js +27 -0
  7. package/dist/core/types.d.ts +21 -1
  8. package/dist/editor/AttributeFieldControl.svelte +127 -3
  9. package/dist/editor/AttributeFieldControl.svelte.d.ts +4 -0
  10. package/dist/editor/BlockAttributesPanel.svelte +12 -3
  11. package/dist/editor/BlockAttributesPanel.svelte.d.ts +5 -0
  12. package/dist/editor/BlockNodeView.svelte +2 -1
  13. package/dist/editor/DocumentMetaPanel.svelte +5 -1
  14. package/dist/editor/Editor.svelte +20 -8
  15. package/dist/editor/Editor.svelte.d.ts +3 -0
  16. package/dist/editor/ImagePicker.svelte +85 -0
  17. package/dist/editor/ImagePicker.svelte.d.ts +13 -0
  18. package/dist/editor/bindEditor.d.ts +3 -0
  19. package/dist/editor/bindEditor.js +1 -1
  20. package/dist/editor/imageSource.d.ts +12 -0
  21. package/dist/editor/imageSource.js +1 -0
  22. package/dist/editor/index.d.ts +1 -0
  23. package/dist/editor/metaController.js +0 -5
  24. package/dist/render/Renderer.svelte +12 -2
  25. package/dist/render/Renderer.svelte.d.ts +2 -1
  26. package/dist/render/RichContent.svelte +4 -2
  27. package/dist/render/RichContent.svelte.d.ts +2 -1
  28. package/dist/render/RichNode.svelte +36 -17
  29. package/dist/render/RichNode.svelte.d.ts +2 -1
  30. package/dist/render/SuperscriptMarkFixture.svelte +7 -0
  31. package/dist/render/SuperscriptMarkFixture.svelte.d.ts +4 -0
  32. package/dist/render/Tabs.svelte +1 -1
  33. package/dist/render/index.d.ts +1 -0
  34. package/dist/render/index.js +1 -0
  35. package/dist/render/resolveImageSrc.d.ts +6 -0
  36. package/dist/render/resolveImageSrc.js +10 -0
  37. package/dist/render/tabSelection.svelte.js +2 -0
  38. package/dist/shared/ImageBlockFixture.svelte +29 -0
  39. package/dist/shared/ImageBlockFixture.svelte.d.ts +11 -0
  40. package/dist/shared/imagePreviews.d.ts +21 -0
  41. package/dist/shared/imagePreviews.js +59 -0
  42. package/dist/shared/tiptap.d.ts +2 -1
  43. package/dist/shared/tiptap.js +16 -13
  44. package/dist/styles/base.css +89 -9
  45. package/dist/ui/ThemeSwitcher.svelte +1 -1
  46. package/dist/ui/theme.svelte.js +1 -1
  47. package/dist/web-components/UncialEditor.svelte +4 -0
  48. package/dist/web-components/UncialEditor.svelte.d.ts +2 -1
  49. package/dist/web-components/index.d.ts +4 -1
  50. package/dist/web-components/index.js +6 -0
  51. package/package.json +3 -3
@@ -0,0 +1,13 @@
1
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
2
+ interface Props {
3
+ browse: () => Promise<string[]>;
4
+ thumbnail?: (src: string) => string;
5
+ imagePreviews?: ImagePreviews;
6
+ current: string;
7
+ onPick: (value: string) => void;
8
+ }
9
+ declare const ImagePicker: import("svelte").Component<Props, {
10
+ open: () => void;
11
+ }, "">;
12
+ type ImagePicker = ReturnType<typeof ImagePicker>;
13
+ export default ImagePicker;
@@ -1,6 +1,7 @@
1
1
  import { Editor as TiptapEditor, type AnyExtension, type JSONContent } from '@tiptap/core';
2
2
  import type { ActionReturn } from 'svelte/action';
3
3
  import type { BlockDefinition, BlockRegistry, ContentSchema, ValidationIssue } from '../core/types.js';
4
+ import type { ImagePreviews } from '../shared/imagePreviews.js';
4
5
  import { type BlockAttributesController } from './attributesController.js';
5
6
  export interface BindEditorOptions {
6
7
  blocks?: BlockRegistry | BlockDefinition[];
@@ -16,6 +17,8 @@ export interface BindEditorOptions {
16
17
  * gutter label opens it.
17
18
  */
18
19
  autoOpenAttributes?: boolean;
20
+ /** Previews the canvas shows in place of just-uploaded `input: 'image'` values. */
21
+ imagePreviews?: ImagePreviews;
19
22
  onIssue?: (issue: ValidationIssue) => void;
20
23
  onChange?: (json: JSONContent) => void;
21
24
  onMetaChange?: (meta: Record<string, unknown>) => void;
@@ -159,7 +159,7 @@ export function bindEditor(node, initialOptions = {}) {
159
159
  }
160
160
  }
161
161
  function createEditor() {
162
- const extensions = createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? []);
162
+ const extensions = createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? [], options.imagePreviews);
163
163
  const initialContent = normalizeAndValidate(options.json ?? emptyDocument());
164
164
  rememberReadOnlyAttrs(initialContent);
165
165
  const sanitized = sanitizeForEditorSchema(toEditorDocument(initialContent), getSchema(extensions));
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Where an `input: 'image'` attribute's values come from. The field offers
3
+ * only the actions the host supplies.
4
+ */
5
+ export interface ImageSource {
6
+ /** Commit a file and answer the value to store. */
7
+ upload?: (file: File) => Promise<string>;
8
+ /** Values of existing images to choose from. */
9
+ browse?: () => Promise<string[]>;
10
+ /** A displayable URL for a stored value; identity when absent. */
11
+ thumbnail?: (src: string) => string;
12
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -10,6 +10,7 @@ export { createBlockAttributesController, createInitialState } from './attribute
10
10
  export { createDocumentMetaController } from './metaController.js';
11
11
  export { CHOOSE_ATTRIBUTE_EVENT } from './chooseAttribute.js';
12
12
  export type { ChooseAttributeRequest } from './chooseAttribute.js';
13
+ export type { ImageSource } from './imageSource.js';
13
14
  export { builtinToolbarFeatures, defaultToolbarFeatures, resolveToolbarFeatures } from './toolbarFeatures.js';
14
15
  export type { BindEditorOptions } from './bindEditor.js';
15
16
  export type { ToolbarFeature, ToolbarFeatureContext, ToolbarFeatureId, ToolbarFeatureSelection } from './toolbarFeatures.js';
@@ -1,10 +1,5 @@
1
1
  import { get, writable } from 'svelte/store';
2
2
  import { parseMetaDraftValues, toMetaDraftValues, validateMeta } from '../core/meta.js';
3
- const INITIAL_STATE = {
4
- draft: {},
5
- errors: {},
6
- dirty: false
7
- };
8
3
  function errorsFromIssues(issues) {
9
4
  const errors = {};
10
5
  for (const issue of issues) {
@@ -7,10 +7,12 @@
7
7
  BlockDefinition,
8
8
  BlockRegistry,
9
9
  ContentSchema,
10
+ MarkDefinition,
11
+ MarkRegistry,
10
12
  ValidationIssue
11
13
  } from '../core/types.js';
12
14
  import type { PMDoc, PMNode } from '../shared/document.js';
13
- import { resolveRegistry } from '../core/registry.js';
15
+ import { resolveMarkRegistry, resolveRegistry } from '../core/registry.js';
14
16
  import { emptyDocument } from '../shared/content.js';
15
17
  import { SVELTE_RUNTIME_ID } from '../shared/runtimeId.js';
16
18
  import RichContent from './RichContent.svelte';
@@ -18,6 +20,7 @@
18
20
  interface Props {
19
21
  content?: JSONContent;
20
22
  blocks?: BlockRegistry | BlockDefinition[];
23
+ marks?: MarkRegistry | MarkDefinition[];
21
24
  schema?: ContentSchema;
22
25
  meta?: Snippet<[Record<string, unknown> | undefined]>;
23
26
  onIssue?: (issue: ValidationIssue) => void;
@@ -26,6 +29,7 @@
26
29
  let {
27
30
  content = emptyDocument(),
28
31
  blocks = [],
32
+ marks = [],
29
33
  schema = undefined,
30
34
  meta: metaSnippet = undefined,
31
35
  onIssue
@@ -39,6 +43,7 @@
39
43
  }
40
44
  return resolved;
41
45
  });
46
+ const markRegistry = $derived(resolveMarkRegistry(marks));
42
47
  const normalizedContent = $derived(
43
48
  normalizeDocument(content as Partial<PMDoc>, registry, schema)
44
49
  );
@@ -52,6 +57,11 @@
52
57
  <div class="uncial-renderer">
53
58
  {@render metaSnippet?.(normalizedContent.meta)}
54
59
  <div class="uncial-content uncial-rich-content">
55
- <RichContent nodes={(normalizedContent.content ?? []) as PMNode[]} {registry} {schema} />
60
+ <RichContent
61
+ nodes={(normalizedContent.content ?? []) as PMNode[]}
62
+ {registry}
63
+ {markRegistry}
64
+ {schema}
65
+ />
56
66
  </div>
57
67
  </div>
@@ -1,9 +1,10 @@
1
1
  import type { JSONContent } from '@tiptap/core';
2
2
  import type { Snippet } from 'svelte';
3
- import type { BlockDefinition, BlockRegistry, ContentSchema, ValidationIssue } from '../core/types.js';
3
+ import type { BlockDefinition, BlockRegistry, ContentSchema, MarkDefinition, MarkRegistry, ValidationIssue } from '../core/types.js';
4
4
  interface Props {
5
5
  content?: JSONContent;
6
6
  blocks?: BlockRegistry | BlockDefinition[];
7
+ marks?: MarkRegistry | MarkDefinition[];
7
8
  schema?: ContentSchema;
8
9
  meta?: Snippet<[Record<string, unknown> | undefined]>;
9
10
  onIssue?: (issue: ValidationIssue) => void;
@@ -1,11 +1,12 @@
1
1
  <script lang="ts">
2
- import type { BlockRegistry, ContentSchema } from '../core/types.js';
2
+ import type { BlockRegistry, ContentSchema, MarkRegistry } from '../core/types.js';
3
3
  import type { PMNode } from '../shared/document.js';
4
4
  import RichNode from './RichNode.svelte';
5
5
 
6
6
  interface Props {
7
7
  nodes?: PMNode[];
8
8
  registry: BlockRegistry;
9
+ markRegistry?: MarkRegistry;
9
10
  schema?: ContentSchema;
10
11
  tabsGroup?: string;
11
12
  tabsLabels?: string[];
@@ -14,6 +15,7 @@
14
15
  let {
15
16
  nodes = [],
16
17
  registry,
18
+ markRegistry = undefined,
17
19
  schema = undefined,
18
20
  tabsGroup = undefined,
19
21
  tabsLabels = undefined
@@ -34,5 +36,5 @@
34
36
  </script>
35
37
 
36
38
  {#each nodes as node, index (getNodeKey(node, index))}
37
- <RichNode {node} {registry} {schema} {tabsGroup} {tabsLabels} />
39
+ <RichNode {node} {registry} {markRegistry} {schema} {tabsGroup} {tabsLabels} />
38
40
  {/each}
@@ -1,8 +1,9 @@
1
- import type { BlockRegistry, ContentSchema } from '../core/types.js';
1
+ import type { BlockRegistry, ContentSchema, MarkRegistry } from '../core/types.js';
2
2
  import type { PMNode } from '../shared/document.js';
3
3
  interface Props {
4
4
  nodes?: PMNode[];
5
5
  registry: BlockRegistry;
6
+ markRegistry?: MarkRegistry;
6
7
  schema?: ContentSchema;
7
8
  tabsGroup?: string;
8
9
  tabsLabels?: string[];
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
2
  import type { Component } from 'svelte';
3
- import type { BlockRegistry, ContentSchema } from '../core/types.js';
3
+ import type { BlockRegistry, ContentSchema, MarkRegistry } from '../core/types.js';
4
4
  import type { PMMark, PMNode } from '../shared/document.js';
5
5
  import { getCodeLanguageClass, highlightCodeToHtml } from '../shared/syntaxHighlight.js';
6
6
  import RichContent from './RichContent.svelte';
@@ -9,6 +9,7 @@
9
9
  interface Props {
10
10
  node: PMNode;
11
11
  registry: BlockRegistry;
12
+ markRegistry?: MarkRegistry;
12
13
  schema?: ContentSchema;
13
14
  tabsGroup?: string;
14
15
  tabsLabels?: string[];
@@ -17,6 +18,7 @@
17
18
  let {
18
19
  node,
19
20
  registry,
21
+ markRegistry = undefined,
20
22
  schema = undefined,
21
23
  tabsGroup = undefined,
22
24
  tabsLabels = undefined
@@ -73,6 +75,7 @@
73
75
  <RichContent
74
76
  nodes={blockContent}
75
77
  {registry}
78
+ {markRegistry}
76
79
  {schema}
77
80
  tabsGroup={childTabsGroup}
78
81
  tabsLabels={childTabsLabels}
@@ -84,7 +87,11 @@
84
87
  {text}
85
88
  {:else}
86
89
  {@const [mark, ...rest] = marks}
87
- {#if mark.type === 'bold'}
90
+ {@const customMark = markRegistry?.get(mark.type)}
91
+ {#if customMark}
92
+ {@const MarkComponent = customMark.component}
93
+ <MarkComponent {...mark.attrs ?? {}}>{@render renderMarkedText(text, rest)}</MarkComponent>
94
+ {:else if mark.type === 'bold'}
88
95
  <strong>{@render renderMarkedText(text, rest)}</strong>
89
96
  {:else if mark.type === 'italic'}
90
97
  <em>{@render renderMarkedText(text, rest)}</em>
@@ -124,7 +131,7 @@
124
131
  />
125
132
  {:else if node.type === 'paragraph'}
126
133
  <p>
127
- <RichContent nodes={node.content ?? []} {registry} {schema} />
134
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
128
135
  </p>
129
136
  {:else if node.type === 'heading'}
130
137
  {@const level = Math.min(6, Math.max(1, Number(node.attrs?.level ?? 1)))}
@@ -137,53 +144,65 @@
137
144
  ? node.attrs.slug
138
145
  : undefined}
139
146
  {#if level === 1}
140
- <h1 id={anchor}><RichContent nodes={node.content ?? []} {registry} {schema} /></h1>
147
+ <h1 id={anchor}>
148
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
149
+ </h1>
141
150
  {:else if level === 2}
142
- <h2 id={anchor}><RichContent nodes={node.content ?? []} {registry} {schema} /></h2>
151
+ <h2 id={anchor}>
152
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
153
+ </h2>
143
154
  {:else if level === 3}
144
- <h3 id={anchor}><RichContent nodes={node.content ?? []} {registry} {schema} /></h3>
155
+ <h3 id={anchor}>
156
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
157
+ </h3>
145
158
  {:else if level === 4}
146
- <h4 id={anchor}><RichContent nodes={node.content ?? []} {registry} {schema} /></h4>
159
+ <h4 id={anchor}>
160
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
161
+ </h4>
147
162
  {:else if level === 5}
148
- <h5 id={anchor}><RichContent nodes={node.content ?? []} {registry} {schema} /></h5>
163
+ <h5 id={anchor}>
164
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
165
+ </h5>
149
166
  {:else}
150
- <h6 id={anchor}><RichContent nodes={node.content ?? []} {registry} {schema} /></h6>
167
+ <h6 id={anchor}>
168
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
169
+ </h6>
151
170
  {/if}
152
171
  {:else if node.type === 'table'}
153
172
  <div class="uncial-table-scroll">
154
173
  <table>
155
174
  <tbody>
156
- <RichContent nodes={node.content ?? []} {registry} {schema} />
175
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
157
176
  </tbody>
158
177
  </table>
159
178
  </div>
160
179
  {:else if node.type === 'tableRow'}
161
180
  <tr>
162
- <RichContent nodes={node.content ?? []} {registry} {schema} />
181
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
163
182
  </tr>
164
183
  {:else if node.type === 'tableHeader'}
165
184
  <th>
166
- <RichContent nodes={node.content ?? []} {registry} {schema} />
185
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
167
186
  </th>
168
187
  {:else if node.type === 'tableCell'}
169
188
  <td>
170
- <RichContent nodes={node.content ?? []} {registry} {schema} />
189
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
171
190
  </td>
172
191
  {:else if node.type === 'bulletList'}
173
192
  <ul>
174
- <RichContent nodes={node.content ?? []} {registry} {schema} />
193
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
175
194
  </ul>
176
195
  {:else if node.type === 'orderedList'}
177
196
  <ol>
178
- <RichContent nodes={node.content ?? []} {registry} {schema} />
197
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
179
198
  </ol>
180
199
  {:else if node.type === 'listItem'}
181
200
  <li>
182
- <RichContent nodes={node.content ?? []} {registry} {schema} />
201
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
183
202
  </li>
184
203
  {:else if node.type === 'blockquote'}
185
204
  <blockquote>
186
- <RichContent nodes={node.content ?? []} {registry} {schema} />
205
+ <RichContent nodes={node.content ?? []} {registry} {markRegistry} {schema} />
187
206
  </blockquote>
188
207
  {:else if node.type === 'horizontalRule'}
189
208
  <hr />
@@ -1,9 +1,10 @@
1
1
  import type { Component } from 'svelte';
2
- import type { BlockRegistry, ContentSchema } from '../core/types.js';
2
+ import type { BlockRegistry, ContentSchema, MarkRegistry } from '../core/types.js';
3
3
  import type { PMNode } from '../shared/document.js';
4
4
  interface Props {
5
5
  node: PMNode;
6
6
  registry: BlockRegistry;
7
+ markRegistry?: MarkRegistry;
7
8
  schema?: ContentSchema;
8
9
  tabsGroup?: string;
9
10
  tabsLabels?: string[];
@@ -0,0 +1,7 @@
1
+ <script lang="ts">
2
+ import type { MarkComponentProps } from '../core/types.js';
3
+
4
+ let { children }: Pick<MarkComponentProps, 'children'> = $props();
5
+ </script>
6
+
7
+ <sup>{@render children()}</sup>
@@ -0,0 +1,4 @@
1
+ import type { MarkComponentProps } from '../core/types.js';
2
+ declare const SuperscriptMarkFixture: import("svelte").Component<Pick<MarkComponentProps, "children">, {}, "">;
3
+ type SuperscriptMarkFixture = ReturnType<typeof SuperscriptMarkFixture>;
4
+ export default SuperscriptMarkFixture;
@@ -39,7 +39,7 @@
39
39
 
40
40
  <section class="uncial-tabs">
41
41
  <div role="tablist" aria-label={`${group} tabs`}>
42
- {#each tabs as tab}
42
+ {#each tabs as tab (tab)}
43
43
  <button
44
44
  type="button"
45
45
  role="tab"
@@ -3,3 +3,4 @@ export { default as RichText } from './RichText.svelte';
3
3
  export { default as Tab } from './Tab.svelte';
4
4
  export { default as Tabs } from './Tabs.svelte';
5
5
  export { getCodeLanguageClass, highlightCodeToHtml } from '../shared/syntaxHighlight.js';
6
+ export { resolveImageSrc } from './resolveImageSrc.js';
@@ -6,3 +6,4 @@ export { default as Tabs } from './Tabs.svelte';
6
6
  // a document — has to read the same as a fence the renderer produced, so the
7
7
  // highlighter the renderer uses is part of the render surface.
8
8
  export { getCodeLanguageClass, highlightCodeToHtml } from '../shared/syntaxHighlight.js';
9
+ export { resolveImageSrc } from './resolveImageSrc.js';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Applies a SvelteKit-style `base` (`''` or `/sub`) to a root-relative image `src`.
3
+ * Anything else — `blob:` editor previews, `data:` URIs, protocol-relative and
4
+ * absolute URLs, relative paths — is already displayable and passes through.
5
+ */
6
+ export declare function resolveImageSrc(src: string, base: string): string;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Applies a SvelteKit-style `base` (`''` or `/sub`) to a root-relative image `src`.
3
+ * Anything else — `blob:` editor previews, `data:` URIs, protocol-relative and
4
+ * absolute URLs, relative paths — is already displayable and passes through.
5
+ */
6
+ export function resolveImageSrc(src, base) {
7
+ if (!src.startsWith('/') || src.startsWith('//'))
8
+ return src;
9
+ return base.replace(/\/+$/, '') + src;
10
+ }
@@ -1,4 +1,6 @@
1
1
  const STORAGE_PREFIX = 'uncial-tabs:';
2
+ // A lookup cache; reactivity lives on each TabSelection, not on the map.
3
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity
2
4
  const selections = new Map();
3
5
  class TabSelection {
4
6
  value = $state('');
@@ -0,0 +1,29 @@
1
+ <script lang="ts">
2
+ // Test fixture: a block that renders exactly the image values it is handed.
3
+ interface Props {
4
+ src?: string;
5
+ items?: Array<{ image?: string; caption?: string }>;
6
+ updateAttributes?: (attrs: Record<string, unknown>) => void;
7
+ }
8
+
9
+ let { src = '', items = [], updateAttributes }: Props = $props();
10
+ </script>
11
+
12
+ <div data-testid="image-block-fixture">
13
+ {#if src}
14
+ <img data-testid="canvas-image" {src} alt="" />
15
+ {/if}
16
+ {#each items as item, index (index)}
17
+ {#if item.image}
18
+ <img data-testid="canvas-gallery-image" src={item.image} alt="" />
19
+ {/if}
20
+ {/each}
21
+ <button
22
+ type="button"
23
+ data-testid="edit-captions"
24
+ onclick={() =>
25
+ updateAttributes?.({ items: items.map((item) => ({ ...item, caption: 'edited' })) })}
26
+ >
27
+ Edit captions
28
+ </button>
29
+ </div>
@@ -0,0 +1,11 @@
1
+ interface Props {
2
+ src?: string;
3
+ items?: Array<{
4
+ image?: string;
5
+ caption?: string;
6
+ }>;
7
+ updateAttributes?: (attrs: Record<string, unknown>) => void;
8
+ }
9
+ declare const ImageBlockFixture: import("svelte").Component<Props, {}, "">;
10
+ type ImageBlockFixture = ReturnType<typeof ImageBlockFixture>;
11
+ export default ImageBlockFixture;
@@ -0,0 +1,21 @@
1
+ import type { BlockDefinition } from '../core/types.js';
2
+ /**
3
+ * Local `blob:` previews of images uploaded this session, keyed by the value
4
+ * the image source returned. An uploaded file is not served until the site
5
+ * redeploys, so the canvas shows the preview in the meantime.
6
+ */
7
+ export interface ImagePreviews {
8
+ get(src: string): string | undefined;
9
+ /** The value a preview URL stands in for. */
10
+ sourceOf(url: string): string | undefined;
11
+ register(src: string, file: Blob): void;
12
+ revokeAll(): void;
13
+ }
14
+ export declare function createImagePreviews(): ImagePreviews;
15
+ /** The attrs a Block component renders on the canvas, with previews in place of image values. */
16
+ export declare function withImagePreviews(block: BlockDefinition, attrs: Record<string, unknown>, previews: ImagePreviews): Record<string, unknown>;
17
+ /**
18
+ * The attrs a Block component writes back, with its previews turned into the
19
+ * values they stand in for, so a preview is never stored.
20
+ */
21
+ export declare function withoutImagePreviews(block: BlockDefinition, attrs: Record<string, unknown>, previews: ImagePreviews): Record<string, unknown>;
@@ -0,0 +1,59 @@
1
+ import { attributeListFields, attributeListValueSpec, inferAttributeInputKind } from '../core/attributes.js';
2
+ export function createImagePreviews() {
3
+ const previews = new Map();
4
+ const sources = new Map();
5
+ return {
6
+ get: (src) => previews.get(src),
7
+ sourceOf: (url) => sources.get(url),
8
+ register(src, file) {
9
+ // A re-upload answers the same src, so nothing re-renders and the canvas
10
+ // and field keep the earlier URL; it is not revoked until teardown.
11
+ const url = URL.createObjectURL(file);
12
+ sources.set(url, src);
13
+ previews.set(src, url);
14
+ },
15
+ revokeAll() {
16
+ sources.forEach((_src, url) => URL.revokeObjectURL(url));
17
+ sources.clear();
18
+ previews.clear();
19
+ }
20
+ };
21
+ }
22
+ function substituteValue(spec, value, lookup) {
23
+ const kind = inferAttributeInputKind(spec);
24
+ if (kind === 'image') {
25
+ return typeof value === 'string' ? (lookup(value) ?? value) : value;
26
+ }
27
+ if (kind !== 'list' || !spec.list || !Array.isArray(value))
28
+ return value;
29
+ const valueSpec = attributeListValueSpec(spec.list);
30
+ if (valueSpec)
31
+ return value.map((item) => substituteValue(valueSpec, item, lookup));
32
+ const fields = attributeListFields(spec.list);
33
+ return value.map((item) => {
34
+ if (typeof item !== 'object' || item === null)
35
+ return item;
36
+ const record = item;
37
+ return Object.fromEntries(Object.entries(record).map(([name, fieldValue]) => {
38
+ const fieldSpec = fields.find(([fieldName]) => fieldName === name)?.[1];
39
+ return [name, fieldSpec ? substituteValue(fieldSpec, fieldValue, lookup) : fieldValue];
40
+ }));
41
+ });
42
+ }
43
+ function substituteAttrs(block, attrs, lookup) {
44
+ return Object.fromEntries(Object.entries(attrs).map(([name, value]) => {
45
+ const spec = block.attributes[name];
46
+ return [name, spec ? substituteValue(spec, value, lookup) : value];
47
+ }));
48
+ }
49
+ /** The attrs a Block component renders on the canvas, with previews in place of image values. */
50
+ export function withImagePreviews(block, attrs, previews) {
51
+ return substituteAttrs(block, attrs, (value) => previews.get(value));
52
+ }
53
+ /**
54
+ * The attrs a Block component writes back, with its previews turned into the
55
+ * values they stand in for, so a preview is never stored.
56
+ */
57
+ export function withoutImagePreviews(block, attrs, previews) {
58
+ return substituteAttrs(block, attrs, (value) => previews.sourceOf(value));
59
+ }
@@ -1,7 +1,8 @@
1
1
  import { type AnyExtension } from '@tiptap/core';
2
2
  import type { BlockDefinition, BlockRegistry, ContentSchema } from '../core/types.js';
3
3
  import type { RichTextFeature } from '../core/types.js';
4
+ import { type ImagePreviews } from './imagePreviews.js';
4
5
  export type BlockActivationCallback = (pos: number) => void;
5
6
  export declare function getBlockDefaultAttrs(block: BlockDefinition): Record<string, unknown>;
6
7
  export declare function createRichTextExtensions(features: ReadonlySet<RichTextFeature>): AnyExtension[];
7
- export declare function createEditorExtensions(blocks?: BlockRegistry | BlockDefinition[], schema?: ContentSchema, onActivateBlock?: BlockActivationCallback, extensions?: AnyExtension[]): AnyExtension[];
8
+ export declare function createEditorExtensions(blocks?: BlockRegistry | BlockDefinition[], schema?: ContentSchema, onActivateBlock?: BlockActivationCallback, extensions?: AnyExtension[], imagePreviews?: ImagePreviews): AnyExtension[];
@@ -7,6 +7,7 @@ import { DEFAULT_MARKS, resolveRegistry } from '../core/registry.js';
7
7
  import { coerceAttributeValue, normalizeBlockAttributes, serializeBlockAttributes } from '../core/attributes.js';
8
8
  import { lowlight } from './syntaxHighlight.js';
9
9
  import { sanitizeHref } from '../render/sanitize.js';
10
+ import { withImagePreviews, withoutImagePreviews } from './imagePreviews.js';
10
11
  function safeParseJSON(raw) {
11
12
  if (!raw)
12
13
  return {};
@@ -21,9 +22,10 @@ function safeParseJSON(raw) {
21
22
  export function getBlockDefaultAttrs(block) {
22
23
  return normalizeBlockAttributes(block, {});
23
24
  }
24
- function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
25
+ function buildBlockEditorProps(block, node, editor, onActivate, getPos, imagePreviews) {
26
+ const attrs = node.attrs ?? {};
25
27
  return {
26
- attrs: node.attrs ?? {},
28
+ attrs: imagePreviews ? withImagePreviews(block, attrs, imagePreviews) : attrs,
27
29
  content: (node.content?.toJSON() ?? []),
28
30
  blockId: block.id,
29
31
  label: block.label,
@@ -52,7 +54,8 @@ function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
52
54
  const current = editor.state.doc.nodeAt(pos);
53
55
  if (!current)
54
56
  return;
55
- editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...current.attrs, ...attrs }));
57
+ const stored = imagePreviews ? withoutImagePreviews(block, attrs, imagePreviews) : attrs;
58
+ editor.view.dispatch(editor.state.tr.setNodeMarkup(pos, undefined, { ...current.attrs, ...stored }));
56
59
  },
57
60
  // ProseMirror's getPos can return undefined when the node is not currently
58
61
  // in the document (e.g. mid-transaction); skip activation in that case
@@ -66,7 +69,7 @@ function buildBlockEditorProps(block, node, editor, onActivate, getPos) {
66
69
  : undefined
67
70
  };
68
71
  }
69
- function mountBlockEditorComponent(block, node, container, contentDOM, mounted, editor, onActivate, getPos) {
72
+ function mountBlockEditorComponent(block, node, container, contentDOM, mounted, editor, onActivate, getPos, imagePreviews) {
70
73
  mounted?.destroy();
71
74
  const createEditorMount = block.components.editor.plugin.createEditorMount;
72
75
  if (!createEditorMount) {
@@ -77,7 +80,7 @@ function mountBlockEditorComponent(block, node, container, contentDOM, mounted,
77
80
  inline: block.behaviors.inline ?? false,
78
81
  component: block.components.editor,
79
82
  contentDOM,
80
- props: buildBlockEditorProps(block, node, editor, onActivate, getPos)
83
+ props: buildBlockEditorProps(block, node, editor, onActivate, getPos, imagePreviews)
81
84
  });
82
85
  }
83
86
  function isDragHandleEvent(event) {
@@ -90,7 +93,7 @@ function isInteractiveNodeViewEvent(event) {
90
93
  return false;
91
94
  return Boolean(target.closest('input, textarea, select, button:not([data-drag-handle]), a[href]'));
92
95
  }
93
- function createBlockNodeView(block, node, getPos, onActivate, editor) {
96
+ function createBlockNodeView(block, node, getPos, onActivate, editor, imagePreviews) {
94
97
  const dom = document.createElement(block.behaviors.inline ? 'span' : 'div');
95
98
  dom.className = 'uncial-nodeview';
96
99
  dom.dataset.uncialBlockId = block.id;
@@ -111,7 +114,7 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
111
114
  contentDOM.className = 'uncial-nodeview-content';
112
115
  }
113
116
  let mounted = null;
114
- mounted = mountBlockEditorComponent(block, node, dom, contentDOM, mounted, editor, onActivate, getPos);
117
+ mounted = mountBlockEditorComponent(block, node, dom, contentDOM, mounted, editor, onActivate, getPos, imagePreviews);
115
118
  syncBlockPosition();
116
119
  return {
117
120
  dom,
@@ -136,12 +139,12 @@ function createBlockNodeView(block, node, getPos, onActivate, editor) {
136
139
  // reach it — the block would keep rendering its old attributes until
137
140
  // something else re-rendered it. A microtask is outside that window
138
141
  // and still lands before paint.
139
- const nextProps = buildBlockEditorProps(block, nextNode, editor, onActivate, getPos);
142
+ const nextProps = buildBlockEditorProps(block, nextNode, editor, onActivate, getPos, imagePreviews);
140
143
  queueMicrotask(() => mounted?.update?.(nextProps));
141
144
  }
142
145
  else {
143
146
  // Fall back to a full remount for runtimes without update support.
144
- mounted = mountBlockEditorComponent(block, nextNode, dom, contentDOM, mounted, editor, onActivate, getPos);
147
+ mounted = mountBlockEditorComponent(block, nextNode, dom, contentDOM, mounted, editor, onActivate, getPos, imagePreviews);
145
148
  }
146
149
  return true;
147
150
  },
@@ -186,7 +189,7 @@ function containerContentExpression(content) {
186
189
  return 'block*';
187
190
  }
188
191
  }
189
- function createBlockNodeExtension(block, onActivate) {
192
+ function createBlockNodeExtension(block, onActivate, imagePreviews) {
190
193
  const isContainer = Boolean(block.content);
191
194
  return Node.create({
192
195
  name: block.id,
@@ -223,7 +226,7 @@ function createBlockNodeExtension(block, onActivate) {
223
226
  return [{ tag: block.html?.parseTag ?? `div[data-uncial-block="${block.id}"]` }];
224
227
  },
225
228
  addNodeView() {
226
- return ({ node, getPos, editor }) => createBlockNodeView(block, node, getPos, onActivate, editor);
229
+ return ({ node, getPos, editor }) => createBlockNodeView(block, node, getPos, onActivate, editor, imagePreviews);
227
230
  },
228
231
  renderHTML({ HTMLAttributes, node }) {
229
232
  const normalizedAttrs = normalizeBlockAttributes(block, node.attrs ?? {});
@@ -375,11 +378,11 @@ export function createRichTextExtensions(features) {
375
378
  ...(features.has('codeBlock') ? [CodeBlockLowlight.configure({ lowlight })] : [])
376
379
  ];
377
380
  }
378
- export function createEditorExtensions(blocks = [], schema, onActivateBlock, extensions = []) {
381
+ export function createEditorExtensions(blocks = [], schema, onActivateBlock, extensions = [], imagePreviews) {
379
382
  const registry = resolveRegistry(blocks);
380
383
  const blockExtensions = registry.blocks
381
384
  .filter((block) => !schema || schema.allowedBlocks.has(block.id))
382
- .map((block) => createBlockNodeExtension(block, onActivateBlock));
385
+ .map((block) => createBlockNodeExtension(block, onActivateBlock, imagePreviews));
383
386
  return [
384
387
  ...createBaseExtensions(schema),
385
388
  createDocumentIdentityExtension(registry),