uncial 1.0.0 → 2.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.
- package/README.md +7 -7
- package/dist/core/attributes.d.ts +14 -2
- package/dist/core/attributes.js +52 -0
- package/dist/core/defineBlock.js +19 -32
- package/dist/core/migrations.d.ts +1 -1
- package/dist/core/migrations.js +10 -3
- package/dist/core/normalize.js +19 -7
- package/dist/core/registry.js +13 -0
- package/dist/core/types.d.ts +23 -1
- package/dist/core/validate.js +5 -1
- package/dist/editor/AttributeFieldControl.svelte +125 -5
- package/dist/editor/AttributeFieldControl.svelte.d.ts +1 -0
- package/dist/editor/BlockAttributesPanel.svelte +42 -15
- package/dist/editor/Editor.svelte +144 -11
- package/dist/editor/Editor.svelte.d.ts +37 -0
- package/dist/editor/attributesController.d.ts +5 -0
- package/dist/editor/attributesController.js +26 -3
- package/dist/editor/bindEditor.d.ts +7 -0
- package/dist/editor/bindEditor.js +39 -1
- package/dist/editor/containerChildOps.js +22 -1
- package/dist/editor/dropdownFloat.d.ts +16 -0
- package/dist/editor/dropdownFloat.js +127 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/render/RichContent.svelte +10 -2
- package/dist/render/RichContent.svelte.d.ts +2 -0
- package/dist/render/RichNode.svelte +64 -8
- package/dist/render/RichNode.svelte.d.ts +2 -0
- package/dist/render/Tab.svelte +27 -0
- package/dist/render/Tab.svelte.d.ts +10 -0
- package/dist/render/Tabs.svelte +57 -0
- package/dist/render/Tabs.svelte.d.ts +10 -0
- package/dist/render/index.d.ts +3 -0
- package/dist/render/index.js +6 -0
- package/dist/render/tabSelection.svelte.d.ts +8 -0
- package/dist/render/tabSelection.svelte.js +40 -0
- package/dist/shared/documentIdentity.d.ts +6 -0
- package/dist/shared/documentIdentity.js +64 -0
- package/dist/shared/syntaxHighlight.d.ts +14 -0
- package/dist/shared/syntaxHighlight.js +36 -3
- package/dist/shared/tiptap.js +84 -6
- package/dist/styles/base.css +177 -0
- package/dist/styles/prose.css +26 -0
- package/dist/styles/tokens.css +53 -69
- package/dist/tabs.d.ts +6 -0
- package/dist/tabs.js +36 -0
- package/dist/web-components/UncialEditor.svelte +13 -12
- package/dist/web-components/UncialEditor.svelte.d.ts +2 -0
- package/dist/web-components/index.d.ts +6 -0
- package/dist/web-components/index.js +12 -0
- package/package.json +134 -133
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import LinkAttributesPanel from './LinkAttributesPanel.svelte';
|
|
18
18
|
import AttributeFieldControl from './AttributeFieldControl.svelte';
|
|
19
19
|
import { dropdownDismiss } from './dropdownDismiss.js';
|
|
20
|
+
import { dropdownFloat } from './dropdownFloat.js';
|
|
20
21
|
import { CHOOSE_ATTRIBUTE_EVENT, type ChooseAttributeRequest } from './chooseAttribute.js';
|
|
21
22
|
|
|
22
23
|
interface Props {
|
|
@@ -47,6 +48,9 @@
|
|
|
47
48
|
? (Object.entries(selectedBlock.attributes) as Array<[string, AttributeSpec<unknown>]>)
|
|
48
49
|
: []
|
|
49
50
|
);
|
|
51
|
+
const selectedBlockReadOnly = $derived(
|
|
52
|
+
Boolean(selectedBlock && 'readOnly' in selectedBlock && selectedBlock.readOnly)
|
|
53
|
+
);
|
|
50
54
|
let childBlockQuery = $state('');
|
|
51
55
|
const hasChildren = $derived(controllerState.containerChildren.length > 0);
|
|
52
56
|
const canRemoveChild = $derived(controllerState.containerChildren.length > 1);
|
|
@@ -62,9 +66,18 @@
|
|
|
62
66
|
Boolean(controllerState.activeBlock) &&
|
|
63
67
|
Boolean(selectedBlock && 'content' in selectedBlock && selectedBlock.content)
|
|
64
68
|
);
|
|
69
|
+
// The open container's own declaration narrows what "Add block" may offer;
|
|
70
|
+
// a container declaring nothing offers everything the schema allows.
|
|
71
|
+
const admissibleChildBlocks = $derived.by(() => {
|
|
72
|
+
const declared =
|
|
73
|
+
selectedBlock && 'content' in selectedBlock ? selectedBlock.content?.allowedBlocks : undefined;
|
|
74
|
+
return declared ? activeBlocks.filter((block) => declared.includes(block.id)) : activeBlocks;
|
|
75
|
+
});
|
|
65
76
|
const filteredChildBlocks = $derived.by(() => {
|
|
66
77
|
const query = childBlockQuery.trim().toLowerCase();
|
|
67
|
-
return
|
|
78
|
+
return admissibleChildBlocks.filter(
|
|
79
|
+
(block) => !query || block.label.toLowerCase().includes(query)
|
|
80
|
+
);
|
|
68
81
|
});
|
|
69
82
|
let draggingChildIndex = $state<number | null>(null);
|
|
70
83
|
let draggingPointerId = $state<number | null>(null);
|
|
@@ -162,16 +175,18 @@
|
|
|
162
175
|
{controllerState.mode === 'edit' ? 'Edit' : 'Configure'}
|
|
163
176
|
{selectedBlock.label || controllerState.selectedBlockId || 'block'}
|
|
164
177
|
</p>
|
|
165
|
-
{#
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
178
|
+
{#if !selectedBlockReadOnly}
|
|
179
|
+
{#each selectedAttributeSpecs as [name, spec] (name)}
|
|
180
|
+
<AttributeFieldControl
|
|
181
|
+
{name}
|
|
182
|
+
{spec}
|
|
183
|
+
value={controllerState.draftAttrs[name]}
|
|
184
|
+
error={controllerState.validationErrors[name]}
|
|
185
|
+
onChange={(value) => controller.setDraftAttr(name, value)}
|
|
186
|
+
onCustom={chooseCustomAttribute}
|
|
187
|
+
/>
|
|
188
|
+
{/each}
|
|
189
|
+
{/if}
|
|
175
190
|
<div class="uncial-panel__actions">
|
|
176
191
|
{#if controllerState.mode === 'edit'}
|
|
177
192
|
<button
|
|
@@ -200,8 +215,12 @@
|
|
|
200
215
|
<div class="uncial-children-section">
|
|
201
216
|
<div class="uncial-children-section__header">
|
|
202
217
|
<p class="uncial-section-label">Nested blocks</p>
|
|
203
|
-
{#if
|
|
204
|
-
<details
|
|
218
|
+
{#if admissibleChildBlocks.length > 0}
|
|
219
|
+
<details
|
|
220
|
+
class="uncial-dropdown uncial-dropdown--end"
|
|
221
|
+
use:dropdownDismiss
|
|
222
|
+
use:dropdownFloat
|
|
223
|
+
>
|
|
205
224
|
<summary class="uncial-btn uncial-btn--primary uncial-btn--xs">
|
|
206
225
|
<PlusIcon size={12} weight="bold" />
|
|
207
226
|
<span>Add block</span>
|
|
@@ -248,12 +267,20 @@
|
|
|
248
267
|
>
|
|
249
268
|
<DotsSixVerticalIcon size={12} weight="bold" />
|
|
250
269
|
</button>
|
|
251
|
-
<
|
|
270
|
+
<button
|
|
271
|
+
type="button"
|
|
272
|
+
class="uncial-child-item__content"
|
|
273
|
+
aria-current={child.pos === controllerState.activeBlock?.pos ? 'true' : undefined}
|
|
274
|
+
onclick={(event) => {
|
|
275
|
+
event.stopPropagation();
|
|
276
|
+
controller.openAttributesAt(child.pos);
|
|
277
|
+
}}
|
|
278
|
+
>
|
|
252
279
|
<span class="uncial-child-item__label">{child.label}</span>
|
|
253
280
|
{#if child.summary}
|
|
254
281
|
<span class="uncial-child-item__summary">{child.summary}</span>
|
|
255
282
|
{/if}
|
|
256
|
-
</
|
|
283
|
+
</button>
|
|
257
284
|
<div class="uncial-child-item__actions">
|
|
258
285
|
<button
|
|
259
286
|
type="button"
|
|
@@ -24,8 +24,10 @@
|
|
|
24
24
|
type DocumentMetaController,
|
|
25
25
|
type DocumentMetaState
|
|
26
26
|
} from './metaController.js';
|
|
27
|
+
import BlockAttributesPanel from './BlockAttributesPanel.svelte';
|
|
27
28
|
import DocumentMetaPanel from './DocumentMetaPanel.svelte';
|
|
28
29
|
import Toolbar from './Toolbar.svelte';
|
|
30
|
+
import type { ChooseAttributeRequest } from './chooseAttribute.js';
|
|
29
31
|
import { dropdownDismiss } from './dropdownDismiss.js';
|
|
30
32
|
import type { ToolbarFeature, ToolbarFeatureSelection } from './toolbarFeatures.js';
|
|
31
33
|
|
|
@@ -40,7 +42,43 @@
|
|
|
40
42
|
toolbarExtensions?: ToolbarFeature[];
|
|
41
43
|
attributesController?: BlockAttributesController | null;
|
|
42
44
|
metaController?: DocumentMetaController | null;
|
|
45
|
+
/**
|
|
46
|
+
* Whether, and how, the editor renders its own block/link attributes
|
|
47
|
+
* panel. Registering a block is meant to be all it takes to edit one, so
|
|
48
|
+
* a panel is on by default; set `'off'` in a host that places
|
|
49
|
+
* `BlockAttributesPanel` itself, or the two panels both render.
|
|
50
|
+
*
|
|
51
|
+
* `'docked'` gives the panel a column of its own, which narrows the
|
|
52
|
+
* document by the panel's width. `'overlay'` floats it against the
|
|
53
|
+
* viewport edge for as long as a block is selected, so the document
|
|
54
|
+
* keeps exactly the width the host gave it — what a host rendering the
|
|
55
|
+
* document at its published width wants.
|
|
56
|
+
*/
|
|
57
|
+
attributesPanel?: boolean | 'docked' | 'overlay' | 'off';
|
|
58
|
+
/**
|
|
59
|
+
* `'card'` draws the editor as a bordered, padded surface — right when
|
|
60
|
+
* the editor sits in a page of its own chrome. `'bare'` draws no surface
|
|
61
|
+
* and reserves no inline space: `.uncial-content`'s content box is
|
|
62
|
+
* exactly the box the host gave the shell, and the block handles float
|
|
63
|
+
* over each block instead of pushing it inwards. A host that renders the
|
|
64
|
+
* document in its published layout wants `'bare'`, or every width the
|
|
65
|
+
* page's CSS derives from the container is wrong under the editor.
|
|
66
|
+
*/
|
|
67
|
+
presentation?: 'card' | 'bare';
|
|
68
|
+
/** Forwarded to the built-in panel; see `BlockAttributesPanel`. */
|
|
69
|
+
onChooseAttribute?: (request: ChooseAttributeRequest) => void;
|
|
43
70
|
onIssue?: (issue: ValidationIssue) => void;
|
|
71
|
+
/**
|
|
72
|
+
* The document after an edit, and the metadata after one.
|
|
73
|
+
*
|
|
74
|
+
* `json` and `meta` are bindable, so a host that only mirrors them needs
|
|
75
|
+
* neither of these. A host that has to *act* on an edit — persist it,
|
|
76
|
+
* mark itself dirty, debounce a save — does: binding alone cannot tell
|
|
77
|
+
* an edit the user made from a document the host itself just loaded, and
|
|
78
|
+
* every such host would otherwise reimplement the same guard flag.
|
|
79
|
+
*/
|
|
80
|
+
onChange?: (json: JSONContent) => void;
|
|
81
|
+
onMetaChange?: (meta: Record<string, unknown>) => void;
|
|
44
82
|
}
|
|
45
83
|
|
|
46
84
|
let {
|
|
@@ -54,7 +92,12 @@
|
|
|
54
92
|
toolbarExtensions = [],
|
|
55
93
|
attributesController = null,
|
|
56
94
|
metaController = null,
|
|
57
|
-
|
|
95
|
+
attributesPanel = true,
|
|
96
|
+
presentation = 'card',
|
|
97
|
+
onChooseAttribute,
|
|
98
|
+
onIssue,
|
|
99
|
+
onChange,
|
|
100
|
+
onMetaChange
|
|
58
101
|
}: Props = $props();
|
|
59
102
|
|
|
60
103
|
let editorHost: HTMLDivElement;
|
|
@@ -71,9 +114,34 @@
|
|
|
71
114
|
dirty: false
|
|
72
115
|
});
|
|
73
116
|
let committedMetaSerialized = JSON.stringify(meta);
|
|
117
|
+
/**
|
|
118
|
+
* Whether the metadata controller has been told what the document's metadata
|
|
119
|
+
* is.
|
|
120
|
+
*
|
|
121
|
+
* The two effects that keep the `meta` prop and the controller in step are a
|
|
122
|
+
* pair, and neither may assume it runs first. Until the controller has been
|
|
123
|
+
* seeded it holds the schema's defaults, and pushing those outward would
|
|
124
|
+
* overwrite the metadata it has not been given yet. That is invisible to a
|
|
125
|
+
* host that mounts the editor first and supplies `meta` afterwards — the
|
|
126
|
+
* prop changes, so the sync runs — and fatal to one that has the document in
|
|
127
|
+
* hand before it renders, where the prop never changes and the seeding
|
|
128
|
+
* comparison below would otherwise match on the first pass and skip.
|
|
129
|
+
*/
|
|
130
|
+
let metaSeeded = $state(false);
|
|
74
131
|
|
|
75
132
|
const registry = $derived(resolveRegistry(blocks));
|
|
76
133
|
const controller = $derived(attributesController ?? internalController);
|
|
134
|
+
const panelMode = $derived.by(() => {
|
|
135
|
+
if (attributesPanel === true) return 'docked';
|
|
136
|
+
if (attributesPanel === false) return 'off';
|
|
137
|
+
return attributesPanel;
|
|
138
|
+
});
|
|
139
|
+
// An overlay panel covers the page, so it is only mounted while it has
|
|
140
|
+
// something to edit; a docked one holds its column either way.
|
|
141
|
+
const panelVisible = $derived(
|
|
142
|
+
panelMode === 'docked' ||
|
|
143
|
+
(panelMode === 'overlay' && (controllerState.open || controllerState.link.open))
|
|
144
|
+
);
|
|
77
145
|
const resolvedMetaFields = $derived.by(() => {
|
|
78
146
|
if (metaFields instanceof Map) return metaFields;
|
|
79
147
|
if (metaFields) return new Map(Object.entries(metaFields));
|
|
@@ -88,12 +156,13 @@
|
|
|
88
156
|
meta,
|
|
89
157
|
extensions,
|
|
90
158
|
attributesController: controller,
|
|
159
|
+
autoOpenAttributes: panelMode !== 'overlay',
|
|
91
160
|
onIssue,
|
|
92
161
|
onChange: (nextDocument) => {
|
|
93
|
-
|
|
162
|
+
setDocument(nextDocument);
|
|
94
163
|
},
|
|
95
164
|
onMetaChange: (nextMeta) => {
|
|
96
|
-
|
|
165
|
+
setMeta(nextMeta);
|
|
97
166
|
committedMetaSerialized = JSON.stringify(nextMeta);
|
|
98
167
|
documentMetaController.reset(nextMeta);
|
|
99
168
|
},
|
|
@@ -108,13 +177,25 @@
|
|
|
108
177
|
: !schema || schema.allowedBlocks.has(block.id)
|
|
109
178
|
)
|
|
110
179
|
);
|
|
180
|
+
/**
|
|
181
|
+
* Room for the block labels the gutter draws.
|
|
182
|
+
*
|
|
183
|
+
* A label wraps, so what has to fit on one line is its longest word, not the
|
|
184
|
+
* whole label. Reserving the whole label let one long block name in the
|
|
185
|
+
* registry take that width away from every document — "Content State
|
|
186
|
+
* conformance table" is thirty-one characters, which reserved about 365px of
|
|
187
|
+
* every editing surface, including in documents that never use the block.
|
|
188
|
+
*/
|
|
111
189
|
const editorGutterWidth = $derived.by(() => {
|
|
112
|
-
const
|
|
113
|
-
(longest, block) =>
|
|
190
|
+
const longestWord = registry.blocks.reduce(
|
|
191
|
+
(longest, block) =>
|
|
192
|
+
block.label
|
|
193
|
+
.split(/\s+/)
|
|
194
|
+
.reduce((widest, word) => Math.max(widest, word.length), longest),
|
|
114
195
|
0
|
|
115
196
|
);
|
|
116
197
|
|
|
117
|
-
return `max(var(--uncial-gutter-width), ${
|
|
198
|
+
return `max(var(--uncial-gutter-width), ${longestWord * 0.68 + 1.75}rem)`;
|
|
118
199
|
});
|
|
119
200
|
function insertBlock(blockId: string): void {
|
|
120
201
|
const ok = controller.insertBlock(blockId);
|
|
@@ -130,9 +211,27 @@
|
|
|
130
211
|
});
|
|
131
212
|
}
|
|
132
213
|
|
|
214
|
+
/**
|
|
215
|
+
* Assign the document and tell the host, in that order.
|
|
216
|
+
*
|
|
217
|
+
* Metadata is part of the document, so a metadata commit has to reach a host
|
|
218
|
+
* that persists on `onChange` exactly as a keystroke does. Assigning `json`
|
|
219
|
+
* directly is what would silently drop it.
|
|
220
|
+
*/
|
|
221
|
+
function setDocument(next: JSONContent): void {
|
|
222
|
+
json = next;
|
|
223
|
+
onChange?.(next);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** The same for metadata: assign, then tell the host. */
|
|
227
|
+
function setMeta(next: Record<string, unknown>): void {
|
|
228
|
+
meta = next;
|
|
229
|
+
onMetaChange?.(next);
|
|
230
|
+
}
|
|
231
|
+
|
|
133
232
|
function commitMeta(nextMeta: Record<string, unknown>): void {
|
|
134
|
-
|
|
135
|
-
|
|
233
|
+
setMeta(nextMeta);
|
|
234
|
+
setDocument({ ...json, meta: nextMeta });
|
|
136
235
|
committedMetaSerialized = JSON.stringify(nextMeta);
|
|
137
236
|
metaTriggerEl?.closest('details')?.removeAttribute('open');
|
|
138
237
|
}
|
|
@@ -194,8 +293,9 @@
|
|
|
194
293
|
|
|
195
294
|
$effect(() => {
|
|
196
295
|
const serialized = JSON.stringify(meta);
|
|
197
|
-
if (serialized === committedMetaSerialized) return;
|
|
296
|
+
if (metaSeeded && serialized === committedMetaSerialized) return;
|
|
198
297
|
committedMetaSerialized = serialized;
|
|
298
|
+
metaSeeded = true;
|
|
199
299
|
documentMetaController.reset(meta);
|
|
200
300
|
});
|
|
201
301
|
|
|
@@ -207,8 +307,16 @@
|
|
|
207
307
|
return unsubscribe;
|
|
208
308
|
});
|
|
209
309
|
|
|
310
|
+
/*
|
|
311
|
+
* Reconcile the metadata panel's state into the document without reporting an
|
|
312
|
+
* edit. This runs on mount, when the panel still holds the schema's defaults
|
|
313
|
+
* and the loaded document's own metadata has not reached it yet: a host that
|
|
314
|
+
* persists on `onChange` would take those defaults for an edit and save a
|
|
315
|
+
* document whose required metadata is empty. A metadata change the author
|
|
316
|
+
* actually made arrives through `commitMeta`, which does report it.
|
|
317
|
+
*/
|
|
210
318
|
$effect(() => {
|
|
211
|
-
if (metaState.dirty) return;
|
|
319
|
+
if (!metaSeeded || metaState.dirty) return;
|
|
212
320
|
const nextMeta = documentMetaController.getMeta();
|
|
213
321
|
const serialized = JSON.stringify(nextMeta);
|
|
214
322
|
if (serialized === committedMetaSerialized) return;
|
|
@@ -255,7 +363,7 @@
|
|
|
255
363
|
});
|
|
256
364
|
</script>
|
|
257
365
|
|
|
258
|
-
<div class=
|
|
366
|
+
<div class={['uncial-editor-shell', presentation === 'bare' && 'uncial-editor-shell--bare']}>
|
|
259
367
|
<div class="uncial-toolbar">
|
|
260
368
|
<Toolbar
|
|
261
369
|
{editor}
|
|
@@ -318,4 +426,29 @@
|
|
|
318
426
|
use:bindEditor={editorBinding}
|
|
319
427
|
></div>
|
|
320
428
|
</div>
|
|
429
|
+
{#if panelVisible}
|
|
430
|
+
<aside
|
|
431
|
+
class={[
|
|
432
|
+
'uncial-editor-sidebar',
|
|
433
|
+
panelMode === 'overlay' && 'uncial-editor-sidebar--overlay'
|
|
434
|
+
]}
|
|
435
|
+
aria-label="Block attributes"
|
|
436
|
+
>
|
|
437
|
+
{#if panelMode === 'overlay'}
|
|
438
|
+
<div class="uncial-editor-sidebar__head">
|
|
439
|
+
<button
|
|
440
|
+
type="button"
|
|
441
|
+
class="uncial-btn uncial-btn--ghost uncial-btn--sm"
|
|
442
|
+
onclick={() => {
|
|
443
|
+
controller.closeLinkAttributes();
|
|
444
|
+
controller.closeAttributes();
|
|
445
|
+
}}
|
|
446
|
+
>
|
|
447
|
+
Close
|
|
448
|
+
</button>
|
|
449
|
+
</div>
|
|
450
|
+
{/if}
|
|
451
|
+
<BlockAttributesPanel {controller} {blocks} {onChooseAttribute} />
|
|
452
|
+
</aside>
|
|
453
|
+
{/if}
|
|
321
454
|
</div>
|
|
@@ -2,6 +2,7 @@ import type { AnyExtension, JSONContent } from '@tiptap/core';
|
|
|
2
2
|
import type { AttributeSpec, BlockDefinition, BlockRegistry, ContentSchema, DocumentMetaSchema, ValidationIssue } from '../core/types.js';
|
|
3
3
|
import { type BlockAttributesController } from './attributesController.js';
|
|
4
4
|
import { type DocumentMetaController } from './metaController.js';
|
|
5
|
+
import type { ChooseAttributeRequest } from './chooseAttribute.js';
|
|
5
6
|
import type { ToolbarFeature, ToolbarFeatureSelection } from './toolbarFeatures.js';
|
|
6
7
|
interface Props {
|
|
7
8
|
blocks?: BlockRegistry | BlockDefinition[];
|
|
@@ -14,7 +15,43 @@ interface Props {
|
|
|
14
15
|
toolbarExtensions?: ToolbarFeature[];
|
|
15
16
|
attributesController?: BlockAttributesController | null;
|
|
16
17
|
metaController?: DocumentMetaController | null;
|
|
18
|
+
/**
|
|
19
|
+
* Whether, and how, the editor renders its own block/link attributes
|
|
20
|
+
* panel. Registering a block is meant to be all it takes to edit one, so
|
|
21
|
+
* a panel is on by default; set `'off'` in a host that places
|
|
22
|
+
* `BlockAttributesPanel` itself, or the two panels both render.
|
|
23
|
+
*
|
|
24
|
+
* `'docked'` gives the panel a column of its own, which narrows the
|
|
25
|
+
* document by the panel's width. `'overlay'` floats it against the
|
|
26
|
+
* viewport edge for as long as a block is selected, so the document
|
|
27
|
+
* keeps exactly the width the host gave it — what a host rendering the
|
|
28
|
+
* document at its published width wants.
|
|
29
|
+
*/
|
|
30
|
+
attributesPanel?: boolean | 'docked' | 'overlay' | 'off';
|
|
31
|
+
/**
|
|
32
|
+
* `'card'` draws the editor as a bordered, padded surface — right when
|
|
33
|
+
* the editor sits in a page of its own chrome. `'bare'` draws no surface
|
|
34
|
+
* and reserves no inline space: `.uncial-content`'s content box is
|
|
35
|
+
* exactly the box the host gave the shell, and the block handles float
|
|
36
|
+
* over each block instead of pushing it inwards. A host that renders the
|
|
37
|
+
* document in its published layout wants `'bare'`, or every width the
|
|
38
|
+
* page's CSS derives from the container is wrong under the editor.
|
|
39
|
+
*/
|
|
40
|
+
presentation?: 'card' | 'bare';
|
|
41
|
+
/** Forwarded to the built-in panel; see `BlockAttributesPanel`. */
|
|
42
|
+
onChooseAttribute?: (request: ChooseAttributeRequest) => void;
|
|
17
43
|
onIssue?: (issue: ValidationIssue) => void;
|
|
44
|
+
/**
|
|
45
|
+
* The document after an edit, and the metadata after one.
|
|
46
|
+
*
|
|
47
|
+
* `json` and `meta` are bindable, so a host that only mirrors them needs
|
|
48
|
+
* neither of these. A host that has to *act* on an edit — persist it,
|
|
49
|
+
* mark itself dirty, debounce a save — does: binding alone cannot tell
|
|
50
|
+
* an edit the user made from a document the host itself just loaded, and
|
|
51
|
+
* every such host would otherwise reimplement the same guard flag.
|
|
52
|
+
*/
|
|
53
|
+
onChange?: (json: JSONContent) => void;
|
|
54
|
+
onMetaChange?: (meta: Record<string, unknown>) => void;
|
|
18
55
|
}
|
|
19
56
|
declare const Editor: import("svelte").Component<Props, {}, "json" | "meta">;
|
|
20
57
|
type Editor = ReturnType<typeof Editor>;
|
|
@@ -24,6 +24,11 @@ export interface ContainerChildInfo {
|
|
|
24
24
|
blockId: string;
|
|
25
25
|
label: string;
|
|
26
26
|
summary: string;
|
|
27
|
+
/**
|
|
28
|
+
* Absolute document position of the child node, so the panel can select it
|
|
29
|
+
* with `openAttributesAt` without re-walking the document.
|
|
30
|
+
*/
|
|
31
|
+
pos: number;
|
|
27
32
|
}
|
|
28
33
|
export interface BlockAttributesState {
|
|
29
34
|
open: boolean;
|
|
@@ -169,7 +169,8 @@ export function createBlockAttributesController() {
|
|
|
169
169
|
key: `${child.type.name}:${String(stableAttr ?? '')}:${offset}:${index}`,
|
|
170
170
|
blockId: child.type.name,
|
|
171
171
|
label,
|
|
172
|
-
summary
|
|
172
|
+
summary,
|
|
173
|
+
pos: active.pos + 1 + offset
|
|
173
174
|
});
|
|
174
175
|
});
|
|
175
176
|
return children;
|
|
@@ -328,6 +329,8 @@ export function createBlockAttributesController() {
|
|
|
328
329
|
}
|
|
329
330
|
function setDraftAttr(name, value) {
|
|
330
331
|
const current = get(state);
|
|
332
|
+
if (current.selectedBlockId && targets.get(current.selectedBlockId)?.readOnly)
|
|
333
|
+
return;
|
|
331
334
|
const nextDraftAttrs = {
|
|
332
335
|
...current.draftAttrs,
|
|
333
336
|
[name]: value
|
|
@@ -365,9 +368,26 @@ export function createBlockAttributesController() {
|
|
|
365
368
|
for (const invalidName of Object.keys(validationErrors)) {
|
|
366
369
|
appliedAttrs[invalidName] = node.attrs?.[invalidName];
|
|
367
370
|
}
|
|
371
|
+
// Per-key attribute steps, never `setNodeMarkup`: replacing the node's
|
|
372
|
+
// markup rewrites the node, which maps a NodeSelection on it away (the
|
|
373
|
+
// caret lands in the document, taking focus out of the field being typed
|
|
374
|
+
// into) and forces the node view to re-render its whole subtree. An
|
|
375
|
+
// attrs-only edit must leave the document's shape, the selection and the
|
|
376
|
+
// mounted block component exactly where they are, because the panel
|
|
377
|
+
// writes through on every keystroke.
|
|
378
|
+
const tr = editor.state.tr;
|
|
379
|
+
let changed = false;
|
|
380
|
+
for (const [attrName, value] of Object.entries(appliedAttrs)) {
|
|
381
|
+
if (node.attrs?.[attrName] === value)
|
|
382
|
+
continue;
|
|
383
|
+
tr.setNodeAttribute(active.pos, attrName, value);
|
|
384
|
+
changed = true;
|
|
385
|
+
}
|
|
386
|
+
if (!changed)
|
|
387
|
+
return;
|
|
368
388
|
applyingDraftAttrs = true;
|
|
369
389
|
try {
|
|
370
|
-
editor.view.dispatch(
|
|
390
|
+
editor.view.dispatch(tr);
|
|
371
391
|
}
|
|
372
392
|
finally {
|
|
373
393
|
applyingDraftAttrs = false;
|
|
@@ -376,7 +396,8 @@ export function createBlockAttributesController() {
|
|
|
376
396
|
function canEditSelectedBlock(blockId) {
|
|
377
397
|
const current = get(state);
|
|
378
398
|
const explicitActive = resolveActiveBlock(current);
|
|
379
|
-
return (
|
|
399
|
+
return (!targets.get(blockId)?.readOnly &&
|
|
400
|
+
(explicitActive ?? findActiveBlock(editor, targets))?.id === blockId);
|
|
380
401
|
}
|
|
381
402
|
function insertBlock(blockId, attrs = {}) {
|
|
382
403
|
if (!editor || !registry?.has(blockId))
|
|
@@ -405,6 +426,8 @@ export function createBlockAttributesController() {
|
|
|
405
426
|
const block = targets.get(current.selectedBlockId);
|
|
406
427
|
if (!block)
|
|
407
428
|
return false;
|
|
429
|
+
if (current.mode === 'edit' && block.readOnly)
|
|
430
|
+
return false;
|
|
408
431
|
const { attrs, validationErrors } = parseAndValidateDraft(block, current.draftAttrs);
|
|
409
432
|
if (Object.keys(validationErrors).length > 0) {
|
|
410
433
|
state.update((draft) => ({ ...draft, validationErrors }));
|
|
@@ -9,6 +9,13 @@ export interface BindEditorOptions {
|
|
|
9
9
|
meta?: Record<string, unknown>;
|
|
10
10
|
extensions?: AnyExtension[];
|
|
11
11
|
attributesController?: BlockAttributesController | null;
|
|
12
|
+
/**
|
|
13
|
+
* Whether selecting a block opens its attributes panel by itself. Right for
|
|
14
|
+
* a panel that holds a column of its own; wrong for one that floats over the
|
|
15
|
+
* document, where an unasked-for panel covers the page. Off, the block's own
|
|
16
|
+
* gutter label opens it.
|
|
17
|
+
*/
|
|
18
|
+
autoOpenAttributes?: boolean;
|
|
12
19
|
onIssue?: (issue: ValidationIssue) => void;
|
|
13
20
|
onChange?: (json: JSONContent) => void;
|
|
14
21
|
onMetaChange?: (meta: Record<string, unknown>) => void;
|
|
@@ -63,6 +63,9 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
63
63
|
controllerState = state;
|
|
64
64
|
});
|
|
65
65
|
let editor = null;
|
|
66
|
+
// ProseMirror rebuilds node attrs in schema order. Script-owned attrs must
|
|
67
|
+
// retain the source representation until their owning script changes them.
|
|
68
|
+
let readOnlyAttrs = new Map();
|
|
66
69
|
// `lastSerialized` is the editor *body* serialization (meta/version stripped)
|
|
67
70
|
// and gates the `setContent` push in `syncEditorDocument`. `lastEmitted` is
|
|
68
71
|
// the *full* normalized serialization (body + meta + version) and gates the
|
|
@@ -83,6 +86,35 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
83
86
|
validate(normalized);
|
|
84
87
|
return normalized;
|
|
85
88
|
}
|
|
89
|
+
function rememberReadOnlyAttrs(document) {
|
|
90
|
+
const next = new Map();
|
|
91
|
+
const visit = (content) => {
|
|
92
|
+
for (const child of content ?? []) {
|
|
93
|
+
const id = child.attrs?.id;
|
|
94
|
+
if (registry.get(child.type)?.readOnly && typeof id === 'string' && child.attrs) {
|
|
95
|
+
next.set(id, child.attrs);
|
|
96
|
+
}
|
|
97
|
+
visit(child.content);
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
visit(document.content);
|
|
101
|
+
readOnlyAttrs = next;
|
|
102
|
+
}
|
|
103
|
+
function restoreReadOnlyAttrs(document) {
|
|
104
|
+
const restore = (node) => {
|
|
105
|
+
const content = node.content?.map(restore);
|
|
106
|
+
const id = node.attrs?.id;
|
|
107
|
+
const attrs = typeof node.type === 'string' && registry.get(node.type)?.readOnly && typeof id === 'string'
|
|
108
|
+
? (readOnlyAttrs.get(id) ?? node.attrs)
|
|
109
|
+
: node.attrs;
|
|
110
|
+
return {
|
|
111
|
+
...node,
|
|
112
|
+
...(attrs ? { attrs } : {}),
|
|
113
|
+
...(content ? { content } : {})
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
return restore(document);
|
|
117
|
+
}
|
|
86
118
|
function setController(nextController) {
|
|
87
119
|
if (controller === nextController)
|
|
88
120
|
return;
|
|
@@ -98,6 +130,9 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
98
130
|
}
|
|
99
131
|
}
|
|
100
132
|
function shouldAutoOpenAttributesForSelection(nextEditor) {
|
|
133
|
+
if (options.autoOpenAttributes === false) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
101
136
|
if (controller.isSelectionAutoOpenSuppressed()) {
|
|
102
137
|
return false;
|
|
103
138
|
}
|
|
@@ -126,6 +161,7 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
126
161
|
function createEditor() {
|
|
127
162
|
const extensions = createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? []);
|
|
128
163
|
const initialContent = normalizeAndValidate(options.json ?? emptyDocument());
|
|
164
|
+
rememberReadOnlyAttrs(initialContent);
|
|
129
165
|
const sanitized = sanitizeForEditorSchema(toEditorDocument(initialContent), getSchema(extensions));
|
|
130
166
|
sanitized.issues.forEach((issue) => options.onIssue?.(issue));
|
|
131
167
|
const initialEditorContent = sanitized.document;
|
|
@@ -137,8 +173,9 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
137
173
|
extensions,
|
|
138
174
|
content: initialEditorContent,
|
|
139
175
|
onUpdate: ({ editor: tiptap }) => {
|
|
140
|
-
const raw = tiptap.getJSON();
|
|
176
|
+
const raw = restoreReadOnlyAttrs(tiptap.getJSON());
|
|
141
177
|
const normalized = normalize(raw);
|
|
178
|
+
rememberReadOnlyAttrs(normalized);
|
|
142
179
|
const serialized = JSON.stringify(normalized);
|
|
143
180
|
// Skip the (comparatively expensive) validate + emit pass when the
|
|
144
181
|
// full normalized document (body + meta) is byte-identical to what
|
|
@@ -214,6 +251,7 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
214
251
|
return;
|
|
215
252
|
}
|
|
216
253
|
const normalized = normalize(options.json ?? emptyDocument());
|
|
254
|
+
rememberReadOnlyAttrs(normalized);
|
|
217
255
|
const serialized = JSON.stringify(normalized);
|
|
218
256
|
// Same guard as `onUpdate`, keyed on the full document (body + meta): when
|
|
219
257
|
// the external document normalizes to exactly what we last emitted (the
|
|
@@ -1,4 +1,22 @@
|
|
|
1
|
+
import { Fragment } from '@tiptap/pm/model';
|
|
1
2
|
import { getBlockDefaultAttrs } from '../shared/tiptap.js';
|
|
3
|
+
/**
|
|
4
|
+
* Build a node of `type` that the reader can put a caret in straight away.
|
|
5
|
+
*
|
|
6
|
+
* `createAndFill` only satisfies the content expression, and a container's
|
|
7
|
+
* expression is zero-or-more (persisted documents carry empty containers), so a
|
|
8
|
+
* fresh container comes back childless with nowhere for the caret. Seeding one
|
|
9
|
+
* node of the expression's default type — recursively, until a textblock is
|
|
10
|
+
* reached — makes the inserted child editable without a reload.
|
|
11
|
+
*/
|
|
12
|
+
function createEditableNode(type, attrs) {
|
|
13
|
+
const node = type.createAndFill(attrs ?? null);
|
|
14
|
+
if (!node || node.isTextblock || node.childCount > 0 || !type.spec.content)
|
|
15
|
+
return node;
|
|
16
|
+
const childType = type.contentMatch.defaultType;
|
|
17
|
+
const child = childType ? createEditableNode(childType) : null;
|
|
18
|
+
return child ? (type.createAndFill(attrs ?? null, Fragment.from(child)) ?? node) : node;
|
|
19
|
+
}
|
|
2
20
|
/**
|
|
3
21
|
* Add / move / remove children of the container block the panel is editing.
|
|
4
22
|
* Each op mutates only the document (never the controller store directly) and
|
|
@@ -53,6 +71,9 @@ export function createContainerChildOps(ctx) {
|
|
|
53
71
|
const containerBlock = registry.get(active.id);
|
|
54
72
|
if (!containerBlock?.content)
|
|
55
73
|
return false;
|
|
74
|
+
const admissible = containerBlock.content.allowedBlocks;
|
|
75
|
+
if (admissible && !admissible.includes(blockId))
|
|
76
|
+
return false;
|
|
56
77
|
const containerNode = editor.state.doc.nodeAt(active.pos);
|
|
57
78
|
if (!containerNode)
|
|
58
79
|
return false;
|
|
@@ -60,7 +81,7 @@ export function createContainerChildOps(ctx) {
|
|
|
60
81
|
const childNodeType = editor.schema.nodes[blockId];
|
|
61
82
|
if (!childBlock || !childNodeType)
|
|
62
83
|
return false;
|
|
63
|
-
const childNode = childNodeType
|
|
84
|
+
const childNode = createEditableNode(childNodeType, attrs ?? getBlockDefaultAttrs(childBlock));
|
|
64
85
|
if (!childNode)
|
|
65
86
|
return false;
|
|
66
87
|
ctx.dispatch(editor.state.tr.insert(active.pos + containerNode.nodeSize - 1, childNode));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ActionReturn } from 'svelte/action';
|
|
2
|
+
/**
|
|
3
|
+
* Lifts an open `<details>` dropdown's menu out of every scrolling ancestor and
|
|
4
|
+
* places it against the viewport, under (or over) its own summary.
|
|
5
|
+
*
|
|
6
|
+
* The attributes sidebar's panel is `position: sticky` with a `max-height` and
|
|
7
|
+
* `overflow-y: auto`, which clips an absolutely positioned menu at the panel's
|
|
8
|
+
* edge — so the "Add block" list was unreachable by mouse. A viewport-positioned
|
|
9
|
+
* menu is not clipped by an overflow ancestor, and the top layer (`popover`,
|
|
10
|
+
* where the browser has it) also lifts it clear of a transformed ancestor, which
|
|
11
|
+
* a `position: fixed` element alone would still be trapped inside.
|
|
12
|
+
*
|
|
13
|
+
* Positioning only. Dismissal stays with `dropdownDismiss`, which the menu's
|
|
14
|
+
* unchanged place in the DOM tree keeps working.
|
|
15
|
+
*/
|
|
16
|
+
export declare function dropdownFloat(node: HTMLDetailsElement): ActionReturn;
|