FFLOW 0.2.0: flows with next/previous commands and per-step cssclasses; grouped settings page
This commit is contained in:
1 parent
021777203b
commit
68e390fb37
12 files changed
+486
-165
No files matched your search
+270
-98
@@ -1,125 +1,297 @@
|
||||
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||
import { App, PluginSettingTab, SearchComponent, Setting, SettingGroup } from "obsidian";
|
||||
import type FflowPlugin from "./main";
|
||||
import { BLOCKED_SYMBOLS, SourceMode, symbolError, TriggerRule, ValueMode } from "./settings";
|
||||
import { BLOCKED_SYMBOLS, Flow, NEW_RULE, symbolError, TriggerRule, ValueMode } from "./settings";
|
||||
|
||||
export class FflowSettingTab extends PluginSettingTab {
|
||||
constructor(app: App, private plugin: FflowPlugin) {
|
||||
super(app, plugin);
|
||||
}
|
||||
|
||||
private save = () => this.plugin.saveSettings();
|
||||
|
||||
display(): void {
|
||||
const { containerEl } = this;
|
||||
const settings = this.plugin.settings;
|
||||
containerEl.empty();
|
||||
containerEl.createEl("p", {
|
||||
cls: "setting-item-description",
|
||||
text:
|
||||
"Type a trigger symbol at the start of a word to pick a value. The typed text is removed and the value is written to the note's frontmatter. " +
|
||||
`Reserved symbols: ${BLOCKED_SYMBOLS.join(" ")}`,
|
||||
});
|
||||
|
||||
this.plugin.settings.rules.forEach((rule, index) => this.renderRule(rule, index));
|
||||
|
||||
new Setting(containerEl).addButton((b) =>
|
||||
b
|
||||
.setButtonText("Add trigger")
|
||||
.setCta()
|
||||
.onClick(async () => {
|
||||
this.plugin.settings.rules.push({ symbol: "", property: "", valueMode: "single", source: "open", values: [] });
|
||||
await this.plugin.saveSettings();
|
||||
new Setting(containerEl)
|
||||
.setHeading()
|
||||
.setName("Triggers")
|
||||
.setDesc(
|
||||
`Type a symbol at the start of a word, pick a value: the typed text disappears and the value goes to the frontmatter. Reserved: ${BLOCKED_SYMBOLS.join(" ")}`,
|
||||
)
|
||||
.addButton((b) =>
|
||||
b.setButtonText("Add trigger").onClick(async () => {
|
||||
settings.rules.push({ ...NEW_RULE, values: [] });
|
||||
await this.save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
);
|
||||
settings.rules.forEach((rule, index) => this.renderRule(rule, index));
|
||||
|
||||
new Setting(containerEl)
|
||||
.setHeading()
|
||||
.setName("Flows")
|
||||
.setDesc("Ordered values for a property. Each step can add cssclasses. Every flow gets “next step” and “previous step” commands for hotkeys.")
|
||||
.addButton((b) => b.setButtonText("Hotkeys").onClick(() => this.openHotkeys()))
|
||||
.addButton((b) =>
|
||||
b.setButtonText("Add flow").onClick(async () => {
|
||||
settings.flows.push({ id: Date.now().toString(36), name: "", property: "", loop: false, steps: [{ value: "", cssclass: "" }] });
|
||||
await this.save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
settings.flows.forEach((flow, index) => this.renderFlow(flow, index));
|
||||
}
|
||||
|
||||
private renderRule(rule: TriggerRule, index: number): void {
|
||||
const rules = this.plugin.settings.rules;
|
||||
const save = () => this.plugin.saveSettings();
|
||||
const box = this.containerEl.createDiv({ cls: "fflow-rule" });
|
||||
const settings = this.plugin.settings;
|
||||
const flow = rule.source === "flow" ? settings.flows.find((f) => f.id === rule.flowId) : undefined;
|
||||
|
||||
new Setting(box)
|
||||
.setHeading()
|
||||
.setName(rule.symbol && rule.property ? `${rule.symbol} → ${rule.property}` : "New trigger")
|
||||
.addExtraButton((b) =>
|
||||
b
|
||||
.setIcon("trash")
|
||||
.setTooltip("Delete trigger")
|
||||
.onClick(async () => {
|
||||
rules.splice(index, 1);
|
||||
await save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
const title = createFragment();
|
||||
const titleEl = title.createSpan();
|
||||
const updateTitle = () => {
|
||||
const property = flow ? flow.property : rule.property;
|
||||
titleEl.setText(`${rule.symbol || "?"} → ${property || "?"}`);
|
||||
};
|
||||
updateTitle();
|
||||
|
||||
const symbolSetting = new Setting(box).setName("Symbol").setDesc("One character, not a letter or digit.");
|
||||
const error = symbolSetting.descEl.createDiv({ cls: "fflow-error" });
|
||||
const showError = (value: string) => error.setText(symbolError(value, rules, index) ?? "");
|
||||
symbolSetting.addText((t) => {
|
||||
t.setPlaceholder("@").setValue(rule.symbol);
|
||||
t.inputEl.addClass("fflow-symbol");
|
||||
t.onChange(async (value) => {
|
||||
showError(value);
|
||||
// Invalid input stays in the field but is never saved.
|
||||
if (symbolError(value, rules, index)) return;
|
||||
rule.symbol = value;
|
||||
await save();
|
||||
const group = new SettingGroup(this.containerEl).setHeading(title).addClass("fflow-group");
|
||||
group.addExtraButton((b) =>
|
||||
b
|
||||
.setIcon("trash")
|
||||
.setTooltip("Delete trigger")
|
||||
.onClick(async () => {
|
||||
settings.rules.splice(index, 1);
|
||||
await this.save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
|
||||
group.addSetting((s) => {
|
||||
s.setName("Symbol");
|
||||
const error = s.descEl.createDiv({ cls: "fflow-error" });
|
||||
const check = (value: string) => error.setText(value ? (symbolError(value, settings.rules, index) ?? "") : "Pick a symbol.");
|
||||
check(rule.symbol);
|
||||
s.addText((t) => {
|
||||
t.setPlaceholder("@").setValue(rule.symbol);
|
||||
t.inputEl.addClass("fflow-symbol");
|
||||
t.onChange(async (value) => {
|
||||
check(value);
|
||||
// Invalid input stays in the field but is never saved.
|
||||
if (symbolError(value, settings.rules, index)) return;
|
||||
rule.symbol = value;
|
||||
updateTitle();
|
||||
await this.save();
|
||||
});
|
||||
});
|
||||
});
|
||||
if (rule.symbol) showError(rule.symbol);
|
||||
|
||||
new Setting(box)
|
||||
.setName("Property")
|
||||
.setDesc("Frontmatter property to set.")
|
||||
.addText((t) =>
|
||||
t
|
||||
.setPlaceholder("type")
|
||||
.setValue(rule.property)
|
||||
.onChange(async (value) => {
|
||||
rule.property = value.trim();
|
||||
await save();
|
||||
}),
|
||||
);
|
||||
|
||||
new Setting(box)
|
||||
.setName("Value mode")
|
||||
.setDesc("Single replaces the current value. List adds to the existing values.")
|
||||
.addDropdown((d) =>
|
||||
d
|
||||
.addOptions({ single: "Single value", list: "List" })
|
||||
.setValue(rule.valueMode)
|
||||
.onChange(async (value) => {
|
||||
rule.valueMode = value as ValueMode;
|
||||
await save();
|
||||
}),
|
||||
);
|
||||
|
||||
new Setting(box)
|
||||
.setName("Suggestions")
|
||||
.setDesc("Open: every value already used in the vault; new values are allowed. Finite: only the values listed below.")
|
||||
.addDropdown((d) =>
|
||||
d
|
||||
.addOptions({ open: "Open (from vault)", finite: "Finite list" })
|
||||
.setValue(rule.source)
|
||||
.onChange(async (value) => {
|
||||
rule.source = value as SourceMode;
|
||||
await save();
|
||||
group.addSetting((s) =>
|
||||
s
|
||||
.setName("Values from")
|
||||
.setDesc(
|
||||
rule.source === "open"
|
||||
? "Every value already used for the property in the vault. New values are allowed."
|
||||
: rule.source === "finite"
|
||||
? "Only the values listed below."
|
||||
: "The steps of the flow. Picking one also applies its cssclasses.",
|
||||
)
|
||||
.addDropdown((d) => {
|
||||
d.addOption("open", "Vault (open list)").addOption("finite", "Fixed list");
|
||||
for (const f of settings.flows) d.addOption(`flow:${f.id}`, `Flow: ${f.name || f.property || "unnamed"}`);
|
||||
d.setValue(rule.source === "flow" ? `flow:${rule.flowId}` : rule.source).onChange(async (value) => {
|
||||
if (value.startsWith("flow:")) {
|
||||
rule.source = "flow";
|
||||
rule.flowId = value.slice("flow:".length);
|
||||
} else {
|
||||
rule.source = value as "open" | "finite";
|
||||
}
|
||||
await this.save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
if (rule.source === "flow") {
|
||||
if (!flow) group.addSetting((s) => s.setName("Flow deleted").setDesc("Pick another source.").descEl.addClass("fflow-error"));
|
||||
return;
|
||||
}
|
||||
|
||||
group.addSetting((s) =>
|
||||
s
|
||||
.setName("Property")
|
||||
.addText((t) =>
|
||||
t
|
||||
.setPlaceholder("type")
|
||||
.setValue(rule.property)
|
||||
.onChange(async (value) => {
|
||||
rule.property = value.trim();
|
||||
updateTitle();
|
||||
await this.save();
|
||||
}),
|
||||
)
|
||||
.addDropdown((d) =>
|
||||
d
|
||||
.addOptions({ single: "Single value", list: "List" })
|
||||
.setValue(rule.valueMode)
|
||||
.onChange(async (value) => {
|
||||
rule.valueMode = value as ValueMode;
|
||||
await this.save();
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
if (rule.source === "finite") {
|
||||
new Setting(box)
|
||||
.setName("Values")
|
||||
.setDesc("One value per line, in the order they should appear.")
|
||||
.addTextArea((t) => {
|
||||
t.setValue(rule.values.join("\n")).onChange(async (value) => {
|
||||
rule.values = value
|
||||
.split("\n")
|
||||
.map((v) => v.trim())
|
||||
.filter(Boolean);
|
||||
await save();
|
||||
});
|
||||
t.inputEl.rows = 6;
|
||||
});
|
||||
group.addSetting((s) =>
|
||||
s
|
||||
.setName("Values")
|
||||
.setDesc("One per line, in display order.")
|
||||
.addTextArea((t) => {
|
||||
t.setValue(rule.values.join("\n")).onChange(async (value) => {
|
||||
rule.values = value
|
||||
.split("\n")
|
||||
.map((v) => v.trim())
|
||||
.filter(Boolean);
|
||||
await this.save();
|
||||
});
|
||||
t.inputEl.rows = 5;
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private renderFlow(flow: Flow, index: number): void {
|
||||
const settings = this.plugin.settings;
|
||||
|
||||
const title = createFragment();
|
||||
const titleEl = title.createSpan();
|
||||
const updateTitle = () => titleEl.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 () => {
|
||||
settings.flows.splice(index, 1);
|
||||
// Triggers that used this flow keep writing the same property, from the vault's values.
|
||||
for (const rule of settings.rules) {
|
||||
if (rule.source === "flow" && rule.flowId === flow.id) {
|
||||
rule.source = "open";
|
||||
rule.property = flow.property;
|
||||
}
|
||||
}
|
||||
await this.save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
|
||||
group.addSetting((s) =>
|
||||
s
|
||||
.setName("Name and property")
|
||||
.setDesc("The name labels the hotkey commands.")
|
||||
.addText((t) =>
|
||||
t
|
||||
.setPlaceholder("Status")
|
||||
.setValue(flow.name)
|
||||
.onChange(async (value) => {
|
||||
flow.name = value;
|
||||
updateTitle();
|
||||
await this.save();
|
||||
}),
|
||||
)
|
||||
.addText((t) =>
|
||||
t
|
||||
.setPlaceholder("status")
|
||||
.setValue(flow.property)
|
||||
.onChange(async (value) => {
|
||||
flow.property = value.trim();
|
||||
updateTitle();
|
||||
await this.save();
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
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) => {
|
||||
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.addText((t) =>
|
||||
t
|
||||
.setPlaceholder("cssclasses (optional)")
|
||||
.setValue(step.cssclass)
|
||||
.onChange(async (value) => {
|
||||
step.cssclass = value.trim();
|
||||
await this.save();
|
||||
}),
|
||||
);
|
||||
const move = async (to: number) => {
|
||||
flow.steps.splice(to, 0, ...flow.steps.splice(i, 1));
|
||||
await this.save();
|
||||
this.display();
|
||||
};
|
||||
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.save();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group.addSetting((s) =>
|
||||
s.addButton((b) =>
|
||||
b.setButtonText("Add step").onClick(async () => {
|
||||
flow.steps.push({ value: "", cssclass: "" });
|
||||
await this.save();
|
||||
this.display();
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** 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"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user