diff --git a/plug-api/syscalls/editor.ts b/plug-api/syscalls/editor.ts index 754a356f..3d18a010 100644 --- a/plug-api/syscalls/editor.ts +++ b/plug-api/syscalls/editor.ts @@ -34,6 +34,14 @@ export function getCurrentPath(extension: boolean = false): Promise { return syscall("editor.getCurrentPath", extension); } +/** + * Returns the names of the pages that have been recently opened + * @returns the names of the recently opened pages + */ +export function getRecentlyOpenedPages(): Promise { + return syscall("editor.getRecentlyOpenedPages"); +} + /** * Returns the name of the currently open editor. * @returns the name of the current editor diff --git a/plugs/editor/page.ts b/plugs/editor/page.ts index 2aabd1cd..160beca0 100644 --- a/plugs/editor/page.ts +++ b/plugs/editor/page.ts @@ -7,10 +7,18 @@ export async function deletePage() { ) { return; } - console.log("Navigating to index page"); - await editor.navigate({ kind: "page", page: "" }); - console.log("Deleting page from space"); + // Query for last + const recentlyOpenedPages = await editor.getRecentlyOpenedPages(); + // Find the the first page that is not the current page + const firstRecentlyOpenedPage = recentlyOpenedPages.find( + (page) => page.name !== pageName, + ); await space.deletePage(pageName); + console.log("Navigating to previous page"); + await editor.navigate({ + kind: "page", + page: firstRecentlyOpenedPage?.name || "", + }); } export async function copyPage( diff --git a/web/reducer.ts b/web/reducer.ts index e84a844b..f0f8060d 100644 --- a/web/reducer.ts +++ b/web/reducer.ts @@ -96,7 +96,9 @@ export default function reducer( case "update-current-page-meta": { // Update in the allPages list as well state.allPages = state.allPages.map((pageMeta) => - pageMeta.name === action.meta.name ? action.meta : pageMeta + pageMeta.name === action.meta.name + ? { ...action.meta, lastOpened: Date.now() } + : pageMeta ); // Can't update page meta if not on a page if (state.current?.kind !== "page") return state; diff --git a/web/syscalls/editor.ts b/web/syscalls/editor.ts index 4f161fd0..89d15a62 100644 --- a/web/syscalls/editor.ts +++ b/web/syscalls/editor.ts @@ -36,6 +36,11 @@ export function editorSyscalls(client: Client): SysCallMapping { "editor.getCurrentEditor": (): string => { return client.documentEditor?.name || "page"; }, + "editor.getRecentlyOpenedPages": (): PageMeta[] => { + return client.ui.viewState.allPages.sort((a, b) => + (b.lastOpened || 0) - (a.lastOpened || 0) + ); + }, "editor.getText": () => { return client.editorView.state.sliceDoc(); }, diff --git a/web/syscalls/space.ts b/web/syscalls/space.ts index fb765082..9bab3cc1 100644 --- a/web/syscalls/space.ts +++ b/web/syscalls/space.ts @@ -70,12 +70,6 @@ export function spaceWriteSyscalls(editor: Client): SysCallMapping { return editor.space.writePage(name, text); }, "space.deletePage": async (_ctx, name: string) => { - // If we're deleting the current page, navigate to the index page - if (editor.currentPage === name) { - await editor.navigate({ kind: "page", page: "" }); - } - // Remove page from open pages in editor - // editor.openPages.openPages.delete(name); console.log("Deleting page"); await editor.space.deletePage(name); },