@nocentraldb/tiptap 0.0.0 → 0.2.0-alpha.35
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/dist/index.d.ts +3 -0
- package/dist/index.js +5 -0
- package/dist/keepUnknown.d.ts +9 -0
- package/dist/keepUnknown.js +24 -0
- package/dist/mention.d.ts +12 -0
- package/dist/mention.js +92 -0
- package/dist/ncEditor.d.ts +16 -0
- package/dist/ncEditor.js +226 -0
- package/dist/vocabulary.d.ts +6 -0
- package/dist/vocabulary.js +34 -0
- package/dist/widen.d.ts +4 -0
- package/dist/widen.js +135 -0
- package/package.json +32 -2
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// @nocentraldb/tiptap: what a TipTap editor needs to edit a rich-text field
|
|
2
|
+
// of NoCentralDB (RFC 0013, "An editor: the binding"; NOC-461).
|
|
3
|
+
export { NcKeepUnknown } from "./keepUnknown.js";
|
|
4
|
+
export { NcMention, } from "./mention.js";
|
|
5
|
+
export { ncEditor } from "./ncEditor.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type AnyExtension, Extension } from "@tiptap/core";
|
|
2
|
+
import type * as Y from "yjs";
|
|
3
|
+
export interface NcKeepUnknownOptions {
|
|
4
|
+
/** The field's document, `body.doc`. */
|
|
5
|
+
document: Y.Doc | null;
|
|
6
|
+
/** The editor's other extensions, against whose schema the document is widened. */
|
|
7
|
+
extensions: AnyExtension[];
|
|
8
|
+
}
|
|
9
|
+
export declare const NcKeepUnknown: Extension<NcKeepUnknownOptions, any>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// NcKeepUnknown, for an app that builds its own TipTap editor (RFC 0013,
|
|
2
|
+
// "An editor that cannot show a node"): it widens the editor's schema from
|
|
3
|
+
// the field's document (measure 1) and refuses, as the editor is created,
|
|
4
|
+
// an editor without `enableContentCheck` (measure 2), with
|
|
5
|
+
// `contentCheckMissing` (Carsten, 2026-11-22, NOC-467; NOC-538, Q3).
|
|
6
|
+
// `ncEditor` sets both itself.
|
|
7
|
+
import { nc } from "@nocentraldb/sdk";
|
|
8
|
+
import { Extension } from "@tiptap/core";
|
|
9
|
+
import { widen } from "./widen.js";
|
|
10
|
+
export const NcKeepUnknown = Extension.create({
|
|
11
|
+
name: "ncKeepUnknown",
|
|
12
|
+
addOptions: () => ({ document: null, extensions: [] }),
|
|
13
|
+
addExtensions() {
|
|
14
|
+
const { document, extensions } = this.options;
|
|
15
|
+
// Without the editor's other extensions there is no schema to widen
|
|
16
|
+
// against; the content check still holds.
|
|
17
|
+
return document && extensions.length > 0 ? widen(document, extensions) : [];
|
|
18
|
+
},
|
|
19
|
+
onBeforeCreate() {
|
|
20
|
+
if (!this.editor.options.enableContentCheck) {
|
|
21
|
+
throw new nc.Error("contentCheckMissing", "the editor of a rich-text field needs enableContentCheck: true, or it deletes what its schema does not know");
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Node } from "@tiptap/core";
|
|
2
|
+
/** What a mention refers to: the target object's space id and object id. */
|
|
3
|
+
export interface NcMentionTarget {
|
|
4
|
+
readonly space: string;
|
|
5
|
+
readonly id: string;
|
|
6
|
+
}
|
|
7
|
+
export interface NcMentionOptions {
|
|
8
|
+
/** The name to show for the target; nothing shows the placeholder. */
|
|
9
|
+
label: (target: NcMentionTarget) => string | undefined | Promise<string | undefined>;
|
|
10
|
+
}
|
|
11
|
+
/** The reference node: `space` and `id`, and no name. */
|
|
12
|
+
export declare const NcMention: Node<NcMentionOptions, any>;
|
package/dist/mention.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// NcMention (RFC 0013, "Mentions"; NOC-461): an inline node named
|
|
2
|
+
// `mention` that holds a reference to an object, the target's `space` and
|
|
3
|
+
// `id` as RFC 0009 writes them, and never a name. The editor shows the
|
|
4
|
+
// name the app's `label` gives for the target when it renders; until then,
|
|
5
|
+
// and when `label` gives nothing, as after an erasure, it shows a neutral
|
|
6
|
+
// placeholder. The name never reaches the document, the HTML or the JSON.
|
|
7
|
+
//
|
|
8
|
+
// The app resolves the target in `label`; there is no `db` option (Carsten,
|
|
9
|
+
// 2026-11-27, NOC-579).
|
|
10
|
+
import { mergeAttributes, Node } from "@tiptap/core";
|
|
11
|
+
/** What the editor shows before it has a name, and when there is none. */
|
|
12
|
+
const PLACEHOLDER = "@…";
|
|
13
|
+
const shown = (name) => typeof name === "string" && name.length > 0 ? `@${name}` : PLACEHOLDER;
|
|
14
|
+
/** The reference node: `space` and `id`, and no name. */
|
|
15
|
+
export const NcMention = Node.create({
|
|
16
|
+
name: "mention",
|
|
17
|
+
group: "inline",
|
|
18
|
+
inline: true,
|
|
19
|
+
atom: true,
|
|
20
|
+
selectable: false,
|
|
21
|
+
addOptions: () => ({ label: () => undefined }),
|
|
22
|
+
addAttributes: () => ({
|
|
23
|
+
space: {
|
|
24
|
+
default: null,
|
|
25
|
+
parseHTML: (element) => element.getAttribute("data-space"),
|
|
26
|
+
renderHTML: (attributes) => ({ "data-space": attributes.space }),
|
|
27
|
+
},
|
|
28
|
+
id: {
|
|
29
|
+
default: null,
|
|
30
|
+
parseHTML: (element) => element.getAttribute("data-id"),
|
|
31
|
+
renderHTML: (attributes) => ({ "data-id": attributes.id }),
|
|
32
|
+
},
|
|
33
|
+
}),
|
|
34
|
+
parseHTML: () => [{ tag: 'span[data-type="mention"]' }],
|
|
35
|
+
// The HTML holds the reference and a bare "@": the name is only shown.
|
|
36
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
37
|
+
"span",
|
|
38
|
+
mergeAttributes({ "data-type": "mention" }, HTMLAttributes),
|
|
39
|
+
"@",
|
|
40
|
+
],
|
|
41
|
+
renderText: () => "@",
|
|
42
|
+
addNodeView() {
|
|
43
|
+
const { label } = this.options;
|
|
44
|
+
return ({ node }) => {
|
|
45
|
+
const dom = document.createElement("span");
|
|
46
|
+
dom.setAttribute("data-type", "mention");
|
|
47
|
+
dom.contentEditable = "false";
|
|
48
|
+
// A name asked for earlier attributes is not shown for later ones.
|
|
49
|
+
let asked = 0;
|
|
50
|
+
const show = (space, id) => {
|
|
51
|
+
dom.setAttribute("data-space", space);
|
|
52
|
+
dom.setAttribute("data-id", id);
|
|
53
|
+
const now = ++asked;
|
|
54
|
+
let name;
|
|
55
|
+
try {
|
|
56
|
+
name = label({ space, id });
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
name = undefined;
|
|
60
|
+
}
|
|
61
|
+
if (name instanceof Promise) {
|
|
62
|
+
dom.textContent = PLACEHOLDER;
|
|
63
|
+
name.then((resolved) => {
|
|
64
|
+
if (now === asked)
|
|
65
|
+
dom.textContent = shown(resolved);
|
|
66
|
+
}, () => {
|
|
67
|
+
if (now === asked)
|
|
68
|
+
dom.textContent = PLACEHOLDER;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
dom.textContent = shown(name);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
show(node.attrs.space, node.attrs.id);
|
|
76
|
+
return {
|
|
77
|
+
dom,
|
|
78
|
+
update: (next) => {
|
|
79
|
+
if (next.type !== node.type)
|
|
80
|
+
return false;
|
|
81
|
+
if (next.attrs.space !== node.attrs.space ||
|
|
82
|
+
next.attrs.id !== node.attrs.id) {
|
|
83
|
+
show(next.attrs.space, next.attrs.id);
|
|
84
|
+
}
|
|
85
|
+
node = next;
|
|
86
|
+
return true;
|
|
87
|
+
},
|
|
88
|
+
ignoreMutation: () => true,
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
},
|
|
92
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RichTextBinding } from "@nocentraldb/sdk";
|
|
2
|
+
import { type AnyExtension, Editor, type EditorOptions } from "@tiptap/core";
|
|
3
|
+
export interface NcEditorOptions {
|
|
4
|
+
/** Where the editor mounts. */
|
|
5
|
+
element: HTMLElement;
|
|
6
|
+
/** The editor's extensions, such as StarterKit, without Collaboration and its undo, which ncEditor adds. */
|
|
7
|
+
extensions: AnyExtension[];
|
|
8
|
+
/** Any other options of TipTap's editor; ncEditor sets its own content check, element, extensions and editable. */
|
|
9
|
+
editorOptions?: Partial<EditorOptions>;
|
|
10
|
+
/** Called with the first editor and with each one a rebuild makes; read the editor from here, never from a closure. */
|
|
11
|
+
onEditor: (editor: Editor) => void;
|
|
12
|
+
}
|
|
13
|
+
/** Builds the field's editor and keeps rebuilding it; `destroy` ends it. */
|
|
14
|
+
export declare const ncEditor: (body: RichTextBinding, options: NcEditorOptions) => {
|
|
15
|
+
destroy(): void;
|
|
16
|
+
};
|
package/dist/ncEditor.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// `ncEditor(body, options)`: the TipTap editor of a rich-text field (RFC
|
|
2
|
+
// 0013, "An editor that cannot show a node"; Carsten, 2026-11-27, NOC-538
|
|
3
|
+
// Q4, b, as the spike on NOC-555 confirmed). It builds the editor itself:
|
|
4
|
+
//
|
|
5
|
+
// - with `enableContentCheck`, the binding's Y.Doc and its one UndoManager,
|
|
6
|
+
// to which it adds y-tiptap's sync origin, so a rebuild keeps the undo
|
|
7
|
+
// history (NOC-467);
|
|
8
|
+
// - with a schema widened from the document, whose placeholders it tells
|
|
9
|
+
// the binding through `known`, so the guard lets their deliberate
|
|
10
|
+
// deletion through;
|
|
11
|
+
// - after checking the editor against the field's vocabulary, which
|
|
12
|
+
// throws `vocabularyMismatch` (NOC-553).
|
|
13
|
+
//
|
|
14
|
+
// It rebuilds the editor when a new kind of node arrives (contentError) and
|
|
15
|
+
// when the binding reloads its document, restores the selection, kept as
|
|
16
|
+
// Yjs relative positions, and the focus, noted as a remote transaction
|
|
17
|
+
// starts, since TipTap takes the focus away before onContentError (NOC-555),
|
|
18
|
+
// and hands each editor to `onEditor`. A composition in the editor reaches
|
|
19
|
+
// the binding, which holds its writes and pulls meanwhile.
|
|
20
|
+
import { Editor, Extension, getSchema, } from "@tiptap/core";
|
|
21
|
+
import Collaboration from "@tiptap/extension-collaboration";
|
|
22
|
+
import { Plugin, TextSelection } from "@tiptap/pm/state";
|
|
23
|
+
import { absolutePositionToRelativePosition, relativePositionToAbsolutePosition, ySyncPluginKey, } from "@tiptap/y-tiptap";
|
|
24
|
+
import { checkVocabulary } from "./vocabulary.js";
|
|
25
|
+
import { widen } from "./widen.js";
|
|
26
|
+
/**
|
|
27
|
+
* Rendering never writes (NOC-557): a transaction a plugin appends to y-sync's
|
|
28
|
+
* render of the document, such as Link's autolink on text that looks like a
|
|
29
|
+
* URL or the trailing paragraph, is refused, since it would reach the Y.Doc
|
|
30
|
+
* as an edit nobody made. ProseMirror filters an appended transaction
|
|
31
|
+
* before it marks it as appended, so the filter notes the render it lets
|
|
32
|
+
* through and refuses every transaction that changes the document until
|
|
33
|
+
* the view has taken the render in; an edit after that, also one an app
|
|
34
|
+
* makes in the same task, goes through. The selection a rebuild restores is
|
|
35
|
+
* treated as a render: what a plugin appends to it, such as a trailing
|
|
36
|
+
* paragraph, is not written either.
|
|
37
|
+
*/
|
|
38
|
+
/** Marks the transaction that restores the selection after a rebuild, which, like a render, nobody made. */
|
|
39
|
+
const RESTORE = "ncRestoreSelection";
|
|
40
|
+
/**
|
|
41
|
+
* A first edit that empties the document is written (NOC-580). y-tiptap
|
|
42
|
+
* 3.3.1 writes a change only once the document has differed from an empty
|
|
43
|
+
* one, and its own first render, dispatched before its view's update hook
|
|
44
|
+
* exists, does not count: an edit that leaves the document empty before any
|
|
45
|
+
* other was never written. When an edit, not a render, leaves the document
|
|
46
|
+
* empty, it is handed to y-sync's binding as y-sync's own update hook hands
|
|
47
|
+
* it: inside the binding's mutex, in a transaction under y-sync's origin,
|
|
48
|
+
* so the binding's UndoManager tracks it. If y-sync wrote it already, the
|
|
49
|
+
* Y.Doc holds it and nothing more is written. y-tiptap is pinned exactly,
|
|
50
|
+
* since this reads its binding.
|
|
51
|
+
*/
|
|
52
|
+
const writeEmptied = (state) => {
|
|
53
|
+
const empty = state.doc.type.createAndFill();
|
|
54
|
+
if (!empty || !state.doc.eq(empty))
|
|
55
|
+
return;
|
|
56
|
+
const binding = ySyncPluginKey.getState(state)?.binding;
|
|
57
|
+
if (!binding)
|
|
58
|
+
return;
|
|
59
|
+
binding.mux(() => {
|
|
60
|
+
binding.doc.transact((tr) => {
|
|
61
|
+
tr.meta.set("addToHistory", true);
|
|
62
|
+
binding._prosemirrorChanged(state.doc);
|
|
63
|
+
}, ySyncPluginKey);
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const noWriteOnRender = () => {
|
|
67
|
+
let rendering = false;
|
|
68
|
+
return Extension.create({
|
|
69
|
+
name: "ncNoWriteOnRender",
|
|
70
|
+
// Before y-sync's plugin, so its view exists when y-sync renders the
|
|
71
|
+
// document while the editor's view is built.
|
|
72
|
+
priority: 1000,
|
|
73
|
+
addProseMirrorPlugins: () => [
|
|
74
|
+
new Plugin({
|
|
75
|
+
filterTransaction: (tr) => {
|
|
76
|
+
if (tr.getMeta(ySyncPluginKey)?.isChangeOrigin ||
|
|
77
|
+
tr.getMeta(RESTORE)) {
|
|
78
|
+
rendering = true;
|
|
79
|
+
// At the latest, the task's end ends the render.
|
|
80
|
+
queueMicrotask(() => {
|
|
81
|
+
rendering = false;
|
|
82
|
+
});
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
return !(rendering && tr.docChanged);
|
|
86
|
+
},
|
|
87
|
+
view: () => ({
|
|
88
|
+
update: (view, previous) => {
|
|
89
|
+
const rendered = rendering;
|
|
90
|
+
// The view takes the render, and what was appended to it, in at
|
|
91
|
+
// once: from then on, transactions are no longer appended to it.
|
|
92
|
+
rendering = false;
|
|
93
|
+
if (!rendered && !view.state.doc.eq(previous.doc)) {
|
|
94
|
+
writeEmptied(view.state);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
}),
|
|
98
|
+
}),
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
/** The names `widened` adds to the schema `base` builds. */
|
|
103
|
+
const placeholderNames = (base, widened) => {
|
|
104
|
+
const before = getSchema(base);
|
|
105
|
+
const after = getSchema([...base, ...widened]);
|
|
106
|
+
const attributes = {};
|
|
107
|
+
for (const [name, type] of Object.entries(after.nodes)) {
|
|
108
|
+
const old = before.nodes[name]?.spec.attrs ?? {};
|
|
109
|
+
const added = Object.keys(type.spec.attrs ?? {}).filter((a) => !(a in old));
|
|
110
|
+
if (added.length > 0)
|
|
111
|
+
attributes[name] = added;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
nodes: Object.keys(after.nodes).filter((n) => !before.nodes[n]),
|
|
115
|
+
marks: Object.keys(after.marks).filter((m) => !before.marks[m]),
|
|
116
|
+
attributes,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
/** Builds the field's editor and keeps rebuilding it; `destroy` ends it. */
|
|
120
|
+
export const ncEditor = (body, options) => {
|
|
121
|
+
const { element, extensions, editorOptions, onEditor } = options;
|
|
122
|
+
checkVocabulary(extensions, body.vocabulary);
|
|
123
|
+
let editor;
|
|
124
|
+
let composing = false;
|
|
125
|
+
let pending = false;
|
|
126
|
+
let hadFocus = false;
|
|
127
|
+
let stopDoc = () => { };
|
|
128
|
+
const editable = () => body.writable && body.status.status !== "readOnly";
|
|
129
|
+
const watch = (doc) => {
|
|
130
|
+
stopDoc();
|
|
131
|
+
// TipTap takes the focus away before onContentError: it is noted as a
|
|
132
|
+
// remote update starts, before the editor sees it (NOC-555).
|
|
133
|
+
const before = (tr) => {
|
|
134
|
+
if (!tr.local && !pending) {
|
|
135
|
+
hadFocus = element.contains(document.activeElement);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
doc.on("beforeTransaction", before);
|
|
139
|
+
stopDoc = () => doc.off("beforeTransaction", before);
|
|
140
|
+
};
|
|
141
|
+
const create = (selection, focus = false) => {
|
|
142
|
+
body.undoManager.addTrackedOrigin(ySyncPluginKey);
|
|
143
|
+
const widened = widen(body.doc, extensions);
|
|
144
|
+
body.known(placeholderNames(extensions, widened));
|
|
145
|
+
watch(body.doc);
|
|
146
|
+
editor = new Editor({
|
|
147
|
+
...editorOptions,
|
|
148
|
+
element,
|
|
149
|
+
editable: editable(),
|
|
150
|
+
enableContentCheck: true,
|
|
151
|
+
onContentError: () => {
|
|
152
|
+
pending = true;
|
|
153
|
+
if (!composing)
|
|
154
|
+
setTimeout(rebuild, 0);
|
|
155
|
+
},
|
|
156
|
+
extensions: [
|
|
157
|
+
...extensions,
|
|
158
|
+
...widened,
|
|
159
|
+
Collaboration.configure({
|
|
160
|
+
document: body.doc,
|
|
161
|
+
yUndoOptions: { undoManager: body.undoManager },
|
|
162
|
+
}),
|
|
163
|
+
noWriteOnRender(),
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
const sync = ySyncPluginKey.getState(editor.state);
|
|
167
|
+
if (selection && sync) {
|
|
168
|
+
const anchor = relativePositionToAbsolutePosition(body.doc, sync.type, selection.anchor, sync.binding.mapping);
|
|
169
|
+
const head = relativePositionToAbsolutePosition(body.doc, sync.type, selection.head, sync.binding.mapping);
|
|
170
|
+
if (anchor !== null && head !== null) {
|
|
171
|
+
const { state } = editor;
|
|
172
|
+
const max = state.doc.content.size;
|
|
173
|
+
editor.view.dispatch(state.tr
|
|
174
|
+
.setSelection(TextSelection.create(state.doc, Math.min(anchor, max), Math.min(head, max)))
|
|
175
|
+
.setMeta(RESTORE, true));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (focus)
|
|
179
|
+
editor.view.focus();
|
|
180
|
+
onEditor(editor);
|
|
181
|
+
};
|
|
182
|
+
/** Builds the editor again; `keepSelection` when the document stays the same. */
|
|
183
|
+
const rebuild = (keepSelection = true) => {
|
|
184
|
+
if (!editor || (keepSelection && (!pending || composing)))
|
|
185
|
+
return;
|
|
186
|
+
pending = false;
|
|
187
|
+
let selection;
|
|
188
|
+
const sync = ySyncPluginKey.getState(editor.state);
|
|
189
|
+
if (keepSelection && sync) {
|
|
190
|
+
const { anchor, head } = editor.state.selection;
|
|
191
|
+
selection = {
|
|
192
|
+
anchor: absolutePositionToRelativePosition(anchor, sync.type, sync.binding.mapping),
|
|
193
|
+
head: absolutePositionToRelativePosition(head, sync.type, sync.binding.mapping),
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const focus = hadFocus || editor.isFocused || element.contains(document.activeElement);
|
|
197
|
+
hadFocus = false;
|
|
198
|
+
editor.destroy();
|
|
199
|
+
create(selection, focus);
|
|
200
|
+
};
|
|
201
|
+
const compositionStart = () => {
|
|
202
|
+
composing = true;
|
|
203
|
+
body.composing(true);
|
|
204
|
+
};
|
|
205
|
+
const compositionEnd = () => {
|
|
206
|
+
composing = false;
|
|
207
|
+
body.composing(false);
|
|
208
|
+
if (pending)
|
|
209
|
+
setTimeout(rebuild, 0);
|
|
210
|
+
};
|
|
211
|
+
element.addEventListener("compositionstart", compositionStart, true);
|
|
212
|
+
element.addEventListener("compositionend", compositionEnd, true);
|
|
213
|
+
const stopReload = body.onReload(() => rebuild(false));
|
|
214
|
+
const stopStatus = body.onStatus(() => editor?.setEditable(editable()));
|
|
215
|
+
create();
|
|
216
|
+
return {
|
|
217
|
+
destroy: () => {
|
|
218
|
+
element.removeEventListener("compositionstart", compositionStart, true);
|
|
219
|
+
element.removeEventListener("compositionend", compositionEnd, true);
|
|
220
|
+
stopReload();
|
|
221
|
+
stopStatus();
|
|
222
|
+
stopDoc();
|
|
223
|
+
editor?.destroy();
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type Vocabulary } from "@nocentraldb/sdk";
|
|
2
|
+
import { type AnyExtension } from "@tiptap/core";
|
|
3
|
+
/** The nodes and marks of the editor `extensions` build, without `doc` and `text`, sorted. */
|
|
4
|
+
export declare const editorVocabulary: (extensions: AnyExtension[]) => Vocabulary;
|
|
5
|
+
/** Throws `vocabularyMismatch` when the editor `extensions` build differs from `vocabulary`, naming what each side lacks. */
|
|
6
|
+
export declare const checkVocabulary: (extensions: AnyExtension[], vocabulary: Vocabulary) => void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The editor against the field's vocabulary (RFC 0013, "The vocabulary, at
|
|
2
|
+
// start"; Carsten, 2026-11-27, NOC-553): the editor's nodes and marks,
|
|
3
|
+
// without ProseMirror's own `doc` and `text`, must be the vocabulary's,
|
|
4
|
+
// or the editor is refused with `vocabularyMismatch` (NOC-538, Q3).
|
|
5
|
+
import { nc } from "@nocentraldb/sdk";
|
|
6
|
+
import { getSchema } from "@tiptap/core";
|
|
7
|
+
/** ProseMirror's own nodes, which a vocabulary never lists (NOC-558). */
|
|
8
|
+
const OWN = new Set(["doc", "text"]);
|
|
9
|
+
/** The nodes and marks of the editor `extensions` build, without `doc` and `text`, sorted. */
|
|
10
|
+
export const editorVocabulary = (extensions) => {
|
|
11
|
+
const schema = getSchema(extensions);
|
|
12
|
+
return {
|
|
13
|
+
nodes: Object.keys(schema.nodes)
|
|
14
|
+
.filter((name) => !OWN.has(name))
|
|
15
|
+
.sort(),
|
|
16
|
+
marks: Object.keys(schema.marks).sort(),
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
/** Throws `vocabularyMismatch` when the editor `extensions` build differs from `vocabulary`, naming what each side lacks. */
|
|
20
|
+
export const checkVocabulary = (extensions, vocabulary) => {
|
|
21
|
+
const editor = editorVocabulary(extensions);
|
|
22
|
+
const missing = (from, to) => from.filter((name) => !to.includes(name));
|
|
23
|
+
const editorOnly = [
|
|
24
|
+
...missing(editor.nodes, vocabulary.nodes),
|
|
25
|
+
...missing(editor.marks, vocabulary.marks),
|
|
26
|
+
];
|
|
27
|
+
const vocabularyOnly = [
|
|
28
|
+
...missing(vocabulary.nodes, editor.nodes),
|
|
29
|
+
...missing(vocabulary.marks, editor.marks),
|
|
30
|
+
];
|
|
31
|
+
if (editorOnly.length === 0 && vocabularyOnly.length === 0)
|
|
32
|
+
return;
|
|
33
|
+
throw new nc.Error("vocabularyMismatch", `the editor differs from the field's vocabulary: the editor has ${editorOnly.join(", ") || "nothing more"}, the vocabulary has ${vocabularyOnly.join(", ") || "nothing more"}; add the names to the schema or to the editor`);
|
|
34
|
+
};
|
package/dist/widen.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type AnyExtension } from "@tiptap/core";
|
|
2
|
+
import * as Y from "yjs";
|
|
3
|
+
/** The extensions that widen the schema `base` builds for `doc`: none when the editor knows every part of it. */
|
|
4
|
+
export declare const widen: (doc: Y.Doc, base: AnyExtension[]) => AnyExtension[];
|
package/dist/widen.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Measure 1 of RFC 0013, "An editor that cannot show a node": a schema
|
|
2
|
+
// widened from the document. Before the editor opens, the field's document
|
|
3
|
+
// is scanned, and each node, mark and attribute the editor does not know
|
|
4
|
+
// gets a placeholder type, so the editor shows it, keeps it and keeps the
|
|
5
|
+
// rest editable: a node of that name, inline or block as it sits, with its
|
|
6
|
+
// attributes declared without a default of their own; a mark with its
|
|
7
|
+
// attributes; an extra attribute on a known node. Each placeholder has
|
|
8
|
+
// parse rules, so copying and pasting it keeps it (NOC-455). The scan
|
|
9
|
+
// reads the Yjs tree and never converts it through a ProseMirror schema,
|
|
10
|
+
// which would delete what that schema lacks (RFC 0013, F16).
|
|
11
|
+
import { Extension, getSchema, Mark, Node, } from "@tiptap/core";
|
|
12
|
+
import * as Y from "yjs";
|
|
13
|
+
/** An attribute kept as it is: a data attribute in HTML, its value as JSON. */
|
|
14
|
+
const attribute = (key) => ({
|
|
15
|
+
default: null,
|
|
16
|
+
parseHTML: (element) => {
|
|
17
|
+
const raw = element.getAttribute(`data-nc-${key}`);
|
|
18
|
+
if (raw === null)
|
|
19
|
+
return null;
|
|
20
|
+
try {
|
|
21
|
+
return JSON.parse(raw);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return raw;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
renderHTML: (attributes) => attributes[key] === null || attributes[key] === undefined
|
|
28
|
+
? {}
|
|
29
|
+
: { [`data-nc-${key}`]: JSON.stringify(attributes[key]) },
|
|
30
|
+
});
|
|
31
|
+
const attributes = (keys) => Object.fromEntries([...keys].map((key) => [key, attribute(key)]));
|
|
32
|
+
/** The extensions that widen the schema `base` builds for `doc`: none when the editor knows every part of it. */
|
|
33
|
+
export const widen = (doc, base) => {
|
|
34
|
+
const schema = getSchema(base);
|
|
35
|
+
const nodes = new Map();
|
|
36
|
+
const marks = new Map();
|
|
37
|
+
const extra = new Map();
|
|
38
|
+
const walk = (parent, inText) => {
|
|
39
|
+
const children = parent.toArray();
|
|
40
|
+
const mixed = children.some((child) => child instanceof Y.XmlText);
|
|
41
|
+
for (const child of children) {
|
|
42
|
+
if (child instanceof Y.XmlText) {
|
|
43
|
+
for (const op of child.toDelta()) {
|
|
44
|
+
for (const [mark, value] of Object.entries(op.attributes ?? {})) {
|
|
45
|
+
if (schema.marks[mark])
|
|
46
|
+
continue;
|
|
47
|
+
const keys = marks.get(mark) ?? new Set();
|
|
48
|
+
if (value && typeof value === "object") {
|
|
49
|
+
for (const key of Object.keys(value))
|
|
50
|
+
keys.add(key);
|
|
51
|
+
}
|
|
52
|
+
marks.set(mark, keys);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (!(child instanceof Y.XmlElement))
|
|
58
|
+
continue;
|
|
59
|
+
const name = child.nodeName;
|
|
60
|
+
const known = schema.nodes[name];
|
|
61
|
+
const keys = Object.keys(child.getAttributes());
|
|
62
|
+
if (known) {
|
|
63
|
+
const declared = known.spec.attrs ?? {};
|
|
64
|
+
const undeclared = keys.filter((key) => !(key in declared));
|
|
65
|
+
if (undeclared.length > 0) {
|
|
66
|
+
const set = extra.get(name) ?? new Set();
|
|
67
|
+
for (const key of undeclared)
|
|
68
|
+
set.add(key);
|
|
69
|
+
extra.set(name, set);
|
|
70
|
+
}
|
|
71
|
+
walk(child, Boolean(known.isTextblock));
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const grandchildren = child.toArray();
|
|
75
|
+
const found = nodes.get(name) ?? {
|
|
76
|
+
inline: inText || mixed,
|
|
77
|
+
text: false,
|
|
78
|
+
children: false,
|
|
79
|
+
attributes: new Set(),
|
|
80
|
+
};
|
|
81
|
+
for (const key of keys)
|
|
82
|
+
found.attributes.add(key);
|
|
83
|
+
found.text ||= grandchildren.some((c) => c instanceof Y.XmlText);
|
|
84
|
+
found.children ||= grandchildren.length > 0;
|
|
85
|
+
nodes.set(name, found);
|
|
86
|
+
walk(child, found.text);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
walk(doc.getXmlFragment("default"), false);
|
|
90
|
+
const widened = [];
|
|
91
|
+
for (const [name, found] of nodes) {
|
|
92
|
+
const tag = found.inline ? "span" : "div";
|
|
93
|
+
const content = found.text
|
|
94
|
+
? "inline*"
|
|
95
|
+
: found.children
|
|
96
|
+
? found.inline
|
|
97
|
+
? "inline*"
|
|
98
|
+
: "block*"
|
|
99
|
+
: "";
|
|
100
|
+
widened.push(Node.create({
|
|
101
|
+
name,
|
|
102
|
+
group: found.inline ? "inline" : "block",
|
|
103
|
+
inline: found.inline,
|
|
104
|
+
atom: content === "",
|
|
105
|
+
content,
|
|
106
|
+
addAttributes: () => attributes(found.attributes),
|
|
107
|
+
parseHTML: () => [{ tag: `${tag}[data-nc-unknown="${name}"]` }],
|
|
108
|
+
renderHTML: ({ HTMLAttributes }) => content === ""
|
|
109
|
+
? [tag, { ...HTMLAttributes, "data-nc-unknown": name }]
|
|
110
|
+
: [tag, { ...HTMLAttributes, "data-nc-unknown": name }, 0],
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
for (const [name, keys] of marks) {
|
|
114
|
+
widened.push(Mark.create({
|
|
115
|
+
name,
|
|
116
|
+
addAttributes: () => attributes(keys),
|
|
117
|
+
parseHTML: () => [{ tag: `span[data-nc-mark="${name}"]` }],
|
|
118
|
+
renderHTML: ({ HTMLAttributes }) => [
|
|
119
|
+
"span",
|
|
120
|
+
{ ...HTMLAttributes, "data-nc-mark": name },
|
|
121
|
+
0,
|
|
122
|
+
],
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
if (extra.size > 0) {
|
|
126
|
+
widened.push(Extension.create({
|
|
127
|
+
name: "ncKeepUnknownAttributes",
|
|
128
|
+
addGlobalAttributes: () => [...extra].map(([type, keys]) => ({
|
|
129
|
+
types: [type],
|
|
130
|
+
attributes: attributes(keys),
|
|
131
|
+
})),
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
return widened;
|
|
135
|
+
};
|
package/package.json
CHANGED
|
@@ -1,11 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocentraldb/tiptap",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Holds the name. Alphas: npm i @nocentraldb/tiptap@alpha",
|
|
3
|
+
"version": "0.2.0-alpha.35",
|
|
5
4
|
"license": "MIT OR Apache-2.0",
|
|
6
5
|
"repository": {
|
|
7
6
|
"type": "git",
|
|
8
7
|
"url": "git+https://github.com/nocentraldb/nocentraldb.git",
|
|
9
8
|
"directory": "packages/tiptap"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -p tsconfig.build.json"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@nocentraldb/sdk": "workspace:*",
|
|
25
|
+
"@tiptap/core": "3.35.0",
|
|
26
|
+
"@tiptap/extension-collaboration": "3.35.0",
|
|
27
|
+
"@tiptap/pm": "3.35.0",
|
|
28
|
+
"@tiptap/starter-kit": "3.35.0",
|
|
29
|
+
"@tiptap/y-tiptap": "3.3.1",
|
|
30
|
+
"happy-dom": "21.0.4",
|
|
31
|
+
"yjs": "13.6.33"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@nocentraldb/sdk": "0.2.0-alpha.35",
|
|
35
|
+
"@tiptap/core": "3.35.0",
|
|
36
|
+
"@tiptap/extension-collaboration": "3.35.0",
|
|
37
|
+
"@tiptap/pm": "3.35.0",
|
|
38
|
+
"@tiptap/y-tiptap": "3.3.1",
|
|
39
|
+
"yjs": "13.6.33"
|
|
10
40
|
}
|
|
11
41
|
}
|