From c7e324500875b1bb608507490cb78ee78ff6c977 Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Sun, 26 Jun 2022 10:21:38 -0700 Subject: [PATCH] Mobile: Fixes #5949: Scroll selection into view in beta editor when window resizes (#6610) --- .../app-mobile/components/NoteEditor/CodeMirror.ts | 10 ++++++++-- .../app-mobile/components/NoteEditor/NoteEditor.tsx | 5 +++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror.ts b/packages/app-mobile/components/NoteEditor/CodeMirror.ts index 4c2ed5e71..e9fac36bb 100644 --- a/packages/app-mobile/components/NoteEditor/CodeMirror.ts +++ b/packages/app-mobile/components/NoteEditor/CodeMirror.ts @@ -26,8 +26,9 @@ interface CodeMirrorResult { editor: EditorView; undo: Function; redo: Function; - select: (anchor: number, head: number)=> void; - insertText: (text: string)=> void; + select(anchor: number, head: number): void; + scrollSelectionIntoView(): void; + insertText(text: string): void; } function postMessage(name: string, data: any) { @@ -231,6 +232,11 @@ export function initCodeMirror(parentElement: any, initialText: string, theme: a scrollIntoView: true, })); }, + scrollSelectionIntoView: () => { + editor.dispatch(editor.state.update({ + scrollIntoView: true, + })); + }, insertText: (text: string) => { editor.dispatch(editor.state.replaceSelection(text)); }, diff --git a/packages/app-mobile/components/NoteEditor/NoteEditor.tsx b/packages/app-mobile/components/NoteEditor/NoteEditor.tsx index d12f8d102..9aa6155a2 100644 --- a/packages/app-mobile/components/NoteEditor/NoteEditor.tsx +++ b/packages/app-mobile/components/NoteEditor/NoteEditor.tsx @@ -266,6 +266,11 @@ function NoteEditor(props: Props, ref: any) { cm = codeMirrorBundle.initCodeMirror(parentElement, initialText, theme); ${setInitialSelectionJS} + + // Fixes https://github.com/laurent22/joplin/issues/5949 + window.onresize = () => { + cm.scrollSelectionIntoView(); + }; } catch (e) { window.ReactNativeWebView.postMessage("error:" + e.message + ": " + JSON.stringify(e)) } finally {