@tiptap/extension-character-count 3.0.0-next.3 → 3.0.0-next.5
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/LICENSE.md +21 -0
- package/README.md +5 -1
- package/dist/index.cjs +11 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -57
- package/dist/index.d.ts +2 -57
- package/dist/index.js +6 -100
- package/dist/index.js.map +1 -1
- package/package.json +10 -8
- package/src/index.ts +2 -2
- package/src/character-count.ts +0 -187
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Tiptap GmbH
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
# @tiptap/extension-character-count
|
|
2
|
+
|
|
2
3
|
[](https://www.npmjs.com/package/@tiptap/extension-character-count)
|
|
3
4
|
[](https://npmcharts.com/compare/tiptap?minimal=true)
|
|
4
5
|
[](https://www.npmjs.com/package/@tiptap/extension-character-count)
|
|
5
6
|
[](https://github.com/sponsors/ueberdosis)
|
|
6
7
|
|
|
7
8
|
## Introduction
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
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
11
|
|
|
10
12
|
## Official Documentation
|
|
13
|
+
|
|
11
14
|
Documentation can be found on the [Tiptap website](https://tiptap.dev).
|
|
12
15
|
|
|
13
16
|
## License
|
|
17
|
+
|
|
14
18
|
Tiptap is open sourced software licensed under the [MIT license](https://github.com/ueberdosis/tiptap/blob/main/LICENSE.md).
|
package/dist/index.cjs
CHANGED
|
@@ -18,114 +18,19 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
20
|
// src/index.ts
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
23
|
-
CharacterCount: () => CharacterCount,
|
|
24
|
-
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CharacterCount: () => import_extensions2.CharacterCount,
|
|
24
|
+
CharacterCountOptions: () => import_extensions2.CharacterCountOptions,
|
|
25
|
+
default: () => index_default
|
|
25
26
|
});
|
|
26
|
-
module.exports = __toCommonJS(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
var import_state = require("@tiptap/pm/state");
|
|
31
|
-
var CharacterCount = import_core.Extension.create({
|
|
32
|
-
name: "characterCount",
|
|
33
|
-
addOptions() {
|
|
34
|
-
return {
|
|
35
|
-
limit: null,
|
|
36
|
-
mode: "textSize",
|
|
37
|
-
textCounter: (text) => text.length,
|
|
38
|
-
wordCounter: (text) => text.split(" ").filter((word) => word !== "").length
|
|
39
|
-
};
|
|
40
|
-
},
|
|
41
|
-
addStorage() {
|
|
42
|
-
return {
|
|
43
|
-
characters: () => 0,
|
|
44
|
-
words: () => 0
|
|
45
|
-
};
|
|
46
|
-
},
|
|
47
|
-
onBeforeCreate() {
|
|
48
|
-
this.storage.characters = (options) => {
|
|
49
|
-
const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
|
|
50
|
-
const mode = (options == null ? void 0 : options.mode) || this.options.mode;
|
|
51
|
-
if (mode === "textSize") {
|
|
52
|
-
const text = node.textBetween(0, node.content.size, void 0, " ");
|
|
53
|
-
return this.options.textCounter(text);
|
|
54
|
-
}
|
|
55
|
-
return node.nodeSize;
|
|
56
|
-
};
|
|
57
|
-
this.storage.words = (options) => {
|
|
58
|
-
const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
|
|
59
|
-
const text = node.textBetween(0, node.content.size, " ", " ");
|
|
60
|
-
return this.options.wordCounter(text);
|
|
61
|
-
};
|
|
62
|
-
},
|
|
63
|
-
addProseMirrorPlugins() {
|
|
64
|
-
let initialEvaluationDone = false;
|
|
65
|
-
return [
|
|
66
|
-
new import_state.Plugin({
|
|
67
|
-
key: new import_state.PluginKey("characterCount"),
|
|
68
|
-
appendTransaction: (transactions, oldState, newState) => {
|
|
69
|
-
if (initialEvaluationDone) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const limit = this.options.limit;
|
|
73
|
-
if (limit === null || limit === void 0 || limit === 0) {
|
|
74
|
-
initialEvaluationDone = true;
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const initialContentSize = this.storage.characters({ node: newState.doc });
|
|
78
|
-
if (initialContentSize > limit) {
|
|
79
|
-
const over = initialContentSize - limit;
|
|
80
|
-
const from = 0;
|
|
81
|
-
const to = over;
|
|
82
|
-
console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`);
|
|
83
|
-
const tr = newState.tr.deleteRange(from, to);
|
|
84
|
-
initialEvaluationDone = true;
|
|
85
|
-
return tr;
|
|
86
|
-
}
|
|
87
|
-
initialEvaluationDone = true;
|
|
88
|
-
},
|
|
89
|
-
filterTransaction: (transaction, state) => {
|
|
90
|
-
const limit = this.options.limit;
|
|
91
|
-
if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
|
-
const oldSize = this.storage.characters({ node: state.doc });
|
|
95
|
-
const newSize = this.storage.characters({ node: transaction.doc });
|
|
96
|
-
if (newSize <= limit) {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
if (oldSize > limit && newSize > limit && newSize <= oldSize) {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
if (oldSize > limit && newSize > limit && newSize > oldSize) {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
const isPaste = transaction.getMeta("paste");
|
|
106
|
-
if (!isPaste) {
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
const pos = transaction.selection.$head.pos;
|
|
110
|
-
const over = newSize - limit;
|
|
111
|
-
const from = pos - over;
|
|
112
|
-
const to = pos;
|
|
113
|
-
transaction.deleteRange(from, to);
|
|
114
|
-
const updatedSize = this.storage.characters({ node: transaction.doc });
|
|
115
|
-
if (updatedSize > limit) {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
})
|
|
121
|
-
];
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// src/index.ts
|
|
126
|
-
var src_default = CharacterCount;
|
|
27
|
+
module.exports = __toCommonJS(index_exports);
|
|
28
|
+
var import_extensions = require("@tiptap/extensions");
|
|
29
|
+
var import_extensions2 = require("@tiptap/extensions");
|
|
30
|
+
var index_default = import_extensions.CharacterCount;
|
|
127
31
|
// Annotate the CommonJS export names for ESM import in node:
|
|
128
32
|
0 && (module.exports = {
|
|
129
|
-
CharacterCount
|
|
33
|
+
CharacterCount,
|
|
34
|
+
CharacterCountOptions
|
|
130
35
|
});
|
|
131
36
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { CharacterCount } from '@tiptap/extensions'\n\nexport { CharacterCount, CharacterCountOptions } from '@tiptap/extensions'\n\nexport default CharacterCount\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAA+B;AAE/B,IAAAA,qBAAsD;AAEtD,IAAO,gBAAQ;","names":["import_extensions"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,57 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
interface CharacterCountOptions {
|
|
5
|
-
/**
|
|
6
|
-
* The maximum number of characters that should be allowed. Defaults to `0`.
|
|
7
|
-
* @default null
|
|
8
|
-
* @example 180
|
|
9
|
-
*/
|
|
10
|
-
limit: number | null | undefined;
|
|
11
|
-
/**
|
|
12
|
-
* The mode by which the size is calculated. If set to `textSize`, the textContent of the document is used.
|
|
13
|
-
* If set to `nodeSize`, the nodeSize of the document is used.
|
|
14
|
-
* @default 'textSize'
|
|
15
|
-
* @example 'textSize'
|
|
16
|
-
*/
|
|
17
|
-
mode: 'textSize' | 'nodeSize';
|
|
18
|
-
/**
|
|
19
|
-
* The text counter function to use. Defaults to a simple character count.
|
|
20
|
-
* @default (text) => text.length
|
|
21
|
-
* @example (text) => [...new Intl.Segmenter().segment(text)].length
|
|
22
|
-
*/
|
|
23
|
-
textCounter: (text: string) => number;
|
|
24
|
-
/**
|
|
25
|
-
* The word counter function to use. Defaults to a simple word count.
|
|
26
|
-
* @default (text) => text.split(' ').filter(word => word !== '').length
|
|
27
|
-
* @example (text) => text.split(/\s+/).filter(word => word !== '').length
|
|
28
|
-
*/
|
|
29
|
-
wordCounter: (text: string) => number;
|
|
30
|
-
}
|
|
31
|
-
interface CharacterCountStorage {
|
|
32
|
-
/**
|
|
33
|
-
* Get the number of characters for the current document.
|
|
34
|
-
* @param options The options for the character count. (optional)
|
|
35
|
-
* @param options.node The node to get the characters from. Defaults to the current document.
|
|
36
|
-
* @param options.mode The mode by which the size is calculated. If set to `textSize`, the textContent of the document is used.
|
|
37
|
-
*/
|
|
38
|
-
characters: (options?: {
|
|
39
|
-
node?: Node;
|
|
40
|
-
mode?: 'textSize' | 'nodeSize';
|
|
41
|
-
}) => number;
|
|
42
|
-
/**
|
|
43
|
-
* Get the number of words for the current document.
|
|
44
|
-
* @param options The options for the character count. (optional)
|
|
45
|
-
* @param options.node The node to get the words from. Defaults to the current document.
|
|
46
|
-
*/
|
|
47
|
-
words: (options?: {
|
|
48
|
-
node?: Node;
|
|
49
|
-
}) => number;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* This extension allows you to count the characters and words of your document.
|
|
53
|
-
* @see https://tiptap.dev/api/extensions/character-count
|
|
54
|
-
*/
|
|
55
|
-
declare const CharacterCount: Extension<CharacterCountOptions, CharacterCountStorage>;
|
|
56
|
-
|
|
57
|
-
export { CharacterCount, type CharacterCountOptions, type CharacterCountStorage, CharacterCount as default };
|
|
1
|
+
import { CharacterCount } from '@tiptap/extensions';
|
|
2
|
+
export { CharacterCount, CharacterCountOptions, CharacterCount as default } from '@tiptap/extensions';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,57 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
interface CharacterCountOptions {
|
|
5
|
-
/**
|
|
6
|
-
* The maximum number of characters that should be allowed. Defaults to `0`.
|
|
7
|
-
* @default null
|
|
8
|
-
* @example 180
|
|
9
|
-
*/
|
|
10
|
-
limit: number | null | undefined;
|
|
11
|
-
/**
|
|
12
|
-
* The mode by which the size is calculated. If set to `textSize`, the textContent of the document is used.
|
|
13
|
-
* If set to `nodeSize`, the nodeSize of the document is used.
|
|
14
|
-
* @default 'textSize'
|
|
15
|
-
* @example 'textSize'
|
|
16
|
-
*/
|
|
17
|
-
mode: 'textSize' | 'nodeSize';
|
|
18
|
-
/**
|
|
19
|
-
* The text counter function to use. Defaults to a simple character count.
|
|
20
|
-
* @default (text) => text.length
|
|
21
|
-
* @example (text) => [...new Intl.Segmenter().segment(text)].length
|
|
22
|
-
*/
|
|
23
|
-
textCounter: (text: string) => number;
|
|
24
|
-
/**
|
|
25
|
-
* The word counter function to use. Defaults to a simple word count.
|
|
26
|
-
* @default (text) => text.split(' ').filter(word => word !== '').length
|
|
27
|
-
* @example (text) => text.split(/\s+/).filter(word => word !== '').length
|
|
28
|
-
*/
|
|
29
|
-
wordCounter: (text: string) => number;
|
|
30
|
-
}
|
|
31
|
-
interface CharacterCountStorage {
|
|
32
|
-
/**
|
|
33
|
-
* Get the number of characters for the current document.
|
|
34
|
-
* @param options The options for the character count. (optional)
|
|
35
|
-
* @param options.node The node to get the characters from. Defaults to the current document.
|
|
36
|
-
* @param options.mode The mode by which the size is calculated. If set to `textSize`, the textContent of the document is used.
|
|
37
|
-
*/
|
|
38
|
-
characters: (options?: {
|
|
39
|
-
node?: Node;
|
|
40
|
-
mode?: 'textSize' | 'nodeSize';
|
|
41
|
-
}) => number;
|
|
42
|
-
/**
|
|
43
|
-
* Get the number of words for the current document.
|
|
44
|
-
* @param options The options for the character count. (optional)
|
|
45
|
-
* @param options.node The node to get the words from. Defaults to the current document.
|
|
46
|
-
*/
|
|
47
|
-
words: (options?: {
|
|
48
|
-
node?: Node;
|
|
49
|
-
}) => number;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* This extension allows you to count the characters and words of your document.
|
|
53
|
-
* @see https://tiptap.dev/api/extensions/character-count
|
|
54
|
-
*/
|
|
55
|
-
declare const CharacterCount: Extension<CharacterCountOptions, CharacterCountStorage>;
|
|
56
|
-
|
|
57
|
-
export { CharacterCount, type CharacterCountOptions, type CharacterCountStorage, CharacterCount as default };
|
|
1
|
+
import { CharacterCount } from '@tiptap/extensions';
|
|
2
|
+
export { CharacterCount, CharacterCountOptions, CharacterCount as default } from '@tiptap/extensions';
|
package/dist/index.js
CHANGED
|
@@ -1,104 +1,10 @@
|
|
|
1
|
-
// src/character-count.ts
|
|
2
|
-
import { Extension } from "@tiptap/core";
|
|
3
|
-
import { Plugin, PluginKey } from "@tiptap/pm/state";
|
|
4
|
-
var CharacterCount = Extension.create({
|
|
5
|
-
name: "characterCount",
|
|
6
|
-
addOptions() {
|
|
7
|
-
return {
|
|
8
|
-
limit: null,
|
|
9
|
-
mode: "textSize",
|
|
10
|
-
textCounter: (text) => text.length,
|
|
11
|
-
wordCounter: (text) => text.split(" ").filter((word) => word !== "").length
|
|
12
|
-
};
|
|
13
|
-
},
|
|
14
|
-
addStorage() {
|
|
15
|
-
return {
|
|
16
|
-
characters: () => 0,
|
|
17
|
-
words: () => 0
|
|
18
|
-
};
|
|
19
|
-
},
|
|
20
|
-
onBeforeCreate() {
|
|
21
|
-
this.storage.characters = (options) => {
|
|
22
|
-
const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
|
|
23
|
-
const mode = (options == null ? void 0 : options.mode) || this.options.mode;
|
|
24
|
-
if (mode === "textSize") {
|
|
25
|
-
const text = node.textBetween(0, node.content.size, void 0, " ");
|
|
26
|
-
return this.options.textCounter(text);
|
|
27
|
-
}
|
|
28
|
-
return node.nodeSize;
|
|
29
|
-
};
|
|
30
|
-
this.storage.words = (options) => {
|
|
31
|
-
const node = (options == null ? void 0 : options.node) || this.editor.state.doc;
|
|
32
|
-
const text = node.textBetween(0, node.content.size, " ", " ");
|
|
33
|
-
return this.options.wordCounter(text);
|
|
34
|
-
};
|
|
35
|
-
},
|
|
36
|
-
addProseMirrorPlugins() {
|
|
37
|
-
let initialEvaluationDone = false;
|
|
38
|
-
return [
|
|
39
|
-
new Plugin({
|
|
40
|
-
key: new PluginKey("characterCount"),
|
|
41
|
-
appendTransaction: (transactions, oldState, newState) => {
|
|
42
|
-
if (initialEvaluationDone) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
const limit = this.options.limit;
|
|
46
|
-
if (limit === null || limit === void 0 || limit === 0) {
|
|
47
|
-
initialEvaluationDone = true;
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const initialContentSize = this.storage.characters({ node: newState.doc });
|
|
51
|
-
if (initialContentSize > limit) {
|
|
52
|
-
const over = initialContentSize - limit;
|
|
53
|
-
const from = 0;
|
|
54
|
-
const to = over;
|
|
55
|
-
console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`);
|
|
56
|
-
const tr = newState.tr.deleteRange(from, to);
|
|
57
|
-
initialEvaluationDone = true;
|
|
58
|
-
return tr;
|
|
59
|
-
}
|
|
60
|
-
initialEvaluationDone = true;
|
|
61
|
-
},
|
|
62
|
-
filterTransaction: (transaction, state) => {
|
|
63
|
-
const limit = this.options.limit;
|
|
64
|
-
if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
const oldSize = this.storage.characters({ node: state.doc });
|
|
68
|
-
const newSize = this.storage.characters({ node: transaction.doc });
|
|
69
|
-
if (newSize <= limit) {
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
if (oldSize > limit && newSize > limit && newSize <= oldSize) {
|
|
73
|
-
return true;
|
|
74
|
-
}
|
|
75
|
-
if (oldSize > limit && newSize > limit && newSize > oldSize) {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
const isPaste = transaction.getMeta("paste");
|
|
79
|
-
if (!isPaste) {
|
|
80
|
-
return false;
|
|
81
|
-
}
|
|
82
|
-
const pos = transaction.selection.$head.pos;
|
|
83
|
-
const over = newSize - limit;
|
|
84
|
-
const from = pos - over;
|
|
85
|
-
const to = pos;
|
|
86
|
-
transaction.deleteRange(from, to);
|
|
87
|
-
const updatedSize = this.storage.characters({ node: transaction.doc });
|
|
88
|
-
if (updatedSize > limit) {
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
return true;
|
|
92
|
-
}
|
|
93
|
-
})
|
|
94
|
-
];
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
|
|
98
1
|
// src/index.ts
|
|
99
|
-
|
|
2
|
+
import { CharacterCount } from "@tiptap/extensions";
|
|
3
|
+
import { CharacterCount as CharacterCount2, CharacterCountOptions } from "@tiptap/extensions";
|
|
4
|
+
var index_default = CharacterCount;
|
|
100
5
|
export {
|
|
101
|
-
CharacterCount,
|
|
102
|
-
|
|
6
|
+
CharacterCount2 as CharacterCount,
|
|
7
|
+
CharacterCountOptions,
|
|
8
|
+
index_default as default
|
|
103
9
|
};
|
|
104
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { CharacterCount } from '@tiptap/extensions'\n\nexport { CharacterCount, CharacterCountOptions } from '@tiptap/extensions'\n\nexport default CharacterCount\n"],"mappings":";AAAA,SAAS,sBAAsB;AAE/B,SAAS,kBAAAA,iBAAgB,6BAA6B;AAEtD,IAAO,gBAAQ;","names":["CharacterCount"]}
|
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": "3.0.0-next.
|
|
4
|
+
"version": "3.0.0-next.5",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -15,7 +15,10 @@
|
|
|
15
15
|
"type": "module",
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
|
-
"types":
|
|
18
|
+
"types": {
|
|
19
|
+
"import": "./dist/index.d.ts",
|
|
20
|
+
"require": "./dist/index.d.cts"
|
|
21
|
+
},
|
|
19
22
|
"import": "./dist/index.js",
|
|
20
23
|
"require": "./dist/index.cjs"
|
|
21
24
|
}
|
|
@@ -28,12 +31,10 @@
|
|
|
28
31
|
"dist"
|
|
29
32
|
],
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"@tiptap/
|
|
32
|
-
"@tiptap/pm": "^3.0.0-next.3"
|
|
34
|
+
"@tiptap/extensions": "^3.0.0-next.5"
|
|
33
35
|
},
|
|
34
36
|
"peerDependencies": {
|
|
35
|
-
"@tiptap/
|
|
36
|
-
"@tiptap/pm": "^3.0.0-next.1"
|
|
37
|
+
"@tiptap/extensions": "^3.0.0-next.3"
|
|
37
38
|
},
|
|
38
39
|
"repository": {
|
|
39
40
|
"type": "git",
|
|
@@ -41,6 +42,7 @@
|
|
|
41
42
|
"directory": "packages/extension-character-count"
|
|
42
43
|
},
|
|
43
44
|
"scripts": {
|
|
44
|
-
"build": "tsup"
|
|
45
|
+
"build": "tsup",
|
|
46
|
+
"lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
|
|
45
47
|
}
|
|
46
|
-
}
|
|
48
|
+
}
|
package/src/index.ts
CHANGED
package/src/character-count.ts
DELETED
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import { Extension } from '@tiptap/core'
|
|
2
|
-
import { Node as ProseMirrorNode } from '@tiptap/pm/model'
|
|
3
|
-
import { Plugin, PluginKey } from '@tiptap/pm/state'
|
|
4
|
-
|
|
5
|
-
export interface CharacterCountOptions {
|
|
6
|
-
/**
|
|
7
|
-
* The maximum number of characters that should be allowed. Defaults to `0`.
|
|
8
|
-
* @default null
|
|
9
|
-
* @example 180
|
|
10
|
-
*/
|
|
11
|
-
limit: number | null | undefined
|
|
12
|
-
/**
|
|
13
|
-
* The mode by which the size is calculated. If set to `textSize`, the textContent of the document is used.
|
|
14
|
-
* If set to `nodeSize`, the nodeSize of the document is used.
|
|
15
|
-
* @default 'textSize'
|
|
16
|
-
* @example 'textSize'
|
|
17
|
-
*/
|
|
18
|
-
mode: 'textSize' | 'nodeSize'
|
|
19
|
-
/**
|
|
20
|
-
* The text counter function to use. Defaults to a simple character count.
|
|
21
|
-
* @default (text) => text.length
|
|
22
|
-
* @example (text) => [...new Intl.Segmenter().segment(text)].length
|
|
23
|
-
*/
|
|
24
|
-
textCounter: (text: string) => number
|
|
25
|
-
/**
|
|
26
|
-
* The word counter function to use. Defaults to a simple word count.
|
|
27
|
-
* @default (text) => text.split(' ').filter(word => word !== '').length
|
|
28
|
-
* @example (text) => text.split(/\s+/).filter(word => word !== '').length
|
|
29
|
-
*/
|
|
30
|
-
wordCounter: (text: string) => number
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface CharacterCountStorage {
|
|
34
|
-
/**
|
|
35
|
-
* Get the number of characters for the current document.
|
|
36
|
-
* @param options The options for the character count. (optional)
|
|
37
|
-
* @param options.node The node to get the characters from. Defaults to the current document.
|
|
38
|
-
* @param options.mode The mode by which the size is calculated. If set to `textSize`, the textContent of the document is used.
|
|
39
|
-
*/
|
|
40
|
-
characters: (options?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Get the number of words for the current document.
|
|
44
|
-
* @param options The options for the character count. (optional)
|
|
45
|
-
* @param options.node The node to get the words from. Defaults to the current document.
|
|
46
|
-
*/
|
|
47
|
-
words: (options?: { node?: ProseMirrorNode }) => number
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* This extension allows you to count the characters and words of your document.
|
|
52
|
-
* @see https://tiptap.dev/api/extensions/character-count
|
|
53
|
-
*/
|
|
54
|
-
export const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({
|
|
55
|
-
name: 'characterCount',
|
|
56
|
-
|
|
57
|
-
addOptions() {
|
|
58
|
-
return {
|
|
59
|
-
limit: null,
|
|
60
|
-
mode: 'textSize',
|
|
61
|
-
textCounter: text => text.length,
|
|
62
|
-
wordCounter: text => text.split(' ').filter(word => word !== '').length,
|
|
63
|
-
}
|
|
64
|
-
},
|
|
65
|
-
|
|
66
|
-
addStorage() {
|
|
67
|
-
return {
|
|
68
|
-
characters: () => 0,
|
|
69
|
-
words: () => 0,
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
onBeforeCreate() {
|
|
74
|
-
this.storage.characters = options => {
|
|
75
|
-
const node = options?.node || this.editor.state.doc
|
|
76
|
-
const mode = options?.mode || this.options.mode
|
|
77
|
-
|
|
78
|
-
if (mode === 'textSize') {
|
|
79
|
-
const text = node.textBetween(0, node.content.size, undefined, ' ')
|
|
80
|
-
|
|
81
|
-
return this.options.textCounter(text)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return node.nodeSize
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
this.storage.words = options => {
|
|
88
|
-
const node = options?.node || this.editor.state.doc
|
|
89
|
-
const text = node.textBetween(0, node.content.size, ' ', ' ')
|
|
90
|
-
|
|
91
|
-
return this.options.wordCounter(text)
|
|
92
|
-
}
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
addProseMirrorPlugins() {
|
|
96
|
-
let initialEvaluationDone = false
|
|
97
|
-
|
|
98
|
-
return [
|
|
99
|
-
new Plugin({
|
|
100
|
-
key: new PluginKey('characterCount'),
|
|
101
|
-
appendTransaction: (transactions, oldState, newState) => {
|
|
102
|
-
if (initialEvaluationDone) {
|
|
103
|
-
return
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const limit = this.options.limit
|
|
107
|
-
|
|
108
|
-
if (limit === null || limit === undefined || limit === 0) {
|
|
109
|
-
initialEvaluationDone = true
|
|
110
|
-
return
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const initialContentSize = this.storage.characters({ node: newState.doc })
|
|
114
|
-
|
|
115
|
-
if (initialContentSize > limit) {
|
|
116
|
-
const over = initialContentSize - limit
|
|
117
|
-
const from = 0
|
|
118
|
-
const to = over
|
|
119
|
-
|
|
120
|
-
console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`)
|
|
121
|
-
const tr = newState.tr.deleteRange(from, to)
|
|
122
|
-
|
|
123
|
-
initialEvaluationDone = true
|
|
124
|
-
return tr
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
initialEvaluationDone = true
|
|
128
|
-
},
|
|
129
|
-
filterTransaction: (transaction, state) => {
|
|
130
|
-
const limit = this.options.limit
|
|
131
|
-
|
|
132
|
-
// Nothing has changed or no limit is defined. Ignore it.
|
|
133
|
-
if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
|
|
134
|
-
return true
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const oldSize = this.storage.characters({ node: state.doc })
|
|
138
|
-
const newSize = this.storage.characters({ node: transaction.doc })
|
|
139
|
-
|
|
140
|
-
// Everything is in the limit. Good.
|
|
141
|
-
if (newSize <= limit) {
|
|
142
|
-
return true
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// The limit has already been exceeded but will be reduced.
|
|
146
|
-
if (oldSize > limit && newSize > limit && newSize <= oldSize) {
|
|
147
|
-
return true
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// The limit has already been exceeded and will be increased further.
|
|
151
|
-
if (oldSize > limit && newSize > limit && newSize > oldSize) {
|
|
152
|
-
return false
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const isPaste = transaction.getMeta('paste')
|
|
156
|
-
|
|
157
|
-
// Block all exceeding transactions that were not pasted.
|
|
158
|
-
if (!isPaste) {
|
|
159
|
-
return false
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// For pasted content, we try to remove the exceeding content.
|
|
163
|
-
const pos = transaction.selection.$head.pos
|
|
164
|
-
const over = newSize - limit
|
|
165
|
-
const from = pos - over
|
|
166
|
-
const to = pos
|
|
167
|
-
|
|
168
|
-
// It’s probably a bad idea to mutate transactions within `filterTransaction`
|
|
169
|
-
// but for now this is working fine.
|
|
170
|
-
transaction.deleteRange(from, to)
|
|
171
|
-
|
|
172
|
-
// In some situations, the limit will continue to be exceeded after trimming.
|
|
173
|
-
// This happens e.g. when truncating within a complex node (e.g. table)
|
|
174
|
-
// and ProseMirror has to close this node again.
|
|
175
|
-
// If this is the case, we prevent the transaction completely.
|
|
176
|
-
const updatedSize = this.storage.characters({ node: transaction.doc })
|
|
177
|
-
|
|
178
|
-
if (updatedSize > limit) {
|
|
179
|
-
return false
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return true
|
|
183
|
-
},
|
|
184
|
-
}),
|
|
185
|
-
]
|
|
186
|
-
},
|
|
187
|
-
})
|