From 8c3625b0a4d89812649c95f618fd82a7f2db26b4 Mon Sep 17 00:00:00 2001 From: Joe Krill Date: Sat, 17 Feb 2024 15:22:51 -0500 Subject: [PATCH] Allow passing additional arguments to `renamePageCommand` and `renamePrefixCommand` (#720) --- plugs/index/refactor.ts | 63 ++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 13 deletions(-) diff --git a/plugs/index/refactor.ts b/plugs/index/refactor.ts index bb75b15f..127ce815 100644 --- a/plugs/index/refactor.ts +++ b/plugs/index/refactor.ts @@ -2,19 +2,31 @@ import { editor, space } from "$sb/syscalls.ts"; import { validatePageName } from "../../plug-api/lib/page_ref.ts"; import { getBackLinks } from "./page_links.ts"; +/** + * Renames a single page. + * @param cmdDef Optional command arguments + * @param cmdDef.oldPage The current name of the page to rename. Defaults to + * the current page selected in the editor. + * @param cmdDef.page The name to rename the page to. If not provided the + * user will be prompted to enter a new name. + * @param cmdDef.navigateThere When true, the user will be navigated to the + * renamed page. Defaults to true. + * @returns True if the rename succeeded; otherwise, false. + */ export async function renamePageCommand(cmdDef: any) { - const oldName = await editor.getCurrentPage(); + const oldName = cmdDef.oldPage || await editor.getCurrentPage(); console.log("Old name is", oldName); const newName = cmdDef.page || await editor.prompt(`Rename ${oldName} to:`, oldName); if (!newName) { - return; + return false; } try { validatePageName(newName); } catch (e: any) { - return editor.flashNotification(e.message, "error"); + await editor.flashNotification(e.message, "error"); + return false; } console.log("New name", newName); @@ -22,7 +34,7 @@ export async function renamePageCommand(cmdDef: any) { if (newName.trim() === oldName.trim()) { // Nothing to do here console.log("Name unchanged, exiting"); - return; + return false; } await editor.save(); @@ -45,13 +57,19 @@ export async function renamePageCommand(cmdDef: any) { throw e; } } - const updatedReferences = await renamePage(oldName, newName, true); + const updatedReferences = await renamePage( + oldName, + newName, + cmdDef.navigateThere ?? true, + ); await editor.flashNotification( `Renamed page, and updated ${updatedReferences} references`, ); + return true; } catch (e: any) { await editor.flashNotification(e.message, "error"); + return false; } } @@ -132,14 +150,31 @@ async function renamePage( return updatedReferences; } -export async function renamePrefixCommand() { - const oldPrefix = await editor.prompt("Prefix to rename:", ""); +/** + * Renames pages based on a prefix string. + * @param cmdDef Optional command arguments + * @param cmdDef.oldPrefix The prefix to rename from. If not provided the + * user will be prompted to enter a prefix. + * @param cmdDef.newPrefix The prefix with which to replace the `oldPrefix` + * value. If not provided the user will be prompted to enter a new prefix. + * @param cmdDef.disableConfirmation If false, the user will be prompted + * to confirm the rename action; Otherwise no confirmation dialog will + * be shown before renaming. Defaults to false. + * @returns True if the rename succeeded; otherwise, false. + */ +export async function renamePrefixCommand(cmdDef: any) { + const oldPrefix = cmdDef.oldPrefix ?? + await editor.prompt("Prefix to rename:", ""); + if (!oldPrefix) { - return; + return false; } - const newPrefix = await editor.prompt("New prefix:", oldPrefix); + + const newPrefix = cmdDef.newPrefix ?? + await editor.prompt("New prefix:", oldPrefix); + if (!newPrefix) { - return; + return false; } const allPages = await space.listPages(); @@ -148,11 +183,11 @@ export async function renamePrefixCommand() { ); if ( - !(await editor.confirm( + cmdDef.disableConfirmation !== true && !(await editor.confirm( `This will affect ${allAffectedPages.length} pages. Are you sure?`, )) ) { - return; + return false; } const allNewNames = allAffectedPages.map((name) => @@ -187,8 +222,10 @@ export async function renamePrefixCommand() { } await editor.flashNotification("Batch rename complete", "info"); + return true; } catch (e: any) { - return editor.flashNotification(e.message, "error"); + await editor.flashNotification(e.message, "error"); + return false; } }