@tiptap/extension-character-count 2.0.0-beta.9 → 2.0.0-rc.2
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/README.md +4 -4
- package/dist/index.cjs +93 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +88 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +96 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/packages/extension-character-count/src/character-count.d.ts +25 -4
- package/package.json +25 -12
- package/src/character-count.ts +109 -14
- package/CHANGELOG.md +0 -126
- package/LICENSE.md +0 -21
- package/dist/tiptap-extension-character-count.cjs.js +0 -5792
- package/dist/tiptap-extension-character-count.cjs.js.map +0 -1
- package/dist/tiptap-extension-character-count.esm.js +0 -5787
- package/dist/tiptap-extension-character-count.esm.js.map +0 -1
- package/dist/tiptap-extension-character-count.umd.js +0 -5796
- package/dist/tiptap-extension-character-count.umd.js.map +0 -1
package/README.md
CHANGED
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
[](https://github.com/sponsors/ueberdosis)
|
|
6
6
|
|
|
7
7
|
## Introduction
|
|
8
|
-
|
|
8
|
+
Tiptap is a headless wrapper around [ProseMirror](https://ProseMirror.net) – a toolkit for building rich text WYSIWYG editors, which is already in use at many well-known companies such as *New York Times*, *The Guardian* or *Atlassian*.
|
|
9
9
|
|
|
10
|
-
##
|
|
11
|
-
Documentation can be found on the [
|
|
10
|
+
## Official Documentation
|
|
11
|
+
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
|
12
12
|
|
|
13
13
|
## License
|
|
14
|
-
|
|
14
|
+
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
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":["Extension","Plugin","PluginKey"],"mappings":";;;;;;;AA2Ba,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,CAAC,WAAW,CAAC,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@ -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,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,CAA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
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,OAA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AI,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,7 +1,28 @@
|
|
|
1
1
|
import { Extension } from '@tiptap/core';
|
|
2
|
-
import {
|
|
3
|
-
export declare const pluginKey: PluginKey<any, any>;
|
|
2
|
+
import { Node as ProseMirrorNode } from '@tiptap/pm/model';
|
|
4
3
|
export interface CharacterCountOptions {
|
|
5
|
-
|
|
4
|
+
/**
|
|
5
|
+
* The maximum number of characters that should be allowed. Defaults to `0`.
|
|
6
|
+
*/
|
|
7
|
+
limit: number | null | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* The mode by which the size is calculated. Defaults to 'textSize'.
|
|
10
|
+
*/
|
|
11
|
+
mode: 'textSize' | 'nodeSize';
|
|
6
12
|
}
|
|
7
|
-
export
|
|
13
|
+
export interface CharacterCountStorage {
|
|
14
|
+
/**
|
|
15
|
+
* Get the number of characters for the current document.
|
|
16
|
+
*/
|
|
17
|
+
characters: (options?: {
|
|
18
|
+
node?: ProseMirrorNode;
|
|
19
|
+
mode?: 'textSize' | 'nodeSize';
|
|
20
|
+
}) => number;
|
|
21
|
+
/**
|
|
22
|
+
* Get the number of words for the current document.
|
|
23
|
+
*/
|
|
24
|
+
words: (options?: {
|
|
25
|
+
node?: ProseMirrorNode;
|
|
26
|
+
}) => number;
|
|
27
|
+
}
|
|
28
|
+
export declare const CharacterCount: Extension<CharacterCountOptions, CharacterCountStorage>;
|
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-
|
|
4
|
+
"version": "2.0.0-rc.2",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -12,24 +12,37 @@
|
|
|
12
12
|
"type": "github",
|
|
13
13
|
"url": "https://github.com/sponsors/ueberdosis"
|
|
14
14
|
},
|
|
15
|
-
"
|
|
16
|
-
"umd": "dist/tiptap-extension-character-count.umd.js",
|
|
17
|
-
"module": "dist/tiptap-extension-character-count.esm.js",
|
|
18
|
-
"types": "dist/packages/extension-character-count/src/index.d.ts",
|
|
15
|
+
"type": "module",
|
|
19
16
|
"exports": {
|
|
20
17
|
".": {
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
"
|
|
18
|
+
"types": "./dist/packages/extension-character-count/src/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js",
|
|
20
|
+
"require": "./dist/index.cjs"
|
|
24
21
|
}
|
|
25
22
|
},
|
|
23
|
+
"main": "dist/index.cjs",
|
|
24
|
+
"module": "dist/index.js",
|
|
25
|
+
"umd": "dist/index.umd.js",
|
|
26
|
+
"types": "dist/packages/extension-character-count/src/index.d.ts",
|
|
26
27
|
"files": [
|
|
27
28
|
"src",
|
|
28
29
|
"dist"
|
|
29
30
|
],
|
|
30
31
|
"peerDependencies": {
|
|
31
|
-
"@tiptap/core": "^2.0.0-
|
|
32
|
-
"@tiptap/
|
|
32
|
+
"@tiptap/core": "^2.0.0-rc.1",
|
|
33
|
+
"@tiptap/pm": "^2.0.0-rc.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@tiptap/core": "^2.0.0-rc.1",
|
|
37
|
+
"@tiptap/pm": "^2.0.0-rc.1"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/ueberdosis/tiptap",
|
|
42
|
+
"directory": "packages/extension-character-count"
|
|
33
43
|
},
|
|
34
|
-
"
|
|
35
|
-
|
|
44
|
+
"scripts": {
|
|
45
|
+
"clean": "rm -rf dist",
|
|
46
|
+
"build": "npm run clean && rollup -c"
|
|
47
|
+
}
|
|
48
|
+
}
|
package/src/character-count.ts
CHANGED
|
@@ -1,33 +1,128 @@
|
|
|
1
1
|
import { Extension } from '@tiptap/core'
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
export const pluginKey = new PluginKey('characterLimit')
|
|
2
|
+
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
3
|
+
import { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
5
4
|
|
|
6
5
|
export interface CharacterCountOptions {
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* The maximum number of characters that should be allowed. Defaults to `0`.
|
|
8
|
+
*/
|
|
9
|
+
limit: number | null | undefined
|
|
10
|
+
/**
|
|
11
|
+
* The mode by which the size is calculated. Defaults to 'textSize'.
|
|
12
|
+
*/
|
|
13
|
+
mode: 'textSize' | 'nodeSize'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface CharacterCountStorage {
|
|
17
|
+
/**
|
|
18
|
+
* Get the number of characters for the current document.
|
|
19
|
+
*/
|
|
20
|
+
characters: (options?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Get the number of words for the current document.
|
|
24
|
+
*/
|
|
25
|
+
words: (options?: { node?: ProseMirrorNode }) => number
|
|
8
26
|
}
|
|
9
27
|
|
|
10
|
-
export const CharacterCount = Extension.create<CharacterCountOptions>({
|
|
28
|
+
export const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({
|
|
11
29
|
name: 'characterCount',
|
|
12
30
|
|
|
13
|
-
|
|
14
|
-
|
|
31
|
+
addOptions() {
|
|
32
|
+
return {
|
|
33
|
+
limit: null,
|
|
34
|
+
mode: 'textSize',
|
|
35
|
+
}
|
|
15
36
|
},
|
|
16
37
|
|
|
17
|
-
|
|
18
|
-
|
|
38
|
+
addStorage() {
|
|
39
|
+
return {
|
|
40
|
+
characters: () => 0,
|
|
41
|
+
words: () => 0,
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
onBeforeCreate() {
|
|
46
|
+
this.storage.characters = options => {
|
|
47
|
+
const node = options?.node || this.editor.state.doc
|
|
48
|
+
const mode = options?.mode || this.options.mode
|
|
19
49
|
|
|
50
|
+
if (mode === 'textSize') {
|
|
51
|
+
const text = node.textBetween(0, node.content.size, undefined, ' ')
|
|
52
|
+
|
|
53
|
+
return text.length
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return node.nodeSize
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.storage.words = options => {
|
|
60
|
+
const node = options?.node || this.editor.state.doc
|
|
61
|
+
const text = node.textBetween(0, node.content.size, ' ', ' ')
|
|
62
|
+
const words = text.split(' ').filter(word => word !== '')
|
|
63
|
+
|
|
64
|
+
return words.length
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
addProseMirrorPlugins() {
|
|
20
69
|
return [
|
|
21
70
|
new Plugin({
|
|
71
|
+
key: new PluginKey('characterCount'),
|
|
72
|
+
filterTransaction: (transaction, state) => {
|
|
73
|
+
const limit = this.options.limit
|
|
22
74
|
|
|
23
|
-
|
|
75
|
+
// Nothing has changed or no limit is defined. Ignore it.
|
|
76
|
+
if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
|
|
77
|
+
return true
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const oldSize = this.storage.characters({ node: state.doc })
|
|
81
|
+
const newSize = this.storage.characters({ node: transaction.doc })
|
|
82
|
+
|
|
83
|
+
// Everything is in the limit. Good.
|
|
84
|
+
if (newSize <= limit) {
|
|
85
|
+
return true
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// The limit has already been exceeded but will be reduced.
|
|
89
|
+
if (oldSize > limit && newSize > limit && newSize <= oldSize) {
|
|
90
|
+
return true
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// The limit has already been exceeded and will be increased further.
|
|
94
|
+
if (oldSize > limit && newSize > limit && newSize > oldSize) {
|
|
95
|
+
return false
|
|
96
|
+
}
|
|
24
97
|
|
|
25
|
-
|
|
26
|
-
const length = newState.doc.content.size
|
|
98
|
+
const isPaste = transaction.getMeta('paste')
|
|
27
99
|
|
|
28
|
-
|
|
29
|
-
|
|
100
|
+
// Block all exceeding transactions that were not pasted.
|
|
101
|
+
if (!isPaste) {
|
|
102
|
+
return false
|
|
30
103
|
}
|
|
104
|
+
|
|
105
|
+
// For pasted content, we try to remove the exceeding content.
|
|
106
|
+
const pos = transaction.selection.$head.pos
|
|
107
|
+
const over = newSize - limit
|
|
108
|
+
const from = pos - over
|
|
109
|
+
const to = pos
|
|
110
|
+
|
|
111
|
+
// It’s probably a bad idea to mutate transactions within `filterTransaction`
|
|
112
|
+
// but for now this is working fine.
|
|
113
|
+
transaction.deleteRange(from, to)
|
|
114
|
+
|
|
115
|
+
// In some situations, the limit will continue to be exceeded after trimming.
|
|
116
|
+
// This happens e.g. when truncating within a complex node (e.g. table)
|
|
117
|
+
// and ProseMirror has to close this node again.
|
|
118
|
+
// If this is the case, we prevent the transaction completely.
|
|
119
|
+
const updatedSize = this.storage.characters({ node: transaction.doc })
|
|
120
|
+
|
|
121
|
+
if (updatedSize > limit) {
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return true
|
|
31
126
|
},
|
|
32
127
|
}),
|
|
33
128
|
]
|