-
Notifications
You must be signed in to change notification settings - Fork 17
/
languagetool.js
302 lines (302 loc) · 10.7 KB
/
languagetool.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { Extension } from '@tiptap/core'
import { Decoration, DecorationSet } from 'prosemirror-view'
import { Plugin, PluginKey } from 'prosemirror-state'
import { debounce } from 'lodash'
import { v4 as uuidv4 } from 'uuid'
import { Dexie } from 'dexie'
// *************** OVER: TYPES *****************
let editorView
let decorationSet
let db
let extensionDocId
let apiUrl = ''
let textNodesWithPosition = []
let match = undefined
let proofReadInitially = false
export var LanguageToolHelpingWords
;(function (LanguageToolHelpingWords) {
LanguageToolHelpingWords['LanguageToolTransactionName'] = 'languageToolTransaction'
LanguageToolHelpingWords['MatchUpdatedTransactionName'] = 'matchUpdated'
LanguageToolHelpingWords['LoadingTransactionName'] = 'languageToolLoading'
})(LanguageToolHelpingWords || (LanguageToolHelpingWords = {}))
const dispatch = (tr) => editorView.dispatch(tr)
const updateMatch = (m) => {
if (m) match = m
else match = undefined
editorView.dispatch(editorView.state.tr.setMeta('matchUpdated', true))
}
const selectElementText = (el) => {
const range = document.createRange()
range.selectNode(el)
const sel = window.getSelection()
sel === null || sel === void 0 ? void 0 : sel.removeAllRanges()
sel === null || sel === void 0 ? void 0 : sel.addRange(range)
}
const mouseEnterEventListener = (e) => {
if (!e.target) return
selectElementText(e.target)
const matchString = e.target.getAttribute('match')
if (matchString) updateMatch(JSON.parse(matchString))
else updateMatch()
}
const mouseLeaveEventListener = () => updateMatch()
const addEventListenersToDecorations = () => {
const decos = document.querySelectorAll('span.lt')
if (decos.length) {
decos.forEach((el) => {
el.addEventListener('click', mouseEnterEventListener)
el.addEventListener('mouseleave', mouseLeaveEventListener)
})
}
}
export function changedDescendants(old, cur, offset, f) {
const oldSize = old.childCount,
curSize = cur.childCount
outer: for (let i = 0, j = 0; i < curSize; i++) {
const child = cur.child(i)
for (let scan = j, e = Math.min(oldSize, i + 3); scan < e; scan++) {
if (old.child(scan) === child) {
j = scan + 1
offset += child.nodeSize
continue outer
}
}
f(child, offset, cur)
if (j < oldSize && old.child(j).sameMarkup(child)) changedDescendants(old.child(j), child, offset + 1, f)
else child.nodesBetween(0, child.content.size, f, offset + 1)
offset += child.nodeSize
}
}
const gimmeDecoration = (from, to, match) =>
Decoration.inline(from, to, {
class: `lt lt-${match.rule.issueType}`,
nodeName: 'span',
match: JSON.stringify(match),
uuid: uuidv4(),
})
const proofreadNodeAndUpdateItsDecorations = async (node, offset, cur) => {
if (editorView === null || editorView === void 0 ? void 0 : editorView.state)
dispatch(editorView.state.tr.setMeta(LanguageToolHelpingWords.LoadingTransactionName, true))
const ltRes = await (
await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body: `text=${encodeURIComponent(node.textContent)}&language=auto&enabledOnly=false`,
})
).json()
decorationSet = decorationSet.remove(decorationSet.find(offset, offset + node.nodeSize))
const nodeSpecificDecorations = []
for (const match of ltRes.matches) {
const from = match.offset + offset
const to = from + match.length
if (extensionDocId) {
const content = editorView.state.doc.textBetween(from, to)
const result = await db.ignoredWords.get({ value: content, documentId: extensionDocId })
if (!result) nodeSpecificDecorations.push(gimmeDecoration(from, to, match))
} else {
nodeSpecificDecorations.push(gimmeDecoration(from, to, match))
}
}
decorationSet = decorationSet.add(cur, nodeSpecificDecorations)
if (editorView) dispatch(editorView.state.tr.setMeta(LanguageToolHelpingWords.LanguageToolTransactionName, true))
}
const debouncedProofreadNodeAndUpdateItsDecorations = debounce(proofreadNodeAndUpdateItsDecorations, 500)
const moreThan500Words = (s) => s.trim().split(/\s+/).length >= 500
const getMatchAndSetDecorations = async (doc, text, originalFrom) => {
const ltRes = await (
await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
},
body: `text=${encodeURIComponent(text)}&language=auto&enabledOnly=false`,
})
).json()
const { matches } = ltRes
const decorations = []
for (const match of matches) {
const from = match.offset + originalFrom
const to = from + match.length
if (extensionDocId) {
const content = doc.textBetween(from, to)
const result = await db.ignoredWords.get({ value: content })
if (!result) decorations.push(gimmeDecoration(from, to, match))
} else {
decorations.push(gimmeDecoration(from, to, match))
}
}
decorationSet = decorationSet.remove(decorationSet.find(originalFrom, originalFrom + text.length))
decorationSet = decorationSet.add(doc, decorations)
if (editorView) dispatch(editorView.state.tr.setMeta(LanguageToolHelpingWords.LanguageToolTransactionName, true))
setTimeout(addEventListenersToDecorations)
}
const proofreadAndDecorateWholeDoc = async (doc, url) => {
apiUrl = url
textNodesWithPosition = []
let index = 0
doc === null || doc === void 0
? void 0
: doc.descendants((node, pos) => {
if (node.isText) {
if (textNodesWithPosition[index]) {
const text = textNodesWithPosition[index].text + node.text
const from = textNodesWithPosition[index].from
const to = from + text.length
textNodesWithPosition[index] = { text, from, to }
} else {
const text = node.text
const from = pos
const to = pos + text.length
textNodesWithPosition[index] = { text, from, to }
}
} else {
index += 1
}
})
textNodesWithPosition = textNodesWithPosition.filter(Boolean)
let finalText = ''
const chunksOf500Words = []
let upperFrom = 0
let newDataSet = true
let lastPos = 1
for (const { text, from, to } of textNodesWithPosition) {
if (!newDataSet) {
upperFrom = from
newDataSet = true
} else {
const diff = from - lastPos
if (diff > 0) finalText += Array(diff + 1).join(' ')
}
lastPos = to
finalText += text
if (moreThan500Words(finalText)) {
const updatedFrom = chunksOf500Words.length ? upperFrom : upperFrom + 1
chunksOf500Words.push({
from: updatedFrom,
text: finalText,
})
finalText = ''
newDataSet = false
}
}
chunksOf500Words.push({
from: chunksOf500Words.length ? upperFrom : 1,
text: finalText,
})
const requests = chunksOf500Words.map(({ text, from }) => getMatchAndSetDecorations(doc, text, from))
if (editorView) dispatch(editorView.state.tr.setMeta(LanguageToolHelpingWords.LoadingTransactionName, true))
Promise.all(requests).then(() => {
if (editorView) dispatch(editorView.state.tr.setMeta(LanguageToolHelpingWords.LoadingTransactionName, false))
})
proofReadInitially = true
}
const debouncedProofreadAndDecorate = debounce(proofreadAndDecorateWholeDoc, 1000)
export const LanguageTool = Extension.create({
name: 'languagetool',
addOptions() {
var _a
return {
language: 'auto',
apiUrl:
((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0
? void 0
: _a.VUE_APP_LANGUAGE_TOOL_URL) + 'check',
automaticMode: true,
documentId: undefined,
}
},
addStorage() {
return {
match: match,
loading: false,
}
},
addCommands() {
return {
proofread:
() =>
({ tr }) => {
proofreadAndDecorateWholeDoc(tr.doc, this.options.apiUrl)
return true
},
toggleProofreading: () => () => {
// TODO: implement toggling proofreading
return false
},
ignoreLanguageToolSuggestion:
() =>
({ editor }) => {
if (this.options.documentId === undefined)
throw new Error('Please provide a unique Document ID(number|string)')
const { selection, doc } = editor.state
const { from, to } = selection
decorationSet = decorationSet.remove(decorationSet.find(from, to))
const content = doc.textBetween(from, to)
db.ignoredWords.add({ value: content, documentId: `${extensionDocId}` })
return false
},
}
},
addProseMirrorPlugins() {
const { apiUrl, documentId } = this.options
return [
new Plugin({
key: new PluginKey('languagetool'),
props: {
decorations(state) {
return this.getState(state)
},
attributes: {
spellcheck: 'false',
},
},
state: {
init: (config, state) => {
decorationSet = DecorationSet.create(state.doc, [])
if (this.options.automaticMode) proofreadAndDecorateWholeDoc(state.doc, apiUrl)
if (documentId) {
extensionDocId = documentId
db = new Dexie('LanguageToolIgnoredSuggestions')
db.version(1).stores({
ignoredWords: `
++id,
&value,
documentId
`,
})
}
return decorationSet
},
apply: (tr, oldPluginState, oldEditorState) => {
const matchUpdated = tr.getMeta(LanguageToolHelpingWords.MatchUpdatedTransactionName)
const loading = tr.getMeta(LanguageToolHelpingWords.LoadingTransactionName)
if (loading) this.storage.loading = true
else this.storage.loading = false
if (matchUpdated) this.storage.match = match
const languageToolDecorations = tr.getMeta(LanguageToolHelpingWords.LanguageToolTransactionName)
if (languageToolDecorations) return decorationSet
if (tr.docChanged && this.options.automaticMode) {
if (!proofReadInitially) debouncedProofreadAndDecorate(tr.doc, apiUrl)
else changedDescendants(oldEditorState.doc, tr.doc, 0, debouncedProofreadNodeAndUpdateItsDecorations)
}
decorationSet = decorationSet.map(tr.mapping, tr.doc)
setTimeout(addEventListenersToDecorations)
return decorationSet
},
},
view: (view) => {
return {
update(view) {
editorView = view
},
}
},
}),
]
},
})
//# sourceMappingURL=languagetool.js.map