@motifx-community/ui 0.1.0-beta.14 → 0.1.0-beta.16
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.
- package/CHANGELOG.md +113 -0
- package/README.md +2 -2
- package/dist/components/diagram/Diagram.d.ts +607 -0
- package/dist/components/diagram/DiagramAutoPan.d.ts +17 -0
- package/dist/components/diagram/DiagramBends.d.ts +70 -0
- package/dist/components/diagram/DiagramDragging.d.ts +66 -0
- package/dist/components/diagram/DiagramEdgeView.d.ts +78 -0
- package/dist/components/diagram/DiagramGrid.d.ts +13 -0
- package/dist/components/diagram/DiagramHistory.d.ts +16 -0
- package/dist/components/diagram/DiagramLinks.d.ts +177 -0
- package/dist/components/diagram/DiagramMap.d.ts +62 -0
- package/dist/components/diagram/DiagramNodeView.d.ts +143 -0
- package/dist/components/diagram/DiagramOverview.d.ts +20 -0
- package/dist/components/diagram/DiagramPanning.d.ts +34 -0
- package/dist/components/diagram/diagram-alignment.d.ts +2 -0
- package/dist/components/diagram/diagram-auto-pan-host.d.ts +8 -0
- package/dist/components/diagram/diagram-background.d.ts +2 -0
- package/dist/components/diagram/diagram-bends-host.d.ts +17 -0
- package/dist/components/diagram/diagram-card-kind.d.ts +42 -0
- package/dist/components/diagram/diagram-card-node.d.ts +11 -0
- package/dist/components/diagram/diagram-center-options.d.ts +6 -0
- package/dist/components/diagram/diagram-change.d.ts +11 -0
- package/dist/components/diagram/diagram-class-member.d.ts +13 -0
- package/dist/components/diagram/diagram-class-node.d.ts +12 -0
- package/dist/components/diagram/diagram-column-checked-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connected-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-connecting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-connection.d.ts +7 -0
- package/dist/components/diagram/diagram-data.d.ts +12 -0
- package/dist/components/diagram/diagram-deleted-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-deleting-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-dragging-host.d.ts +47 -0
- package/dist/components/diagram/diagram-drawn-node.d.ts +10 -0
- package/dist/components/diagram/diagram-edge-end.d.ts +2 -0
- package/dist/components/diagram/diagram-edge-path.d.ts +5 -0
- package/dist/components/diagram/diagram-edge-reshaped-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-edge-reshaping-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-edge.d.ts +49 -0
- package/dist/components/diagram/diagram-edit.d.ts +10 -0
- package/dist/components/diagram/diagram-export-options.d.ts +14 -0
- package/dist/components/diagram/diagram-export.d.ts +28 -0
- package/dist/components/diagram/diagram-fit-options.d.ts +12 -0
- package/dist/components/diagram/diagram-history-changed-event-args.d.ts +14 -0
- package/dist/components/diagram/diagram-history-step.d.ts +9 -0
- package/dist/components/diagram/diagram-kinds.d.ts +9 -0
- package/dist/components/diagram/diagram-layered.d.ts +39 -0
- package/dist/components/diagram/diagram-layout-direction.d.ts +2 -0
- package/dist/components/diagram/diagram-layout-options.d.ts +24 -0
- package/dist/components/diagram/diagram-layout-type.d.ts +5 -0
- package/dist/components/diagram/diagram-layout.d.ts +33 -0
- package/dist/components/diagram/diagram-line-look.d.ts +6 -0
- package/dist/components/diagram/diagram-line.d.ts +11 -0
- package/dist/components/diagram/diagram-links-host.d.ts +45 -0
- package/dist/components/diagram/diagram-links-options.d.ts +7 -0
- package/dist/components/diagram/diagram-map-props.d.ts +10 -0
- package/dist/components/diagram/diagram-map-target.d.ts +27 -0
- package/dist/components/diagram/diagram-marker.d.ts +6 -0
- package/dist/components/diagram/diagram-mini-map-options.d.ts +13 -0
- package/dist/components/diagram/diagram-mini-map-position.d.ts +2 -0
- package/dist/components/diagram/diagram-node-context.d.ts +26 -0
- package/dist/components/diagram/diagram-node-host.d.ts +23 -0
- package/dist/components/diagram/diagram-node-kind.d.ts +43 -0
- package/dist/components/diagram/diagram-node-move.d.ts +11 -0
- package/dist/components/diagram/diagram-node-resized-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-node-resizing-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-node-size.d.ts +8 -0
- package/dist/components/diagram/diagram-node-status.d.ts +2 -0
- package/dist/components/diagram/diagram-node-toggled-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-node.d.ts +56 -0
- package/dist/components/diagram/diagram-nodes-moved-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-nodes-moving-event-args.d.ts +6 -0
- package/dist/components/diagram/diagram-panning-host.d.ts +19 -0
- package/dist/components/diagram/diagram-pasted-event-args.d.ts +7 -0
- package/dist/components/diagram/diagram-pasting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-path-ends.d.ts +11 -0
- package/dist/components/diagram/diagram-path-result.d.ts +6 -0
- package/dist/components/diagram/diagram-paths.d.ts +35 -0
- package/dist/components/diagram/diagram-point.d.ts +5 -0
- package/dist/components/diagram/diagram-port-place.d.ts +9 -0
- package/dist/components/diagram/diagram-port.d.ts +27 -0
- package/dist/components/diagram/diagram-props.d.ts +159 -0
- package/dist/components/diagram/diagram-reconnected-event-args.d.ts +10 -0
- package/dist/components/diagram/diagram-reconnecting-event-args.d.ts +11 -0
- package/dist/components/diagram/diagram-rect.d.ts +7 -0
- package/dist/components/diagram/diagram-resize-side.d.ts +2 -0
- package/dist/components/diagram/diagram-route.d.ts +10 -0
- package/dist/components/diagram/diagram-selection-changed-event-args.d.ts +9 -0
- package/dist/components/diagram/diagram-selection.d.ts +5 -0
- package/dist/components/diagram/diagram-side.d.ts +2 -0
- package/dist/components/diagram/diagram-table-column.d.ts +13 -0
- package/dist/components/diagram/diagram-table-node.d.ts +12 -0
- package/dist/components/diagram/diagram-tree.d.ts +14 -0
- package/dist/components/diagram/diagram-viewport-changed-event-args.d.ts +8 -0
- package/dist/components/diagram/diagram-viewport-options.d.ts +5 -0
- package/dist/components/diagram/diagram-viewport.d.ts +6 -0
- package/dist/components/diagram/index.d.ts +65 -0
- package/dist/components/diagram/swallow-next-click.d.ts +5 -0
- package/dist/components/expressioneditor/ExpressionEditor.d.ts +111 -0
- package/dist/components/expressioneditor/expression-editor-props.d.ts +24 -0
- package/dist/components/expressioneditor/index.d.ts +2 -0
- package/dist/components/formlayout/FormLayout.d.ts +10 -3
- package/dist/components/formlayout/form-editor-type.d.ts +1 -1
- package/dist/components/formlayout/form-item.d.ts +4 -1
- package/dist/components/formlayout/form-layout-props.d.ts +1 -1
- package/dist/components/formlayout/index.d.ts +1 -0
- package/dist/components/formlayout/schema-items.d.ts +26 -0
- package/dist/components/pivotgrid/model/PivotAxisWindow.d.ts +53 -1
- package/dist/components/pivotgrid/model/PivotCellWindow.d.ts +2 -0
- package/dist/components/pivotgrid/model/PivotWindows.d.ts +3 -6
- package/dist/components/toolbar/Toolbar.d.ts +5 -3
- package/dist/core/control.d.ts +2 -0
- package/dist/core/keyboard.d.ts +5 -0
- package/dist/core/measure/index.d.ts +2 -0
- package/dist/core/measure/picture.d.ts +27 -0
- package/dist/core/shortcut-options.d.ts +5 -2
- package/dist/core/ui-strings.d.ts +191 -0
- package/dist/generated/tokens.d.ts +1615 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +11501 -705
- package/dist/index.js.map +1 -1
- package/dist/locales/en.json +180 -1
- package/dist/locales/tr.json +180 -1
- package/dist/styles.css +2262 -3
- package/dist/tokens/index.d.ts +1615 -0
- package/dist/tokens/index.js +1615 -0
- package/dist/tokens/index.js.map +1 -1
- package/dist/{tr-BGbF9uCs.js → tr-4GNbFdQh.js} +361 -3
- package/dist/{tr-BGbF9uCs.js.map → tr-4GNbFdQh.js.map} +1 -1
- package/docs/accordion.md +1 -1
- package/docs/actionsheet.md +1 -1
- package/docs/appbar.md +1 -1
- package/docs/appmenu-api.md +1 -1
- package/docs/appmenu-programmatic.md +1 -1
- package/docs/appmenu-search.md +1 -1
- package/docs/appmenu.md +1 -1
- package/docs/avatar.md +1 -1
- package/docs/badge.md +1 -1
- package/docs/bar-gauge-angles.md +1 -1
- package/docs/bar-gauge-data.md +1 -1
- package/docs/bar-gauge-labels.md +1 -1
- package/docs/bar-gauge.md +1 -1
- package/docs/barcode.md +1 -1
- package/docs/bottomnavigation.md +1 -1
- package/docs/bottomsheet.md +1 -1
- package/docs/breadcrumb.md +1 -1
- package/docs/button.md +1 -1
- package/docs/buttongroup.md +1 -1
- package/docs/buttons.md +1 -1
- package/docs/calendar.md +1 -1
- package/docs/card.md +1 -1
- package/docs/chart-bars.md +1 -1
- package/docs/chart-candles.md +1 -1
- package/docs/chart-funnel.md +1 -1
- package/docs/chart-heatmap.md +1 -1
- package/docs/chart-lines.md +1 -1
- package/docs/chart-live.md +1 -1
- package/docs/chart-pie.md +1 -1
- package/docs/chart-radar.md +1 -1
- package/docs/chart-ranges.md +1 -1
- package/docs/chart-references.md +1 -1
- package/docs/chart-scatter.md +1 -1
- package/docs/chart-waterfall.md +1 -1
- package/docs/chart-zoom.md +1 -1
- package/docs/chart.md +1 -1
- package/docs/chat.md +1 -1
- package/docs/checkbox.md +1 -1
- package/docs/chip.md +1 -1
- package/docs/choice.md +1 -1
- package/docs/colorbox.md +1 -1
- package/docs/combobox.md +1 -1
- package/docs/commands.md +1 -1
- package/docs/control.md +1 -1
- package/docs/data.md +2 -2
- package/docs/datagrid-api.md +1 -1
- package/docs/datagrid-bands.md +1 -1
- package/docs/datagrid-drag-select.md +1 -1
- package/docs/datagrid-drag.md +1 -1
- package/docs/datagrid-editing.md +1 -1
- package/docs/datagrid-filtering.md +1 -1
- package/docs/datagrid-forms.md +1 -1
- package/docs/datagrid-grouping.md +1 -1
- package/docs/datagrid-layout.md +1 -1
- package/docs/datagrid-lookups.md +1 -1
- package/docs/datagrid-master-detail.md +1 -1
- package/docs/datagrid-scrolling.md +1 -1
- package/docs/datagrid-selection.md +1 -1
- package/docs/datagrid.md +1 -1
- package/docs/datalayer-api.md +491 -0
- package/docs/datalayer-changes.md +501 -0
- package/docs/datalayer-odata.md +998 -0
- package/docs/datalayer-pivot.md +850 -0
- package/docs/datalayer-queries.md +516 -0
- package/docs/datalayer-rest.md +529 -0
- package/docs/datalayer-soap.md +576 -0
- package/docs/datalayer-source.md +464 -0
- package/docs/datalayer.md +264 -0
- package/docs/datebox.md +1 -1
- package/docs/daterangebox.md +1 -1
- package/docs/diagram-api.md +200 -0
- package/docs/diagram-commands.md +79 -0
- package/docs/diagram-connections.md +157 -0
- package/docs/diagram-custom-nodes.md +325 -0
- package/docs/diagram-designer.md +816 -0
- package/docs/diagram-editing.md +62 -0
- package/docs/diagram-flow.md +157 -0
- package/docs/diagram-keyboard.md +58 -0
- package/docs/diagram-layout.md +354 -0
- package/docs/diagram-nodes.md +206 -0
- package/docs/diagram-tables.md +212 -0
- package/docs/diagram.md +217 -0
- package/docs/display.md +1 -1
- package/docs/draggable.md +1 -1
- package/docs/drawer.md +1 -1
- package/docs/dropdown.md +1 -1
- package/docs/editors.md +1 -1
- package/docs/expressioneditor.md +469 -0
- package/docs/expressions.md +431 -0
- package/docs/feedback.md +1 -1
- package/docs/fileuploader.md +1 -1
- package/docs/filterbuilder.md +1 -1
- package/docs/formlayout-schema.md +143 -0
- package/docs/formlayout.md +16 -4
- package/docs/foundations.md +1 -1
- package/docs/gallery.md +1 -1
- package/docs/gauge-base.md +1 -1
- package/docs/gauge-circular.md +1 -1
- package/docs/gauge-dashboards.md +1 -1
- package/docs/gauge-linear.md +1 -1
- package/docs/gauge-markers.md +1 -1
- package/docs/gauge-marks.md +1 -1
- package/docs/gauge-ranges.md +1 -1
- package/docs/gauge-scales.md +1 -1
- package/docs/gauge.md +1 -1
- package/docs/home.md +1 -1
- package/docs/keyboard.md +2 -2
- package/docs/language.md +1 -1
- package/docs/layout.md +1 -1
- package/docs/listbox.md +1 -1
- package/docs/loadpanel.md +1 -1
- package/docs/lookup.md +1 -1
- package/docs/markdown.md +1 -1
- package/docs/menus-api.md +1 -1
- package/docs/menus-context.md +1 -1
- package/docs/menus-customization.md +1 -1
- package/docs/menus-merging.md +1 -1
- package/docs/menus-programmatic.md +1 -1
- package/docs/menus.md +1 -1
- package/docs/multiview.md +1 -1
- package/docs/navigation.md +1 -1
- package/docs/numberbox.md +1 -1
- package/docs/otpinput.md +1 -1
- package/docs/pagination.md +1 -1
- package/docs/pivotgrid-api.md +1 -1
- package/docs/pivotgrid-chart.md +1 -1
- package/docs/pivotgrid-drilldown.md +1 -1
- package/docs/pivotgrid-export.md +1 -1
- package/docs/pivotgrid-fields.md +1 -1
- package/docs/pivotgrid-keyboard.md +1 -1
- package/docs/pivotgrid-layout.md +1 -1
- package/docs/pivotgrid-panel.md +1 -1
- package/docs/pivotgrid-remote.md +122 -44
- package/docs/pivotgrid-sorting.md +3 -3
- package/docs/pivotgrid-state.md +1 -1
- package/docs/pivotgrid-summaries.md +1 -1
- package/docs/pivotgrid.md +1 -1
- package/docs/popover.md +1 -1
- package/docs/progress.md +1 -1
- package/docs/qrcode.md +1 -1
- package/docs/radiogroup.md +1 -1
- package/docs/rating.md +1 -1
- package/docs/resizable.md +1 -1
- package/docs/ribbonmenu-api.md +1 -1
- package/docs/ribbonmenu-commands.md +1 -1
- package/docs/ribbonmenu-customization.md +1 -1
- package/docs/ribbonmenu-display.md +1 -1
- package/docs/ribbonmenu-layouts.md +1 -1
- package/docs/ribbonmenu-programmatic.md +1 -1
- package/docs/ribbonmenu.md +1 -1
- package/docs/scheduler-agenda.md +1 -1
- package/docs/scheduler-big-data.md +1 -1
- package/docs/scheduler-editing.md +1 -1
- package/docs/scheduler-form.md +1 -1
- package/docs/scheduler-icalendar.md +1 -1
- package/docs/scheduler-mobile.md +1 -1
- package/docs/scheduler-recurrence.md +1 -1
- package/docs/scheduler-resources.md +1 -1
- package/docs/scheduler-timeline.md +1 -1
- package/docs/scheduler.md +1 -1
- package/docs/scrollview.md +1 -1
- package/docs/shortcuteditor.md +1 -1
- package/docs/showcase-flowstudio.md +61 -0
- package/docs/showcase-home.md +1 -1
- package/docs/showcase-mail.md +1 -1
- package/docs/showcase.md +1 -1
- package/docs/signature.md +1 -1
- package/docs/skeleton.md +1 -1
- package/docs/slider.md +1 -1
- package/docs/sortable.md +1 -1
- package/docs/sparkline.md +1 -1
- package/docs/speeddial.md +1 -1
- package/docs/splitter.md +1 -1
- package/docs/stepper.md +1 -1
- package/docs/switch.md +1 -1
- package/docs/tabdocs-dock.md +1 -1
- package/docs/tabdocs-drag.md +1 -1
- package/docs/tabdocs-menus.md +1 -1
- package/docs/tabdocs-mobile.md +1 -1
- package/docs/tabdocs-tabs.md +1 -1
- package/docs/tabdocs-windows.md +1 -1
- package/docs/tabdocs-workspace.md +1 -1
- package/docs/tabdocs.md +1 -1
- package/docs/tabs.md +1 -1
- package/docs/tagbox.md +1 -1
- package/docs/textarea.md +1 -1
- package/docs/textbox.md +1 -1
- package/docs/tileview.md +1 -1
- package/docs/timeline.md +1 -1
- package/docs/toast.md +1 -1
- package/docs/tokens.md +324 -1
- package/docs/toolbar-api.md +1 -1
- package/docs/toolbar-customization.md +1 -1
- package/docs/toolbar.md +1 -1
- package/docs/tooltip.md +1 -1
- package/docs/treeview.md +1 -1
- package/docs/validationsummary.md +1 -1
- package/docs/window.md +1 -1
- package/docs/wizard.md +1 -1
- package/llms-full.txt +10142 -462
- package/llms.txt +32 -1
- package/package.json +2 -2
|
@@ -0,0 +1,816 @@
|
|
|
1
|
+
# Diagram: Build your own flow designer
|
|
2
|
+
|
|
3
|
+
> Diagram · @motifx-community/ui 0.1.0-beta.16
|
|
4
|
+
|
|
5
|
+
A small flow designer, from a kind of step to a run of the flow: its card, the form of its settings, the data it reads from the steps before it, the checks, the definition a server reads, and the run. The designer is on this page with its code, and each section below takes one piece of it.
|
|
6
|
+
|
|
7
|
+
## The designer
|
|
8
|
+
|
|
9
|
+
Select a step to set it up, add one after it, join the ports, and run the flow. Its code is three files: `DesignerSteps.ts` has the kinds of step, `DesignerFlow.ts` what the flow means, and `Designer.tsx` the view. Copy them, and write your own kinds.
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { expression, type jsonSchema } from '@motifx-community/data';
|
|
13
|
+
import type { DiagramCardNode, DiagramPort } from '@motifx-community/ui';
|
|
14
|
+
|
|
15
|
+
/** A step of the flow: a card, with the settings of its kind. It is plain data, but for the icon. */
|
|
16
|
+
export interface StepNode extends DiagramCardNode {
|
|
17
|
+
kind: string;
|
|
18
|
+
settings: Record<string, unknown>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A kind of step: everything the designer knows about it. */
|
|
22
|
+
export interface StepKind {
|
|
23
|
+
title: string;
|
|
24
|
+
/** The name of its icon, one of the kit's. */
|
|
25
|
+
icon: string;
|
|
26
|
+
ports: DiagramPort[];
|
|
27
|
+
/** The schema of its settings: the form is made from it, and the settings are checked against it. */
|
|
28
|
+
settings: jsonSchema.JsonSchema;
|
|
29
|
+
/** The type of what it gives, which the steps after it read as `steps.<id>`. */
|
|
30
|
+
gives: expression.Type;
|
|
31
|
+
/** What it is set to, in the line under its title. */
|
|
32
|
+
line: (settings: Record<string, unknown>) => string;
|
|
33
|
+
/** Its work, with its settings worked out. Here it is made up; on a server it reads, calls and sends. */
|
|
34
|
+
run: (settings: Record<string, unknown>) => unknown;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const IN: DiagramPort = { id: 'in', side: 'left', direction: 'target', maxConnections: 1 };
|
|
38
|
+
const OUT: DiagramPort = { id: 'out', side: 'right', direction: 'source', maxConnections: 1 };
|
|
39
|
+
|
|
40
|
+
const text = (value: unknown) => (typeof value === 'string' ? value : '');
|
|
41
|
+
|
|
42
|
+
const orders: StepKind = {
|
|
43
|
+
title: 'Read orders',
|
|
44
|
+
icon: 'table',
|
|
45
|
+
ports: [OUT],
|
|
46
|
+
settings: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
required: ['state'],
|
|
49
|
+
properties: {
|
|
50
|
+
state: { type: 'string', title: 'State', enum: ['open', 'paid'], default: 'open' },
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
gives: expression.type('{ rows: list<{ customer: text, total: number }> }'),
|
|
54
|
+
line: (settings) => `The ${text(settings.state)} ones`,
|
|
55
|
+
run: () => ({
|
|
56
|
+
rows: [
|
|
57
|
+
{ customer: 'Elif', total: 120 },
|
|
58
|
+
{ customer: 'Tom', total: 80 },
|
|
59
|
+
],
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const discount: StepKind = {
|
|
64
|
+
title: 'Discount',
|
|
65
|
+
icon: 'star',
|
|
66
|
+
ports: [IN, OUT],
|
|
67
|
+
settings: {
|
|
68
|
+
type: 'object',
|
|
69
|
+
required: ['amount', 'percent'],
|
|
70
|
+
properties: {
|
|
71
|
+
amount: {
|
|
72
|
+
type: 'string',
|
|
73
|
+
title: 'Amount',
|
|
74
|
+
'x-editor': 'expression',
|
|
75
|
+
'x-editorProps': { expect: 'number' },
|
|
76
|
+
},
|
|
77
|
+
percent: { type: 'integer', title: 'Percent', minimum: 1, maximum: 90, default: 10 },
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
gives: expression.type('{ amount: number, saved: number }'),
|
|
81
|
+
line: (settings) => `${Number(settings.percent)}% off`,
|
|
82
|
+
run: (settings) => {
|
|
83
|
+
const amount = settings.amount as number;
|
|
84
|
+
const saved = (amount * (settings.percent as number)) / 100;
|
|
85
|
+
return { amount: amount - saved, saved };
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const message: StepKind = {
|
|
90
|
+
title: 'Write a message',
|
|
91
|
+
icon: 'send',
|
|
92
|
+
ports: [IN, OUT],
|
|
93
|
+
settings: {
|
|
94
|
+
type: 'object',
|
|
95
|
+
required: ['text'],
|
|
96
|
+
properties: {
|
|
97
|
+
text: {
|
|
98
|
+
type: 'string',
|
|
99
|
+
title: 'Text',
|
|
100
|
+
'x-editor': 'template',
|
|
101
|
+
'x-editorProps': { multiline: true, rows: 3 },
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
gives: expression.type('{ text: text }'),
|
|
106
|
+
line: (settings) => text(settings.text),
|
|
107
|
+
run: (settings) => ({ text: settings.text }),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/** The kinds of the designer, by the name a step gives in `kind`. A new kind is one more entry: the rest reads this. */
|
|
111
|
+
export const STEPS: Record<string, StepKind> = { orders, discount, message };
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## A kind of step, from scratch
|
|
115
|
+
|
|
116
|
+
A kind is one object that says everything about its steps. `Discount` takes an amount and gives a smaller one; write it in this order.
|
|
117
|
+
|
|
118
|
+
### 1. Its settings
|
|
119
|
+
|
|
120
|
+
The settings are a plain object, and its schema says what is in it. A value the user types is a type with its bounds and its default. A value that comes from the data is an expression (`x-editor: 'expression'`), and a text with data in it a template (`x-editor: 'template'`); `expect` is the type the value is wanted as.
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
settings: {
|
|
124
|
+
type: 'object',
|
|
125
|
+
required: ['amount', 'percent'],
|
|
126
|
+
properties: {
|
|
127
|
+
amount: {
|
|
128
|
+
type: 'string',
|
|
129
|
+
title: 'Amount',
|
|
130
|
+
'x-editor': 'expression',
|
|
131
|
+
'x-editorProps': { expect: 'number' },
|
|
132
|
+
},
|
|
133
|
+
percent: { type: 'integer', title: 'Percent', minimum: 1, maximum: 90, default: 10 },
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### 2. What it gives
|
|
139
|
+
|
|
140
|
+
The type of its result, as `expression.type` reads it. The steps after it read the result as `steps.discount1`; the type is what the editor offers and what the checks go by.
|
|
141
|
+
|
|
142
|
+
```tsx
|
|
143
|
+
gives: expression.type('{ amount: number, saved: number }'),
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### 3. Its ports
|
|
147
|
+
|
|
148
|
+
A way in and a way out, with one connection each. A kind that decides has two ways out with a `label`, such as Yes and No.
|
|
149
|
+
|
|
150
|
+
```tsx
|
|
151
|
+
const IN: DiagramPort = { id: 'in', side: 'left', direction: 'target', maxConnections: 1 };
|
|
152
|
+
const OUT: DiagramPort = { id: 'out', side: 'right', direction: 'source', maxConnections: 1 };
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### 4. Its work
|
|
156
|
+
|
|
157
|
+
`run` is given the settings worked out: the expression is the number it gave. What it returns is of the type in `gives`. In the page the work is made up, so a run shows how the data goes; a server does the real work of the same kind.
|
|
158
|
+
|
|
159
|
+
```tsx
|
|
160
|
+
run: (settings) => {
|
|
161
|
+
const amount = settings.amount as number;
|
|
162
|
+
const saved = (amount * (settings.percent as number)) / 100;
|
|
163
|
+
return { amount: amount - saved, saved };
|
|
164
|
+
},
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### 5. The kind
|
|
168
|
+
|
|
169
|
+
The whole kind, and its entry in `STEPS`. The toolbar, the card, the form, the names, the checks, the definition and the run all read `STEPS`, so a new kind needs nothing else.
|
|
170
|
+
|
|
171
|
+
```tsx
|
|
172
|
+
const discount: StepKind = {
|
|
173
|
+
title: 'Discount',
|
|
174
|
+
icon: 'star',
|
|
175
|
+
ports: [IN, OUT],
|
|
176
|
+
settings: {
|
|
177
|
+
type: 'object',
|
|
178
|
+
required: ['amount', 'percent'],
|
|
179
|
+
properties: {
|
|
180
|
+
amount: {
|
|
181
|
+
type: 'string',
|
|
182
|
+
title: 'Amount',
|
|
183
|
+
'x-editor': 'expression',
|
|
184
|
+
'x-editorProps': { expect: 'number' },
|
|
185
|
+
},
|
|
186
|
+
percent: { type: 'integer', title: 'Percent', minimum: 1, maximum: 90, default: 10 },
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
gives: expression.type('{ amount: number, saved: number }'),
|
|
190
|
+
line: (settings) => `${Number(settings.percent)}% off`,
|
|
191
|
+
run: (settings) => {
|
|
192
|
+
const amount = settings.amount as number;
|
|
193
|
+
const saved = (amount * (settings.percent as number)) / 100;
|
|
194
|
+
return { amount: amount - saved, saved };
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export const STEPS: Record<string, StepKind> = { orders, discount, message };
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Its card
|
|
202
|
+
|
|
203
|
+
`cardKind` makes the kind of the node: the header with the icon, the step’s name and the line under it, and a row for each port with a label. A node gives its kind’s name in `kind`, and keeps its settings with it.
|
|
204
|
+
|
|
205
|
+
```tsx
|
|
206
|
+
const kinds: Record<string, DiagramNodeKind<StepNode>> = {};
|
|
207
|
+
for (const [name, kind] of Object.entries(STEPS)) {
|
|
208
|
+
kinds[name] = cardKind<StepNode>({ ports: kind.ports, subtitle: (node) => kind.line(node.settings) });
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Its form
|
|
213
|
+
|
|
214
|
+
`schemaItems` makes the form from the schema, on the settings of the step themselves: what is typed shows on the card with `refresh`. The names go to the editors as a function, so a connection drawn or taken away changes what they offer. When focus leaves the form, what changed becomes one step of the diagram’s history, and Ctrl+Z puts it back.
|
|
215
|
+
|
|
216
|
+
```tsx
|
|
217
|
+
const formOf = (node: StepNode) => {
|
|
218
|
+
let form: FormLayout<Settings> | null = null;
|
|
219
|
+
let before = JSON.stringify(node.settings);
|
|
220
|
+
const write = (settings: string) => {
|
|
221
|
+
for (const name of Object.keys(node.settings)) delete node.settings[name];
|
|
222
|
+
Object.assign(node.settings, JSON.parse(settings));
|
|
223
|
+
before = settings;
|
|
224
|
+
form?.refresh();
|
|
225
|
+
};
|
|
226
|
+
const record = () => {
|
|
227
|
+
const was = before;
|
|
228
|
+
const now = JSON.stringify(node.settings);
|
|
229
|
+
if (now === was) return;
|
|
230
|
+
before = now;
|
|
231
|
+
diagram.record({ undo: () => write(was), redo: () => write(now), nodes: [node] });
|
|
232
|
+
};
|
|
233
|
+
// The names are a function: the editors follow the connections.
|
|
234
|
+
const names = () => {
|
|
235
|
+
void s.revision;
|
|
236
|
+
return namesOf(flow, node.id);
|
|
237
|
+
};
|
|
238
|
+
const editorProps = { expression: { schema: names }, template: { schema: names } };
|
|
239
|
+
return (
|
|
240
|
+
<FormLayout<Settings>
|
|
241
|
+
ref={(f: FormLayout<Settings>) => (form = f)}
|
|
242
|
+
label={node.text}
|
|
243
|
+
readOnly={() => s.running}
|
|
244
|
+
data={node.settings}
|
|
245
|
+
items={schemaItems(STEPS[node.kind].settings, { editorProps })}
|
|
246
|
+
onFieldChanged={() => diagram.refresh()}
|
|
247
|
+
onfocusout={record}
|
|
248
|
+
/>
|
|
249
|
+
);
|
|
250
|
+
};
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## The data of the other steps
|
|
254
|
+
|
|
255
|
+
A step reads only what has surely run before it. Here a step has one way in, so those are the steps on the way back; in a flow with branches, they are the steps every way to it passes, and in a loop there is also its item. The names and their types go to the editor, to the check and to the run alike.
|
|
256
|
+
|
|
257
|
+
```tsx
|
|
258
|
+
export function stepsBefore(flow: Flow, id: string): StepNode[] {
|
|
259
|
+
const found: StepNode[] = [];
|
|
260
|
+
for (let at = id; ; ) {
|
|
261
|
+
const way = flow.edges.find((edge) => edge.target === at);
|
|
262
|
+
const step = way && flow.nodes.find((node) => node.id === way.source);
|
|
263
|
+
if (!step || found.includes(step)) return found;
|
|
264
|
+
found.push(step);
|
|
265
|
+
at = step.id;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function namesOf(flow: Flow, id: string): expression.Schema {
|
|
270
|
+
const fields: Record<string, expression.Type> = {};
|
|
271
|
+
for (const step of stepsBefore(flow, id)) fields[step.id] = STEPS[step.kind].gives;
|
|
272
|
+
return { steps: { name: 'object', fields } };
|
|
273
|
+
}
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Its checks
|
|
277
|
+
|
|
278
|
+
Three checks say whether the flow runs: the connections (a step nothing leads to), the settings against their schema with `jsonSchema.validate`, and the expressions against the names with `expression.check`. Each problem names its step, so a click can show it. `expressionProblemText` gives a problem in the kit’s language.
|
|
279
|
+
|
|
280
|
+
```tsx
|
|
281
|
+
export function problemsOf(flow: Flow): { node: string; text: string }[] {
|
|
282
|
+
const found: { node: string; text: string }[] = [];
|
|
283
|
+
for (const node of flow.nodes) {
|
|
284
|
+
const kind = STEPS[node.kind];
|
|
285
|
+
const add = (text: string) => found.push({ node: node.id, text: `${node.text}: ${text}` });
|
|
286
|
+
const wantsIn = kind.ports.some((port) => port.direction === 'target');
|
|
287
|
+
if (wantsIn && !flow.edges.some((edge) => edge.target === node.id)) add('Nothing leads to it.');
|
|
288
|
+
for (const problem of jsonSchema.validate(kind.settings, node.settings).problems) add(problem.message);
|
|
289
|
+
const names = namesOf(flow, node.id);
|
|
290
|
+
for (const [name, field] of Object.entries(kind.settings.properties ?? {})) {
|
|
291
|
+
const read = readingOf(field, node.settings[name]);
|
|
292
|
+
if (!read) continue;
|
|
293
|
+
const { expect } = (field['x-editorProps'] ?? {}) as { expect?: string };
|
|
294
|
+
const { problems } = read.tree ? expression.check(read.tree, names, { expect }) : read;
|
|
295
|
+
for (const problem of problems) add(`${field.title}: ${expressionProblemText(problem)}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return found;
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## The definition
|
|
303
|
+
|
|
304
|
+
The definition is what the flow does and nothing of how it looks: no places, no statuses. It carries a `format` and a `version`, so whatever reads it knows what it is. An expression is kept as its tree, which a server works out without reading the text again; its `source` opens it in the editor.
|
|
305
|
+
|
|
306
|
+
```tsx
|
|
307
|
+
export function define(flow: Flow): FlowDefinition {
|
|
308
|
+
return {
|
|
309
|
+
format: 'my-flow',
|
|
310
|
+
version: 1,
|
|
311
|
+
steps: flow.nodes.map((node) => {
|
|
312
|
+
const fields = STEPS[node.kind].settings.properties ?? {};
|
|
313
|
+
const settings: Record<string, unknown> = {};
|
|
314
|
+
for (const [name, value] of Object.entries(node.settings)) {
|
|
315
|
+
settings[name] = readingOf(fields[name], value)?.tree ?? value;
|
|
316
|
+
}
|
|
317
|
+
return { id: node.id, kind: node.kind, name: node.text ?? '', settings };
|
|
318
|
+
}),
|
|
319
|
+
transitions: flow.edges.map((edge) => ({ from: edge.source, to: edge.target })),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
```tsx
|
|
325
|
+
{
|
|
326
|
+
"format": "my-flow",
|
|
327
|
+
"version": 1,
|
|
328
|
+
"steps": [
|
|
329
|
+
{ "id": "orders1", "kind": "orders", "name": "Read orders", "settings": { "state": "open" } },
|
|
330
|
+
{ "id": "discount1", "kind": "discount", "name": "Discount", "settings": {
|
|
331
|
+
"amount": { "version": 1, "source": "sum(steps.orders1.rows, r => r.total)", "root": { "kind": "call", "name": "sum", "args": [ … ] } },
|
|
332
|
+
"percent": 10
|
|
333
|
+
} }
|
|
334
|
+
],
|
|
335
|
+
"transitions": [{ "from": "orders1", "to": "discount1" }]
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
- A step’s `id` is the name the expressions read, so renaming a step changes its `name` and never its `id`.
|
|
340
|
+
|
|
341
|
+
- What is secret never enters the definition: a step names a connection, and the server knows its address and its password.
|
|
342
|
+
|
|
343
|
+
- Read again, the definition gives the same flow (`flowOf`), and its definition is the same; the layout places its steps.
|
|
344
|
+
|
|
345
|
+
## A run
|
|
346
|
+
|
|
347
|
+
The run works on the definition alone, as a server’s would. It goes from step to step along the transitions, works out the settings with what the steps before gave, and tells each step’s status, which the diagram shows. A step that fails keeps its reason in the status, and the run ends there.
|
|
348
|
+
|
|
349
|
+
```tsx
|
|
350
|
+
export async function runFlow(definition: FlowDefinition, options: RunOptions): Promise<Record<string, unknown>> {
|
|
351
|
+
const { show, pause = 600 } = options;
|
|
352
|
+
const given: Record<string, unknown> = {};
|
|
353
|
+
const reached = new Set(definition.transitions.map((way) => way.to));
|
|
354
|
+
let at = definition.steps.find((step) => !reached.has(step.id));
|
|
355
|
+
while (at) {
|
|
356
|
+
const step = at;
|
|
357
|
+
show(step.id, 'running');
|
|
358
|
+
await new Promise((done) => setTimeout(done, pause));
|
|
359
|
+
try {
|
|
360
|
+
const settings = turned(step.settings, (tree) => expression.evaluate(tree, { steps: given }));
|
|
361
|
+
given[step.id] = STEPS[step.kind].run(settings);
|
|
362
|
+
show(step.id, 'done');
|
|
363
|
+
} catch (error) {
|
|
364
|
+
show(step.id, 'error', error instanceof Error ? error.message : String(error));
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
const way = definition.transitions.find((one) => one.from === step.id);
|
|
368
|
+
at = definition.steps.find((one) => one.id === way?.to);
|
|
369
|
+
}
|
|
370
|
+
return given;
|
|
371
|
+
}
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
## From here
|
|
375
|
+
|
|
376
|
+
The automations of [FlowStudio](showcase-flowstudio.md) are the same pieces on a larger scale: decisions and loops, connections by name, the query of a table diagram as a step, rules with their codes, a definition that is exported, and a run with sample rows.
|
|
377
|
+
|
|
378
|
+
| Piece | Page | What it does here |
|
|
379
|
+
| --- | --- | --- |
|
|
380
|
+
| `cardKind, DiagramPort` | [Flow nodes](diagram-flow.md) | The card of a step, its ports, its status. |
|
|
381
|
+
| `schemaItems` | [FormLayout: From a schema](formlayout-schema.md) | The form of a step’s settings. |
|
|
382
|
+
| `jsonSchema` | [FormLayout: From a schema](formlayout-schema.md#checks) | The check of the settings, in the page and on a server. |
|
|
383
|
+
| `ExpressionEditor` | [ExpressionEditor](expressioneditor.md) | The editors of expressions and templates, with the names they read. |
|
|
384
|
+
| `expression` | [Expression language](expressions.md) | Reading, checking and working out expressions; the types of what steps give. |
|
|
385
|
+
| `record, refresh, setNodeStatus` | [API reference](diagram-api.md) | The history, drawing again, and the statuses of a run. |
|
|
386
|
+
|
|
387
|
+
## Feedback
|
|
388
|
+
|
|
389
|
+
Loading issues…
|
|
390
|
+
|
|
391
|
+
## Example files
|
|
392
|
+
|
|
393
|
+
`Designer.tsx`
|
|
394
|
+
|
|
395
|
+
```tsx
|
|
396
|
+
import { reactive, untracked } from '@motifx/core';
|
|
397
|
+
import { jsonSchema } from '@motifx-community/data';
|
|
398
|
+
import { Button, Diagram, FormLayout, Stack, cardKind, schemaItems, type DiagramNodeKind } from '@motifx-community/ui';
|
|
399
|
+
import { define, namesOf, problemsOf, runFlow, wayOf, type Flow } from './DesignerFlow';
|
|
400
|
+
import { STEPS, type StepNode } from './DesignerSteps';
|
|
401
|
+
|
|
402
|
+
type Settings = Record<string, unknown>;
|
|
403
|
+
|
|
404
|
+
/** A glyph of the kit, drawn by the design language in use. */
|
|
405
|
+
const glyph = (name: string) => <span class="mx-glyph" data-icon={name} aria-hidden="true" />;
|
|
406
|
+
|
|
407
|
+
/** The kinds of the diagram, one for each kind of step: a card with its ports, and what it is set to under its title. */
|
|
408
|
+
const kinds: Record<string, DiagramNodeKind<StepNode>> = {};
|
|
409
|
+
for (const [name, kind] of Object.entries(STEPS)) {
|
|
410
|
+
kinds[name] = cardKind<StepNode>({ ports: kind.ports, subtitle: (node) => kind.line(node.settings) });
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/** A step of a kind, with its icon. */
|
|
414
|
+
function stepOf(id: string, kind: string, settings: Settings): StepNode {
|
|
415
|
+
return { id, x: 0, y: 0, kind, text: STEPS[kind].title, icon: glyph(STEPS[kind].icon), settings };
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** The flow the designer starts with. */
|
|
419
|
+
const startFlow = (): Flow => ({
|
|
420
|
+
nodes: [
|
|
421
|
+
stepOf('orders1', 'orders', { state: 'open' }),
|
|
422
|
+
stepOf('discount1', 'discount', { amount: 'sum(steps.orders1.rows, r => r.total)', percent: 10 }),
|
|
423
|
+
stepOf('message1', 'message', {
|
|
424
|
+
text: '{{ count(steps.orders1.rows) }} orders, {{ steps.discount1.amount }} in all.',
|
|
425
|
+
}),
|
|
426
|
+
],
|
|
427
|
+
edges: [wayOf('orders1', 'discount1'), wayOf('discount1', 'message1')],
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
/** A designer of small flows: its steps are cards, their settings a form, and what it makes a definition that runs. */
|
|
431
|
+
export default function Designer() {
|
|
432
|
+
const s = reactive({
|
|
433
|
+
selected: null as string | null,
|
|
434
|
+
revision: 0,
|
|
435
|
+
running: false,
|
|
436
|
+
shown: 'Select a step, or run the flow.',
|
|
437
|
+
});
|
|
438
|
+
const flow = startFlow();
|
|
439
|
+
let diagram!: Diagram<StepNode>;
|
|
440
|
+
|
|
441
|
+
/** Adds a step after the one selected, as a step of the history. */
|
|
442
|
+
const add = (kind: string) => {
|
|
443
|
+
const after = flow.nodes.find((node) => node.id === s.selected);
|
|
444
|
+
const free = after && !flow.edges.some((edge) => edge.source === after.id);
|
|
445
|
+
const takes = STEPS[kind].ports.some((port) => port.id === 'in');
|
|
446
|
+
let n = 1;
|
|
447
|
+
while (flow.nodes.some((node) => node.id === `${kind}${n}`)) n++;
|
|
448
|
+
const node = stepOf(`${kind}${n}`, kind, jsonSchema.defaults(STEPS[kind].settings) as Settings);
|
|
449
|
+
node.x = after ? after.x + (diagram.nodeRect(after.id)?.width ?? 0) + 80 : 0;
|
|
450
|
+
node.y = after ? after.y : 120;
|
|
451
|
+
const way = free && takes ? wayOf(after.id, node.id) : null;
|
|
452
|
+
const redo = () => {
|
|
453
|
+
flow.nodes.push(node);
|
|
454
|
+
if (way) flow.edges.push(way);
|
|
455
|
+
};
|
|
456
|
+
const undo = () => {
|
|
457
|
+
flow.nodes.splice(flow.nodes.indexOf(node), 1);
|
|
458
|
+
if (way) flow.edges.splice(flow.edges.indexOf(way), 1);
|
|
459
|
+
};
|
|
460
|
+
redo();
|
|
461
|
+
diagram.refresh();
|
|
462
|
+
diagram.record({ undo, redo, nodes: [node] });
|
|
463
|
+
diagram.select({ nodes: [node.id] });
|
|
464
|
+
diagram.centerOn(node.id, { animate: true });
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
/** The form of a step: what is typed shows on its card at once, and the form left makes it one step of the history. */
|
|
468
|
+
const formOf = (node: StepNode) => {
|
|
469
|
+
let form: FormLayout<Settings> | null = null;
|
|
470
|
+
let before = JSON.stringify(node.settings);
|
|
471
|
+
const write = (settings: string) => {
|
|
472
|
+
for (const name of Object.keys(node.settings)) delete node.settings[name];
|
|
473
|
+
Object.assign(node.settings, JSON.parse(settings));
|
|
474
|
+
before = settings;
|
|
475
|
+
form?.refresh();
|
|
476
|
+
};
|
|
477
|
+
const record = () => {
|
|
478
|
+
const was = before;
|
|
479
|
+
const now = JSON.stringify(node.settings);
|
|
480
|
+
if (now === was) return;
|
|
481
|
+
before = now;
|
|
482
|
+
diagram.record({ undo: () => write(was), redo: () => write(now), nodes: [node] });
|
|
483
|
+
};
|
|
484
|
+
// The names are a function: the editors follow the connections.
|
|
485
|
+
const names = () => {
|
|
486
|
+
void s.revision;
|
|
487
|
+
return namesOf(flow, node.id);
|
|
488
|
+
};
|
|
489
|
+
const editorProps = { expression: { schema: names }, template: { schema: names } };
|
|
490
|
+
return (
|
|
491
|
+
<FormLayout<Settings>
|
|
492
|
+
ref={(f: FormLayout<Settings>) => (form = f)}
|
|
493
|
+
label={node.text}
|
|
494
|
+
readOnly={() => s.running}
|
|
495
|
+
data={node.settings}
|
|
496
|
+
items={schemaItems(STEPS[node.kind].settings, { editorProps })}
|
|
497
|
+
onFieldChanged={() => diagram.refresh()}
|
|
498
|
+
onfocusout={record}
|
|
499
|
+
/>
|
|
500
|
+
);
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
const run = async () => {
|
|
504
|
+
for (const node of flow.nodes) diagram.setNodeStatus(node.id, null);
|
|
505
|
+
const [problem] = problemsOf(flow);
|
|
506
|
+
if (problem) {
|
|
507
|
+
s.shown = problem.text;
|
|
508
|
+
diagram.select({ nodes: [problem.node] });
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
s.running = true;
|
|
512
|
+
const given = await runFlow(define(flow), {
|
|
513
|
+
show: (id, status, text) => diagram.setNodeStatus(id, status, text),
|
|
514
|
+
});
|
|
515
|
+
s.running = false;
|
|
516
|
+
s.shown = JSON.stringify(given, null, 2);
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
const showDefinition = () => {
|
|
520
|
+
s.shown = JSON.stringify(define(flow), null, 2);
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
return (
|
|
524
|
+
<Stack gap="md" class="docs-wide docs-designer">
|
|
525
|
+
<Stack orientation="horizontal" gap="sm" wrap align="center">
|
|
526
|
+
{Object.entries(STEPS).map(([name, kind]) => (
|
|
527
|
+
<Button
|
|
528
|
+
key={name}
|
|
529
|
+
text={kind.title}
|
|
530
|
+
icon={() => glyph('plus')}
|
|
531
|
+
variant="outline"
|
|
532
|
+
disabled={() => s.running}
|
|
533
|
+
onclick={() => add(name)}
|
|
534
|
+
/>
|
|
535
|
+
))}
|
|
536
|
+
<Button text="Run" tone="accent" disabled={() => s.running} onclick={() => void run()} />
|
|
537
|
+
<Button text="Show the definition" variant="outline" onclick={showDefinition} />
|
|
538
|
+
</Stack>
|
|
539
|
+
<Diagram
|
|
540
|
+
ref={(d: Diagram<StepNode>) => (diagram = d)}
|
|
541
|
+
label="Flow"
|
|
542
|
+
nodes={flow.nodes}
|
|
543
|
+
edges={flow.edges}
|
|
544
|
+
kinds={kinds}
|
|
545
|
+
edgePath="curve"
|
|
546
|
+
autoLayout
|
|
547
|
+
layoutOptions={{ direction: 'right' }}
|
|
548
|
+
autoFit
|
|
549
|
+
readOnly={() => s.running}
|
|
550
|
+
onSelectionChanged={(e) => (s.selected = e.nodes.length === 1 ? e.nodes[0].id : null)}
|
|
551
|
+
onHistoryChanged={() => s.revision++}
|
|
552
|
+
/>
|
|
553
|
+
{() => {
|
|
554
|
+
const node = flow.nodes.find((one) => one.id === s.selected);
|
|
555
|
+
return node ? untracked(() => formOf(node)) : <p>Select a step to set it up.</p>;
|
|
556
|
+
}}
|
|
557
|
+
<pre class="docs-designer-shown" tabindex="0" aria-label="What the flow gives">
|
|
558
|
+
{() => s.shown}
|
|
559
|
+
</pre>
|
|
560
|
+
</Stack>
|
|
561
|
+
);
|
|
562
|
+
}
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
`DesignerFlow.ts`
|
|
566
|
+
|
|
567
|
+
```tsx
|
|
568
|
+
import { expression, jsonSchema } from '@motifx-community/data';
|
|
569
|
+
import { expressionProblemText, type DiagramEdge, type DiagramNodeStatus } from '@motifx-community/ui';
|
|
570
|
+
import { STEPS, type StepNode } from './DesignerSteps';
|
|
571
|
+
|
|
572
|
+
export interface Flow {
|
|
573
|
+
nodes: StepNode[];
|
|
574
|
+
edges: DiagramEdge[];
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/** What the flow does, without how it looks: what a server reads. */
|
|
578
|
+
export interface FlowDefinition {
|
|
579
|
+
format: 'my-flow';
|
|
580
|
+
version: 1;
|
|
581
|
+
steps: { id: string; kind: string; name: string; settings: Record<string, unknown> }[];
|
|
582
|
+
transitions: { from: string; to: string }[];
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/** The way from a step to the one after it. */
|
|
586
|
+
export const wayOf = (from: string, to: string): DiagramEdge => ({
|
|
587
|
+
id: `${from}-${to}`,
|
|
588
|
+
source: from,
|
|
589
|
+
sourcePort: 'out',
|
|
590
|
+
target: to,
|
|
591
|
+
targetPort: 'in',
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
/** The steps that surely ran before a step, the nearest first. Each step has one way in, so it is the way back. */
|
|
595
|
+
export function stepsBefore(flow: Flow, id: string): StepNode[] {
|
|
596
|
+
const found: StepNode[] = [];
|
|
597
|
+
for (let at = id; ; ) {
|
|
598
|
+
const way = flow.edges.find((edge) => edge.target === at);
|
|
599
|
+
const step = way && flow.nodes.find((node) => node.id === way.source);
|
|
600
|
+
if (!step || found.includes(step)) return found;
|
|
601
|
+
found.push(step);
|
|
602
|
+
at = step.id;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/** The names a step's expressions read, and their types. */
|
|
607
|
+
export function namesOf(flow: Flow, id: string): expression.Schema {
|
|
608
|
+
const fields: Record<string, expression.Type> = {};
|
|
609
|
+
for (const step of stepsBefore(flow, id)) fields[step.id] = STEPS[step.kind].gives;
|
|
610
|
+
return { steps: { name: 'object', fields } };
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** The reading of a setting written as an expression or a template; `null` for a setting that is a plain value. */
|
|
614
|
+
function readingOf(field: jsonSchema.JsonSchema | undefined, value: unknown): expression.ParseResult | null {
|
|
615
|
+
const editor = field?.['x-editor'];
|
|
616
|
+
if (typeof value !== 'string' || (editor !== 'expression' && editor !== 'template')) return null;
|
|
617
|
+
return editor === 'template' ? expression.parseTemplate(value) : expression.parse(value);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
/** What keeps the flow from running, and at which step. */
|
|
621
|
+
export function problemsOf(flow: Flow): { node: string; text: string }[] {
|
|
622
|
+
const found: { node: string; text: string }[] = [];
|
|
623
|
+
for (const node of flow.nodes) {
|
|
624
|
+
const kind = STEPS[node.kind];
|
|
625
|
+
const add = (text: string) => found.push({ node: node.id, text: `${node.text}: ${text}` });
|
|
626
|
+
const wantsIn = kind.ports.some((port) => port.direction === 'target');
|
|
627
|
+
if (wantsIn && !flow.edges.some((edge) => edge.target === node.id)) add('Nothing leads to it.');
|
|
628
|
+
for (const problem of jsonSchema.validate(kind.settings, node.settings).problems) add(problem.message);
|
|
629
|
+
const names = namesOf(flow, node.id);
|
|
630
|
+
for (const [name, field] of Object.entries(kind.settings.properties ?? {})) {
|
|
631
|
+
const read = readingOf(field, node.settings[name]);
|
|
632
|
+
if (!read) continue;
|
|
633
|
+
const { expect } = (field['x-editorProps'] ?? {}) as { expect?: string };
|
|
634
|
+
const { problems } = read.tree ? expression.check(read.tree, names, { expect }) : read;
|
|
635
|
+
for (const problem of problems) add(`${field.title}: ${expressionProblemText(problem)}`);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
return found;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/** The definition of a flow with no problem: an expression is kept as its tree, which a server works out. */
|
|
642
|
+
export function define(flow: Flow): FlowDefinition {
|
|
643
|
+
return {
|
|
644
|
+
format: 'my-flow',
|
|
645
|
+
version: 1,
|
|
646
|
+
steps: flow.nodes.map((node) => {
|
|
647
|
+
const fields = STEPS[node.kind].settings.properties ?? {};
|
|
648
|
+
const settings: Record<string, unknown> = {};
|
|
649
|
+
for (const [name, value] of Object.entries(node.settings)) {
|
|
650
|
+
settings[name] = readingOf(fields[name], value)?.tree ?? value;
|
|
651
|
+
}
|
|
652
|
+
return { id: node.id, kind: node.kind, name: node.text ?? '', settings };
|
|
653
|
+
}),
|
|
654
|
+
transitions: flow.edges.map((edge) => ({ from: edge.source, to: edge.target })),
|
|
655
|
+
};
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const isTree = (value: unknown): value is expression.Tree =>
|
|
659
|
+
typeof value === 'object' && value !== null && 'root' in value && 'source' in value;
|
|
660
|
+
|
|
661
|
+
/** Settings with each expression turned into something else: its text, or its value. */
|
|
662
|
+
function turned(settings: Record<string, unknown>, into: (tree: expression.Tree) => unknown): Record<string, unknown> {
|
|
663
|
+
return Object.fromEntries(Object.entries(settings).map(([name, value]) => [name, isTree(value) ? into(value) : value]));
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/** The flow of a definition, to open it again: an expression as its text. Its steps have no place yet, so lay them out. */
|
|
667
|
+
export function flowOf(definition: FlowDefinition): Flow {
|
|
668
|
+
return {
|
|
669
|
+
nodes: definition.steps.map((step) => ({
|
|
670
|
+
id: step.id,
|
|
671
|
+
x: 0,
|
|
672
|
+
y: 0,
|
|
673
|
+
kind: step.kind,
|
|
674
|
+
text: step.name,
|
|
675
|
+
settings: turned(step.settings, (tree) => tree.source),
|
|
676
|
+
})),
|
|
677
|
+
edges: definition.transitions.map((way) => wayOf(way.from, way.to)),
|
|
678
|
+
};
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/** What a run tells as it goes: each step's status, and what it gave. */
|
|
682
|
+
export interface RunOptions {
|
|
683
|
+
show: (id: string, status: DiagramNodeStatus, text?: string) => void;
|
|
684
|
+
/** How long a step shows before it does its work, in milliseconds. */
|
|
685
|
+
pause?: number;
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** Runs a definition from the step nothing leads to, and gives what each step gave. A step that fails ends it. */
|
|
689
|
+
export async function runFlow(definition: FlowDefinition, options: RunOptions): Promise<Record<string, unknown>> {
|
|
690
|
+
const { show, pause = 600 } = options;
|
|
691
|
+
const given: Record<string, unknown> = {};
|
|
692
|
+
const reached = new Set(definition.transitions.map((way) => way.to));
|
|
693
|
+
let at = definition.steps.find((step) => !reached.has(step.id));
|
|
694
|
+
while (at) {
|
|
695
|
+
const step = at;
|
|
696
|
+
show(step.id, 'running');
|
|
697
|
+
await new Promise((done) => setTimeout(done, pause));
|
|
698
|
+
try {
|
|
699
|
+
const settings = turned(step.settings, (tree) => expression.evaluate(tree, { steps: given }));
|
|
700
|
+
given[step.id] = STEPS[step.kind].run(settings);
|
|
701
|
+
show(step.id, 'done');
|
|
702
|
+
} catch (error) {
|
|
703
|
+
show(step.id, 'error', error instanceof Error ? error.message : String(error));
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
const way = definition.transitions.find((one) => one.from === step.id);
|
|
707
|
+
at = definition.steps.find((one) => one.id === way?.to);
|
|
708
|
+
}
|
|
709
|
+
return given;
|
|
710
|
+
}
|
|
711
|
+
```
|
|
712
|
+
|
|
713
|
+
`DesignerSteps.ts`
|
|
714
|
+
|
|
715
|
+
```tsx
|
|
716
|
+
import { expression, type jsonSchema } from '@motifx-community/data';
|
|
717
|
+
import type { DiagramCardNode, DiagramPort } from '@motifx-community/ui';
|
|
718
|
+
|
|
719
|
+
/** A step of the flow: a card, with the settings of its kind. It is plain data, but for the icon. */
|
|
720
|
+
export interface StepNode extends DiagramCardNode {
|
|
721
|
+
kind: string;
|
|
722
|
+
settings: Record<string, unknown>;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/** A kind of step: everything the designer knows about it. */
|
|
726
|
+
export interface StepKind {
|
|
727
|
+
title: string;
|
|
728
|
+
/** The name of its icon, one of the kit's. */
|
|
729
|
+
icon: string;
|
|
730
|
+
ports: DiagramPort[];
|
|
731
|
+
/** The schema of its settings: the form is made from it, and the settings are checked against it. */
|
|
732
|
+
settings: jsonSchema.JsonSchema;
|
|
733
|
+
/** The type of what it gives, which the steps after it read as `steps.<id>`. */
|
|
734
|
+
gives: expression.Type;
|
|
735
|
+
/** What it is set to, in the line under its title. */
|
|
736
|
+
line: (settings: Record<string, unknown>) => string;
|
|
737
|
+
/** Its work, with its settings worked out. Here it is made up; on a server it reads, calls and sends. */
|
|
738
|
+
run: (settings: Record<string, unknown>) => unknown;
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
const IN: DiagramPort = { id: 'in', side: 'left', direction: 'target', maxConnections: 1 };
|
|
742
|
+
const OUT: DiagramPort = { id: 'out', side: 'right', direction: 'source', maxConnections: 1 };
|
|
743
|
+
|
|
744
|
+
const text = (value: unknown) => (typeof value === 'string' ? value : '');
|
|
745
|
+
|
|
746
|
+
const orders: StepKind = {
|
|
747
|
+
title: 'Read orders',
|
|
748
|
+
icon: 'table',
|
|
749
|
+
ports: [OUT],
|
|
750
|
+
settings: {
|
|
751
|
+
type: 'object',
|
|
752
|
+
required: ['state'],
|
|
753
|
+
properties: {
|
|
754
|
+
state: { type: 'string', title: 'State', enum: ['open', 'paid'], default: 'open' },
|
|
755
|
+
},
|
|
756
|
+
},
|
|
757
|
+
gives: expression.type('{ rows: list<{ customer: text, total: number }> }'),
|
|
758
|
+
line: (settings) => `The ${text(settings.state)} ones`,
|
|
759
|
+
run: () => ({
|
|
760
|
+
rows: [
|
|
761
|
+
{ customer: 'Elif', total: 120 },
|
|
762
|
+
{ customer: 'Tom', total: 80 },
|
|
763
|
+
],
|
|
764
|
+
}),
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
const discount: StepKind = {
|
|
768
|
+
title: 'Discount',
|
|
769
|
+
icon: 'star',
|
|
770
|
+
ports: [IN, OUT],
|
|
771
|
+
settings: {
|
|
772
|
+
type: 'object',
|
|
773
|
+
required: ['amount', 'percent'],
|
|
774
|
+
properties: {
|
|
775
|
+
amount: {
|
|
776
|
+
type: 'string',
|
|
777
|
+
title: 'Amount',
|
|
778
|
+
'x-editor': 'expression',
|
|
779
|
+
'x-editorProps': { expect: 'number' },
|
|
780
|
+
},
|
|
781
|
+
percent: { type: 'integer', title: 'Percent', minimum: 1, maximum: 90, default: 10 },
|
|
782
|
+
},
|
|
783
|
+
},
|
|
784
|
+
gives: expression.type('{ amount: number, saved: number }'),
|
|
785
|
+
line: (settings) => `${Number(settings.percent)}% off`,
|
|
786
|
+
run: (settings) => {
|
|
787
|
+
const amount = settings.amount as number;
|
|
788
|
+
const saved = (amount * (settings.percent as number)) / 100;
|
|
789
|
+
return { amount: amount - saved, saved };
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const message: StepKind = {
|
|
794
|
+
title: 'Write a message',
|
|
795
|
+
icon: 'send',
|
|
796
|
+
ports: [IN, OUT],
|
|
797
|
+
settings: {
|
|
798
|
+
type: 'object',
|
|
799
|
+
required: ['text'],
|
|
800
|
+
properties: {
|
|
801
|
+
text: {
|
|
802
|
+
type: 'string',
|
|
803
|
+
title: 'Text',
|
|
804
|
+
'x-editor': 'template',
|
|
805
|
+
'x-editorProps': { multiline: true, rows: 3 },
|
|
806
|
+
},
|
|
807
|
+
},
|
|
808
|
+
},
|
|
809
|
+
gives: expression.type('{ text: text }'),
|
|
810
|
+
line: (settings) => text(settings.text),
|
|
811
|
+
run: (settings) => ({ text: settings.text }),
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
/** The kinds of the designer, by the name a step gives in `kind`. A new kind is one more entry: the rest reads this. */
|
|
815
|
+
export const STEPS: Record<string, StepKind> = { orders, discount, message };
|
|
816
|
+
```
|