diff --git a/common/web/keyboard-processor/src/text/outputTarget.ts b/common/web/keyboard-processor/src/text/outputTarget.ts index ab9656b699..6ad579d1fa 100644 --- a/common/web/keyboard-processor/src/text/outputTarget.ts +++ b/common/web/keyboard-processor/src/text/outputTarget.ts @@ -21,15 +21,17 @@ export function isEmptyTransform(transform: Transform) { export class TextTransform implements Transform { readonly insert: string; readonly deleteLeft: number; - readonly deleteRight?: number; + readonly deleteRight: number; + readonly erasedSelection: boolean; - constructor(insert: string, deleteLeft: number, deleteRight?: number) { + constructor(insert: string, deleteLeft: number, deleteRight: number, erasedSelection: boolean) { this.insert = insert; this.deleteLeft = deleteLeft; - this.deleteRight = deleteRight || 0; + this.deleteRight = deleteRight; + this.erasedSelection = erasedSelection; } - public static readonly nil = new TextTransform('', 0, 0); + public static readonly nil = new TextTransform('', 0, 0, false); } export class Transcription { @@ -138,7 +140,7 @@ export default abstract class OutputTarget { // caret mid-word.. const deletedRight = fromRight.substring(0, rightDivergenceIndex + 1)._kmwLength(); - return new TextTransform(insertedText, deletedLeft, deletedRight); + return new TextTransform(insertedText, deletedLeft, deletedRight, original.getSelectedText() && !this.getSelectedText()); } buildTranscriptionFrom(original: OutputTarget, keyEvent: KeyEvent, readonly: boolean, alternates?: Alternate[]): Transcription { diff --git a/common/web/keyboard-processor/tests/node/transcriptions.js b/common/web/keyboard-processor/tests/node/transcriptions.js index 6d6be7b540..b071532bf2 100644 --- a/common/web/keyboard-processor/tests/node/transcriptions.js +++ b/common/web/keyboard-processor/tests/node/transcriptions.js @@ -448,7 +448,8 @@ but not himself.`; // Sheev Palpatine, in the Star Wars prequels. assert.deepEqual(transform, { insert: '', deleteLeft: 0, - deleteRight: 0 + deleteRight: 0, + erasedSelection: true }); }); @@ -459,7 +460,8 @@ but not himself.`; // Sheev Palpatine, in the Star Wars prequels. const transform = { insert: '', deleteLeft: 0, - deleteRight: 0 + deleteRight: 0, + erasedSelection: true }; target.apply(transform); diff --git a/web/src/app/webview/src/contextManager.ts b/web/src/app/webview/src/contextManager.ts index d9ef2c6e52..a2450ad58c 100644 --- a/web/src/app/webview/src/contextManager.ts +++ b/web/src/app/webview/src/contextManager.ts @@ -1,4 +1,4 @@ -import { type Keyboard, Mock, OutputTarget, Transcription, findCommonSubstringEndIndex } from '@keymanapp/keyboard-processor'; +import { type Keyboard, Mock, OutputTarget, Transcription, findCommonSubstringEndIndex, isEmptyTransform } from '@keymanapp/keyboard-processor'; import { KeyboardStub } from 'keyman/engine/package-cache'; import { ContextManagerBase, ContextManagerConfiguration } from 'keyman/engine/main'; import { WebviewConfiguration } from './configuration.js'; @@ -41,7 +41,9 @@ export class ContextHost extends Mock { // Signal the necessary text changes to the embedding app, if it exists. if(this.oninserttext) { - this.oninserttext(transform.deleteLeft, transform.insert, transform.deleteRight); + if(!isEmptyTransform(transform) || transform.erasedSelection) { + this.oninserttext(transform.deleteLeft, transform.insert, transform.deleteRight); + } } }