Files
fflow/src/settings.ts
T

116 lines
4.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/** ordered: a fixed list of steps with effects and next/previous commands. open: any value already used in the vault. */
export type FlowKind = "ordered" | "open";
/** single: replace the value. list: append to a list, no duplicates. Ordered flows are always single. */
export type ValueMode = "single" | "list";
/** Another property written when the note reaches a step. */
export interface FlowEffect {
property: string;
/** Scalars: true/false and numbers are typed. List properties: comma-separated items. */
value: string;
}
export interface FlowStep {
value: string;
effects: FlowEffect[];
}
export interface Flow {
/** Stable id: command ids derive from it, so hotkeys survive renames. */
id: string;
name: string;
/** Optional trigger: one non-alphanumeric character typed at the start of a word. */
symbol: string;
property: string;
kind: FlowKind;
valueMode: ValueMode;
/** After the last step, "next" goes back to the first (and "previous" the other way). */
loop: boolean;
steps: FlowStep[];
}
export interface FflowSettings {
flows: Flow[];
}
export function newFlow(fields: Partial<Flow>): Flow {
return {
id: Date.now().toString(36) + Math.random().toString(36).slice(2, 6),
name: "",
symbol: "",
property: "",
kind: "ordered",
valueMode: "single",
loop: false,
steps: [],
...fields,
};
}
export function defaultSettings(): FflowSettings {
return { flows: [newFlow({ name: "Type", symbol: "@", property: "type", kind: "open" })] };
}
/** Reserved by Obsidian or common plugins: tags, links, slash commands, emoji shortcodes. */
export const BLOCKED_SYMBOLS = ["#", "[", "/", ":"];
/** Returns an error message, or null when `symbol` is usable by flow `index`. Empty means "no trigger" and is valid. */
export function symbolError(symbol: string, flows: Flow[], index: number): string | null {
if (symbol === "") return null;
if ([...symbol].length !== 1) return "Use exactly one character.";
if (/[\p{L}\p{N}\s]/u.test(symbol)) return "Letters, digits and spaces are not allowed.";
if (BLOCKED_SYMBOLS.includes(symbol)) return `"${symbol}" is reserved (${BLOCKED_SYMBOLS.join(" ")}).`;
if (flows.some((f, i) => i !== index && f.symbol === symbol)) return `"${symbol}" is already used by another flow.`;
return null;
}
/** 0.1–0.2 stored separate triggers and flows; the shapes below are only read during migration. */
interface LegacyRule {
symbol: string;
property: string;
valueMode: ValueMode;
source: "open" | "finite" | "flow";
values: string[];
flowId?: string;
}
interface LegacyFlow {
id: string;
name: string;
property: string;
loop: boolean;
steps: { value: string; cssclass: string }[];
}
/** Builds settings from saved data, converting the 0.1–0.2 trigger/flow split into flows. */
export function loadSettings(saved: unknown): FflowSettings {
if (!saved || typeof saved !== "object") return defaultSettings();
if ("flows" in saved && !("rules" in saved) && Array.isArray(saved.flows)) {
// Current format: fill fields added since the flow was saved.
return { flows: saved.flows.map((f: Partial<Flow>) => newFlow(f)) };
}
// Own persisted blob from an older version of this plugin; fields were always written together.
const legacy = saved as { rules?: LegacyRule[]; flows?: LegacyFlow[] };
const rules = legacy.rules ?? [];
const flows: Flow[] = (legacy.flows ?? []).map((f) =>
newFlow({
id: f.id,
name: f.name,
property: f.property,
loop: f.loop,
symbol: rules.find((r) => r.source === "flow" && r.flowId === f.id)?.symbol ?? "",
steps: f.steps.map((s) => ({
value: s.value,
effects: s.cssclass.trim() ? [{ property: "cssclasses", value: s.cssclass.trim().split(/\s+/).join(", ") }] : [],
})),
}),
);
for (const r of rules) {
if (r.source === "open") flows.push(newFlow({ name: r.property, symbol: r.symbol, property: r.property, kind: "open", valueMode: r.valueMode }));
if (r.source === "finite") {
flows.push(newFlow({ name: r.property, symbol: r.symbol, property: r.property, steps: r.values.map((value) => ({ value, effects: [] })) }));
}
}
return { flows };
}