Skip to content

Commit

Permalink
fix: scroll position changing when entering new line on editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonella Sgarlatta committed Jun 23, 2021
1 parent 28aab95 commit 6170bca
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions app/assets/javascripts/views/editor/editor_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
public editorValues: EditorValues = { title: '', text: '' };
onEditorLoad?: () => void;

private scrollPosition = 0;
private removeAltKeyObserver?: any;
private removeTrashKeyObserver?: any;
private removeTabObserver?: any;
Expand All @@ -131,6 +132,8 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {

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();
};
Expand Down Expand Up @@ -868,6 +871,20 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
});
}

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;
Expand Down Expand Up @@ -915,13 +932,19 @@ class EditorViewCtrl extends PureViewCtrl<unknown, EditorState> {
},
});

editor.addEventListener('scroll', this.setScrollPosition);
editor.addEventListener('input', this.resetScrollPosition);

/**
* Handles when the editor is destroyed,
* (and not when our controller is destroyed.)
*/
angular.element(editor).one('$destroy', () => {
this.removeTabObserver?.();
this.removeTabObserver = undefined;
editor.removeEventListener('scroll', this.setScrollPosition);
editor.removeEventListener('scroll', this.resetScrollPosition);
this.scrollPosition = 0;
});
}
}
Expand Down

0 comments on commit 6170bca

Please sign in to comment.