Skip to content

Commit f672fc8

Browse files
marcbachmannpeyerluk
authored andcommitted
fix: Do not send multiple change events when removing multiple nodes that belong to one highlight element
1 parent 51325a6 commit f672fc8

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

src/highlight-support.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ const highlightSupport = {
8080
const elems = editableHost.querySelectorAll(`[data-word-id="${highlightId}"]`)
8181
for (const elem of elems) {
8282
content.unwrap(elem)
83-
// in Chrome browsers the unwrap method leaves the host node split into 2 (lastChild !== firstChild)
84-
editableHost.normalize()
85-
if (dispatcher) dispatcher.notify('change', editableHost)
8683
}
84+
85+
// in Chrome browsers the unwrap method leaves the host node split into 2 (lastChild !== firstChild)
86+
editableHost.normalize()
87+
if (dispatcher) dispatcher.notify('change', editableHost)
8788
},
8889

8990
hasHighlight (editableHost, highlightId) {

src/plugins/highlighting/text-highlighting.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ export default class WordHighlighting {
2323

2424
findMatches (text, highlights) {
2525
if (!text || text === '' || !this.isElement(this.marker)) return
26-
27-
if (highlights && highlights.length > 0) {
28-
return this.searchMatches(text, highlights)
29-
}
26+
if (!highlights?.length) return
27+
return this.searchMatches(text, highlights)
3028
}
3129

3230
searchMatches (text, highlights) {

src/plugins/highlighting/whitespace-highlighting.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const characters = {
2323
// a paragraph and the highlighting prevents browsers from converting
2424
// the no-break space back to a normal space when the user keeps typing.
2525
const specialWhitespaceChars = '\\u2000-\\u200A\\u202F\\u205F\\u3000'
26+
const specialWhitespaceCharsRegex = new RegExp(`[${specialWhitespaceChars}]`, 'g')
2627

2728
export default class WhitespaceHighlighting {
2829

@@ -33,13 +34,7 @@ export default class WhitespaceHighlighting {
3334
findMatches (text) {
3435
if (!text) return
3536

36-
let regex = `[${specialWhitespaceChars}]`
37-
regex = new RegExp(regex, 'g')
38-
39-
const matches = []
40-
let match
41-
while ((match = regex.exec(text))) matches.push(match)
42-
37+
const matches = [...text.matchAll(specialWhitespaceCharsRegex)]
4338
return matches.map((entry) => this.prepareMatch(entry))
4439
}
4540

0 commit comments

Comments
 (0)