uncial 0.0.1
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/LICENSE +21 -0
- package/README.md +228 -0
- package/dist/core/attributes.d.ts +11 -0
- package/dist/core/attributes.js +149 -0
- package/dist/core/defineBlock.d.ts +2 -0
- package/dist/core/defineBlock.js +77 -0
- package/dist/core/index.d.ts +7 -0
- package/dist/core/index.js +6 -0
- package/dist/core/normalize.d.ts +5 -0
- package/dist/core/normalize.js +38 -0
- package/dist/core/registry.d.ts +4 -0
- package/dist/core/registry.js +41 -0
- package/dist/core/types.d.ts +130 -0
- package/dist/core/types.js +1 -0
- package/dist/core/validate.d.ts +4 -0
- package/dist/core/validate.js +181 -0
- package/dist/editor/BlockAttributesPanel.svelte +354 -0
- package/dist/editor/BlockAttributesPanel.svelte.d.ts +9 -0
- package/dist/editor/BlockMenu.svelte +63 -0
- package/dist/editor/BlockMenu.svelte.d.ts +24 -0
- package/dist/editor/BlockNodeView.svelte +88 -0
- package/dist/editor/BlockNodeView.svelte.d.ts +14 -0
- package/dist/editor/Editor.svelte +453 -0
- package/dist/editor/Editor.svelte.d.ts +17 -0
- package/dist/editor/LinkAttributesPanel.svelte +96 -0
- package/dist/editor/LinkAttributesPanel.svelte.d.ts +17 -0
- package/dist/editor/RichTextAttributeEditor.svelte +232 -0
- package/dist/editor/RichTextAttributeEditor.svelte.d.ts +11 -0
- package/dist/editor/Toolbar.svelte +124 -0
- package/dist/editor/Toolbar.svelte.d.ts +14 -0
- package/dist/editor/attributesController.d.ts +64 -0
- package/dist/editor/attributesController.js +608 -0
- package/dist/editor/bindEditor.d.ts +15 -0
- package/dist/editor/bindEditor.js +159 -0
- package/dist/editor/createEditorExtensions.d.ts +1 -0
- package/dist/editor/createEditorExtensions.js +1 -0
- package/dist/editor/index.d.ts +11 -0
- package/dist/editor/index.js +8 -0
- package/dist/editor/toolbarFeatures.d.ts +26 -0
- package/dist/editor/toolbarFeatures.js +144 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/render/ContainerBlockFixture.svelte +22 -0
- package/dist/render/ContainerBlockFixture.svelte.d.ts +10 -0
- package/dist/render/Renderer.svelte +40 -0
- package/dist/render/Renderer.svelte.d.ts +11 -0
- package/dist/render/RichContent.svelte +30 -0
- package/dist/render/RichContent.svelte.d.ts +10 -0
- package/dist/render/RichNode.svelte +105 -0
- package/dist/render/RichNode.svelte.d.ts +10 -0
- package/dist/render/RichText.svelte +29 -0
- package/dist/render/RichText.svelte.d.ts +8 -0
- package/dist/render/index.d.ts +2 -0
- package/dist/render/index.js +2 -0
- package/dist/render/sanitize.d.ts +1 -0
- package/dist/render/sanitize.js +17 -0
- package/dist/shared/codeBlockAttributes.d.ts +31 -0
- package/dist/shared/codeBlockAttributes.js +25 -0
- package/dist/shared/content.d.ts +2 -0
- package/dist/shared/content.js +7 -0
- package/dist/shared/document.d.ts +18 -0
- package/dist/shared/document.js +1 -0
- package/dist/shared/richText.d.ts +11 -0
- package/dist/shared/richText.js +108 -0
- package/dist/shared/syntaxHighlight.d.ts +16 -0
- package/dist/shared/syntaxHighlight.js +47 -0
- package/dist/shared/tiptap.d.ts +7 -0
- package/dist/shared/tiptap.js +259 -0
- package/package.json +116 -0
|
@@ -0,0 +1,608 @@
|
|
|
1
|
+
import { get, writable } from 'svelte/store';
|
|
2
|
+
import { NodeSelection } from '@tiptap/pm/state';
|
|
3
|
+
import { getBlockDefaultAttrs } from '../shared/tiptap.js';
|
|
4
|
+
import { parseBlockDraftAttributes, toAttributeDraftValues } from '../core/attributes.js';
|
|
5
|
+
import { CODE_BLOCK_ID, codeBlockAttributeTarget } from '../shared/codeBlockAttributes.js';
|
|
6
|
+
const INITIAL_STATE = {
|
|
7
|
+
open: false,
|
|
8
|
+
mode: null,
|
|
9
|
+
selectedBlockId: '',
|
|
10
|
+
draftAttrs: {},
|
|
11
|
+
validationErrors: {},
|
|
12
|
+
activeBlock: null,
|
|
13
|
+
allowedBlockIds: [],
|
|
14
|
+
containerChildren: [],
|
|
15
|
+
link: {
|
|
16
|
+
open: false,
|
|
17
|
+
attrs: {}
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
function getActiveLinkAttrs(editor) {
|
|
21
|
+
if (!editor?.isActive('link'))
|
|
22
|
+
return null;
|
|
23
|
+
return editor.getAttributes('link');
|
|
24
|
+
}
|
|
25
|
+
function findActiveBlock(editor, registry) {
|
|
26
|
+
if (!editor || !registry)
|
|
27
|
+
return null;
|
|
28
|
+
const selection = editor.state.selection;
|
|
29
|
+
const selectedNode = selection.node;
|
|
30
|
+
const selectedType = selectedNode?.type?.name;
|
|
31
|
+
if (selectedType && (registry.has(selectedType) || selectedType === CODE_BLOCK_ID)) {
|
|
32
|
+
return {
|
|
33
|
+
id: selectedType,
|
|
34
|
+
attrs: selectedNode?.attrs ?? {},
|
|
35
|
+
pos: selection.from
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
for (let depth = selection.$from.depth; depth > 0; depth -= 1) {
|
|
39
|
+
const node = selection.$from.node(depth);
|
|
40
|
+
const nodeType = node.type.name;
|
|
41
|
+
if (!registry.has(nodeType) && nodeType !== CODE_BLOCK_ID) {
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
id: nodeType,
|
|
46
|
+
attrs: node.attrs ?? {},
|
|
47
|
+
pos: selection.$from.before(depth)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (selection.$from.depth > 0)
|
|
51
|
+
return null;
|
|
52
|
+
const adjacentNode = selection.$from.nodeAfter ?? selection.$from.nodeBefore ?? null;
|
|
53
|
+
const adjacentType = adjacentNode?.type?.name;
|
|
54
|
+
if (!adjacentType || (!registry.has(adjacentType) && adjacentType !== CODE_BLOCK_ID))
|
|
55
|
+
return null;
|
|
56
|
+
return {
|
|
57
|
+
id: adjacentType,
|
|
58
|
+
attrs: adjacentNode?.attrs ?? {},
|
|
59
|
+
pos: selection.from
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function findBlockAt(editor, registry, pos) {
|
|
63
|
+
if (!editor || !registry || pos < 0 || pos > editor.state.doc.content.size)
|
|
64
|
+
return null;
|
|
65
|
+
const node = editor.state.doc.nodeAt(pos);
|
|
66
|
+
if (!node || (!registry.has(node.type.name) && node.type.name !== CODE_BLOCK_ID))
|
|
67
|
+
return null;
|
|
68
|
+
return {
|
|
69
|
+
id: node.type.name,
|
|
70
|
+
attrs: node.attrs ?? {},
|
|
71
|
+
pos
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export function createBlockAttributesController() {
|
|
75
|
+
const state = writable(INITIAL_STATE);
|
|
76
|
+
let editor = null;
|
|
77
|
+
let registry = null;
|
|
78
|
+
let schema = undefined;
|
|
79
|
+
let explicitContainerOpenUntil = 0;
|
|
80
|
+
let applyingDraftAttrs = false;
|
|
81
|
+
function allowedBlocks() {
|
|
82
|
+
if (!registry)
|
|
83
|
+
return [];
|
|
84
|
+
return registry.blocks
|
|
85
|
+
.map((block) => block.id)
|
|
86
|
+
.filter((id) => !schema || schema.allowedBlocks.has(id));
|
|
87
|
+
}
|
|
88
|
+
function getAttributeTarget(id) {
|
|
89
|
+
if (id === CODE_BLOCK_ID)
|
|
90
|
+
return codeBlockAttributeTarget;
|
|
91
|
+
return registry?.get(id) ?? null;
|
|
92
|
+
}
|
|
93
|
+
function resetDraftForBlock(blockId) {
|
|
94
|
+
if (!registry || !blockId) {
|
|
95
|
+
state.update((current) => ({ ...current, draftAttrs: {} }));
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const block = getAttributeTarget(blockId);
|
|
99
|
+
state.update((current) => ({
|
|
100
|
+
...current,
|
|
101
|
+
draftAttrs: block
|
|
102
|
+
? toAttributeDraftValues(block, blockId === CODE_BLOCK_ID ? {} : getBlockDefaultAttrs(block))
|
|
103
|
+
: {},
|
|
104
|
+
validationErrors: {}
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
function getContainerChildrenInfo(activeOverride) {
|
|
108
|
+
if (!editor || !registry)
|
|
109
|
+
return [];
|
|
110
|
+
const active = activeOverride ?? findActiveBlock(editor, registry);
|
|
111
|
+
if (!active)
|
|
112
|
+
return [];
|
|
113
|
+
const block = registry.get(active.id);
|
|
114
|
+
if (!block || !block.content)
|
|
115
|
+
return [];
|
|
116
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
117
|
+
if (!node)
|
|
118
|
+
return [];
|
|
119
|
+
const children = [];
|
|
120
|
+
node.forEach((child, offset, index) => {
|
|
121
|
+
const childBlock = registry.get(child.type.name);
|
|
122
|
+
const label = childBlock?.label ?? child.type.name;
|
|
123
|
+
let summary = '';
|
|
124
|
+
const attrs = child.attrs;
|
|
125
|
+
if (attrs) {
|
|
126
|
+
for (const key of ['title', 'name', 'label', 'heading']) {
|
|
127
|
+
const val = attrs[key];
|
|
128
|
+
if (typeof val === 'string' && val.trim()) {
|
|
129
|
+
summary = val.trim().length > 40 ? val.trim().slice(0, 40) + '…' : val.trim();
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const childAttrs = child.attrs;
|
|
135
|
+
const stableAttr = childAttrs?.id ?? childAttrs?.key ?? childAttrs?.name ?? childAttrs?.title;
|
|
136
|
+
children.push({
|
|
137
|
+
index,
|
|
138
|
+
key: `${child.type.name}:${String(stableAttr ?? '')}:${offset}:${index}`,
|
|
139
|
+
blockId: child.type.name,
|
|
140
|
+
label,
|
|
141
|
+
summary
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
return children;
|
|
145
|
+
}
|
|
146
|
+
function syncFromSelection(source = 'selection') {
|
|
147
|
+
const active = findActiveBlock(editor, registry);
|
|
148
|
+
const activeLinkAttrs = getActiveLinkAttrs(editor);
|
|
149
|
+
state.update((current) => {
|
|
150
|
+
const activeBlock = active ? getAttributeTarget(active.id) : undefined;
|
|
151
|
+
const activeIsCodeBlock = active?.id === CODE_BLOCK_ID;
|
|
152
|
+
const shouldCloseOnSelection = Boolean(source === 'selection' &&
|
|
153
|
+
current.open &&
|
|
154
|
+
current.mode === 'edit' &&
|
|
155
|
+
!activeLinkAttrs &&
|
|
156
|
+
(!active || active.id !== current.selectedBlockId));
|
|
157
|
+
if (shouldCloseOnSelection) {
|
|
158
|
+
return {
|
|
159
|
+
...current,
|
|
160
|
+
open: false,
|
|
161
|
+
mode: null,
|
|
162
|
+
activeBlock: null,
|
|
163
|
+
containerChildren: [],
|
|
164
|
+
validationErrors: {}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const currentActive = current.activeBlock
|
|
168
|
+
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
169
|
+
: null;
|
|
170
|
+
const currentActiveIsContainer = Boolean(currentActive && getAttributeTarget(currentActive.id)?.content);
|
|
171
|
+
const shouldPreserveContainerOnUpdate = Boolean(source === 'update' && current.open && current.mode === 'edit' && currentActiveIsContainer);
|
|
172
|
+
const shouldEditActiveAtom = Boolean((current.open || activeIsCodeBlock) &&
|
|
173
|
+
active &&
|
|
174
|
+
activeBlock &&
|
|
175
|
+
!activeBlock.content &&
|
|
176
|
+
!shouldPreserveContainerOnUpdate);
|
|
177
|
+
const explicitContainerOpen = Boolean(current.open &&
|
|
178
|
+
current.mode === 'edit' &&
|
|
179
|
+
currentActiveIsContainer &&
|
|
180
|
+
(!shouldEditActiveAtom ||
|
|
181
|
+
shouldPreserveContainerOnUpdate ||
|
|
182
|
+
Date.now() < explicitContainerOpenUntil));
|
|
183
|
+
const selectedBlockId = explicitContainerOpen
|
|
184
|
+
? currentActive.id
|
|
185
|
+
: shouldEditActiveAtom
|
|
186
|
+
? active.id
|
|
187
|
+
: current.selectedBlockId;
|
|
188
|
+
const nextActive = explicitContainerOpen ? currentActive : active;
|
|
189
|
+
const nextMode = explicitContainerOpen ||
|
|
190
|
+
shouldEditActiveAtom ||
|
|
191
|
+
(current.open && selectedBlockId && active?.id === selectedBlockId)
|
|
192
|
+
? 'edit'
|
|
193
|
+
: current.open && selectedBlockId
|
|
194
|
+
? 'insert'
|
|
195
|
+
: current.mode;
|
|
196
|
+
const shouldPreserveDraftAttrs = Boolean(source === 'update' && applyingDraftAttrs && current.open && current.mode === 'edit');
|
|
197
|
+
const nextDraft = shouldPreserveDraftAttrs
|
|
198
|
+
? current.draftAttrs
|
|
199
|
+
: nextMode === 'edit' && active && activeBlock && active.id === selectedBlockId
|
|
200
|
+
? toAttributeDraftValues(activeBlock, active.attrs)
|
|
201
|
+
: explicitContainerOpen && currentActive && getAttributeTarget(currentActive.id)
|
|
202
|
+
? toAttributeDraftValues(getAttributeTarget(currentActive.id), currentActive.attrs)
|
|
203
|
+
: current.draftAttrs;
|
|
204
|
+
return {
|
|
205
|
+
...current,
|
|
206
|
+
open: current.open || activeIsCodeBlock,
|
|
207
|
+
selectedBlockId,
|
|
208
|
+
activeBlock: nextActive,
|
|
209
|
+
mode: nextMode,
|
|
210
|
+
draftAttrs: nextDraft,
|
|
211
|
+
validationErrors: nextMode === 'edit' ? {} : current.validationErrors,
|
|
212
|
+
containerChildren: getContainerChildrenInfo(nextActive),
|
|
213
|
+
link: activeLinkAttrs ? { open: true, attrs: activeLinkAttrs } : { open: false, attrs: {} }
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
function selectBlock(blockId) {
|
|
218
|
+
const ids = allowedBlocks();
|
|
219
|
+
const isAllowed = ids.includes(blockId);
|
|
220
|
+
const nextId = isAllowed ? blockId : '';
|
|
221
|
+
state.update((current) => ({
|
|
222
|
+
...current,
|
|
223
|
+
selectedBlockId: nextId,
|
|
224
|
+
mode: null,
|
|
225
|
+
validationErrors: {}
|
|
226
|
+
}));
|
|
227
|
+
resetDraftForBlock(nextId);
|
|
228
|
+
}
|
|
229
|
+
function openAttributes(blockId) {
|
|
230
|
+
if (blockId !== undefined) {
|
|
231
|
+
selectBlock(blockId);
|
|
232
|
+
}
|
|
233
|
+
const current = get(state);
|
|
234
|
+
const active = findActiveBlock(editor, registry);
|
|
235
|
+
let selectedBlockId = blockId ?? active?.id ?? current.selectedBlockId;
|
|
236
|
+
if (!selectedBlockId && current.activeBlock?.id) {
|
|
237
|
+
selectedBlockId = current.activeBlock.id;
|
|
238
|
+
selectBlock(selectedBlockId);
|
|
239
|
+
}
|
|
240
|
+
if (!selectedBlockId)
|
|
241
|
+
return;
|
|
242
|
+
const mode = active?.id === selectedBlockId ? 'edit' : 'insert';
|
|
243
|
+
const activeTarget = active ? getAttributeTarget(active.id) : null;
|
|
244
|
+
const draftAttrs = mode === 'edit' && active && activeTarget
|
|
245
|
+
? toAttributeDraftValues(activeTarget, active.attrs)
|
|
246
|
+
: get(state).draftAttrs;
|
|
247
|
+
state.update((next) => ({
|
|
248
|
+
...next,
|
|
249
|
+
open: true,
|
|
250
|
+
selectedBlockId,
|
|
251
|
+
mode,
|
|
252
|
+
draftAttrs,
|
|
253
|
+
validationErrors: {}
|
|
254
|
+
}));
|
|
255
|
+
}
|
|
256
|
+
function openAttributesAt(pos) {
|
|
257
|
+
const active = findBlockAt(editor, registry, pos);
|
|
258
|
+
if (!active || !registry)
|
|
259
|
+
return;
|
|
260
|
+
const block = getAttributeTarget(active.id);
|
|
261
|
+
if (!block)
|
|
262
|
+
return;
|
|
263
|
+
explicitContainerOpenUntil = block.content ? Date.now() + 300 : 0;
|
|
264
|
+
if (editor) {
|
|
265
|
+
const tr = editor.state.tr.setSelection(NodeSelection.create(editor.state.doc, active.pos));
|
|
266
|
+
editor.view.dispatch(tr);
|
|
267
|
+
}
|
|
268
|
+
state.update((current) => ({
|
|
269
|
+
...current,
|
|
270
|
+
open: true,
|
|
271
|
+
mode: 'edit',
|
|
272
|
+
selectedBlockId: active.id,
|
|
273
|
+
activeBlock: active,
|
|
274
|
+
draftAttrs: toAttributeDraftValues(block, active.attrs),
|
|
275
|
+
validationErrors: {},
|
|
276
|
+
containerChildren: getContainerChildrenInfo(active)
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
function closeAttributes() {
|
|
280
|
+
state.update((current) => ({
|
|
281
|
+
...current,
|
|
282
|
+
open: false,
|
|
283
|
+
mode: null,
|
|
284
|
+
validationErrors: {}
|
|
285
|
+
}));
|
|
286
|
+
}
|
|
287
|
+
function openLinkAttributes() {
|
|
288
|
+
if (!editor)
|
|
289
|
+
return;
|
|
290
|
+
const attrs = getActiveLinkAttrs(editor) ?? { href: '' };
|
|
291
|
+
state.update((current) => ({
|
|
292
|
+
...current,
|
|
293
|
+
link: { open: true, attrs }
|
|
294
|
+
}));
|
|
295
|
+
}
|
|
296
|
+
function closeLinkAttributes() {
|
|
297
|
+
state.update((current) => ({
|
|
298
|
+
...current,
|
|
299
|
+
link: { open: false, attrs: {} }
|
|
300
|
+
}));
|
|
301
|
+
}
|
|
302
|
+
function setLinkAttr(name, value) {
|
|
303
|
+
state.update((current) => ({
|
|
304
|
+
...current,
|
|
305
|
+
link: {
|
|
306
|
+
open: true,
|
|
307
|
+
attrs: {
|
|
308
|
+
...current.link.attrs,
|
|
309
|
+
[name]: value
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
function commitLinkAttributes() {
|
|
315
|
+
if (!editor)
|
|
316
|
+
return false;
|
|
317
|
+
const attrs = get(state).link.attrs;
|
|
318
|
+
const href = attrs.href?.trim();
|
|
319
|
+
if (!href)
|
|
320
|
+
return false;
|
|
321
|
+
return editor
|
|
322
|
+
.chain()
|
|
323
|
+
.focus()
|
|
324
|
+
.extendMarkRange('link')
|
|
325
|
+
.setMark('link', {
|
|
326
|
+
...attrs,
|
|
327
|
+
href,
|
|
328
|
+
target: attrs.target || null,
|
|
329
|
+
rel: attrs.rel || null,
|
|
330
|
+
title: attrs.title?.trim() || null,
|
|
331
|
+
class: attrs.class?.trim() || null
|
|
332
|
+
})
|
|
333
|
+
.run();
|
|
334
|
+
}
|
|
335
|
+
function removeLink() {
|
|
336
|
+
if (!editor)
|
|
337
|
+
return false;
|
|
338
|
+
const ok = editor.chain().focus().extendMarkRange('link').unsetLink().run();
|
|
339
|
+
if (ok)
|
|
340
|
+
closeLinkAttributes();
|
|
341
|
+
return ok;
|
|
342
|
+
}
|
|
343
|
+
function parseAndValidateDraft(block, draftAttrs) {
|
|
344
|
+
const attrs = parseBlockDraftAttributes(block, draftAttrs);
|
|
345
|
+
const validationErrors = Object.fromEntries(Object.entries(block.attributes).flatMap(([name, spec]) => {
|
|
346
|
+
const value = attrs[name];
|
|
347
|
+
if ((value === undefined || value === null || value === '') && spec.required) {
|
|
348
|
+
return [[name, 'Required']];
|
|
349
|
+
}
|
|
350
|
+
if (spec.validate && !spec.validate(value)) {
|
|
351
|
+
return [[name, 'Invalid value']];
|
|
352
|
+
}
|
|
353
|
+
return [];
|
|
354
|
+
}));
|
|
355
|
+
return { attrs, validationErrors };
|
|
356
|
+
}
|
|
357
|
+
function setDraftAttr(name, value) {
|
|
358
|
+
const current = get(state);
|
|
359
|
+
const nextDraftAttrs = {
|
|
360
|
+
...current.draftAttrs,
|
|
361
|
+
[name]: value
|
|
362
|
+
};
|
|
363
|
+
state.update((draft) => ({
|
|
364
|
+
...draft,
|
|
365
|
+
draftAttrs: nextDraftAttrs,
|
|
366
|
+
validationErrors: {
|
|
367
|
+
...draft.validationErrors,
|
|
368
|
+
[name]: ''
|
|
369
|
+
}
|
|
370
|
+
}));
|
|
371
|
+
if (current.mode !== 'edit' || !current.activeBlock || !current.selectedBlockId)
|
|
372
|
+
return;
|
|
373
|
+
const block = getAttributeTarget(current.selectedBlockId);
|
|
374
|
+
if (!block)
|
|
375
|
+
return;
|
|
376
|
+
const { attrs, validationErrors } = parseAndValidateDraft(block, nextDraftAttrs);
|
|
377
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
378
|
+
state.update((draft) => ({ ...draft, validationErrors }));
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const active = findBlockAt(editor, registry, current.activeBlock.pos);
|
|
382
|
+
if (!editor || active?.id !== current.selectedBlockId)
|
|
383
|
+
return;
|
|
384
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
385
|
+
if (!node)
|
|
386
|
+
return;
|
|
387
|
+
applyingDraftAttrs = true;
|
|
388
|
+
try {
|
|
389
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(active.pos, undefined, attrs));
|
|
390
|
+
}
|
|
391
|
+
finally {
|
|
392
|
+
applyingDraftAttrs = false;
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
function canEditSelectedBlock(blockId) {
|
|
396
|
+
const current = get(state);
|
|
397
|
+
const explicitActive = current.activeBlock
|
|
398
|
+
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
399
|
+
: null;
|
|
400
|
+
return (explicitActive ?? findActiveBlock(editor, registry))?.id === blockId;
|
|
401
|
+
}
|
|
402
|
+
function insertBlock(blockId, attrs = {}) {
|
|
403
|
+
if (!editor || !registry?.has(blockId))
|
|
404
|
+
return false;
|
|
405
|
+
const block = registry.get(blockId);
|
|
406
|
+
if (!block)
|
|
407
|
+
return false;
|
|
408
|
+
return editor
|
|
409
|
+
.chain()
|
|
410
|
+
.focus()
|
|
411
|
+
.insertContent({
|
|
412
|
+
type: blockId,
|
|
413
|
+
attrs
|
|
414
|
+
})
|
|
415
|
+
.run();
|
|
416
|
+
}
|
|
417
|
+
function updateSelectedBlockAttrs(blockId, partialAttrs) {
|
|
418
|
+
if (!editor || !canEditSelectedBlock(blockId))
|
|
419
|
+
return false;
|
|
420
|
+
return editor.chain().focus().updateAttributes(blockId, partialAttrs).run();
|
|
421
|
+
}
|
|
422
|
+
function commit() {
|
|
423
|
+
const current = get(state);
|
|
424
|
+
if (!current.selectedBlockId)
|
|
425
|
+
return false;
|
|
426
|
+
const block = getAttributeTarget(current.selectedBlockId);
|
|
427
|
+
if (!block)
|
|
428
|
+
return false;
|
|
429
|
+
const { attrs, validationErrors } = parseAndValidateDraft(block, current.draftAttrs);
|
|
430
|
+
if (Object.keys(validationErrors).length > 0) {
|
|
431
|
+
state.update((draft) => ({ ...draft, validationErrors }));
|
|
432
|
+
return false;
|
|
433
|
+
}
|
|
434
|
+
let ok = false;
|
|
435
|
+
if (current.mode === 'edit' && current.activeBlock) {
|
|
436
|
+
const active = findBlockAt(editor, registry, current.activeBlock.pos);
|
|
437
|
+
if (editor && active?.id === current.selectedBlockId) {
|
|
438
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
439
|
+
if (node) {
|
|
440
|
+
editor.view.dispatch(editor.state.tr.setNodeMarkup(active.pos, undefined, attrs));
|
|
441
|
+
ok = true;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
ok = insertBlock(current.selectedBlockId, attrs);
|
|
447
|
+
}
|
|
448
|
+
if (!ok)
|
|
449
|
+
return false;
|
|
450
|
+
closeAttributes();
|
|
451
|
+
syncFromSelection('update');
|
|
452
|
+
return true;
|
|
453
|
+
}
|
|
454
|
+
function attach(nextEditor, nextRegistry, nextSchema) {
|
|
455
|
+
editor = nextEditor;
|
|
456
|
+
registry = nextRegistry;
|
|
457
|
+
schema = nextSchema;
|
|
458
|
+
state.update((current) => ({
|
|
459
|
+
...current,
|
|
460
|
+
allowedBlockIds: allowedBlocks()
|
|
461
|
+
}));
|
|
462
|
+
syncFromSelection('update');
|
|
463
|
+
}
|
|
464
|
+
function detach() {
|
|
465
|
+
editor = null;
|
|
466
|
+
registry = null;
|
|
467
|
+
schema = undefined;
|
|
468
|
+
state.set(INITIAL_STATE);
|
|
469
|
+
}
|
|
470
|
+
function getActiveBlock() {
|
|
471
|
+
return findActiveBlock(editor, registry);
|
|
472
|
+
}
|
|
473
|
+
function isSelectionAutoOpenSuppressed() {
|
|
474
|
+
return Date.now() < explicitContainerOpenUntil;
|
|
475
|
+
}
|
|
476
|
+
function moveContainerChild(fromIndex, toIndex) {
|
|
477
|
+
if (!editor || !registry || fromIndex === toIndex)
|
|
478
|
+
return false;
|
|
479
|
+
const current = get(state);
|
|
480
|
+
const active = current.activeBlock
|
|
481
|
+
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
482
|
+
: null;
|
|
483
|
+
if (!active)
|
|
484
|
+
return false;
|
|
485
|
+
const block = registry.get(active.id);
|
|
486
|
+
if (!block || !block.content)
|
|
487
|
+
return false;
|
|
488
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
489
|
+
if (!node)
|
|
490
|
+
return false;
|
|
491
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
492
|
+
const children = [];
|
|
493
|
+
node.forEach((child) => children.push(child));
|
|
494
|
+
if (fromIndex < 0 ||
|
|
495
|
+
fromIndex >= children.length ||
|
|
496
|
+
toIndex < 0 ||
|
|
497
|
+
toIndex >= children.length) {
|
|
498
|
+
return false;
|
|
499
|
+
}
|
|
500
|
+
const [moved] = children.splice(fromIndex, 1);
|
|
501
|
+
children.splice(toIndex, 0, moved);
|
|
502
|
+
const { tr } = editor.state;
|
|
503
|
+
const contentStart = active.pos + 1;
|
|
504
|
+
const contentEnd = active.pos + node.nodeSize - 1;
|
|
505
|
+
explicitContainerOpenUntil = Date.now() + 1000;
|
|
506
|
+
tr.replaceWith(contentStart, contentEnd, children);
|
|
507
|
+
editor.view.dispatch(tr);
|
|
508
|
+
syncFromSelection('update');
|
|
509
|
+
return true;
|
|
510
|
+
}
|
|
511
|
+
function insertContainerChild(blockId, attrs) {
|
|
512
|
+
if (!editor || !registry?.has(blockId))
|
|
513
|
+
return false;
|
|
514
|
+
if (schema && !schema.allowedBlocks.has(blockId))
|
|
515
|
+
return false;
|
|
516
|
+
const current = get(state);
|
|
517
|
+
const active = current.activeBlock
|
|
518
|
+
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
519
|
+
: null;
|
|
520
|
+
if (!active)
|
|
521
|
+
return false;
|
|
522
|
+
const containerBlock = registry.get(active.id);
|
|
523
|
+
if (!containerBlock?.content)
|
|
524
|
+
return false;
|
|
525
|
+
const containerNode = editor.state.doc.nodeAt(active.pos);
|
|
526
|
+
if (!containerNode)
|
|
527
|
+
return false;
|
|
528
|
+
const childBlock = registry.get(blockId);
|
|
529
|
+
const childNodeType = editor.schema.nodes[blockId];
|
|
530
|
+
if (!childBlock || !childNodeType)
|
|
531
|
+
return false;
|
|
532
|
+
const childNode = childNodeType.createAndFill(attrs ?? getBlockDefaultAttrs(childBlock));
|
|
533
|
+
if (!childNode)
|
|
534
|
+
return false;
|
|
535
|
+
explicitContainerOpenUntil = Date.now() + 1000;
|
|
536
|
+
editor.view.dispatch(editor.state.tr.insert(active.pos + containerNode.nodeSize - 1, childNode));
|
|
537
|
+
syncFromSelection('update');
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
function removeActiveBlock() {
|
|
541
|
+
if (!editor || !registry)
|
|
542
|
+
return false;
|
|
543
|
+
const current = get(state);
|
|
544
|
+
const active = current.activeBlock
|
|
545
|
+
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
546
|
+
: null;
|
|
547
|
+
if (!active)
|
|
548
|
+
return false;
|
|
549
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
550
|
+
if (!node)
|
|
551
|
+
return false;
|
|
552
|
+
editor.view.dispatch(editor.state.tr.delete(active.pos, active.pos + node.nodeSize));
|
|
553
|
+
closeAttributes();
|
|
554
|
+
syncFromSelection('update');
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
function removeContainerChild(index) {
|
|
558
|
+
if (!editor || !registry)
|
|
559
|
+
return false;
|
|
560
|
+
const current = get(state);
|
|
561
|
+
const active = current.activeBlock
|
|
562
|
+
? findBlockAt(editor, registry, current.activeBlock.pos)
|
|
563
|
+
: null;
|
|
564
|
+
if (!active)
|
|
565
|
+
return false;
|
|
566
|
+
const block = registry.get(active.id);
|
|
567
|
+
if (!block?.content)
|
|
568
|
+
return false;
|
|
569
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
570
|
+
if (!node || node.childCount <= 1 || index < 0 || index >= node.childCount)
|
|
571
|
+
return false;
|
|
572
|
+
let childStart = active.pos + 1;
|
|
573
|
+
for (let i = 0; i < index; i += 1) {
|
|
574
|
+
childStart += node.child(i).nodeSize;
|
|
575
|
+
}
|
|
576
|
+
const child = node.child(index);
|
|
577
|
+
explicitContainerOpenUntil = Date.now() + 1000;
|
|
578
|
+
editor.view.dispatch(editor.state.tr.delete(childStart, childStart + child.nodeSize));
|
|
579
|
+
syncFromSelection('update');
|
|
580
|
+
return true;
|
|
581
|
+
}
|
|
582
|
+
return {
|
|
583
|
+
subscribe: state.subscribe,
|
|
584
|
+
attach,
|
|
585
|
+
detach,
|
|
586
|
+
openAttributes,
|
|
587
|
+
openAttributesAt,
|
|
588
|
+
closeAttributes,
|
|
589
|
+
openLinkAttributes,
|
|
590
|
+
closeLinkAttributes,
|
|
591
|
+
setLinkAttr,
|
|
592
|
+
commitLinkAttributes,
|
|
593
|
+
removeLink,
|
|
594
|
+
selectBlock,
|
|
595
|
+
setDraftAttr,
|
|
596
|
+
commit,
|
|
597
|
+
insertBlock,
|
|
598
|
+
updateSelectedBlockAttrs,
|
|
599
|
+
canEditSelectedBlock,
|
|
600
|
+
getActiveBlock,
|
|
601
|
+
syncFromSelection,
|
|
602
|
+
isSelectionAutoOpenSuppressed,
|
|
603
|
+
moveContainerChild,
|
|
604
|
+
insertContainerChild,
|
|
605
|
+
removeActiveBlock,
|
|
606
|
+
removeContainerChild
|
|
607
|
+
};
|
|
608
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Editor as TiptapEditor, type AnyExtension, type JSONContent } from '@tiptap/core';
|
|
2
|
+
import type { ActionReturn } from 'svelte/action';
|
|
3
|
+
import type { BlockDefinition, BlockRegistry, ContentSchema, ValidationIssue } from '../core/types.js';
|
|
4
|
+
import { type BlockAttributesController } from './attributesController.js';
|
|
5
|
+
export interface BindEditorOptions {
|
|
6
|
+
blocks?: BlockRegistry | BlockDefinition[];
|
|
7
|
+
schema?: ContentSchema;
|
|
8
|
+
json?: JSONContent;
|
|
9
|
+
extensions?: AnyExtension[];
|
|
10
|
+
attributesController?: BlockAttributesController | null;
|
|
11
|
+
onIssue?: (issue: ValidationIssue) => void;
|
|
12
|
+
onChange?: (json: JSONContent) => void;
|
|
13
|
+
onEditor?: (editor: TiptapEditor | null) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function bindEditor(node: HTMLElement, initialOptions?: BindEditorOptions): ActionReturn<BindEditorOptions>;
|