diff --git a/plugs/core/core.plug.yaml b/plugs/core/core.plug.yaml index 102930d5..27a5ffc2 100644 --- a/plugs/core/core.plug.yaml +++ b/plugs/core/core.plug.yaml @@ -47,6 +47,10 @@ functions: path: "./page.ts:deletePage" command: name: "Page: Delete" + copyPage: + path: "./page.ts:copyPage" + command: + name: "Page: Copy" # Attachments attachmentQueryProvider: diff --git a/plugs/core/page.ts b/plugs/core/page.ts index 0bfe8a10..59c94e23 100644 --- a/plugs/core/page.ts +++ b/plugs/core/page.ts @@ -93,6 +93,39 @@ export async function deletePage() { await space.deletePage(pageName); } +export async function copyPage() { + const oldName = await editor.getCurrentPage(); + const newName = await editor.prompt(`New page title:`, `${oldName} (copy)`); + + if (!newName) { + return; + } + + try { + // This throws an error if the page does not exist, which we expect to be the case + await space.getPageMeta(newName); + // So when we get to this point, we error out + throw new Error( + `Page ${newName} already exists, cannot rename to existing page.`, + ); + } catch (e: any) { + if (e.message === "Not found") { + // Expected not found error, so we can continue + } else { + await editor.flashNotification(e.message, "error"); + throw e; + } + } + + const text = await editor.getText(); + + console.log("Writing new page to space"); + await space.writePage(newName, text); + + console.log("Navigating to new page"); + await editor.navigate(newName); +} + export async function renamePage(cmdDef: any) { console.log("Got a target name", cmdDef.page); const oldName = await editor.getCurrentPage();