@tiptap/y-tiptap 1.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Kevin Jahns <kevin.jahns@protonmail.com>.
4
+ Copyright (c) 2025 Tiptap GmbH.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,203 @@
1
+ # y-tiptap
2
+
3
+ > [Tiptap](http://tiptap.dev/) Binding for [Yjs](https://github.com/yjs/yjs)
4
+
5
+ This binding maps a Y.XmlFragment to the ProseMirror state.
6
+
7
+ ## Features
8
+
9
+ * Sync ProseMirror state
10
+ * Shared Cursors
11
+ * Shared Undo / Redo (each client has its own undo-/redo-history)
12
+ * Successfully recovers when concurrents edit result in an invalid document schema
13
+
14
+ ### Example
15
+
16
+ ```js
17
+ import { ySyncPlugin, yCursorPlugin, yUndoPlugin, undo, redo, initProseMirrorDoc } from '@tiptap/y-tiptap'
18
+ import { exampleSetup } from 'prosemirror-example-setup'
19
+ import { keymap } from '@tiptap/pm/keymap'
20
+ ..
21
+
22
+ const type = ydocument.get('prosemirror', Y.XmlFragment)
23
+ const { doc, mapping } = initProseMirrorDoc(type, schema)
24
+
25
+ const prosemirrorView = new EditorView(document.querySelector('#editor'), {
26
+ state: EditorState.create({
27
+ doc,
28
+ schema,
29
+ plugins: [
30
+ ySyncPlugin(type, { mapping }),
31
+ yCursorPlugin(provider.awareness),
32
+ yUndoPlugin(),
33
+ keymap({
34
+ 'Mod-z': undo,
35
+ 'Mod-y': redo,
36
+ 'Mod-Shift-z': redo
37
+ })
38
+ ].concat(exampleSetup({ schema }))
39
+ })
40
+ })
41
+ ```
42
+
43
+ #### Remote Cursors
44
+
45
+ The shared cursors depend on the Awareness instance that is exported by most providers. The [Awareness protocol](https://github.com/yjs/y-protocols#awareness-protocol) handles non-permanent data like the number of users, their user names, their cursor location, and their colors. You can change the name and color of the user like this:
46
+
47
+ ```js
48
+ example.binding.awareness.setLocalStateField('user', { color: '#008833', name: 'My real name' })
49
+ ```
50
+
51
+ In order to render cursor information you need to embed custom CSS for the user icon. This is a template that you can use for styling cursor information.
52
+
53
+ ```css
54
+ /* this is a rough fix for the first cursor position when the first paragraph is empty */
55
+ .ProseMirror > .ProseMirror-yjs-cursor:first-child {
56
+ margin-top: 16px;
57
+ }
58
+ .ProseMirror p:first-child, .ProseMirror h1:first-child, .ProseMirror h2:first-child, .ProseMirror h3:first-child, .ProseMirror h4:first-child, .ProseMirror h5:first-child, .ProseMirror h6:first-child {
59
+ margin-top: 16px
60
+ }
61
+ /* This gives the remote user caret. The colors are automatically overwritten*/
62
+ .ProseMirror-yjs-cursor {
63
+ position: relative;
64
+ margin-left: -1px;
65
+ margin-right: -1px;
66
+ border-left: 1px solid black;
67
+ border-right: 1px solid black;
68
+ border-color: orange;
69
+ word-break: normal;
70
+ pointer-events: none;
71
+ }
72
+ /* This renders the username above the caret */
73
+ .ProseMirror-yjs-cursor > div {
74
+ position: absolute;
75
+ top: -1.05em;
76
+ left: -1px;
77
+ font-size: 13px;
78
+ background-color: rgb(250, 129, 0);
79
+ font-family: serif;
80
+ font-style: normal;
81
+ font-weight: normal;
82
+ line-height: normal;
83
+ user-select: none;
84
+ color: white;
85
+ padding-left: 2px;
86
+ padding-right: 2px;
87
+ white-space: nowrap;
88
+ }
89
+ ```
90
+
91
+ You can also overwrite the default Widget dom by specifying a cursor builder in the yCursorPlugin
92
+
93
+ ```js
94
+ /**
95
+ * This function receives the remote users "user" awareness state.
96
+ */
97
+ export const myCursorBuilder = user => {
98
+ const cursor = document.createElement('span')
99
+ cursor.classList.add('ProseMirror-yjs-cursor')
100
+ cursor.setAttribute('style', `border-color: ${user.color}`)
101
+ const userDiv = document.createElement('div')
102
+ userDiv.setAttribute('style', `background-color: ${user.color}`)
103
+ userDiv.insertBefore(document.createTextNode(user.name), null)
104
+ cursor.insertBefore(userDiv, null)
105
+ return cursor
106
+ }
107
+
108
+ const prosemirrorView = new EditorView(document.querySelector('#editor'), {
109
+ state: EditorState.create({
110
+ schema,
111
+ plugins: [
112
+ ySyncPlugin(type),
113
+ yCursorPlugin(provider.awareness, { cursorBuilder: myCursorBuilder }),
114
+ yUndoPlugin(),
115
+ keymap({
116
+ 'Mod-z': undo,
117
+ 'Mod-y': redo,
118
+ 'Mod-Shift-z': redo
119
+ })
120
+ ].concat(exampleSetup({ schema }))
121
+ })
122
+ })
123
+ ```
124
+
125
+ #### Utilities
126
+
127
+ The package includes a number of utility methods for converting back and forth between
128
+ a Y.Doc and Prosemirror compatible data structures. These can be useful for persisting
129
+ to a datastore or for importing existing documents.
130
+
131
+ > _Note_: Serializing and deserializing to JSON will not store collaboration history
132
+ > steps and as such should not be used as the primary storage. You will still need
133
+ > to store the Y.Doc binary update format.
134
+
135
+ ```js
136
+ import { prosemirrorToYDoc } from '@tiptap/y-tiptap'
137
+
138
+ // Pass JSON previously output from Prosemirror
139
+ const doc = Node.fromJSON(schema, {
140
+ type: "doc",
141
+ content: [...]
142
+ })
143
+ const ydoc = prosemirrorToYDoc(doc)
144
+ ```
145
+
146
+ Because JSON is a common usecase there is an equivalent method that skips the need
147
+ to create a Prosemirror Node.
148
+
149
+ ```js
150
+ import { prosemirrorJSONToYDoc } from '@tiptap/y-tiptap'
151
+
152
+ // Pass JSON previously output from Prosemirror
153
+ const ydoc = prosemirrorJSONToYDoc(schema, {
154
+ type: "doc",
155
+ content: [...]
156
+ })
157
+ ```
158
+
159
+ ```js
160
+ import { yDocToProsemirror } from '@tiptap/y-tiptap'
161
+
162
+ // apply binary updates from elsewhere
163
+ const ydoc = new Y.Doc()
164
+ ydoc.applyUpdate(update)
165
+
166
+ const node = yDocToProsemirror(schema, ydoc)
167
+ ```
168
+
169
+ Because JSON is a common usecase there is an equivalent method that outputs JSON
170
+ directly, this method does not require the Prosemirror schema.
171
+
172
+ ```js
173
+ import { yDocToProsemirrorJSON } from '@tiptap/y-tiptap'
174
+
175
+ // apply binary updates from elsewhere
176
+ const ydoc = new Y.Doc()
177
+ ydoc.applyUpdate(update)
178
+
179
+ const node = yDocToProsemirrorJSON(ydoc)
180
+ ```
181
+
182
+ ### Undo/Redo
183
+
184
+ The package exports `undo` and `redo` commands which can be used in place of
185
+ [prosemirror-history](https://prosemirror.net/docs/ref/#history) by mapping the
186
+ mod-Z/Y keys - see [ProseMirror](https://github.com/yjs/yjs-demos/blob/main/prosemirror/prosemirror.js#L29)
187
+ and [Tiptap](https://github.com/ueberdosis/tiptap/blob/main/packages/extension-collaboration/src/collaboration.ts)
188
+ examples.
189
+
190
+ Undo and redo are be scoped to the local client, so one peer won't undo another's
191
+ changes. See [Y.UndoManager](https://docs.yjs.dev/api/undo-manager) for more details.
192
+
193
+ Just like prosemirror-history, you can set a transaction's `addToHistory` meta property
194
+ to false to prevent that transaction from being rolled back by undo. This can be helpful for programmatic
195
+ document changes that aren't initiated by the user.
196
+
197
+ ```js
198
+ tr.setMeta("addToHistory", false);
199
+ ```
200
+
201
+ ### License
202
+
203
+ [The MIT License](./LICENSE) © Kevin Jahns, Tiptap GmbH
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Utility method to convert a Prosemirror Doc Node into a Y.Doc.
3
+ *
4
+ * This can be used when importing existing content to Y.Doc for the first time,
5
+ * note that this should not be used to rehydrate a Y.Doc from a database once
6
+ * collaboration has begun as all history will be lost
7
+ *
8
+ * @param {Node} doc
9
+ * @param {string} xmlFragment
10
+ * @return {Y.Doc}
11
+ */
12
+ export function prosemirrorToYDoc(doc: Node, xmlFragment?: string): Y.Doc;
13
+ /**
14
+ * Utility method to update an empty Y.XmlFragment with content from a Prosemirror Doc Node.
15
+ *
16
+ * This can be used when importing existing content to Y.Doc for the first time,
17
+ * note that this should not be used to rehydrate a Y.Doc from a database once
18
+ * collaboration has begun as all history will be lost
19
+ *
20
+ * Note: The Y.XmlFragment does not need to be part of a Y.Doc document at the time that this
21
+ * method is called, but it must be added before any other operations are performed on it.
22
+ *
23
+ * @param {Node} doc prosemirror document.
24
+ * @param {Y.XmlFragment} [xmlFragment] If supplied, an xml fragment to be
25
+ * populated from the prosemirror state; otherwise a new XmlFragment will be created.
26
+ * @return {Y.XmlFragment}
27
+ */
28
+ export function prosemirrorToYXmlFragment(doc: Node, xmlFragment?: Y.XmlFragment): Y.XmlFragment;
29
+ /**
30
+ * Utility method to convert Prosemirror compatible JSON into a Y.Doc.
31
+ *
32
+ * This can be used when importing existing content to Y.Doc for the first time,
33
+ * note that this should not be used to rehydrate a Y.Doc from a database once
34
+ * collaboration has begun as all history will be lost
35
+ *
36
+ * @param {Schema} schema
37
+ * @param {any} state
38
+ * @param {string} xmlFragment
39
+ * @return {Y.Doc}
40
+ */
41
+ export function prosemirrorJSONToYDoc(schema: Schema, state: any, xmlFragment?: string): Y.Doc;
42
+ /**
43
+ * Utility method to convert Prosemirror compatible JSON to a Y.XmlFragment
44
+ *
45
+ * This can be used when importing existing content to Y.Doc for the first time,
46
+ * note that this should not be used to rehydrate a Y.Doc from a database once
47
+ * collaboration has begun as all history will be lost
48
+ *
49
+ * @param {Schema} schema
50
+ * @param {any} state
51
+ * @param {Y.XmlFragment} [xmlFragment] If supplied, an xml fragment to be
52
+ * populated from the prosemirror state; otherwise a new XmlFragment will be created.
53
+ * @return {Y.XmlFragment}
54
+ */
55
+ export function prosemirrorJSONToYXmlFragment(schema: Schema, state: any, xmlFragment?: Y.XmlFragment): Y.XmlFragment;
56
+ /**
57
+ * @deprecated Use `yXmlFragmentToProseMirrorRootNode` instead
58
+ *
59
+ * Utility method to convert a Y.Doc to a Prosemirror Doc node.
60
+ *
61
+ * @param {Schema} schema
62
+ * @param {Y.Doc} ydoc
63
+ * @return {Node}
64
+ */
65
+ export function yDocToProsemirror(schema: Schema, ydoc: Y.Doc): Node;
66
+ /**
67
+ *
68
+ * @deprecated Use `yXmlFragmentToProseMirrorRootNode` instead
69
+ *
70
+ * Utility method to convert a Y.XmlFragment to a Prosemirror Doc node.
71
+ *
72
+ * @param {Schema} schema
73
+ * @param {Y.XmlFragment} xmlFragment
74
+ * @return {Node}
75
+ */
76
+ export function yXmlFragmentToProsemirror(schema: Schema, xmlFragment: Y.XmlFragment): Node;
77
+ /**
78
+ *
79
+ * @deprecated Use `yXmlFragmentToProseMirrorRootNode` instead
80
+ *
81
+ * Utility method to convert a Y.Doc to Prosemirror compatible JSON.
82
+ *
83
+ * @param {Y.Doc} ydoc
84
+ * @param {string} xmlFragment
85
+ * @return {Record<string, any>}
86
+ */
87
+ export function yDocToProsemirrorJSON(ydoc: Y.Doc, xmlFragment?: string): Record<string, any>;
88
+ /**
89
+ * @deprecated Use `yXmlFragmentToProseMirrorRootNode` instead
90
+ *
91
+ * Utility method to convert a Y.Doc to Prosemirror compatible JSON.
92
+ *
93
+ * @param {Y.XmlFragment} xmlFragment The fragment, which must be part of a Y.Doc.
94
+ * @return {Record<string, any>}
95
+ */
96
+ export function yXmlFragmentToProsemirrorJSON(xmlFragment: Y.XmlFragment): Record<string, any>;
97
+ export function setMeta(view: any, key: any, value: any): void;
98
+ export function absolutePositionToRelativePosition(pos: number, type: Y.XmlFragment, mapping: ProsemirrorMapping): any;
99
+ export function relativePositionToAbsolutePosition(y: Y.Doc, documentType: Y.XmlFragment, relPos: any, mapping: ProsemirrorMapping): null | number;
100
+ export function yXmlFragmentToProseMirrorFragment(yXmlFragment: Y.XmlFragment, schema: Schema): Fragment;
101
+ export function yXmlFragmentToProseMirrorRootNode(yXmlFragment: Y.XmlFragment, schema: Schema): Node;
102
+ export function initProseMirrorDoc(yXmlFragment: Y.XmlFragment, schema: Schema): {
103
+ doc: Node;
104
+ mapping: ProsemirrorMapping;
105
+ };
106
+ /**
107
+ * Either a node if type is YXmlElement or an Array of text nodes if YXmlText
108
+ */
109
+ export type ProsemirrorMapping = Map<Y.AbstractType<any>, Node | Array<Node>>;
110
+ import { Node } from '@tiptap/pm/model';
111
+ import * as Y from 'yjs';
112
+ import { Schema } from '@tiptap/pm/model';
113
+ import { Fragment } from '@tiptap/pm/model';
@@ -0,0 +1,17 @@
1
+ export function defaultAwarenessStateFilter(currentClientId: number, userClientId: number, _user: any): boolean;
2
+ export function defaultCursorBuilder(user: any): HTMLElement;
3
+ export function defaultSelectionBuilder(user: any): import('@tiptap/pm/view').DecorationAttrs;
4
+ export function createDecorations(state: any, awareness: Awareness, awarenessFilter: (arg0: number, arg1: number, arg2: any) => boolean, createCursor: (arg0: {
5
+ name: string;
6
+ color: string;
7
+ }) => Element, createSelection: (arg0: {
8
+ name: string;
9
+ color: string;
10
+ }) => import('@tiptap/pm/view').DecorationAttrs): any;
11
+ export function yCursorPlugin(awareness: Awareness, { awarenessStateFilter, cursorBuilder, selectionBuilder, getSelection }?: {
12
+ awarenessStateFilter?: (arg0: any, arg1: any, arg2: any) => boolean;
13
+ cursorBuilder?: (arg0: any) => HTMLElement;
14
+ selectionBuilder?: (arg0: any) => import('@tiptap/pm/view').DecorationAttrs;
15
+ getSelection?: (arg0: any) => any;
16
+ }, cursorStateField?: string): any;
17
+ import { Awareness } from "y-protocols/awareness";
@@ -0,0 +1,19 @@
1
+ /**
2
+ * The unique prosemirror plugin key for syncPlugin
3
+ *
4
+ * @public
5
+ */
6
+ export const ySyncPluginKey: PluginKey<any>;
7
+ /**
8
+ * The unique prosemirror plugin key for undoPlugin
9
+ *
10
+ * @public
11
+ */
12
+ export const yUndoPluginKey: PluginKey<any>;
13
+ /**
14
+ * The unique prosemirror plugin key for cursorPlugin
15
+ *
16
+ * @public
17
+ */
18
+ export const yCursorPluginKey: PluginKey<any>;
19
+ import { PluginKey } from '@tiptap/pm/state';
@@ -0,0 +1,100 @@
1
+ export const MarkPrefix: "_mark_";
2
+ export function isVisible(item: Y.Item, snapshot?: Y.Snapshot): boolean;
3
+ export function ySyncPlugin(yXmlFragment: Y.XmlFragment, { colors, colorMapping, permanentUserData, onFirstRender, mapping }?: YSyncOpts): any;
4
+ export function getRelativeSelection(pmbinding: any, state: any): {
5
+ anchor: any;
6
+ head: any;
7
+ };
8
+ /**
9
+ * Binding for prosemirror.
10
+ *
11
+ * @protected
12
+ */
13
+ export class ProsemirrorBinding {
14
+ /**
15
+ * @param {Y.XmlFragment} yXmlFragment The bind source
16
+ * @param {ProsemirrorMapping} mapping
17
+ */
18
+ constructor(yXmlFragment: Y.XmlFragment, mapping?: ProsemirrorMapping);
19
+ type: Y.XmlFragment;
20
+ /**
21
+ * this will be set once the view is created
22
+ * @type {any}
23
+ */
24
+ prosemirrorView: any;
25
+ mux: import("lib0/mutex").mutex;
26
+ mapping: ProsemirrorMapping;
27
+ _observeFunction: any;
28
+ /**
29
+ * @type {Y.Doc}
30
+ */
31
+ doc: Y.Doc;
32
+ /**
33
+ * current selection as relative positions in the Yjs model
34
+ */
35
+ beforeTransactionSelection: {
36
+ anchor: any;
37
+ head: any;
38
+ };
39
+ beforeAllTransactions: () => void;
40
+ afterAllTransactions: () => void;
41
+ _domSelectionInView: boolean;
42
+ /**
43
+ * Create a transaction for changing the prosemirror state.
44
+ *
45
+ * @returns
46
+ */
47
+ get _tr(): any;
48
+ _isLocalCursorInView(): boolean;
49
+ _isDomSelectionInView(): boolean;
50
+ /**
51
+ * @param {Y.Snapshot} snapshot
52
+ * @param {Y.Snapshot} prevSnapshot
53
+ */
54
+ renderSnapshot(snapshot: Y.Snapshot, prevSnapshot: Y.Snapshot): void;
55
+ unrenderSnapshot(): void;
56
+ _forceRerender(): void;
57
+ /**
58
+ * @param {Y.Snapshot|Uint8Array} snapshot
59
+ * @param {Y.Snapshot|Uint8Array} prevSnapshot
60
+ * @param {Object} pluginState
61
+ */
62
+ _renderSnapshot(snapshot: Y.Snapshot | Uint8Array, prevSnapshot: Y.Snapshot | Uint8Array, pluginState: any): void;
63
+ /**
64
+ * @param {Array<Y.YEvent<any>>} events
65
+ * @param {Y.Transaction} transaction
66
+ */
67
+ _typeChanged(events: Array<Y.YEvent<any>>, transaction: Y.Transaction): void;
68
+ _prosemirrorChanged(doc: any): void;
69
+ /**
70
+ * View is ready to listen to changes. Register observers.
71
+ * @param {any} prosemirrorView
72
+ */
73
+ initView(prosemirrorView: any): void;
74
+ destroy(): void;
75
+ }
76
+ export function createNodeFromYElement(el: Y.XmlElement, schema: any, mapping: ProsemirrorMapping, snapshot?: Y.Snapshot, prevSnapshot?: Y.Snapshot, computeYChange?: (arg0: 'removed' | 'added', arg1: Y.ID) => any): PModel.Node | null;
77
+ export function updateYFragment(y: {
78
+ transact: Function;
79
+ }, yDomFragment: Y.XmlFragment, pNode: any, mapping: ProsemirrorMapping): void;
80
+ /**
81
+ * Either a node if type is YXmlElement or an Array of text nodes if YXmlText
82
+ */
83
+ export type ProsemirrorMapping = Map<Y.AbstractType<any>, PModel.Node | Array<PModel.Node>>;
84
+ export type ColorDef = {
85
+ light: string;
86
+ dark: string;
87
+ };
88
+ export type YSyncOpts = {
89
+ colors?: Array<ColorDef>;
90
+ colorMapping?: Map<string, ColorDef>;
91
+ permanentUserData?: Y.PermanentUserData | null;
92
+ mapping?: ProsemirrorMapping;
93
+ /**
94
+ * Fired when the content from Yjs is initially rendered to ProseMirror
95
+ */
96
+ onFirstRender?: Function;
97
+ };
98
+ export type NormalizedPNodeContent = Array<Array<PModel.Node> | PModel.Node>;
99
+ import * as Y from 'yjs';
100
+ import * as PModel from '@tiptap/pm/model';
@@ -0,0 +1,15 @@
1
+ export function undo(state: any): boolean;
2
+ export function redo(state: any): boolean;
3
+ export const defaultProtectedNodes: Set<string>;
4
+ export function defaultDeleteFilter(item: any, protectedNodes: any): boolean;
5
+ export function yUndoPlugin({ protectedNodes, trackedOrigins, undoManager }?: {
6
+ protectedNodes?: Set<string>;
7
+ trackedOrigins?: any[];
8
+ undoManager?: any;
9
+ }): Plugin<{
10
+ undoManager: any;
11
+ prevSel: any;
12
+ hasUndoOps: boolean;
13
+ hasRedoOps: boolean;
14
+ }>;
15
+ import { Plugin } from '@tiptap/pm/state';
@@ -0,0 +1,5 @@
1
+ export * from "./plugins/cursor-plugin.js";
2
+ export * from "./plugins/undo-plugin.js";
3
+ export * from "./plugins/keys.js";
4
+ export { ySyncPlugin, isVisible, getRelativeSelection, ProsemirrorBinding, updateYFragment } from "./plugins/sync-plugin.js";
5
+ export { absolutePositionToRelativePosition, relativePositionToAbsolutePosition, setMeta, prosemirrorJSONToYDoc, yDocToProsemirrorJSON, yDocToProsemirror, prosemirrorToYDoc, prosemirrorJSONToYXmlFragment, yXmlFragmentToProsemirrorJSON, yXmlFragmentToProsemirror, prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode, yXmlFragmentToProseMirrorFragment, initProseMirrorDoc } from "./lib.js";