Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): selection range sync for long contexts #10956

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading