-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathcharacter-count.ts
137 lines (112 loc) · 3.77 KB
/
character-count.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { Extension } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'
import { Node as ProseMirrorNode } from 'prosemirror-model'
export interface CharacterCountOptions {
/**
* The maximum number of characters that should be allowed. Defaults to `0`.
*/
limit: number,
/**
* The mode by which the size is calculated. Defaults to 'textSize'.
*/
mode: 'textSize' | 'nodeSize',
}
export interface CharacterCountStorage {
/**
* Get the number of characters for the current document.
*/
characters?: (options: {
node?: ProseMirrorNode,
mode?: 'textSize' | 'nodeSize',
}) => number,
/**
* Get the number of words for the current document.
*/
words?: (options: {
node?: ProseMirrorNode,
}) => number,
}
export const CharacterCount = Extension.create<CharacterCountOptions, CharacterCountStorage>({
name: 'characterCount',
addOptions() {
return {
limit: 0,
mode: 'textSize',
}
},
addStorage() {
return {
characters: undefined,
words: undefined,
}
},
onBeforeCreate() {
this.storage.characters = options => {
const node = options?.node || this.editor.state.doc
const mode = options?.mode || this.options.mode
if (mode === 'textSize') {
const text = node.textBetween(0, node.content.size, undefined, ' ')
return text.length
}
return node.nodeSize
}
this.storage.words = options => {
const node = options?.node || this.editor.state.doc
const text = node.textBetween(0, node.content.size, ' ', ' ')
const words = text
.split(' ')
.filter(word => word !== '')
return words.length
}
},
addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('characterCount'),
filterTransaction: (transaction, state) => {
const limit = this.options.limit
// Nothing has changed or no limit is defined. Ignore it.
if (!transaction.docChanged || limit === 0) {
return true
}
const oldSize = this.storage.characters?.({ node: state.doc }) || 0
const newSize = this.storage.characters?.({ node: transaction.doc }) || 0
// Everything is in the limit. Good.
if (newSize <= limit) {
return true
}
// The limit has already been exceeded but will be reduced.
if (oldSize > limit && newSize > limit && newSize <= oldSize) {
return true
}
// The limit has already been exceeded and will be increased further.
if (oldSize > limit && newSize > limit && newSize > oldSize) {
return false
}
const isPaste = transaction.getMeta('paste')
// Block all exceeding transactions that were not pasted.
if (!isPaste) {
return false
}
// For pasted content, we try to remove the exceeding content.
const pos = transaction.selection.$head.pos
const over = newSize - limit
const from = pos - over
const to = pos
// It’s probably a bad idea to mutate transactions within `filterTransaction`
// but for now this is working fine.
transaction.deleteRange(from, to)
// In some situations, the limit will continue to be exceeded after trimming.
// This happens e.g. when truncating within a complex node (e.g. table)
// and ProseMirror has to close this node again.
// If this is the case, we prevent the transaction completely.
const updatedSize = this.storage.characters?.({ node: transaction.doc }) || 0
if (updatedSize > limit) {
return false
}
return true
},
}),
]
},
})