Files
fflow/src/settings-tab.ts
T

255 lines
7.3 KiB
TypeScript

import { App, PluginSettingTab, SearchComponent, Setting, SettingGroup } from "obsidian";
import type FflowPlugin from "./main";
import { BLOCKED_SYMBOLS, Flow, FlowKind, FlowStep, newFlow, symbolError, ValueMode } from "./settings";
export class FflowSettingTab extends PluginSettingTab {
constructor(app: App, private plugin: FflowPlugin) {
super(app, plugin);
}
private save = () => this.plugin.saveSettings();
/** Saves, then redraws: for edits that add, remove or reorder rows. */
private async saveAndRedraw(): Promise<void> {
await this.save();
this.display();
}
display(): void {
const { containerEl } = this;
const flows = this.plugin.settings.flows;
containerEl.empty();
new Setting(containerEl)
.setHeading()
.setName("Flows")
.setDesc(
`Type a flow's symbol at the start of a word and pick a value: the text disappears, the property is set, and the step's other properties are applied. Ordered flows get “next step” and “previous step” commands for hotkeys. Reserved symbols: ${BLOCKED_SYMBOLS.join(" ")}`,
)
.addButton((b) => b.setButtonText("Hotkeys").onClick(() => this.openHotkeys()))
.addButton((b) =>
b.setButtonText("Add flow").onClick(async () => {
flows.push(newFlow({ steps: [{ value: "", effects: [] }] }));
await this.saveAndRedraw();
}),
);
flows.forEach((flow, index) => this.renderFlow(flow, index));
}
private renderFlow(flow: Flow, index: number): void {
const flows = this.plugin.settings.flows;
const title = createFragment();
const badge = title.createSpan({ cls: "fflow-badge" });
const name = title.createSpan();
const updateTitle = () => {
badge.setText(flow.symbol);
badge.toggle(flow.symbol !== "");
name.setText(flow.name || flow.property || "New flow");
};
updateTitle();
const group = new SettingGroup(this.containerEl).setHeading(title).addClass("fflow-group");
group.addExtraButton((b) =>
b
.setIcon("trash")
.setTooltip("Delete flow")
.onClick(async () => {
flows.splice(index, 1);
await this.saveAndRedraw();
}),
);
group.addSetting((s) =>
s
.setName("Name")
.setDesc(flow.kind === "ordered" ? "Labels the hotkey commands." : "For your reference.")
.addText((t) =>
t
.setPlaceholder("Status")
.setValue(flow.name)
.onChange(async (value) => {
flow.name = value;
updateTitle();
await this.save();
}),
),
);
group.addSetting((s) => {
s.setName("Symbol").setDesc("Optional. One character, not a letter or digit.");
const error = s.descEl.createDiv({ cls: "fflow-error" });
error.setText(symbolError(flow.symbol, flows, index) ?? "");
s.addText((t) => {
t.setPlaceholder("@").setValue(flow.symbol);
t.inputEl.addClass("fflow-symbol");
t.onChange(async (value) => {
const problem = symbolError(value, flows, index);
error.setText(problem ?? "");
// Invalid input stays in the field but is never saved.
if (problem) return;
flow.symbol = value;
updateTitle();
await this.save();
});
});
});
group.addSetting((s) => {
s.setName("Property")
.setDesc(
flow.kind === "ordered"
? "Ordered: the steps below, in order. Each step can set other properties."
: "Open: every value already used in the vault; new values allowed. No steps or hotkeys.",
)
.addText((t) =>
t
.setPlaceholder("status")
.setValue(flow.property)
.onChange(async (value) => {
flow.property = value.trim();
updateTitle();
await this.save();
}),
)
.addDropdown((d) =>
d
.addOptions({ ordered: "Ordered list", open: "Open (vault)" })
.setValue(flow.kind)
.onChange(async (value) => {
flow.kind = value as FlowKind;
await this.saveAndRedraw();
}),
);
if (flow.kind === "open") {
s.addDropdown((d) =>
d
.addOptions({ single: "Single value", list: "List" })
.setValue(flow.valueMode)
.onChange(async (value) => {
flow.valueMode = value as ValueMode;
await this.save();
}),
);
}
});
if (flow.kind === "open") return;
group.addSetting((s) =>
s
.setName("Loop")
.setDesc("After the last step, “next” goes back to the first.")
.addToggle((t) =>
t.setValue(flow.loop).onChange(async (value) => {
flow.loop = value;
await this.save();
}),
),
);
flow.steps.forEach((step, i) => this.renderStep(group, flow, step, i));
group.addSetting((s) =>
s.addButton((b) =>
b.setButtonText("Add step").onClick(async () => {
flow.steps.push({ value: "", effects: [] });
await this.saveAndRedraw();
}),
),
);
}
private renderStep(group: SettingGroup, flow: Flow, step: FlowStep, i: number): void {
group.addSetting((s) => {
s.setName(`Step ${i + 1}`).setClass("fflow-step");
s.addText((t) =>
t
.setPlaceholder("value")
.setValue(step.value)
.onChange(async (value) => {
step.value = value.trim();
await this.save();
}),
);
s.addExtraButton((b) =>
b
.setIcon("list-plus")
.setTooltip("Also set a property")
.onClick(async () => {
step.effects.push({ property: "", value: "" });
await this.saveAndRedraw();
}),
);
const move = async (to: number) => {
flow.steps.splice(to, 0, ...flow.steps.splice(i, 1));
await this.saveAndRedraw();
};
s.addExtraButton((b) => b.setIcon("arrow-up").setTooltip("Move up").setDisabled(i === 0).onClick(() => move(i - 1)));
s.addExtraButton((b) =>
b
.setIcon("arrow-down")
.setTooltip("Move down")
.setDisabled(i === flow.steps.length - 1)
.onClick(() => move(i + 1)),
);
s.addExtraButton((b) =>
b
.setIcon("x")
.setTooltip("Remove step")
.onClick(async () => {
flow.steps.splice(i, 1);
await this.saveAndRedraw();
}),
);
});
step.effects.forEach((effect, j) =>
group.addSetting((s) =>
s
.setClass("fflow-effect")
.setDesc(j === 0 ? "Also sets" : "")
.addText((t) =>
t
.setPlaceholder("property")
.setValue(effect.property)
.onChange(async (value) => {
effect.property = value.trim();
await this.save();
}),
)
.addText((t) =>
t
.setPlaceholder("value (lists: a, b)")
.setValue(effect.value)
.onChange(async (value) => {
effect.value = value;
await this.save();
}),
)
.addExtraButton((b) =>
b
.setIcon("x")
.setTooltip("Remove property")
.onClick(async () => {
step.effects.splice(j, 1);
await this.saveAndRedraw();
}),
),
),
);
}
/** Opens Settings → Hotkeys filtered on this plugin. `app.setting` and the tab's search box are internal API: probe before use. */
private openHotkeys(): void {
const setting: unknown = Reflect.get(this.app, "setting");
if (!setting || typeof setting !== "object" || !("openTabById" in setting) || typeof setting.openTabById !== "function") return;
const tab: unknown = setting.openTabById("hotkeys");
const search = tab && typeof tab === "object" && "searchComponent" in tab ? tab.searchComponent : null;
if (!(search instanceof SearchComponent)) return;
search.setValue(this.plugin.manifest.name);
search.inputEl.dispatchEvent(new Event("input"));
}
}