From 3948c63619e61643e613513a0ff15a517a4cbf34 Mon Sep 17 00:00:00 2001 From: Ben Silverman Date: Mon, 14 Oct 2024 17:16:54 -0400 Subject: [PATCH] Prevent duplicate highlights from being created on multiline selections --- client/src/TextResource.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/client/src/TextResource.js b/client/src/TextResource.js index ac79db1a..af251b79 100644 --- a/client/src/TextResource.js +++ b/client/src/TextResource.js @@ -1285,13 +1285,14 @@ class TextResource extends Component { let alteredHighlights = []; let effectedMarks = []; const editorState = this.getEditorState(); + if (steps.length > 0 && steps.every((step) => step instanceof AddMarkStep && step.mark.type.name === this.state.documentSchema.marks.highlight.name)) { + // on highlight, will create 1 AddMarkStep per line break / element crossing + // save one new highlight for the entire range + this.createHighlight(steps[0].mark, tx.curSelection.content(), serializer); + } steps.forEach(step => { - // save new highlight - if (step instanceof AddMarkStep && step.mark.type.name === this.state.documentSchema.marks.highlight.name) { - this.createHighlight(step.mark, tx.curSelection.content(), serializer); - } // process highlights that have been removed or altered by a text content change or a mark toggle - else if (step instanceof ReplaceStep || (step instanceof RemoveMarkStep && step.mark.type.name === this.state.documentSchema.marks.highlight.name)) { + if (step instanceof ReplaceStep || (step instanceof RemoveMarkStep && step.mark.type.name === this.state.documentSchema.marks.highlight.name)) { // TODO: handle case where the space between two highlights is eliminated // pad the range where we look for effected highlights in order to accommodate edge cases with cursor at beginning or end of highlight let from = Math.max(step.from - 1, 0), to = step.to;