@tiptap/extension-character-count 2.0.0-beta.26 → 2.0.0-beta.29

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.
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-extension-character-count.cjs.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Node as ProseMirrorNode } from 'prosemirror-model'\n\nexport interface CharacterCountOptions {\n /**\n * The maximum number of characters that should be allowed. Defaults to `0`.\n */\n limit: number | null | undefined,\n /**\n * The mode by which the size is calculated. Defaults to 'textSize'.\n */\n mode: 'textSize' | 'nodeSize',\n}\n\nexport interface CharacterCountStorage {\n /**\n * Get the number of characters for the current document.\n */\n characters: (options?: {\n node?: ProseMirrorNode,\n mode?: 'textSize' | 'nodeSize',\n }) => number,\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: {\n node?: ProseMirrorNode,\n }) => number,\n}\n\nexport const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({\n name: 'characterCount',\n\n addOptions() {\n return {\n limit: null,\n mode: 'textSize',\n }\n },\n\n addStorage() {\n return {\n characters: () => 0,\n words: () => 0,\n }\n },\n\n onBeforeCreate() {\n this.storage.characters = options => {\n const node = options?.node || this.editor.state.doc\n const mode = options?.mode || this.options.mode\n\n if (mode === 'textSize') {\n const text = node.textBetween(0, node.content.size, undefined, ' ')\n\n return text.length\n }\n\n return node.nodeSize\n }\n\n this.storage.words = options => {\n const node = options?.node || this.editor.state.doc\n const text = node.textBetween(0, node.content.size, ' ', ' ')\n const words = text\n .split(' ')\n .filter(word => word !== '')\n\n return words.length\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('characterCount'),\n filterTransaction: (transaction, state) => {\n const limit = this.options.limit\n\n // Nothing has changed or no limit is defined. Ignore it.\n if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {\n return true\n }\n\n const oldSize = this.storage.characters({ node: state.doc })\n const newSize = this.storage.characters({ node: transaction.doc })\n\n // Everything is in the limit. Good.\n if (newSize <= limit) {\n return true\n }\n\n // The limit has already been exceeded but will be reduced.\n if (oldSize > limit && newSize > limit && newSize <= oldSize) {\n return true\n }\n\n // The limit has already been exceeded and will be increased further.\n if (oldSize > limit && newSize > limit && newSize > oldSize) {\n return false\n }\n\n const isPaste = transaction.getMeta('paste')\n\n // Block all exceeding transactions that were not pasted.\n if (!isPaste) {\n return false\n }\n\n // For pasted content, we try to remove the exceeding content.\n const pos = transaction.selection.$head.pos\n const over = newSize - limit\n const from = pos - over\n const to = pos\n\n // It’s probably a bad idea to mutate transactions within `filterTransaction`\n // but for now this is working fine.\n transaction.deleteRange(from, to)\n\n // In some situations, the limit will continue to be exceeded after trimming.\n // This happens e.g. when truncating within a complex node (e.g. table)\n // and ProseMirror has to close this node again.\n // If this is the case, we prevent the transaction completely.\n const updatedSize = this.storage.characters({ node: transaction.doc })\n\n if (updatedSize > limit) {\n return false\n }\n\n return true\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey"],"mappings":";;;;;;;MAgCa,cAAc,GAAGA,cAAS,CAAC,MAAM,CAA+C;IAC3F,IAAI,EAAE,gBAAgB;IAEtB,UAAU;QACR,OAAO;YACL,KAAK,EAAE,IAAI;YACX,IAAI,EAAE,UAAU;SACjB,CAAA;KACF;IAED,UAAU;QACR,OAAO;YACL,UAAU,EAAE,MAAM,CAAC;YACnB,KAAK,EAAE,MAAM,CAAC;SACf,CAAA;KACF;IAED,cAAc;QACZ,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,OAAO;YAC/B,MAAM,IAAI,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;YACnD,MAAM,IAAI,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,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G,EAAE,CAAC,CAAA;oBAC5D,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAA;;oBAGlE,IAAI,OAAO,IAAI,KAAK,EAAE;wBACpB,OAAO,IAAI,CAAA;qBACZ;;oBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE;wBAC5D,OAAO,IAAI,CAAA;qBACZ;;oBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,OAAO,EAAE;wBAC3D,OAAO,KAAK,CAAA;qBACb;oBAED,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;;oBAG5C,IAAI,CAAC,OAAO,EAAE;wBACZ,OAAO,KAAK,CAAA;qBACb;;oBAGD,MAAM,GAAG,GAAG,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAA;oBAC3C,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,CAAA;oBAC5B,MAAM,IAAI,GAAG,GAAG,GAAG,IAAI,CAAA;oBACvB,MAAM,EAAE,GAAG,GAAG,CAAA;;;oBAId,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;;;;;oBAMjC,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAA;oBAEtE,IAAI,WAAW,GAAG,KAAK,EAAE;wBACvB,OAAO,KAAK,CAAA;qBACb;oBAED,OAAO,IAAI,CAAA;iBACZ;aACF,CAAC;SACH,CAAA;KACF;CACF;;;;;"}
1
+ {"version":3,"file":"tiptap-extension-character-count.cjs.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Node as ProseMirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\n\nexport interface CharacterCountOptions {\n /**\n * The maximum number of characters that should be allowed. Defaults to `0`.\n */\n limit: number | null | undefined,\n /**\n * The mode by which the size is calculated. Defaults to 'textSize'.\n */\n mode: 'textSize' | 'nodeSize',\n}\n\nexport interface CharacterCountStorage {\n /**\n * Get the number of characters for the current document.\n */\n characters: (options?: {\n node?: ProseMirrorNode,\n mode?: 'textSize' | 'nodeSize',\n }) => number,\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: {\n node?: ProseMirrorNode,\n }) => number,\n}\n\nexport const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({\n name: 'characterCount',\n\n addOptions() {\n return {\n limit: null,\n mode: 'textSize',\n }\n },\n\n addStorage() {\n return {\n characters: () => 0,\n words: () => 0,\n }\n },\n\n onBeforeCreate() {\n this.storage.characters = options => {\n const node = options?.node || this.editor.state.doc\n const mode = options?.mode || this.options.mode\n\n if (mode === 'textSize') {\n const text = node.textBetween(0, node.content.size, undefined, ' ')\n\n return text.length\n }\n\n return node.nodeSize\n }\n\n this.storage.words = options => {\n const node = options?.node || this.editor.state.doc\n const text = node.textBetween(0, node.content.size, ' ', ' ')\n const words = text\n .split(' ')\n .filter(word => word !== '')\n\n return words.length\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('characterCount'),\n filterTransaction: (transaction, state) => {\n const limit = this.options.limit\n\n // Nothing has changed or no limit is defined. Ignore it.\n if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {\n return true\n }\n\n const oldSize = this.storage.characters({ node: state.doc })\n const newSize = this.storage.characters({ node: transaction.doc })\n\n // Everything is in the limit. Good.\n if (newSize <= limit) {\n return true\n }\n\n // The limit has already been exceeded but will be reduced.\n if (oldSize > limit && newSize > limit && newSize <= oldSize) {\n return true\n }\n\n // The limit has already been exceeded and will be increased further.\n if (oldSize > limit && newSize > limit && newSize > oldSize) {\n return false\n }\n\n const isPaste = transaction.getMeta('paste')\n\n // Block all exceeding transactions that were not pasted.\n if (!isPaste) {\n return false\n }\n\n // For pasted content, we try to remove the exceeding content.\n const pos = transaction.selection.$head.pos\n const over = newSize - limit\n const from = pos - over\n const to = pos\n\n // It’s probably a bad idea to mutate transactions within `filterTransaction`\n // but for now this is working fine.\n transaction.deleteRange(from, to)\n\n // In some situations, the limit will continue to be exceeded after trimming.\n // This happens e.g. when truncating within a complex node (e.g. table)\n // and ProseMirror has to close this node again.\n // If this is the case, we prevent the transaction completely.\n const updatedSize = this.storage.characters({ node: transaction.doc })\n\n if (updatedSize > limit) {\n return false\n }\n\n return true\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey"],"mappings":";;;;;;;AAgCa,MAAA,cAAc,GAAGA,cAAS,CAAC,MAAM,CAA+C;AAC3F,IAAA,IAAI,EAAE,gBAAgB;IAEtB,UAAU,GAAA;QACR,OAAO;AACL,YAAA,KAAK,EAAE,IAAI;AACX,YAAA,IAAI,EAAE,UAAU;SACjB,CAAA;KACF;IAED,UAAU,GAAA;QACR,OAAO;AACL,YAAA,UAAU,EAAE,MAAM,CAAC;AACnB,YAAA,KAAK,EAAE,MAAM,CAAC;SACf,CAAA;KACF;IAED,cAAc,GAAA;AACZ,QAAA,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,OAAO,IAAG;AAClC,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAP,IAAA,IAAA,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;AACnD,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAA,IAAA,IAAP,OAAO,KAAP,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;YAE/C,IAAI,IAAI,KAAK,UAAU,EAAE;AACvB,gBAAA,MAAM,IAAI,GAAG,IAAI,CA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@@
1
- {"version":3,"file":"tiptap-extension-character-count.esm.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Node as ProseMirrorNode } from 'prosemirror-model'\n\nexport interface CharacterCountOptions {\n /**\n * The maximum number of characters that should be allowed. Defaults to `0`.\n */\n limit: number | null | undefined,\n /**\n * The mode by which the size is calculated. Defaults to 'textSize'.\n */\n mode: 'textSize' | 'nodeSize',\n}\n\nexport interface CharacterCountStorage {\n /**\n * Get the number of characters for the current document.\n */\n characters: (options?: {\n node?: ProseMirrorNode,\n mode?: 'textSize' | 'nodeSize',\n }) => number,\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: {\n node?: ProseMirrorNode,\n }) => number,\n}\n\nexport const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({\n name: 'characterCount',\n\n addOptions() {\n return {\n limit: null,\n mode: 'textSize',\n }\n },\n\n addStorage() {\n return {\n characters: () => 0,\n words: () => 0,\n }\n },\n\n onBeforeCreate() {\n this.storage.characters = options => {\n const node = options?.node || this.editor.state.doc\n const mode = options?.mode || this.options.mode\n\n if (mode === 'textSize') {\n const text = node.textBetween(0, node.content.size, undefined, ' ')\n\n return text.length\n }\n\n return node.nodeSize\n }\n\n this.storage.words = options => {\n const node = options?.node || this.editor.state.doc\n const text = node.textBetween(0, node.content.size, ' ', ' ')\n const words = text\n .split(' ')\n .filter(word => word !== '')\n\n return words.length\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('characterCount'),\n filterTransaction: (transaction, state) => {\n const limit = this.options.limit\n\n // Nothing has changed or no limit is defined. Ignore it.\n if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {\n return true\n }\n\n const oldSize = this.storage.characters({ node: state.doc })\n const newSize = this.storage.characters({ node: transaction.doc })\n\n // Everything is in the limit. Good.\n if (newSize <= limit) {\n return true\n }\n\n // The limit has already been exceeded but will be reduced.\n if (oldSize > limit && newSize > limit && newSize <= oldSize) {\n return true\n }\n\n // The limit has already been exceeded and will be increased further.\n if (oldSize > limit && newSize > limit && newSize > oldSize) {\n return false\n }\n\n const isPaste = transaction.getMeta('paste')\n\n // Block all exceeding transactions that were not pasted.\n if (!isPaste) {\n return false\n }\n\n // For pasted content, we try to remove the exceeding content.\n const pos = transaction.selection.$head.pos\n const over = newSize - limit\n const from = pos - over\n const to = pos\n\n // It’s probably a bad idea to mutate transactions within `filterTransaction`\n // but for now this is working fine.\n transaction.deleteRange(from, to)\n\n // In some situations, the limit will continue to be exceeded after trimming.\n // This happens e.g. when truncating within a complex node (e.g. table)\n // and ProseMirror has to close this node again.\n // If this is the case, we prevent the transaction completely.\n const updatedSize = this.storage.characters({ node: transaction.doc })\n\n if (updatedSize > limit) {\n return false\n }\n\n return true\n },\n }),\n ]\n },\n})\n"],"names":[],"mappings":";;;MAgCa,cAAc,GAAG,SAAS,CAAC,MAAM,CAA+C;IAC3F,IAAI,EAAE,gBAAgB;IAEtB,UAAU;QACR,OAAO;YACL,KAAK,EAAE,IAAI;YACX,IAAI,EAAE,UAAU;SACjB,CAAA;KACF;IAED,UAAU;QACR,OAAO;YACL,UAAU,EAAE,MAAM,CAAC;YACnB,KAAK,EAAE,MAAM,CAAC;SACf,CAAA;KACF;IAED,cAAc;QACZ,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,OAAO;YAC/B,MAAM,IAAI,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;YACnD,MAAM,IAAI,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;YAE/C,IAAI,IAAI,KAAK,UAAU,EAAE;gBACvB,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC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K,EAAE;wBACpB,OAAO,IAAI,CAAA;qBACZ;;oBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE;wBAC5D,OAAO,IAAI,CAAA;qBACZ;;oBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,OAAO,EAAE;wBAC3D,OAAO,KAAK,CAAA;qBACb;oBAED,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;;oBAG5C,IAAI,CAAC,OAAO,EAAE;wBACZ,OAAO,KAAK,CAAA;qBACb;;oBAGD,MAAM,GAAG,GAAG,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAA;oBAC3C,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,CAAA;oBAC5B,MAAM,IAAI,GAAG,GAAG,GAAG,IAAI,CAAA;oBACvB,MAAM,EAAE,GAAG,GAAG,CAAA;;;oBAId,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;;;;;oBAMjC,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAA;oBAEtE,IAAI,WAAW,GAAG,KAAK,EAAE;wBACvB,OAAO,KAAK,CAAA;qBACb;oBAED,OAAO,IAAI,CAAA;iBACZ;aACF,CAAC;SACH,CAAA;KACF;CACF;;;;"}
1
+ {"version":3,"file":"tiptap-extension-character-count.esm.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Node as ProseMirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\n\nexport interface CharacterCountOptions {\n /**\n * The maximum number of characters that should be allowed. Defaults to `0`.\n */\n limit: number | null | undefined,\n /**\n * The mode by which the size is calculated. Defaults to 'textSize'.\n */\n mode: 'textSize' | 'nodeSize',\n}\n\nexport interface CharacterCountStorage {\n /**\n * Get the number of characters for the current document.\n */\n characters: (options?: {\n node?: ProseMirrorNode,\n mode?: 'textSize' | 'nodeSize',\n }) => number,\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: {\n node?: ProseMirrorNode,\n }) => number,\n}\n\nexport const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({\n name: 'characterCount',\n\n addOptions() {\n return {\n limit: null,\n mode: 'textSize',\n }\n },\n\n addStorage() {\n return {\n characters: () => 0,\n words: () => 0,\n }\n },\n\n onBeforeCreate() {\n this.storage.characters = options => {\n const node = options?.node || this.editor.state.doc\n const mode = options?.mode || this.options.mode\n\n if (mode === 'textSize') {\n const text = node.textBetween(0, node.content.size, undefined, ' ')\n\n return text.length\n }\n\n return node.nodeSize\n }\n\n this.storage.words = options => {\n const node = options?.node || this.editor.state.doc\n const text = node.textBetween(0, node.content.size, ' ', ' ')\n const words = text\n .split(' ')\n .filter(word => word !== '')\n\n return words.length\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('characterCount'),\n filterTransaction: (transaction, state) => {\n const limit = this.options.limit\n\n // Nothing has changed or no limit is defined. Ignore it.\n if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {\n return true\n }\n\n const oldSize = this.storage.characters({ node: state.doc })\n const newSize = this.storage.characters({ node: transaction.doc })\n\n // Everything is in the limit. Good.\n if (newSize <= limit) {\n return true\n }\n\n // The limit has already been exceeded but will be reduced.\n if (oldSize > limit && newSize > limit && newSize <= oldSize) {\n return true\n }\n\n // The limit has already been exceeded and will be increased further.\n if (oldSize > limit && newSize > limit && newSize > oldSize) {\n return false\n }\n\n const isPaste = transaction.getMeta('paste')\n\n // Block all exceeding transactions that were not pasted.\n if (!isPaste) {\n return false\n }\n\n // For pasted content, we try to remove the exceeding content.\n const pos = transaction.selection.$head.pos\n const over = newSize - limit\n const from = pos - over\n const to = pos\n\n // It’s probably a bad idea to mutate transactions within `filterTransaction`\n // but for now this is working fine.\n transaction.deleteRange(from, to)\n\n // In some situations, the limit will continue to be exceeded after trimming.\n // This happens e.g. when truncating within a complex node (e.g. table)\n // and ProseMirror has to close this node again.\n // If this is the case, we prevent the transaction completely.\n const updatedSize = this.storage.characters({ node: transaction.doc })\n\n if (updatedSize > limit) {\n return false\n }\n\n return true\n },\n }),\n ]\n },\n})\n"],"names":[],"mappings":";;;AAgCa,MAAA,cAAc,GAAG,SAAS,CAAC,MAAM,CAA+C;AAC3F,IAAA,IAAI,EAAE,gBAAgB;IAEtB,UAAU,GAAA;QACR,OAAO;AACL,YAAA,KAAK,EAAE,IAAI;AACX,YAAA,IAAI,EAAE,UAAU;SACjB,CAAA;KACF;IAED,UAAU,GAAA;QACR,OAAO;AACL,YAAA,UAAU,EAAE,MAAM,CAAC;AACnB,YAAA,KAAK,EAAE,MAAM,CAAC;SACf,CAAA;KACF;IAED,cAAc,GAAA;AACZ,QAAA,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,OAAO,IAAG;AAClC,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAP,IAAA,IAAA,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;AACnD,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAA,IAAA,IAAP,OAAO,KAAP,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;YAE/C,IAAI,IAAI,KAAK,UAAU,EAAE;AACvB,gBAAA,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,CAAA;gBAEnE,OAAO,IAAI,CAAC,MAAM,CAAA;AACnB,aAAA;YAED,OAAO,IAAI,CAAC,QAAQ,CAAA;AACtB,SAAC,CA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AO,IAAI,CAAA;AACZ,qBAAA;;oBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE;AAC5D,wBAAA,OAAO,IAAI,CAAA;AACZ,qBAAA;;oBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,OAAO,EAAE;AAC3D,wBAAA,OAAO,KAAK,CAAA;AACb,qBAAA;oBAED,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;;oBAG5C,IAAI,CAAC,OAAO,EAAE;AACZ,wBAAA,OAAO,KAAK,CAAA;AACb,qBAAA;;oBAGD,MAAM,GAAG,GAAG,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAA;AAC3C,oBAAA,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,CAAA;AAC5B,oBAAA,MAAM,IAAI,GAAG,GAAG,GAAG,IAAI,CAAA;oBACvB,MAAM,EAAE,GAAG,GAAG,CAAA;;;AAId,oBAAA,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;;;;;AAMjC,oBAAA,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAA;oBAEtE,IAAI,WAAW,GAAG,KAAK,EAAE;AACvB,wBAAA,OAAO,KAAK,CAAA;AACb,qBAAA;AAED,oBAAA,OAAO,IAAI,CAAA;iBACZ;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"tiptap-extension-character-count.umd.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Plugin, PluginKey } from 'prosemirror-state'\nimport { Node as ProseMirrorNode } from 'prosemirror-model'\n\nexport interface CharacterCountOptions {\n /**\n * The maximum number of characters that should be allowed. Defaults to `0`.\n */\n limit: number | null | undefined,\n /**\n * The mode by which the size is calculated. Defaults to 'textSize'.\n */\n mode: 'textSize' | 'nodeSize',\n}\n\nexport interface CharacterCountStorage {\n /**\n * Get the number of characters for the current document.\n */\n characters: (options?: {\n node?: ProseMirrorNode,\n mode?: 'textSize' | 'nodeSize',\n }) => number,\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: {\n node?: ProseMirrorNode,\n }) => number,\n}\n\nexport const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({\n name: 'characterCount',\n\n addOptions() {\n return {\n limit: null,\n mode: 'textSize',\n }\n },\n\n addStorage() {\n return {\n characters: () => 0,\n words: () => 0,\n }\n },\n\n onBeforeCreate() {\n this.storage.characters = options => {\n const node = options?.node || this.editor.state.doc\n const mode = options?.mode || this.options.mode\n\n if (mode === 'textSize') {\n const text = node.textBetween(0, node.content.size, undefined, ' ')\n\n return text.length\n }\n\n return node.nodeSize\n }\n\n this.storage.words = options => {\n const node = options?.node || this.editor.state.doc\n const text = node.textBetween(0, node.content.size, ' ', ' ')\n const words = text\n .split(' ')\n .filter(word => word !== '')\n\n return words.length\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('characterCount'),\n filterTransaction: (transaction, state) => {\n const limit = this.options.limit\n\n // Nothing has changed or no limit is defined. Ignore it.\n if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {\n return true\n }\n\n const oldSize = this.storage.characters({ node: state.doc })\n const newSize = this.storage.characters({ node: transaction.doc })\n\n // Everything is in the limit. Good.\n if (newSize <= limit) {\n return true\n }\n\n // The limit has already been exceeded but will be reduced.\n if (oldSize > limit && newSize > limit && newSize <= oldSize) {\n return true\n }\n\n // The limit has already been exceeded and will be increased further.\n if (oldSize > limit && newSize > limit && newSize > oldSize) {\n return false\n }\n\n const isPaste = transaction.getMeta('paste')\n\n // Block all exceeding transactions that were not pasted.\n if (!isPaste) {\n return false\n }\n\n // For pasted content, we try to remove the exceeding content.\n const pos = transaction.selection.$head.pos\n const over = newSize - limit\n const from = pos - over\n const to = pos\n\n // It’s probably a bad idea to mutate transactions within `filterTransaction`\n // but for now this is working fine.\n transaction.deleteRange(from, to)\n\n // In some situations, the limit will continue to be exceeded after trimming.\n // This happens e.g. when truncating within a complex node (e.g. table)\n // and ProseMirror has to close this node again.\n // If this is the case, we prevent the transaction completely.\n const updatedSize = this.storage.characters({ node: transaction.doc })\n\n if (updatedSize > limit) {\n return false\n }\n\n return true\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey"],"mappings":";;;;;;QAgCa,cAAc,GAAGA,cAAS,CAAC,MAAM,CAA+C;MAC3F,IAAI,EAAE,gBAAgB;MAEtB,UAAU;UACR,OAAO;cACL,KAAK,EAAE,IAAI;cACX,IAAI,EAAE,UAAU;WACjB,CAAA;OACF;MAED,UAAU;UACR,OAAO;cACL,UAAU,EAAE,MAAM,CAAC;cACnB,KAAK,EAAE,MAAM,CAAC;WACf,CAAA;OACF;MAED,cAAc;UACZ,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,OAAO;cAC/B,MAAM,IAAI,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;cACnD,MAAM,IAAI,GAAG,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;cAE/C,IAAI,IAAI,KAAK,UAAU,EAAE;kBACvB,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,CAAA;kBAEnE,OAAO,IAAI,CAAC,MAAM,CAAA;eACnB;cAED,OAAO,IAAI,CAAC,QAAQ,CAAA;WACrB,CAAA;UAED,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,O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I,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,OAAO,EAAE;0BAC3D,OAAO,KAAK,CAAA;uBACb;sBAED,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;;sBAG5C,IAAI,CAAC,OAAO,EAAE;0BACZ,OAAO,KAAK,CAAA;uBACb;;sBAGD,MAAM,GAAG,GAAG,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAA;sBAC3C,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,CAAA;sBAC5B,MAAM,IAAI,GAAG,GAAG,GAAG,IAAI,CAAA;sBACvB,MAAM,EAAE,GAAG,GAAG,CAAA;;;sBAId,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;;;;;sBAMjC,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAA;sBAEtE,IAAI,WAAW,GAAG,KAAK,EAAE;0BACvB,OAAO,KAAK,CAAA;uBACb;sBAED,OAAO,IAAI,CAAA;mBACZ;eACF,CAAC;WACH,CAAA;OACF;GACF;;;;;;;;;;;"}
1
+ {"version":3,"file":"tiptap-extension-character-count.umd.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Node as ProseMirrorNode } from 'prosemirror-model'\nimport { Plugin, PluginKey } from 'prosemirror-state'\n\nexport interface CharacterCountOptions {\n /**\n * The maximum number of characters that should be allowed. Defaults to `0`.\n */\n limit: number | null | undefined,\n /**\n * The mode by which the size is calculated. Defaults to 'textSize'.\n */\n mode: 'textSize' | 'nodeSize',\n}\n\nexport interface CharacterCountStorage {\n /**\n * Get the number of characters for the current document.\n */\n characters: (options?: {\n node?: ProseMirrorNode,\n mode?: 'textSize' | 'nodeSize',\n }) => number,\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: {\n node?: ProseMirrorNode,\n }) => number,\n}\n\nexport const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({\n name: 'characterCount',\n\n addOptions() {\n return {\n limit: null,\n mode: 'textSize',\n }\n },\n\n addStorage() {\n return {\n characters: () => 0,\n words: () => 0,\n }\n },\n\n onBeforeCreate() {\n this.storage.characters = options => {\n const node = options?.node || this.editor.state.doc\n const mode = options?.mode || this.options.mode\n\n if (mode === 'textSize') {\n const text = node.textBetween(0, node.content.size, undefined, ' ')\n\n return text.length\n }\n\n return node.nodeSize\n }\n\n this.storage.words = options => {\n const node = options?.node || this.editor.state.doc\n const text = node.textBetween(0, node.content.size, ' ', ' ')\n const words = text\n .split(' ')\n .filter(word => word !== '')\n\n return words.length\n }\n },\n\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: new PluginKey('characterCount'),\n filterTransaction: (transaction, state) => {\n const limit = this.options.limit\n\n // Nothing has changed or no limit is defined. Ignore it.\n if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {\n return true\n }\n\n const oldSize = this.storage.characters({ node: state.doc })\n const newSize = this.storage.characters({ node: transaction.doc })\n\n // Everything is in the limit. Good.\n if (newSize <= limit) {\n return true\n }\n\n // The limit has already been exceeded but will be reduced.\n if (oldSize > limit && newSize > limit && newSize <= oldSize) {\n return true\n }\n\n // The limit has already been exceeded and will be increased further.\n if (oldSize > limit && newSize > limit && newSize > oldSize) {\n return false\n }\n\n const isPaste = transaction.getMeta('paste')\n\n // Block all exceeding transactions that were not pasted.\n if (!isPaste) {\n return false\n }\n\n // For pasted content, we try to remove the exceeding content.\n const pos = transaction.selection.$head.pos\n const over = newSize - limit\n const from = pos - over\n const to = pos\n\n // It’s probably a bad idea to mutate transactions within `filterTransaction`\n // but for now this is working fine.\n transaction.deleteRange(from, to)\n\n // In some situations, the limit will continue to be exceeded after trimming.\n // This happens e.g. when truncating within a complex node (e.g. table)\n // and ProseMirror has to close this node again.\n // If this is the case, we prevent the transaction completely.\n const updatedSize = this.storage.characters({ node: transaction.doc })\n\n if (updatedSize > limit) {\n return false\n }\n\n return true\n },\n }),\n ]\n },\n})\n"],"names":["Extension","Plugin","PluginKey"],"mappings":";;;;;;AAgCa,QAAA,cAAc,GAAGA,cAAS,CAAC,MAAM,CAA+C;EAC3F,IAAA,IAAI,EAAE,gBAAgB;MAEtB,UAAU,GAAA;UACR,OAAO;EACL,YAAA,KAAK,EAAE,IAAI;EACX,YAAA,IAAI,EAAE,UAAU;WACjB,CAAA;OACF;MAED,UAAU,GAAA;UACR,OAAO;EACL,YAAA,UAAU,EAAE,MAAM,CAAC;EACnB,YAAA,KAAK,EAAE,MAAM,CAAC;WACf,CAAA;OACF;MAED,cAAc,GAAA;EACZ,QAAA,IAAI,CAAC,OAAO,CAAC,UAAU,GAAG,OAAO,IAAG;EAClC,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAP,IAAA,IAAA,OAAO,uBAAP,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAA;EACnD,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAA,IAAA,IAAP,OAAO,KAAP,KAAA,CAAA,GAAA,KAAA,CAAA,GAAA,OAAO,CAAE,IAAI,KAAI,IAAI,CAAC,OAAO,CAAC,IAAI,CAAA;cAE/C,IAAI,IAAI,KAAK,UAAU,EAAE;EACvB,gBAAA,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,GAAG,CAAC,CAAA;kBAEnE,OAAO,IAAI,CAAC,MAAM,CAAA;EACnB,aAAA;cAED,OAAO,IAAI,CAAC,QAAQ,CAAA;EACtB,SAAC,CAAA;EAED,QAAA,IAAI,CAAC,OAAO,CAAC,KAAK,GAAG,OAAO,IAAG;EAC7B,YAAA,MAAM,IAAI,GAAG,CAAA,OAAO,KAAP,IAAA,IAAA,OAAO,uBAAP,OAAO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AAI,CAAA;EACZ,qBAAA;;sBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,OAAO,EAAE;EAC3D,wBAAA,OAAO,KAAK,CAAA;EACb,qBAAA;sBAED,MAAM,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;;sBAG5C,IAAI,CAAC,OAAO,EAAE;EACZ,wBAAA,OAAO,KAAK,CAAA;EACb,qBAAA;;sBAGD,MAAM,GAAG,GAAG,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAA;EAC3C,oBAAA,MAAM,IAAI,GAAG,OAAO,GAAG,KAAK,CAAA;EAC5B,oBAAA,MAAM,IAAI,GAAG,GAAG,GAAG,IAAI,CAAA;sBACvB,MAAM,EAAE,GAAG,GAAG,CAAA;;;EAId,oBAAA,WAAW,CAAC,WAAW,CAAC,IAAI,EAAE,EAAE,CAAC,CAAA;;;;;EAMjC,oBAAA,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,GAAG,EAAE,CAAC,CAAA;sBAEtE,IAAI,WAAW,GAAG,KAAK,EAAE;EACvB,wBAAA,OAAO,KAAK,CAAA;EACb,qBAAA;EAED,oBAAA,OAAO,IAAI,CAAA;mBACZ;eACF,CAAC;WACH,CAAA;OACF;EACF,CAAA;;;;;;;;;;;"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-character-count",
3
3
  "description": "font family extension for tiptap",
4
- "version": "2.0.0-beta.26",
4
+ "version": "2.0.0-beta.29",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -32,5 +32,5 @@
32
32
  "url": "https://github.com/ueberdosis/tiptap",
33
33
  "directory": "packages/extension-character-count"
34
34
  },
35
- "gitHead": "90e719c711dbccae6a7333956d543fb93789f3d3"
35
+ "gitHead": "591c0807a2ab5c34b4b7fe12c12511fe4f493ebd"
36
36
  }
@@ -1,6 +1,6 @@
1
1
  import { Extension } from '@tiptap/core'
2
- import { Plugin, PluginKey } from 'prosemirror-state'
3
2
  import { Node as ProseMirrorNode } from 'prosemirror-model'
3
+ import { Plugin, PluginKey } from 'prosemirror-state'
4
4
 
5
5
  export interface CharacterCountOptions {
6
6
  /**