Skip to content

Commit

Permalink
fix cursor issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Rain-Zheng committed May 16, 2024
1 parent b230301 commit 741df8d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/roosterjs-content-model-plugins/lib/edit/EditPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class EditPlugin implements EditorPlugin {
private editor: IEditor | null = null;
private disposer: (() => void) | null = null;
private shouldHandleNextInputEvent = false;
private restoreSelectionCallback: (() => void) | null = null;

/**
* Get name of this plugin
Expand Down Expand Up @@ -70,6 +71,10 @@ export class EditPlugin implements EditorPlugin {
case 'keyDown':
this.handleKeyDownEvent(this.editor, event);
break;
case 'keyUp':
this.restoreSelectionCallback?.();
this.restoreSelectionCallback = null;
break;
}
}
}
Expand Down Expand Up @@ -150,15 +155,12 @@ export class EditPlugin implements EditorPlugin {
if (handled) {
rawEvent.preventDefault();

// Restore the selection to avoid the cursor jump issue
// Restore the selection on keyup event to avoid the cursor jump issue
// See: https://issues.chromium.org/issues/330596261
const selection = editor.getDOMSelection();
const doc = this.editor?.getDocument();
doc?.defaultView?.requestAnimationFrame(() => {
if (this.editor) {
this.editor.setDOMSelection(selection);
}
});
this.restoreSelectionCallback = () => {
this.editor?.setDOMSelection(selection);
};
}
}
}

0 comments on commit 741df8d

Please sign in to comment.