Skip to content

Commit

Permalink
Merge pull request #10956 from keymanapp/fix/ios/sliding-context-range
Browse files Browse the repository at this point in the history
  • Loading branch information
jahorton authored Mar 19, 2024
2 parents 107d1c9 + 3240ee7 commit 8e4a80f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,16 @@ function setKeymanContext(text, doSync, selStart, selLength) {
let selEnd = selStart + selLength;
selEnd = isNaN(selEnd) ? undefined : selEnd;

const shouldReset = keyman.context.updateContext(text, selStart, selEnd);
if(!doSync || shouldReset) {
if(doSync) {
const shouldReset = keyman.context.updateContext(text, selStart, selEnd);
if(shouldReset) {
keyman.resetContext();
}
} else {
// if not in "sync" mode, we have a hard context reset; just full-reset it.
keyman.context.setText(text);
keyman.context.setSelection(selStart, selStart+selLength);
keyman.resetContext();
}
}

Expand Down
16 changes: 11 additions & 5 deletions web/src/app/webview/src/contextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ export class ContextHost extends Mock {

updateContext(text: string, selStart: number, selEnd: number): boolean {
let shouldResetContext = false;
if(text != this.text) {
let tempMock = new Mock(text, selStart ?? text._kmwLength(), selEnd ?? text._kmwLength());

let newLeft = tempMock.getTextBeforeCaret();
let oldLeft = this.getTextBeforeCaret();
let tempMock = new Mock(text, selStart ?? text._kmwLength(), selEnd ?? text._kmwLength());
let newLeft = tempMock.getTextBeforeCaret();
let oldLeft = this.getTextBeforeCaret();

if(text != this.text) {
let unexpectedBeforeCharCount = findCommonSubstringEndIndex(newLeft, oldLeft, true) + 1;
shouldResetContext = !!unexpectedBeforeCharCount;
}

if(shouldResetContext) {
this.text = text;
this.selStart = selStart;
this.selEnd = selEnd;
} else {
// Transform selection coordinates to their location within the longform context window.
let delta = oldLeft._kmwLength() - newLeft._kmwLength();
this.selStart = selStart - delta;
this.selEnd = selEnd - delta;
}

if(selStart === undefined || selEnd === undefined) {
Expand Down

0 comments on commit 8e4a80f

Please sign in to comment.