Allow passing additional arguments to `renamePageCommand` and `renamePrefixCommand` (#720)
parent
9ba4d008f2
commit
8c3625b0a4
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue