@tiptap/extension-character-count 2.0.0-beta.21 → 2.0.0-beta.211

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,80 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/character-count.ts
2
+ var _core = require('@tiptap/core');
3
+ var _state = require('@tiptap/pm/state');
4
+ var CharacterCount = _core.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 ? void 0 : options.node) || this.editor.state.doc;
21
+ const mode = (options == null ? void 0 : options.mode) || this.options.mode;
22
+ if (mode === "textSize") {
23
+ const text = node.textBetween(0, node.content.size, void 0, " ");
24
+ return text.length;
25
+ }
26
+ return node.nodeSize;
27
+ };
28
+ this.storage.words = (options) => {
29
+ const node = (options == null ? 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 (0, _state.Plugin)({
38
+ key: new (0, _state.PluginKey)("characterCount"),
39
+ filterTransaction: (transaction, state) => {
40
+ const limit = this.options.limit;
41
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
42
+ return true;
43
+ }
44
+ const oldSize = this.storage.characters({ node: state.doc });
45
+ const newSize = this.storage.characters({ node: transaction.doc });
46
+ if (newSize <= limit) {
47
+ return true;
48
+ }
49
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) {
50
+ return true;
51
+ }
52
+ if (oldSize > limit && newSize > limit && newSize > oldSize) {
53
+ return false;
54
+ }
55
+ const isPaste = transaction.getMeta("paste");
56
+ if (!isPaste) {
57
+ return false;
58
+ }
59
+ const pos = transaction.selection.$head.pos;
60
+ const over = newSize - limit;
61
+ const from = pos - over;
62
+ const to = pos;
63
+ transaction.deleteRange(from, to);
64
+ const updatedSize = this.storage.characters({ node: transaction.doc });
65
+ if (updatedSize > limit) {
66
+ return false;
67
+ }
68
+ return true;
69
+ }
70
+ })
71
+ ];
72
+ }
73
+ });
74
+
75
+ // src/index.ts
76
+ var src_default = CharacterCount;
77
+
78
+
79
+
80
+ exports.CharacterCount = CharacterCount; exports.default = src_default;
@@ -1,28 +1,31 @@
1
- import { Extension } from '@tiptap/core';
2
- import { Node as ProseMirrorNode } from 'prosemirror-model';
3
- export interface CharacterCountOptions {
4
- /**
5
- * The maximum number of characters that should be allowed. Defaults to `0`.
6
- */
7
- limit: number;
8
- /**
9
- * The mode by which the size is calculated. Defaults to 'textSize'.
10
- */
11
- mode: 'textSize' | 'nodeSize';
12
- }
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>;
1
+ import { Extension } from '@tiptap/core';
2
+ import { Node } from '@tiptap/pm/model';
3
+
4
+ interface CharacterCountOptions {
5
+ /**
6
+ * The maximum number of characters that should be allowed. Defaults to `0`.
7
+ */
8
+ limit: number | null | undefined;
9
+ /**
10
+ * The mode by which the size is calculated. Defaults to 'textSize'.
11
+ */
12
+ mode: 'textSize' | 'nodeSize';
13
+ }
14
+ interface CharacterCountStorage {
15
+ /**
16
+ * Get the number of characters for the current document.
17
+ */
18
+ characters: (options?: {
19
+ node?: Node;
20
+ mode?: 'textSize' | 'nodeSize';
21
+ }) => number;
22
+ /**
23
+ * Get the number of words for the current document.
24
+ */
25
+ words: (options?: {
26
+ node?: Node;
27
+ }) => number;
28
+ }
29
+ declare const CharacterCount: Extension<CharacterCountOptions, CharacterCountStorage>;
30
+
31
+ export { CharacterCount, CharacterCountOptions, CharacterCountStorage, CharacterCount as default };
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
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
+ };
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 ? void 0 : options.node) || this.editor.state.doc;
21
+ const mode = (options == null ? void 0 : options.mode) || this.options.mode;
22
+ if (mode === "textSize") {
23
+ const text = node.textBetween(0, node.content.size, void 0, " ");
24
+ return text.length;
25
+ }
26
+ return node.nodeSize;
27
+ };
28
+ this.storage.words = (options) => {
29
+ const node = (options == null ? 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
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === void 0) {
42
+ return true;
43
+ }
44
+ const oldSize = this.storage.characters({ node: state.doc });
45
+ const newSize = this.storage.characters({ node: transaction.doc });
46
+ if (newSize <= limit) {
47
+ return true;
48
+ }
49
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) {
50
+ return true;
51
+ }
52
+ if (oldSize > limit && newSize > limit && newSize > oldSize) {
53
+ return false;
54
+ }
55
+ const isPaste = transaction.getMeta("paste");
56
+ if (!isPaste) {
57
+ return false;
58
+ }
59
+ const pos = transaction.selection.$head.pos;
60
+ const over = newSize - limit;
61
+ const from = pos - over;
62
+ const to = pos;
63
+ transaction.deleteRange(from, to);
64
+ const updatedSize = this.storage.characters({ node: transaction.doc });
65
+ if (updatedSize > limit) {
66
+ return false;
67
+ }
68
+ return true;
69
+ }
70
+ })
71
+ ];
72
+ }
73
+ });
74
+
75
+ // src/index.ts
76
+ var src_default = CharacterCount;
77
+ export {
78
+ CharacterCount,
79
+ src_default as default
80
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-character-count",
3
3
  "description": "font family extension for tiptap",
4
- "version": "2.0.0-beta.21",
4
+ "version": "2.0.0-beta.211",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -12,22 +12,46 @@
12
12
  "type": "github",
13
13
  "url": "https://github.com/sponsors/ueberdosis"
14
14
  },
15
- "main": "dist/tiptap-extension-character-count.cjs.js",
16
- "umd": "dist/tiptap-extension-character-count.umd.js",
17
- "module": "dist/tiptap-extension-character-count.esm.js",
18
- "types": "dist/packages/extension-character-count/src/index.d.ts",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "require": "./dist/index.cjs"
21
+ }
22
+ },
23
+ "main": "dist/index.cjs",
24
+ "module": "dist/index.js",
25
+ "types": "dist/index.d.ts",
19
26
  "files": [
20
27
  "src",
21
28
  "dist"
22
29
  ],
23
30
  "peerDependencies": {
24
- "@tiptap/core": "^2.0.0-beta.1",
25
- "@tiptap/extension-text-style": "^2.0.0-beta.1"
31
+ "@tiptap/core": "^2.0.0-beta.209",
32
+ "@tiptap/pm": "^2.0.0-beta.209"
33
+ },
34
+ "devDependencies": {
35
+ "@tiptap/core": "^2.0.0-beta.211",
36
+ "@tiptap/pm": "^2.0.0-beta.211"
26
37
  },
27
38
  "repository": {
28
39
  "type": "git",
29
40
  "url": "https://github.com/ueberdosis/tiptap",
30
41
  "directory": "packages/extension-character-count"
31
42
  },
32
- "gitHead": "914c75b3056cdbfc74c1ae585a36c5d13655595d"
43
+ "scripts": {
44
+ "build": "tsup"
45
+ },
46
+ "tsup": {
47
+ "entry": [
48
+ "src/index.ts"
49
+ ],
50
+ "dts": true,
51
+ "splitting": true,
52
+ "format": [
53
+ "esm",
54
+ "cjs"
55
+ ]
56
+ }
33
57
  }
@@ -1,33 +1,28 @@
1
1
  import { Extension } from '@tiptap/core'
2
- import { Plugin, PluginKey } from 'prosemirror-state'
3
- import { Node as ProseMirrorNode } from 'prosemirror-model'
2
+ import { Node as ProseMirrorNode } from '@tiptap/pm/model'
3
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
4
 
5
5
  export interface CharacterCountOptions {
6
6
  /**
7
7
  * The maximum number of characters that should be allowed. Defaults to `0`.
8
8
  */
9
- limit: number,
9
+ limit: number | null | undefined
10
10
  /**
11
11
  * The mode by which the size is calculated. Defaults to 'textSize'.
12
12
  */
13
- mode: 'textSize' | 'nodeSize',
13
+ mode: 'textSize' | 'nodeSize'
14
14
  }
15
15
 
16
16
  export interface CharacterCountStorage {
17
17
  /**
18
18
  * Get the number of characters for the current document.
19
19
  */
20
- characters?: (options?: {
21
- node?: ProseMirrorNode,
22
- mode?: 'textSize' | 'nodeSize',
23
- }) => number,
20
+ characters: (options?: { node?: ProseMirrorNode; mode?: 'textSize' | 'nodeSize' }) => number
24
21
 
25
22
  /**
26
23
  * Get the number of words for the current document.
27
24
  */
28
- words?: (options?: {
29
- node?: ProseMirrorNode,
30
- }) => number,
25
+ words: (options?: { node?: ProseMirrorNode }) => number
31
26
  }
32
27
 
33
28
  export const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({
@@ -35,15 +30,15 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
35
30
 
36
31
  addOptions() {
37
32
  return {
38
- limit: 0,
33
+ limit: null,
39
34
  mode: 'textSize',
40
35
  }
41
36
  },
42
37
 
43
38
  addStorage() {
44
39
  return {
45
- characters: undefined,
46
- words: undefined,
40
+ characters: () => 0,
41
+ words: () => 0,
47
42
  }
48
43
  },
49
44
 
@@ -64,9 +59,7 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
64
59
  this.storage.words = options => {
65
60
  const node = options?.node || this.editor.state.doc
66
61
  const text = node.textBetween(0, node.content.size, ' ', ' ')
67
- const words = text
68
- .split(' ')
69
- .filter(word => word !== '')
62
+ const words = text.split(' ').filter(word => word !== '')
70
63
 
71
64
  return words.length
72
65
  }
@@ -80,12 +73,12 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
80
73
  const limit = this.options.limit
81
74
 
82
75
  // Nothing has changed or no limit is defined. Ignore it.
83
- if (!transaction.docChanged || limit === 0) {
76
+ if (!transaction.docChanged || limit === 0 || limit === null || limit === undefined) {
84
77
  return true
85
78
  }
86
79
 
87
- const oldSize = this.storage.characters?.({ node: state.doc }) || 0
88
- const newSize = this.storage.characters?.({ node: transaction.doc }) || 0
80
+ const oldSize = this.storage.characters({ node: state.doc })
81
+ const newSize = this.storage.characters({ node: transaction.doc })
89
82
 
90
83
  // Everything is in the limit. Good.
91
84
  if (newSize <= limit) {
@@ -123,7 +116,7 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
123
116
  // This happens e.g. when truncating within a complex node (e.g. table)
124
117
  // and ProseMirror has to close this node again.
125
118
  // If this is the case, we prevent the transaction completely.
126
- const updatedSize = this.storage.characters?.({ node: transaction.doc }) || 0
119
+ const updatedSize = this.storage.characters({ node: transaction.doc })
127
120
 
128
121
  if (updatedSize > limit) {
129
122
  return false
package/LICENSE.md DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2021, überdosis GbR
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1,3 +0,0 @@
1
- import { CharacterCount } from './character-count';
2
- export * from './character-count';
3
- export default CharacterCount;