import { Extension } from "@tiptap/core"; import { Plugin, PluginKey } from "@tiptap/pm/state"; //#region src/character-count/character-count.ts /** * This extension allows you to count the characters and words of your document. * @see https://tiptap.dev/api/extensions/character-count */ const CharacterCount = Extension.create({ name: "characterCount", addOptions() { return { limit: null, autoTrim: true, mode: "textSize", textCounter: (text) => text.length, wordCounter: (text) => text.split(" ").filter((word) => word !== "").length }; }, addStorage() { return { characters: () => 0, words: () => 0 }; }, onBeforeCreate() { this.storage.characters = (options) => { const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc; if (((options === null || options === void 0 ? void 0 : options.mode) || this.options.mode) === "textSize") { const text = node.textBetween(0, node.content.size, void 0, " "); return this.options.textCounter(text); } return node.nodeSize; }; this.storage.words = (options) => { const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc; const text = node.textBetween(0, node.content.size, " ", " "); return this.options.wordCounter(text); }; }, addProseMirrorPlugins() { let initialEvaluationDone = false; return [new Plugin({ key: new PluginKey("characterCount"), appendTransaction: (transactions, oldState, newState) => { if (initialEvaluationDone) return; const limit = this.options.limit; const autoTrim = this.options.autoTrim; if (limit === null || limit === void 0 || limit === 0 || autoTrim === false) { initialEvaluationDone = true; return; } const initialContentSize = this.storage.characters({ node: newState.doc }); if (initialContentSize > limit) { const over = initialContentSize - limit; const from = 0; const to = over; console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`); const tr = newState.tr.deleteRange(from, to); initialEvaluationDone = true; return tr; } initialEvaluationDone = true; }, filterTransaction: (transaction, state) => { const limit = this.options.limit; if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) return true; const oldSize = this.storage.characters({ node: state.doc }); const newSize = this.storage.characters({ node: transaction.doc }); if (newSize <= limit) return true; if (oldSize > limit && newSize > limit && newSize <= oldSize) return true; if (oldSize > limit && newSize > limit && newSize > oldSize) return false; if (!transaction.getMeta("paste")) return false; const pos = transaction.selection.$head.pos; const from = pos - (newSize - limit); const to = pos; transaction.deleteRange(from, to); if (this.storage.characters({ node: transaction.doc }) > limit) return false; return true; } })]; } }); //#endregion export { CharacterCount }; //# sourceMappingURL=index.js.map