From 6170bca0d824d2d7c5b2be22efcfdcad58351e7b Mon Sep 17 00:00:00 2001 From: Antonella Sgarlatta Date: Wed, 23 Jun 2021 15:57:33 -0300 Subject: [PATCH] fix: scroll position changing when entering new line on editor --- .../javascripts/views/editor/editor_view.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/app/assets/javascripts/views/editor/editor_view.ts b/app/assets/javascripts/views/editor/editor_view.ts index 8cb4c3559db..822c4d50e66 100644 --- a/app/assets/javascripts/views/editor/editor_view.ts +++ b/app/assets/javascripts/views/editor/editor_view.ts @@ -105,6 +105,7 @@ class EditorViewCtrl extends PureViewCtrl { public editorValues: EditorValues = { title: '', text: '' }; onEditorLoad?: () => void; + private scrollPosition = 0; private removeAltKeyObserver?: any; private removeTrashKeyObserver?: any; private removeTabObserver?: any; @@ -131,6 +132,8 @@ class EditorViewCtrl extends PureViewCtrl { this.editorMenuOnSelect = this.editorMenuOnSelect.bind(this); this.onPanelResizeFinish = this.onPanelResizeFinish.bind(this); + this.setScrollPosition = this.setScrollPosition.bind(this); + this.resetScrollPosition = this.resetScrollPosition.bind(this); this.onEditorLoad = () => { this.application!.getDesktopService().redoSearch(); }; @@ -868,6 +871,20 @@ class EditorViewCtrl extends PureViewCtrl { }); } + setScrollPosition() { + const editor = document.getElementById( + ElementIds.NoteTextEditor + ) as HTMLInputElement; + this.scrollPosition = editor.scrollTop; + } + + resetScrollPosition() { + const editor = document.getElementById( + ElementIds.NoteTextEditor + ) as HTMLInputElement; + editor.scrollTop = this.scrollPosition; + } + onSystemEditorLoad() { if (this.removeTabObserver) { return; @@ -915,6 +932,9 @@ class EditorViewCtrl extends PureViewCtrl { }, }); + editor.addEventListener('scroll', this.setScrollPosition); + editor.addEventListener('input', this.resetScrollPosition); + /** * Handles when the editor is destroyed, * (and not when our controller is destroyed.) @@ -922,6 +942,9 @@ class EditorViewCtrl extends PureViewCtrl { angular.element(editor).one('$destroy', () => { this.removeTabObserver?.(); this.removeTabObserver = undefined; + editor.removeEventListener('scroll', this.setScrollPosition); + editor.removeEventListener('scroll', this.resetScrollPosition); + this.scrollPosition = 0; }); } }