FFLOW 0.3.0: everything is a flow; steps set any properties, symbol and hotkeys share them
This commit is contained in:
1 parent
68e390fb37
commit
8553b7c75a
12 files changed
+423
-335
No files matched your search
+161
-204
@@ -1,6 +1,6 @@
|
||||
import { App, PluginSettingTab, SearchComponent, Setting, SettingGroup } from "obsidian";
|
||||
import type FflowPlugin from "./main";
|
||||
import { BLOCKED_SYMBOLS, Flow, NEW_RULE, symbolError, TriggerRule, ValueMode } from "./settings";
|
||||
import { BLOCKED_SYMBOLS, Flow, FlowKind, FlowStep, newFlow, symbolError, ValueMode } from "./settings";
|
||||
|
||||
export class FflowSettingTab extends PluginSettingTab {
|
||||
constructor(app: App, private plugin: FflowPlugin) {
|
||||
@@ -9,164 +9,45 @@ export class FflowSettingTab extends PluginSettingTab {
|
||||
|
||||
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 settings = this.plugin.settings;
|
||||
const flows = this.plugin.settings.flows;
|
||||
containerEl.empty();
|
||||
|
||||
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.")
|
||||
.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 () => {
|
||||
settings.flows.push({ id: Date.now().toString(36), name: "", property: "", loop: false, steps: [{ value: "", cssclass: "" }] });
|
||||
await this.save();
|
||||
this.display();
|
||||
flows.push(newFlow({ steps: [{ value: "", effects: [] }] }));
|
||||
await this.saveAndRedraw();
|
||||
}),
|
||||
);
|
||||
settings.flows.forEach((flow, index) => this.renderFlow(flow, index));
|
||||
}
|
||||
|
||||
private renderRule(rule: TriggerRule, index: number): void {
|
||||
const settings = this.plugin.settings;
|
||||
const flow = rule.source === "flow" ? settings.flows.find((f) => f.id === rule.flowId) : undefined;
|
||||
|
||||
const title = createFragment();
|
||||
const titleEl = title.createSpan();
|
||||
const updateTitle = () => {
|
||||
const property = flow ? flow.property : rule.property;
|
||||
titleEl.setText(`${rule.symbol || "?"} → ${property || "?"}`);
|
||||
};
|
||||
updateTitle();
|
||||
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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") {
|
||||
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;
|
||||
}),
|
||||
);
|
||||
}
|
||||
flows.forEach((flow, index) => this.renderFlow(flow, index));
|
||||
}
|
||||
|
||||
private renderFlow(flow: Flow, index: number): void {
|
||||
const settings = this.plugin.settings;
|
||||
const flows = this.plugin.settings.flows;
|
||||
|
||||
const title = createFragment();
|
||||
const titleEl = title.createSpan();
|
||||
const updateTitle = () => titleEl.setText(flow.name || flow.property || "New flow");
|
||||
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");
|
||||
@@ -175,23 +56,15 @@ export class FflowSettingTab extends PluginSettingTab {
|
||||
.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();
|
||||
flows.splice(index, 1);
|
||||
await this.saveAndRedraw();
|
||||
}),
|
||||
);
|
||||
|
||||
group.addSetting((s) =>
|
||||
s
|
||||
.setName("Name and property")
|
||||
.setDesc("The name labels the hotkey commands.")
|
||||
.setName("Name")
|
||||
.setDesc(flow.kind === "ordered" ? "Labels the hotkey commands." : "For your reference.")
|
||||
.addText((t) =>
|
||||
t
|
||||
.setPlaceholder("Status")
|
||||
@@ -201,6 +74,34 @@ export class FflowSettingTab extends PluginSettingTab {
|
||||
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
|
||||
@@ -211,8 +112,30 @@ export class FflowSettingTab extends PluginSettingTab {
|
||||
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
|
||||
@@ -226,64 +149,98 @@ export class FflowSettingTab extends PluginSettingTab {
|
||||
),
|
||||
);
|
||||
|
||||
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();
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
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: "", cssclass: "" });
|
||||
await this.save();
|
||||
this.display();
|
||||
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");
|
||||
|
||||
Reference in new issue
Block a user