@barocss/doc-sync-tiptap 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +86 -2
- package/dist/index.d.ts +32 -0
- package/dist/index.js +238 -0
- package/dist/sync-client-compat.d.ts +11 -0
- package/dist/sync-client-compat.js +173 -0
- package/package.json +43 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 jinho park(cyberuls@gmail.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,87 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @barocss/doc-sync-tiptap
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A Tiptap extension that preserves Tiptap's transaction/plugin ownership while Doc Sync owns the shared model and history. It reuses the public ProseMirror binding and upstream ProseMirror classes.
|
|
4
|
+
|
|
5
|
+
## Availability and setup
|
|
6
|
+
|
|
7
|
+
This is a **private, unpublished `0.0.0` workspace**, not an npm installation promise. Use the repository workspace or locally packed artifacts. `publishConfig.access: public` describes a future release only.
|
|
8
|
+
|
|
9
|
+
From the repository root, with Node.js **22.18 or newer**:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm ci
|
|
13
|
+
npm run build
|
|
14
|
+
npm run check:packages
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The last command packs the library packages and checks isolated local consumers. For a separate application, pack the required Doc Sync packages with `npm pack --workspace <package-name>` from the repository root and install those local `.tgz` files together with the editor peers below. The packages export ESM JavaScript and TypeScript declarations from `dist`; they do not bundle the core or editor.
|
|
18
|
+
|
|
19
|
+
The internal Doc Sync peer range is `>=0.0.0 <0.1.0`. This is a draft local-artifact compatibility policy, not a stable registry release guarantee. See [package and release guidance](https://barocss.github.io/doc-sync/guide/packages.html).
|
|
20
|
+
|
|
21
|
+
### Required peers
|
|
22
|
+
|
|
23
|
+
- `@barocss/doc-sync` and `@barocss/doc-sync-prosemirror` (`>=0.0.0 <0.1.0`)
|
|
24
|
+
- `@tiptap/core` and `@tiptap/pm`, both **`3.32.2`**
|
|
25
|
+
|
|
26
|
+
Install the ProseMirror binding's editor peers as well. Keep the Tiptap pair at the same supported version.
|
|
27
|
+
|
|
28
|
+
## Minimal browser editor
|
|
29
|
+
|
|
30
|
+
Provide an element with `id="editor"` and bundle this TypeScript for the browser:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import {Editor} from "@tiptap/core";
|
|
34
|
+
import type {Schema} from "@barocss/doc-sync";
|
|
35
|
+
import {Client} from "@barocss/doc-sync/client";
|
|
36
|
+
import {DocSync, createTiptapExtensions} from "@barocss/doc-sync-tiptap";
|
|
37
|
+
|
|
38
|
+
const schema: Schema = {
|
|
39
|
+
root: "content",
|
|
40
|
+
marks: ["bold", "italic"],
|
|
41
|
+
nodes: {
|
|
42
|
+
content: {shape: "flow", children: ["paragraph"], default: "paragraph"},
|
|
43
|
+
paragraph: {shape: "text"},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
const client = new Client({schema});
|
|
47
|
+
const editor = new Editor({
|
|
48
|
+
element: document.querySelector("#editor")!,
|
|
49
|
+
extensions: [
|
|
50
|
+
...createTiptapExtensions(schema),
|
|
51
|
+
DocSync.configure({client}),
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
editor.commands.insertContent("Hello");
|
|
55
|
+
editor.commands.docSyncUndo();
|
|
56
|
+
editor.commands.docSyncRedo();
|
|
57
|
+
|
|
58
|
+
// On unmount:
|
|
59
|
+
function dispose() {
|
|
60
|
+
editor.destroy();
|
|
61
|
+
client.dispose(); // Only when the caller is finished with this client.
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For synchronization, connect and receive the initial snapshot before mounting. The shared client supplies initial content; the Editor `content` option does not replace it. `DocSync` also accepts `LocalClient` and legacy `SyncClient`. Multiple local views may share one client and history with separate selections. A sync backend supports **one mounted Tiptap editor**; wrapping the same backend in another client API does not bypass that claim. Destroying the editor releases it and removes callbacks. The caller retains ownership of the client.
|
|
66
|
+
|
|
67
|
+
## Schema, history and lifecycle
|
|
68
|
+
|
|
69
|
+
- `createTiptapExtensions(schema, domHints?)` generates the exact supported schema. Do not combine it with StarterKit's document, paragraph, text, list, mark or history extensions. Compatible UI/command extensions can be added; arbitrary additional node/mark schemas cannot.
|
|
70
|
+
- `DocSync.configure({client, presence?, onSelection?, onError?})` installs synchronization. Callback errors are reported through `onError(error, callback)`.
|
|
71
|
+
- `docSyncUndo` / `docSyncRedo` and Mod-Z / Mod-Shift-Z / Mod-Y use actor-owned CRDT history. Do not install Tiptap UndoRedo, ProseMirror history or another collaboration extension.
|
|
72
|
+
- Run undo/redo in a separate chain from document edits. A mixed edit/history chain is rejected before either document changes.
|
|
73
|
+
- Local mode does not publish presence. Synchronized presence requires the transport's send/receive paths; names and colors are display metadata, not authority.
|
|
74
|
+
- After an accepted schema change, rebuild generated extensions and the editor. A dedicated Tiptap open-editor transition host is not yet qualified.
|
|
75
|
+
|
|
76
|
+
## Collaboration and safety boundaries
|
|
77
|
+
|
|
78
|
+
The examples use an in-memory TypeScript client. For independent synchronized actors, use separate clients configured with unique `actor` IDs, the same compatible schema and a transport. A binding does not provide persistence, authentication, authorization or a production document server. See the [Client guide](https://barocss.github.io/doc-sync/guide/client.html) for connection and recovery contracts.
|
|
79
|
+
|
|
80
|
+
Text-editor bindings reject graphic schemas. Schema data retention does not automatically supply native commands or rendering for every node type. Applications own rendering, URL sanitization, toolbar/plugins and their disposal. Headless and simulated browser input tests do not establish actual OS IME acceptance, production readiness or large-document latency guarantees.
|
|
81
|
+
|
|
82
|
+
## Further reading and verification
|
|
83
|
+
|
|
84
|
+
- [Tiptap guide](https://barocss.github.io/doc-sync/guide/tiptap.html)
|
|
85
|
+
- [ProseMirror conversion rules](https://barocss.github.io/doc-sync/guide/prosemirror.html)
|
|
86
|
+
- Two-editor demo, from repository root: `node apps/demo/tiptap/start.mjs`
|
|
87
|
+
- Repository checks: `npm run typecheck`, `npm run check:packages`; core tests include Tiptap integration scenarios.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Extension } from "@tiptap/core";
|
|
2
|
+
import { PmBinding } from "@barocss/doc-sync-prosemirror";
|
|
3
|
+
import type { Extensions } from "@tiptap/core";
|
|
4
|
+
import type { Schema } from "@barocss/doc-sync";
|
|
5
|
+
import type { BindingClient } from "@barocss/doc-sync/client";
|
|
6
|
+
import type { DomHints } from "@barocss/doc-sync-prosemirror";
|
|
7
|
+
import type { Presence } from "@barocss/doc-sync/sync";
|
|
8
|
+
/** Generate the exact flattened schema supported by the shared ProseMirror converter.
|
|
9
|
+
* Do not combine these nodes/marks with StarterKit or another document schema.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createTiptapExtensions(schema: Schema, dom?: DomHints): Extensions;
|
|
12
|
+
export interface DocSyncOptions {
|
|
13
|
+
client: BindingClient | null;
|
|
14
|
+
/** Name, color, and optional actor appearance. Selection endpoints come from the editor. */
|
|
15
|
+
presence: Partial<Omit<Presence, "actor" | "anchor" | "focus">> | null;
|
|
16
|
+
onSelection: ((anchor: string | null, focus: string | null) => void) | null;
|
|
17
|
+
/** Host callbacks run after the native state is installed. Failures are reported here. */
|
|
18
|
+
onError: ((error: unknown, callback: string) => void) | null;
|
|
19
|
+
}
|
|
20
|
+
export interface DocSyncStorage {
|
|
21
|
+
binding: PmBinding<BindingClient> | null;
|
|
22
|
+
}
|
|
23
|
+
declare module "@tiptap/core" {
|
|
24
|
+
interface Commands<ReturnType> {
|
|
25
|
+
docSync: {
|
|
26
|
+
docSyncUndo: () => ReturnType;
|
|
27
|
+
docSyncRedo: () => ReturnType;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/** Tiptap owns EditorState and applies all transactions. DocSync owns CRDT history. */
|
|
32
|
+
export declare const DocSync: Extension<DocSyncOptions, DocSyncStorage>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Extension, Mark, Node } from "@tiptap/core";
|
|
2
|
+
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
3
|
+
import { buildPmSchema } from "@barocss/doc-sync-prosemirror";
|
|
4
|
+
import { toPmJSON } from "@barocss/doc-sync-prosemirror";
|
|
5
|
+
import { createTiptapClientCompat, initialClientModel } from "./sync-client-compat.js";
|
|
6
|
+
import { PmBinding, REMOTE_META } from "@barocss/doc-sync-prosemirror";
|
|
7
|
+
function defaults(attrs) { return JSON.stringify(Object.entries(attrs ?? {}).map(([key, value]) => [key, value.default])); }
|
|
8
|
+
function names(value) { return Object.keys(value).sort().join(","); }
|
|
9
|
+
const TIPTAP_NODE_SPEC_FIELDS = Object.freeze(["content", "group", "marks", "tableRole", "whitespace"]);
|
|
10
|
+
const EXCLUDED_TIPTAP_EXTENSIONS = Object.freeze(["undoRedo", "history", "collaboration", "collaborationCaret", "collaborationCursor"]);
|
|
11
|
+
/** Generate the exact flattened schema supported by the shared ProseMirror converter.
|
|
12
|
+
* Do not combine these nodes/marks with StarterKit or another document schema.
|
|
13
|
+
*/
|
|
14
|
+
export function createTiptapExtensions(schema, dom = {}) {
|
|
15
|
+
const { pm } = buildPmSchema(schema, dom);
|
|
16
|
+
const extensions = [];
|
|
17
|
+
for (const [name, type] of Object.entries(pm.nodes)) {
|
|
18
|
+
const spec = type.spec;
|
|
19
|
+
extensions.push(Node.create({
|
|
20
|
+
name,
|
|
21
|
+
topNode: type === pm.topNodeType,
|
|
22
|
+
content: spec.content,
|
|
23
|
+
group: spec.group,
|
|
24
|
+
inline: spec.inline,
|
|
25
|
+
atom: spec.atom,
|
|
26
|
+
marks: spec.marks,
|
|
27
|
+
defining: spec.defining,
|
|
28
|
+
isolating: spec.isolating,
|
|
29
|
+
addAttributes: () => Object.fromEntries(Object.entries(spec.attrs ?? {}).map(([key, attr]) => [key, {
|
|
30
|
+
...attr,
|
|
31
|
+
parseHTML: element => {
|
|
32
|
+
const value = element.getAttribute(`data-doc-sync-${key.toLowerCase()}`);
|
|
33
|
+
return value === null ? attr.default : JSON.parse(value);
|
|
34
|
+
},
|
|
35
|
+
renderHTML: attrs => attrs[key] == null ? {} : { [`data-doc-sync-${key.toLowerCase()}`]: JSON.stringify(attrs[key]) },
|
|
36
|
+
}])),
|
|
37
|
+
parseHTML: () => [{ tag: `[data-doc-sync-node="${name}"]` }],
|
|
38
|
+
...(spec.toDOM ? { renderHTML: ({ node, HTMLAttributes }) => {
|
|
39
|
+
const output = spec.toDOM(node);
|
|
40
|
+
if (!Array.isArray(output))
|
|
41
|
+
return output;
|
|
42
|
+
const attrs = typeof output[1] === "object" && !Array.isArray(output[1]) ? output[1] : {};
|
|
43
|
+
const tail = attrs === output[1] ? output.slice(2) : output.slice(1);
|
|
44
|
+
return [output[0], { ...attrs, ...HTMLAttributes, "data-doc-sync-node": name }, ...tail];
|
|
45
|
+
} } : {}),
|
|
46
|
+
extendNodeSchema: extension => extension.name === name && spec.tableRole ? { tableRole: spec.tableRole } : {},
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
for (const [name, type] of Object.entries(pm.marks)) {
|
|
50
|
+
const spec = type.spec;
|
|
51
|
+
extensions.push(Mark.create({
|
|
52
|
+
name,
|
|
53
|
+
inclusive: spec.inclusive,
|
|
54
|
+
addAttributes: () => Object.fromEntries(Object.entries(spec.attrs ?? {}).map(([key, attr]) => [key, {
|
|
55
|
+
...attr,
|
|
56
|
+
parseHTML: element => {
|
|
57
|
+
const value = element.getAttribute(`data-doc-sync-${key.toLowerCase()}`);
|
|
58
|
+
return value === null ? attr.default : JSON.parse(value);
|
|
59
|
+
},
|
|
60
|
+
renderHTML: attrs => attrs[key] == null ? {} : { [`data-doc-sync-${key.toLowerCase()}`]: JSON.stringify(attrs[key]) },
|
|
61
|
+
}])),
|
|
62
|
+
parseHTML: () => [{ tag: `[data-doc-sync-mark="${name}"]` }],
|
|
63
|
+
renderHTML: ({ mark, HTMLAttributes }) => {
|
|
64
|
+
const output = spec.toDOM(mark, true);
|
|
65
|
+
if (!Array.isArray(output))
|
|
66
|
+
return output;
|
|
67
|
+
return [output[0], { ...output[1], ...HTMLAttributes, "data-doc-sync-mark": name }, 0];
|
|
68
|
+
},
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
return extensions;
|
|
72
|
+
}
|
|
73
|
+
function validateSchema(actual, expected) {
|
|
74
|
+
if (names(actual.nodes) !== names(expected.nodes) || names(actual.marks) !== names(expected.marks) || actual.topNodeType.name !== expected.topNodeType.name) {
|
|
75
|
+
throw new Error("DocSync: incompatible Tiptap schema. Use createTiptapExtensions(client.schema).");
|
|
76
|
+
}
|
|
77
|
+
for (const [name, type] of Object.entries(expected.nodes)) {
|
|
78
|
+
const node = actual.nodes[name];
|
|
79
|
+
// Tiptap adds HTML attribute metadata. Compare document semantics and effective defaults.
|
|
80
|
+
const fields = TIPTAP_NODE_SPEC_FIELDS;
|
|
81
|
+
if (fields.some(field => node.spec[field] !== type.spec[field]) || node.isInline !== type.isInline || node.isAtom !== type.isAtom || !!node.spec.defining !== !!type.spec.defining || !!node.spec.isolating !== !!type.spec.isolating || defaults(node.spec.attrs) !== defaults(type.spec.attrs))
|
|
82
|
+
throw new Error(`DocSync: incompatible node ${name}.`);
|
|
83
|
+
}
|
|
84
|
+
for (const [name, type] of Object.entries(expected.marks)) {
|
|
85
|
+
const mark = actual.marks[name];
|
|
86
|
+
if (defaults(mark.spec.attrs) !== defaults(type.spec.attrs) || mark.spec.inclusive !== type.spec.inclusive || mark.spec.excludes !== type.spec.excludes || mark.spec.group !== type.spec.group || mark.spec.spanning !== type.spec.spanning)
|
|
87
|
+
throw new Error(`DocSync: incompatible mark ${name}.`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Tiptap owns EditorState and applies all transactions. DocSync owns CRDT history. */
|
|
91
|
+
export const DocSync = Extension.create({
|
|
92
|
+
name: "docSync",
|
|
93
|
+
priority: 1000,
|
|
94
|
+
addOptions: () => ({ client: null, presence: null, onSelection: null, onError: null }),
|
|
95
|
+
addStorage: () => ({ binding: null }),
|
|
96
|
+
onBeforeCreate() {
|
|
97
|
+
if (!this.options.client)
|
|
98
|
+
throw new Error("DocSync: client is required.");
|
|
99
|
+
this.editor.options.content = toPmJSON(this.options.client.schema, initialClientModel(this.options.client));
|
|
100
|
+
const incompatible = this.editor.extensionManager.extensions.find(extension => EXCLUDED_TIPTAP_EXTENSIONS.includes(extension.name));
|
|
101
|
+
if (incompatible)
|
|
102
|
+
throw new Error(`DocSync: disable ${incompatible.name}; DocSync provides actor-owned history and presence.`);
|
|
103
|
+
},
|
|
104
|
+
addCommands() {
|
|
105
|
+
return {
|
|
106
|
+
docSyncUndo: () => ({ dispatch, tr }) => {
|
|
107
|
+
const binding = this.storage.binding;
|
|
108
|
+
if (dispatch)
|
|
109
|
+
tr.setMeta("doc-sync-history-request", true);
|
|
110
|
+
if (!binding || !binding.host.historyStatus().canUndo)
|
|
111
|
+
return false;
|
|
112
|
+
if (dispatch) {
|
|
113
|
+
// CommandManager dispatches its own transaction after this command.
|
|
114
|
+
// Execute after it so the transaction is never stale.
|
|
115
|
+
tr.setMeta("doc-sync-history", "undo");
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
docSyncRedo: () => ({ dispatch, tr }) => {
|
|
120
|
+
const binding = this.storage.binding;
|
|
121
|
+
if (dispatch)
|
|
122
|
+
tr.setMeta("doc-sync-history-request", true);
|
|
123
|
+
if (!binding || !binding.host.historyStatus().canRedo)
|
|
124
|
+
return false;
|
|
125
|
+
if (dispatch)
|
|
126
|
+
tr.setMeta("doc-sync-history", "redo");
|
|
127
|
+
return true;
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
addKeyboardShortcuts() {
|
|
132
|
+
return { "Mod-z": () => this.editor.commands.docSyncUndo(), "Mod-Shift-z": () => this.editor.commands.docSyncRedo(), "Mod-y": () => this.editor.commands.docSyncRedo() };
|
|
133
|
+
},
|
|
134
|
+
addProseMirrorPlugins() {
|
|
135
|
+
const client = this.options.client;
|
|
136
|
+
if (!client)
|
|
137
|
+
throw new Error("DocSync: client is required.");
|
|
138
|
+
const info = buildPmSchema(client.schema);
|
|
139
|
+
validateSchema(this.editor.schema, info.pm);
|
|
140
|
+
info.pm = this.editor.schema;
|
|
141
|
+
let binding = null;
|
|
142
|
+
let appending = false;
|
|
143
|
+
let correction = null;
|
|
144
|
+
let active = true;
|
|
145
|
+
let publishSelection = true;
|
|
146
|
+
const storage = this.storage;
|
|
147
|
+
const options = this.options;
|
|
148
|
+
let cursor = null;
|
|
149
|
+
return [new Plugin({
|
|
150
|
+
key: new PluginKey("docSync"),
|
|
151
|
+
filterTransaction(transaction) {
|
|
152
|
+
if (transaction.getMeta("doc-sync-history-request") && transaction.docChanged)
|
|
153
|
+
throw new Error("DocSync: run undo/redo in a separate chain from document edits.");
|
|
154
|
+
return true;
|
|
155
|
+
},
|
|
156
|
+
appendTransaction(transactions, _oldState, newState) {
|
|
157
|
+
if (!binding)
|
|
158
|
+
return null;
|
|
159
|
+
appending = true;
|
|
160
|
+
correction = null;
|
|
161
|
+
const priorPublish = publishSelection;
|
|
162
|
+
publishSelection = priorPublish && transactions.some(tr => tr.getMeta(REMOTE_META) !== "remote" && !tr.getMeta("doc-sync-presence"));
|
|
163
|
+
try {
|
|
164
|
+
binding.accept(transactions, newState);
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
appending = false;
|
|
168
|
+
publishSelection = priorPublish;
|
|
169
|
+
}
|
|
170
|
+
return correction;
|
|
171
|
+
},
|
|
172
|
+
props: { decorations(state) { return cursor?.props.decorations?.call(cursor, state) ?? null; } },
|
|
173
|
+
view(view) {
|
|
174
|
+
if (view.state.plugins.some(plugin => /^(history|y-sync|y-undo)/.test(plugin.key)))
|
|
175
|
+
throw new Error("DocSync: remove local history and other collaboration plugins.");
|
|
176
|
+
const send = (tr) => {
|
|
177
|
+
if (!active)
|
|
178
|
+
return;
|
|
179
|
+
if (appending)
|
|
180
|
+
correction = tr;
|
|
181
|
+
else
|
|
182
|
+
view.dispatch(tr);
|
|
183
|
+
};
|
|
184
|
+
binding = new PmBinding(client, info, [], { state: view.state, dispatch: send });
|
|
185
|
+
const compat = createTiptapClientCompat(client, options, binding.host);
|
|
186
|
+
storage.binding = binding;
|
|
187
|
+
cursor = binding.cursorPlugin();
|
|
188
|
+
binding.onSelection = (anchor, focus) => compat.selection(anchor, focus, publishSelection);
|
|
189
|
+
compat.connect(() => {
|
|
190
|
+
if (active && binding) {
|
|
191
|
+
const priorPublish = publishSelection;
|
|
192
|
+
publishSelection = false;
|
|
193
|
+
try {
|
|
194
|
+
binding.state = view.state;
|
|
195
|
+
binding.pull();
|
|
196
|
+
}
|
|
197
|
+
finally {
|
|
198
|
+
publishSelection = priorPublish;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}, () => {
|
|
202
|
+
if (active && !appending && binding) {
|
|
203
|
+
const priorPublish = publishSelection;
|
|
204
|
+
publishSelection = false;
|
|
205
|
+
try {
|
|
206
|
+
binding.state = view.state;
|
|
207
|
+
binding.pull();
|
|
208
|
+
view.dispatch(view.state.tr.setMeta("doc-sync-presence", true));
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
publishSelection = priorPublish;
|
|
212
|
+
}
|
|
213
|
+
compat.publishIfNeeded();
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
return {
|
|
217
|
+
update(view) { if (binding)
|
|
218
|
+
binding.state = view.state; },
|
|
219
|
+
destroy() {
|
|
220
|
+
active = false;
|
|
221
|
+
compat.dispose();
|
|
222
|
+
binding?.dispose();
|
|
223
|
+
storage.binding = null;
|
|
224
|
+
binding = null;
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
},
|
|
228
|
+
})];
|
|
229
|
+
},
|
|
230
|
+
onCreate() { this.storage.binding?.pull(); },
|
|
231
|
+
onTransaction({ transaction }) {
|
|
232
|
+
const action = transaction.getMeta("doc-sync-history");
|
|
233
|
+
if (action === "undo")
|
|
234
|
+
this.storage.binding?.undo();
|
|
235
|
+
if (action === "redo")
|
|
236
|
+
this.storage.binding?.redo();
|
|
237
|
+
},
|
|
238
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { BindingClient } from "@barocss/doc-sync/client";
|
|
2
|
+
import type { EditorHost } from "@barocss/doc-sync/editor";
|
|
3
|
+
import type { DocSyncOptions } from "./index.ts";
|
|
4
|
+
export declare function initialClientModel(client: BindingClient): import("@barocss/doc-sync").NodeJSON;
|
|
5
|
+
/** Preserve legacy callback ordering while the native binding uses the Host contract. */
|
|
6
|
+
export declare function createTiptapClientCompat(input: BindingClient, options: DocSyncOptions, host?: EditorHost): {
|
|
7
|
+
connect(pull: () => void, _redraw: () => void): void;
|
|
8
|
+
selection(anchor: string | null, focus: string | null, _publishSelection: boolean): void;
|
|
9
|
+
publishIfNeeded(): void;
|
|
10
|
+
dispose(): void;
|
|
11
|
+
};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { createEditorHost, clientOwner } from "@barocss/doc-sync/client";
|
|
2
|
+
import { retireClientCallback, resolveClientCallback } from "@barocss/doc-sync/editor";
|
|
3
|
+
import { SyncClient } from "@barocss/doc-sync/sync";
|
|
4
|
+
const owners = new WeakMap();
|
|
5
|
+
export function initialClientModel(client) {
|
|
6
|
+
return createEditorHost(client).model().model;
|
|
7
|
+
}
|
|
8
|
+
/** Preserve legacy callback ordering while the native binding uses the Host contract. */
|
|
9
|
+
export function createTiptapClientCompat(input, options, host = createEditorHost(input)) {
|
|
10
|
+
const client = clientOwner(input);
|
|
11
|
+
if (!(client instanceof SyncClient))
|
|
12
|
+
return createLocalCompat(host, options);
|
|
13
|
+
const key = client;
|
|
14
|
+
if (owners.has(key))
|
|
15
|
+
throw new Error("DocSync: this SyncClient already has a Tiptap editor.");
|
|
16
|
+
const owner = {};
|
|
17
|
+
owners.set(key, owner);
|
|
18
|
+
let active = true;
|
|
19
|
+
let selection = null;
|
|
20
|
+
let sent = null;
|
|
21
|
+
let sentProfile = null;
|
|
22
|
+
const notify = (callback, invoke) => queueMicrotask(() => {
|
|
23
|
+
if (!active)
|
|
24
|
+
return;
|
|
25
|
+
try {
|
|
26
|
+
invoke();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
try {
|
|
30
|
+
if (options.onError)
|
|
31
|
+
options.onError(error, callback);
|
|
32
|
+
else
|
|
33
|
+
console.error(`DocSync: ${callback} failed.`, error);
|
|
34
|
+
}
|
|
35
|
+
catch (reportError) {
|
|
36
|
+
console.error("DocSync: onError failed.", reportError);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const profileKey = () => JSON.stringify(Object.entries(options.presence ?? {}).sort(([a], [b]) => a.localeCompare(b)));
|
|
41
|
+
const publish = () => {
|
|
42
|
+
if (!options.presence) {
|
|
43
|
+
sent = null;
|
|
44
|
+
sentProfile = null;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (!selection || !host.status().synchronized)
|
|
48
|
+
return;
|
|
49
|
+
const profile = profileKey();
|
|
50
|
+
const receipt = JSON.stringify([profile, ...selection]);
|
|
51
|
+
if (sent === receipt)
|
|
52
|
+
return;
|
|
53
|
+
// Store the receipt before transport delivery can synchronously re-enter.
|
|
54
|
+
sent = receipt;
|
|
55
|
+
sentProfile = profile;
|
|
56
|
+
const presence = { ...options.presence, anchor: selection[0], focus: selection[1] };
|
|
57
|
+
notify("presence", () => {
|
|
58
|
+
if (!host.status().synchronized) {
|
|
59
|
+
sent = null;
|
|
60
|
+
sentProfile = null;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
host.setPresence(presence);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
sent = null;
|
|
68
|
+
sentProfile = null;
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const priorModel = client.onModelEvents;
|
|
74
|
+
const priorUpdate = client.onUpdate;
|
|
75
|
+
let model = null;
|
|
76
|
+
let update = null;
|
|
77
|
+
return {
|
|
78
|
+
connect(pull, redraw) {
|
|
79
|
+
model = (events, info) => {
|
|
80
|
+
if (!active) {
|
|
81
|
+
resolveClientCallback(priorModel)?.(events, info);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
if (priorModel)
|
|
85
|
+
notify("onModelEvents", () => priorModel(events, info));
|
|
86
|
+
if (active)
|
|
87
|
+
pull();
|
|
88
|
+
};
|
|
89
|
+
update = () => {
|
|
90
|
+
if (!active) {
|
|
91
|
+
resolveClientCallback(priorUpdate)?.();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (priorUpdate)
|
|
95
|
+
notify("onUpdate", priorUpdate);
|
|
96
|
+
if (active)
|
|
97
|
+
redraw();
|
|
98
|
+
};
|
|
99
|
+
client.onModelEvents = model;
|
|
100
|
+
client.onUpdate = update;
|
|
101
|
+
},
|
|
102
|
+
selection(anchor, focus, publishSelection) {
|
|
103
|
+
const changed = !selection || selection[0] !== anchor || selection[1] !== focus;
|
|
104
|
+
selection = [anchor, focus];
|
|
105
|
+
if (changed && options.onSelection) {
|
|
106
|
+
const callback = options.onSelection;
|
|
107
|
+
notify("onSelection", () => callback(anchor, focus));
|
|
108
|
+
}
|
|
109
|
+
if (publishSelection)
|
|
110
|
+
publish();
|
|
111
|
+
},
|
|
112
|
+
publishIfNeeded() {
|
|
113
|
+
if (!host.status().synchronized || !options.presence) {
|
|
114
|
+
sent = null;
|
|
115
|
+
sentProfile = null;
|
|
116
|
+
}
|
|
117
|
+
else if (sent === null || sentProfile !== profileKey())
|
|
118
|
+
publish();
|
|
119
|
+
},
|
|
120
|
+
dispose() {
|
|
121
|
+
active = false;
|
|
122
|
+
if (model)
|
|
123
|
+
retireClientCallback(model, priorModel);
|
|
124
|
+
if (update)
|
|
125
|
+
retireClientCallback(update, priorUpdate);
|
|
126
|
+
if (model && client.onModelEvents === model)
|
|
127
|
+
client.onModelEvents = resolveClientCallback(priorModel);
|
|
128
|
+
if (update && client.onUpdate === update)
|
|
129
|
+
client.onUpdate = resolveClientCallback(priorUpdate);
|
|
130
|
+
if (owners.get(key) === owner)
|
|
131
|
+
owners.delete(key);
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** Local views subscribe through their own host origin. They do not publish presence. */
|
|
136
|
+
function createLocalCompat(host, options) {
|
|
137
|
+
let active = true;
|
|
138
|
+
let stop = null;
|
|
139
|
+
let selection = null;
|
|
140
|
+
return {
|
|
141
|
+
connect(pull, _redraw) {
|
|
142
|
+
stop = host.subscribe({ modelEvents: () => { if (active)
|
|
143
|
+
pull(); } });
|
|
144
|
+
},
|
|
145
|
+
selection(anchor, focus, _publishSelection) {
|
|
146
|
+
const changed = !selection || selection[0] !== anchor || selection[1] !== focus;
|
|
147
|
+
selection = [anchor, focus];
|
|
148
|
+
if (!changed || !options.onSelection)
|
|
149
|
+
return;
|
|
150
|
+
const callback = options.onSelection;
|
|
151
|
+
queueMicrotask(() => {
|
|
152
|
+
if (!active)
|
|
153
|
+
return;
|
|
154
|
+
try {
|
|
155
|
+
callback(anchor, focus);
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
try {
|
|
159
|
+
if (options.onError)
|
|
160
|
+
options.onError(error, "onSelection");
|
|
161
|
+
else
|
|
162
|
+
console.error("DocSync: onSelection failed.", error);
|
|
163
|
+
}
|
|
164
|
+
catch (reportError) {
|
|
165
|
+
console.error("DocSync: onError failed.", reportError);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
publishIfNeeded() { },
|
|
171
|
+
dispose() { active = false; stop?.(); stop = null; },
|
|
172
|
+
};
|
|
173
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
|
+
"version": "0.1.0",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"engines": {
|
|
5
|
+
"node": ">=22.18"
|
|
6
|
+
},
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE"
|
|
11
|
+
],
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "node ../../scripts/build-package.mjs",
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
18
|
+
"prepack": "npm run build"
|
|
19
|
+
},
|
|
2
20
|
"name": "@barocss/doc-sync-tiptap",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
21
|
+
"description": "DocSync tiptap binding",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"@barocss/doc-sync": "^0.1.0",
|
|
31
|
+
"@tiptap/core": "3.32.2",
|
|
32
|
+
"@tiptap/pm": "3.32.2",
|
|
33
|
+
"@barocss/doc-sync-prosemirror": "^0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"homepage": "https://github.com/barocss/doc-sync#readme",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/barocss/doc-sync.git",
|
|
40
|
+
"directory": "packages/doc-sync-tiptap"
|
|
41
|
+
},
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/barocss/doc-sync/issues"
|
|
44
|
+
}
|
|
45
|
+
}
|