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
+78
-34
@@ -1,71 +1,115 @@
|
||||
/** 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";
|
||||
export type SourceMode = "open" | "finite" | "flow";
|
||||
|
||||
export interface TriggerRule {
|
||||
/** One non-alphanumeric character that opens the suggestions. */
|
||||
symbol: string;
|
||||
/** Frontmatter property written on selection. Ignored for flow sources: the flow owns it. */
|
||||
/** Another property written when the note reaches a step. */
|
||||
export interface FlowEffect {
|
||||
property: string;
|
||||
/** single: replace the value. list: append to a list, no duplicates. Flows are always single. */
|
||||
valueMode: ValueMode;
|
||||
/** open: values already used in the vault. finite: `values`. flow: the steps of `flowId`. */
|
||||
source: SourceMode;
|
||||
values: string[];
|
||||
flowId: string;
|
||||
/** Scalars: true/false and numbers are typed. List properties: comma-separated items. */
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface FlowStep {
|
||||
value: string;
|
||||
/** Space-separated classes added to `cssclasses` while the note is on this step. */
|
||||
cssclass: 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 {
|
||||
rules: TriggerRule[];
|
||||
flows: Flow[];
|
||||
}
|
||||
|
||||
export const NEW_RULE: TriggerRule = { symbol: "", property: "", valueMode: "single", source: "open", values: [], flowId: "" };
|
||||
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 const DEFAULT_SETTINGS: FflowSettings = {
|
||||
rules: [{ ...NEW_RULE, symbol: "@", property: "type" }],
|
||||
flows: [],
|
||||
};
|
||||
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 rule `index`. */
|
||||
export function symbolError(symbol: string, rules: TriggerRule[], index: number): string | null {
|
||||
/** 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 (rules.some((r, i) => i !== index && r.symbol === symbol)) return `"${symbol}" is already used by another trigger.`;
|
||||
if (flows.some((f, i) => i !== index && f.symbol === symbol)) return `"${symbol}" is already used by another flow.`;
|
||||
return null;
|
||||
}
|
||||
|
||||
/** What a usable trigger writes to; null when the rule is incomplete or points to a missing flow. */
|
||||
export function resolveRule(settings: FflowSettings, index: number): { property: string; flow: Flow | null } | null {
|
||||
const rule = settings.rules[index];
|
||||
if (symbolError(rule.symbol, settings.rules, index)) return null;
|
||||
if (rule.source === "flow") {
|
||||
const flow = settings.flows.find((f) => f.id === rule.flowId);
|
||||
return flow && flow.property ? { property: flow.property, flow } : null;
|
||||
}
|
||||
return rule.property ? { property: rule.property, flow: null } : 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 }[];
|
||||
}
|
||||
|
||||
/** Steps usable at runtime: blank values are skipped. */
|
||||
export function flowSteps(flow: Flow): FlowStep[] {
|
||||
return flow.steps.filter((s) => s.value.trim() !== "");
|
||||
/** 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 };
|
||||
}
|
||||
Reference in new issue
Block a user