@digipair/skill-web-editor 0.90.0 → 0.91.0-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.
- package/.swcrc +28 -0
- package/README.md +7 -0
- package/eslint.config.mjs +22 -0
- package/package.json +21 -5
- package/rollup.config.cjs +28 -0
- package/src/lib/blocks/json.ts +145 -0
- package/src/lib/editor.element.ts +279 -0
- package/src/lib/generator/blockly-to-json.ts +360 -0
- package/src/lib/generator/json-to-blockly.ts +757 -0
- package/src/lib/generator/pins-to-blockly.ts +927 -0
- package/src/lib/schemas/web.fr.schema.ts +34473 -0
- package/src/lib/schemas/web.schema.ts +34473 -0
- package/src/lib/skill-web-editor.spec.ts +7 -0
- package/src/lib/skill-web-editor.ts +1 -0
- package/tsconfig.json +13 -0
- package/tsconfig.lib.json +19 -0
- package/blockly-to-json.cjs.js +0 -338
- package/blockly-to-json.esm.js +0 -336
- package/index.cjs.d.ts +0 -1
- package/index.cjs.js +0 -12
- package/index.cjs2.js +0 -72611
- package/index.esm.js +0 -1
- package/index.esm2.js +0 -72608
- package/libs/skill-web-editor/src/lib/blocks/json.d.ts +0 -145
- package/libs/skill-web-editor/src/lib/editor.element.d.ts +0 -24
- package/libs/skill-web-editor/src/lib/generator/blockly-to-json.d.ts +0 -1
- package/libs/skill-web-editor/src/lib/generator/json-to-blockly.d.ts +0 -1
- package/libs/skill-web-editor/src/lib/generator/pins-to-blockly.d.ts +0 -139
- package/libs/skill-web-editor/src/lib/schemas/web.fr.schema.d.ts +0 -4028
- package/libs/skill-web-editor/src/lib/schemas/web.schema.d.ts +0 -4028
- package/libs/skill-web-editor/src/lib/skill-web-editor.d.ts +0 -1
- package/pins-to-blockly.cjs.js +0 -628
- package/pins-to-blockly.esm.js +0 -624
- /package/{index.d.ts → src/index.d.ts} +0 -0
- /package/{libs/skill-web-editor/src/index.d.ts → src/index.ts} +0 -0
- /package/{schema.fr.json → src/schema.fr.json} +0 -0
- /package/{schema.json → src/schema.json} +0 -0
@@ -0,0 +1,757 @@
|
|
1
|
+
import { PinsSettings } from '@digipair/engine';
|
2
|
+
|
3
|
+
let libraries: any[];
|
4
|
+
|
5
|
+
export function initializeWorkspaceFromPinsAndLibraries(
|
6
|
+
pinsSettings: any,
|
7
|
+
workspace: any,
|
8
|
+
librariesToLoad: any,
|
9
|
+
) {
|
10
|
+
workspace.clear();
|
11
|
+
libraries = librariesToLoad;
|
12
|
+
generateSceneBlock(pinsSettings, workspace);
|
13
|
+
}
|
14
|
+
|
15
|
+
function generateSceneBlock(pinsSettings: any, workspace: any) {
|
16
|
+
let sceneBlockName = '';
|
17
|
+
let sceneBlockLibrary = '';
|
18
|
+
let hasSceneBlock = false;
|
19
|
+
|
20
|
+
sceneBlockName = '/' + pinsSettings.element;
|
21
|
+
sceneBlockLibrary = pinsSettings.library;
|
22
|
+
|
23
|
+
const foundLibrary = libraries.find(
|
24
|
+
(library: { info: { title: string } }) => library.info.title === sceneBlockLibrary,
|
25
|
+
);
|
26
|
+
hasSceneBlock =
|
27
|
+
foundLibrary &&
|
28
|
+
foundLibrary['x-scene-blocks'] &&
|
29
|
+
Object.hasOwnProperty.call(foundLibrary['x-scene-blocks'], sceneBlockName);
|
30
|
+
|
31
|
+
let sceneBlock;
|
32
|
+
if (hasSceneBlock) {
|
33
|
+
//generating scene block
|
34
|
+
const sceneBlockId = sceneBlockLibrary + '/__SCENEBLOCK__' + sceneBlockName;
|
35
|
+
sceneBlock = workspace.newBlock(sceneBlockId);
|
36
|
+
|
37
|
+
if (sceneBlock.mutator) {
|
38
|
+
sceneBlock.itemList_ = itemListFromSceneSettings(
|
39
|
+
pinsSettings,
|
40
|
+
foundLibrary['x-scene-blocks'][sceneBlockName],
|
41
|
+
);
|
42
|
+
sceneBlock.updateShape_();
|
43
|
+
}
|
44
|
+
sceneBlock.moveBy(20, 20);
|
45
|
+
sceneBlock.initSvg();
|
46
|
+
sceneBlock.render();
|
47
|
+
|
48
|
+
if (pinsSettings.metadata) {
|
49
|
+
for (const [metadataKey, metadataValue] of Object.entries(pinsSettings.metadata)) {
|
50
|
+
const inputName = 'metadata--' + metadataKey;
|
51
|
+
|
52
|
+
if (sceneBlock.getInput(inputName)) {
|
53
|
+
const parameter = foundLibrary['x-scene-blocks'][sceneBlockName].metadata?.find(
|
54
|
+
(p: { name: string }) => p.name === metadataKey,
|
55
|
+
);
|
56
|
+
if (parameter) {
|
57
|
+
if (parameter.schema.type === 'array' && parameter.schema.items.$ref) {
|
58
|
+
const parameterType = getParameterType(parameter.schema.items);
|
59
|
+
|
60
|
+
if (typeof metadataValue === 'string') {
|
61
|
+
const metadataBlock = generateParameterBlock(
|
62
|
+
{ type: 'string' },
|
63
|
+
metadataValue,
|
64
|
+
workspace,
|
65
|
+
foundLibrary,
|
66
|
+
);
|
67
|
+
const sceneInputConnection = sceneBlock.getInput(
|
68
|
+
inputName + '__EVALUATE',
|
69
|
+
).connection;
|
70
|
+
connectBlock(metadataBlock, sceneInputConnection);
|
71
|
+
continue;
|
72
|
+
}
|
73
|
+
|
74
|
+
for (const componentSettings of (metadataValue as any[]).reverse()) {
|
75
|
+
const componentBlock =
|
76
|
+
parameterType === 'component'
|
77
|
+
? generateBlockFromComponent(
|
78
|
+
componentSettings,
|
79
|
+
workspace,
|
80
|
+
foundLibrary,
|
81
|
+
parameter.schema.items.$ref,
|
82
|
+
)
|
83
|
+
: generateBlockFromPins(componentSettings, workspace);
|
84
|
+
const sceneInputConnection = sceneBlock.getInput(inputName).connection;
|
85
|
+
connectBlock(componentBlock, sceneInputConnection);
|
86
|
+
}
|
87
|
+
} else {
|
88
|
+
const metadataBlock = generateParameterBlock(
|
89
|
+
parameter.schema,
|
90
|
+
metadataValue,
|
91
|
+
workspace,
|
92
|
+
foundLibrary,
|
93
|
+
);
|
94
|
+
const sceneInputConnection = sceneBlock.getInput(inputName).connection;
|
95
|
+
connectBlock(metadataBlock, sceneInputConnection);
|
96
|
+
}
|
97
|
+
} else {
|
98
|
+
console.log(
|
99
|
+
`[generateSceneBlock] - Aucun metadata nommé "${inputName}" trouvé dans les métadonnées de "${sceneBlockName}".`,
|
100
|
+
);
|
101
|
+
}
|
102
|
+
} else {
|
103
|
+
console.log(
|
104
|
+
`[generateSceneBlock] - Le bloc "${sceneBlockId}" n'a pas d'entrée metadata nommée "${inputName}" mais son format JSON semble en avoir une, une perte de donnée est possible si vous sauvegardez sans mettre à jour la librarie "${sceneBlockLibrary}"`,
|
105
|
+
);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}
|
109
|
+
|
110
|
+
if (pinsSettings.properties) {
|
111
|
+
for (const [key, value] of Object.entries(pinsSettings.properties)) {
|
112
|
+
const inputName = key;
|
113
|
+
if (sceneBlock.getInput(inputName)) {
|
114
|
+
const parameter = foundLibrary['x-scene-blocks'][sceneBlockName].parameters?.find(
|
115
|
+
(p: { name: string }) => p.name === inputName,
|
116
|
+
);
|
117
|
+
if (parameter) {
|
118
|
+
if (parameter.schema.type === 'array' && parameter.schema.items.$ref) {
|
119
|
+
const parameterType = getParameterType(parameter.schema.items);
|
120
|
+
|
121
|
+
if (typeof value === 'string') {
|
122
|
+
const metadataBlock = generateParameterBlock(
|
123
|
+
{ type: 'string' },
|
124
|
+
value,
|
125
|
+
workspace,
|
126
|
+
foundLibrary,
|
127
|
+
);
|
128
|
+
const sceneInputConnection = sceneBlock.getInput(
|
129
|
+
inputName + '__EVALUATE',
|
130
|
+
).connection;
|
131
|
+
connectBlock(metadataBlock, sceneInputConnection);
|
132
|
+
continue;
|
133
|
+
}
|
134
|
+
|
135
|
+
for (const componentSettings of (value as any[]).reverse()) {
|
136
|
+
const componentBlock =
|
137
|
+
parameterType === 'component'
|
138
|
+
? generateBlockFromComponent(
|
139
|
+
componentSettings,
|
140
|
+
workspace,
|
141
|
+
foundLibrary,
|
142
|
+
parameter.schema.items.$ref,
|
143
|
+
)
|
144
|
+
: generateBlockFromPins(componentSettings, workspace);
|
145
|
+
const sceneInputConnection = sceneBlock.getInput(inputName).connection;
|
146
|
+
connectBlock(componentBlock, sceneInputConnection);
|
147
|
+
}
|
148
|
+
} else {
|
149
|
+
const metadataBlock = generateParameterBlock(
|
150
|
+
parameter.schema,
|
151
|
+
value,
|
152
|
+
workspace,
|
153
|
+
foundLibrary,
|
154
|
+
);
|
155
|
+
const sceneInputConnection = sceneBlock.getInput(inputName).connection;
|
156
|
+
connectBlock(metadataBlock, sceneInputConnection);
|
157
|
+
}
|
158
|
+
} else {
|
159
|
+
console.log(
|
160
|
+
`[generateSceneBlock] - Aucun paramètre nommé "${inputName}" trouvé dans les métadonnées de "${sceneBlockName}".`,
|
161
|
+
);
|
162
|
+
}
|
163
|
+
} else {
|
164
|
+
console.log(
|
165
|
+
`[generateSceneBlock] - Le bloc "${sceneBlockId}" n'a pas d'entrée parameters nommée "${inputName}" mais son format JSON semble en avoir une, une perte de donnée est possible si vous sauvegardez sans mettre à jour la librarie "${sceneBlockLibrary}"`,
|
166
|
+
);
|
167
|
+
}
|
168
|
+
}
|
169
|
+
}
|
170
|
+
} else {
|
171
|
+
//generating generic debug scene block
|
172
|
+
console.log(
|
173
|
+
'[generateSceneBlock] - Scene missing type or not existing in library ',
|
174
|
+
sceneBlockLibrary,
|
175
|
+
);
|
176
|
+
sceneBlock = workspace.newBlock('generic-scene-block');
|
177
|
+
sceneBlock.initSvg();
|
178
|
+
|
179
|
+
const typeValue = pinsSettings && pinsSettings.type ? pinsSettings.type : 'null';
|
180
|
+
sceneBlock.setFieldValue(typeValue, 'SCENE_TYPE');
|
181
|
+
|
182
|
+
const metadataValue = pinsSettings && pinsSettings.metadata ? pinsSettings.metadata : null;
|
183
|
+
const metadataBlock = generateBlockFromJson(metadataValue, workspace);
|
184
|
+
|
185
|
+
const sceneInputConnection = sceneBlock.getInput('METADATA_INPUT').connection;
|
186
|
+
connectBlock(metadataBlock, sceneInputConnection);
|
187
|
+
}
|
188
|
+
|
189
|
+
const pinsConnection = sceneBlock.getInput('EXECUTE_PINS')?.connection;
|
190
|
+
|
191
|
+
if (pinsConnection) {
|
192
|
+
const reversedPins = [...(pinsSettings.pins || [])].reverse();
|
193
|
+
for (const pinsSetting of reversedPins) {
|
194
|
+
const pinsBlock = generateBlockFromPins(pinsSetting, workspace);
|
195
|
+
connectBlock(pinsBlock, pinsConnection);
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
sceneBlock.render();
|
200
|
+
return sceneBlock;
|
201
|
+
}
|
202
|
+
|
203
|
+
function generateBlockFromPins(pinsSettings: any, workspace: any): any {
|
204
|
+
const blockPath = pinsSettings.library + '/__PINS__/' + pinsSettings.element;
|
205
|
+
const library: any = getLibraryFromName(pinsSettings.library);
|
206
|
+
|
207
|
+
if (!library) {
|
208
|
+
const pinsBlock = workspace.newBlock('unknown-pins');
|
209
|
+
console.log('[generateBlockFromPins] - Library not loaded', pinsSettings.library);
|
210
|
+
pinsBlock.initSvg();
|
211
|
+
pinsBlock.render();
|
212
|
+
return pinsBlock;
|
213
|
+
}
|
214
|
+
|
215
|
+
const pinsDefinition = library.paths['/' + pinsSettings.element]?.post;
|
216
|
+
|
217
|
+
if (!pinsDefinition) {
|
218
|
+
const pinsBlock = workspace.newBlock('unknown-pins');
|
219
|
+
console.log(
|
220
|
+
'[generateBlockFromPins] - Pins not existing in library',
|
221
|
+
pinsSettings.element,
|
222
|
+
pinsSettings.library,
|
223
|
+
);
|
224
|
+
pinsBlock.initSvg();
|
225
|
+
pinsBlock.render();
|
226
|
+
return pinsBlock;
|
227
|
+
}
|
228
|
+
|
229
|
+
const pinsBlock = workspace.newBlock(blockPath);
|
230
|
+
if (pinsBlock.mutator) {
|
231
|
+
pinsBlock.itemList_ = itemListFromPinsSettings(pinsSettings, pinsDefinition);
|
232
|
+
pinsBlock.updateShape_();
|
233
|
+
}
|
234
|
+
pinsBlock.initSvg();
|
235
|
+
pinsBlock.render();
|
236
|
+
|
237
|
+
if (
|
238
|
+
library.paths['/' + pinsSettings.element].post.tags &&
|
239
|
+
library.paths['/' + pinsSettings.element].post.tags.includes('needPins') &&
|
240
|
+
pinsSettings.pins &&
|
241
|
+
pinsSettings.pins.length > 0
|
242
|
+
) {
|
243
|
+
const pinsConnection = pinsBlock.getInput('pins').connection;
|
244
|
+
|
245
|
+
const reversedPins = [...(pinsSettings.pins || [])].reverse();
|
246
|
+
for (const pinsSetting of reversedPins) {
|
247
|
+
const pinsBlock = generateBlockFromPins(pinsSetting, workspace);
|
248
|
+
connectBlock(pinsBlock, pinsConnection);
|
249
|
+
}
|
250
|
+
}
|
251
|
+
|
252
|
+
for (const parameter of pinsDefinition.parameters) {
|
253
|
+
if (
|
254
|
+
!pinsSettings.properties ||
|
255
|
+
!Object.prototype.hasOwnProperty.call(pinsSettings.properties, parameter.name)
|
256
|
+
) {
|
257
|
+
continue;
|
258
|
+
}
|
259
|
+
|
260
|
+
const valueToLoad = pinsSettings.properties[parameter.name];
|
261
|
+
if (parameter.schema.type === 'array' && parameter.schema.items.$ref) {
|
262
|
+
const parameterType = getParameterType(parameter.schema.items);
|
263
|
+
|
264
|
+
if (typeof valueToLoad === 'string') {
|
265
|
+
const parameterBlock = generateParameterBlock(
|
266
|
+
{ type: 'string' },
|
267
|
+
valueToLoad,
|
268
|
+
workspace,
|
269
|
+
library,
|
270
|
+
);
|
271
|
+
const inputConnection = pinsBlock.getInput(parameter.name + '__EVALUATE').connection;
|
272
|
+
connectBlock(parameterBlock, inputConnection);
|
273
|
+
continue;
|
274
|
+
}
|
275
|
+
|
276
|
+
for (const propertyValue of (valueToLoad as any[]).reverse()) {
|
277
|
+
const parameterBlock =
|
278
|
+
parameterType === 'component'
|
279
|
+
? generateBlockFromComponent(
|
280
|
+
propertyValue,
|
281
|
+
workspace,
|
282
|
+
library,
|
283
|
+
parameter.schema.items.$ref,
|
284
|
+
)
|
285
|
+
: generateBlockFromPins(propertyValue, workspace);
|
286
|
+
const inputConnection = pinsBlock.getInput(parameter.name).connection;
|
287
|
+
connectBlock(parameterBlock, inputConnection);
|
288
|
+
}
|
289
|
+
} else {
|
290
|
+
const parameterBlock = generateParameterBlock(
|
291
|
+
parameter.schema,
|
292
|
+
valueToLoad,
|
293
|
+
workspace,
|
294
|
+
library,
|
295
|
+
);
|
296
|
+
const inputConnection = pinsBlock.getInput(parameter.name).connection;
|
297
|
+
connectBlock(parameterBlock, inputConnection);
|
298
|
+
}
|
299
|
+
}
|
300
|
+
|
301
|
+
for (const event of pinsDefinition['x-events'] || []) {
|
302
|
+
if (
|
303
|
+
!pinsSettings.events ||
|
304
|
+
!Object.prototype.hasOwnProperty.call(pinsSettings.events, event.name)
|
305
|
+
) {
|
306
|
+
continue;
|
307
|
+
}
|
308
|
+
|
309
|
+
const valueToLoad = pinsSettings.events[event.name];
|
310
|
+
for (const propertyValue of (valueToLoad as any[]).reverse()) {
|
311
|
+
const parameterBlock = generateBlockFromPins(propertyValue, workspace);
|
312
|
+
const inputConnection = pinsBlock.getInput('__EVENT__/' + event.name).connection;
|
313
|
+
connectBlock(parameterBlock, inputConnection);
|
314
|
+
}
|
315
|
+
}
|
316
|
+
|
317
|
+
const conditions = [
|
318
|
+
{ name: 'if', schema: { type: 'boolean' } },
|
319
|
+
{ name: 'each', schema: { type: 'array', items: { type: 'object' } } },
|
320
|
+
] as any[];
|
321
|
+
for (const parameter of conditions) {
|
322
|
+
if (
|
323
|
+
!pinsSettings.conditions ||
|
324
|
+
!Object.prototype.hasOwnProperty.call(pinsSettings.conditions, parameter.name)
|
325
|
+
) {
|
326
|
+
continue;
|
327
|
+
}
|
328
|
+
|
329
|
+
const valueToLoad = pinsSettings.conditions[parameter.name];
|
330
|
+
if (parameter.schema.type === 'array' && parameter.schema.items?.$ref) {
|
331
|
+
const parameterType = getParameterType(parameter.schema.items);
|
332
|
+
|
333
|
+
if (typeof valueToLoad === 'string') {
|
334
|
+
const parameterBlock = generateParameterBlock(
|
335
|
+
{ type: 'string' },
|
336
|
+
valueToLoad,
|
337
|
+
workspace,
|
338
|
+
library,
|
339
|
+
);
|
340
|
+
const inputConnection = pinsBlock.getInput(
|
341
|
+
'__CONDITION__/' + parameter.name + '__EVALUATE',
|
342
|
+
).connection;
|
343
|
+
connectBlock(parameterBlock, inputConnection);
|
344
|
+
continue;
|
345
|
+
}
|
346
|
+
|
347
|
+
for (const propertyValue of (valueToLoad as any[]).reverse()) {
|
348
|
+
const parameterBlock =
|
349
|
+
parameterType === 'component'
|
350
|
+
? generateBlockFromComponent(
|
351
|
+
propertyValue,
|
352
|
+
workspace,
|
353
|
+
library,
|
354
|
+
parameter.schema.items.$ref,
|
355
|
+
)
|
356
|
+
: generateBlockFromPins(propertyValue, workspace);
|
357
|
+
const inputConnection = pinsBlock.getInput('__CONDITION__/' + parameter.name).connection;
|
358
|
+
connectBlock(parameterBlock, inputConnection);
|
359
|
+
}
|
360
|
+
} else {
|
361
|
+
const parameterBlock = generateParameterBlock(
|
362
|
+
parameter.schema,
|
363
|
+
valueToLoad,
|
364
|
+
workspace,
|
365
|
+
library,
|
366
|
+
);
|
367
|
+
const inputConnection = pinsBlock.getInput('__CONDITION__/' + parameter.name).connection;
|
368
|
+
connectBlock(parameterBlock, inputConnection);
|
369
|
+
}
|
370
|
+
}
|
371
|
+
|
372
|
+
return pinsBlock;
|
373
|
+
}
|
374
|
+
|
375
|
+
function generateBlockFromComponent(
|
376
|
+
componentSettings: any,
|
377
|
+
workspace: any,
|
378
|
+
library: { info: { title: string }; components: { schemas: { [x: string]: any } } },
|
379
|
+
component$ref: string,
|
380
|
+
): any {
|
381
|
+
const componentName = component$ref.split('/').pop() as string;
|
382
|
+
const blockPath = library.info.title + '/__COMPONENTS__/' + componentName;
|
383
|
+
|
384
|
+
const componentDefinition = library.components.schemas[componentName];
|
385
|
+
|
386
|
+
if (!componentDefinition) {
|
387
|
+
const componentBlock = workspace.newBlock('unknown-component');
|
388
|
+
componentBlock.initSvg();
|
389
|
+
componentBlock.render();
|
390
|
+
return componentBlock;
|
391
|
+
}
|
392
|
+
|
393
|
+
const componentBlock = workspace.newBlock(blockPath);
|
394
|
+
if (componentBlock.mutator) {
|
395
|
+
componentBlock.itemList_ = itemListFromComponentSettings(
|
396
|
+
componentSettings,
|
397
|
+
componentDefinition,
|
398
|
+
);
|
399
|
+
componentBlock.updateShape_();
|
400
|
+
}
|
401
|
+
componentBlock.initSvg();
|
402
|
+
componentBlock.render();
|
403
|
+
|
404
|
+
for (const [propertyKey, schema] of Object.entries<any>(componentDefinition.properties)) {
|
405
|
+
if (!Object.prototype.hasOwnProperty.call(componentSettings, propertyKey)) {
|
406
|
+
continue;
|
407
|
+
}
|
408
|
+
const valueToLoad = componentSettings[propertyKey];
|
409
|
+
|
410
|
+
if (schema.type === 'array' && schema.items.$ref) {
|
411
|
+
const parameterType = getParameterType(schema.items);
|
412
|
+
|
413
|
+
if (typeof valueToLoad === 'string') {
|
414
|
+
const parameterBlock = generateParameterBlock(
|
415
|
+
{ type: 'string' },
|
416
|
+
valueToLoad,
|
417
|
+
workspace,
|
418
|
+
library,
|
419
|
+
);
|
420
|
+
const componentInputConnection = componentBlock.getInput(
|
421
|
+
propertyKey + '__EVALUATE',
|
422
|
+
).connection;
|
423
|
+
connectBlock(parameterBlock, componentInputConnection);
|
424
|
+
continue;
|
425
|
+
}
|
426
|
+
|
427
|
+
for (const propertyValue of (valueToLoad as any[]).reverse()) {
|
428
|
+
const parameterBlock =
|
429
|
+
parameterType === 'component'
|
430
|
+
? generateBlockFromComponent(propertyValue, workspace, library, schema.items.$ref)
|
431
|
+
: generateBlockFromPins(propertyValue, workspace);
|
432
|
+
const componentInputConnection = componentBlock.getInput(propertyKey).connection;
|
433
|
+
connectBlock(parameterBlock, componentInputConnection);
|
434
|
+
}
|
435
|
+
} else {
|
436
|
+
const parameterBlock = generateParameterBlock(schema, valueToLoad, workspace, library);
|
437
|
+
const componentInputConnection = componentBlock.getInput(propertyKey).connection;
|
438
|
+
connectBlock(parameterBlock, componentInputConnection);
|
439
|
+
}
|
440
|
+
}
|
441
|
+
|
442
|
+
componentBlock.render();
|
443
|
+
|
444
|
+
return componentBlock;
|
445
|
+
}
|
446
|
+
|
447
|
+
function generateParameterBlock(
|
448
|
+
schema: { type: string; $ref?: any; items?: any },
|
449
|
+
valueToLoad: any,
|
450
|
+
workspace: { newBlock: (arg0: string) => any },
|
451
|
+
library: any,
|
452
|
+
) {
|
453
|
+
if (valueToLoad === undefined) {
|
454
|
+
return;
|
455
|
+
}
|
456
|
+
|
457
|
+
const parameterType = typeof valueToLoad === 'string' ? 'string' : getParameterType(schema);
|
458
|
+
let arrayInputConnection;
|
459
|
+
let paramBlock;
|
460
|
+
|
461
|
+
switch (parameterType) {
|
462
|
+
case 'string':
|
463
|
+
paramBlock = workspace.newBlock('text_multiline');
|
464
|
+
if (valueToLoad === undefined) {
|
465
|
+
console.log(`[getParameterType] - valueToLoad is undefined in case string`);
|
466
|
+
break;
|
467
|
+
}
|
468
|
+
paramBlock.setFieldValue(valueToLoad, 'TEXT');
|
469
|
+
break;
|
470
|
+
case 'number':
|
471
|
+
paramBlock = workspace.newBlock('math_number');
|
472
|
+
if (valueToLoad === undefined) {
|
473
|
+
console.log(`[getParameterType] - valueToLoad is undefined in case number`);
|
474
|
+
break;
|
475
|
+
}
|
476
|
+
paramBlock.getField('NUM').setValue(valueToLoad);
|
477
|
+
break;
|
478
|
+
case 'boolean':
|
479
|
+
paramBlock = workspace.newBlock('logic_boolean');
|
480
|
+
if (valueToLoad === true) {
|
481
|
+
paramBlock.getField('BOOL').setValue('TRUE');
|
482
|
+
} else if (valueToLoad === false) {
|
483
|
+
paramBlock.getField('BOOL').setValue('FALSE');
|
484
|
+
} else {
|
485
|
+
console.log(`[getParameterType] - valueToLoad is undefined in case boolean`);
|
486
|
+
break;
|
487
|
+
}
|
488
|
+
break;
|
489
|
+
case 'array':
|
490
|
+
paramBlock = workspace.newBlock('array');
|
491
|
+
paramBlock.initSvg();
|
492
|
+
paramBlock.render();
|
493
|
+
arrayInputConnection = paramBlock.getInput('MEMBERS').connection;
|
494
|
+
|
495
|
+
for (let key = valueToLoad.length - 1; key >= 0; key--) {
|
496
|
+
const memberBlock = workspace.newBlock('array-input');
|
497
|
+
memberBlock.initSvg();
|
498
|
+
memberBlock.render();
|
499
|
+
|
500
|
+
const parentConnection = memberBlock.getInput('MEMBER_VALUE').connection;
|
501
|
+
const newBlock = generateParameterBlock(schema.items, valueToLoad[key], workspace, library);
|
502
|
+
connectBlock(newBlock, parentConnection);
|
503
|
+
|
504
|
+
memberBlock.initSvg();
|
505
|
+
memberBlock.render();
|
506
|
+
|
507
|
+
connectBlock(memberBlock, arrayInputConnection);
|
508
|
+
}
|
509
|
+
break;
|
510
|
+
case 'component':
|
511
|
+
paramBlock = generateBlockFromComponent(valueToLoad, workspace, library, schema.$ref);
|
512
|
+
break;
|
513
|
+
case 'pins':
|
514
|
+
paramBlock = generateBlockFromPins(valueToLoad, workspace);
|
515
|
+
break;
|
516
|
+
case 'object':
|
517
|
+
paramBlock = generateBlockFromJson(valueToLoad, workspace);
|
518
|
+
break;
|
519
|
+
default:
|
520
|
+
paramBlock = workspace.newBlock('unknown-format');
|
521
|
+
break;
|
522
|
+
}
|
523
|
+
|
524
|
+
return paramBlock;
|
525
|
+
}
|
526
|
+
|
527
|
+
function getParameterType(schema: any) {
|
528
|
+
let typeToReturn = 'unknown-type';
|
529
|
+
const basicTypes = ['string', 'number', 'boolean', 'array', 'object'];
|
530
|
+
|
531
|
+
if (schema !== null && typeof schema !== 'undefined') {
|
532
|
+
if (Object.prototype.hasOwnProperty.call(schema, 'type')) {
|
533
|
+
if (basicTypes.includes(schema.type)) {
|
534
|
+
typeToReturn = schema.type;
|
535
|
+
}
|
536
|
+
} else if (Object.prototype.hasOwnProperty.call(schema, '$ref')) {
|
537
|
+
if (schema.$ref.includes('#/components/schemas/')) {
|
538
|
+
typeToReturn = 'component';
|
539
|
+
} else typeToReturn = 'pins';
|
540
|
+
}
|
541
|
+
}
|
542
|
+
|
543
|
+
return typeToReturn;
|
544
|
+
}
|
545
|
+
|
546
|
+
function getLibraryFromName(name: any) {
|
547
|
+
const library = libraries.find(
|
548
|
+
(library: { info: { title: any } }) => library.info.title === name,
|
549
|
+
);
|
550
|
+
return library;
|
551
|
+
}
|
552
|
+
|
553
|
+
function connectBlock(targetBlock: any, parentConnection: any) {
|
554
|
+
if (parentConnection) {
|
555
|
+
const childConnection = targetBlock?.outputConnection || targetBlock?.previousConnection;
|
556
|
+
parentConnection.connect(childConnection);
|
557
|
+
}
|
558
|
+
targetBlock?.initSvg();
|
559
|
+
targetBlock?.render();
|
560
|
+
}
|
561
|
+
|
562
|
+
function generateBlockFromJson(json_structure: any, workspace: any) {
|
563
|
+
if (json_structure === null) {
|
564
|
+
const targetBlock = workspace.newBlock('logic_null');
|
565
|
+
return targetBlock;
|
566
|
+
}
|
567
|
+
|
568
|
+
const type = typeof json_structure;
|
569
|
+
let targetBlock;
|
570
|
+
let memberInputConnection;
|
571
|
+
|
572
|
+
switch (type) {
|
573
|
+
case 'string':
|
574
|
+
targetBlock = workspace.newBlock('text_multiline');
|
575
|
+
targetBlock.setFieldValue(json_structure, 'TEXT');
|
576
|
+
break;
|
577
|
+
case 'number':
|
578
|
+
targetBlock = workspace.newBlock('math_number');
|
579
|
+
targetBlock.setFieldValue(String(json_structure), 'NUM');
|
580
|
+
break;
|
581
|
+
case 'boolean':
|
582
|
+
targetBlock = workspace.newBlock('logic_boolean');
|
583
|
+
targetBlock.setFieldValue(json_structure ? 'TRUE' : 'FALSE', 'BOOL');
|
584
|
+
break;
|
585
|
+
case 'object':
|
586
|
+
if (Array.isArray(json_structure)) {
|
587
|
+
targetBlock = workspace.newBlock('array');
|
588
|
+
targetBlock.initSvg();
|
589
|
+
targetBlock.render();
|
590
|
+
memberInputConnection = targetBlock.getInput('MEMBERS').connection;
|
591
|
+
|
592
|
+
for (let key = json_structure.length - 1; key >= 0; key--) {
|
593
|
+
const memberBlock = workspace.newBlock('array-input');
|
594
|
+
memberBlock.initSvg();
|
595
|
+
memberBlock.render();
|
596
|
+
|
597
|
+
const parentConnection = memberBlock.getInput('MEMBER_VALUE').connection;
|
598
|
+
const newBlock = generateBlockFromJson(json_structure[key], workspace);
|
599
|
+
connectBlock(newBlock, parentConnection);
|
600
|
+
|
601
|
+
memberBlock.initSvg();
|
602
|
+
memberBlock.render();
|
603
|
+
|
604
|
+
connectBlock(memberBlock, memberInputConnection);
|
605
|
+
}
|
606
|
+
} else {
|
607
|
+
targetBlock = workspace.newBlock('object');
|
608
|
+
targetBlock.initSvg();
|
609
|
+
targetBlock.render();
|
610
|
+
memberInputConnection = targetBlock.getInput('MEMBERS').connection;
|
611
|
+
|
612
|
+
const keys = Object.keys(json_structure);
|
613
|
+
for (let key = keys.length - 1; key >= 0; key--) {
|
614
|
+
const memberBlock = workspace.newBlock('member');
|
615
|
+
memberBlock.setFieldValue(keys[key], 'MEMBER_NAME');
|
616
|
+
memberBlock.initSvg();
|
617
|
+
memberBlock.render();
|
618
|
+
|
619
|
+
const parentConnection = memberBlock.getInput('MEMBER_VALUE').connection;
|
620
|
+
const newBlock = generateBlockFromJson(json_structure[keys[key]], workspace);
|
621
|
+
connectBlock(newBlock, parentConnection);
|
622
|
+
|
623
|
+
memberBlock.initSvg();
|
624
|
+
memberBlock.render();
|
625
|
+
|
626
|
+
connectBlock(memberBlock, memberInputConnection);
|
627
|
+
}
|
628
|
+
}
|
629
|
+
break;
|
630
|
+
}
|
631
|
+
return targetBlock;
|
632
|
+
}
|
633
|
+
|
634
|
+
function itemListFromPinsSettings(
|
635
|
+
pinsSettings: { properties: any; events: any; conditions: any; pins: PinsSettings[] },
|
636
|
+
pinsDefinition: { [x: string]: any; parameters: any; tags: string | string[] },
|
637
|
+
): any {
|
638
|
+
const inputArray: { id: any; name: any }[] = [];
|
639
|
+
|
640
|
+
if (!pinsSettings || !pinsDefinition) {
|
641
|
+
console.log('[itemListFromPinsSettings] - Undefined pinsSettings or pinsDefinition');
|
642
|
+
return inputArray;
|
643
|
+
}
|
644
|
+
|
645
|
+
if (pinsDefinition.parameters && pinsSettings.properties) {
|
646
|
+
for (const parameter of pinsDefinition.parameters) {
|
647
|
+
if (!Object.prototype.hasOwnProperty.call(pinsSettings.properties, parameter.name)) {
|
648
|
+
continue;
|
649
|
+
}
|
650
|
+
|
651
|
+
const isEvaluate =
|
652
|
+
parameter.schema?.type === 'array' &&
|
653
|
+
typeof pinsSettings.properties[parameter.name] === 'string' &&
|
654
|
+
(parameter.schema.items?.$ref === 'https://schemas.digipair.ai/pinsSettings' ||
|
655
|
+
parameter.schema.items?.$ref?.includes('#/components/schemas/'));
|
656
|
+
const name = isEvaluate ? parameter.name + '__EVALUATE' : parameter.name;
|
657
|
+
const summary = isEvaluate
|
658
|
+
? (parameter.summary || parameter.name) + ' (evaluate)'
|
659
|
+
: parameter.summary || parameter.name;
|
660
|
+
|
661
|
+
inputArray.push({ id: name, name: summary });
|
662
|
+
}
|
663
|
+
}
|
664
|
+
|
665
|
+
if (
|
666
|
+
pinsDefinition.tags &&
|
667
|
+
pinsDefinition.tags.includes('needPins') &&
|
668
|
+
pinsSettings.pins &&
|
669
|
+
pinsSettings.pins.length > 0
|
670
|
+
) {
|
671
|
+
inputArray.push({ id: 'pins', name: 'pins' });
|
672
|
+
}
|
673
|
+
|
674
|
+
if (pinsDefinition['x-events'] && pinsSettings.events) {
|
675
|
+
for (const event of pinsDefinition['x-events']) {
|
676
|
+
if (!Object.prototype.hasOwnProperty.call(pinsSettings.events, event.name)) {
|
677
|
+
continue;
|
678
|
+
}
|
679
|
+
inputArray.push({
|
680
|
+
id: '__EVENT__/' + event.name,
|
681
|
+
name: '@' + (event.summary || event.name),
|
682
|
+
});
|
683
|
+
}
|
684
|
+
}
|
685
|
+
|
686
|
+
if (pinsSettings.conditions) {
|
687
|
+
if (pinsSettings.conditions.if) {
|
688
|
+
inputArray.push({ id: '__CONDITION__/if', name: '#if' });
|
689
|
+
}
|
690
|
+
|
691
|
+
if (pinsSettings.conditions.each) {
|
692
|
+
inputArray.push({ id: '__CONDITION__/each', name: '#each' });
|
693
|
+
}
|
694
|
+
}
|
695
|
+
|
696
|
+
return inputArray;
|
697
|
+
}
|
698
|
+
|
699
|
+
function itemListFromComponentSettings(
|
700
|
+
componentSettings: any,
|
701
|
+
componentDefinition: { properties: { [s: string]: any } | ArrayLike<any> },
|
702
|
+
): any {
|
703
|
+
const inputArray = [];
|
704
|
+
|
705
|
+
for (const [propertyKey, propertyValue] of Object.entries<any>(componentDefinition.properties)) {
|
706
|
+
if (
|
707
|
+
!Object.prototype.hasOwnProperty.call(componentSettings, propertyKey) ||
|
708
|
+
(propertyValue.type === 'array' &&
|
709
|
+
(!componentSettings[propertyKey] || componentSettings[propertyKey].length === 0))
|
710
|
+
) {
|
711
|
+
continue;
|
712
|
+
}
|
713
|
+
|
714
|
+
const isEvaluate =
|
715
|
+
propertyValue.type === 'array' &&
|
716
|
+
typeof componentSettings[propertyKey] === 'string' &&
|
717
|
+
(propertyValue.items?.$ref === 'https://schemas.digipair.ai/pinsSettings' ||
|
718
|
+
propertyValue.items?.$ref?.includes('#/components/schemas/'));
|
719
|
+
const name = isEvaluate ? propertyKey + '__EVALUATE' : propertyKey;
|
720
|
+
const summary = isEvaluate
|
721
|
+
? (propertyValue.summary || propertyKey) + ' (evaluate)'
|
722
|
+
: propertyValue.summary || propertyKey;
|
723
|
+
|
724
|
+
inputArray.push({ id: name, name: summary });
|
725
|
+
}
|
726
|
+
|
727
|
+
return inputArray;
|
728
|
+
}
|
729
|
+
|
730
|
+
function itemListFromSceneSettings(
|
731
|
+
sceneSettings: { properties: any },
|
732
|
+
sceneDefinition: { parameters: any },
|
733
|
+
): any {
|
734
|
+
const inputArray = [];
|
735
|
+
|
736
|
+
if (sceneDefinition.parameters && sceneSettings.properties) {
|
737
|
+
for (const parameter of sceneDefinition.parameters) {
|
738
|
+
if (!Object.prototype.hasOwnProperty.call(sceneSettings.properties, parameter.name)) {
|
739
|
+
continue;
|
740
|
+
}
|
741
|
+
|
742
|
+
const isEvaluate =
|
743
|
+
parameter.schema?.type === 'array' &&
|
744
|
+
typeof sceneSettings.properties[parameter.name] === 'string' &&
|
745
|
+
(parameter.schema.items?.$ref === 'https://schemas.digipair.ai/pinsSettings' ||
|
746
|
+
parameter.schema.items?.$ref?.includes('#/components/schemas/'));
|
747
|
+
const name = isEvaluate ? parameter.name + '__EVALUATE' : parameter.name;
|
748
|
+
const summary = isEvaluate
|
749
|
+
? (parameter.summary || parameter.name) + ' (evaluate)'
|
750
|
+
: parameter.summary || parameter.name;
|
751
|
+
|
752
|
+
inputArray.push({ id: name, name: summary });
|
753
|
+
}
|
754
|
+
}
|
755
|
+
|
756
|
+
return inputArray;
|
757
|
+
}
|