foxygit / RPA-Remisser Log in
commits tags

/webui/frontend/src/lib/stepops.ts · 1.27 KB

raw
import type { Branch, Step } from "./api";
import { uid } from "./ids";
import type { Schema } from "./schema";

export function newBranch(): Branch {
  return { condition: { kind: "element_exists" } };
}

/** Ny nod av given typ med schemats defaultvärden och en canvas-position. */
export function newNode(schema: Schema, type: string, position = { x: 0, y: 0 }): Step {
  const spec = schema.step_types.find((s) => s.type === type);
  const node: Step = { type, id: uid(), position: { ...position } };
  for (const f of spec?.fields ?? []) {
    if (f.default !== undefined && f.default !== "") node[f.key] = f.default;
  }
  if (spec?.control === "if") node.branches = [newBranch()];
  if (type === "extract_row") node.fields = [{ name: "", by: "css", value: "" }];
  if (type === "call_flow") {
    node.target = "";
    node.in = [];
    node.out = [];
  }
  return node;
}

/** Byter typ men behåller id/position/label/enabled/on_error. */
export function retype(schema: Schema, node: Step, type: string): Step {
  const fresh = newNode(schema, type, node.position ?? { x: 0, y: 0 });
  fresh.id = node.id;
  if (node.label) fresh.label = node.label;
  if (node.enabled === false) fresh.enabled = false;
  if (node.on_error === "continue") fresh.on_error = "continue";
  return fresh;
}