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
@@ -0,0 +1,63 @@
|
||||
import { Notice, TFile } from "obsidian";
|
||||
import { setFlowStep, updateFrontmatter } from "./frontmatter";
|
||||
import type FflowPlugin from "./main";
|
||||
import { Flow, flowSteps } from "./settings";
|
||||
|
||||
type Direction = 1 | -1;
|
||||
|
||||
/** Re-registers the next/previous commands of every flow. Ids derive from the flow id, so hotkeys survive renames. */
|
||||
export class FlowCommands {
|
||||
private registered: string[] = [];
|
||||
|
||||
constructor(private plugin: FflowPlugin) {}
|
||||
|
||||
sync(): void {
|
||||
for (const id of this.registered) this.plugin.removeCommand(id);
|
||||
this.registered = [];
|
||||
for (const flow of this.plugin.settings.flows) {
|
||||
this.add(flow, 1, "next", "next step");
|
||||
this.add(flow, -1, "previous", "previous step");
|
||||
}
|
||||
}
|
||||
|
||||
private add(flow: Flow, direction: Direction, suffix: string, label: string): void {
|
||||
const id = `flow-${flow.id}-${suffix}`;
|
||||
this.registered.push(id);
|
||||
this.plugin.addCommand({
|
||||
id,
|
||||
name: `${flow.name || flow.property || "Unnamed flow"}: ${label}`,
|
||||
checkCallback: (checking) => {
|
||||
const file = this.plugin.app.workspace.getActiveFile();
|
||||
if (!file || file.extension !== "md" || !flow.property || flowSteps(flow).length === 0) return false;
|
||||
if (!checking) void moveStep(this.plugin, file, flow, direction);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function moveStep(plugin: FflowPlugin, file: TFile, flow: Flow, direction: Direction): Promise<void> {
|
||||
const steps = flowSteps(flow);
|
||||
let message = "";
|
||||
try {
|
||||
// Read the current value inside the update: the metadata cache can lag behind the editor.
|
||||
await updateFrontmatter(plugin.app, file, (fm) => {
|
||||
const index = steps.findIndex((s) => s.value === String(fm[flow.property]));
|
||||
// A note outside the flow (no value, or a value not in the steps) enters at the first step.
|
||||
let target = index === -1 ? 0 : index + direction;
|
||||
if (target < 0 || target >= steps.length) {
|
||||
if (!flow.loop) {
|
||||
message = `${flow.property}: already at the ${direction === 1 ? "last" : "first"} step`;
|
||||
return;
|
||||
}
|
||||
target = (target + steps.length) % steps.length;
|
||||
}
|
||||
setFlowStep(fm, flow, steps[target].value);
|
||||
message = `${flow.property}: ${steps[target].value}`;
|
||||
});
|
||||
new Notice(message);
|
||||
} catch (err) {
|
||||
console.error("FFLOW: could not move flow step", err);
|
||||
new Notice(`FFLOW: could not set "${flow.property}". See console.`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user