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,31 +1,56 @@
|
|
|
1
|
-
import { Editor as TiptapEditor } from '@tiptap/core';
|
|
1
|
+
import { Editor as TiptapEditor, getSchema } from '@tiptap/core';
|
|
2
2
|
import { normalizeDocument } from '../core/normalize.js';
|
|
3
3
|
import { validateDocument } from '../core/validate.js';
|
|
4
4
|
import { resolveRegistry } from '../core/registry.js';
|
|
5
5
|
import { emptyDocument } from '../shared/content.js';
|
|
6
6
|
import { createEditorExtensions } from '../shared/tiptap.js';
|
|
7
|
-
import { createBlockAttributesController } from './attributesController.js';
|
|
7
|
+
import { createBlockAttributesController, createInitialState } from './attributesController.js';
|
|
8
8
|
function toEditorDocument(document) {
|
|
9
9
|
const { version: _version, meta: _meta, ...editorDocument } = document;
|
|
10
10
|
void _version;
|
|
11
11
|
void _meta;
|
|
12
12
|
return editorDocument;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Pre-validates a document against the Tiptap editor schema. Tiptap v3
|
|
16
|
+
* replaces the ENTIRE document with empty content when any node in the
|
|
17
|
+
* initial content is invalid for the editor schema (e.g. an unknown block
|
|
18
|
+
* type that core normalization deliberately preserves), which would then
|
|
19
|
+
* propagate an empty document to the host via `onChange`. Instead, strip only
|
|
20
|
+
* the offending top-level nodes, report each one as an issue, and hand Tiptap
|
|
21
|
+
* the surviving siblings.
|
|
22
|
+
*/
|
|
23
|
+
function sanitizeForEditorSchema(editorDocument, editorSchema) {
|
|
24
|
+
try {
|
|
25
|
+
editorSchema.nodeFromJSON(editorDocument).check();
|
|
26
|
+
return { document: editorDocument, issues: [] };
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
// Fall through and strip the offending top-level nodes.
|
|
30
|
+
}
|
|
31
|
+
const content = Array.isArray(editorDocument.content) ? editorDocument.content : [];
|
|
32
|
+
const surviving = [];
|
|
33
|
+
const issues = [];
|
|
34
|
+
content.forEach((child, index) => {
|
|
35
|
+
try {
|
|
36
|
+
editorSchema.nodeFromJSON(child).check();
|
|
37
|
+
surviving.push(child);
|
|
27
38
|
}
|
|
28
|
-
|
|
39
|
+
catch (error) {
|
|
40
|
+
const blockType = typeof child?.type === 'string' ? child.type : undefined;
|
|
41
|
+
issues.push({
|
|
42
|
+
code: 'UNKNOWN_BLOCK',
|
|
43
|
+
path: ['content', index],
|
|
44
|
+
message: `Node "${blockType ?? '(unknown)'}" is not supported by the editor schema and was excluded from the editing session`,
|
|
45
|
+
severity: 'warning',
|
|
46
|
+
details: {
|
|
47
|
+
block: blockType,
|
|
48
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return { document: { ...editorDocument, content: surviving }, issues };
|
|
29
54
|
}
|
|
30
55
|
export function bindEditor(node, initialOptions = {}) {
|
|
31
56
|
const internalController = createBlockAttributesController();
|
|
@@ -33,19 +58,63 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
33
58
|
let registry = resolveRegistry(options.blocks ?? []);
|
|
34
59
|
let schema = options.schema;
|
|
35
60
|
let controller = options.attributesController ?? internalController;
|
|
36
|
-
let controllerState =
|
|
61
|
+
let controllerState = createInitialState();
|
|
37
62
|
let unsubscribe = controller.subscribe((state) => {
|
|
38
63
|
controllerState = state;
|
|
39
64
|
});
|
|
40
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();
|
|
69
|
+
// `lastSerialized` is the editor *body* serialization (meta/version stripped)
|
|
70
|
+
// and gates the `setContent` push in `syncEditorDocument`. `lastEmitted` is
|
|
71
|
+
// the *full* normalized serialization (body + meta + version) and gates the
|
|
72
|
+
// validate + emit pass, so a metadata-only change is still validated and
|
|
73
|
+
// emitted even when the body is unchanged.
|
|
41
74
|
let lastSerialized = '';
|
|
42
|
-
|
|
43
|
-
|
|
75
|
+
let lastEmitted = '';
|
|
76
|
+
function normalize(document) {
|
|
77
|
+
return normalizeDocument({ ...(document ?? emptyDocument()), meta: options.meta }, registry, schema);
|
|
78
|
+
}
|
|
79
|
+
function validate(normalized) {
|
|
44
80
|
if (schema) {
|
|
45
81
|
validateDocument(normalized, registry, schema, { onIssue: options.onIssue });
|
|
46
82
|
}
|
|
83
|
+
}
|
|
84
|
+
function normalizeAndValidate(document) {
|
|
85
|
+
const normalized = normalize(document);
|
|
86
|
+
validate(normalized);
|
|
47
87
|
return normalized;
|
|
48
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
|
+
}
|
|
49
118
|
function setController(nextController) {
|
|
50
119
|
if (controller === nextController)
|
|
51
120
|
return;
|
|
@@ -61,6 +130,9 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
61
130
|
}
|
|
62
131
|
}
|
|
63
132
|
function shouldAutoOpenAttributesForSelection(nextEditor) {
|
|
133
|
+
if (options.autoOpenAttributes === false) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
64
136
|
if (controller.isSelectionAutoOpenSuppressed()) {
|
|
65
137
|
return false;
|
|
66
138
|
}
|
|
@@ -87,19 +159,36 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
87
159
|
}
|
|
88
160
|
}
|
|
89
161
|
function createEditor() {
|
|
162
|
+
const extensions = createEditorExtensions(registry, schema, (pos) => controller.openAttributesAt(pos), options.extensions ?? []);
|
|
90
163
|
const initialContent = normalizeAndValidate(options.json ?? emptyDocument());
|
|
91
|
-
|
|
164
|
+
rememberReadOnlyAttrs(initialContent);
|
|
165
|
+
const sanitized = sanitizeForEditorSchema(toEditorDocument(initialContent), getSchema(extensions));
|
|
166
|
+
sanitized.issues.forEach((issue) => options.onIssue?.(issue));
|
|
167
|
+
const initialEditorContent = sanitized.document;
|
|
92
168
|
lastSerialized = JSON.stringify(initialEditorContent);
|
|
169
|
+
lastEmitted = JSON.stringify(initialContent);
|
|
93
170
|
setExternalDocument(initialContent);
|
|
94
171
|
editor = new TiptapEditor({
|
|
95
172
|
element: node,
|
|
96
|
-
extensions
|
|
173
|
+
extensions,
|
|
97
174
|
content: initialEditorContent,
|
|
98
175
|
onUpdate: ({ editor: tiptap }) => {
|
|
99
|
-
const raw = tiptap.getJSON();
|
|
100
|
-
const normalized =
|
|
101
|
-
|
|
102
|
-
|
|
176
|
+
const raw = restoreReadOnlyAttrs(tiptap.getJSON());
|
|
177
|
+
const normalized = normalize(raw);
|
|
178
|
+
rememberReadOnlyAttrs(normalized);
|
|
179
|
+
const serialized = JSON.stringify(normalized);
|
|
180
|
+
// Skip the (comparatively expensive) validate + emit pass when the
|
|
181
|
+
// full normalized document (body + meta) is byte-identical to what
|
|
182
|
+
// we last emitted. A single doc edit round-trips through onChange →
|
|
183
|
+
// the host re-sets `json` → applyOptions, so without this guard each
|
|
184
|
+
// keystroke would normalize+validate+stringify the same document
|
|
185
|
+
// twice per cycle.
|
|
186
|
+
if (serialized !== lastEmitted) {
|
|
187
|
+
lastEmitted = serialized;
|
|
188
|
+
lastSerialized = JSON.stringify(toEditorDocument(normalized));
|
|
189
|
+
validate(normalized);
|
|
190
|
+
setExternalDocument(normalized);
|
|
191
|
+
}
|
|
103
192
|
controller.syncFromSelection('update');
|
|
104
193
|
},
|
|
105
194
|
onSelectionUpdate: ({ editor: tiptap }) => {
|
|
@@ -122,10 +211,12 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
122
211
|
function syncEditorDocument(nextDocument) {
|
|
123
212
|
if (!editor)
|
|
124
213
|
return;
|
|
125
|
-
const
|
|
214
|
+
const sanitized = sanitizeForEditorSchema(toEditorDocument(nextDocument), editor.schema);
|
|
215
|
+
const editorDocument = sanitized.document;
|
|
126
216
|
const serialized = JSON.stringify(editorDocument);
|
|
127
217
|
if (serialized === lastSerialized)
|
|
128
218
|
return;
|
|
219
|
+
sanitized.issues.forEach((issue) => options.onIssue?.(issue));
|
|
129
220
|
editor.commands.setContent(editorDocument, { emitUpdate: false });
|
|
130
221
|
lastSerialized = serialized;
|
|
131
222
|
controller.syncFromSelection('update');
|
|
@@ -138,6 +229,18 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
138
229
|
registry = resolveRegistry(options.blocks ?? []);
|
|
139
230
|
schema = options.schema;
|
|
140
231
|
setController(options.attributesController ?? internalController);
|
|
232
|
+
// Recreation is keyed on *reference* identity of `blocks`/`schema`/
|
|
233
|
+
// `extensions` — a full teardown + rebuild of the Tiptap instance is the
|
|
234
|
+
// only safe way to swap the block set or editor extensions, but it also
|
|
235
|
+
// discards selection/undo state. Callers MUST pass stable references for
|
|
236
|
+
// these across renders and only produce a fresh array/object when the
|
|
237
|
+
// content genuinely changes; otherwise every render (and therefore every
|
|
238
|
+
// keystroke, since editing updates `json`) would remount the editor.
|
|
239
|
+
// The in-repo callers (`Editor.svelte`, the `uncial-editor` custom element)
|
|
240
|
+
// hold these in `$state`/props so they stay reference-stable while only
|
|
241
|
+
// `json`/`meta` change. A structural compare is deliberately avoided here:
|
|
242
|
+
// `blocks` carries functions and Svelte components that are not safely or
|
|
243
|
+
// cheaply serializable.
|
|
141
244
|
const shouldRecreate = forceRecreate ||
|
|
142
245
|
previousBlocks !== options.blocks ||
|
|
143
246
|
previousSchema !== options.schema ||
|
|
@@ -147,7 +250,18 @@ export function bindEditor(node, initialOptions = {}) {
|
|
|
147
250
|
createEditor();
|
|
148
251
|
return;
|
|
149
252
|
}
|
|
150
|
-
const normalized =
|
|
253
|
+
const normalized = normalize(options.json ?? emptyDocument());
|
|
254
|
+
rememberReadOnlyAttrs(normalized);
|
|
255
|
+
const serialized = JSON.stringify(normalized);
|
|
256
|
+
// Same guard as `onUpdate`, keyed on the full document (body + meta): when
|
|
257
|
+
// the external document normalizes to exactly what we last emitted (the
|
|
258
|
+
// common case for the edit round-trip), there is nothing to validate,
|
|
259
|
+
// emit, or push back into Tiptap. `syncEditorDocument` skips the actual
|
|
260
|
+
// `setContent` on its own when only the metadata differs.
|
|
261
|
+
if (serialized === lastEmitted)
|
|
262
|
+
return;
|
|
263
|
+
lastEmitted = serialized;
|
|
264
|
+
validate(normalized);
|
|
151
265
|
setExternalDocument(normalized);
|
|
152
266
|
syncEditorDocument(normalized);
|
|
153
267
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A request from the block attributes panel to resolve a custom attribute value
|
|
3
|
+
* through host-provided UI (for example uncial-wagtail's image chooser). The
|
|
4
|
+
* host receives this via the `onChooseAttribute` prop on `BlockAttributesPanel`
|
|
5
|
+
* and writes the chosen value(s) back through `setAttrs`.
|
|
6
|
+
*/
|
|
7
|
+
export interface ChooseAttributeRequest {
|
|
8
|
+
/** The custom input kind declared on the attribute spec (e.g. `"wagtail-image"`). */
|
|
9
|
+
inputKind: string;
|
|
10
|
+
/** The attribute name being chosen. */
|
|
11
|
+
name: string;
|
|
12
|
+
/** Current draft attribute values for the active block. */
|
|
13
|
+
attrs: Record<string, unknown>;
|
|
14
|
+
/** Write one or more resolved attribute values back into the draft. */
|
|
15
|
+
setAttrs: (attrs: Record<string, unknown>) => void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Name of the legacy `window`-level `CustomEvent` used before `onChooseAttribute`
|
|
19
|
+
* existed. `BlockAttributesPanel` still dispatches it as a fallback when no
|
|
20
|
+
* callback is supplied, so existing global listeners keep working for one
|
|
21
|
+
* release, but a `window` channel cross-talks between multiple editors on a
|
|
22
|
+
* page — prefer the `onChooseAttribute` prop.
|
|
23
|
+
*
|
|
24
|
+
* @deprecated Pass `onChooseAttribute` to `BlockAttributesPanel` instead.
|
|
25
|
+
*/
|
|
26
|
+
export declare const CHOOSE_ATTRIBUTE_EVENT = "uncial:choose-attribute";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Name of the legacy `window`-level `CustomEvent` used before `onChooseAttribute`
|
|
3
|
+
* existed. `BlockAttributesPanel` still dispatches it as a fallback when no
|
|
4
|
+
* callback is supplied, so existing global listeners keep working for one
|
|
5
|
+
* release, but a `window` channel cross-talks between multiple editors on a
|
|
6
|
+
* page — prefer the `onChooseAttribute` prop.
|
|
7
|
+
*
|
|
8
|
+
* @deprecated Pass `onChooseAttribute` to `BlockAttributesPanel` instead.
|
|
9
|
+
*/
|
|
10
|
+
export const CHOOSE_ATTRIBUTE_EVENT = 'uncial:choose-attribute';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Editor } from '@tiptap/core';
|
|
2
|
+
import type { Transaction } from '@tiptap/pm/state';
|
|
3
|
+
import type { BlockRegistry, ContentSchema } from '../core/types.js';
|
|
4
|
+
import type { ActiveBlockSelection } from './attributesController.js';
|
|
5
|
+
export interface ContainerChildOps {
|
|
6
|
+
move(fromIndex: number, toIndex: number): boolean;
|
|
7
|
+
insert(blockId: string, attrs?: Record<string, unknown>): boolean;
|
|
8
|
+
remove(index: number): boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Live view of the controller's mutable context. The ops read the editor,
|
|
12
|
+
* registry and schema lazily (they change on attach/detach) and resolve the
|
|
13
|
+
* currently-open block on demand, so a single ops instance created once at
|
|
14
|
+
* controller construction stays correct across re-attaches.
|
|
15
|
+
*/
|
|
16
|
+
export interface ContainerChildOpsContext {
|
|
17
|
+
getEditor(): Editor | null;
|
|
18
|
+
getRegistry(): BlockRegistry | null;
|
|
19
|
+
getSchema(): ContentSchema | undefined;
|
|
20
|
+
/** The block the panel is currently editing, re-resolved against the live doc. */
|
|
21
|
+
resolveActiveBlock(): ActiveBlockSelection | null;
|
|
22
|
+
/** Dispatch a transaction with `bindEditor`'s auto-open suppressed for its echo. */
|
|
23
|
+
dispatch(tr: Transaction): void;
|
|
24
|
+
/** Re-derive controller state after the doc changed (an 'update' sync). */
|
|
25
|
+
sync(): void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Add / move / remove children of the container block the panel is editing.
|
|
29
|
+
* Each op mutates only the document (never the controller store directly) and
|
|
30
|
+
* then asks the controller to re-sync, so container-child edits produce exactly
|
|
31
|
+
* one state notification — the same as before this was extracted.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createContainerChildOps(ctx: ContainerChildOpsContext): ContainerChildOps;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Fragment } from '@tiptap/pm/model';
|
|
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
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Add / move / remove children of the container block the panel is editing.
|
|
22
|
+
* Each op mutates only the document (never the controller store directly) and
|
|
23
|
+
* then asks the controller to re-sync, so container-child edits produce exactly
|
|
24
|
+
* one state notification — the same as before this was extracted.
|
|
25
|
+
*/
|
|
26
|
+
export function createContainerChildOps(ctx) {
|
|
27
|
+
function move(fromIndex, toIndex) {
|
|
28
|
+
const editor = ctx.getEditor();
|
|
29
|
+
const registry = ctx.getRegistry();
|
|
30
|
+
if (!editor || !registry || fromIndex === toIndex)
|
|
31
|
+
return false;
|
|
32
|
+
const active = ctx.resolveActiveBlock();
|
|
33
|
+
if (!active)
|
|
34
|
+
return false;
|
|
35
|
+
const block = registry.get(active.id);
|
|
36
|
+
if (!block || !block.content)
|
|
37
|
+
return false;
|
|
38
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
39
|
+
if (!node)
|
|
40
|
+
return false;
|
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
42
|
+
const children = [];
|
|
43
|
+
node.forEach((child) => children.push(child));
|
|
44
|
+
if (fromIndex < 0 ||
|
|
45
|
+
fromIndex >= children.length ||
|
|
46
|
+
toIndex < 0 ||
|
|
47
|
+
toIndex >= children.length) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
const [moved] = children.splice(fromIndex, 1);
|
|
51
|
+
children.splice(toIndex, 0, moved);
|
|
52
|
+
const { tr } = editor.state;
|
|
53
|
+
const contentStart = active.pos + 1;
|
|
54
|
+
const contentEnd = active.pos + node.nodeSize - 1;
|
|
55
|
+
tr.replaceWith(contentStart, contentEnd, children);
|
|
56
|
+
ctx.dispatch(tr);
|
|
57
|
+
ctx.sync();
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
function insert(blockId, attrs) {
|
|
61
|
+
const editor = ctx.getEditor();
|
|
62
|
+
const registry = ctx.getRegistry();
|
|
63
|
+
const schema = ctx.getSchema();
|
|
64
|
+
if (!editor || !registry?.has(blockId))
|
|
65
|
+
return false;
|
|
66
|
+
if (schema && !schema.allowedBlocks.has(blockId))
|
|
67
|
+
return false;
|
|
68
|
+
const active = ctx.resolveActiveBlock();
|
|
69
|
+
if (!active)
|
|
70
|
+
return false;
|
|
71
|
+
const containerBlock = registry.get(active.id);
|
|
72
|
+
if (!containerBlock?.content)
|
|
73
|
+
return false;
|
|
74
|
+
const admissible = containerBlock.content.allowedBlocks;
|
|
75
|
+
if (admissible && !admissible.includes(blockId))
|
|
76
|
+
return false;
|
|
77
|
+
const containerNode = editor.state.doc.nodeAt(active.pos);
|
|
78
|
+
if (!containerNode)
|
|
79
|
+
return false;
|
|
80
|
+
const childBlock = registry.get(blockId);
|
|
81
|
+
const childNodeType = editor.schema.nodes[blockId];
|
|
82
|
+
if (!childBlock || !childNodeType)
|
|
83
|
+
return false;
|
|
84
|
+
const childNode = createEditableNode(childNodeType, attrs ?? getBlockDefaultAttrs(childBlock));
|
|
85
|
+
if (!childNode)
|
|
86
|
+
return false;
|
|
87
|
+
ctx.dispatch(editor.state.tr.insert(active.pos + containerNode.nodeSize - 1, childNode));
|
|
88
|
+
ctx.sync();
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
function remove(index) {
|
|
92
|
+
const editor = ctx.getEditor();
|
|
93
|
+
const registry = ctx.getRegistry();
|
|
94
|
+
if (!editor || !registry)
|
|
95
|
+
return false;
|
|
96
|
+
const active = ctx.resolveActiveBlock();
|
|
97
|
+
if (!active)
|
|
98
|
+
return false;
|
|
99
|
+
const block = registry.get(active.id);
|
|
100
|
+
if (!block?.content)
|
|
101
|
+
return false;
|
|
102
|
+
const node = editor.state.doc.nodeAt(active.pos);
|
|
103
|
+
if (!node || node.childCount <= 1 || index < 0 || index >= node.childCount)
|
|
104
|
+
return false;
|
|
105
|
+
let childStart = active.pos + 1;
|
|
106
|
+
for (let i = 0; i < index; i += 1) {
|
|
107
|
+
childStart += node.child(i).nodeSize;
|
|
108
|
+
}
|
|
109
|
+
const child = node.child(index);
|
|
110
|
+
ctx.dispatch(editor.state.tr.delete(childStart, childStart + child.nodeSize));
|
|
111
|
+
ctx.sync();
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
return { move, insert, remove };
|
|
115
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ActionReturn } from 'svelte/action';
|
|
2
|
+
/**
|
|
3
|
+
* Makes a native `<details>` dropdown dismissable by keyboard and by
|
|
4
|
+
* interacting outside it. The browser only closes a `<details>` when its own
|
|
5
|
+
* summary is toggled; this action adds:
|
|
6
|
+
*
|
|
7
|
+
* - **Escape** closes the open menu and returns focus to the summary.
|
|
8
|
+
* - A **pointer press or focus move outside** the element closes the menu
|
|
9
|
+
* (without stealing focus), matching the expected menu-dismissal behaviour.
|
|
10
|
+
*
|
|
11
|
+
* Listeners on `document` use the capture phase so the menu closes even when
|
|
12
|
+
* the outside target stops propagation. "Inside" is decided with
|
|
13
|
+
* `composedPath()` rather than `event.target` so it stays correct when the
|
|
14
|
+
* editor is mounted in a shadow root (e.g. the `<uncial-editor>` custom
|
|
15
|
+
* element), where `event.target` retargets to the shadow host and would make
|
|
16
|
+
* every in-menu interaction look like an outside click.
|
|
17
|
+
*/
|
|
18
|
+
export declare function dropdownDismiss(node: HTMLDetailsElement): ActionReturn;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes a native `<details>` dropdown dismissable by keyboard and by
|
|
3
|
+
* interacting outside it. The browser only closes a `<details>` when its own
|
|
4
|
+
* summary is toggled; this action adds:
|
|
5
|
+
*
|
|
6
|
+
* - **Escape** closes the open menu and returns focus to the summary.
|
|
7
|
+
* - A **pointer press or focus move outside** the element closes the menu
|
|
8
|
+
* (without stealing focus), matching the expected menu-dismissal behaviour.
|
|
9
|
+
*
|
|
10
|
+
* Listeners on `document` use the capture phase so the menu closes even when
|
|
11
|
+
* the outside target stops propagation. "Inside" is decided with
|
|
12
|
+
* `composedPath()` rather than `event.target` so it stays correct when the
|
|
13
|
+
* editor is mounted in a shadow root (e.g. the `<uncial-editor>` custom
|
|
14
|
+
* element), where `event.target` retargets to the shadow host and would make
|
|
15
|
+
* every in-menu interaction look like an outside click.
|
|
16
|
+
*/
|
|
17
|
+
export function dropdownDismiss(node) {
|
|
18
|
+
function close(restoreFocus) {
|
|
19
|
+
if (!node.open)
|
|
20
|
+
return;
|
|
21
|
+
node.open = false;
|
|
22
|
+
if (restoreFocus) {
|
|
23
|
+
node.querySelector('summary')?.focus();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function onKeydown(event) {
|
|
27
|
+
if (event.key === 'Escape') {
|
|
28
|
+
close(true);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function onOutside(event) {
|
|
32
|
+
if (!event.composedPath().includes(node)) {
|
|
33
|
+
close(false);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
node.addEventListener('keydown', onKeydown);
|
|
37
|
+
document.addEventListener('pointerdown', onOutside, true);
|
|
38
|
+
document.addEventListener('focusin', onOutside, true);
|
|
39
|
+
return {
|
|
40
|
+
destroy() {
|
|
41
|
+
node.removeEventListener('keydown', onKeydown);
|
|
42
|
+
document.removeEventListener('pointerdown', onOutside, true);
|
|
43
|
+
document.removeEventListener('focusin', onOutside, true);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/** Breathing room between the menu and the viewport edge it is clamped to. */
|
|
2
|
+
const VIEWPORT_MARGIN = 8;
|
|
3
|
+
/** The vertical padding and border `max-height` does not account for. */
|
|
4
|
+
function frameHeight(element) {
|
|
5
|
+
const style = getComputedStyle(element);
|
|
6
|
+
if (style.boxSizing === 'border-box')
|
|
7
|
+
return 0;
|
|
8
|
+
return (Number.parseFloat(style.borderTopWidth) +
|
|
9
|
+
Number.parseFloat(style.borderBottomWidth) +
|
|
10
|
+
Number.parseFloat(style.paddingTop) +
|
|
11
|
+
Number.parseFloat(style.paddingBottom));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Lifts an open `<details>` dropdown's menu out of every scrolling ancestor and
|
|
15
|
+
* places it against the viewport, under (or over) its own summary.
|
|
16
|
+
*
|
|
17
|
+
* The attributes sidebar's panel is `position: sticky` with a `max-height` and
|
|
18
|
+
* `overflow-y: auto`, which clips an absolutely positioned menu at the panel's
|
|
19
|
+
* edge — so the "Add block" list was unreachable by mouse. A viewport-positioned
|
|
20
|
+
* menu is not clipped by an overflow ancestor, and the top layer (`popover`,
|
|
21
|
+
* where the browser has it) also lifts it clear of a transformed ancestor, which
|
|
22
|
+
* a `position: fixed` element alone would still be trapped inside.
|
|
23
|
+
*
|
|
24
|
+
* Positioning only. Dismissal stays with `dropdownDismiss`, which the menu's
|
|
25
|
+
* unchanged place in the DOM tree keeps working.
|
|
26
|
+
*/
|
|
27
|
+
export function dropdownFloat(node) {
|
|
28
|
+
let gap = null;
|
|
29
|
+
function parts() {
|
|
30
|
+
const summary = node.querySelector('summary');
|
|
31
|
+
const menu = node.querySelector('.uncial-dropdown__menu');
|
|
32
|
+
return summary && menu ? { summary, menu } : null;
|
|
33
|
+
}
|
|
34
|
+
function place() {
|
|
35
|
+
const found = parts();
|
|
36
|
+
if (!found)
|
|
37
|
+
return;
|
|
38
|
+
const { summary, menu } = found;
|
|
39
|
+
// The stylesheet's `margin-top` is the trigger-to-menu gap; it has to be
|
|
40
|
+
// read before the first placement zeroes it.
|
|
41
|
+
if (gap === null)
|
|
42
|
+
gap = Number.parseFloat(getComputedStyle(menu).marginTop) || 0;
|
|
43
|
+
if (menu.popover !== 'manual' && 'popover' in menu)
|
|
44
|
+
menu.popover = 'manual';
|
|
45
|
+
if (menu.popover && !menu.matches(':popover-open'))
|
|
46
|
+
menu.showPopover();
|
|
47
|
+
// Lifting the height cap to measure resets the menu's own scroll, so a
|
|
48
|
+
// re-place must not lose how far the reader had scrolled the list.
|
|
49
|
+
const scrolled = menu.scrollTop;
|
|
50
|
+
menu.style.position = 'fixed';
|
|
51
|
+
menu.style.margin = '0';
|
|
52
|
+
menu.style.right = 'auto';
|
|
53
|
+
menu.style.bottom = 'auto';
|
|
54
|
+
menu.style.maxHeight = '';
|
|
55
|
+
menu.style.overflowY = '';
|
|
56
|
+
menu.style.top = '0px';
|
|
57
|
+
menu.style.left = '0px';
|
|
58
|
+
const anchor = summary.getBoundingClientRect();
|
|
59
|
+
const natural = menu.getBoundingClientRect();
|
|
60
|
+
const spaceBelow = window.innerHeight - anchor.bottom - gap - VIEWPORT_MARGIN;
|
|
61
|
+
const spaceAbove = anchor.top - gap - VIEWPORT_MARGIN;
|
|
62
|
+
const below = natural.height <= spaceBelow || spaceBelow >= spaceAbove;
|
|
63
|
+
const available = Math.max(below ? spaceBelow : spaceAbove, 0);
|
|
64
|
+
if (natural.height > available) {
|
|
65
|
+
// Under `content-box` sizing — the default, and the package's stylesheet
|
|
66
|
+
// sets none — `max-height` bounds the content alone, so the menu's own
|
|
67
|
+
// frame has to come out of the space asked for.
|
|
68
|
+
menu.style.maxHeight = `${Math.max(available - frameHeight(menu), 0)}px`;
|
|
69
|
+
menu.style.overflowY = 'auto';
|
|
70
|
+
}
|
|
71
|
+
menu.scrollTop = scrolled;
|
|
72
|
+
const height = menu.getBoundingClientRect().height;
|
|
73
|
+
const top = below ? anchor.bottom + gap : anchor.top - gap - height;
|
|
74
|
+
// `--end` dropdowns hang their right edge off the trigger's right edge.
|
|
75
|
+
const preferredLeft = node.classList.contains('uncial-dropdown--end')
|
|
76
|
+
? anchor.right - natural.width
|
|
77
|
+
: anchor.left;
|
|
78
|
+
const left = Math.max(VIEWPORT_MARGIN, Math.min(preferredLeft, window.innerWidth - natural.width - VIEWPORT_MARGIN));
|
|
79
|
+
menu.style.top = `${Math.max(top, VIEWPORT_MARGIN)}px`;
|
|
80
|
+
menu.style.left = `${left}px`;
|
|
81
|
+
}
|
|
82
|
+
function release() {
|
|
83
|
+
const found = parts();
|
|
84
|
+
if (!found)
|
|
85
|
+
return;
|
|
86
|
+
const { menu } = found;
|
|
87
|
+
if (menu.popover && menu.matches(':popover-open'))
|
|
88
|
+
menu.hidePopover();
|
|
89
|
+
for (const property of [
|
|
90
|
+
'position',
|
|
91
|
+
'margin',
|
|
92
|
+
'top',
|
|
93
|
+
'left',
|
|
94
|
+
'right',
|
|
95
|
+
'bottom',
|
|
96
|
+
'max-height',
|
|
97
|
+
'overflow-y'
|
|
98
|
+
]) {
|
|
99
|
+
menu.style.removeProperty(property);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function onToggle() {
|
|
103
|
+
if (node.open) {
|
|
104
|
+
place();
|
|
105
|
+
// Capture, so a scroll in any ancestor — the panel or the page — moves
|
|
106
|
+
// the menu with its trigger.
|
|
107
|
+
document.addEventListener('scroll', place, true);
|
|
108
|
+
window.addEventListener('resize', place);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
release();
|
|
112
|
+
document.removeEventListener('scroll', place, true);
|
|
113
|
+
window.removeEventListener('resize', place);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
node.addEventListener('toggle', onToggle);
|
|
117
|
+
if (node.open)
|
|
118
|
+
onToggle();
|
|
119
|
+
return {
|
|
120
|
+
destroy() {
|
|
121
|
+
node.removeEventListener('toggle', onToggle);
|
|
122
|
+
document.removeEventListener('scroll', place, true);
|
|
123
|
+
window.removeEventListener('resize', place);
|
|
124
|
+
release();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
package/dist/editor/index.d.ts
CHANGED
|
@@ -6,8 +6,10 @@ export { default as Toolbar } from './Toolbar.svelte';
|
|
|
6
6
|
export { default as BlockMenu } from './BlockMenu.svelte';
|
|
7
7
|
export { bindEditor } from './bindEditor.js';
|
|
8
8
|
export { createEditorExtensions } from './createEditorExtensions.js';
|
|
9
|
-
export { createBlockAttributesController } from './attributesController.js';
|
|
9
|
+
export { createBlockAttributesController, createInitialState } from './attributesController.js';
|
|
10
10
|
export { createDocumentMetaController } from './metaController.js';
|
|
11
|
+
export { CHOOSE_ATTRIBUTE_EVENT } from './chooseAttribute.js';
|
|
12
|
+
export type { ChooseAttributeRequest } from './chooseAttribute.js';
|
|
11
13
|
export { builtinToolbarFeatures, defaultToolbarFeatures, resolveToolbarFeatures } from './toolbarFeatures.js';
|
|
12
14
|
export type { BindEditorOptions } from './bindEditor.js';
|
|
13
15
|
export type { ToolbarFeature, ToolbarFeatureContext, ToolbarFeatureId, ToolbarFeatureSelection } from './toolbarFeatures.js';
|
package/dist/editor/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export { default as Toolbar } from './Toolbar.svelte';
|
|
|
6
6
|
export { default as BlockMenu } from './BlockMenu.svelte';
|
|
7
7
|
export { bindEditor } from './bindEditor.js';
|
|
8
8
|
export { createEditorExtensions } from './createEditorExtensions.js';
|
|
9
|
-
export { createBlockAttributesController } from './attributesController.js';
|
|
9
|
+
export { createBlockAttributesController, createInitialState } from './attributesController.js';
|
|
10
10
|
export { createDocumentMetaController } from './metaController.js';
|
|
11
|
+
export { CHOOSE_ATTRIBUTE_EVENT } from './chooseAttribute.js';
|
|
11
12
|
export { builtinToolbarFeatures, defaultToolbarFeatures, resolveToolbarFeatures } from './toolbarFeatures.js';
|