uncial 0.0.7 → 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 +82 -15
- package/dist/core/attributes.d.ts +21 -3
- package/dist/core/attributes.js +86 -15
- package/dist/core/defineBlock.js +26 -12
- package/dist/core/index.d.ts +3 -1
- package/dist/core/index.js +2 -1
- package/dist/core/issues.d.ts +7 -0
- package/dist/core/issues.js +9 -0
- package/dist/core/meta.js +2 -7
- package/dist/core/migrations.d.ts +30 -0
- package/dist/core/migrations.js +50 -0
- package/dist/core/normalize.d.ts +12 -1
- package/dist/core/normalize.js +123 -22
- package/dist/core/registry.d.ts +7 -0
- package/dist/core/registry.js +35 -4
- package/dist/core/runtime.d.ts +7 -0
- package/dist/core/types.d.ts +31 -3
- package/dist/core/validate.js +27 -7
- package/dist/editor/AttributeFieldControl.svelte +155 -7
- package/dist/editor/AttributeFieldControl.svelte.d.ts +1 -0
- package/dist/editor/BlockAttributesPanel.svelte +76 -44
- package/dist/editor/BlockAttributesPanel.svelte.d.ts +9 -1
- package/dist/editor/Editor.svelte +149 -27
- package/dist/editor/Editor.svelte.d.ts +37 -0
- package/dist/editor/RichTextAttributeEditor.svelte +75 -145
- package/dist/editor/RichTextAttributeEditor.svelte.d.ts +2 -1
- package/dist/editor/Toolbar.svelte +2 -3
- package/dist/editor/attributeTargets.d.ts +23 -0
- package/dist/editor/attributeTargets.js +21 -0
- package/dist/editor/attributesController.d.ts +12 -0
- package/dist/editor/attributesController.js +142 -230
- package/dist/editor/bindEditor.d.ts +7 -0
- package/dist/editor/bindEditor.js +141 -27
- package/dist/editor/chooseAttribute.d.ts +26 -0
- package/dist/editor/chooseAttribute.js +10 -0
- package/dist/editor/containerChildOps.d.ts +33 -0
- package/dist/editor/containerChildOps.js +115 -0
- package/dist/editor/dropdownDismiss.d.ts +18 -0
- package/dist/editor/dropdownDismiss.js +46 -0
- package/dist/editor/dropdownFloat.d.ts +16 -0
- package/dist/editor/dropdownFloat.js +127 -0
- package/dist/editor/index.d.ts +3 -1
- package/dist/editor/index.js +2 -1
- package/dist/editor/linkController.d.ts +19 -0
- package/dist/editor/linkController.js +72 -0
- package/dist/editor/toolbarFeatures.d.ts +12 -0
- package/dist/editor/toolbarFeatures.js +36 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/render/Renderer.svelte +1 -1
- package/dist/render/RichContent.svelte +12 -4
- package/dist/render/RichContent.svelte.d.ts +2 -0
- package/dist/render/RichNode.svelte +80 -10
- 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/sanitize.d.ts +6 -0
- package/dist/render/sanitize.js +20 -1
- package/dist/render/tabSelection.svelte.d.ts +8 -0
- package/dist/render/tabSelection.svelte.js +40 -0
- package/dist/runtime/reactiveProps.svelte.d.ts +16 -0
- package/dist/runtime/reactiveProps.svelte.js +16 -0
- package/dist/runtime/svelte.d.ts +2 -1
- package/dist/runtime/svelte.js +26 -6
- package/dist/shared/EditorBlockFixture.svelte +24 -0
- package/dist/shared/EditorBlockFixture.svelte.d.ts +9 -0
- package/dist/shared/content.js +2 -1
- package/dist/shared/documentIdentity.d.ts +6 -0
- package/dist/shared/documentIdentity.js +64 -0
- package/dist/shared/guards.d.ts +13 -0
- package/dist/shared/guards.js +16 -0
- package/dist/shared/richText.js +20 -4
- package/dist/shared/runtimeId.d.ts +8 -0
- package/dist/shared/runtimeId.js +8 -0
- package/dist/shared/syntaxHighlight.d.ts +14 -0
- package/dist/shared/syntaxHighlight.js +36 -3
- package/dist/shared/tiptap.js +140 -27
- 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 +23 -7
- package/dist/web-components/UncialEditor.svelte.d.ts +7 -2
- package/dist/web-components/index.d.ts +45 -10
- package/dist/web-components/index.js +98 -36
- package/dist/web-components/reactiveProps.svelte.d.ts +10 -0
- package/dist/web-components/reactiveProps.svelte.js +13 -0
- package/package.json +134 -133
|
@@ -1,35 +1,39 @@
|
|
|
1
1
|
import { get, writable } from 'svelte/store';
|
|
2
2
|
import { NodeSelection } from '@tiptap/pm/state';
|
|
3
|
-
import { getBlockDefaultAttrs } from '../shared/tiptap.js';
|
|
4
3
|
import { parseBlockDraftAttributes, toAttributeDraftValues } from '../core/attributes.js';
|
|
5
|
-
import { CODE_BLOCK_ID
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
import { CODE_BLOCK_ID } from '../shared/codeBlockAttributes.js';
|
|
5
|
+
import { createAttributeTargets } from './attributeTargets.js';
|
|
6
|
+
import { createLinkController, getActiveLinkAttrs } from './linkController.js';
|
|
7
|
+
import { createContainerChildOps } from './containerChildOps.js';
|
|
8
|
+
/**
|
|
9
|
+
* The single source of truth for a fresh controller state snapshot. Every
|
|
10
|
+
* consumer that needs an initial `BlockAttributesState` (the controller store,
|
|
11
|
+
* `bindEditor`, and the Svelte panels) imports this factory rather than
|
|
12
|
+
* duplicating the literal, so the shape can never drift between them.
|
|
13
|
+
*/
|
|
14
|
+
export function createInitialState() {
|
|
15
|
+
return {
|
|
17
16
|
open: false,
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
mode: null,
|
|
18
|
+
selectedBlockId: '',
|
|
19
|
+
draftAttrs: {},
|
|
20
|
+
validationErrors: {},
|
|
21
|
+
activeBlock: null,
|
|
22
|
+
allowedBlockIds: [],
|
|
23
|
+
containerChildren: [],
|
|
24
|
+
link: {
|
|
25
|
+
open: false,
|
|
26
|
+
attrs: {}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
25
29
|
}
|
|
26
|
-
function findActiveBlock(editor,
|
|
27
|
-
if (!editor
|
|
30
|
+
function findActiveBlock(editor, targets) {
|
|
31
|
+
if (!editor)
|
|
28
32
|
return null;
|
|
29
33
|
const selection = editor.state.selection;
|
|
30
34
|
const selectedNode = selection.node;
|
|
31
35
|
const selectedType = selectedNode?.type?.name;
|
|
32
|
-
if (selectedType &&
|
|
36
|
+
if (selectedType && targets.has(selectedType)) {
|
|
33
37
|
return {
|
|
34
38
|
id: selectedType,
|
|
35
39
|
attrs: selectedNode?.attrs ?? {},
|
|
@@ -39,7 +43,7 @@ function findActiveBlock(editor, registry) {
|
|
|
39
43
|
for (let depth = selection.$from.depth; depth > 0; depth -= 1) {
|
|
40
44
|
const node = selection.$from.node(depth);
|
|
41
45
|
const nodeType = node.type.name;
|
|
42
|
-
if (!
|
|
46
|
+
if (!targets.has(nodeType)) {
|
|
43
47
|
continue;
|
|
44
48
|
}
|
|
45
49
|
return {
|
|
@@ -52,7 +56,7 @@ function findActiveBlock(editor, registry) {
|
|
|
52
56
|
return null;
|
|
53
57
|
const adjacentNode = selection.$from.nodeAfter ?? selection.$from.nodeBefore ?? null;
|
|
54
58
|
const adjacentType = adjacentNode?.type?.name;
|
|
55
|
-
if (!adjacentType ||
|
|
59
|
+
if (!adjacentType || !targets.has(adjacentType))
|
|
56
60
|
return null;
|
|
57
61
|
return {
|
|
58
62
|
id: adjacentType,
|
|
@@ -60,11 +64,11 @@ function findActiveBlock(editor, registry) {
|
|
|
60
64
|
pos: selection.from
|
|
61
65
|
};
|
|
62
66
|
}
|
|
63
|
-
function findBlockAt(editor,
|
|
64
|
-
if (!editor ||
|
|
67
|
+
function findBlockAt(editor, targets, pos) {
|
|
68
|
+
if (!editor || pos < 0 || pos > editor.state.doc.content.size)
|
|
65
69
|
return null;
|
|
66
70
|
const node = editor.state.doc.nodeAt(pos);
|
|
67
|
-
if (!node ||
|
|
71
|
+
if (!node || !targets.has(node.type.name))
|
|
68
72
|
return null;
|
|
69
73
|
return {
|
|
70
74
|
id: node.type.name,
|
|
@@ -73,12 +77,42 @@ function findBlockAt(editor, registry, pos) {
|
|
|
73
77
|
};
|
|
74
78
|
}
|
|
75
79
|
export function createBlockAttributesController() {
|
|
76
|
-
const state = writable(
|
|
80
|
+
const state = writable(createInitialState());
|
|
77
81
|
let editor = null;
|
|
78
82
|
let registry = null;
|
|
79
83
|
let schema = undefined;
|
|
80
|
-
|
|
84
|
+
// Attribute-metadata resolver over the current registry plus the virtual code
|
|
85
|
+
// block; rebuilt whenever the registry changes (attach/detach).
|
|
86
|
+
let targets = createAttributeTargets(null);
|
|
87
|
+
// One-shot suppression of `bindEditor`'s auto-open behavior. The controller
|
|
88
|
+
// dispatches its own transactions (selecting a block, mutating a container's
|
|
89
|
+
// children); Tiptap fires the resulting `selectionUpdate`/`update` events
|
|
90
|
+
// SYNCHRONOUSLY inside `view.dispatch`, so a flag that is raised for the
|
|
91
|
+
// duration of the dispatch and lowered immediately after covers exactly those
|
|
92
|
+
// self-triggered echo events and cannot wedge (the `finally` always clears it).
|
|
93
|
+
let suppressAutoOpen = false;
|
|
81
94
|
let applyingDraftAttrs = false;
|
|
95
|
+
const link = createLinkController(() => editor, state);
|
|
96
|
+
const containerOps = createContainerChildOps({
|
|
97
|
+
getEditor: () => editor,
|
|
98
|
+
getRegistry: () => registry,
|
|
99
|
+
getSchema: () => schema,
|
|
100
|
+
resolveActiveBlock: () => resolveActiveBlock(get(state)),
|
|
101
|
+
dispatch: (tr) => dispatchSuppressingAutoOpen(tr),
|
|
102
|
+
sync: () => syncFromSelection('update')
|
|
103
|
+
});
|
|
104
|
+
function dispatchSuppressingAutoOpen(tr) {
|
|
105
|
+
if (!editor)
|
|
106
|
+
return;
|
|
107
|
+
const previous = suppressAutoOpen;
|
|
108
|
+
suppressAutoOpen = true;
|
|
109
|
+
try {
|
|
110
|
+
editor.view.dispatch(tr);
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
suppressAutoOpen = previous;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
82
116
|
function allowedBlocks() {
|
|
83
117
|
if (!registry)
|
|
84
118
|
return [];
|
|
@@ -86,29 +120,25 @@ export function createBlockAttributesController() {
|
|
|
86
120
|
.map((block) => block.id)
|
|
87
121
|
.filter((id) => !schema || schema.allowedBlocks.has(id));
|
|
88
122
|
}
|
|
89
|
-
function
|
|
90
|
-
|
|
91
|
-
return codeBlockAttributeTarget;
|
|
92
|
-
return registry?.get(id) ?? null;
|
|
123
|
+
function resolveActiveBlock(current) {
|
|
124
|
+
return current.activeBlock ? findBlockAt(editor, targets, current.activeBlock.pos) : null;
|
|
93
125
|
}
|
|
94
126
|
function resetDraftForBlock(blockId) {
|
|
95
127
|
if (!registry || !blockId) {
|
|
96
128
|
state.update((current) => ({ ...current, draftAttrs: {} }));
|
|
97
129
|
return;
|
|
98
130
|
}
|
|
99
|
-
const block =
|
|
131
|
+
const block = targets.get(blockId);
|
|
100
132
|
state.update((current) => ({
|
|
101
133
|
...current,
|
|
102
|
-
draftAttrs: block
|
|
103
|
-
? toAttributeDraftValues(block, blockId === CODE_BLOCK_ID ? {} : getBlockDefaultAttrs(block))
|
|
104
|
-
: {},
|
|
134
|
+
draftAttrs: block ? toAttributeDraftValues(block, targets.defaultAttrs(blockId)) : {},
|
|
105
135
|
validationErrors: {}
|
|
106
136
|
}));
|
|
107
137
|
}
|
|
108
138
|
function getContainerChildrenInfo(activeOverride) {
|
|
109
139
|
if (!editor || !registry)
|
|
110
140
|
return [];
|
|
111
|
-
const active = activeOverride ?? findActiveBlock(editor,
|
|
141
|
+
const active = activeOverride ?? findActiveBlock(editor, targets);
|
|
112
142
|
if (!active)
|
|
113
143
|
return [];
|
|
114
144
|
const block = registry.get(active.id);
|
|
@@ -139,16 +169,17 @@ export function createBlockAttributesController() {
|
|
|
139
169
|
key: `${child.type.name}:${String(stableAttr ?? '')}:${offset}:${index}`,
|
|
140
170
|
blockId: child.type.name,
|
|
141
171
|
label,
|
|
142
|
-
summary
|
|
172
|
+
summary,
|
|
173
|
+
pos: active.pos + 1 + offset
|
|
143
174
|
});
|
|
144
175
|
});
|
|
145
176
|
return children;
|
|
146
177
|
}
|
|
147
178
|
function syncFromSelection(source = 'selection') {
|
|
148
|
-
const active = findActiveBlock(editor,
|
|
179
|
+
const active = findActiveBlock(editor, targets);
|
|
149
180
|
const activeLinkAttrs = getActiveLinkAttrs(editor);
|
|
150
181
|
state.update((current) => {
|
|
151
|
-
const activeBlock = active ?
|
|
182
|
+
const activeBlock = active ? targets.get(active.id) : undefined;
|
|
152
183
|
const activeIsCodeBlock = active?.id === CODE_BLOCK_ID;
|
|
153
184
|
const shouldCloseOnSelection = Boolean(source === 'selection' &&
|
|
154
185
|
current.open &&
|
|
@@ -165,10 +196,8 @@ export function createBlockAttributesController() {
|
|
|
165
196
|
validationErrors: {}
|
|
166
197
|
};
|
|
167
198
|
}
|
|
168
|
-
const currentActive = current
|
|
169
|
-
|
|
170
|
-
: null;
|
|
171
|
-
const currentActiveIsContainer = Boolean(currentActive && getAttributeTarget(currentActive.id)?.content);
|
|
199
|
+
const currentActive = resolveActiveBlock(current);
|
|
200
|
+
const currentActiveIsContainer = Boolean(currentActive && targets.get(currentActive.id)?.content);
|
|
172
201
|
const shouldPreserveContainerOnUpdate = Boolean(source === 'update' && current.open && current.mode === 'edit' && currentActiveIsContainer);
|
|
173
202
|
const shouldEditActiveAtom = Boolean((current.open || activeIsCodeBlock) &&
|
|
174
203
|
active &&
|
|
@@ -178,9 +207,7 @@ export function createBlockAttributesController() {
|
|
|
178
207
|
const explicitContainerOpen = Boolean(current.open &&
|
|
179
208
|
current.mode === 'edit' &&
|
|
180
209
|
currentActiveIsContainer &&
|
|
181
|
-
(!shouldEditActiveAtom ||
|
|
182
|
-
shouldPreserveContainerOnUpdate ||
|
|
183
|
-
Date.now() < explicitContainerOpenUntil));
|
|
210
|
+
(!shouldEditActiveAtom || shouldPreserveContainerOnUpdate));
|
|
184
211
|
const selectedBlockId = explicitContainerOpen
|
|
185
212
|
? currentActive.id
|
|
186
213
|
: shouldEditActiveAtom
|
|
@@ -199,8 +226,8 @@ export function createBlockAttributesController() {
|
|
|
199
226
|
? current.draftAttrs
|
|
200
227
|
: nextMode === 'edit' && active && activeBlock && active.id === selectedBlockId
|
|
201
228
|
? toAttributeDraftValues(activeBlock, active.attrs)
|
|
202
|
-
: explicitContainerOpen && currentActive &&
|
|
203
|
-
? toAttributeDraftValues(
|
|
229
|
+
: explicitContainerOpen && currentActive && targets.get(currentActive.id)
|
|
230
|
+
? toAttributeDraftValues(targets.get(currentActive.id), currentActive.attrs)
|
|
204
231
|
: current.draftAttrs;
|
|
205
232
|
return {
|
|
206
233
|
...current,
|
|
@@ -209,7 +236,10 @@ export function createBlockAttributesController() {
|
|
|
209
236
|
activeBlock: nextActive,
|
|
210
237
|
mode: nextMode,
|
|
211
238
|
draftAttrs: nextDraft,
|
|
212
|
-
|
|
239
|
+
// A write-through from setDraftAttr echoes back here as an 'update';
|
|
240
|
+
// wiping errors then would hide a still-invalid sibling field's error
|
|
241
|
+
// right after a valid edit to another field.
|
|
242
|
+
validationErrors: nextMode === 'edit' && !shouldPreserveDraftAttrs ? {} : current.validationErrors,
|
|
213
243
|
containerChildren: getContainerChildrenInfo(nextActive),
|
|
214
244
|
link: activeLinkAttrs ? { open: true, attrs: activeLinkAttrs } : { open: false, attrs: {} }
|
|
215
245
|
};
|
|
@@ -232,7 +262,7 @@ export function createBlockAttributesController() {
|
|
|
232
262
|
selectBlock(blockId);
|
|
233
263
|
}
|
|
234
264
|
const current = get(state);
|
|
235
|
-
const active = findActiveBlock(editor,
|
|
265
|
+
const active = findActiveBlock(editor, targets);
|
|
236
266
|
let selectedBlockId = blockId ?? active?.id ?? current.selectedBlockId;
|
|
237
267
|
if (!selectedBlockId && current.activeBlock?.id) {
|
|
238
268
|
selectedBlockId = current.activeBlock.id;
|
|
@@ -241,7 +271,7 @@ export function createBlockAttributesController() {
|
|
|
241
271
|
if (!selectedBlockId)
|
|
242
272
|
return;
|
|
243
273
|
const mode = active?.id === selectedBlockId ? 'edit' : 'insert';
|
|
244
|
-
const activeTarget = active ?
|
|
274
|
+
const activeTarget = active ? targets.get(active.id) : null;
|
|
245
275
|
const draftAttrs = mode === 'edit' && active && activeTarget
|
|
246
276
|
? toAttributeDraftValues(activeTarget, active.attrs)
|
|
247
277
|
: get(state).draftAttrs;
|
|
@@ -255,16 +285,14 @@ export function createBlockAttributesController() {
|
|
|
255
285
|
}));
|
|
256
286
|
}
|
|
257
287
|
function openAttributesAt(pos) {
|
|
258
|
-
const active = findBlockAt(editor,
|
|
288
|
+
const active = findBlockAt(editor, targets, pos);
|
|
259
289
|
if (!active || !registry)
|
|
260
290
|
return;
|
|
261
|
-
const block =
|
|
291
|
+
const block = targets.get(active.id);
|
|
262
292
|
if (!block)
|
|
263
293
|
return;
|
|
264
|
-
explicitContainerOpenUntil = block.content ? Date.now() + 300 : 0;
|
|
265
294
|
if (editor) {
|
|
266
|
-
|
|
267
|
-
editor.view.dispatch(tr);
|
|
295
|
+
dispatchSuppressingAutoOpen(editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, active.pos)));
|
|
268
296
|
}
|
|
269
297
|
state.update((current) => ({
|
|
270
298
|
...current,
|
|
@@ -285,62 +313,6 @@ export function createBlockAttributesController() {
|
|
|
285
313
|
validationErrors: {}
|
|
286
314
|
}));
|
|
287
315
|
}
|
|
288
|
-
function openLinkAttributes() {
|
|
289
|
-
if (!editor)
|
|
290
|
-
return;
|
|
291
|
-
const attrs = getActiveLinkAttrs(editor) ?? { href: '' };
|
|
292
|
-
state.update((current) => ({
|
|
293
|
-
...current,
|
|
294
|
-
link: { open: true, attrs }
|
|
295
|
-
}));
|
|
296
|
-
}
|
|
297
|
-
function closeLinkAttributes() {
|
|
298
|
-
state.update((current) => ({
|
|
299
|
-
...current,
|
|
300
|
-
link: { open: false, attrs: {} }
|
|
301
|
-
}));
|
|
302
|
-
}
|
|
303
|
-
function setLinkAttr(name, value) {
|
|
304
|
-
state.update((current) => ({
|
|
305
|
-
...current,
|
|
306
|
-
link: {
|
|
307
|
-
open: true,
|
|
308
|
-
attrs: {
|
|
309
|
-
...current.link.attrs,
|
|
310
|
-
[name]: value
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
}));
|
|
314
|
-
}
|
|
315
|
-
function commitLinkAttributes() {
|
|
316
|
-
if (!editor)
|
|
317
|
-
return false;
|
|
318
|
-
const attrs = get(state).link.attrs;
|
|
319
|
-
const href = sanitizeHref(attrs.href);
|
|
320
|
-
if (!href)
|
|
321
|
-
return false;
|
|
322
|
-
return editor
|
|
323
|
-
.chain()
|
|
324
|
-
.focus()
|
|
325
|
-
.extendMarkRange('link')
|
|
326
|
-
.setMark('link', {
|
|
327
|
-
...attrs,
|
|
328
|
-
href,
|
|
329
|
-
target: attrs.target || null,
|
|
330
|
-
rel: attrs.rel || null,
|
|
331
|
-
title: attrs.title?.trim() || null,
|
|
332
|
-
class: attrs.class?.trim() || null
|
|
333
|
-
})
|
|
334
|
-
.run();
|
|
335
|
-
}
|
|
336
|
-
function removeLink() {
|
|
337
|
-
if (!editor)
|
|
338
|
-
return false;
|
|
339
|
-
const ok = editor.chain().focus().extendMarkRange('link').unsetLink().run();
|
|
340
|
-
if (ok)
|
|
341
|
-
closeLinkAttributes();
|
|
342
|
-
return ok;
|
|
343
|
-
}
|
|
344
316
|
function parseAndValidateDraft(block, draftAttrs) {
|
|
345
317
|
const attrs = parseBlockDraftAttributes(block, draftAttrs);
|
|
346
318
|
const validationErrors = Object.fromEntries(Object.entries(block.attributes).flatMap(([name, spec]) => {
|
|
@@ -357,6 +329,8 @@ export function createBlockAttributesController() {
|
|
|
357
329
|
}
|
|
358
330
|
function setDraftAttr(name, value) {
|
|
359
331
|
const current = get(state);
|
|
332
|
+
if (current.selectedBlockId && targets.get(current.selectedBlockId)?.readOnly)
|
|
333
|
+
return;
|
|
360
334
|
const nextDraftAttrs = {
|
|
361
335
|
...current.draftAttrs,
|
|
362
336
|
[name]: value
|
|
@@ -371,23 +345,49 @@ export function createBlockAttributesController() {
|
|
|
371
345
|
}));
|
|
372
346
|
if (current.mode !== 'edit' || !current.activeBlock || !current.selectedBlockId)
|
|
373
347
|
return;
|
|
374
|
-
const block =
|
|
348
|
+
const block = targets.get(current.selectedBlockId);
|
|
375
349
|
if (!block)
|
|
376
350
|
return;
|
|
377
351
|
const { attrs, validationErrors } = parseAndValidateDraft(block, nextDraftAttrs);
|
|
378
|
-
|
|
379
|
-
|
|
352
|
+
state.update((draft) => ({ ...draft, validationErrors: { [name]: '', ...validationErrors } }));
|
|
353
|
+
// Only the EDITED field gates its own write-through. An invalid sibling
|
|
354
|
+
// field (a cleared required title, an image block whose required image is
|
|
355
|
+
// not chosen yet) must not silently block live edits to other fields —
|
|
356
|
+
// that reads as "the attribute editor stopped working".
|
|
357
|
+
if (validationErrors[name])
|
|
380
358
|
return;
|
|
381
|
-
|
|
382
|
-
const active = findBlockAt(editor, registry, current.activeBlock.pos);
|
|
359
|
+
const active = findBlockAt(editor, targets, current.activeBlock.pos);
|
|
383
360
|
if (!editor || active?.id !== current.selectedBlockId)
|
|
384
361
|
return;
|
|
385
362
|
const node = editor.state.doc.nodeAt(active.pos);
|
|
386
363
|
if (!node)
|
|
387
364
|
return;
|
|
365
|
+
// Write valid fields through; fields whose draft is invalid keep the
|
|
366
|
+
// node's current (last valid) values.
|
|
367
|
+
const appliedAttrs = { ...attrs };
|
|
368
|
+
for (const invalidName of Object.keys(validationErrors)) {
|
|
369
|
+
appliedAttrs[invalidName] = node.attrs?.[invalidName];
|
|
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;
|
|
388
388
|
applyingDraftAttrs = true;
|
|
389
389
|
try {
|
|
390
|
-
editor.view.dispatch(
|
|
390
|
+
editor.view.dispatch(tr);
|
|
391
391
|
}
|
|
392
392
|
finally {
|
|
393
393
|
applyingDraftAttrs = false;
|
|
@@ -395,10 +395,9 @@ export function createBlockAttributesController() {
|
|
|
395
395
|
}
|
|
396
396
|
function canEditSelectedBlock(blockId) {
|
|
397
397
|
const current = get(state);
|
|
398
|
-
const explicitActive = current
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
return (explicitActive ?? findActiveBlock(editor, registry))?.id === blockId;
|
|
398
|
+
const explicitActive = resolveActiveBlock(current);
|
|
399
|
+
return (!targets.get(blockId)?.readOnly &&
|
|
400
|
+
(explicitActive ?? findActiveBlock(editor, targets))?.id === blockId);
|
|
402
401
|
}
|
|
403
402
|
function insertBlock(blockId, attrs = {}) {
|
|
404
403
|
if (!editor || !registry?.has(blockId))
|
|
@@ -424,9 +423,11 @@ export function createBlockAttributesController() {
|
|
|
424
423
|
const current = get(state);
|
|
425
424
|
if (!current.selectedBlockId)
|
|
426
425
|
return false;
|
|
427
|
-
const block =
|
|
426
|
+
const block = targets.get(current.selectedBlockId);
|
|
428
427
|
if (!block)
|
|
429
428
|
return false;
|
|
429
|
+
if (current.mode === 'edit' && block.readOnly)
|
|
430
|
+
return false;
|
|
430
431
|
const { attrs, validationErrors } = parseAndValidateDraft(block, current.draftAttrs);
|
|
431
432
|
if (Object.keys(validationErrors).length > 0) {
|
|
432
433
|
state.update((draft) => ({ ...draft, validationErrors }));
|
|
@@ -434,7 +435,7 @@ export function createBlockAttributesController() {
|
|
|
434
435
|
}
|
|
435
436
|
let ok = false;
|
|
436
437
|
if (current.mode === 'edit' && current.activeBlock) {
|
|
437
|
-
const active = findBlockAt(editor,
|
|
438
|
+
const active = findBlockAt(editor, targets, current.activeBlock.pos);
|
|
438
439
|
if (editor && active?.id === current.selectedBlockId) {
|
|
439
440
|
const node = editor.state.doc.nodeAt(active.pos);
|
|
440
441
|
if (node) {
|
|
@@ -456,6 +457,7 @@ export function createBlockAttributesController() {
|
|
|
456
457
|
editor = nextEditor;
|
|
457
458
|
registry = nextRegistry;
|
|
458
459
|
schema = nextSchema;
|
|
460
|
+
targets = createAttributeTargets(nextRegistry);
|
|
459
461
|
state.update((current) => ({
|
|
460
462
|
...current,
|
|
461
463
|
allowedBlockIds: allowedBlocks()
|
|
@@ -466,85 +468,20 @@ export function createBlockAttributesController() {
|
|
|
466
468
|
editor = null;
|
|
467
469
|
registry = null;
|
|
468
470
|
schema = undefined;
|
|
469
|
-
|
|
471
|
+
targets = createAttributeTargets(null);
|
|
472
|
+
state.set(createInitialState());
|
|
470
473
|
}
|
|
471
474
|
function getActiveBlock() {
|
|
472
|
-
return findActiveBlock(editor,
|
|
475
|
+
return findActiveBlock(editor, targets);
|
|
473
476
|
}
|
|
474
477
|
function isSelectionAutoOpenSuppressed() {
|
|
475
|
-
return
|
|
476
|
-
}
|
|
477
|
-
function moveContainerChild(fromIndex, toIndex) {
|
|
478
|
-
if (!editor || !registry || fromIndex === toIndex)
|
|
479
|
-
return false;
|
|
480
|
-
const current = get(state);
|
|
481
|
-
const active = current.activeBlock
|
|
482
|
-
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
483
|
-
: null;
|
|
484
|
-
if (!active)
|
|
485
|
-
return false;
|
|
486
|
-
const block = registry.get(active.id);
|
|
487
|
-
if (!block || !block.content)
|
|
488
|
-
return false;
|
|
489
|
-
const node = editor.state.doc.nodeAt(active.pos);
|
|
490
|
-
if (!node)
|
|
491
|
-
return false;
|
|
492
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
493
|
-
const children = [];
|
|
494
|
-
node.forEach((child) => children.push(child));
|
|
495
|
-
if (fromIndex < 0 ||
|
|
496
|
-
fromIndex >= children.length ||
|
|
497
|
-
toIndex < 0 ||
|
|
498
|
-
toIndex >= children.length) {
|
|
499
|
-
return false;
|
|
500
|
-
}
|
|
501
|
-
const [moved] = children.splice(fromIndex, 1);
|
|
502
|
-
children.splice(toIndex, 0, moved);
|
|
503
|
-
const { tr } = editor.state;
|
|
504
|
-
const contentStart = active.pos + 1;
|
|
505
|
-
const contentEnd = active.pos + node.nodeSize - 1;
|
|
506
|
-
explicitContainerOpenUntil = Date.now() + 1000;
|
|
507
|
-
tr.replaceWith(contentStart, contentEnd, children);
|
|
508
|
-
editor.view.dispatch(tr);
|
|
509
|
-
syncFromSelection('update');
|
|
510
|
-
return true;
|
|
511
|
-
}
|
|
512
|
-
function insertContainerChild(blockId, attrs) {
|
|
513
|
-
if (!editor || !registry?.has(blockId))
|
|
514
|
-
return false;
|
|
515
|
-
if (schema && !schema.allowedBlocks.has(blockId))
|
|
516
|
-
return false;
|
|
517
|
-
const current = get(state);
|
|
518
|
-
const active = current.activeBlock
|
|
519
|
-
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
520
|
-
: null;
|
|
521
|
-
if (!active)
|
|
522
|
-
return false;
|
|
523
|
-
const containerBlock = registry.get(active.id);
|
|
524
|
-
if (!containerBlock?.content)
|
|
525
|
-
return false;
|
|
526
|
-
const containerNode = editor.state.doc.nodeAt(active.pos);
|
|
527
|
-
if (!containerNode)
|
|
528
|
-
return false;
|
|
529
|
-
const childBlock = registry.get(blockId);
|
|
530
|
-
const childNodeType = editor.schema.nodes[blockId];
|
|
531
|
-
if (!childBlock || !childNodeType)
|
|
532
|
-
return false;
|
|
533
|
-
const childNode = childNodeType.createAndFill(attrs ?? getBlockDefaultAttrs(childBlock));
|
|
534
|
-
if (!childNode)
|
|
535
|
-
return false;
|
|
536
|
-
explicitContainerOpenUntil = Date.now() + 1000;
|
|
537
|
-
editor.view.dispatch(editor.state.tr.insert(active.pos + containerNode.nodeSize - 1, childNode));
|
|
538
|
-
syncFromSelection('update');
|
|
539
|
-
return true;
|
|
478
|
+
return suppressAutoOpen;
|
|
540
479
|
}
|
|
541
480
|
function removeActiveBlock() {
|
|
542
481
|
if (!editor || !registry)
|
|
543
482
|
return false;
|
|
544
483
|
const current = get(state);
|
|
545
|
-
const active = current
|
|
546
|
-
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
547
|
-
: null;
|
|
484
|
+
const active = resolveActiveBlock(current);
|
|
548
485
|
if (!active)
|
|
549
486
|
return false;
|
|
550
487
|
const node = editor.state.doc.nodeAt(active.pos);
|
|
@@ -555,31 +492,6 @@ export function createBlockAttributesController() {
|
|
|
555
492
|
syncFromSelection('update');
|
|
556
493
|
return true;
|
|
557
494
|
}
|
|
558
|
-
function removeContainerChild(index) {
|
|
559
|
-
if (!editor || !registry)
|
|
560
|
-
return false;
|
|
561
|
-
const current = get(state);
|
|
562
|
-
const active = current.activeBlock
|
|
563
|
-
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
564
|
-
: null;
|
|
565
|
-
if (!active)
|
|
566
|
-
return false;
|
|
567
|
-
const block = registry.get(active.id);
|
|
568
|
-
if (!block?.content)
|
|
569
|
-
return false;
|
|
570
|
-
const node = editor.state.doc.nodeAt(active.pos);
|
|
571
|
-
if (!node || node.childCount <= 1 || index < 0 || index >= node.childCount)
|
|
572
|
-
return false;
|
|
573
|
-
let childStart = active.pos + 1;
|
|
574
|
-
for (let i = 0; i < index; i += 1) {
|
|
575
|
-
childStart += node.child(i).nodeSize;
|
|
576
|
-
}
|
|
577
|
-
const child = node.child(index);
|
|
578
|
-
explicitContainerOpenUntil = Date.now() + 1000;
|
|
579
|
-
editor.view.dispatch(editor.state.tr.delete(childStart, childStart + child.nodeSize));
|
|
580
|
-
syncFromSelection('update');
|
|
581
|
-
return true;
|
|
582
|
-
}
|
|
583
495
|
return {
|
|
584
496
|
subscribe: state.subscribe,
|
|
585
497
|
attach,
|
|
@@ -587,11 +499,11 @@ export function createBlockAttributesController() {
|
|
|
587
499
|
openAttributes,
|
|
588
500
|
openAttributesAt,
|
|
589
501
|
closeAttributes,
|
|
590
|
-
openLinkAttributes,
|
|
591
|
-
closeLinkAttributes,
|
|
592
|
-
setLinkAttr,
|
|
593
|
-
commitLinkAttributes,
|
|
594
|
-
removeLink,
|
|
502
|
+
openLinkAttributes: link.open,
|
|
503
|
+
closeLinkAttributes: link.close,
|
|
504
|
+
setLinkAttr: link.setAttr,
|
|
505
|
+
commitLinkAttributes: link.commit,
|
|
506
|
+
removeLink: link.remove,
|
|
595
507
|
selectBlock,
|
|
596
508
|
setDraftAttr,
|
|
597
509
|
commit,
|
|
@@ -601,9 +513,9 @@ export function createBlockAttributesController() {
|
|
|
601
513
|
getActiveBlock,
|
|
602
514
|
syncFromSelection,
|
|
603
515
|
isSelectionAutoOpenSuppressed,
|
|
604
|
-
moveContainerChild,
|
|
605
|
-
insertContainerChild,
|
|
516
|
+
moveContainerChild: containerOps.move,
|
|
517
|
+
insertContainerChild: containerOps.insert,
|
|
606
518
|
removeActiveBlock,
|
|
607
|
-
removeContainerChild
|
|
519
|
+
removeContainerChild: containerOps.remove
|
|
608
520
|
};
|
|
609
521
|
}
|
|
@@ -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;
|