UNPKG

@tiptap/extension-collaboration-cursor

Version:

collaboration cursor extension for tiptap

97 lines (92 loc) 4.18 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tiptap/core'), require('y-prosemirror')) : typeof define === 'function' && define.amd ? define(['exports', '@tiptap/core', 'y-prosemirror'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@tiptap/extension-collaboration-cursor"] = {}, global.core, global.yProsemirror)); })(this, (function (exports, core, yProsemirror) { 'use strict'; const awarenessStatesToArray = (states) => { return Array.from(states.entries()).map(([key, value]) => { return { clientId: key, ...value.user, }; }); }; const defaultOnUpdate = () => null; /** * This extension allows you to add collaboration cursors to your editor. * @see https://tiptap.dev/api/extensions/collaboration-cursor */ const CollaborationCursor = core.Extension.create({ name: 'collaborationCursor', priority: 999, addOptions() { return { provider: null, user: { name: null, color: null, }, render: user => { const cursor = document.createElement('span'); cursor.classList.add('collaboration-cursor__caret'); cursor.setAttribute('style', `border-color: ${user.color}`); const label = document.createElement('div'); label.classList.add('collaboration-cursor__label'); label.setAttribute('style', `background-color: ${user.color}`); label.insertBefore(document.createTextNode(user.name), null); cursor.insertBefore(label, null); return cursor; }, selectionRender: yProsemirror.defaultSelectionBuilder, onUpdate: defaultOnUpdate, }; }, onCreate() { if (this.options.onUpdate !== defaultOnUpdate) { console.warn('[tiptap warn]: DEPRECATED: The "onUpdate" option is deprecated. Please use `editor.storage.collaborationCursor.users` instead. Read more: https://tiptap.dev/api/extensions/collaboration-cursor'); } if (!this.options.provider) { throw new Error('The "provider" option is required for the CollaborationCursor extension'); } }, addStorage() { return { users: [], }; }, addCommands() { return { updateUser: attributes => () => { this.options.user = attributes; this.options.provider.awareness.setLocalStateField('user', this.options.user); return true; }, user: attributes => ({ editor }) => { console.warn('[tiptap warn]: DEPRECATED: The "user" command is deprecated. Please use "updateUser" instead. Read more: https://tiptap.dev/api/extensions/collaboration-cursor'); return editor.commands.updateUser(attributes); }, }; }, addProseMirrorPlugins() { return [ yProsemirror.yCursorPlugin((() => { this.options.provider.awareness.setLocalStateField('user', this.options.user); this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states); this.options.provider.awareness.on('update', () => { this.storage.users = awarenessStatesToArray(this.options.provider.awareness.states); }); return this.options.provider.awareness; })(), // @ts-ignore { cursorBuilder: this.options.render, selectionBuilder: this.options.selectionRender, }), ]; }, }); exports.CollaborationCursor = CollaborationCursor; exports.default = CollaborationCursor; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=index.umd.js.map