@rme-sdk/extension-count 1.1.0
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 +21 -0
- package/dist/_tsup-dts-rollup.d.cts +88 -0
- package/dist/_tsup-dts-rollup.d.ts +88 -0
- package/dist/rme-sdk-extension-count.cjs +244 -0
- package/dist/rme-sdk-extension-count.d.cts +3 -0
- package/dist/rme-sdk-extension-count.d.ts +3 -0
- package/dist/rme-sdk-extension-count.js +219 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 drl990114
|
|
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.
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { DecorationSet } from '@rme-sdk/pm/view';
|
|
2
|
+
import type { EditorState } from '@rme-sdk/core';
|
|
3
|
+
import type { Helper } from '@rme-sdk/core';
|
|
4
|
+
import { PlainExtension } from '@rme-sdk/core';
|
|
5
|
+
import { Plugin as Plugin_2 } from '@rme-sdk/pm/state';
|
|
6
|
+
import type { ProsemirrorNode } from '@rme-sdk/core';
|
|
7
|
+
import type { Static } from '@rme-sdk/core';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Count words or characters in your editor, and set a soft max length
|
|
11
|
+
*/
|
|
12
|
+
declare class CountExtension extends PlainExtension<CountOptions> {
|
|
13
|
+
get name(): "count";
|
|
14
|
+
/**
|
|
15
|
+
* Get the configured maximum characters/words.
|
|
16
|
+
*/
|
|
17
|
+
getCountMaximum(): Helper<number>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the count of characters in the document.
|
|
20
|
+
*
|
|
21
|
+
* @param state
|
|
22
|
+
*/
|
|
23
|
+
getCharacterCount(state?: EditorState): Helper<number>;
|
|
24
|
+
/**
|
|
25
|
+
* Get the count of words in the document.
|
|
26
|
+
*
|
|
27
|
+
* @param state
|
|
28
|
+
*/
|
|
29
|
+
getWordCount(state?: EditorState): Helper<number>;
|
|
30
|
+
/**
|
|
31
|
+
* Is the current number of characters/words valid in the current strategy.
|
|
32
|
+
*
|
|
33
|
+
* @param state
|
|
34
|
+
*/
|
|
35
|
+
isCountValid(state?: EditorState): Helper<boolean>;
|
|
36
|
+
protected createDecorationSet(state: EditorState): DecorationSet;
|
|
37
|
+
createExternalPlugins(): Plugin_2[];
|
|
38
|
+
}
|
|
39
|
+
export { CountExtension }
|
|
40
|
+
export { CountExtension as CountExtension_alias_1 }
|
|
41
|
+
|
|
42
|
+
declare interface CountOptions {
|
|
43
|
+
/**
|
|
44
|
+
* An optional soft limit. Text that exceeds this limit will be highlighted.
|
|
45
|
+
*
|
|
46
|
+
* @defaultValue -1
|
|
47
|
+
*/
|
|
48
|
+
maximum?: Static<number>;
|
|
49
|
+
/**
|
|
50
|
+
* The classname to use when highlighting text that exceed the given maximum.
|
|
51
|
+
*
|
|
52
|
+
* @defaultValue 'remirror-max-count-exceeded'
|
|
53
|
+
*/
|
|
54
|
+
maximumExceededClassName?: Static<string>;
|
|
55
|
+
/**
|
|
56
|
+
* The counting strategy to use. Either CountStrategy.CHARACTERS or CountStrategy.WORDS
|
|
57
|
+
*
|
|
58
|
+
* @defaultValue CountStrategy.CHARACTERS
|
|
59
|
+
*/
|
|
60
|
+
maximumStrategy?: Static<CountStrategy>;
|
|
61
|
+
}
|
|
62
|
+
export { CountOptions }
|
|
63
|
+
export { CountOptions as CountOptions_alias_1 }
|
|
64
|
+
|
|
65
|
+
declare enum CountStrategy {
|
|
66
|
+
CHARACTERS = "CHARACTERS",
|
|
67
|
+
WORDS = "WORDS"
|
|
68
|
+
}
|
|
69
|
+
export { CountStrategy }
|
|
70
|
+
export { CountStrategy as CountStrategy_alias_1 }
|
|
71
|
+
|
|
72
|
+
export declare function getCharacterExceededPosition({ doc }: EditorState, maxCharacters: number): number;
|
|
73
|
+
|
|
74
|
+
export declare function getTextLength(node: ProsemirrorNode): number;
|
|
75
|
+
|
|
76
|
+
export declare function getWordExceededPosition({ doc }: EditorState, maxWords: number): number;
|
|
77
|
+
|
|
78
|
+
export declare const WORDS_REGEX: RegExp;
|
|
79
|
+
|
|
80
|
+
export { }
|
|
81
|
+
|
|
82
|
+
declare global {
|
|
83
|
+
namespace Remirror {
|
|
84
|
+
interface AllExtensions {
|
|
85
|
+
count: CountExtension;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { DecorationSet } from '@rme-sdk/pm/view';
|
|
2
|
+
import type { EditorState } from '@rme-sdk/core';
|
|
3
|
+
import type { Helper } from '@rme-sdk/core';
|
|
4
|
+
import { PlainExtension } from '@rme-sdk/core';
|
|
5
|
+
import { Plugin as Plugin_2 } from '@rme-sdk/pm/state';
|
|
6
|
+
import type { ProsemirrorNode } from '@rme-sdk/core';
|
|
7
|
+
import type { Static } from '@rme-sdk/core';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Count words or characters in your editor, and set a soft max length
|
|
11
|
+
*/
|
|
12
|
+
declare class CountExtension extends PlainExtension<CountOptions> {
|
|
13
|
+
get name(): "count";
|
|
14
|
+
/**
|
|
15
|
+
* Get the configured maximum characters/words.
|
|
16
|
+
*/
|
|
17
|
+
getCountMaximum(): Helper<number>;
|
|
18
|
+
/**
|
|
19
|
+
* Get the count of characters in the document.
|
|
20
|
+
*
|
|
21
|
+
* @param state
|
|
22
|
+
*/
|
|
23
|
+
getCharacterCount(state?: EditorState): Helper<number>;
|
|
24
|
+
/**
|
|
25
|
+
* Get the count of words in the document.
|
|
26
|
+
*
|
|
27
|
+
* @param state
|
|
28
|
+
*/
|
|
29
|
+
getWordCount(state?: EditorState): Helper<number>;
|
|
30
|
+
/**
|
|
31
|
+
* Is the current number of characters/words valid in the current strategy.
|
|
32
|
+
*
|
|
33
|
+
* @param state
|
|
34
|
+
*/
|
|
35
|
+
isCountValid(state?: EditorState): Helper<boolean>;
|
|
36
|
+
protected createDecorationSet(state: EditorState): DecorationSet;
|
|
37
|
+
createExternalPlugins(): Plugin_2[];
|
|
38
|
+
}
|
|
39
|
+
export { CountExtension }
|
|
40
|
+
export { CountExtension as CountExtension_alias_1 }
|
|
41
|
+
|
|
42
|
+
declare interface CountOptions {
|
|
43
|
+
/**
|
|
44
|
+
* An optional soft limit. Text that exceeds this limit will be highlighted.
|
|
45
|
+
*
|
|
46
|
+
* @defaultValue -1
|
|
47
|
+
*/
|
|
48
|
+
maximum?: Static<number>;
|
|
49
|
+
/**
|
|
50
|
+
* The classname to use when highlighting text that exceed the given maximum.
|
|
51
|
+
*
|
|
52
|
+
* @defaultValue 'remirror-max-count-exceeded'
|
|
53
|
+
*/
|
|
54
|
+
maximumExceededClassName?: Static<string>;
|
|
55
|
+
/**
|
|
56
|
+
* The counting strategy to use. Either CountStrategy.CHARACTERS or CountStrategy.WORDS
|
|
57
|
+
*
|
|
58
|
+
* @defaultValue CountStrategy.CHARACTERS
|
|
59
|
+
*/
|
|
60
|
+
maximumStrategy?: Static<CountStrategy>;
|
|
61
|
+
}
|
|
62
|
+
export { CountOptions }
|
|
63
|
+
export { CountOptions as CountOptions_alias_1 }
|
|
64
|
+
|
|
65
|
+
declare enum CountStrategy {
|
|
66
|
+
CHARACTERS = "CHARACTERS",
|
|
67
|
+
WORDS = "WORDS"
|
|
68
|
+
}
|
|
69
|
+
export { CountStrategy }
|
|
70
|
+
export { CountStrategy as CountStrategy_alias_1 }
|
|
71
|
+
|
|
72
|
+
export declare function getCharacterExceededPosition({ doc }: EditorState, maxCharacters: number): number;
|
|
73
|
+
|
|
74
|
+
export declare function getTextLength(node: ProsemirrorNode): number;
|
|
75
|
+
|
|
76
|
+
export declare function getWordExceededPosition({ doc }: EditorState, maxWords: number): number;
|
|
77
|
+
|
|
78
|
+
export declare const WORDS_REGEX: RegExp;
|
|
79
|
+
|
|
80
|
+
export { }
|
|
81
|
+
|
|
82
|
+
declare global {
|
|
83
|
+
namespace Remirror {
|
|
84
|
+
interface AllExtensions {
|
|
85
|
+
count: CountExtension;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
8
|
+
var __typeError = (msg) => {
|
|
9
|
+
throw TypeError(msg);
|
|
10
|
+
};
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
13
|
+
var __export = (target, all) => {
|
|
14
|
+
for (var name in all)
|
|
15
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
var __decoratorStart = (base) => {
|
|
27
|
+
var _a2;
|
|
28
|
+
return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
|
|
29
|
+
};
|
|
30
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
31
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
32
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
33
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
34
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
35
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
36
|
+
return value;
|
|
37
|
+
};
|
|
38
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
39
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
40
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
41
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
42
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
43
|
+
return __privateGet(this, extra);
|
|
44
|
+
}, set [name](x) {
|
|
45
|
+
return __privateSet(this, extra, x);
|
|
46
|
+
} }, name));
|
|
47
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
48
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
49
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
50
|
+
if (k) {
|
|
51
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
52
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
53
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
54
|
+
}
|
|
55
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
56
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
57
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
58
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
59
|
+
}
|
|
60
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
61
|
+
};
|
|
62
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
63
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
64
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
65
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
66
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
67
|
+
|
|
68
|
+
// src/index.ts
|
|
69
|
+
var src_exports = {};
|
|
70
|
+
__export(src_exports, {
|
|
71
|
+
CountExtension: () => CountExtension,
|
|
72
|
+
CountStrategy: () => CountStrategy
|
|
73
|
+
});
|
|
74
|
+
module.exports = __toCommonJS(src_exports);
|
|
75
|
+
|
|
76
|
+
// src/count-extension.ts
|
|
77
|
+
var import_core2 = require("@rme-sdk/core");
|
|
78
|
+
var import_state = require("@rme-sdk/pm/state");
|
|
79
|
+
var import_view = require("@rme-sdk/pm/view");
|
|
80
|
+
|
|
81
|
+
// src/count-utils.ts
|
|
82
|
+
var import_core = require("@rme-sdk/core");
|
|
83
|
+
var WORDS_REGEX = /\S+/g;
|
|
84
|
+
function getTextLength(node) {
|
|
85
|
+
if (node.type.isTextblock) {
|
|
86
|
+
return 1;
|
|
87
|
+
}
|
|
88
|
+
if (node.type.isText) {
|
|
89
|
+
return node.textBetween(0, node.nodeSize).length;
|
|
90
|
+
}
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
function getCharacterExceededPosition({ doc }, maxCharacters) {
|
|
94
|
+
let count = 0;
|
|
95
|
+
let foundPos = 0;
|
|
96
|
+
doc.nodesBetween(0, doc.nodeSize - 2, (node, pos) => {
|
|
97
|
+
if (foundPos > 0) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const nodeTextLength = getTextLength(node);
|
|
101
|
+
if (count + nodeTextLength > maxCharacters) {
|
|
102
|
+
foundPos = pos + 1 + (maxCharacters - count);
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
count += nodeTextLength;
|
|
106
|
+
return true;
|
|
107
|
+
});
|
|
108
|
+
return foundPos;
|
|
109
|
+
}
|
|
110
|
+
function getWordExceededPosition({ doc }, maxWords) {
|
|
111
|
+
let count = 0;
|
|
112
|
+
let foundPos = 0;
|
|
113
|
+
doc.nodesBetween(0, doc.nodeSize - 2, (node, pos) => {
|
|
114
|
+
var _a2;
|
|
115
|
+
if (foundPos > 0) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
if (!node.type.isText) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
const text = node.textBetween(0, node.nodeSize);
|
|
122
|
+
const matches = (0, import_core.findMatches)(text, WORDS_REGEX);
|
|
123
|
+
if (count + matches.length > maxWords) {
|
|
124
|
+
const index = maxWords - count;
|
|
125
|
+
const word = matches[index];
|
|
126
|
+
foundPos = pos + ((_a2 = word == null ? void 0 : word.index) != null ? _a2 : 0);
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
count += matches.length;
|
|
130
|
+
return true;
|
|
131
|
+
});
|
|
132
|
+
return foundPos;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/count-extension.ts
|
|
136
|
+
var CountStrategy = /* @__PURE__ */ ((CountStrategy2) => {
|
|
137
|
+
CountStrategy2["CHARACTERS"] = "CHARACTERS";
|
|
138
|
+
CountStrategy2["WORDS"] = "WORDS";
|
|
139
|
+
return CountStrategy2;
|
|
140
|
+
})(CountStrategy || {});
|
|
141
|
+
var _isCountValid_dec, _getWordCount_dec, _getCharacterCount_dec, _getCountMaximum_dec, _a, _CountExtension_decorators, _init;
|
|
142
|
+
_CountExtension_decorators = [(0, import_core2.extension)({
|
|
143
|
+
defaultOptions: {
|
|
144
|
+
maximum: -1,
|
|
145
|
+
maximumExceededClassName: "remirror-max-count-exceeded",
|
|
146
|
+
maximumStrategy: "CHARACTERS" /* CHARACTERS */
|
|
147
|
+
},
|
|
148
|
+
staticKeys: ["maximum", "maximumStrategy", "maximumExceededClassName"]
|
|
149
|
+
})];
|
|
150
|
+
var CountExtension = class extends (_a = import_core2.PlainExtension, _getCountMaximum_dec = [(0, import_core2.helper)()], _getCharacterCount_dec = [(0, import_core2.helper)()], _getWordCount_dec = [(0, import_core2.helper)()], _isCountValid_dec = [(0, import_core2.helper)()], _a) {
|
|
151
|
+
constructor() {
|
|
152
|
+
super(...arguments);
|
|
153
|
+
__runInitializers(_init, 5, this);
|
|
154
|
+
}
|
|
155
|
+
get name() {
|
|
156
|
+
return "count";
|
|
157
|
+
}
|
|
158
|
+
getCountMaximum() {
|
|
159
|
+
return this.options.maximum;
|
|
160
|
+
}
|
|
161
|
+
getCharacterCount(state = this.store.getState()) {
|
|
162
|
+
let count = 0;
|
|
163
|
+
state.doc.nodesBetween(0, state.doc.nodeSize - 2, (node) => {
|
|
164
|
+
count += getTextLength(node);
|
|
165
|
+
return true;
|
|
166
|
+
});
|
|
167
|
+
return Math.max(count - 1, 0);
|
|
168
|
+
}
|
|
169
|
+
getWordCount(state = this.store.getState()) {
|
|
170
|
+
const text = this.store.helpers.getText({ lineBreakDivider: " ", state });
|
|
171
|
+
return (0, import_core2.findMatches)(text, WORDS_REGEX).length;
|
|
172
|
+
}
|
|
173
|
+
isCountValid(state = this.store.getState()) {
|
|
174
|
+
const { maximumStrategy, maximum } = this.options;
|
|
175
|
+
if (maximum < 1) {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
if (maximumStrategy === "CHARACTERS" /* CHARACTERS */) {
|
|
179
|
+
const count = this.store.helpers.getCharacterCount(state);
|
|
180
|
+
return count <= maximum;
|
|
181
|
+
}
|
|
182
|
+
return this.store.helpers.getWordCount(state) <= maximum;
|
|
183
|
+
}
|
|
184
|
+
createDecorationSet(state) {
|
|
185
|
+
const { maximum = -1, maximumStrategy, maximumExceededClassName } = this.options;
|
|
186
|
+
const isCharacterCountStrategy = maximumStrategy === "CHARACTERS" /* CHARACTERS */;
|
|
187
|
+
const posStrategy = isCharacterCountStrategy ? getCharacterExceededPosition : getWordExceededPosition;
|
|
188
|
+
const pos = posStrategy(state, maximum);
|
|
189
|
+
return import_view.DecorationSet.create(state.doc, [
|
|
190
|
+
import_view.Decoration.inline(pos, state.doc.nodeSize - 2, {
|
|
191
|
+
class: maximumExceededClassName
|
|
192
|
+
})
|
|
193
|
+
]);
|
|
194
|
+
}
|
|
195
|
+
createExternalPlugins() {
|
|
196
|
+
const { maximum } = this.options;
|
|
197
|
+
const plugin = new import_state.Plugin({
|
|
198
|
+
state: {
|
|
199
|
+
init: (_, state) => {
|
|
200
|
+
if (this.isCountValid(state)) {
|
|
201
|
+
return {
|
|
202
|
+
decorationSet: import_view.DecorationSet.empty
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
decorationSet: this.createDecorationSet(state)
|
|
207
|
+
};
|
|
208
|
+
},
|
|
209
|
+
apply: (tr, pluginState, _, state) => {
|
|
210
|
+
if (!tr.docChanged || maximum < 1) {
|
|
211
|
+
return pluginState;
|
|
212
|
+
}
|
|
213
|
+
if (this.isCountValid(state)) {
|
|
214
|
+
return {
|
|
215
|
+
decorationSet: import_view.DecorationSet.empty
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
decorationSet: this.createDecorationSet(state)
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
props: {
|
|
224
|
+
decorations(state) {
|
|
225
|
+
var _a2, _b;
|
|
226
|
+
return (_b = (_a2 = plugin.getState(state)) == null ? void 0 : _a2.decorationSet) != null ? _b : null;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
return [plugin];
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
_init = __decoratorStart(_a);
|
|
234
|
+
__decorateElement(_init, 1, "getCountMaximum", _getCountMaximum_dec, CountExtension);
|
|
235
|
+
__decorateElement(_init, 1, "getCharacterCount", _getCharacterCount_dec, CountExtension);
|
|
236
|
+
__decorateElement(_init, 1, "getWordCount", _getWordCount_dec, CountExtension);
|
|
237
|
+
__decorateElement(_init, 1, "isCountValid", _isCountValid_dec, CountExtension);
|
|
238
|
+
CountExtension = __decorateElement(_init, 0, "CountExtension", _CountExtension_decorators, CountExtension);
|
|
239
|
+
__runInitializers(_init, 1, CountExtension);
|
|
240
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
241
|
+
0 && (module.exports = {
|
|
242
|
+
CountExtension,
|
|
243
|
+
CountStrategy
|
|
244
|
+
});
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
5
|
+
var __typeError = (msg) => {
|
|
6
|
+
throw TypeError(msg);
|
|
7
|
+
};
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
10
|
+
var __decoratorStart = (base) => {
|
|
11
|
+
var _a2;
|
|
12
|
+
return [, , , __create((_a2 = base == null ? void 0 : base[__knownSymbol("metadata")]) != null ? _a2 : null)];
|
|
13
|
+
};
|
|
14
|
+
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
15
|
+
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
16
|
+
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
17
|
+
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
18
|
+
var __runInitializers = (array, flags, self, value) => {
|
|
19
|
+
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) flags & 1 ? fns[i].call(self) : value = fns[i].call(self, value);
|
|
20
|
+
return value;
|
|
21
|
+
};
|
|
22
|
+
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
23
|
+
var fn, it, done, ctx, access, k = flags & 7, s = !!(flags & 8), p = !!(flags & 16);
|
|
24
|
+
var j = k > 3 ? array.length + 1 : k ? s ? 1 : 2 : 0, key = __decoratorStrings[k + 5];
|
|
25
|
+
var initializers = k > 3 && (array[j - 1] = []), extraInitializers = array[j] || (array[j] = []);
|
|
26
|
+
var desc = k && (!p && !s && (target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(k < 4 ? target : { get [name]() {
|
|
27
|
+
return __privateGet(this, extra);
|
|
28
|
+
}, set [name](x) {
|
|
29
|
+
return __privateSet(this, extra, x);
|
|
30
|
+
} }, name));
|
|
31
|
+
k ? p && k < 4 && __name(extra, (k > 2 ? "set " : k > 1 ? "get " : "") + name) : __name(target, name);
|
|
32
|
+
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
33
|
+
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
34
|
+
if (k) {
|
|
35
|
+
ctx.static = s, ctx.private = p, access = ctx.access = { has: p ? (x) => __privateIn(target, x) : (x) => name in x };
|
|
36
|
+
if (k ^ 3) access.get = p ? (x) => (k ^ 1 ? __privateGet : __privateMethod)(x, target, k ^ 4 ? extra : desc.get) : (x) => x[name];
|
|
37
|
+
if (k > 2) access.set = p ? (x, y) => __privateSet(x, target, y, k ^ 4 ? extra : desc.set) : (x, y) => x[name] = y;
|
|
38
|
+
}
|
|
39
|
+
it = (0, decorators[i])(k ? k < 4 ? p ? extra : desc[key] : k > 4 ? void 0 : { get: desc.get, set: desc.set } : target, ctx), done._ = 1;
|
|
40
|
+
if (k ^ 4 || it === void 0) __expectFn(it) && (k > 4 ? initializers.unshift(it) : k ? p ? extra = it : desc[key] = it : target = it);
|
|
41
|
+
else if (typeof it !== "object" || it === null) __typeError("Object expected");
|
|
42
|
+
else __expectFn(fn = it.get) && (desc.get = fn), __expectFn(fn = it.set) && (desc.set = fn), __expectFn(fn = it.init) && initializers.unshift(fn);
|
|
43
|
+
}
|
|
44
|
+
return k || __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
45
|
+
};
|
|
46
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
47
|
+
var __privateIn = (member, obj) => Object(obj) !== obj ? __typeError('Cannot use the "in" operator on this value') : member.has(obj);
|
|
48
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
49
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
|
|
50
|
+
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
|
|
51
|
+
|
|
52
|
+
// src/count-extension.ts
|
|
53
|
+
import { extension, findMatches as findMatches2, helper, PlainExtension } from "@rme-sdk/core";
|
|
54
|
+
import { Plugin } from "@rme-sdk/pm/state";
|
|
55
|
+
import { Decoration, DecorationSet } from "@rme-sdk/pm/view";
|
|
56
|
+
|
|
57
|
+
// src/count-utils.ts
|
|
58
|
+
import { findMatches } from "@rme-sdk/core";
|
|
59
|
+
var WORDS_REGEX = /\S+/g;
|
|
60
|
+
function getTextLength(node) {
|
|
61
|
+
if (node.type.isTextblock) {
|
|
62
|
+
return 1;
|
|
63
|
+
}
|
|
64
|
+
if (node.type.isText) {
|
|
65
|
+
return node.textBetween(0, node.nodeSize).length;
|
|
66
|
+
}
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
function getCharacterExceededPosition({ doc }, maxCharacters) {
|
|
70
|
+
let count = 0;
|
|
71
|
+
let foundPos = 0;
|
|
72
|
+
doc.nodesBetween(0, doc.nodeSize - 2, (node, pos) => {
|
|
73
|
+
if (foundPos > 0) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const nodeTextLength = getTextLength(node);
|
|
77
|
+
if (count + nodeTextLength > maxCharacters) {
|
|
78
|
+
foundPos = pos + 1 + (maxCharacters - count);
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
count += nodeTextLength;
|
|
82
|
+
return true;
|
|
83
|
+
});
|
|
84
|
+
return foundPos;
|
|
85
|
+
}
|
|
86
|
+
function getWordExceededPosition({ doc }, maxWords) {
|
|
87
|
+
let count = 0;
|
|
88
|
+
let foundPos = 0;
|
|
89
|
+
doc.nodesBetween(0, doc.nodeSize - 2, (node, pos) => {
|
|
90
|
+
var _a2;
|
|
91
|
+
if (foundPos > 0) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
if (!node.type.isText) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
const text = node.textBetween(0, node.nodeSize);
|
|
98
|
+
const matches = findMatches(text, WORDS_REGEX);
|
|
99
|
+
if (count + matches.length > maxWords) {
|
|
100
|
+
const index = maxWords - count;
|
|
101
|
+
const word = matches[index];
|
|
102
|
+
foundPos = pos + ((_a2 = word == null ? void 0 : word.index) != null ? _a2 : 0);
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
count += matches.length;
|
|
106
|
+
return true;
|
|
107
|
+
});
|
|
108
|
+
return foundPos;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/count-extension.ts
|
|
112
|
+
var CountStrategy = /* @__PURE__ */ ((CountStrategy2) => {
|
|
113
|
+
CountStrategy2["CHARACTERS"] = "CHARACTERS";
|
|
114
|
+
CountStrategy2["WORDS"] = "WORDS";
|
|
115
|
+
return CountStrategy2;
|
|
116
|
+
})(CountStrategy || {});
|
|
117
|
+
var _isCountValid_dec, _getWordCount_dec, _getCharacterCount_dec, _getCountMaximum_dec, _a, _CountExtension_decorators, _init;
|
|
118
|
+
_CountExtension_decorators = [extension({
|
|
119
|
+
defaultOptions: {
|
|
120
|
+
maximum: -1,
|
|
121
|
+
maximumExceededClassName: "remirror-max-count-exceeded",
|
|
122
|
+
maximumStrategy: "CHARACTERS" /* CHARACTERS */
|
|
123
|
+
},
|
|
124
|
+
staticKeys: ["maximum", "maximumStrategy", "maximumExceededClassName"]
|
|
125
|
+
})];
|
|
126
|
+
var CountExtension = class extends (_a = PlainExtension, _getCountMaximum_dec = [helper()], _getCharacterCount_dec = [helper()], _getWordCount_dec = [helper()], _isCountValid_dec = [helper()], _a) {
|
|
127
|
+
constructor() {
|
|
128
|
+
super(...arguments);
|
|
129
|
+
__runInitializers(_init, 5, this);
|
|
130
|
+
}
|
|
131
|
+
get name() {
|
|
132
|
+
return "count";
|
|
133
|
+
}
|
|
134
|
+
getCountMaximum() {
|
|
135
|
+
return this.options.maximum;
|
|
136
|
+
}
|
|
137
|
+
getCharacterCount(state = this.store.getState()) {
|
|
138
|
+
let count = 0;
|
|
139
|
+
state.doc.nodesBetween(0, state.doc.nodeSize - 2, (node) => {
|
|
140
|
+
count += getTextLength(node);
|
|
141
|
+
return true;
|
|
142
|
+
});
|
|
143
|
+
return Math.max(count - 1, 0);
|
|
144
|
+
}
|
|
145
|
+
getWordCount(state = this.store.getState()) {
|
|
146
|
+
const text = this.store.helpers.getText({ lineBreakDivider: " ", state });
|
|
147
|
+
return findMatches2(text, WORDS_REGEX).length;
|
|
148
|
+
}
|
|
149
|
+
isCountValid(state = this.store.getState()) {
|
|
150
|
+
const { maximumStrategy, maximum } = this.options;
|
|
151
|
+
if (maximum < 1) {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
if (maximumStrategy === "CHARACTERS" /* CHARACTERS */) {
|
|
155
|
+
const count = this.store.helpers.getCharacterCount(state);
|
|
156
|
+
return count <= maximum;
|
|
157
|
+
}
|
|
158
|
+
return this.store.helpers.getWordCount(state) <= maximum;
|
|
159
|
+
}
|
|
160
|
+
createDecorationSet(state) {
|
|
161
|
+
const { maximum = -1, maximumStrategy, maximumExceededClassName } = this.options;
|
|
162
|
+
const isCharacterCountStrategy = maximumStrategy === "CHARACTERS" /* CHARACTERS */;
|
|
163
|
+
const posStrategy = isCharacterCountStrategy ? getCharacterExceededPosition : getWordExceededPosition;
|
|
164
|
+
const pos = posStrategy(state, maximum);
|
|
165
|
+
return DecorationSet.create(state.doc, [
|
|
166
|
+
Decoration.inline(pos, state.doc.nodeSize - 2, {
|
|
167
|
+
class: maximumExceededClassName
|
|
168
|
+
})
|
|
169
|
+
]);
|
|
170
|
+
}
|
|
171
|
+
createExternalPlugins() {
|
|
172
|
+
const { maximum } = this.options;
|
|
173
|
+
const plugin = new Plugin({
|
|
174
|
+
state: {
|
|
175
|
+
init: (_, state) => {
|
|
176
|
+
if (this.isCountValid(state)) {
|
|
177
|
+
return {
|
|
178
|
+
decorationSet: DecorationSet.empty
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
decorationSet: this.createDecorationSet(state)
|
|
183
|
+
};
|
|
184
|
+
},
|
|
185
|
+
apply: (tr, pluginState, _, state) => {
|
|
186
|
+
if (!tr.docChanged || maximum < 1) {
|
|
187
|
+
return pluginState;
|
|
188
|
+
}
|
|
189
|
+
if (this.isCountValid(state)) {
|
|
190
|
+
return {
|
|
191
|
+
decorationSet: DecorationSet.empty
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
decorationSet: this.createDecorationSet(state)
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
props: {
|
|
200
|
+
decorations(state) {
|
|
201
|
+
var _a2, _b;
|
|
202
|
+
return (_b = (_a2 = plugin.getState(state)) == null ? void 0 : _a2.decorationSet) != null ? _b : null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
return [plugin];
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
_init = __decoratorStart(_a);
|
|
210
|
+
__decorateElement(_init, 1, "getCountMaximum", _getCountMaximum_dec, CountExtension);
|
|
211
|
+
__decorateElement(_init, 1, "getCharacterCount", _getCharacterCount_dec, CountExtension);
|
|
212
|
+
__decorateElement(_init, 1, "getWordCount", _getWordCount_dec, CountExtension);
|
|
213
|
+
__decorateElement(_init, 1, "isCountValid", _isCountValid_dec, CountExtension);
|
|
214
|
+
CountExtension = __decorateElement(_init, 0, "CountExtension", _CountExtension_decorators, CountExtension);
|
|
215
|
+
__runInitializers(_init, 1, CountExtension);
|
|
216
|
+
export {
|
|
217
|
+
CountExtension,
|
|
218
|
+
CountStrategy
|
|
219
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rme-sdk/extension-count",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Count characters or words in your editor, and set a soft max length",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"remirror",
|
|
7
|
+
"extension"
|
|
8
|
+
],
|
|
9
|
+
"homepage": "https://github.com/remirror/remirror/tree/HEAD/packages/remirror__extension-count",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/remirror/remirror.git",
|
|
13
|
+
"directory": "packages/remirror__extension-count"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"contributors": [
|
|
17
|
+
"Will Hawker <w.hawker@hotmail.co.uk>"
|
|
18
|
+
],
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/rme-sdk-extension-count.d.ts",
|
|
24
|
+
"import": "./dist/rme-sdk-extension-count.js",
|
|
25
|
+
"require": "./dist/rme-sdk-extension-count.cjs"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"main": "./dist/rme-sdk-extension-count.cjs",
|
|
30
|
+
"module": "./dist/rme-sdk-extension-count.js",
|
|
31
|
+
"types": "./dist/rme-sdk-extension-count.d.ts",
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"dist-types"
|
|
35
|
+
],
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@babel/runtime": "^7.27.0",
|
|
38
|
+
"@rme-sdk/core": "1.1.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rme-sdk/pm": "1.1.0",
|
|
42
|
+
"@rme-sdk/cli": "1.1.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"@rme-sdk/pm": "1.1.0"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"@rme-sdk": {
|
|
51
|
+
"sizeLimit": "10 KB"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "remirror-cli build"
|
|
55
|
+
}
|
|
56
|
+
}
|