@tiptap/extension-character-count 2.0.0-beta.22 → 2.0.0-beta.220

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/dist/index.cjs ADDED
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var core = require('@tiptap/core');
6
+ var state = require('@tiptap/pm/state');
7
+
8
+ const CharacterCount = core.Extension.create({
9
+ name: 'characterCount',
10
+ addOptions() {
11
+ return {
12
+ limit: null,
13
+ mode: 'textSize',
14
+ };
15
+ },
16
+ addStorage() {
17
+ return {
18
+ characters: () => 0,
19
+ words: () => 0,
20
+ };
21
+ },
22
+ onBeforeCreate() {
23
+ this.storage.characters = options => {
24
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
25
+ const mode = (options === null || options === void 0 ? void 0 : options.mode) || this.options.mode;
26
+ if (mode === 'textSize') {
27
+ const text = node.textBetween(0, node.content.size, undefined, ' ');
28
+ return text.length;
29
+ }
30
+ return node.nodeSize;
31
+ };
32
+ this.storage.words = options => {
33
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
34
+ const text = node.textBetween(0, node.content.size, ' ', ' ');
35
+ const words = text.split(' ').filter(word => word !== '');
36
+ return words.length;
37
+ };
38
+ },
39
+ addProseMirrorPlugins() {
40
+ return [
41
+ new state.Plugin({
42
+ key: new state.PluginKey('characterCount'),
43
+ filterTransaction: (transaction, state) => {
44
+ const limit = this.options.limit;
45
+ // Nothing has changed or no limit is defined. Ignore it.
46
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
47
+ return true;
48
+ }
49
+ const oldSize = this.storage.characters({ node: state.doc });
50
+ const newSize = this.storage.characters({ node: transaction.doc });
51
+ // Everything is in the limit. Good.
52
+ if (newSize <= limit) {
53
+ return true;
54
+ }
55
+ // The limit has already been exceeded but will be reduced.
56
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) {
57
+ return true;
58
+ }
59
+ // The limit has already been exceeded and will be increased further.
60
+ if (oldSize > limit && newSize > limit && newSize > oldSize) {
61
+ return false;
62
+ }
63
+ const isPaste = transaction.getMeta('paste');
64
+ // Block all exceeding transactions that were not pasted.
65
+ if (!isPaste) {
66
+ return false;
67
+ }
68
+ // For pasted content, we try to remove the exceeding content.
69
+ const pos = transaction.selection.$head.pos;
70
+ const over = newSize - limit;
71
+ const from = pos - over;
72
+ const to = pos;
73
+ // It’s probably a bad idea to mutate transactions within `filterTransaction`
74
+ // but for now this is working fine.
75
+ transaction.deleteRange(from, to);
76
+ // In some situations, the limit will continue to be exceeded after trimming.
77
+ // This happens e.g. when truncating within a complex node (e.g. table)
78
+ // and ProseMirror has to close this node again.
79
+ // If this is the case, we prevent the transaction completely.
80
+ const updatedSize = this.storage.characters({ node: transaction.doc });
81
+ if (updatedSize > limit) {
82
+ return false;
83
+ }
84
+ return true;
85
+ },
86
+ }),
87
+ ];
88
+ },
89
+ });
90
+
91
+ exports.CharacterCount = CharacterCount;
92
+ exports["default"] = CharacterCount;
93
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/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?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: { node?: ProseMirrorNode }) => 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.split(' ').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":["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I,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;;;;;"}
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ import { Extension } from '@tiptap/core';
2
+ import { Plugin, PluginKey } from '@tiptap/pm/state';
3
+
4
+ const CharacterCount = Extension.create({
5
+ name: 'characterCount',
6
+ addOptions() {
7
+ return {
8
+ limit: null,
9
+ mode: 'textSize',
10
+ };
11
+ },
12
+ addStorage() {
13
+ return {
14
+ characters: () => 0,
15
+ words: () => 0,
16
+ };
17
+ },
18
+ onBeforeCreate() {
19
+ this.storage.characters = options => {
20
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
21
+ const mode = (options === null || options === void 0 ? void 0 : options.mode) || this.options.mode;
22
+ if (mode === 'textSize') {
23
+ const text = node.textBetween(0, node.content.size, undefined, ' ');
24
+ return text.length;
25
+ }
26
+ return node.nodeSize;
27
+ };
28
+ this.storage.words = options => {
29
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
30
+ const text = node.textBetween(0, node.content.size, ' ', ' ');
31
+ const words = text.split(' ').filter(word => word !== '');
32
+ return words.length;
33
+ };
34
+ },
35
+ addProseMirrorPlugins() {
36
+ return [
37
+ new Plugin({
38
+ key: new PluginKey('characterCount'),
39
+ filterTransaction: (transaction, state) => {
40
+ const limit = this.options.limit;
41
+ // Nothing has changed or no limit is defined. Ignore it.
42
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
43
+ return true;
44
+ }
45
+ const oldSize = this.storage.characters({ node: state.doc });
46
+ const newSize = this.storage.characters({ node: transaction.doc });
47
+ // Everything is in the limit. Good.
48
+ if (newSize <= limit) {
49
+ return true;
50
+ }
51
+ // The limit has already been exceeded but will be reduced.
52
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) {
53
+ return true;
54
+ }
55
+ // The limit has already been exceeded and will be increased further.
56
+ if (oldSize > limit && newSize > limit && newSize > oldSize) {
57
+ return false;
58
+ }
59
+ const isPaste = transaction.getMeta('paste');
60
+ // Block all exceeding transactions that were not pasted.
61
+ if (!isPaste) {
62
+ return false;
63
+ }
64
+ // For pasted content, we try to remove the exceeding content.
65
+ const pos = transaction.selection.$head.pos;
66
+ const over = newSize - limit;
67
+ const from = pos - over;
68
+ const to = pos;
69
+ // It’s probably a bad idea to mutate transactions within `filterTransaction`
70
+ // but for now this is working fine.
71
+ transaction.deleteRange(from, to);
72
+ // In some situations, the limit will continue to be exceeded after trimming.
73
+ // This happens e.g. when truncating within a complex node (e.g. table)
74
+ // and ProseMirror has to close this node again.
75
+ // If this is the case, we prevent the transaction completely.
76
+ const updatedSize = this.storage.characters({ node: transaction.doc });
77
+ if (updatedSize > limit) {
78
+ return false;
79
+ }
80
+ return true;
81
+ },
82
+ }),
83
+ ];
84
+ },
85
+ });
86
+
87
+ export { CharacterCount, CharacterCount as default };
88
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/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?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: { node?: ProseMirrorNode }) => 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.split(' ').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":";;;AA2Ba,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,CA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AAK,EAAE;AACvB,wBAAA,OAAO,KAAK,CAAA;AACb,qBAAA;AAED,oBAAA,OAAO,IAAI,CAAA;iBACZ;aACF,CAAC;SACH,CAAA;KACF;AACF,CAAA;;;;"}
@@ -0,0 +1,96 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tiptap/core'), require('@tiptap/pm/state')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@tiptap/core', '@tiptap/pm/state'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["@tiptap/extension-character-count"] = {}, global.core, global.state));
5
+ })(this, (function (exports, core, state) { 'use strict';
6
+
7
+ const CharacterCount = core.Extension.create({
8
+ name: 'characterCount',
9
+ addOptions() {
10
+ return {
11
+ limit: null,
12
+ mode: 'textSize',
13
+ };
14
+ },
15
+ addStorage() {
16
+ return {
17
+ characters: () => 0,
18
+ words: () => 0,
19
+ };
20
+ },
21
+ onBeforeCreate() {
22
+ this.storage.characters = options => {
23
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
24
+ const mode = (options === null || options === void 0 ? void 0 : options.mode) || this.options.mode;
25
+ if (mode === 'textSize') {
26
+ const text = node.textBetween(0, node.content.size, undefined, ' ');
27
+ return text.length;
28
+ }
29
+ return node.nodeSize;
30
+ };
31
+ this.storage.words = options => {
32
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
33
+ const text = node.textBetween(0, node.content.size, ' ', ' ');
34
+ const words = text.split(' ').filter(word => word !== '');
35
+ return words.length;
36
+ };
37
+ },
38
+ addProseMirrorPlugins() {
39
+ return [
40
+ new state.Plugin({
41
+ key: new state.PluginKey('characterCount'),
42
+ filterTransaction: (transaction, state) => {
43
+ const limit = this.options.limit;
44
+ // Nothing has changed or no limit is defined. Ignore it.
45
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
46
+ return true;
47
+ }
48
+ const oldSize = this.storage.characters({ node: state.doc });
49
+ const newSize = this.storage.characters({ node: transaction.doc });
50
+ // Everything is in the limit. Good.
51
+ if (newSize <= limit) {
52
+ return true;
53
+ }
54
+ // The limit has already been exceeded but will be reduced.
55
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) {
56
+ return true;
57
+ }
58
+ // The limit has already been exceeded and will be increased further.
59
+ if (oldSize > limit && newSize > limit && newSize > oldSize) {
60
+ return false;
61
+ }
62
+ const isPaste = transaction.getMeta('paste');
63
+ // Block all exceeding transactions that were not pasted.
64
+ if (!isPaste) {
65
+ return false;
66
+ }
67
+ // For pasted content, we try to remove the exceeding content.
68
+ const pos = transaction.selection.$head.pos;
69
+ const over = newSize - limit;
70
+ const from = pos - over;
71
+ const to = pos;
72
+ // It’s probably a bad idea to mutate transactions within `filterTransaction`
73
+ // but for now this is working fine.
74
+ transaction.deleteRange(from, to);
75
+ // In some situations, the limit will continue to be exceeded after trimming.
76
+ // This happens e.g. when truncating within a complex node (e.g. table)
77
+ // and ProseMirror has to close this node again.
78
+ // If this is the case, we prevent the transaction completely.
79
+ const updatedSize = this.storage.characters({ node: transaction.doc });
80
+ if (updatedSize > limit) {
81
+ return false;
82
+ }
83
+ return true;
84
+ },
85
+ }),
86
+ ];
87
+ },
88
+ });
89
+
90
+ exports.CharacterCount = CharacterCount;
91
+ exports["default"] = CharacterCount;
92
+
93
+ Object.defineProperty(exports, '__esModule', { value: true });
94
+
95
+ }));
96
+ //# sourceMappingURL=index.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.umd.js","sources":["../src/character-count.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\nimport { Node as ProseMirrorNode } from '@tiptap/pm/model'\nimport { Plugin, PluginKey } from '@tiptap/pm/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?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number\n\n /**\n * Get the number of words for the current document.\n */\n words: (options?: { node?: ProseMirrorNode }) => 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.split(' ').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":";;;;;;AA2Ba,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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ACZ,qBAAA;;sBAGD,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,GAAG,KAAK,IAAI,OAAO,IAAI,OAAO,EAAE;EAC5D,wBAAA,OAAO,IAAI,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;;;;;;;;;;;"}
@@ -1,10 +1,10 @@
1
1
  import { Extension } from '@tiptap/core';
2
- import { Node as ProseMirrorNode } from 'prosemirror-model';
2
+ import { Node as ProseMirrorNode } from '@tiptap/pm/model';
3
3
  export interface CharacterCountOptions {
4
4
  /**
5
5
  * The maximum number of characters that should be allowed. Defaults to `0`.
6
6
  */
7
- limit: number;
7
+ limit: number | null | undefined;
8
8
  /**
9
9
  * The mode by which the size is calculated. Defaults to 'textSize'.
10
10
  */
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.22",
4
+ "version": "2.0.0-beta.220",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -12,22 +12,37 @@
12
12
  "type": "github",
13
13
  "url": "https://github.com/sponsors/ueberdosis"
14
14
  },
15
- "main": "dist/tiptap-extension-character-count.cjs.js",
16
- "umd": "dist/tiptap-extension-character-count.umd.js",
17
- "module": "dist/tiptap-extension-character-count.esm.js",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/packages/extension-character-count/src/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "dist/index.cjs",
24
+ "module": "dist/index.js",
25
+ "umd": "dist/index.umd.js",
18
26
  "types": "dist/packages/extension-character-count/src/index.d.ts",
19
27
  "files": [
20
28
  "src",
21
29
  "dist"
22
30
  ],
23
31
  "peerDependencies": {
24
- "@tiptap/core": "^2.0.0-beta.1",
25
- "@tiptap/extension-text-style": "^2.0.0-beta.1"
32
+ "@tiptap/core": "^2.0.0-beta.209",
33
+ "@tiptap/pm": "^2.0.0-beta.209"
34
+ },
35
+ "devDependencies": {
36
+ "@tiptap/core": "^2.0.0-beta.220",
37
+ "@tiptap/pm": "^2.0.0-beta.220"
26
38
  },
27
39
  "repository": {
28
40
  "type": "git",
29
41
  "url": "https://github.com/ueberdosis/tiptap",
30
42
  "directory": "packages/extension-character-count"
31
43
  },
32
- "gitHead": "53213439b5ff1f4b6ec5f258d84cf7b8ec594b51"
44
+ "scripts": {
45
+ "clean": "rm -rf dist",
46
+ "build": "npm run clean && rollup -c"
47
+ }
33
48
  }
@@ -1,33 +1,28 @@
1
1
  import { Extension } from '@tiptap/core'
2
- import { Plugin, PluginKey } from 'prosemirror-state'
3
- import { Node as ProseMirrorNode } from 'prosemirror-model'
2
+ import { Node as ProseMirrorNode } from '@tiptap/pm/model'
3
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
4
 
5
5
  export interface CharacterCountOptions {
6
6
  /**
7
7
  * The maximum number of characters that should be allowed. Defaults to `0`.
8
8
  */
9
- limit: number,
9
+ limit: number | null | undefined
10
10
  /**
11
11
  * The mode by which the size is calculated. Defaults to 'textSize'.
12
12
  */
13
- mode: 'textSize' | 'nodeSize',
13
+ mode: 'textSize' | 'nodeSize'
14
14
  }
15
15
 
16
16
  export interface CharacterCountStorage {
17
17
  /**
18
18
  * Get the number of characters for the current document.
19
19
  */
20
- characters: (options?: {
21
- node?: ProseMirrorNode,
22
- mode?: 'textSize' | 'nodeSize',
23
- }) => number,
20
+ characters: (options?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number
24
21
 
25
22
  /**
26
23
  * Get the number of words for the current document.
27
24
  */
28
- words: (options?: {
29
- node?: ProseMirrorNode,
30
- }) => number,
25
+ words: (options?: { node?: ProseMirrorNode }) => number
31
26
  }
32
27
 
33
28
  export const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({
@@ -35,7 +30,7 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
35
30
 
36
31
  addOptions() {
37
32
  return {
38
- limit: 0,
33
+ limit: null,
39
34
  mode: 'textSize',
40
35
  }
41
36
  },
@@ -64,9 +59,7 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
64
59
  this.storage.words = options => {
65
60
  const node = options?.node || this.editor.state.doc
66
61
  const text = node.textBetween(0, node.content.size, ' ', ' ')
67
- const words = text
68
- .split(' ')
69
- .filter(word => word !== '')
62
+ const words = text.split(' ').filter(word => word !== '')
70
63
 
71
64
  return words.length
72
65
  }
@@ -80,7 +73,7 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
80
73
  const limit = this.options.limit
81
74
 
82
75
  // Nothing has changed or no limit is defined. Ignore it.
83
- if (!transaction.docChanged || limit === 0) {
76
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
84
77
  return true
85
78
  }
86
79
 
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021, überdosis GbR
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.