@tiptap/extension-collaboration-caret 3.0.0-next.7
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.md +21 -0
- package/README.md +18 -0
- package/dist/index.cjs +121 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +88 -0
- package/dist/index.d.ts +88 -0
- package/dist/index.js +94 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -0
- package/src/collaboration-caret.ts +191 -0
- package/src/index.ts +5 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Tiptap GmbH
|
|
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
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @tiptap/extension-collaboration-caret
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@tiptap/extension-collaboration-caret)
|
|
4
|
+
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
|
5
|
+
[](https://www.npmjs.com/package/@tiptap/extension-collaboration-caret)
|
|
6
|
+
[](https://github.com/sponsors/ueberdosis)
|
|
7
|
+
|
|
8
|
+
## Introduction
|
|
9
|
+
|
|
10
|
+
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as _New York Times_, _The Guardian_ or _Atlassian_.
|
|
11
|
+
|
|
12
|
+
## Official Documentation
|
|
13
|
+
|
|
14
|
+
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
|
15
|
+
|
|
16
|
+
## License
|
|
17
|
+
|
|
18
|
+
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CollaborationCaret: () => CollaborationCaret,
|
|
24
|
+
default: () => index_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/collaboration-caret.ts
|
|
29
|
+
var import_core = require("@tiptap/core");
|
|
30
|
+
var import_y_tiptap = require("@tiptap/y-tiptap");
|
|
31
|
+
var awarenessStatesToArray = (states) => {
|
|
32
|
+
return Array.from(states.entries()).map(([key, value]) => {
|
|
33
|
+
return {
|
|
34
|
+
clientId: key,
|
|
35
|
+
...value.user
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
var defaultOnUpdate = () => null;
|
|
40
|
+
var CollaborationCaret = import_core.Extension.create({
|
|
41
|
+
name: "collaborationCaret",
|
|
42
|
+
priority: 999,
|
|
43
|
+
addOptions() {
|
|
44
|
+
return {
|
|
45
|
+
provider: null,
|
|
46
|
+
user: {
|
|
47
|
+
name: null,
|
|
48
|
+
color: null
|
|
49
|
+
},
|
|
50
|
+
render: (user) => {
|
|
51
|
+
const cursor = document.createElement("span");
|
|
52
|
+
cursor.classList.add("collaboration-carets__caret");
|
|
53
|
+
cursor.setAttribute("style", `border-color: ${user.color}`);
|
|
54
|
+
const label = document.createElement("div");
|
|
55
|
+
label.classList.add("collaboration-carets__label");
|
|
56
|
+
label.setAttribute("style", `background-color: ${user.color}`);
|
|
57
|
+
label.insertBefore(document.createTextNode(user.name), null);
|
|
58
|
+
cursor.insertBefore(label, null);
|
|
59
|
+
return cursor;
|
|
60
|
+
},
|
|
61
|
+
selectionRender: import_y_tiptap.defaultSelectionBuilder,
|
|
62
|
+
onUpdate: defaultOnUpdate
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
onCreate() {
|
|
66
|
+
if (this.options.onUpdate !== defaultOnUpdate) {
|
|
67
|
+
console.warn(
|
|
68
|
+
'[tiptap warn]: DEPRECATED: The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret'
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
if (!this.options.provider) {
|
|
72
|
+
throw new Error('The "provider" option is required for the CollaborationCaret extension');
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
addStorage() {
|
|
76
|
+
return {
|
|
77
|
+
users: []
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
addCommands() {
|
|
81
|
+
return {
|
|
82
|
+
updateUser: (attributes) => () => {
|
|
83
|
+
this.options.user = attributes;
|
|
84
|
+
this.options.provider.awareness.setLocalStateField("user", this.options.user);
|
|
85
|
+
return true;
|
|
86
|
+
},
|
|
87
|
+
user: (attributes) => ({ editor }) => {
|
|
88
|
+
console.warn(
|
|
89
|
+
'[tiptap warn]: DEPRECATED: The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret'
|
|
90
|
+
);
|
|
91
|
+
return editor.commands.updateUser(attributes);
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
addProseMirrorPlugins() {
|
|
96
|
+
return [
|
|
97
|
+
(0, import_y_tiptap.yCursorPlugin)(
|
|
98
|
+
(() => {
|
|
99
|
+
this.options.provider.awareness.setLocalStateField("user", this.options.user);
|
|
100
|
+
this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states);
|
|
101
|
+
this.options.provider.awareness.on("update", () => {
|
|
102
|
+
this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states);
|
|
103
|
+
});
|
|
104
|
+
return this.options.provider.awareness;
|
|
105
|
+
})(),
|
|
106
|
+
{
|
|
107
|
+
cursorBuilder: this.options.render,
|
|
108
|
+
selectionBuilder: this.options.selectionRender
|
|
109
|
+
}
|
|
110
|
+
)
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// src/index.ts
|
|
116
|
+
var index_default = CollaborationCaret;
|
|
117
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
118
|
+
0 && (module.exports = {
|
|
119
|
+
CollaborationCaret
|
|
120
|
+
});
|
|
121
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/collaboration-caret.ts"],"sourcesContent":["import { CollaborationCaret } from './collaboration-caret.js'\n\nexport * from './collaboration-caret.js'\n\nexport default CollaborationCaret\n","import { Extension } from '@tiptap/core'\nimport type { DecorationAttrs } from '@tiptap/pm/view'\nimport { defaultSelectionBuilder, yCursorPlugin } from '@tiptap/y-tiptap'\n\ntype CollaborationCaretStorage = {\n users: { clientId: number; [key: string]: any }[]\n}\n\nexport interface CollaborationCaretOptions {\n /**\n * The Hocuspocus provider instance. This can also be a TiptapCloudProvider instance.\n * @type {HocuspocusProvider | TiptapCloudProvider}\n * @example new HocuspocusProvider()\n */\n provider: any\n\n /**\n * The user details object – feel free to add properties to this object as needed\n * @example { name: 'John Doe', color: '#305500' }\n */\n user: Record<string, any>\n\n /**\n * A function that returns a DOM element for the cursor.\n * @param user The user details object\n * @example\n * render: user => {\n * const cursor = document.createElement('span')\n * cursor.classList.add('collaboration-carets__caret')\n * cursor.setAttribute('style', `border-color: ${user.color}`)\n *\n * const label = document.createElement('div')\n * label.classList.add('collaboration-carets__label')\n * label.setAttribute('style', `background-color: ${user.color}`)\n * label.insertBefore(document.createTextNode(user.name), null)\n *\n * cursor.insertBefore(label, null)\n * return cursor\n * }\n */\n render(user: Record<string, any>): HTMLElement\n\n /**\n * A function that returns a ProseMirror DecorationAttrs object for the selection.\n * @param user The user details object\n * @example\n * selectionRender: user => {\n * return {\n * nodeName: 'span',\n * class: 'collaboration-carets__selection',\n * style: `background-color: ${user.color}`,\n * 'data-user': user.name,\n * }\n */\n selectionRender(user: Record<string, any>): DecorationAttrs\n\n /**\n * @deprecated The \"onUpdate\" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret\n */\n onUpdate: (users: { clientId: number; [key: string]: any }[]) => null\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n collaborationCaret: {\n /**\n * Update details of the current user\n * @example editor.commands.updateUser({ name: 'John Doe', color: '#305500' })\n */\n updateUser: (attributes: Record<string, any>) => ReturnType\n /**\n * Update details of the current user\n *\n * @deprecated The \"user\" command is deprecated. Please use \"updateUser\" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret\n */\n user: (attributes: Record<string, any>) => ReturnType\n }\n }\n\n interface Storage {\n collaborationCaret: CollaborationCaretStorage\n }\n}\n\nconst awarenessStatesToArray = (states: Map<number, Record<string, any>>) => {\n return Array.from(states.entries()).map(([key, value]) => {\n return {\n clientId: key,\n ...value.user,\n }\n })\n}\n\nconst defaultOnUpdate = () => null\n\n/**\n * This extension allows you to add collaboration carets to your editor.\n * @see https://tiptap.dev/api/extensions/collaboration-caret\n */\nexport const CollaborationCaret = Extension.create<CollaborationCaretOptions, CollaborationCaretStorage>({\n name: 'collaborationCaret',\n\n priority: 999,\n\n addOptions() {\n return {\n provider: null,\n user: {\n name: null,\n color: null,\n },\n render: user => {\n const cursor = document.createElement('span')\n\n cursor.classList.add('collaboration-carets__caret')\n cursor.setAttribute('style', `border-color: ${user.color}`)\n\n const label = document.createElement('div')\n\n label.classList.add('collaboration-carets__label')\n label.setAttribute('style', `background-color: ${user.color}`)\n label.insertBefore(document.createTextNode(user.name), null)\n cursor.insertBefore(label, null)\n\n return cursor\n },\n selectionRender: defaultSelectionBuilder,\n onUpdate: defaultOnUpdate,\n }\n },\n\n onCreate() {\n if (this.options.onUpdate !== defaultOnUpdate) {\n console.warn(\n '[tiptap warn]: DEPRECATED: The \"onUpdate\" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret',\n )\n }\n if (!this.options.provider) {\n throw new Error('The \"provider\" option is required for the CollaborationCaret extension')\n }\n },\n\n addStorage() {\n return {\n users: [],\n }\n },\n\n addCommands() {\n return {\n updateUser: attributes => () => {\n this.options.user = attributes\n\n this.options.provider.awareness.setLocalStateField('user', this.options.user)\n\n return true\n },\n user:\n attributes =>\n ({ editor }) => {\n console.warn(\n '[tiptap warn]: DEPRECATED: The \"user\" command is deprecated. Please use \"updateUser\" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret',\n )\n\n return editor.commands.updateUser(attributes)\n },\n }\n },\n\n addProseMirrorPlugins() {\n return [\n yCursorPlugin(\n (() => {\n this.options.provider.awareness.setLocalStateField('user', this.options.user)\n\n this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states)\n\n this.options.provider.awareness.on('update', () => {\n this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states)\n })\n\n return this.options.provider.awareness\n })(),\n {\n cursorBuilder: this.options.render,\n selectionBuilder: this.options.selectionRender,\n },\n ),\n ]\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0B;AAE1B,sBAAuD;AAkFvD,IAAM,yBAAyB,CAAC,WAA6C;AAC3E,SAAO,MAAM,KAAK,OAAO,QAAQ,CAAC,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AACxD,WAAO;AAAA,MACL,UAAU;AAAA,MACV,GAAG,MAAM;AAAA,IACX;AAAA,EACF,CAAC;AACH;AAEA,IAAM,kBAAkB,MAAM;AAMvB,IAAM,qBAAqB,sBAAU,OAA6D;AAAA,EACvG,MAAM;AAAA,EAEN,UAAU;AAAA,EAEV,aAAa;AACX,WAAO;AAAA,MACL,UAAU;AAAA,MACV,MAAM;AAAA,QACJ,MAAM;AAAA,QACN,OAAO;AAAA,MACT;AAAA,MACA,QAAQ,UAAQ;AACd,cAAM,SAAS,SAAS,cAAc,MAAM;AAE5C,eAAO,UAAU,IAAI,6BAA6B;AAClD,eAAO,aAAa,SAAS,iBAAiB,KAAK,KAAK,EAAE;AAE1D,cAAM,QAAQ,SAAS,cAAc,KAAK;AAE1C,cAAM,UAAU,IAAI,6BAA6B;AACjD,cAAM,aAAa,SAAS,qBAAqB,KAAK,KAAK,EAAE;AAC7D,cAAM,aAAa,SAAS,eAAe,KAAK,IAAI,GAAG,IAAI;AAC3D,eAAO,aAAa,OAAO,IAAI;AAE/B,eAAO;AAAA,MACT;AAAA,MACA,iBAAiB;AAAA,MACjB,UAAU;AAAA,IACZ;AAAA,EACF;AAAA,EAEA,WAAW;AACT,QAAI,KAAK,QAAQ,aAAa,iBAAiB;AAC7C,cAAQ;AAAA,QACN;AAAA,MACF;AAAA,IACF;AACA,QAAI,CAAC,KAAK,QAAQ,UAAU;AAC1B,YAAM,IAAI,MAAM,wEAAwE;AAAA,IAC1F;AAAA,EACF;AAAA,EAEA,aAAa;AACX,WAAO;AAAA,MACL,OAAO,CAAC;AAAA,IACV;AAAA,EACF;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,YAAY,gBAAc,MAAM;AAC9B,aAAK,QAAQ,OAAO;AAEpB,aAAK,QAAQ,SAAS,UAAU,mBAAmB,QAAQ,KAAK,QAAQ,IAAI;AAE5E,eAAO;AAAA,MACT;AAAA,MACA,MACE,gBACA,CAAC,EAAE,OAAO,MAAM;AACd,gBAAQ;AAAA,UACN;AAAA,QACF;AAEA,eAAO,OAAO,SAAS,WAAW,UAAU;AAAA,MAC9C;AAAA,IACJ;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,WAAO;AAAA,UACL;AAAA,SACG,MAAM;AACL,eAAK,QAAQ,SAAS,UAAU,mBAAmB,QAAQ,KAAK,QAAQ,IAAI;AAE5E,eAAK,QAAQ,QAAQ,uBAAuB,KAAK,QAAQ,SAAS,UAAU,MAAM;AAElF,eAAK,QAAQ,SAAS,UAAU,GAAG,UAAU,MAAM;AACjD,iBAAK,QAAQ,QAAQ,uBAAuB,KAAK,QAAQ,SAAS,UAAU,MAAM;AAAA,UACpF,CAAC;AAED,iBAAO,KAAK,QAAQ,SAAS;AAAA,QAC/B,GAAG;AAAA,QACH;AAAA,UACE,eAAe,KAAK,QAAQ;AAAA,UAC5B,kBAAkB,KAAK,QAAQ;AAAA,QACjC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;;;AD1LD,IAAO,gBAAQ;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core';
|
|
2
|
+
import { DecorationAttrs } from '@tiptap/pm/view';
|
|
3
|
+
|
|
4
|
+
type CollaborationCaretStorage = {
|
|
5
|
+
users: {
|
|
6
|
+
clientId: number;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}[];
|
|
9
|
+
};
|
|
10
|
+
interface CollaborationCaretOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The Hocuspocus provider instance. This can also be a TiptapCloudProvider instance.
|
|
13
|
+
* @type {HocuspocusProvider | TiptapCloudProvider}
|
|
14
|
+
* @example new HocuspocusProvider()
|
|
15
|
+
*/
|
|
16
|
+
provider: any;
|
|
17
|
+
/**
|
|
18
|
+
* The user details object – feel free to add properties to this object as needed
|
|
19
|
+
* @example { name: 'John Doe', color: '#305500' }
|
|
20
|
+
*/
|
|
21
|
+
user: Record<string, any>;
|
|
22
|
+
/**
|
|
23
|
+
* A function that returns a DOM element for the cursor.
|
|
24
|
+
* @param user The user details object
|
|
25
|
+
* @example
|
|
26
|
+
* render: user => {
|
|
27
|
+
* const cursor = document.createElement('span')
|
|
28
|
+
* cursor.classList.add('collaboration-carets__caret')
|
|
29
|
+
* cursor.setAttribute('style', `border-color: ${user.color}`)
|
|
30
|
+
*
|
|
31
|
+
* const label = document.createElement('div')
|
|
32
|
+
* label.classList.add('collaboration-carets__label')
|
|
33
|
+
* label.setAttribute('style', `background-color: ${user.color}`)
|
|
34
|
+
* label.insertBefore(document.createTextNode(user.name), null)
|
|
35
|
+
*
|
|
36
|
+
* cursor.insertBefore(label, null)
|
|
37
|
+
* return cursor
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
render(user: Record<string, any>): HTMLElement;
|
|
41
|
+
/**
|
|
42
|
+
* A function that returns a ProseMirror DecorationAttrs object for the selection.
|
|
43
|
+
* @param user The user details object
|
|
44
|
+
* @example
|
|
45
|
+
* selectionRender: user => {
|
|
46
|
+
* return {
|
|
47
|
+
* nodeName: 'span',
|
|
48
|
+
* class: 'collaboration-carets__selection',
|
|
49
|
+
* style: `background-color: ${user.color}`,
|
|
50
|
+
* 'data-user': user.name,
|
|
51
|
+
* }
|
|
52
|
+
*/
|
|
53
|
+
selectionRender(user: Record<string, any>): DecorationAttrs;
|
|
54
|
+
/**
|
|
55
|
+
* @deprecated The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret
|
|
56
|
+
*/
|
|
57
|
+
onUpdate: (users: {
|
|
58
|
+
clientId: number;
|
|
59
|
+
[key: string]: any;
|
|
60
|
+
}[]) => null;
|
|
61
|
+
}
|
|
62
|
+
declare module '@tiptap/core' {
|
|
63
|
+
interface Commands<ReturnType> {
|
|
64
|
+
collaborationCaret: {
|
|
65
|
+
/**
|
|
66
|
+
* Update details of the current user
|
|
67
|
+
* @example editor.commands.updateUser({ name: 'John Doe', color: '#305500' })
|
|
68
|
+
*/
|
|
69
|
+
updateUser: (attributes: Record<string, any>) => ReturnType;
|
|
70
|
+
/**
|
|
71
|
+
* Update details of the current user
|
|
72
|
+
*
|
|
73
|
+
* @deprecated The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret
|
|
74
|
+
*/
|
|
75
|
+
user: (attributes: Record<string, any>) => ReturnType;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
interface Storage {
|
|
79
|
+
collaborationCaret: CollaborationCaretStorage;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* This extension allows you to add collaboration carets to your editor.
|
|
84
|
+
* @see https://tiptap.dev/api/extensions/collaboration-caret
|
|
85
|
+
*/
|
|
86
|
+
declare const CollaborationCaret: Extension<CollaborationCaretOptions, CollaborationCaretStorage>;
|
|
87
|
+
|
|
88
|
+
export { CollaborationCaret, type CollaborationCaretOptions, CollaborationCaret as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core';
|
|
2
|
+
import { DecorationAttrs } from '@tiptap/pm/view';
|
|
3
|
+
|
|
4
|
+
type CollaborationCaretStorage = {
|
|
5
|
+
users: {
|
|
6
|
+
clientId: number;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
}[];
|
|
9
|
+
};
|
|
10
|
+
interface CollaborationCaretOptions {
|
|
11
|
+
/**
|
|
12
|
+
* The Hocuspocus provider instance. This can also be a TiptapCloudProvider instance.
|
|
13
|
+
* @type {HocuspocusProvider | TiptapCloudProvider}
|
|
14
|
+
* @example new HocuspocusProvider()
|
|
15
|
+
*/
|
|
16
|
+
provider: any;
|
|
17
|
+
/**
|
|
18
|
+
* The user details object – feel free to add properties to this object as needed
|
|
19
|
+
* @example { name: 'John Doe', color: '#305500' }
|
|
20
|
+
*/
|
|
21
|
+
user: Record<string, any>;
|
|
22
|
+
/**
|
|
23
|
+
* A function that returns a DOM element for the cursor.
|
|
24
|
+
* @param user The user details object
|
|
25
|
+
* @example
|
|
26
|
+
* render: user => {
|
|
27
|
+
* const cursor = document.createElement('span')
|
|
28
|
+
* cursor.classList.add('collaboration-carets__caret')
|
|
29
|
+
* cursor.setAttribute('style', `border-color: ${user.color}`)
|
|
30
|
+
*
|
|
31
|
+
* const label = document.createElement('div')
|
|
32
|
+
* label.classList.add('collaboration-carets__label')
|
|
33
|
+
* label.setAttribute('style', `background-color: ${user.color}`)
|
|
34
|
+
* label.insertBefore(document.createTextNode(user.name), null)
|
|
35
|
+
*
|
|
36
|
+
* cursor.insertBefore(label, null)
|
|
37
|
+
* return cursor
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
render(user: Record<string, any>): HTMLElement;
|
|
41
|
+
/**
|
|
42
|
+
* A function that returns a ProseMirror DecorationAttrs object for the selection.
|
|
43
|
+
* @param user The user details object
|
|
44
|
+
* @example
|
|
45
|
+
* selectionRender: user => {
|
|
46
|
+
* return {
|
|
47
|
+
* nodeName: 'span',
|
|
48
|
+
* class: 'collaboration-carets__selection',
|
|
49
|
+
* style: `background-color: ${user.color}`,
|
|
50
|
+
* 'data-user': user.name,
|
|
51
|
+
* }
|
|
52
|
+
*/
|
|
53
|
+
selectionRender(user: Record<string, any>): DecorationAttrs;
|
|
54
|
+
/**
|
|
55
|
+
* @deprecated The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret
|
|
56
|
+
*/
|
|
57
|
+
onUpdate: (users: {
|
|
58
|
+
clientId: number;
|
|
59
|
+
[key: string]: any;
|
|
60
|
+
}[]) => null;
|
|
61
|
+
}
|
|
62
|
+
declare module '@tiptap/core' {
|
|
63
|
+
interface Commands<ReturnType> {
|
|
64
|
+
collaborationCaret: {
|
|
65
|
+
/**
|
|
66
|
+
* Update details of the current user
|
|
67
|
+
* @example editor.commands.updateUser({ name: 'John Doe', color: '#305500' })
|
|
68
|
+
*/
|
|
69
|
+
updateUser: (attributes: Record<string, any>) => ReturnType;
|
|
70
|
+
/**
|
|
71
|
+
* Update details of the current user
|
|
72
|
+
*
|
|
73
|
+
* @deprecated The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret
|
|
74
|
+
*/
|
|
75
|
+
user: (attributes: Record<string, any>) => ReturnType;
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
interface Storage {
|
|
79
|
+
collaborationCaret: CollaborationCaretStorage;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* This extension allows you to add collaboration carets to your editor.
|
|
84
|
+
* @see https://tiptap.dev/api/extensions/collaboration-caret
|
|
85
|
+
*/
|
|
86
|
+
declare const CollaborationCaret: Extension<CollaborationCaretOptions, CollaborationCaretStorage>;
|
|
87
|
+
|
|
88
|
+
export { CollaborationCaret, type CollaborationCaretOptions, CollaborationCaret as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/collaboration-caret.ts
|
|
2
|
+
import { Extension } from "@tiptap/core";
|
|
3
|
+
import { defaultSelectionBuilder, yCursorPlugin } from "@tiptap/y-tiptap";
|
|
4
|
+
var awarenessStatesToArray = (states) => {
|
|
5
|
+
return Array.from(states.entries()).map(([key, value]) => {
|
|
6
|
+
return {
|
|
7
|
+
clientId: key,
|
|
8
|
+
...value.user
|
|
9
|
+
};
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
var defaultOnUpdate = () => null;
|
|
13
|
+
var CollaborationCaret = Extension.create({
|
|
14
|
+
name: "collaborationCaret",
|
|
15
|
+
priority: 999,
|
|
16
|
+
addOptions() {
|
|
17
|
+
return {
|
|
18
|
+
provider: null,
|
|
19
|
+
user: {
|
|
20
|
+
name: null,
|
|
21
|
+
color: null
|
|
22
|
+
},
|
|
23
|
+
render: (user) => {
|
|
24
|
+
const cursor = document.createElement("span");
|
|
25
|
+
cursor.classList.add("collaboration-carets__caret");
|
|
26
|
+
cursor.setAttribute("style", `border-color: ${user.color}`);
|
|
27
|
+
const label = document.createElement("div");
|
|
28
|
+
label.classList.add("collaboration-carets__label");
|
|
29
|
+
label.setAttribute("style", `background-color: ${user.color}`);
|
|
30
|
+
label.insertBefore(document.createTextNode(user.name), null);
|
|
31
|
+
cursor.insertBefore(label, null);
|
|
32
|
+
return cursor;
|
|
33
|
+
},
|
|
34
|
+
selectionRender: defaultSelectionBuilder,
|
|
35
|
+
onUpdate: defaultOnUpdate
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
onCreate() {
|
|
39
|
+
if (this.options.onUpdate !== defaultOnUpdate) {
|
|
40
|
+
console.warn(
|
|
41
|
+
'[tiptap warn]: DEPRECATED: The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret'
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
if (!this.options.provider) {
|
|
45
|
+
throw new Error('The "provider" option is required for the CollaborationCaret extension');
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
addStorage() {
|
|
49
|
+
return {
|
|
50
|
+
users: []
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
addCommands() {
|
|
54
|
+
return {
|
|
55
|
+
updateUser: (attributes) => () => {
|
|
56
|
+
this.options.user = attributes;
|
|
57
|
+
this.options.provider.awareness.setLocalStateField("user", this.options.user);
|
|
58
|
+
return true;
|
|
59
|
+
},
|
|
60
|
+
user: (attributes) => ({ editor }) => {
|
|
61
|
+
console.warn(
|
|
62
|
+
'[tiptap warn]: DEPRECATED: The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret'
|
|
63
|
+
);
|
|
64
|
+
return editor.commands.updateUser(attributes);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
addProseMirrorPlugins() {
|
|
69
|
+
return [
|
|
70
|
+
yCursorPlugin(
|
|
71
|
+
(() => {
|
|
72
|
+
this.options.provider.awareness.setLocalStateField("user", this.options.user);
|
|
73
|
+
this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states);
|
|
74
|
+
this.options.provider.awareness.on("update", () => {
|
|
75
|
+
this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states);
|
|
76
|
+
});
|
|
77
|
+
return this.options.provider.awareness;
|
|
78
|
+
})(),
|
|
79
|
+
{
|
|
80
|
+
cursorBuilder: this.options.render,
|
|
81
|
+
selectionBuilder: this.options.selectionRender
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// src/index.ts
|
|
89
|
+
var index_default = CollaborationCaret;
|
|
90
|
+
export {
|
|
91
|
+
CollaborationCaret,
|
|
92
|
+
index_default as default
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/collaboration-caret.ts","../src/index.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport type { DecorationAttrs } from '@tiptap/pm/view'\nimport { defaultSelectionBuilder, yCursorPlugin } from '@tiptap/y-tiptap'\n\ntype CollaborationCaretStorage = {\n users: { clientId: number; [key: string]: any }[]\n}\n\nexport interface CollaborationCaretOptions {\n /**\n * The Hocuspocus provider instance. This can also be a TiptapCloudProvider instance.\n * @type {HocuspocusProvider | TiptapCloudProvider}\n * @example new HocuspocusProvider()\n */\n provider: any\n\n /**\n * The user details object – feel free to add properties to this object as needed\n * @example { name: 'John Doe', color: '#305500' }\n */\n user: Record<string, any>\n\n /**\n * A function that returns a DOM element for the cursor.\n * @param user The user details object\n * @example\n * render: user => {\n * const cursor = document.createElement('span')\n * cursor.classList.add('collaboration-carets__caret')\n * cursor.setAttribute('style', `border-color: ${user.color}`)\n *\n * const label = document.createElement('div')\n * label.classList.add('collaboration-carets__label')\n * label.setAttribute('style', `background-color: ${user.color}`)\n * label.insertBefore(document.createTextNode(user.name), null)\n *\n * cursor.insertBefore(label, null)\n * return cursor\n * }\n */\n render(user: Record<string, any>): HTMLElement\n\n /**\n * A function that returns a ProseMirror DecorationAttrs object for the selection.\n * @param user The user details object\n * @example\n * selectionRender: user => {\n * return {\n * nodeName: 'span',\n * class: 'collaboration-carets__selection',\n * style: `background-color: ${user.color}`,\n * 'data-user': user.name,\n * }\n */\n selectionRender(user: Record<string, any>): DecorationAttrs\n\n /**\n * @deprecated The \"onUpdate\" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret\n */\n onUpdate: (users: { clientId: number; [key: string]: any }[]) => null\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n collaborationCaret: {\n /**\n * Update details of the current user\n * @example editor.commands.updateUser({ name: 'John Doe', color: '#305500' })\n */\n updateUser: (attributes: Record<string, any>) => ReturnType\n /**\n * Update details of the current user\n *\n * @deprecated The \"user\" command is deprecated. Please use \"updateUser\" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret\n */\n user: (attributes: Record<string, any>) => ReturnType\n }\n }\n\n interface Storage {\n collaborationCaret: CollaborationCaretStorage\n }\n}\n\nconst awarenessStatesToArray = (states: Map<number, Record<string, any>>) => {\n return Array.from(states.entries()).map(([key, value]) => {\n return {\n clientId: key,\n ...value.user,\n }\n })\n}\n\nconst defaultOnUpdate = () => null\n\n/**\n * This extension allows you to add collaboration carets to your editor.\n * @see https://tiptap.dev/api/extensions/collaboration-caret\n */\nexport const CollaborationCaret = Extension.create<CollaborationCaretOptions, CollaborationCaretStorage>({\n name: 'collaborationCaret',\n\n priority: 999,\n\n addOptions() {\n return {\n provider: null,\n user: {\n name: null,\n color: null,\n },\n render: user => {\n const cursor = document.createElement('span')\n\n cursor.classList.add('collaboration-carets__caret')\n cursor.setAttribute('style', `border-color: ${user.color}`)\n\n const label = document.createElement('div')\n\n label.classList.add('collaboration-carets__label')\n label.setAttribute('style', `background-color: ${user.color}`)\n label.insertBefore(document.createTextNode(user.name), null)\n cursor.insertBefore(label, null)\n\n return cursor\n },\n selectionRender: defaultSelectionBuilder,\n onUpdate: defaultOnUpdate,\n }\n },\n\n onCreate() {\n if (this.options.onUpdate !== defaultOnUpdate) {\n console.warn(\n '[tiptap warn]: DEPRECATED: The \"onUpdate\" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret',\n )\n }\n if (!this.options.provider) {\n throw new Error('The \"provider\" option is required for the CollaborationCaret extension')\n }\n },\n\n addStorage() {\n return {\n users: [],\n }\n },\n\n addCommands() {\n return {\n updateUser: attributes => () => {\n this.options.user = attributes\n\n this.options.provider.awareness.setLocalStateField('user', this.options.user)\n\n return true\n },\n user:\n attributes =>\n ({ editor }) => {\n console.warn(\n '[tiptap warn]: DEPRECATED: The \"user\" command is deprecated. Please use \"updateUser\" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret',\n )\n\n return editor.commands.updateUser(attributes)\n },\n }\n },\n\n addProseMirrorPlugins() {\n return [\n yCursorPlugin(\n (() => {\n this.options.provider.awareness.setLocalStateField('user', this.options.user)\n\n this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states)\n\n this.options.provider.awareness.on('update', () => {\n this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states)\n })\n\n return this.options.provider.awareness\n })(),\n {\n cursorBuilder: this.options.render,\n selectionBuilder: this.options.selectionRender,\n },\n ),\n ]\n },\n})\n","import { CollaborationCaret } from './collaboration-caret.js'\n\nexport * from './collaboration-caret.js'\n\nexport default CollaborationCaret\n"],"mappings":";AAAA,SAAS,iBAAiB;AAE1B,SAAS,yBAAyB,qBAAqB;AAkFvD,IAAM,yBAAyB,CAAC,WAA6C;AAC3E,SAAO,MAAM,KAAK,OAAO,QAAQ,CAAC,EAAE,IAAI,CAAC,CAAC,KAAK,KAAK,MAAM;AACxD,WAAO;AAAA,MACL,UAAU;AAAA,MACV,GAAG,MAAM;AAAA,IACX;AAAA,EACF,CAAC;AACH;AAEA,IAAM,kBAAkB,MAAM;AAMvB,IAAM,qBAAqB,UAAU,OAA6D;AAAA,EACvG,MAAM;AAAA,EAEN,UAAU;AAAA,EAEV,aAAa;AACX,WAAO;AAAA,MACL,UAAU;AAAA,MACV,MAAM;AAAA,QACJ,MAAM;AAAA,QACN,OAAO;AAAA,MACT;AAAA,MACA,QAAQ,UAAQ;AACd,cAAM,SAAS,SAAS,cAAc,MAAM;AAE5C,eAAO,UAAU,IAAI,6BAA6B;AAClD,eAAO,aAAa,SAAS,iBAAiB,KAAK,KAAK,EAAE;AAE1D,cAAM,QAAQ,SAAS,cAAc,KAAK;AAE1C,cAAM,UAAU,IAAI,6BAA6B;AACjD,cAAM,aAAa,SAAS,qBAAqB,KAAK,KAAK,EAAE;AAC7D,cAAM,aAAa,SAAS,eAAe,KAAK,IAAI,GAAG,IAAI;AAC3D,eAAO,aAAa,OAAO,IAAI;AAE/B,eAAO;AAAA,MACT;AAAA,MACA,iBAAiB;AAAA,MACjB,UAAU;AAAA,IACZ;AAAA,EACF;AAAA,EAEA,WAAW;AACT,QAAI,KAAK,QAAQ,aAAa,iBAAiB;AAC7C,cAAQ;AAAA,QACN;AAAA,MACF;AAAA,IACF;AACA,QAAI,CAAC,KAAK,QAAQ,UAAU;AAC1B,YAAM,IAAI,MAAM,wEAAwE;AAAA,IAC1F;AAAA,EACF;AAAA,EAEA,aAAa;AACX,WAAO;AAAA,MACL,OAAO,CAAC;AAAA,IACV;AAAA,EACF;AAAA,EAEA,cAAc;AACZ,WAAO;AAAA,MACL,YAAY,gBAAc,MAAM;AAC9B,aAAK,QAAQ,OAAO;AAEpB,aAAK,QAAQ,SAAS,UAAU,mBAAmB,QAAQ,KAAK,QAAQ,IAAI;AAE5E,eAAO;AAAA,MACT;AAAA,MACA,MACE,gBACA,CAAC,EAAE,OAAO,MAAM;AACd,gBAAQ;AAAA,UACN;AAAA,QACF;AAEA,eAAO,OAAO,SAAS,WAAW,UAAU;AAAA,MAC9C;AAAA,IACJ;AAAA,EACF;AAAA,EAEA,wBAAwB;AACtB,WAAO;AAAA,MACL;AAAA,SACG,MAAM;AACL,eAAK,QAAQ,SAAS,UAAU,mBAAmB,QAAQ,KAAK,QAAQ,IAAI;AAE5E,eAAK,QAAQ,QAAQ,uBAAuB,KAAK,QAAQ,SAAS,UAAU,MAAM;AAElF,eAAK,QAAQ,SAAS,UAAU,GAAG,UAAU,MAAM;AACjD,iBAAK,QAAQ,QAAQ,uBAAuB,KAAK,QAAQ,SAAS,UAAU,MAAM;AAAA,UACpF,CAAC;AAED,iBAAO,KAAK,QAAQ,SAAS;AAAA,QAC/B,GAAG;AAAA,QACH;AAAA,UACE,eAAe,KAAK,QAAQ;AAAA,UAC5B,kBAAkB,KAAK,QAAQ;AAAA,QACjC;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF,CAAC;;;AC1LD,IAAO,gBAAQ;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tiptap/extension-collaboration-caret",
|
|
3
|
+
"description": "collaboration caret extension for tiptap",
|
|
4
|
+
"version": "3.0.0-next.7",
|
|
5
|
+
"homepage": "https://tiptap.dev",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tiptap",
|
|
8
|
+
"tiptap extension"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"funding": {
|
|
12
|
+
"type": "github",
|
|
13
|
+
"url": "https://github.com/sponsors/ueberdosis"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": {
|
|
19
|
+
"import": "./dist/index.d.ts",
|
|
20
|
+
"require": "./dist/index.d.cts"
|
|
21
|
+
},
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"require": "./dist/index.cjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"main": "dist/index.cjs",
|
|
27
|
+
"module": "dist/index.js",
|
|
28
|
+
"types": "dist/index.d.ts",
|
|
29
|
+
"files": [
|
|
30
|
+
"src",
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@tiptap/core": "^3.0.0-next.7",
|
|
35
|
+
"@tiptap/pm": "^3.0.0-next.7",
|
|
36
|
+
"@tiptap/y-tiptap": "^1.0.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@tiptap/core": "^3.0.0-next.3",
|
|
40
|
+
"@tiptap/pm": "^3.0.0-next.3",
|
|
41
|
+
"@tiptap/y-tiptap": "^1.0.0"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/ueberdosis/tiptap",
|
|
46
|
+
"directory": "packages/extension-collaboration-caret"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "tsup",
|
|
50
|
+
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Extension } from '@tiptap/core'
|
|
2
|
+
import type { DecorationAttrs } from '@tiptap/pm/view'
|
|
3
|
+
import { defaultSelectionBuilder, yCursorPlugin } from '@tiptap/y-tiptap'
|
|
4
|
+
|
|
5
|
+
type CollaborationCaretStorage = {
|
|
6
|
+
users: { clientId: number; [key: string]: any }[]
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface CollaborationCaretOptions {
|
|
10
|
+
/**
|
|
11
|
+
* The Hocuspocus provider instance. This can also be a TiptapCloudProvider instance.
|
|
12
|
+
* @type {HocuspocusProvider | TiptapCloudProvider}
|
|
13
|
+
* @example new HocuspocusProvider()
|
|
14
|
+
*/
|
|
15
|
+
provider: any
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The user details object – feel free to add properties to this object as needed
|
|
19
|
+
* @example { name: 'John Doe', color: '#305500' }
|
|
20
|
+
*/
|
|
21
|
+
user: Record<string, any>
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A function that returns a DOM element for the cursor.
|
|
25
|
+
* @param user The user details object
|
|
26
|
+
* @example
|
|
27
|
+
* render: user => {
|
|
28
|
+
* const cursor = document.createElement('span')
|
|
29
|
+
* cursor.classList.add('collaboration-carets__caret')
|
|
30
|
+
* cursor.setAttribute('style', `border-color: ${user.color}`)
|
|
31
|
+
*
|
|
32
|
+
* const label = document.createElement('div')
|
|
33
|
+
* label.classList.add('collaboration-carets__label')
|
|
34
|
+
* label.setAttribute('style', `background-color: ${user.color}`)
|
|
35
|
+
* label.insertBefore(document.createTextNode(user.name), null)
|
|
36
|
+
*
|
|
37
|
+
* cursor.insertBefore(label, null)
|
|
38
|
+
* return cursor
|
|
39
|
+
* }
|
|
40
|
+
*/
|
|
41
|
+
render(user: Record<string, any>): HTMLElement
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A function that returns a ProseMirror DecorationAttrs object for the selection.
|
|
45
|
+
* @param user The user details object
|
|
46
|
+
* @example
|
|
47
|
+
* selectionRender: user => {
|
|
48
|
+
* return {
|
|
49
|
+
* nodeName: 'span',
|
|
50
|
+
* class: 'collaboration-carets__selection',
|
|
51
|
+
* style: `background-color: ${user.color}`,
|
|
52
|
+
* 'data-user': user.name,
|
|
53
|
+
* }
|
|
54
|
+
*/
|
|
55
|
+
selectionRender(user: Record<string, any>): DecorationAttrs
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @deprecated The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret
|
|
59
|
+
*/
|
|
60
|
+
onUpdate: (users: { clientId: number; [key: string]: any }[]) => null
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare module '@tiptap/core' {
|
|
64
|
+
interface Commands<ReturnType> {
|
|
65
|
+
collaborationCaret: {
|
|
66
|
+
/**
|
|
67
|
+
* Update details of the current user
|
|
68
|
+
* @example editor.commands.updateUser({ name: 'John Doe', color: '#305500' })
|
|
69
|
+
*/
|
|
70
|
+
updateUser: (attributes: Record<string, any>) => ReturnType
|
|
71
|
+
/**
|
|
72
|
+
* Update details of the current user
|
|
73
|
+
*
|
|
74
|
+
* @deprecated The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret
|
|
75
|
+
*/
|
|
76
|
+
user: (attributes: Record<string, any>) => ReturnType
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface Storage {
|
|
81
|
+
collaborationCaret: CollaborationCaretStorage
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const awarenessStatesToArray = (states: Map<number, Record<string, any>>) => {
|
|
86
|
+
return Array.from(states.entries()).map(([key, value]) => {
|
|
87
|
+
return {
|
|
88
|
+
clientId: key,
|
|
89
|
+
...value.user,
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const defaultOnUpdate = () => null
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* This extension allows you to add collaboration carets to your editor.
|
|
98
|
+
* @see https://tiptap.dev/api/extensions/collaboration-caret
|
|
99
|
+
*/
|
|
100
|
+
export const CollaborationCaret = Extension.create<CollaborationCaretOptions, CollaborationCaretStorage>({
|
|
101
|
+
name: 'collaborationCaret',
|
|
102
|
+
|
|
103
|
+
priority: 999,
|
|
104
|
+
|
|
105
|
+
addOptions() {
|
|
106
|
+
return {
|
|
107
|
+
provider: null,
|
|
108
|
+
user: {
|
|
109
|
+
name: null,
|
|
110
|
+
color: null,
|
|
111
|
+
},
|
|
112
|
+
render: user => {
|
|
113
|
+
const cursor = document.createElement('span')
|
|
114
|
+
|
|
115
|
+
cursor.classList.add('collaboration-carets__caret')
|
|
116
|
+
cursor.setAttribute('style', `border-color: ${user.color}`)
|
|
117
|
+
|
|
118
|
+
const label = document.createElement('div')
|
|
119
|
+
|
|
120
|
+
label.classList.add('collaboration-carets__label')
|
|
121
|
+
label.setAttribute('style', `background-color: ${user.color}`)
|
|
122
|
+
label.insertBefore(document.createTextNode(user.name), null)
|
|
123
|
+
cursor.insertBefore(label, null)
|
|
124
|
+
|
|
125
|
+
return cursor
|
|
126
|
+
},
|
|
127
|
+
selectionRender: defaultSelectionBuilder,
|
|
128
|
+
onUpdate: defaultOnUpdate,
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
|
|
132
|
+
onCreate() {
|
|
133
|
+
if (this.options.onUpdate !== defaultOnUpdate) {
|
|
134
|
+
console.warn(
|
|
135
|
+
'[tiptap warn]: DEPRECATED: The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCaret.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret',
|
|
136
|
+
)
|
|
137
|
+
}
|
|
138
|
+
if (!this.options.provider) {
|
|
139
|
+
throw new Error('The "provider" option is required for the CollaborationCaret extension')
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
|
|
143
|
+
addStorage() {
|
|
144
|
+
return {
|
|
145
|
+
users: [],
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
addCommands() {
|
|
150
|
+
return {
|
|
151
|
+
updateUser: attributes => () => {
|
|
152
|
+
this.options.user = attributes
|
|
153
|
+
|
|
154
|
+
this.options.provider.awareness.setLocalStateField('user', this.options.user)
|
|
155
|
+
|
|
156
|
+
return true
|
|
157
|
+
},
|
|
158
|
+
user:
|
|
159
|
+
attributes =>
|
|
160
|
+
({ editor }) => {
|
|
161
|
+
console.warn(
|
|
162
|
+
'[tiptap warn]: DEPRECATED: The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-caret',
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
return editor.commands.updateUser(attributes)
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
|
|
170
|
+
addProseMirrorPlugins() {
|
|
171
|
+
return [
|
|
172
|
+
yCursorPlugin(
|
|
173
|
+
(() => {
|
|
174
|
+
this.options.provider.awareness.setLocalStateField('user', this.options.user)
|
|
175
|
+
|
|
176
|
+
this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states)
|
|
177
|
+
|
|
178
|
+
this.options.provider.awareness.on('update', () => {
|
|
179
|
+
this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
return this.options.provider.awareness
|
|
183
|
+
})(),
|
|
184
|
+
{
|
|
185
|
+
cursorBuilder: this.options.render,
|
|
186
|
+
selectionBuilder: this.options.selectionRender,
|
|
187
|
+
},
|
|
188
|
+
),
|
|
189
|
+
]
|
|
190
|
+
},
|
|
191
|
+
})
|