UNPKG

@tiptap/extension-collaboration

Version:

collaboration extension for tiptap

178 lines (174 loc) 6.02 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { Collaboration: () => Collaboration, default: () => index_default, isChangeOrigin: () => isChangeOrigin }); module.exports = __toCommonJS(index_exports); // src/collaboration.ts var import_core = require("@tiptap/core"); var import_state = require("@tiptap/pm/state"); var import_y_tiptap = require("@tiptap/y-tiptap"); var Collaboration = import_core.Extension.create({ name: "collaboration", priority: 1e3, addOptions() { return { document: null, field: "default", fragment: null, provider: null }; }, addStorage() { return { isDisabled: false }; }, onCreate() { if (this.editor.extensionManager.extensions.find((extension) => extension.name === "undoRedo")) { console.warn( '[tiptap warn]: "@tiptap/extension-collaboration" comes with its own history support and is not compatible with "@tiptap/extension-undo-redo".' ); } }, addCommands() { return { undo: () => ({ tr, state, dispatch }) => { tr.setMeta("preventDispatch", true); const undoManager = import_y_tiptap.yUndoPluginKey.getState(state).undoManager; if (undoManager.undoStack.length === 0) { return false; } if (!dispatch) { return true; } return (0, import_y_tiptap.undo)(state); }, redo: () => ({ tr, state, dispatch }) => { tr.setMeta("preventDispatch", true); const undoManager = import_y_tiptap.yUndoPluginKey.getState(state).undoManager; if (undoManager.redoStack.length === 0) { return false; } if (!dispatch) { return true; } return (0, import_y_tiptap.redo)(state); } }; }, addKeyboardShortcuts() { return { "Mod-z": () => this.editor.commands.undo(), "Mod-y": () => this.editor.commands.redo(), "Shift-Mod-z": () => this.editor.commands.redo() }; }, addProseMirrorPlugins() { var _a; const fragment = this.options.fragment ? this.options.fragment : this.options.document.getXmlFragment(this.options.field); const yUndoPluginInstance = (0, import_y_tiptap.yUndoPlugin)(this.options.yUndoOptions); const originalUndoPluginView = yUndoPluginInstance.spec.view; yUndoPluginInstance.spec.view = (view) => { const { undoManager } = import_y_tiptap.yUndoPluginKey.getState(view.state); if (undoManager.restore) { undoManager.restore(); undoManager.restore = () => { }; } const viewRet = originalUndoPluginView ? originalUndoPluginView(view) : void 0; return { destroy: () => { const hasUndoManSelf = undoManager.trackedOrigins.has(undoManager); const observers = undoManager._observers; undoManager.restore = () => { if (hasUndoManSelf) { undoManager.trackedOrigins.add(undoManager); } undoManager.doc.on("afterTransaction", undoManager.afterTransactionHandler); undoManager._observers = observers; }; if (viewRet == null ? void 0 : viewRet.destroy) { viewRet.destroy(); } } }; }; const ySyncPluginOptions = { ...this.options.ySyncOptions, onFirstRender: this.options.onFirstRender }; const ySyncPluginInstance = (0, import_y_tiptap.ySyncPlugin)(fragment, ySyncPluginOptions); if (this.editor.options.enableContentCheck) { (_a = fragment.doc) == null ? void 0 : _a.on("beforeTransaction", () => { try { const jsonContent = (0, import_y_tiptap.yXmlFragmentToProsemirrorJSON)(fragment); if (jsonContent.content.length === 0) { return; } this.editor.schema.nodeFromJSON(jsonContent).check(); } catch (error) { this.editor.emit("contentError", { error, editor: this.editor, disableCollaboration: () => { var _a2; (_a2 = fragment.doc) == null ? void 0 : _a2.destroy(); this.storage.isDisabled = true; } }); return false; } }); } return [ ySyncPluginInstance, yUndoPluginInstance, // Only add the filterInvalidContent plugin if content checking is enabled this.editor.options.enableContentCheck && new import_state.Plugin({ key: new import_state.PluginKey("filterInvalidContent"), filterTransaction: () => { var _a2; if (this.storage.isDisabled !== false) { (_a2 = fragment.doc) == null ? void 0 : _a2.destroy(); return true; } return true; } }) ].filter(Boolean); } }); // src/helpers/isChangeOrigin.ts var import_y_tiptap2 = require("@tiptap/y-tiptap"); function isChangeOrigin(transaction) { return !!transaction.getMeta(import_y_tiptap2.ySyncPluginKey); } // src/index.ts var index_default = Collaboration; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Collaboration, isChangeOrigin }); //# sourceMappingURL=index.cjs.map