Files
plainleaf/plugs/editor/page.ts
T
Zef HemelandGitHub dc2987a3f9 Sync engine rearchitecture (#1516)
* Don't require auth for .ping
* Finally introducing loggers
* Remove file list caching from disk_space_primitives
I don't think this is required anymore with the sync model
* Simplification of space primitives and more tests
* More reliable online status
* Add .zed settings
* Remove selfUpdate argument from `writeFile` no longer used
- Add `sync` config schema and document sync options
- Move sync config handling to client boot logic
- Move BootConfig type to ui_types.ts
- Remove sync env vars from server
- Update documentation for sync and architecture
* Improve sync logic and proxy routing for full sync state
* Add BoxProxy for late-bound client and improve Lua boot
- Add isMobile syscall for editor
* Add concurrency to sync process and log indexing duration
* Better service worker lifecycle management
2025-09-12 14:43:57 +02:00

67 lines
1.9 KiB
TypeScript

import { editor, space } from "@silverbulletmd/silverbullet/syscalls";
import { notFoundError } from "../../lib/constants.ts";
export async function deletePage() {
const pageName = await editor.getCurrentPage();
if (
!await editor.confirm(`Are you sure you would like to delete ${pageName}?`)
) {
return;
}
// 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(firstRecentlyOpenedPage?.name || "");
}
export async function copyPage(
_def: any,
sourcePage?: string,
toName?: string,
) {
const currentPage = await editor.getCurrentPage();
const fromName = sourcePage || currentPage;
const suggestedName = toName || fromName;
const newName = await editor.prompt(`Copy to page:`, suggestedName);
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(
`"${newName}" already exists, cannot copy to existing page.`,
);
} catch (e: any) {
if (e.message === notFoundError.message) {
// Expected not found error, so we can continue
} else {
await editor.flashNotification(e.message, "error");
throw e;
}
}
const text = await space.readPage(fromName);
console.log("Writing new page to space");
await space.writePage(newName, text);
if (currentPage === fromName) {
// If we're copying the current page, navigate there
console.log("Navigating to new page");
await editor.navigate(newName);
} else {
// Otherwise just notify of success
await editor.flashNotification("Page copied successfully");
}
}