2023-08-28 15:12:15 +00:00
|
|
|
import { editor, space } from "$sb/syscalls.ts";
|
2024-01-11 13:06:24 +00:00
|
|
|
import { isFederationPath } from "$sb/lib/resolve.ts";
|
2022-10-14 13:11:33 +00:00
|
|
|
|
2022-02-28 13:35:51 +00:00
|
|
|
export async function deletePage() {
|
2022-10-14 13:11:33 +00:00
|
|
|
const pageName = await editor.getCurrentPage();
|
2022-11-18 15:04:37 +00:00
|
|
|
if (
|
|
|
|
!await editor.confirm(`Are you sure you would like to delete ${pageName}?`)
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-28 12:14:15 +00:00
|
|
|
console.log("Navigating to index page");
|
2022-10-14 13:11:33 +00:00
|
|
|
await editor.navigate("");
|
2022-02-28 13:35:51 +00:00
|
|
|
console.log("Deleting page from space");
|
2022-10-14 13:11:33 +00:00
|
|
|
await space.deletePage(pageName);
|
2022-02-28 13:35:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 13:06:24 +00:00
|
|
|
export async function copyPage(_def: any, predefinedNewName: string) {
|
2023-06-14 07:20:15 +00:00
|
|
|
const oldName = await editor.getCurrentPage();
|
2024-01-11 13:06:24 +00:00
|
|
|
let suggestedName = predefinedNewName || oldName;
|
|
|
|
|
|
|
|
if (isFederationPath(oldName)) {
|
|
|
|
const pieces = oldName.split("/");
|
|
|
|
suggestedName = pieces.slice(1).join("/");
|
|
|
|
}
|
|
|
|
const newName = await editor.prompt(`Copy to new page:`, suggestedName);
|
2023-06-14 07:20:15 +00:00
|
|
|
|
|
|
|
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(
|
2024-01-11 13:06:24 +00:00
|
|
|
`"${newName}" already exists, cannot copy to existing page.`,
|
2023-06-14 07:20:15 +00:00
|
|
|
);
|
|
|
|
} 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);
|
|
|
|
}
|