@parallelworks/ui 0.37.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,196 @@
1
+ import { FaPlus } from "../icons/fa/FaPlus.js";
2
+ import { IconButton } from "../components/IconButton.js";
3
+ import { Toggle } from "../components/Toggle.js";
4
+ import { useGraphEditorStrings, useInputsEditorStrings } from "./editorStrings.js";
5
+ import { useFieldLint } from "./fieldProblems.js";
6
+ import { asRecord } from "./records.js";
7
+ import { SuggestionInput } from "./SuggestionInput.js";
8
+ import { ExpressionToggle, FieldError, FieldLabel, LabelledField, flagError, flagValue, text } from "./editorFields.js";
9
+ import { useId, useState } from "react";
10
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
11
+ //#region src/graph/inputRefs.tsx
12
+ /** Every input with a value, where a run reads it: groups nest unless flattened, lists hide their rows. */
13
+ function inputRefs(editing, inputs) {
14
+ const root = asRecord(inputs);
15
+ const flatSteps = editing.wizardFlattens(root);
16
+ const out = [];
17
+ const walk = (map, prefix) => {
18
+ for (const [name, raw] of Object.entries(map)) {
19
+ if (name === "$meta") continue;
20
+ const definition = asRecord(raw);
21
+ const type = text(definition["type"]);
22
+ const childKey = editing.inputChildrenKey(type);
23
+ if (type === "group" || type === "step") {
24
+ const flat = type === "step" ? flatSteps : definition["flatten"] === true;
25
+ walk(asRecord(childKey ? definition[childKey] : void 0), flat ? prefix : [...prefix, name]);
26
+ continue;
27
+ }
28
+ if (type === "header") continue;
29
+ out.push({
30
+ path: [...prefix, name],
31
+ type,
32
+ label: text(definition["label"]) || name,
33
+ definition
34
+ });
35
+ }
36
+ };
37
+ walk(root, []);
38
+ return out;
39
+ }
40
+ function refExpression(path, suffix) {
41
+ return `\${{ inputs.${[...path, ...suffix ? [suffix] : []].join(".")} }}`;
42
+ }
43
+ function flagExpression(path, negated) {
44
+ return `\${{ ${negated ? "!" : ""}inputs.${path.join(".")} }}`;
45
+ }
46
+ function equalsExpression(path, value, negated = false) {
47
+ const operator = negated ? "!=" : "==";
48
+ return `\${{ inputs.${path.join(".")} ${operator} '${value.replace(/'/g, "''")}' }}`;
49
+ }
50
+ /** The option values a dropdown input offers, for conditions on it. */
51
+ function optionValues(definition) {
52
+ const options = definition["options"];
53
+ return Array.isArray(options) ? options.map((option) => typeof option === "object" && option !== null ? text(option["value"]) : text(option)) : [];
54
+ }
55
+ /** Makes a new input of `type` for the field, when the host can; `onCreated` gets where it went. */
56
+ function NewInputButton({ type, source, onCreated }) {
57
+ const t = useGraphEditorStrings();
58
+ const inputsEditor = useInputsEditorStrings();
59
+ const create = source?.create;
60
+ if (!create) return null;
61
+ const types = inputsEditor.types;
62
+ return /* @__PURE__ */ jsx(IconButton, {
63
+ icon: /* @__PURE__ */ jsx(FaPlus, { className: "h-4 w-4" }),
64
+ label: t.newInputOf(types[type] ?? type),
65
+ size: "sm",
66
+ variant: "ghost",
67
+ onClick: () => create(type, onCreated)
68
+ });
69
+ }
70
+ var ONE_OF = ["dropdown", "radio"];
71
+ /** A text field that takes any value, expressions included, with suggestions to pick from. */
72
+ function ExpressionInput({ id, label, value, onChange, suggestions = [], placeholder, error, describedBy, required }) {
73
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(SuggestionInput, {
74
+ id,
75
+ ariaLabel: label,
76
+ "aria-describedby": describedBy,
77
+ required,
78
+ value,
79
+ placeholder,
80
+ suggestions,
81
+ onChange
82
+ }), /* @__PURE__ */ jsx(FieldError, { message: error })] });
83
+ }
84
+ /** Conditions on the form's switches and choices, for fields that run on one. */
85
+ function conditionSuggestions(source, t) {
86
+ return (source?.refs ?? []).flatMap((ref) => {
87
+ if (ref.type === "boolean") return [{
88
+ value: flagExpression(ref.path, false),
89
+ label: t.whenOn(ref.label)
90
+ }, {
91
+ value: flagExpression(ref.path, true),
92
+ label: t.whenOff(ref.label)
93
+ }];
94
+ return ONE_OF.includes(ref.type) ? optionValues(ref.definition).map((option) => ({
95
+ value: equalsExpression(ref.path, option),
96
+ label: t.whenIs(ref.label, option)
97
+ })) : [];
98
+ });
99
+ }
100
+ /** A switch, or an expression typed in its place, such as one that follows another input. */
101
+ function FlagField({ label, yamlKey, description, value, original, fallback = false, onChange, source }) {
102
+ const t = useGraphEditorStrings();
103
+ const id = useId();
104
+ const [typed, setTyped] = useState(typeof value === "string");
105
+ const checked = typeof value === "boolean" ? value : fallback;
106
+ const setTypedValue = (next) => onChange(next.trim() ? next : void 0);
107
+ const lint = useFieldLint(yamlKey, id, typeof value === "string" ? value : "", setTypedValue);
108
+ return /* @__PURE__ */ jsxs("div", {
109
+ className: "flex flex-col gap-1.5",
110
+ children: [
111
+ /* @__PURE__ */ jsxs("div", {
112
+ className: "flex items-center justify-between gap-3",
113
+ children: [/* @__PURE__ */ jsx(FieldLabel, {
114
+ label,
115
+ yamlKey,
116
+ description
117
+ }), /* @__PURE__ */ jsxs("div", {
118
+ className: "flex shrink-0 items-center gap-1",
119
+ children: [!typed && /* @__PURE__ */ jsx(Toggle, {
120
+ checked,
121
+ onChange: (on) => onChange(flagValue(on, fallback, original)),
122
+ label
123
+ }), /* @__PURE__ */ jsx(ExpressionToggle, {
124
+ active: typed,
125
+ onChange: (on) => {
126
+ setTyped(on);
127
+ onChange(void 0);
128
+ }
129
+ })]
130
+ })]
131
+ }),
132
+ typed && /* @__PURE__ */ jsxs("div", {
133
+ className: "flex items-center gap-2",
134
+ children: [/* @__PURE__ */ jsx("div", {
135
+ className: "flex-1",
136
+ children: /* @__PURE__ */ jsx(ExpressionInput, {
137
+ id,
138
+ label,
139
+ value: typeof value === "string" ? value : "",
140
+ suggestions: conditionSuggestions(source, t),
141
+ error: flagError(value, t) ?? lint.message,
142
+ onChange: setTypedValue
143
+ })
144
+ }), /* @__PURE__ */ jsx(NewInputButton, {
145
+ type: "boolean",
146
+ source,
147
+ onCreated: (path) => onChange(flagExpression(path, false))
148
+ })]
149
+ }),
150
+ typed && lint.fixes
151
+ ]
152
+ });
153
+ }
154
+ /** A value typed in, with the form's inputs of the given types offered, or a new one made for it. */
155
+ function ValueOrInputField({ label, yamlKey, description, value, onChange, source, types, suffix, literal = true, required = false, error, suggestions = [], placeholder }) {
156
+ const t = useGraphEditorStrings();
157
+ const id = useId();
158
+ const lint = useFieldLint(yamlKey, id, value, onChange);
159
+ const firstType = types[0] ?? "string";
160
+ const offered = [...source.refs.filter((ref) => types.includes(ref.type)).map((ref) => ({
161
+ value: refExpression(ref.path, suffix),
162
+ label: ref.label
163
+ })), ...literal ? suggestions.map((suggestion) => ({ value: suggestion })) : []];
164
+ return /* @__PURE__ */ jsxs(LabelledField, {
165
+ label: required ? `${label} *` : label,
166
+ yamlKey,
167
+ description,
168
+ descriptionId: `${id}-help`,
169
+ children: [
170
+ /* @__PURE__ */ jsxs("div", {
171
+ className: "flex items-center gap-2",
172
+ children: [/* @__PURE__ */ jsx("div", {
173
+ className: "flex-1",
174
+ children: /* @__PURE__ */ jsx(ExpressionInput, {
175
+ id,
176
+ label,
177
+ describedBy: `${id}-help`,
178
+ required,
179
+ value,
180
+ placeholder: placeholder ?? (literal ? void 0 : t.chooseInput),
181
+ suggestions: offered,
182
+ onChange
183
+ })
184
+ }), /* @__PURE__ */ jsx(NewInputButton, {
185
+ type: firstType,
186
+ source,
187
+ onCreated: (path) => onChange(refExpression(path, suffix))
188
+ })]
189
+ }),
190
+ /* @__PURE__ */ jsx(FieldError, { message: error ?? lint.message }),
191
+ lint.fixes
192
+ ]
193
+ });
194
+ }
195
+ //#endregion
196
+ export { ExpressionInput, FlagField, ValueOrInputField, equalsExpression, flagExpression, inputRefs, refExpression };
@@ -0,0 +1,43 @@
1
+ import type { InputPath } from '../editing.js';
2
+ /** One input's row as drawn, with its place among its list's inputs. */
3
+ export interface DrawnRow {
4
+ path: InputPath;
5
+ /** Which copy of the input this is, when a list draws it in each of its rows. */
6
+ instance: string;
7
+ index: number;
8
+ rect: DOMRect;
9
+ el: HTMLElement;
10
+ hidden: boolean;
11
+ /** A hidden input a list with widths draws after the inputs the form shows. */
12
+ trailing: boolean;
13
+ }
14
+ /** Inputs drawn one under another: an input and those `anchor-below` it, top to bottom. */
15
+ export interface Column {
16
+ rows: DrawnRow[];
17
+ /** The cell a list with widths draws the column in. */
18
+ cell: HTMLElement | null;
19
+ }
20
+ /** The columns of one list drawn side by side on one line of the form, left to right. */
21
+ export interface Line {
22
+ parent: InputPath;
23
+ columns: Column[];
24
+ }
25
+ /** Where dragged inputs land: before `index` of `parent`, as a column beside one, or in one. */
26
+ export interface Drop {
27
+ parent: InputPath;
28
+ index: number;
29
+ beside?: {
30
+ path: InputPath;
31
+ side: 'left' | 'right';
32
+ };
33
+ stack?: {
34
+ path: InputPath;
35
+ side: 'above' | 'below';
36
+ };
37
+ }
38
+ export declare const rowsOf: (line: Line) => DrawnRow[];
39
+ export declare function inputCell(el: HTMLElement): HTMLElement | null;
40
+ /** The form's lines: a column is the rows one cell draws, and columns at one height share a line. */
41
+ export declare function linesIn(root: HTMLElement): Line[];
42
+ /** Neighbours on one line, split at `pointer` (a percent of their list's width), in 5% steps. */
43
+ export declare function snapSplit(total: number, pointer: number): [left: number, right: number];
@@ -0,0 +1,65 @@
1
+ //#region src/graph/inputRows.ts
2
+ var key = (path) => JSON.stringify(path);
3
+ var samePath = (a, b) => a.length === b.length && a.every((name, i) => b[i] === name);
4
+ var rowsOf = (line) => line.columns.flatMap((column) => column.rows);
5
+ var topOf = (column) => Math.min(...column.rows.map((row) => row.rect.top));
6
+ var leftOf = (column) => Math.min(...column.rows.map((row) => row.rect.left));
7
+ function inputCell(el) {
8
+ let child = el;
9
+ for (let parent = el.parentElement; parent; parent = parent.parentElement) {
10
+ if (parent.hasAttribute("data-input-path")) return null;
11
+ if (parent.hasAttribute("data-input-cell")) return parent;
12
+ if (parent.parentElement?.hasAttribute("data-layout-grid")) return child;
13
+ if (parent.hasAttribute("data-layout-boundary")) return null;
14
+ child = parent;
15
+ }
16
+ return null;
17
+ }
18
+ /** The form's lines: a column is the rows one cell draws, and columns at one height share a line. */
19
+ function linesIn(root) {
20
+ const columns = /* @__PURE__ */ new Map();
21
+ for (const el of root.querySelectorAll("[data-input-path]")) {
22
+ const path = JSON.parse(el.dataset["inputPath"] ?? "[]");
23
+ const cell = inputCell(el);
24
+ const owner = cell ?? el;
25
+ const entry = columns.get(owner) ?? {
26
+ parent: path.slice(0, -1),
27
+ column: {
28
+ rows: [],
29
+ cell
30
+ }
31
+ };
32
+ columns.set(owner, entry);
33
+ entry.column.rows.push({
34
+ path,
35
+ instance: el.dataset["inputInstance"] ?? key(path),
36
+ index: Number(el.dataset["inputIndex"] ?? 0),
37
+ rect: el.getBoundingClientRect(),
38
+ el,
39
+ hidden: el.dataset["inputHidden"] !== void 0,
40
+ trailing: false
41
+ });
42
+ }
43
+ const lines = [];
44
+ for (const { parent, column } of columns.values()) {
45
+ column.rows.sort((a, b) => a.rect.top - b.rect.top);
46
+ if (column.cell && column.rows.every((row) => row.hidden)) for (const row of column.rows) row.trailing = true;
47
+ const top = topOf(column);
48
+ const line = lines.find((other) => samePath(other.parent, parent) && Math.abs(topOf(other.columns[0]) - top) < 1);
49
+ if (line) line.columns.push(column);
50
+ else lines.push({
51
+ parent,
52
+ columns: [column]
53
+ });
54
+ }
55
+ for (const line of lines) line.columns.sort((a, b) => leftOf(a) - leftOf(b));
56
+ return lines;
57
+ }
58
+ /** Neighbours on one line, split at `pointer` (a percent of their list's width), in 5% steps. */
59
+ function snapSplit(total, pointer) {
60
+ const floor = Math.min(10, total / 2);
61
+ const left = Math.min(Math.max(Math.round(pointer / 5) * 5, floor), total - floor);
62
+ return [left, total - left];
63
+ }
64
+ //#endregion
65
+ export { inputCell, linesIn, rowsOf, snapSplit };
@@ -0,0 +1,62 @@
1
+ import { TOOLTIP_ID } from "../components/Tooltip.js";
2
+ import Loader from "../components/Loader.js";
3
+ import { useGraphEditorStrings } from "./editorStrings.js";
4
+ import { ProblemList } from "./fieldProblems.js";
5
+ import { ChoiceButtons, FieldError } from "./editorFields.js";
6
+ import { Suspense, lazy } from "react";
7
+ import { jsx, jsxs } from "react/jsx-runtime";
8
+ //#region src/graph/settingsViews.tsx
9
+ var MonacoEditor = lazy(() => import("../editor/Monaco.js"));
10
+ function yamlProblem(yaml, t, editing) {
11
+ const problem = editing.settingsYamlProblem(yaml);
12
+ return !problem ? void 0 : problem.kind === "syntax" ? t.yamlSyntax(problem.message) : t.yamlNotMap;
13
+ }
14
+ function ViewSwitch({ yaml, onForm, onYaml }) {
15
+ const t = useGraphEditorStrings();
16
+ return /* @__PURE__ */ jsx("div", {
17
+ "data-tooltip-id": TOOLTIP_ID,
18
+ "data-tooltip-content": t.help.view,
19
+ children: /* @__PURE__ */ jsx(ChoiceButtons, {
20
+ options: [{
21
+ value: "yaml",
22
+ label: t.viewYaml
23
+ }, {
24
+ value: "form",
25
+ label: t.viewForm
26
+ }],
27
+ value: yaml ? "yaml" : "form",
28
+ onChange: (view) => view === "yaml" ? onYaml() : onForm()
29
+ })
30
+ });
31
+ }
32
+ var NO_SCOPED = {
33
+ markers: [],
34
+ problems: []
35
+ };
36
+ function YamlPane({ path, value, onChange, problem, scoped, nested }) {
37
+ return /* @__PURE__ */ jsxs("div", {
38
+ className: "flex flex-col gap-1.5",
39
+ children: [
40
+ /* @__PURE__ */ jsx("div", {
41
+ className: "h-[50vh] overflow-hidden rounded-md border theme-border",
42
+ children: /* @__PURE__ */ jsx(Suspense, {
43
+ fallback: /* @__PURE__ */ jsx(Loader, {}),
44
+ children: /* @__PURE__ */ jsx(MonacoEditor, {
45
+ path,
46
+ language: "yaml",
47
+ value,
48
+ height: "100%",
49
+ scrollBeyondLastLine: false,
50
+ onChange,
51
+ markers: scoped.markers,
52
+ ...nested ? { nested } : {}
53
+ })
54
+ })
55
+ }),
56
+ /* @__PURE__ */ jsx(FieldError, { message: problem }),
57
+ /* @__PURE__ */ jsx(ProblemList, { problems: scoped.problems })
58
+ ]
59
+ });
60
+ }
61
+ //#endregion
62
+ export { NO_SCOPED, ViewSwitch, YamlPane, yamlProblem };