Another attempt at fixing scrolling

pull/73/head
Zef Hemel 2022-09-06 16:33:00 +02:00
parent 0090db37e6
commit d40d05fbf4
1 changed files with 21 additions and 11 deletions

View File

@ -221,7 +221,7 @@ export class Editor {
return; return;
} }
await this.loadPage(pageName); let stateRestored = await this.loadPage(pageName);
if (pos) { if (pos) {
if (typeof pos === "string") { if (typeof pos === "string") {
console.log("Navigating to anchor", pos); console.log("Navigating to anchor", pos);
@ -248,6 +248,11 @@ export class Editor {
selection: { anchor: pos }, selection: { anchor: pos },
scrollIntoView: true, scrollIntoView: true,
}); });
} else if (!stateRestored) {
this.editorView.dispatch({
selection: { anchor: 0 },
scrollIntoView: true,
});
} }
}); });
@ -551,7 +556,7 @@ export class Editor {
markdownSyscalls(buildMarkdown(this.mdExtensions)) markdownSyscalls(buildMarkdown(this.mdExtensions))
); );
this.saveState(); this.saveState(this.currentPage);
editorView.setState( editorView.setState(
this.createEditorState(this.currentPage, editorView.state.sliceDoc()) this.createEditorState(this.currentPage, editorView.state.sliceDoc())
@ -603,22 +608,24 @@ export class Editor {
await this.pageNavigator.navigate(name, pos, replaceState); await this.pageNavigator.navigate(name, pos, replaceState);
} }
async loadPage(pageName: string) { async loadPage(pageName: string): Promise<boolean> {
const loadingDifferentPage = pageName !== this.currentPage; const loadingDifferentPage = pageName !== this.currentPage;
const editorView = this.editorView; const editorView = this.editorView;
if (!editorView) { if (!editorView) {
return; return false;
} }
const previousPage = this.currentPage;
this.viewDispatch({ this.viewDispatch({
type: "page-loading", type: "page-loading",
name: pageName, name: pageName,
}); });
// Persist current page state and nicely close page // Persist current page state and nicely close page
if (this.currentPage) { if (previousPage) {
this.saveState(); this.saveState(previousPage);
this.space.unwatchPage(this.currentPage); this.space.unwatchPage(previousPage);
await this.save(true); await this.save(true);
} }
@ -640,7 +647,7 @@ export class Editor {
if (editorView.contentDOM) { if (editorView.contentDOM) {
this.tweakEditorDOM(editorView.contentDOM, doc.meta.perm === "ro"); this.tweakEditorDOM(editorView.contentDOM, doc.meta.perm === "ro");
} }
this.restoreState(pageName); let stateRestored = this.restoreState(pageName);
this.space.watchPage(pageName); this.space.watchPage(pageName);
this.viewDispatch({ this.viewDispatch({
@ -653,6 +660,8 @@ export class Editor {
} else { } else {
await this.eventHook.dispatchEvent("editor:pageReloaded", pageName); await this.eventHook.dispatchEvent("editor:pageReloaded", pageName);
} }
return stateRestored;
} }
tweakEditorDOM(contentDOM: HTMLElement, readOnly: boolean) { tweakEditorDOM(contentDOM: HTMLElement, readOnly: boolean) {
@ -676,7 +685,7 @@ export class Editor {
} }
} }
private restoreState(pageName: string) { private restoreState(pageName: string): boolean {
let pageState = this.openPages.get(pageName); let pageState = this.openPages.get(pageName);
const editorView = this.editorView!; const editorView = this.editorView!;
if (pageState) { if (pageState) {
@ -695,11 +704,12 @@ export class Editor {
}); });
} }
editorView.focus(); editorView.focus();
return !!pageState;
} }
private saveState() { private saveState(currentPage: string) {
this.openPages.set( this.openPages.set(
this.currentPage!, currentPage,
new PageState( new PageState(
this.editorView!.scrollDOM.scrollTop, this.editorView!.scrollDOM.scrollTop,
this.editorView!.state.selection this.editorView!.state.selection