Skip to content

Commit

Permalink
Merge pull request #87 from mindofmatthew/retain-scroll
Browse files Browse the repository at this point in the history
Preserve scroll position when switching tabs
  • Loading branch information
matthewkaney authored May 21, 2024
2 parents 18ce104 + 7c7dd08 commit 99e16b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions core/extensions/layout/tabs/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class EditorTabState extends TabState<EditorState> {
export class EditorTabView extends TabView<EditorState> {
private editor;

// TODO: ScrollTarget type isn't exported currently
private scrollSnapshot: any | null = null;

constructor(
layout: LayoutView,
id: string,
Expand Down Expand Up @@ -72,6 +75,18 @@ export class EditorTabView extends TabView<EditorState> {
}
}

beforeUnmount() {
this.scrollSnapshot = this.editor.scrollSnapshot();
}

afterMount() {
if (this.scrollSnapshot !== null) {
this.editor.update([
this.editor.state.update({ effects: this.scrollSnapshot }),
]);
}
}

beforeClose() {
this.api.requestClose(this.state.id);
return false;
Expand Down
6 changes: 6 additions & 0 deletions core/extensions/layout/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class LayoutView {

if (!currentTab) throw Error("View doesn't have old current tab");

currentTab.beforeUnmount();
this.dom.removeChild(currentTab.dom);
}

Expand All @@ -86,6 +87,7 @@ export class LayoutView {
if (!currentTab) throw Error("View doesn't have old new tab");

this.dom.insertBefore(currentTab.dom, this.panelArea);
currentTab.afterMount();
}

// Update tab bar
Expand Down Expand Up @@ -113,6 +115,10 @@ export abstract class TabView<T> {
this.state = tr.state.tabs[this.state.id];
}

beforeUnmount() {}

afterMount() {}

beforeClose() {
return true;
}
Expand Down

0 comments on commit 99e16b8

Please sign in to comment.