Allow passing additional arguments to `renamePageCommand` and `renamePrefixCommand` (#720)

pull/721/head
Joe Krill 2024-02-17 15:22:51 -05:00 committed by GitHub
parent 9ba4d008f2
commit 8c3625b0a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 50 additions and 13 deletions

View File

@ -2,19 +2,31 @@ import { editor, space } from "$sb/syscalls.ts";
import { validatePageName } from "../../plug-api/lib/page_ref.ts"; import { validatePageName } from "../../plug-api/lib/page_ref.ts";
import { getBackLinks } from "./page_links.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) { export async function renamePageCommand(cmdDef: any) {
const oldName = await editor.getCurrentPage(); const oldName = cmdDef.oldPage || await editor.getCurrentPage();
console.log("Old name is", oldName); console.log("Old name is", oldName);
const newName = cmdDef.page || const newName = cmdDef.page ||
await editor.prompt(`Rename ${oldName} to:`, oldName); await editor.prompt(`Rename ${oldName} to:`, oldName);
if (!newName) { if (!newName) {
return; return false;
} }
try { try {
validatePageName(newName); validatePageName(newName);
} catch (e: any) { } catch (e: any) {
return editor.flashNotification(e.message, "error"); await editor.flashNotification(e.message, "error");
return false;
} }
console.log("New name", newName); console.log("New name", newName);
@ -22,7 +34,7 @@ export async function renamePageCommand(cmdDef: any) {
if (newName.trim() === oldName.trim()) { if (newName.trim() === oldName.trim()) {
// Nothing to do here // Nothing to do here
console.log("Name unchanged, exiting"); console.log("Name unchanged, exiting");
return; return false;
} }
await editor.save(); await editor.save();
@ -45,13 +57,19 @@ export async function renamePageCommand(cmdDef: any) {
throw e; throw e;
} }
} }
const updatedReferences = await renamePage(oldName, newName, true); const updatedReferences = await renamePage(
oldName,
newName,
cmdDef.navigateThere ?? true,
);
await editor.flashNotification( await editor.flashNotification(
`Renamed page, and updated ${updatedReferences} references`, `Renamed page, and updated ${updatedReferences} references`,
); );
return true;
} catch (e: any) { } catch (e: any) {
await editor.flashNotification(e.message, "error"); await editor.flashNotification(e.message, "error");
return false;
} }
} }
@ -132,14 +150,31 @@ async function renamePage(
return updatedReferences; 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) { 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) { if (!newPrefix) {
return; return false;
} }
const allPages = await space.listPages(); const allPages = await space.listPages();
@ -148,11 +183,11 @@ export async function renamePrefixCommand() {
); );
if ( if (
!(await editor.confirm( cmdDef.disableConfirmation !== true && !(await editor.confirm(
`This will affect ${allAffectedPages.length} pages. Are you sure?`, `This will affect ${allAffectedPages.length} pages. Are you sure?`,
)) ))
) { ) {
return; return false;
} }
const allNewNames = allAffectedPages.map((name) => const allNewNames = allAffectedPages.map((name) =>
@ -187,8 +222,10 @@ export async function renamePrefixCommand() {
} }
await editor.flashNotification("Batch rename complete", "info"); await editor.flashNotification("Batch rename complete", "info");
return true;
} catch (e: any) { } catch (e: any) {
return editor.flashNotification(e.message, "error"); await editor.flashNotification(e.message, "error");
return false;
} }
} }