Skip to content
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 @@ -753,6 +753,7 @@ export class PresentationEditor extends EventEmitter {

const beforeX = win.scrollX;
const beforeY = win.scrollY;
const alreadyFocused = view.hasFocus();
let focused = false;

// Strategy 1: Try focus with preventScroll option (modern browsers)
Expand Down Expand Up @@ -791,6 +792,16 @@ export class PresentationEditor extends EventEmitter {
}
}

// When the editor was not focused before, the browser places the DOM selection
// at an arbitrary position inside the off-screen contenteditable. ProseMirror's
// DOMObserver would read this stale position via a selectionchange event and
// overwrite PM state, causing the cursor to jump. Suppress selection updates
// for the next 50ms so PM re-applies its own selection to the DOM instead.
if (!alreadyFocused) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(view as any).domObserver.suppressSelectionUpdates();
}

// Restore scroll position if any focus attempt changed it
if (win.scrollX !== beforeX || win.scrollY !== beforeY) {
win.scrollTo(beforeX, beforeY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ vi.mock('../../Editor.js', () => {
focus: function () {
// Plain function that can be wrapped
},
hasFocus: function () {
return domElement === domElement.ownerDocument.activeElement;
},
domObserver: {
suppressSelectionUpdates: vi.fn(),
},
dispatch: vi.fn(),
},
options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ vi.mock('../../Editor.js', () => {
focus: function () {
// Plain function that can be wrapped
},
hasFocus: function () {
return domElement === domElement.ownerDocument.activeElement;
},
domObserver: {
suppressSelectionUpdates: vi.fn(),
},
dispatch: vi.fn(),
},
options: {
Expand Down
Loading