Skip to content

Commit 11fe194

Browse files
committed
new node-type for numeric values: number-value .. value and number-value also trigger the node instead of the whole flow when changed
1 parent 6e6cbd4 commit 11fe194

File tree

4 files changed

+273
-61
lines changed

4 files changed

+273
-61
lines changed

examples-test-flows/test-flow-with-draggable-values.json

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"id": "2c2ab16e-ddc9-46ee-be49-5b510c91e354",
4747
"x": 10590.913232625046,
4848
"y": 2075.6191535879448,
49-
"width": 120.0002010973282,
50-
"height": 55.999878211635284,
49+
"width": 120.00021288405681,
50+
"height": 55.99988371208746,
5151
"nodeType": "Shape",
5252
"nodeInfo": {
5353
"type": "show-input",
@@ -100,8 +100,8 @@
100100
"id": "b0174761-22cc-4db6-8e83-27a09ad462c5",
101101
"x": 10183.360322930836,
102102
"y": 2388.8825245919566,
103-
"width": 240.00037802829422,
104-
"height": 55.99995455356423,
103+
"width": 240.00042576811362,
104+
"height": 56.000099345893176,
105105
"nodeType": "Shape",
106106
"nodeInfo": {
107107
"type": "sum",
@@ -131,8 +131,8 @@
131131
"id": "61581958-5591-4b51-984d-cd0f2ab47372",
132132
"x": 10151.010505581524,
133133
"y": 1792.4660031566073,
134-
"width": 174.42185563990446,
135-
"height": 83.9999251343452,
134+
"width": 174.42187277206804,
135+
"height": 83.99993338503404,
136136
"nodeType": "Shape",
137137
"nodeInfo": {
138138
"type": "scope-variable",
@@ -252,8 +252,8 @@
252252
"id": "b263083e-a153-4208-bb68-116e00460c23",
253253
"x": 10197.643266767667,
254254
"y": 2727.1226810117664,
255-
"width": 120.0002010973282,
256-
"height": 55.999878211635284,
255+
"width": 120.00076206685672,
256+
"height": 55.99986461364577,
257257
"nodeType": "Shape",
258258
"nodeInfo": {
259259
"type": "show-input",
@@ -266,50 +266,50 @@
266266
}
267267
},
268268
{
269-
"id": "d599c236-1308-4b6c-b1b5-fa640282be69",
270-
"x": 9028.04658563688,
271-
"y": 2642.216719475015,
272-
"width": 200,
273-
"height": 112,
269+
"id": "84047b6f-7f0b-44c9-be01-3a7de97c6781",
270+
"x": 9458.310091566247,
271+
"y": 2479.0801354514206,
272+
"width": 131.5148659851293,
273+
"height": 103.6776176126441,
274274
"nodeType": "Shape",
275275
"nodeInfo": {
276-
"type": "value",
276+
"type": "annotation",
277277
"formValues": {
278-
"value": "2"
279-
}
278+
"annotation": "{\n \"a\" : 10,\n \"b\" : 20\n}",
279+
"fontSize": "16",
280+
"fontWeight": "false"
281+
},
282+
"isSettingsPopup": true,
283+
"nodeCannotBeReplaced": true,
284+
"isAnnotation": true
280285
}
281286
},
282287
{
283-
"id": "61f144bd-4e2c-4880-b233-687954b3bbb6",
284-
"x": 9028.04658563688,
285-
"y": 2774.216712362698,
288+
"id": "db04d194-95cf-41ef-ba84-5b542284e062",
289+
"x": 9028.048025703505,
290+
"y": 2640.635179947197,
286291
"width": 200,
287292
"height": 112,
288293
"nodeType": "Shape",
289294
"nodeInfo": {
290-
"type": "value",
295+
"type": "number-value",
291296
"formValues": {
292-
"value": "3"
297+
"value": "2"
293298
}
294299
}
295300
},
296301
{
297-
"id": "84047b6f-7f0b-44c9-be01-3a7de97c6781",
298-
"x": 9458.310091566247,
299-
"y": 2479.0801354514206,
300-
"width": 131.5148659851293,
301-
"height": 103.6776176126441,
302+
"id": "fc0f881f-80f7-46e9-b09f-739b77a79e97",
303+
"x": 9028.787346085319,
304+
"y": 2774.216819793708,
305+
"width": 200,
306+
"height": 112,
302307
"nodeType": "Shape",
303308
"nodeInfo": {
304-
"type": "annotation",
309+
"type": "number-value",
305310
"formValues": {
306-
"annotation": "{\n \"a\" : 10,\n \"b\" : 20\n}",
307-
"fontSize": "16",
308-
"fontWeight": "false"
309-
},
310-
"isSettingsPopup": true,
311-
"nodeCannotBeReplaced": true,
312-
"isAnnotation": true
311+
"value": "3"
312+
}
313313
}
314314
},
315315
{
@@ -347,7 +347,7 @@
347347
"x": 9977.394152186711,
348348
"y": 2416.7596505691417,
349349
"endX": 10183.360322930836,
350-
"endY": 2416.8825018687385,
350+
"endY": 2416.8825742649033,
351351
"startNodeId": "5c55d11b-3983-4b13-ac70-166d1899c5cd",
352352
"endNodeId": "b0174761-22cc-4db6-8e83-27a09ad462c5",
353353
"startThumbName": "output",
@@ -389,11 +389,11 @@
389389
},
390390
{
391391
"id": "d017df0a-d2d8-43c2-a462-cab377621050",
392-
"x": 9228.04658563688,
393-
"y": 2698.216719475015,
392+
"x": 9228.048025703505,
393+
"y": 2696.635179947197,
394394
"endX": 9322.372983471387,
395395
"endY": 2709.6586107991366,
396-
"startNodeId": "d599c236-1308-4b6c-b1b5-fa640282be69",
396+
"startNodeId": "db04d194-95cf-41ef-ba84-5b542284e062",
397397
"endNodeId": "6c2d5383-788b-43ae-8793-970a43c7975e",
398398
"startThumbName": "output",
399399
"endThumbName": "input1",
@@ -404,11 +404,11 @@
404404
},
405405
{
406406
"id": "043e13a0-9bc6-4dba-be45-c7a490d1c208",
407-
"x": 9228.04658563688,
408-
"y": 2830.216712362698,
407+
"x": 9228.787346085319,
408+
"y": 2830.216819793708,
409409
"endX": 9322.372983471387,
410410
"endY": 2759.6586107991366,
411-
"startNodeId": "61f144bd-4e2c-4880-b233-687954b3bbb6",
411+
"startNodeId": "fc0f881f-80f7-46e9-b09f-739b77a79e97",
412412
"endNodeId": "6c2d5383-788b-43ae-8793-970a43c7975e",
413413
"startThumbName": "output",
414414
"endThumbName": "input2",

libs/web-flow-executor/src/node-task-registry/canvas-node-task-registry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ import {
222222
vectorDistanceNodeName,
223223
} from '../nodes/vector-distance';
224224
import { createGuid, createGuidNodeName } from '../nodes/create-guid';
225+
import { getNumberValue } from '../nodes/value-number';
225226

226227
export const canvasNodeTaskRegistry: NodeTypeRegistry<NodeInfo> = {};
227228
export const canvasNodeTaskRegistryLabels: Record<string, string> = {};
@@ -271,6 +272,7 @@ export const setupCanvasNodeTaskRegistry = (
271272
// registerNodeFactory('expression-part', getExpressionPart);
272273
// registerNodeFactory('expression-execute', getExpressionExecute);
273274
registerNodeFactory('value', getValue);
275+
registerNodeFactory('number-value', getNumberValue);
274276

275277
registerNodeFactory('send-command', getSendCommand);
276278

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import {
2+
IFlowCanvasBase,
3+
createElement,
4+
FormComponent,
5+
FormFieldType,
6+
InitialValues,
7+
INodeComponent,
8+
IRectNodeComponent,
9+
NodeTask,
10+
NodeTaskFactory,
11+
replaceValues,
12+
ThumbConnectionType,
13+
ThumbType,
14+
FlowChangeType,
15+
} from '@devhelpr/visual-programming-system';
16+
import { NodeInfo } from '../types/node-info';
17+
const thumbs = [
18+
{
19+
thumbType: ThumbType.StartConnectorCenter,
20+
thumbIndex: 0,
21+
connectionType: ThumbConnectionType.start,
22+
color: 'white',
23+
label: ' ',
24+
//thumbConstraint: 'value',
25+
},
26+
{
27+
thumbType: ThumbType.EndConnectorCenter,
28+
thumbIndex: 0,
29+
connectionType: ThumbConnectionType.end,
30+
color: 'white',
31+
label: ' ',
32+
//thumbConstraint: 'value',
33+
},
34+
];
35+
export const getNumberValue: NodeTaskFactory<NodeInfo> = (
36+
updated: (
37+
shouldClearExecutionHistory?: boolean,
38+
isStoreOnly?: boolean,
39+
flowChangeType?: FlowChangeType,
40+
node?: INodeComponent<NodeInfo> | undefined
41+
) => void
42+
): NodeTask<NodeInfo> => {
43+
let node: IRectNodeComponent<NodeInfo>;
44+
45+
const initializeCompute = () => {
46+
return;
47+
};
48+
const compute = (input: string, loopIndex?: number, payload?: any) => {
49+
const result = parseFloat(
50+
replaceValues(node?.nodeInfo?.formValues?.['value'] ?? '', {
51+
value: input,
52+
currentValue: input,
53+
index: loopIndex ?? 0,
54+
payload: payload,
55+
})
56+
);
57+
if (isFinite(result)) {
58+
return {
59+
result,
60+
followPath: undefined,
61+
};
62+
}
63+
return {
64+
result: undefined,
65+
output: undefined,
66+
stop: true,
67+
followPath: undefined,
68+
};
69+
};
70+
71+
return {
72+
name: 'number-value',
73+
family: 'flow-canvas',
74+
isContainer: false,
75+
thumbs,
76+
createVisualNode: (
77+
canvasApp: IFlowCanvasBase<NodeInfo>,
78+
x: number,
79+
y: number,
80+
id?: string,
81+
initalValues?: InitialValues,
82+
containerNode?: IRectNodeComponent<NodeInfo>
83+
) => {
84+
const initialValue = initalValues?.['value'] ?? '';
85+
86+
const formElements = [
87+
{
88+
fieldType: FormFieldType.Text,
89+
fieldName: 'value',
90+
label: 'Value (number)',
91+
value: initialValue ?? '',
92+
onChange: (value: string) => {
93+
if (!node.nodeInfo) {
94+
return;
95+
}
96+
node.nodeInfo.formValues = {
97+
...node.nodeInfo.formValues,
98+
value: value,
99+
};
100+
console.log('onChange', node.nodeInfo);
101+
if (updated) {
102+
updated(undefined, undefined, FlowChangeType.TriggerNode, node);
103+
}
104+
},
105+
},
106+
];
107+
108+
const componentWrapper = createElement(
109+
'div',
110+
{
111+
class: `inner-node bg-slate-500 p-4 rounded`,
112+
},
113+
undefined
114+
) as unknown as INodeComponent<NodeInfo>;
115+
if (componentWrapper?.domElement) {
116+
FormComponent({
117+
rootElement: componentWrapper?.domElement as HTMLElement,
118+
id: id ?? '',
119+
formElements,
120+
hasSubmitButton: false,
121+
onSave: (formValues) => {
122+
console.log('onSave', formValues);
123+
},
124+
}) as unknown as HTMLElement;
125+
}
126+
127+
const rect = canvasApp.createRect(
128+
x,
129+
y,
130+
200,
131+
100,
132+
undefined,
133+
thumbs,
134+
componentWrapper,
135+
{
136+
classNames: `bg-slate-500 p-4 rounded`,
137+
},
138+
undefined,
139+
undefined,
140+
undefined,
141+
id,
142+
{
143+
type: 'number-value',
144+
formValues: {
145+
value: initialValue ?? '',
146+
},
147+
},
148+
containerNode
149+
);
150+
if (!rect.nodeComponent) {
151+
throw new Error('rect.nodeComponent is undefined');
152+
}
153+
154+
node = rect.nodeComponent;
155+
156+
if (node.nodeInfo) {
157+
node.nodeInfo.formElements = formElements;
158+
node.nodeInfo.compute = compute;
159+
node.nodeInfo.initializeCompute = initializeCompute;
160+
node.nodeInfo.compileInfo = {
161+
getCode: (input: any) => {
162+
return `\
163+
getValue(${input ? input : '""'},undefined,undefined,"${
164+
node?.nodeInfo?.formValues?.['value'] ?? ''
165+
}");
166+
`;
167+
},
168+
getGlobalCode: () => {
169+
return `\
170+
const replaceValuesGetValue = (
171+
content,
172+
payload,
173+
keepUnknownFields = false
174+
) => {
175+
let resultContent = content;
176+
const matches = resultContent.match(/{.+?}/g);
177+
if (matches) {
178+
matches.map((match) => {
179+
const variableName = match.slice(1, -1);
180+
let value = payload[variableName];
181+
if (!!keepUnknownFields && value === undefined) {
182+
value = match;
183+
}
184+
const allOccurancesOfMatchRegex = new RegExp(match, 'g');
185+
resultContent = resultContent.replace(allOccurancesOfMatchRegex, value);
186+
});
187+
}
188+
return resultContent;
189+
};
190+
191+
function getValue(input, loopIndex, payload, nodeValue) {
192+
return replaceValuesGetValue(nodeValue ?? "", {
193+
value: input,
194+
currentValue: input,
195+
index: loopIndex ?? 0,
196+
payload: payload,
197+
});
198+
};
199+
`;
200+
},
201+
};
202+
}
203+
return node;
204+
},
205+
};
206+
};

0 commit comments

Comments
 (0)