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

Desktop: Rich Text Editor: Preserve cursor location when updating editor content #10781

Merged
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
10 changes: 10 additions & 0 deletions packages/app-desktop/gui/NoteEditor/NoteBody/TinyMCE/TinyMCE.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,13 @@ const TinyMCE = (props: NoteBodyEditorProps, ref: any) => {
);
if (cancelled) return;

// Use an offset bookmark -- the default bookmark type is not preserved after unloading
// and reloading the editor.
// See https://github.com/tinymce/tinymce/issues/9736 for a brief description of the
// different bookmark types. An offset bookmark seems to have the smallest change
// when the note content is updated externally.
const offsetBookmarkId = 2;
const bookmark = editor.selection.getBookmark(offsetBookmarkId);
editor.setContent(awfulInitHack(result.html));

if (lastOnChangeEventInfo.current.contentKey !== props.contentKey) {
Expand All @@ -960,6 +967,9 @@ const TinyMCE = (props: NoteBodyEditorProps, ref: any) => {
// times would result in an empty note.
// https://github.com/laurent22/joplin/issues/3534
editor.undoManager.reset();
} else {
// Restore the cursor location
editor.selection.bookmarkManager.moveToBookmark(bookmark);
}

lastOnChangeEventInfo.current = {
Expand Down
Loading