@tiptap/extension-character-count 2.0.0-beta.18 → 2.0.0-beta.19

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.
@@ -1,7 +1,28 @@
1
1
  import { Extension } from '@tiptap/core';
2
- import { PluginKey } from 'prosemirror-state';
3
- export declare const pluginKey: PluginKey<any, any>;
2
+ import { Node as ProseMirrorNode } from 'prosemirror-model';
4
3
  export interface CharacterCountOptions {
5
- limit?: number;
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';
6
12
  }
7
- export declare const CharacterCount: Extension<CharacterCountOptions, any>;
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>;
@@ -5286,24 +5286,86 @@ PluginKey.prototype.get = function get (state) { return state.config.pluginsByKe
5286
5286
  // Get the plugin's state from an editor state.
5287
5287
  PluginKey.prototype.getState = function getState (state) { return state[this.key] };
5288
5288
 
5289
- const pluginKey = new PluginKey('characterLimit');
5290
5289
  const CharacterCount = core.Extension.create({
5291
5290
  name: 'characterCount',
5292
5291
  addOptions() {
5293
5292
  return {
5294
5293
  limit: 0,
5294
+ mode: 'textSize',
5295
+ };
5296
+ },
5297
+ addStorage() {
5298
+ return {
5299
+ characters: undefined,
5300
+ words: undefined,
5301
+ };
5302
+ },
5303
+ onBeforeCreate() {
5304
+ this.storage.characters = options => {
5305
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
5306
+ const mode = (options === null || options === void 0 ? void 0 : options.mode) || this.options.mode;
5307
+ if (mode === 'textSize') {
5308
+ const text = node.textBetween(0, node.content.size, undefined, ' ');
5309
+ return text.length;
5310
+ }
5311
+ return node.nodeSize;
5312
+ };
5313
+ this.storage.words = options => {
5314
+ const node = (options === null || options === void 0 ? void 0 : options.node) || this.editor.state.doc;
5315
+ const text = node.textBetween(0, node.content.size, undefined, ' ');
5316
+ const words = text
5317
+ .split(' ')
5318
+ .filter(word => word !== '');
5319
+ return words.length;
5295
5320
  };
5296
5321
  },
5297
5322
  addProseMirrorPlugins() {
5298
- const { options } = this;
5299
5323
  return [
5300
5324
  new Plugin({
5301
- key: pluginKey,
5302
- appendTransaction: (transactions, oldState, newState) => {
5303
- const length = newState.doc.content.size;
5304
- if (options.limit && length > options.limit) {
5305
- return newState.tr.insertText('', options.limit + 1, length);
5325
+ key: new PluginKey('characterCount'),
5326
+ filterTransaction: (transaction, state) => {
5327
+ var _a, _b, _c, _d, _e, _f;
5328
+ const limit = this.options.limit;
5329
+ // Nothing has changed or no limit is defined. Ignore it.
5330
+ if (!transaction.docChanged || limit === 0) {
5331
+ return true;
5332
+ }
5333
+ const oldSize = ((_b = (_a = this.storage).characters) === null || _b === void 0 ? void 0 : _b.call(_a, { node: state.doc })) || 0;
5334
+ const newSize = ((_d = (_c = this.storage).characters) === null || _d === void 0 ? void 0 : _d.call(_c, { node: transaction.doc })) || 0;
5335
+ // Everything is in the limit. Good.
5336
+ if (newSize <= limit) {
5337
+ return true;
5338
+ }
5339
+ // The limit has already been exceeded but will be reduced.
5340
+ if (oldSize > limit && newSize > limit && newSize <= oldSize) {
5341
+ return true;
5342
+ }
5343
+ // The limit has already been exceeded and will be increased further.
5344
+ if (oldSize > limit && newSize > limit && newSize > oldSize) {
5345
+ return false;
5346
+ }
5347
+ const isPaste = transaction.getMeta('paste');
5348
+ // Block all exceeding transactions that were not pasted.
5349
+ if (!isPaste) {
5350
+ return false;
5351
+ }
5352
+ // For pasted content, we try to remove the exceeding content.
5353
+ const pos = transaction.selection.$head.pos;
5354
+ const over = newSize - limit;
5355
+ const from = pos - over;
5356
+ const to = pos;
5357
+ // It’s probably a bad idea to mutate transactions within `filterTransaction`
5358
+ // but for now this is working fine.
5359
+ transaction.deleteRange(from, to);
5360
+ // In some situations, the limit will continue to be exceeded after trimming.
5361
+ // This happens e.g. when truncating within a complex node (e.g. table)
5362
+ // and ProseMirror has to close this node again.
5363
+ // If this is the case, we prevent the transaction completely.
5364
+ const updatedSize = ((_f = (_e = this.storage).characters) === null || _f === void 0 ? void 0 : _f.call(_e, { node: transaction.doc })) || 0;
5365
+ if (updatedSize > limit) {
5366
+ return false;
5306
5367
  }
5368
+ return true;
5307
5369
  },
5308
5370
  }),
5309
5371
  ];
@@ -5312,5 +5374,4 @@ const CharacterCount = core.Extension.create({
5312
5374
 
5313
5375
  exports.CharacterCount = CharacterCount;
5314
5376
  exports["default"] = CharacterCount;
5315
- exports.pluginKey = pluginKey;
5316
5377
  //# sourceMappingURL=tiptap-extension-character-count.cjs.js.map