From 028d20df59d92af7d3692ee9d244f4ca906a6015 Mon Sep 17 00:00:00 2001 From: Daniel Souza Date: Mon, 23 Sep 2024 16:06:15 -0300 Subject: [PATCH] fix: handle syncfusion previous line bug (#188) --- src/syncfusion/index.ts | 1 - src/syncfusion/selection.ts | 40 ++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/syncfusion/index.ts b/src/syncfusion/index.ts index d88c2c0..f8a684a 100644 --- a/src/syncfusion/index.ts +++ b/src/syncfusion/index.ts @@ -688,7 +688,6 @@ export class IaraSyncfusionAdapter inference.inferenceId ? `inferenceId_${inference.inferenceId}` : undefined, - true, this.config.highlightInference ); diff --git a/src/syncfusion/selection.ts b/src/syncfusion/selection.ts index a0ae458..adf459f 100644 --- a/src/syncfusion/selection.ts +++ b/src/syncfusion/selection.ts @@ -39,7 +39,6 @@ export class IaraSyncfusionSelectionManager { private _editor: DocumentEditor, private _config: IaraSyncfusionConfig, bookmarkId?: string, - getSurrondingWords = false, highlightSelection = false ) { if (highlightSelection) this._highlightSelection(); @@ -62,42 +61,51 @@ export class IaraSyncfusionSelectionManager { }; const { endOffset, startOffset } = this._editor.selection; - this._editor.editor.insertBookmark(this.initialSelectionData.bookmarkId); - if (!getSurrondingWords) return; + const [wordBefore, lineBefore] = this._getWordBeforeSelection(); + this.wordBeforeSelection = wordBefore; this._editor.selection.select(startOffset, endOffset); - this.wordBeforeSelection = this._getWordBeforeSelection(); + const [wordAfter, lineAfter] = this._getWordAfterSelection(); + this.wordAfterSelection = wordAfter; - this._editor.selection.select(startOffset, endOffset); - this.wordAfterSelection = this._getWordAfterSelection(); + if (lineAfter.trim() === lineBefore.trim()) { + // Handle a syncfusion bug where the previous text is returned as the entire previous line. + // This only happens on the start of a line + this.wordBeforeSelection = ""; + } - this._editor.selection.extendToLineStart(); - this.isAtStartOfLine = this._editor.selection.text.length === 0; + this.isAtStartOfLine = + this.wordBeforeSelection.length === 0 || startOffset.endsWith(";0"); + + this._editor.selection.select(startOffset, endOffset); + this._editor.editor.insertBookmark(this.initialSelectionData.bookmarkId); this.resetSelection(); } - private _getWordAfterSelection(): string { + private _getWordAfterSelection(): string[] { this._editor.selection.extendToLineEnd(); - const startsWithSpace = this._editor.selection.text.startsWith(" "); - const words = this._editor.selection.text + const lineAfter = this._editor.selection.text; + const startsWithSpace = lineAfter.startsWith(" "); + const words = lineAfter .split(" ") .slice(0, 2) .filter(word => word.trim()); const wordAfter = `${startsWithSpace ? " " : ""}${words[0] || ""}`; - return wordAfter; + return [wordAfter, lineAfter]; } - private _getWordBeforeSelection(): string { + private _getWordBeforeSelection(): string[] { this._editor.selection.extendToLineStart(); - const endsWithSpace = this._editor.selection.text.endsWith(" "); - const words = this._editor.selection.text + const lineBefore = this._editor.selection.text; + const endsWithSpace = lineBefore.endsWith(" "); + const words = lineBefore .split(" ") .slice(-2) .filter(word => word.trim()); const wordBefore = `${words.pop() || ""}${endsWithSpace ? " " : ""}`; - return wordBefore; + return [wordBefore, lineBefore]; } private _highlightSelection(): void {