FFLOW 0.1.0: trigger symbols set frontmatter properties
This commit is contained in:
commit
021777203b
15 files changed
+1189
No files matched your search
@@ -0,0 +1,125 @@
|
||||
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||
import type FflowPlugin from "./main";
|
||||
import { BLOCKED_SYMBOLS, SourceMode, symbolError, TriggerRule, ValueMode } from "./settings";
|
||||
|
||||
export class FflowSettingTab extends PluginSettingTab {
|
||||
constructor(app: App, private plugin: FflowPlugin) {
|
||||
super(app, plugin);
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const { containerEl } = this;
|
||||
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();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
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" });
|
||||
|
||||
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 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();
|
||||
});
|
||||
});
|
||||
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();
|
||||
this.display();
|
||||
}),
|
||||
);
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user