Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 17 additions & 27 deletions src/gui/ChoiceBuilder/choiceBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { type App, Modal, Setting } from "obsidian";
import { type App, Modal, Setting, setIcon } from "obsidian";
import type { SvelteComponent } from "svelte";
import { log } from "../../logger/logManager";
import type IChoice from "../../types/choices/IChoice";
import type { FileViewMode2, OpenLocation } from "../../types/fileOpening";
import {
normalizeFileOpening,
type FileOpeningSettings,
} from "../../utils/fileOpeningDefaults";
import GenericInputPrompt from "../GenericInputPrompt/GenericInputPrompt";
import { GenericTextSuggester } from "../suggesters/genericTextSuggester";
import { promptRenameChoice } from "../choiceRename";

export abstract class ChoiceBuilder extends Modal {
private resolvePromise: (input: IChoice) => void;
private rejectPromise: (reason?: unknown) => void;
private input: IChoice;
public waitForClose: Promise<IChoice>;
abstract choice: IChoice;
private didSubmit = false;
protected svelteElements: SvelteComponent[] = [];

protected constructor(app: App) {
super(app);

this.waitForClose = new Promise<IChoice>((resolve, reject) => {
this.waitForClose = new Promise<IChoice>((resolve) => {
this.resolvePromise = resolve;
this.rejectPromise = reject;
});

this.containerEl.addClass("quickAddModal");
Expand Down Expand Up @@ -85,23 +80,21 @@ export abstract class ChoiceBuilder extends Modal {
const headerEl: HTMLHeadingElement = this.contentEl.createEl("h2", {
cls: "choiceNameHeader",
});
headerEl.setText(choice.name);
const textEl = headerEl.createSpan({
text: choice.name,
cls: "choiceNameHeaderText",
});
const iconEl = headerEl.createSpan({
cls: "choiceNameHeaderIcon",
attr: { "aria-hidden": "true" },
});
setIcon(iconEl, "pencil");

headerEl.addEventListener("click", async (ev) => {
try {
const newName: string = await GenericInputPrompt.Prompt(
this.app,
choice.name,
"Choice name",
choice.name,
);
if (newName !== choice.name) {
choice.name = newName;
headerEl.setText(newName);
}
} catch {
log.logMessage(`No new name given for ${choice.name}`);
}
const newName = await promptRenameChoice(this.app, choice.name);
if (!newName) return;
choice.name = newName;
textEl.setText(newName);
});
}

Expand Down Expand Up @@ -211,12 +204,9 @@ export abstract class ChoiceBuilder extends Modal {

onClose() {
super.onClose();
this.resolvePromise(this.choice);
this.svelteElements.forEach((el) => {
if (el && el.$destroy) el.$destroy();
});

if (!this.didSubmit) this.rejectPromise("No answer given.");
else this.resolvePromise(this.input);
this.resolvePromise(this.choice);
}
}
2 changes: 2 additions & 0 deletions src/gui/choiceList/ChoiceList.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
on:configureChoice
on:toggleCommand
on:duplicateChoice
on:renameChoice
on:moveChoice
startDrag={startDrag}
bind:choice
Expand All @@ -86,6 +87,7 @@
on:configureChoice
on:toggleCommand
on:duplicateChoice
on:renameChoice
on:moveChoice
on:reorderChoices
startDrag={startDrag}
Expand Down
1 change: 1 addition & 0 deletions src/gui/choiceList/ChoiceListItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

function onContextMenu(evt: MouseEvent) {
showChoiceContextMenu(app, evt, choice, roots, {
onRename: () => dispatcher("renameChoice", { choice }),
onToggle: () => toggleCommandForChoice(),
onConfigure: () => configureChoice(),
onDuplicate: () => duplicateChoice(),
Expand Down
18 changes: 18 additions & 0 deletions src/gui/choiceList/ChoiceView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import type IChoice from "../../types/choices/IChoice";
import { AIAssistantSettingsModal } from "../AIAssistantSettingsModal";
import ObsidianIcon from "../components/ObsidianIcon.svelte";
import { promptRenameChoice } from "../choiceRename";
import AddChoiceBox from "./AddChoiceBox.svelte";
import ChoiceList from "./ChoiceList.svelte";
import { moveChoice as moveChoiceService } from "../../services/choiceService";
Expand Down Expand Up @@ -132,6 +133,21 @@
return oldChoice;
}

async function handleRenameChoice(e: any) {
const { choice } = e.detail;
if (!choice) return;

const newName = await promptRenameChoice(app, choice.name);
if (!newName) return;

const updatedChoice = { ...choice, name: newName };
choices = choices.map((entry) =>
updateChoiceHelper(entry, updatedChoice),
);
commandRegistry.updateCommand(choice, updatedChoice);
saveChoices(choices);
}

async function toggleCommandForChoice(e: any) {
const { choice: oldChoice } = e.detail;
const updatedChoice = createToggleCommandChoice(oldChoice);
Expand Down Expand Up @@ -209,6 +225,7 @@
on:configureChoice={handleConfigureChoice}
on:toggleCommand={toggleCommandForChoice}
on:duplicateChoice={handleDuplicateChoice}
on:renameChoice={handleRenameChoice}
on:moveChoice={handleMoveChoice}
on:reorderChoices={(e) => saveChoices(e.detail.choices)}
/>
Expand All @@ -223,6 +240,7 @@
on:configureChoice={handleConfigureChoice}
on:toggleCommand={toggleCommandForChoice}
on:duplicateChoice={handleDuplicateChoice}
on:renameChoice={handleRenameChoice}
on:moveChoice={handleMoveChoice}
/>
{/if}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/choiceList/MultiChoiceListItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

function onContextMenu(evt: MouseEvent) {
showChoiceContextMenu(app, evt, choice, roots, {
onRename: () => dispatcher('renameChoice', { choice }),
onToggle: () => toggleCommandForChoice(),
onConfigure: () => configureChoice(),
onDuplicate: () => duplicateChoice(),
Expand Down Expand Up @@ -114,6 +115,7 @@
on:toggleCommand
on:duplicateChoice
on:moveChoice
on:renameChoice
on:reorderChoices={handleNestedReorder}
bind:multiChoice={choice}
bind:choices={choice.choices}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/choiceList/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function isInvalidTarget(moving: IChoice, target: IChoice): boolean {
}

type MenuActions = {
onRename: () => void;
onToggle: () => void;
onConfigure: () => void;
onDuplicate: () => void;
Expand Down Expand Up @@ -79,6 +80,7 @@ export function showChoiceContextMenu(
.setIcon("zap")
.onClick(actions.onToggle),
)
.addItem((item) => item.setTitle("Rename").setIcon("pencil").onClick(actions.onRename))
.addItem((item) => item.setTitle("Configure").setIcon("settings").onClick(actions.onConfigure))
.addItem((item) => item.setTitle("Duplicate").setIcon("copy").onClick(actions.onDuplicate))
.addItem((item) => item.setTitle("Delete").setIcon("trash-2").onClick(actions.onDelete))
Expand Down
21 changes: 21 additions & 0 deletions src/gui/choiceRename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { App } from "obsidian";
import GenericInputPrompt from "./GenericInputPrompt/GenericInputPrompt";

export async function promptRenameChoice(
app: App,
currentName: string,
): Promise<string | null> {
try {
const newName = await GenericInputPrompt.Prompt(
app,
"Choice name",
undefined,
currentName,
);
const trimmed = newName.trim();
if (!trimmed || trimmed === currentName) return null;
return trimmed;
} catch {
return null;
}
}
10 changes: 10 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,22 @@

.choiceNameHeader {
text-align: center;
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
}

.choiceNameHeader:hover {
cursor: pointer;
}

.choiceNameHeaderIcon {
display: inline-flex;
align-items: center;
opacity: 0.5;
}

.folderInputContainer {
display: flex;
align-content: center;
Expand Down