@ckeditor/ckeditor5-word-count 36.0.1 → 37.0.0-alpha.0
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +18 -17
- package/src/index.d.ts +8 -0
- package/src/utils.d.ts +14 -0
- package/src/wordcount.d.ts +233 -0
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ckeditor/ckeditor5-word-count",
|
3
|
-
"version": "
|
3
|
+
"version": "37.0.0-alpha.0",
|
4
4
|
"description": "Word and character count feature for CKEditor 5.",
|
5
5
|
"keywords": [
|
6
6
|
"ckeditor",
|
@@ -13,23 +13,23 @@
|
|
13
13
|
"main": "src/index.js",
|
14
14
|
"dependencies": {
|
15
15
|
"lodash-es": "^4.17.15",
|
16
|
-
"ckeditor5": "^
|
16
|
+
"ckeditor5": "^37.0.0-alpha.0"
|
17
17
|
},
|
18
18
|
"devDependencies": {
|
19
|
-
"@ckeditor/ckeditor5-basic-styles": "^
|
20
|
-
"@ckeditor/ckeditor5-block-quote": "^
|
21
|
-
"@ckeditor/ckeditor5-core": "^
|
22
|
-
"@ckeditor/ckeditor5-dev-utils": "^
|
23
|
-
"@ckeditor/ckeditor5-editor-classic": "^
|
24
|
-
"@ckeditor/ckeditor5-engine": "^
|
25
|
-
"@ckeditor/ckeditor5-enter": "^
|
26
|
-
"@ckeditor/ckeditor5-image": "^
|
27
|
-
"@ckeditor/ckeditor5-link": "^
|
28
|
-
"@ckeditor/ckeditor5-list": "^
|
29
|
-
"@ckeditor/ckeditor5-paragraph": "^
|
30
|
-
"@ckeditor/ckeditor5-table": "^
|
31
|
-
"@ckeditor/ckeditor5-theme-lark": "^
|
32
|
-
"@ckeditor/ckeditor5-utils": "^
|
19
|
+
"@ckeditor/ckeditor5-basic-styles": "^37.0.0-alpha.0",
|
20
|
+
"@ckeditor/ckeditor5-block-quote": "^37.0.0-alpha.0",
|
21
|
+
"@ckeditor/ckeditor5-core": "^37.0.0-alpha.0",
|
22
|
+
"@ckeditor/ckeditor5-dev-utils": "^34.0.0",
|
23
|
+
"@ckeditor/ckeditor5-editor-classic": "^37.0.0-alpha.0",
|
24
|
+
"@ckeditor/ckeditor5-engine": "^37.0.0-alpha.0",
|
25
|
+
"@ckeditor/ckeditor5-enter": "^37.0.0-alpha.0",
|
26
|
+
"@ckeditor/ckeditor5-image": "^37.0.0-alpha.0",
|
27
|
+
"@ckeditor/ckeditor5-link": "^37.0.0-alpha.0",
|
28
|
+
"@ckeditor/ckeditor5-list": "^37.0.0-alpha.0",
|
29
|
+
"@ckeditor/ckeditor5-paragraph": "^37.0.0-alpha.0",
|
30
|
+
"@ckeditor/ckeditor5-table": "^37.0.0-alpha.0",
|
31
|
+
"@ckeditor/ckeditor5-theme-lark": "^37.0.0-alpha.0",
|
32
|
+
"@ckeditor/ckeditor5-utils": "^37.0.0-alpha.0",
|
33
33
|
"typescript": "^4.8.4",
|
34
34
|
"webpack": "^5.58.1",
|
35
35
|
"webpack-cli": "^4.9.0"
|
@@ -60,5 +60,6 @@
|
|
60
60
|
"dll:build": "webpack",
|
61
61
|
"build": "tsc -p ./tsconfig.release.json",
|
62
62
|
"postversion": "npm run build"
|
63
|
-
}
|
63
|
+
},
|
64
|
+
"types": "src/index.d.ts"
|
64
65
|
}
|
package/src/index.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
/**
|
6
|
+
* @module word-count
|
7
|
+
*/
|
8
|
+
export { default as WordCount, type WordCountUpdateEvent } from './wordcount';
|
package/src/utils.d.ts
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
/**
|
6
|
+
* @module word-count/utils
|
7
|
+
*/
|
8
|
+
import type { Item } from 'ckeditor5/src/engine';
|
9
|
+
/**
|
10
|
+
* Returns a plain text representation of an element and its children.
|
11
|
+
*
|
12
|
+
* @returns Plain text representing the model's data.
|
13
|
+
*/
|
14
|
+
export declare function modelElementToPlainText(item: Item): string;
|
@@ -0,0 +1,233 @@
|
|
1
|
+
/**
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
4
|
+
*/
|
5
|
+
import { Plugin, type Editor } from 'ckeditor5/src/core';
|
6
|
+
/**
|
7
|
+
* The word count plugin.
|
8
|
+
*
|
9
|
+
* This plugin calculates all words and characters in all {@link module:engine/model/text~Text text nodes} available in the model.
|
10
|
+
* It also provides an HTML element that updates its state whenever the editor content is changed.
|
11
|
+
*
|
12
|
+
* The model's data is first converted to plain text using {@link module:word-count/utils~modelElementToPlainText}.
|
13
|
+
* The number of words and characters in your text are determined based on the created plain text. Please keep in mind
|
14
|
+
* that every block in the editor is separated with a newline character, which is included in the calculation.
|
15
|
+
*
|
16
|
+
* Here are some examples of how the word and character calculations are made:
|
17
|
+
*
|
18
|
+
* ```html
|
19
|
+
* <paragraph>foo</paragraph>
|
20
|
+
* <paragraph>bar</paragraph>
|
21
|
+
* // Words: 2, Characters: 7
|
22
|
+
*
|
23
|
+
* <paragraph><$text bold="true">foo</$text>bar</paragraph>
|
24
|
+
* // Words: 1, Characters: 6
|
25
|
+
*
|
26
|
+
* <paragraph>*&^%)</paragraph>
|
27
|
+
* // Words: 0, Characters: 5
|
28
|
+
*
|
29
|
+
* <paragraph>foo(bar)</paragraph>
|
30
|
+
* //Words: 1, Characters: 8
|
31
|
+
*
|
32
|
+
* <paragraph>12345</paragraph>
|
33
|
+
* // Words: 1, Characters: 5
|
34
|
+
* ```
|
35
|
+
*/
|
36
|
+
export default class WordCount extends Plugin {
|
37
|
+
/**
|
38
|
+
* The number of characters in the editor.
|
39
|
+
*
|
40
|
+
* @observable
|
41
|
+
* @readonly
|
42
|
+
*/
|
43
|
+
characters: number;
|
44
|
+
/**
|
45
|
+
* The number of words in the editor.
|
46
|
+
*
|
47
|
+
* @observable
|
48
|
+
* @readonly
|
49
|
+
*/
|
50
|
+
words: number;
|
51
|
+
/**
|
52
|
+
* The label used to display the words value in the {@link #wordCountContainer output container}.
|
53
|
+
*
|
54
|
+
* @observable
|
55
|
+
* @private
|
56
|
+
* @readonly
|
57
|
+
*/
|
58
|
+
_wordsLabel: string | undefined;
|
59
|
+
/**
|
60
|
+
* The label used to display the characters value in the {@link #wordCountContainer output container}.
|
61
|
+
*
|
62
|
+
* @observable
|
63
|
+
* @private
|
64
|
+
* @readonly
|
65
|
+
*/
|
66
|
+
_charactersLabel: string | undefined;
|
67
|
+
/**
|
68
|
+
* The configuration of this plugin.
|
69
|
+
*/
|
70
|
+
private _config;
|
71
|
+
/**
|
72
|
+
* The reference to a {@link module:ui/view~View view object} that contains the self-updating HTML container.
|
73
|
+
*/
|
74
|
+
private _outputView;
|
75
|
+
/**
|
76
|
+
* A regular expression used to recognize words in the editor's content.
|
77
|
+
*/
|
78
|
+
private readonly _wordsMatchRegExp;
|
79
|
+
/**
|
80
|
+
* @inheritDoc
|
81
|
+
*/
|
82
|
+
constructor(editor: Editor);
|
83
|
+
/**
|
84
|
+
* @inheritDoc
|
85
|
+
*/
|
86
|
+
static get pluginName(): 'WordCount';
|
87
|
+
/**
|
88
|
+
* @inheritDoc
|
89
|
+
*/
|
90
|
+
init(): void;
|
91
|
+
/**
|
92
|
+
* @inheritDoc
|
93
|
+
*/
|
94
|
+
destroy(): void;
|
95
|
+
/**
|
96
|
+
* Creates a self-updating HTML element. Repeated executions return the same element.
|
97
|
+
* The returned element has the following HTML structure:
|
98
|
+
*
|
99
|
+
* ```html
|
100
|
+
* <div class="ck ck-word-count">
|
101
|
+
* <div class="ck-word-count__words">Words: 4</div>
|
102
|
+
* <div class="ck-word-count__characters">Characters: 28</div>
|
103
|
+
* </div>
|
104
|
+
* ```
|
105
|
+
*/
|
106
|
+
get wordCountContainer(): HTMLElement;
|
107
|
+
/**
|
108
|
+
* Determines the number of characters in the current editor's model.
|
109
|
+
*/
|
110
|
+
private _getCharacters;
|
111
|
+
/**
|
112
|
+
* Determines the number of words in the current editor's model.
|
113
|
+
*/
|
114
|
+
private _getWords;
|
115
|
+
/**
|
116
|
+
* Determines the number of words and characters in the current editor's model and assigns it to {@link #characters} and {@link #words}.
|
117
|
+
* It also fires the {@link #event:update}.
|
118
|
+
*
|
119
|
+
* @fires update
|
120
|
+
*/
|
121
|
+
private _refreshStats;
|
122
|
+
}
|
123
|
+
/**
|
124
|
+
* An event fired after {@link #words} and {@link #characters} are updated.
|
125
|
+
*
|
126
|
+
* @eventName update
|
127
|
+
*/
|
128
|
+
export type WordCountUpdateEvent = {
|
129
|
+
name: 'update';
|
130
|
+
args: [{
|
131
|
+
words: number;
|
132
|
+
characters: number;
|
133
|
+
}];
|
134
|
+
};
|
135
|
+
/**
|
136
|
+
* The configuration of the word count feature.
|
137
|
+
*
|
138
|
+
* ```ts
|
139
|
+
* ClassicEditor
|
140
|
+
* .create( {
|
141
|
+
* wordCount: ... // Word count feature configuration.
|
142
|
+
* } )
|
143
|
+
* .then( ... )
|
144
|
+
* .catch( ... );
|
145
|
+
* ```
|
146
|
+
*
|
147
|
+
* See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
|
148
|
+
*/
|
149
|
+
export interface WordCountConfig {
|
150
|
+
/**
|
151
|
+
* This option allows for hiding the word counter. The element obtained through
|
152
|
+
* {@link module:word-count/wordcount~WordCount#wordCountContainer} will only preserve
|
153
|
+
* the characters part. Word counter is displayed by default when this configuration option is not defined.
|
154
|
+
*
|
155
|
+
* ```ts
|
156
|
+
* const wordCountConfig = {
|
157
|
+
* displayWords: false
|
158
|
+
* };
|
159
|
+
* ```
|
160
|
+
*
|
161
|
+
* The configuration above will result in the following container:
|
162
|
+
*
|
163
|
+
* ```html
|
164
|
+
* <div class="ck ck-word-count">
|
165
|
+
* <div class="ck-word-count__characters">Characters: 28</div>
|
166
|
+
* </div>
|
167
|
+
* ```
|
168
|
+
*/
|
169
|
+
displayWords?: boolean;
|
170
|
+
/**
|
171
|
+
* This option allows for hiding the character counter. The element obtained through
|
172
|
+
* {@link module:word-count/wordcount~WordCount#wordCountContainer} will only preserve
|
173
|
+
* the words part. Character counter is displayed by default when this configuration option is not defined.
|
174
|
+
*
|
175
|
+
* ```ts
|
176
|
+
* const wordCountConfig = {
|
177
|
+
* displayCharacters: false
|
178
|
+
* };
|
179
|
+
* ```
|
180
|
+
*
|
181
|
+
* The configuration above will result in the following container:
|
182
|
+
*
|
183
|
+
* ```html
|
184
|
+
* <div class="ck ck-word-count">
|
185
|
+
* <div class="ck-word-count__words">Words: 4</div>
|
186
|
+
* </div>
|
187
|
+
* ```
|
188
|
+
*/
|
189
|
+
displayCharacters?: boolean;
|
190
|
+
/**
|
191
|
+
* This configuration takes a function that is executed whenever the word count plugin updates its values.
|
192
|
+
* This function is called with one argument, which is an object with the `words` and `characters` keys containing
|
193
|
+
* the number of detected words and characters in the document.
|
194
|
+
*
|
195
|
+
* ```ts
|
196
|
+
* const wordCountConfig = {
|
197
|
+
* onUpdate: function( stats ) {
|
198
|
+
* doSthWithWordNumber( stats.words );
|
199
|
+
* doSthWithCharacterNumber( stats.characters );
|
200
|
+
* }
|
201
|
+
* };
|
202
|
+
* ```
|
203
|
+
*/
|
204
|
+
onUpdate?: (data: {
|
205
|
+
words: number;
|
206
|
+
characters: number;
|
207
|
+
}) => void;
|
208
|
+
/**
|
209
|
+
* Allows for providing the HTML element that the
|
210
|
+
* {@link module:word-count/wordcount~WordCount#wordCountContainer word count container} will be appended to automatically.
|
211
|
+
*
|
212
|
+
* ```ts
|
213
|
+
* const wordCountConfig = {
|
214
|
+
* container: document.getElementById( 'container-for-word-count' );
|
215
|
+
* };
|
216
|
+
* ```
|
217
|
+
*/
|
218
|
+
container?: HTMLElement;
|
219
|
+
}
|
220
|
+
declare module '@ckeditor/ckeditor5-core' {
|
221
|
+
interface PluginsMap {
|
222
|
+
[WordCount.pluginName]: WordCount;
|
223
|
+
}
|
224
|
+
interface EditorConfig {
|
225
|
+
/**
|
226
|
+
* The configuration of the word count feature.
|
227
|
+
* It is introduced by the {@link module:word-count/wordcount~WordCount} feature.
|
228
|
+
*
|
229
|
+
* Read more in {@link module:word-count/wordcount~WordCountConfig}.
|
230
|
+
*/
|
231
|
+
wordCount?: WordCountConfig;
|
232
|
+
}
|
233
|
+
}
|