@rkmodules/rules 0.0.50 → 0.0.52
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/dist/index.cjs.css +850 -0
- package/dist/index.cjs.js +981 -3
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.css +850 -0
- package/dist/index.esm.js +3289 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +8 -4
|
@@ -0,0 +1,3289 @@
|
|
|
1
|
+
import { create, all } from 'mathjs';
|
|
2
|
+
import React, { createContext, useLayoutEffect, useEffect, useState, useCallback, useMemo, isValidElement, cloneElement, useContext } from 'react';
|
|
3
|
+
import { Handle, Position, useConnection, getSimpleBezierPath, ReactFlowProvider, useOnSelectionChange, applyNodeChanges, applyEdgeChanges, ReactFlow, Background, BackgroundVariant, Controls } from '@xyflow/react';
|
|
4
|
+
import katex from 'katex';
|
|
5
|
+
import classNames from 'classnames';
|
|
6
|
+
import rcin from 'rc-input-number';
|
|
7
|
+
import { create as create$1 } from 'zustand';
|
|
8
|
+
import { persist } from 'zustand/middleware';
|
|
9
|
+
|
|
10
|
+
/******************************************************************************
|
|
11
|
+
Copyright (c) Microsoft Corporation.
|
|
12
|
+
|
|
13
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
14
|
+
purpose with or without fee is hereby granted.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
17
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
18
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
19
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
20
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
21
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
22
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
23
|
+
***************************************************************************** */
|
|
24
|
+
/* global Reflect, Promise, SuppressedError, Symbol, Iterator */
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
var __assign = function() {
|
|
28
|
+
__assign = Object.assign || function __assign(t) {
|
|
29
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
30
|
+
s = arguments[i];
|
|
31
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
32
|
+
}
|
|
33
|
+
return t;
|
|
34
|
+
};
|
|
35
|
+
return __assign.apply(this, arguments);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
function __rest(s, e) {
|
|
39
|
+
var t = {};
|
|
40
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
41
|
+
t[p] = s[p];
|
|
42
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
43
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
44
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
45
|
+
t[p[i]] = s[p[i]];
|
|
46
|
+
}
|
|
47
|
+
return t;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
51
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
52
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
53
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
54
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
55
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
56
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function __generator(thisArg, body) {
|
|
61
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
62
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
63
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
64
|
+
function step(op) {
|
|
65
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
66
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
67
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
68
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
69
|
+
switch (op[0]) {
|
|
70
|
+
case 0: case 1: t = op; break;
|
|
71
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
72
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
73
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
74
|
+
default:
|
|
75
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
76
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
77
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
78
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
79
|
+
if (t[2]) _.ops.pop();
|
|
80
|
+
_.trys.pop(); continue;
|
|
81
|
+
}
|
|
82
|
+
op = body.call(thisArg, _);
|
|
83
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
84
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function __values(o) {
|
|
89
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
90
|
+
if (m) return m.call(o);
|
|
91
|
+
if (o && typeof o.length === "number") return {
|
|
92
|
+
next: function () {
|
|
93
|
+
if (o && i >= o.length) o = void 0;
|
|
94
|
+
return { value: o && o[i++], done: !o };
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function __read(o, n) {
|
|
101
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
102
|
+
if (!m) return o;
|
|
103
|
+
var i = m.call(o), r, ar = [], e;
|
|
104
|
+
try {
|
|
105
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
106
|
+
}
|
|
107
|
+
catch (error) { e = { error: error }; }
|
|
108
|
+
finally {
|
|
109
|
+
try {
|
|
110
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
111
|
+
}
|
|
112
|
+
finally { if (e) throw e.error; }
|
|
113
|
+
}
|
|
114
|
+
return ar;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function __spreadArray(to, from, pack) {
|
|
118
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
119
|
+
if (ar || !(i in from)) {
|
|
120
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
121
|
+
ar[i] = from[i];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
128
|
+
var e = new Error(message);
|
|
129
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
var DISCARD = Symbol("DISCARD");
|
|
133
|
+
/**
|
|
134
|
+
* removes the common prefix from the paths in the tree
|
|
135
|
+
* @param tree
|
|
136
|
+
* @returns
|
|
137
|
+
*/
|
|
138
|
+
function simplifyTree(tree) {
|
|
139
|
+
var keys = Object.keys(tree);
|
|
140
|
+
if (keys.length <= 1)
|
|
141
|
+
return tree; // ✅ Don't simplify if only one path
|
|
142
|
+
// Parse all paths into number arrays
|
|
143
|
+
var parsedPaths = keys.map(function (path) { return path.split(";").map(Number); });
|
|
144
|
+
// Find common prefix
|
|
145
|
+
var prefixLength = parsedPaths[0].length;
|
|
146
|
+
for (var i = 1; i < parsedPaths.length; i++) {
|
|
147
|
+
var path = parsedPaths[i];
|
|
148
|
+
for (var j = 0; j < prefixLength; j++) {
|
|
149
|
+
if (path[j] !== parsedPaths[0][j]) {
|
|
150
|
+
prefixLength = j;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
var simplified = {};
|
|
156
|
+
for (var i = 0; i < parsedPaths.length; i++) {
|
|
157
|
+
var oldPath = keys[i];
|
|
158
|
+
var newPath = parsedPaths[i].slice(prefixLength).join(";");
|
|
159
|
+
simplified[newPath] = tree[oldPath];
|
|
160
|
+
}
|
|
161
|
+
return simplified;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Expands a tree by applying a function to each item in the tree.
|
|
165
|
+
* @param tree
|
|
166
|
+
* @param fn
|
|
167
|
+
* @returns
|
|
168
|
+
*/
|
|
169
|
+
function expandTree(tree, fn) {
|
|
170
|
+
var result = {};
|
|
171
|
+
var _loop_1 = function (path) {
|
|
172
|
+
var items = tree[path];
|
|
173
|
+
items.forEach(function (item, i) {
|
|
174
|
+
var subpath = "".concat(path, ";").concat(i); // Create new sub-branch per item
|
|
175
|
+
var expandedItems = fn(item, path, i); // Apply function
|
|
176
|
+
// if (expandedItems.length > 0) {
|
|
177
|
+
result[subpath] = expandedItems;
|
|
178
|
+
// }
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
for (var path in tree) {
|
|
182
|
+
_loop_1(path);
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Creates a new branch for each item in the existing tree branches
|
|
188
|
+
* @param tree
|
|
189
|
+
* @returns
|
|
190
|
+
*/
|
|
191
|
+
function graftTree(tree) {
|
|
192
|
+
return Object.fromEntries(Object.entries(tree).reduce(function (entries, _a) {
|
|
193
|
+
var _b = __read(_a, 2), path = _b[0], values = _b[1];
|
|
194
|
+
entries.push.apply(entries, __spreadArray([], __read(values.map(function (value, index) {
|
|
195
|
+
return [path + ";" + index, [value]];
|
|
196
|
+
})), false));
|
|
197
|
+
return entries;
|
|
198
|
+
}, []));
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Reduce the complexity of a tree by merging the outermost branches.
|
|
202
|
+
* @param tree
|
|
203
|
+
*/
|
|
204
|
+
function trimTree(tree, depth) {
|
|
205
|
+
var _a;
|
|
206
|
+
if (depth === void 0) { depth = 1; }
|
|
207
|
+
if (depth <= 0)
|
|
208
|
+
return tree;
|
|
209
|
+
var result = {};
|
|
210
|
+
for (var path in tree) {
|
|
211
|
+
var parts = path.split(";");
|
|
212
|
+
// leave at least a path length of 1
|
|
213
|
+
var end = depth >= parts.length ? 1 : -depth;
|
|
214
|
+
var newPath = parts.slice(0, end).join(";");
|
|
215
|
+
if (!result[newPath]) {
|
|
216
|
+
result[newPath] = [];
|
|
217
|
+
}
|
|
218
|
+
// flatten onto new branch
|
|
219
|
+
(_a = result[newPath]).push.apply(_a, __spreadArray([], __read(tree[path]), false));
|
|
220
|
+
}
|
|
221
|
+
return result;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* checks if the given structure is a data tree
|
|
225
|
+
*/
|
|
226
|
+
function isTree(value) {
|
|
227
|
+
return (typeof value === "object" &&
|
|
228
|
+
value !== null &&
|
|
229
|
+
!Array.isArray(value) &&
|
|
230
|
+
Object.keys(value).length > 0 &&
|
|
231
|
+
Object.values(value).every(function (v) { return Array.isArray(v); }));
|
|
232
|
+
}
|
|
233
|
+
function isSingleTon(value) {
|
|
234
|
+
return (Object.keys(value).length === 1 && Object.values(value)[0].length === 1);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* turns a value or array of values into a tree
|
|
238
|
+
*/
|
|
239
|
+
function broadCast(value) {
|
|
240
|
+
if (Array.isArray(value)) {
|
|
241
|
+
return { "0": value };
|
|
242
|
+
}
|
|
243
|
+
if (isTree(value)) {
|
|
244
|
+
return value;
|
|
245
|
+
}
|
|
246
|
+
return { "0": [value] };
|
|
247
|
+
}
|
|
248
|
+
function getBranch(tree, path) {
|
|
249
|
+
return tree[path];
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* maps a tree, the tree branches are flatmapped, so if an array is returned for each item in the list, a flat list is returned
|
|
253
|
+
* @param tree
|
|
254
|
+
* @param fn
|
|
255
|
+
* @returns
|
|
256
|
+
*/
|
|
257
|
+
function mapTree(tree, fn) {
|
|
258
|
+
return nAryOnTree([tree], function (_a, _b, index) {
|
|
259
|
+
var _c = __read(_a, 1), item = _c[0];
|
|
260
|
+
var _d = __read(_b, 1), path = _d[0];
|
|
261
|
+
return fn(item, path, index);
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
function mapTreeBranch(tree, fn) {
|
|
265
|
+
return nAryOnTreeBranch([tree], function (_a, _b) {
|
|
266
|
+
var _c = __read(_a, 1), branch = _c[0];
|
|
267
|
+
var _d = __read(_b, 1), path = _d[0];
|
|
268
|
+
return fn(branch, path);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* n-ary operation on a tree, branch by branch
|
|
273
|
+
* - branches are matched by index,
|
|
274
|
+
* - last branches are repeated when the branch count differs
|
|
275
|
+
* - branch names from the longest or first tree are used
|
|
276
|
+
* - if a corresponding index does not exist in the branch, its item argument is undefined
|
|
277
|
+
* - the callback function is called with (items, paths)
|
|
278
|
+
* @param trees
|
|
279
|
+
* @param fn
|
|
280
|
+
* @returns
|
|
281
|
+
*/
|
|
282
|
+
function nAryOnTreeBranch(trees, fn) {
|
|
283
|
+
var result = {};
|
|
284
|
+
var allKeys = trees.map(function (tree) { return Object.keys(tree); });
|
|
285
|
+
var maxBranches = Math.max.apply(Math, __spreadArray([], __read(allKeys.map(function (keys) { return keys.length; })), false));
|
|
286
|
+
var _loop_2 = function (i) {
|
|
287
|
+
var paths = allKeys.map(function (keys) { return keys[Math.min(i, keys.length - 1)]; });
|
|
288
|
+
var branches = trees.map(function (tree) { return tree[paths[trees.indexOf(tree)]] || []; });
|
|
289
|
+
// get all the paths of which the trees are the longest, use the first
|
|
290
|
+
var maxPaths = paths.filter(function (_, i) { return allKeys[i].length === maxBranches; });
|
|
291
|
+
var path = maxPaths[0];
|
|
292
|
+
var newBranch = fn(branches, paths);
|
|
293
|
+
if (newBranch === null || newBranch === void 0 ? void 0 : newBranch.length) {
|
|
294
|
+
result[path] = newBranch;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
// if (!trees.every((t) => Object.keys(t).length > 0)) {
|
|
298
|
+
// return result;
|
|
299
|
+
// }
|
|
300
|
+
for (var i = 0; i < maxBranches; i++) {
|
|
301
|
+
_loop_2(i);
|
|
302
|
+
}
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* n-ary operation on a tree, branch by branch
|
|
307
|
+
* - branches are matched by index,
|
|
308
|
+
* - last branches are repeated when the branch count differs
|
|
309
|
+
* - branch names from the longest or first tree are used
|
|
310
|
+
* - if a corresponding index does not exist in the branch, its item argument is undefined
|
|
311
|
+
* - the callback function is called with (items, paths, index)
|
|
312
|
+
* - return DISCARD to not add the item to the branch
|
|
313
|
+
* @param trees
|
|
314
|
+
* @param fn
|
|
315
|
+
* @returns
|
|
316
|
+
*/
|
|
317
|
+
function nAryOnTree(trees, fn, fill) {
|
|
318
|
+
return nAryOnTreeBranch(trees, function (branches, paths) {
|
|
319
|
+
var maxLength = Math.max.apply(Math, __spreadArray([], __read(branches.map(function (b) { return b.length; })), false));
|
|
320
|
+
if (maxLength === 0)
|
|
321
|
+
return; // Skip empty branches
|
|
322
|
+
var results = [];
|
|
323
|
+
var _loop_3 = function (i) {
|
|
324
|
+
var items = branches.map(function (b) {
|
|
325
|
+
if (fill) {
|
|
326
|
+
// repeat last item if index exceeds branch length
|
|
327
|
+
return b[Math.min(i, b.length - 1)];
|
|
328
|
+
}
|
|
329
|
+
return b[i];
|
|
330
|
+
});
|
|
331
|
+
var result = fn(items, paths, i);
|
|
332
|
+
if (result !== DISCARD) {
|
|
333
|
+
results = results.concat(result);
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
for (var i = 0; i < maxLength; i++) {
|
|
337
|
+
_loop_3(i);
|
|
338
|
+
}
|
|
339
|
+
return results;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* binary operation on a tree, branch by branch
|
|
344
|
+
* - branches are matched by index,
|
|
345
|
+
* - last branches are repeated when the branch count differs
|
|
346
|
+
* - branch names from the longest or first tree are used
|
|
347
|
+
* - if a corresponding index does not exist in the branch, its item argument is undefined
|
|
348
|
+
* - the callback function is called with (itemA, itemB, pathA, pathB)
|
|
349
|
+
* @param treeA
|
|
350
|
+
* @param treeB
|
|
351
|
+
* @param fn
|
|
352
|
+
* @returns
|
|
353
|
+
*/
|
|
354
|
+
function binaryOnTreeBranch(treeA, treeB, fn) {
|
|
355
|
+
return nAryOnTreeBranch([treeA, treeB], function (_a, _b) {
|
|
356
|
+
var _c = __read(_a, 2), branchA = _c[0], branchB = _c[1];
|
|
357
|
+
var _d = __read(_b, 2), pathA = _d[0], pathB = _d[1];
|
|
358
|
+
return fn(branchA, branchB, pathA, pathB);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* binary operation on a tree, branch by branch
|
|
363
|
+
* - branches are matched by index,
|
|
364
|
+
* - last branches are repeated when the branch count differs
|
|
365
|
+
* - branch names from the longest or first tree are used
|
|
366
|
+
* - if a corresponding index does not exist in the branch, its item argument is undefined
|
|
367
|
+
* - the callback function is called with (itemA, itemB, pathA, pathB, index)
|
|
368
|
+
* @param treeA
|
|
369
|
+
* @param treeB
|
|
370
|
+
* @param fn
|
|
371
|
+
* @returns
|
|
372
|
+
*/
|
|
373
|
+
function binaryOnTree(treeA, treeB, fn, fill) {
|
|
374
|
+
return nAryOnTree([treeA, treeB], function (_a, _b, index) {
|
|
375
|
+
var _c = __read(_a, 2), itemA = _c[0], itemB = _c[1];
|
|
376
|
+
var _d = __read(_b, 2), pathA = _d[0], pathB = _d[1];
|
|
377
|
+
return fn(itemA, itemB, pathA, pathB, index);
|
|
378
|
+
}, fill);
|
|
379
|
+
}
|
|
380
|
+
function treeSize(tree) {
|
|
381
|
+
return Object.keys(tree).length;
|
|
382
|
+
}
|
|
383
|
+
function sameShape(a, b) {
|
|
384
|
+
var sa = Object.keys(a).sort().join("|");
|
|
385
|
+
var sb = Object.keys(b).sort().join("|");
|
|
386
|
+
return sa === sb;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* flattens the whole tree into an array
|
|
390
|
+
*/
|
|
391
|
+
function toArray(a) {
|
|
392
|
+
return Object.values(a).flat();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// utilities
|
|
396
|
+
/**
|
|
397
|
+
* merges two trees by concatenating the branches with the same path
|
|
398
|
+
* @param a
|
|
399
|
+
* @param b
|
|
400
|
+
* @returns
|
|
401
|
+
*/
|
|
402
|
+
function mergeTrees(a, b) {
|
|
403
|
+
var e_1, _a;
|
|
404
|
+
var _b;
|
|
405
|
+
var out = __assign({}, a);
|
|
406
|
+
try {
|
|
407
|
+
for (var _c = __values(Object.entries(b)), _d = _c.next(); !_d.done; _d = _c.next()) {
|
|
408
|
+
var _e = __read(_d.value, 2), k = _e[0], arr = _e[1];
|
|
409
|
+
out[k] = ((_b = out[k]) !== null && _b !== void 0 ? _b : []).concat(arr);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
413
|
+
finally {
|
|
414
|
+
try {
|
|
415
|
+
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
416
|
+
}
|
|
417
|
+
finally { if (e_1) throw e_1.error; }
|
|
418
|
+
}
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function isReference(value) {
|
|
423
|
+
return (typeof value === "string" &&
|
|
424
|
+
value.startsWith("<") &&
|
|
425
|
+
value.endsWith(">"));
|
|
426
|
+
}
|
|
427
|
+
function parseReference(ref) {
|
|
428
|
+
return ref.slice(1, -1);
|
|
429
|
+
}
|
|
430
|
+
function uid$1() {
|
|
431
|
+
return Math.random().toString(36).substring(2, 15);
|
|
432
|
+
}
|
|
433
|
+
function getValue$1(obj, path) {
|
|
434
|
+
if (!(obj && path)) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
var parts = path.split(".");
|
|
438
|
+
var value = obj[parts.shift() || ""];
|
|
439
|
+
while (parts.length && value) {
|
|
440
|
+
value = value[parts.shift() || ""];
|
|
441
|
+
}
|
|
442
|
+
return value;
|
|
443
|
+
}
|
|
444
|
+
function interpolateValue(value, scope) {
|
|
445
|
+
if (Array.isArray(value)) {
|
|
446
|
+
if (!value.length)
|
|
447
|
+
return broadCast([]);
|
|
448
|
+
if (!value.every(isTree)) {
|
|
449
|
+
// interpolate to an array of trees
|
|
450
|
+
var mappedValue_1 = value.map(function (v) { return interpolateValue(v, scope); });
|
|
451
|
+
if (!mappedValue_1.every(isTree)) {
|
|
452
|
+
// this is a bit of a hack when the values were all plain values
|
|
453
|
+
// in that case, just return them, which will result in a single tree
|
|
454
|
+
return mappedValue_1;
|
|
455
|
+
}
|
|
456
|
+
// then, for each value, combine the branches with the same index
|
|
457
|
+
return nAryOnTreeBranch(mappedValue_1, function (branches) { return branches.flat(); });
|
|
458
|
+
}
|
|
459
|
+
else {
|
|
460
|
+
// combine the trees into one
|
|
461
|
+
var tree = value.reduce(function (acc, t) { return mergeTrees(acc, t); });
|
|
462
|
+
return mapTree(tree, function (v) { return interpolateValue(v, scope); });
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (isTree(value)) {
|
|
466
|
+
return mapTree(value, function (v) { return interpolateValue(v, scope); });
|
|
467
|
+
}
|
|
468
|
+
if (typeof value === "object" && value !== null) {
|
|
469
|
+
if (Object.keys(value).length === 0) {
|
|
470
|
+
return {};
|
|
471
|
+
}
|
|
472
|
+
// interpolate to a record of trees:
|
|
473
|
+
var interpolated = interpolate(value, scope);
|
|
474
|
+
// then, for each value, combine the branches with the same index
|
|
475
|
+
var keys_1 = Object.keys(interpolated);
|
|
476
|
+
return nAryOnTree(Object.values(interpolated), function (items) {
|
|
477
|
+
return Object.fromEntries(items.map(function (item, keyIndex) {
|
|
478
|
+
return [keys_1[keyIndex], item];
|
|
479
|
+
}));
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
if (!isReference(value)) {
|
|
483
|
+
return value;
|
|
484
|
+
}
|
|
485
|
+
var parts = parseReference(value).split(".");
|
|
486
|
+
var mappedValue = getValue$1(scope, parts.slice(0, 2).join("."));
|
|
487
|
+
if (parts.length > 2) {
|
|
488
|
+
// deep interpolate
|
|
489
|
+
mappedValue = mapTree(mappedValue, function (v) {
|
|
490
|
+
return getValue$1(v, parts.slice(2).join("."));
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
return mappedValue;
|
|
494
|
+
}
|
|
495
|
+
function interpolate(inputs, scope) {
|
|
496
|
+
return Object.fromEntries(Object.entries(inputs).map(function (_a) {
|
|
497
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
498
|
+
var mappedValue = interpolateValue(value, scope);
|
|
499
|
+
return [key, broadCast(mappedValue)];
|
|
500
|
+
}));
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* TODO: need rank polymorhism on the tree item sizes
|
|
505
|
+
*/
|
|
506
|
+
var getValue = {
|
|
507
|
+
name: "getValue",
|
|
508
|
+
label: "Get Value",
|
|
509
|
+
description: "Get a value from an object at a specified path.",
|
|
510
|
+
inputs: {
|
|
511
|
+
tree: "any",
|
|
512
|
+
path: "string",
|
|
513
|
+
},
|
|
514
|
+
outputs: {
|
|
515
|
+
tree: "any",
|
|
516
|
+
},
|
|
517
|
+
// TODO: may want to do something with other values of path
|
|
518
|
+
impl: function (inputs, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
519
|
+
var paths, tree;
|
|
520
|
+
return __generator(this, function (_a) {
|
|
521
|
+
paths = toArray(inputs.path || {});
|
|
522
|
+
tree = mapTree(inputs.tree, function (value) {
|
|
523
|
+
// get the property from the object
|
|
524
|
+
return getValue$1(value, paths[0]);
|
|
525
|
+
});
|
|
526
|
+
return [2 /*return*/, { tree: tree }];
|
|
527
|
+
});
|
|
528
|
+
}); },
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
var log = {
|
|
532
|
+
name: "log",
|
|
533
|
+
label: "Log",
|
|
534
|
+
description: "Logs the input data to the console",
|
|
535
|
+
nodeType: "Log",
|
|
536
|
+
inputs: {
|
|
537
|
+
data: "any",
|
|
538
|
+
},
|
|
539
|
+
outputs: {
|
|
540
|
+
data: "any", // Return the data for potential further use
|
|
541
|
+
},
|
|
542
|
+
impl: function (input) { return __awaiter(void 0, void 0, void 0, function () {
|
|
543
|
+
return __generator(this, function (_a) {
|
|
544
|
+
console.table(input.data);
|
|
545
|
+
return [2 /*return*/, {
|
|
546
|
+
data: input.data, // Return the data for potential further use
|
|
547
|
+
}];
|
|
548
|
+
});
|
|
549
|
+
}); },
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
/**
|
|
553
|
+
* value primitive function
|
|
554
|
+
*/
|
|
555
|
+
var value = {
|
|
556
|
+
name: "value",
|
|
557
|
+
label: "Value",
|
|
558
|
+
description: "Outputs a constant value of a specified type",
|
|
559
|
+
inputs: {},
|
|
560
|
+
params: {
|
|
561
|
+
value: "string",
|
|
562
|
+
type: {
|
|
563
|
+
type: "string",
|
|
564
|
+
default: "number",
|
|
565
|
+
options: ["number", "boolean", "string"],
|
|
566
|
+
},
|
|
567
|
+
},
|
|
568
|
+
outputs: {
|
|
569
|
+
value: "any",
|
|
570
|
+
},
|
|
571
|
+
impl: function (inputs, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
572
|
+
var value;
|
|
573
|
+
return __generator(this, function (_a) {
|
|
574
|
+
value = params.value;
|
|
575
|
+
switch (params.type) {
|
|
576
|
+
case "number":
|
|
577
|
+
return [2 /*return*/, { value: broadCast(Number(value)) }];
|
|
578
|
+
case "boolean":
|
|
579
|
+
return [2 /*return*/, { value: broadCast(Boolean(value)) }];
|
|
580
|
+
case "string":
|
|
581
|
+
return [2 /*return*/, { value: broadCast(String(value)) }];
|
|
582
|
+
default:
|
|
583
|
+
// return as is
|
|
584
|
+
return [2 /*return*/, { value: broadCast(value) }];
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
}); },
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
var _a$6;
|
|
591
|
+
var primitives$6 = (_a$6 = {},
|
|
592
|
+
_a$6[getValue.name] = getValue,
|
|
593
|
+
_a$6[log.name] = log,
|
|
594
|
+
_a$6[value.name] = value,
|
|
595
|
+
_a$6);
|
|
596
|
+
|
|
597
|
+
var add = {
|
|
598
|
+
name: "add",
|
|
599
|
+
label: "Add",
|
|
600
|
+
description: "Add two numbers",
|
|
601
|
+
inputs: {
|
|
602
|
+
a: { type: "number", default: 0 },
|
|
603
|
+
b: { type: "number", default: 0 },
|
|
604
|
+
},
|
|
605
|
+
outputs: {
|
|
606
|
+
sum: "number",
|
|
607
|
+
},
|
|
608
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
609
|
+
return __generator(this, function (_a) {
|
|
610
|
+
return [2 /*return*/, {
|
|
611
|
+
sum: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
612
|
+
return itemA + itemB;
|
|
613
|
+
}, true),
|
|
614
|
+
}];
|
|
615
|
+
});
|
|
616
|
+
}); },
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
var math$1 = create(all, {});
|
|
620
|
+
function evaluate(expression, scope) {
|
|
621
|
+
// console.log("evaluating", expression, "in", scope);
|
|
622
|
+
var result;
|
|
623
|
+
try {
|
|
624
|
+
result = math$1.evaluate(expression, scope);
|
|
625
|
+
if (typeof result.toNumber === "function") {
|
|
626
|
+
result = result.toNumber();
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
catch (e) {
|
|
630
|
+
console.log("Calc:", e);
|
|
631
|
+
console.log("Calc:", { expression: expression, scope: scope });
|
|
632
|
+
// todo: communicate errors
|
|
633
|
+
}
|
|
634
|
+
// console.log("result", result);
|
|
635
|
+
return result;
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* TODO: need rank polymorhism on the tree item sizes
|
|
639
|
+
*/
|
|
640
|
+
var calc = {
|
|
641
|
+
name: "calc",
|
|
642
|
+
label: "Calc",
|
|
643
|
+
nodeType: "Calc",
|
|
644
|
+
inputs: {},
|
|
645
|
+
params: {
|
|
646
|
+
expression: "string",
|
|
647
|
+
},
|
|
648
|
+
outputs: {
|
|
649
|
+
result: "number",
|
|
650
|
+
},
|
|
651
|
+
impl: function (inputs, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
652
|
+
var expression, inputEntries, result;
|
|
653
|
+
return __generator(this, function (_a) {
|
|
654
|
+
expression = params.expression;
|
|
655
|
+
if (!expression)
|
|
656
|
+
return [2 /*return*/, {}];
|
|
657
|
+
inputEntries = Object.entries(inputs);
|
|
658
|
+
if (!inputEntries.every(function (_a, i, a) {
|
|
659
|
+
var _b = __read(_a, 2); _b[0]; var v = _b[1];
|
|
660
|
+
return sameShape(v, a[0][1]);
|
|
661
|
+
})) {
|
|
662
|
+
throw new Error("All inputs must have the same shape");
|
|
663
|
+
}
|
|
664
|
+
// if no inputs, just return the result of the expression
|
|
665
|
+
if (inputEntries.length === 0) {
|
|
666
|
+
return [2 /*return*/, { result: broadCast(evaluate(expression, {})) }];
|
|
667
|
+
}
|
|
668
|
+
result = mapTree(inputEntries[0][1], function (_, p, i) {
|
|
669
|
+
// create a scope for each input
|
|
670
|
+
var vars = {};
|
|
671
|
+
inputEntries.forEach(function (_a) {
|
|
672
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
673
|
+
vars[key] = value[p][i];
|
|
674
|
+
});
|
|
675
|
+
return evaluate(expression, vars);
|
|
676
|
+
});
|
|
677
|
+
// await new Promise((resolve) => setTimeout(resolve, 5000)); // yield to event loop
|
|
678
|
+
return [2 /*return*/, { result: result }];
|
|
679
|
+
});
|
|
680
|
+
}); },
|
|
681
|
+
};
|
|
682
|
+
var BUILT_INS = new Set([
|
|
683
|
+
// constants
|
|
684
|
+
"pi",
|
|
685
|
+
"e",
|
|
686
|
+
"tau",
|
|
687
|
+
"phi",
|
|
688
|
+
"Infinity",
|
|
689
|
+
"NaN",
|
|
690
|
+
"i",
|
|
691
|
+
"true",
|
|
692
|
+
"false",
|
|
693
|
+
"null",
|
|
694
|
+
// common built-in functions (skip as variables when used as callees)
|
|
695
|
+
"sin",
|
|
696
|
+
"cos",
|
|
697
|
+
"tan",
|
|
698
|
+
"asin",
|
|
699
|
+
"acos",
|
|
700
|
+
"atan",
|
|
701
|
+
"atan2",
|
|
702
|
+
"sinh",
|
|
703
|
+
"cosh",
|
|
704
|
+
"tanh",
|
|
705
|
+
"log",
|
|
706
|
+
"log10",
|
|
707
|
+
"ln",
|
|
708
|
+
"exp",
|
|
709
|
+
"sqrt",
|
|
710
|
+
"abs",
|
|
711
|
+
"min",
|
|
712
|
+
"max",
|
|
713
|
+
"round",
|
|
714
|
+
"floor",
|
|
715
|
+
"ceil",
|
|
716
|
+
"sum",
|
|
717
|
+
"mean",
|
|
718
|
+
"median",
|
|
719
|
+
"mode",
|
|
720
|
+
"std",
|
|
721
|
+
"var",
|
|
722
|
+
"pow",
|
|
723
|
+
"mod",
|
|
724
|
+
"gcd",
|
|
725
|
+
"lcm",
|
|
726
|
+
"derivative",
|
|
727
|
+
"integral",
|
|
728
|
+
"simplify",
|
|
729
|
+
"im",
|
|
730
|
+
"re",
|
|
731
|
+
]);
|
|
732
|
+
/**
|
|
733
|
+
* Return a sorted list of unbound symbol names in a MathJS expression.
|
|
734
|
+
* @param expr The expression string, e.g. "x + y"
|
|
735
|
+
* @param scope Names already defined/known (e.g. from your evaluation scope)
|
|
736
|
+
* @param extraBound Extra names to treat as bound (e.g. UI reserved words)
|
|
737
|
+
*/
|
|
738
|
+
function getUnboundSymbols(expr, scope, extraBound) {
|
|
739
|
+
if (scope === void 0) { scope = {}; }
|
|
740
|
+
if (extraBound === void 0) { extraBound = []; }
|
|
741
|
+
var ast;
|
|
742
|
+
try {
|
|
743
|
+
ast = math$1.parse(expr);
|
|
744
|
+
}
|
|
745
|
+
catch (_a) {
|
|
746
|
+
return [];
|
|
747
|
+
}
|
|
748
|
+
var bound = new Set(__spreadArray(__spreadArray([], __read(Object.keys(scope)), false), __read(extraBound), false));
|
|
749
|
+
var free = new Set();
|
|
750
|
+
var isFunctionCallee = function (node, parent) {
|
|
751
|
+
return (parent === null || parent === void 0 ? void 0 : parent.type) === "FunctionNode" && parent.fn === node;
|
|
752
|
+
};
|
|
753
|
+
ast.traverse(function (node, _path, parent) {
|
|
754
|
+
if (node.type !== "SymbolNode")
|
|
755
|
+
return;
|
|
756
|
+
var name = node.name;
|
|
757
|
+
// Skip if symbol is the function name in a call, e.g. sin in sin(x)
|
|
758
|
+
if (isFunctionCallee(node, parent))
|
|
759
|
+
return;
|
|
760
|
+
// Skip mathjs built-ins and anything caller said is already bound
|
|
761
|
+
if (BUILT_INS.has(name) || bound.has(name))
|
|
762
|
+
return;
|
|
763
|
+
free.add(name);
|
|
764
|
+
});
|
|
765
|
+
// If you want to also exclude left-hand sides of assignments within the expr:
|
|
766
|
+
// (Uncommon in "fill in variables" UIs, but easy to guard against.)
|
|
767
|
+
ast.traverse(function (node) {
|
|
768
|
+
if (node.type === "AssignmentNode") {
|
|
769
|
+
var target = node.object;
|
|
770
|
+
if ((target === null || target === void 0 ? void 0 : target.type) === "SymbolNode") {
|
|
771
|
+
free.delete(target.name);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
});
|
|
775
|
+
return Array.from(free).sort();
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
var divide = {
|
|
779
|
+
name: "divide",
|
|
780
|
+
label: "Divide",
|
|
781
|
+
description: "Divide two numbers",
|
|
782
|
+
inputs: {
|
|
783
|
+
a: { type: "number", default: 1 },
|
|
784
|
+
b: { type: "number", default: 1 },
|
|
785
|
+
},
|
|
786
|
+
outputs: {
|
|
787
|
+
quotient: "number",
|
|
788
|
+
},
|
|
789
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
790
|
+
return __generator(this, function (_a) {
|
|
791
|
+
return [2 /*return*/, {
|
|
792
|
+
quotient: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
793
|
+
return itemA / itemB;
|
|
794
|
+
}, true),
|
|
795
|
+
}];
|
|
796
|
+
});
|
|
797
|
+
}); },
|
|
798
|
+
};
|
|
799
|
+
|
|
800
|
+
var greaterThan = {
|
|
801
|
+
name: "greaterThan",
|
|
802
|
+
label: "Greater Than",
|
|
803
|
+
description: "Compare on greater than or greater than or equal to",
|
|
804
|
+
inputs: {
|
|
805
|
+
a: "number",
|
|
806
|
+
b: "number",
|
|
807
|
+
},
|
|
808
|
+
outputs: {
|
|
809
|
+
gt: "boolean",
|
|
810
|
+
gte: "boolean",
|
|
811
|
+
},
|
|
812
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
813
|
+
return __generator(this, function (_a) {
|
|
814
|
+
return [2 /*return*/, {
|
|
815
|
+
gt: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
816
|
+
return itemA > itemB;
|
|
817
|
+
}, true),
|
|
818
|
+
gte: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
819
|
+
return itemA >= itemB;
|
|
820
|
+
}, true),
|
|
821
|
+
}];
|
|
822
|
+
});
|
|
823
|
+
}); },
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
var lessThan = {
|
|
827
|
+
name: "lessThan",
|
|
828
|
+
label: "Less Than",
|
|
829
|
+
description: "Compare on less than or less than or equal to",
|
|
830
|
+
inputs: {
|
|
831
|
+
a: "number",
|
|
832
|
+
b: "number",
|
|
833
|
+
},
|
|
834
|
+
outputs: {
|
|
835
|
+
lt: "boolean",
|
|
836
|
+
lte: "boolean",
|
|
837
|
+
},
|
|
838
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
839
|
+
return __generator(this, function (_a) {
|
|
840
|
+
return [2 /*return*/, {
|
|
841
|
+
lt: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
842
|
+
return itemA < itemB;
|
|
843
|
+
}, true),
|
|
844
|
+
lte: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
845
|
+
return itemA <= itemB;
|
|
846
|
+
}, true),
|
|
847
|
+
}];
|
|
848
|
+
});
|
|
849
|
+
}); },
|
|
850
|
+
};
|
|
851
|
+
|
|
852
|
+
var multiply = {
|
|
853
|
+
name: "multiply",
|
|
854
|
+
label: "Multiply",
|
|
855
|
+
description: "Multiply two numbers",
|
|
856
|
+
inputs: {
|
|
857
|
+
a: { type: "number", default: 1 },
|
|
858
|
+
b: { type: "number", default: 1 },
|
|
859
|
+
},
|
|
860
|
+
outputs: {
|
|
861
|
+
product: "number",
|
|
862
|
+
},
|
|
863
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
864
|
+
return __generator(this, function (_a) {
|
|
865
|
+
return [2 /*return*/, {
|
|
866
|
+
product: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
867
|
+
return itemA * itemB;
|
|
868
|
+
}, true),
|
|
869
|
+
}];
|
|
870
|
+
});
|
|
871
|
+
}); },
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
var subtract = {
|
|
875
|
+
name: "subtract",
|
|
876
|
+
label: "Subtract",
|
|
877
|
+
description: "Subtract two numbers",
|
|
878
|
+
inputs: {
|
|
879
|
+
a: { type: "number", default: 0 },
|
|
880
|
+
b: { type: "number", default: 0 },
|
|
881
|
+
},
|
|
882
|
+
outputs: {
|
|
883
|
+
difference: "number",
|
|
884
|
+
},
|
|
885
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
886
|
+
return __generator(this, function (_a) {
|
|
887
|
+
return [2 /*return*/, {
|
|
888
|
+
difference: binaryOnTree(inputs.a, inputs.b, function (itemA, itemB) {
|
|
889
|
+
return itemA - itemB;
|
|
890
|
+
}, true),
|
|
891
|
+
}];
|
|
892
|
+
});
|
|
893
|
+
}); },
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
var _a$5;
|
|
897
|
+
var primitives$5 = (_a$5 = {},
|
|
898
|
+
_a$5[calc.name] = calc,
|
|
899
|
+
_a$5[lessThan.name] = lessThan,
|
|
900
|
+
_a$5[greaterThan.name] = greaterThan,
|
|
901
|
+
_a$5[add.name] = add,
|
|
902
|
+
_a$5[subtract.name] = subtract,
|
|
903
|
+
_a$5[multiply.name] = multiply,
|
|
904
|
+
_a$5[divide.name] = divide,
|
|
905
|
+
_a$5);
|
|
906
|
+
|
|
907
|
+
var filterList = {
|
|
908
|
+
name: "filterList",
|
|
909
|
+
label: "Filter List",
|
|
910
|
+
description: "Filters the list by a boolean value",
|
|
911
|
+
inputs: {
|
|
912
|
+
list: "any",
|
|
913
|
+
keep: "boolean",
|
|
914
|
+
},
|
|
915
|
+
outputs: {
|
|
916
|
+
list: "any",
|
|
917
|
+
},
|
|
918
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
919
|
+
return __generator(this, function (_a) {
|
|
920
|
+
return [2 /*return*/, {
|
|
921
|
+
list: binaryOnTreeBranch(inputs.list || {}, inputs.keep || {}, function (branchA, branchB) {
|
|
922
|
+
return branchA.filter(function (item, index) {
|
|
923
|
+
return (branchB[Math.min(index, branchB.length - 1)] ===
|
|
924
|
+
true);
|
|
925
|
+
});
|
|
926
|
+
}),
|
|
927
|
+
}];
|
|
928
|
+
});
|
|
929
|
+
}); },
|
|
930
|
+
};
|
|
931
|
+
|
|
932
|
+
var splitGroup = {
|
|
933
|
+
name: "splitGroup",
|
|
934
|
+
label: "Split Group",
|
|
935
|
+
description: "Splits every item of a group into its own group",
|
|
936
|
+
inputs: {
|
|
937
|
+
tree: "any",
|
|
938
|
+
},
|
|
939
|
+
outputs: {
|
|
940
|
+
tree: "any", // Return the grafted nodes
|
|
941
|
+
},
|
|
942
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
943
|
+
return __generator(this, function (_a) {
|
|
944
|
+
return [2 /*return*/, {
|
|
945
|
+
tree: graftTree(inputs.tree || {}),
|
|
946
|
+
}];
|
|
947
|
+
});
|
|
948
|
+
}); },
|
|
949
|
+
};
|
|
950
|
+
|
|
951
|
+
var listItem = {
|
|
952
|
+
name: "listItem",
|
|
953
|
+
label: "List Item",
|
|
954
|
+
description: "Retrieve the items from the list at the specified indices",
|
|
955
|
+
inputs: {
|
|
956
|
+
list: "any",
|
|
957
|
+
index: {
|
|
958
|
+
type: "number",
|
|
959
|
+
default: 0,
|
|
960
|
+
},
|
|
961
|
+
},
|
|
962
|
+
outputs: {
|
|
963
|
+
item: "any",
|
|
964
|
+
},
|
|
965
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
966
|
+
return __generator(this, function (_a) {
|
|
967
|
+
return [2 /*return*/, {
|
|
968
|
+
// item: mapTreeBranch(inputs.list, (branch, path) => {
|
|
969
|
+
// let indices = getBranch(inputs.index, path);
|
|
970
|
+
// if (indices) {
|
|
971
|
+
// return indices.map((i) => branch[i]);
|
|
972
|
+
// }
|
|
973
|
+
// }),
|
|
974
|
+
item: binaryOnTreeBranch(inputs.list || {}, inputs.index || {}, function (branch, indices) {
|
|
975
|
+
if (indices) {
|
|
976
|
+
return indices.map(function (i) { return branch[i]; });
|
|
977
|
+
}
|
|
978
|
+
}),
|
|
979
|
+
}];
|
|
980
|
+
});
|
|
981
|
+
}); },
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
var listLength = {
|
|
985
|
+
name: "listLength",
|
|
986
|
+
label: "List Length",
|
|
987
|
+
description: "Get the length of a list",
|
|
988
|
+
inputs: {
|
|
989
|
+
list: "any",
|
|
990
|
+
},
|
|
991
|
+
outputs: {
|
|
992
|
+
length: "number",
|
|
993
|
+
},
|
|
994
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
995
|
+
return __generator(this, function (_a) {
|
|
996
|
+
return [2 /*return*/, {
|
|
997
|
+
length: mapTreeBranch(inputs.list || {}, function (branch) {
|
|
998
|
+
return [branch.length];
|
|
999
|
+
}),
|
|
1000
|
+
}];
|
|
1001
|
+
});
|
|
1002
|
+
}); },
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
var mergeGroup = {
|
|
1006
|
+
name: "mergeGroup",
|
|
1007
|
+
label: "Merge Group",
|
|
1008
|
+
description: "Merge the deepest groups into their parent.",
|
|
1009
|
+
inputs: {
|
|
1010
|
+
tree: "any",
|
|
1011
|
+
},
|
|
1012
|
+
params: {
|
|
1013
|
+
depth: { type: "number", default: 1 }, // Optional depth to trim the tree
|
|
1014
|
+
},
|
|
1015
|
+
outputs: {
|
|
1016
|
+
tree: "any", // Return the trimed nodes
|
|
1017
|
+
},
|
|
1018
|
+
impl: function (inputs, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1019
|
+
var depth;
|
|
1020
|
+
var _a;
|
|
1021
|
+
return __generator(this, function (_b) {
|
|
1022
|
+
depth = (_a = params.depth) !== null && _a !== void 0 ? _a : 1;
|
|
1023
|
+
return [2 /*return*/, {
|
|
1024
|
+
tree: trimTree(inputs.tree, depth),
|
|
1025
|
+
}];
|
|
1026
|
+
});
|
|
1027
|
+
}); },
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
var shiftList = {
|
|
1031
|
+
name: "shiftList",
|
|
1032
|
+
label: "Shift List",
|
|
1033
|
+
description: "Shifts the list to the left, removing the first element and adding it at the end if wrap is enabled",
|
|
1034
|
+
inputs: {
|
|
1035
|
+
list: "any",
|
|
1036
|
+
step: { type: "number", default: 1, step: 1 },
|
|
1037
|
+
wrap: "boolean",
|
|
1038
|
+
},
|
|
1039
|
+
outputs: {
|
|
1040
|
+
list: "any",
|
|
1041
|
+
},
|
|
1042
|
+
impl: function (inputs, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1043
|
+
return __generator(this, function (_a) {
|
|
1044
|
+
return [2 /*return*/, {
|
|
1045
|
+
list: nAryOnTreeBranch([inputs.list || {}, inputs.step || {}, inputs.wrap || {}], function (_a) {
|
|
1046
|
+
var _b;
|
|
1047
|
+
var _c = __read(_a, 3), list = _c[0], step = _c[1], wrap = _c[2];
|
|
1048
|
+
var n = (_b = step[0]) !== null && _b !== void 0 ? _b : 1;
|
|
1049
|
+
if (wrap[0]) {
|
|
1050
|
+
n = n % (list.length || 1);
|
|
1051
|
+
}
|
|
1052
|
+
var shifted = list.slice(n);
|
|
1053
|
+
if (wrap[0]) {
|
|
1054
|
+
shifted.push.apply(shifted, __spreadArray([], __read(list.slice(0, n)), false));
|
|
1055
|
+
}
|
|
1056
|
+
return shifted;
|
|
1057
|
+
}),
|
|
1058
|
+
}];
|
|
1059
|
+
});
|
|
1060
|
+
}); },
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
var _a$4;
|
|
1064
|
+
var primitives$4 = (_a$4 = {},
|
|
1065
|
+
_a$4[splitGroup.name] = splitGroup,
|
|
1066
|
+
_a$4[mergeGroup.name] = mergeGroup,
|
|
1067
|
+
_a$4[listItem.name] = listItem,
|
|
1068
|
+
_a$4[listLength.name] = listLength,
|
|
1069
|
+
_a$4[filterList.name] = filterList,
|
|
1070
|
+
_a$4[shiftList.name] = shiftList,
|
|
1071
|
+
_a$4);
|
|
1072
|
+
|
|
1073
|
+
var series = {
|
|
1074
|
+
name: "series",
|
|
1075
|
+
label: "Series",
|
|
1076
|
+
description: "Create a series of numbers.",
|
|
1077
|
+
inputs: {
|
|
1078
|
+
start: { type: "number", default: 0 },
|
|
1079
|
+
step: { type: "number", default: 1 },
|
|
1080
|
+
count: { type: "number", default: 10 },
|
|
1081
|
+
},
|
|
1082
|
+
outputs: {
|
|
1083
|
+
series: "number",
|
|
1084
|
+
},
|
|
1085
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1086
|
+
return __generator(this, function (_a) {
|
|
1087
|
+
return [2 /*return*/, {
|
|
1088
|
+
series: nAryOnTreeBranch([
|
|
1089
|
+
graftTree(inputs.start),
|
|
1090
|
+
graftTree(inputs.step),
|
|
1091
|
+
graftTree(inputs.count),
|
|
1092
|
+
], function (_a) {
|
|
1093
|
+
var _b = __read(_a, 3), _c = __read(_b[0], 1), _d = _c[0], start = _d === void 0 ? 0 : _d, _e = __read(_b[1], 1), _f = _e[0], step = _f === void 0 ? 1 : _f, _g = __read(_b[2], 1), _h = _g[0], count = _h === void 0 ? 10 : _h;
|
|
1094
|
+
var series = [];
|
|
1095
|
+
for (var i = 0; i < count; i++) {
|
|
1096
|
+
series.push(start + i * step);
|
|
1097
|
+
}
|
|
1098
|
+
return series;
|
|
1099
|
+
}),
|
|
1100
|
+
}];
|
|
1101
|
+
});
|
|
1102
|
+
}); },
|
|
1103
|
+
};
|
|
1104
|
+
|
|
1105
|
+
var _a$3;
|
|
1106
|
+
var primitives$3 = (_a$3 = {},
|
|
1107
|
+
_a$3[series.name] = series,
|
|
1108
|
+
_a$3);
|
|
1109
|
+
|
|
1110
|
+
var mergeTree = {
|
|
1111
|
+
name: "mergeTree",
|
|
1112
|
+
label: "Merge Tree",
|
|
1113
|
+
description: "Merge multiple trees into a single tree.",
|
|
1114
|
+
nodeType: "Merge",
|
|
1115
|
+
inputs: {},
|
|
1116
|
+
outputs: {
|
|
1117
|
+
result: "any",
|
|
1118
|
+
},
|
|
1119
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1120
|
+
var result;
|
|
1121
|
+
return __generator(this, function (_a) {
|
|
1122
|
+
result = {};
|
|
1123
|
+
Object.values(inputs).forEach(function (input) {
|
|
1124
|
+
Object.entries(input).forEach(function (_a) {
|
|
1125
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
1126
|
+
if (!result[key]) {
|
|
1127
|
+
result[key] = value;
|
|
1128
|
+
}
|
|
1129
|
+
else {
|
|
1130
|
+
result[key] = __spreadArray(__spreadArray([], __read(result[key]), false), __read(value), false);
|
|
1131
|
+
}
|
|
1132
|
+
});
|
|
1133
|
+
});
|
|
1134
|
+
return [2 /*return*/, { result: result }];
|
|
1135
|
+
});
|
|
1136
|
+
}); },
|
|
1137
|
+
};
|
|
1138
|
+
|
|
1139
|
+
var _a$2;
|
|
1140
|
+
var primitives$2 = (_a$2 = {},
|
|
1141
|
+
_a$2[mergeTree.name] = mergeTree,
|
|
1142
|
+
_a$2);
|
|
1143
|
+
|
|
1144
|
+
var groupAnd = {
|
|
1145
|
+
name: "groupAnd",
|
|
1146
|
+
label: "Group and",
|
|
1147
|
+
description: "Returns true if all of the items in a group are true",
|
|
1148
|
+
inputs: {
|
|
1149
|
+
tree: "boolean",
|
|
1150
|
+
},
|
|
1151
|
+
outputs: {
|
|
1152
|
+
result: "boolean",
|
|
1153
|
+
},
|
|
1154
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1155
|
+
return __generator(this, function (_a) {
|
|
1156
|
+
return [2 /*return*/, {
|
|
1157
|
+
result: mapTreeBranch(inputs.tree, function (branch) {
|
|
1158
|
+
return [branch.reduce(function (acc, val) { return acc && val; }, true)];
|
|
1159
|
+
}),
|
|
1160
|
+
}];
|
|
1161
|
+
});
|
|
1162
|
+
}); },
|
|
1163
|
+
};
|
|
1164
|
+
|
|
1165
|
+
var groupOr = {
|
|
1166
|
+
name: "groupOr",
|
|
1167
|
+
label: "Group or",
|
|
1168
|
+
description: "Returns true if any of the items in a group are true",
|
|
1169
|
+
inputs: {
|
|
1170
|
+
tree: "boolean",
|
|
1171
|
+
},
|
|
1172
|
+
outputs: {
|
|
1173
|
+
result: "boolean",
|
|
1174
|
+
},
|
|
1175
|
+
impl: function (inputs) { return __awaiter(void 0, void 0, void 0, function () {
|
|
1176
|
+
return __generator(this, function (_a) {
|
|
1177
|
+
return [2 /*return*/, {
|
|
1178
|
+
result: mapTreeBranch(inputs.tree, function (branch) {
|
|
1179
|
+
return [branch.reduce(function (acc, val) { return acc || val; }, false)];
|
|
1180
|
+
}),
|
|
1181
|
+
}];
|
|
1182
|
+
});
|
|
1183
|
+
}); },
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
var _a$1;
|
|
1187
|
+
var primitives$1 = (_a$1 = {},
|
|
1188
|
+
_a$1[groupAnd.name] = groupAnd,
|
|
1189
|
+
_a$1[groupOr.name] = groupOr,
|
|
1190
|
+
_a$1);
|
|
1191
|
+
|
|
1192
|
+
var Lib = { Util: primitives$6, Math: primitives$5, List: primitives$4, Sequence: primitives$3, Tree: primitives$2, Logic: primitives$1 };
|
|
1193
|
+
var primitives = __assign(__assign(__assign(__assign(__assign(__assign({}, primitives$6), primitives$5), primitives$4), primitives$3), primitives$2), primitives$1);
|
|
1194
|
+
|
|
1195
|
+
function isPrimitive(node) {
|
|
1196
|
+
return node.impl !== undefined;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
function normalizeVarDef(varDef) {
|
|
1200
|
+
if (typeof varDef === "string") {
|
|
1201
|
+
return {
|
|
1202
|
+
type: varDef,
|
|
1203
|
+
};
|
|
1204
|
+
}
|
|
1205
|
+
return varDef;
|
|
1206
|
+
}
|
|
1207
|
+
function getReferences(inputs) {
|
|
1208
|
+
var references = [];
|
|
1209
|
+
Object.entries(inputs).forEach(function (_a) {
|
|
1210
|
+
var _b = __read(_a, 2), name = _b[0], value = _b[1];
|
|
1211
|
+
var values = Array.isArray(value) ? value : [value];
|
|
1212
|
+
values.forEach(function (value) {
|
|
1213
|
+
if (!isReference(value)) {
|
|
1214
|
+
return;
|
|
1215
|
+
}
|
|
1216
|
+
var path = parseReference(value);
|
|
1217
|
+
var parts = path.split(".");
|
|
1218
|
+
references.push({
|
|
1219
|
+
name: name,
|
|
1220
|
+
path: path,
|
|
1221
|
+
parts: parts,
|
|
1222
|
+
refNode: parts[0],
|
|
1223
|
+
refField: parts.slice(1).join("."),
|
|
1224
|
+
});
|
|
1225
|
+
});
|
|
1226
|
+
});
|
|
1227
|
+
return references;
|
|
1228
|
+
}
|
|
1229
|
+
function topSort(fn) {
|
|
1230
|
+
var ordered = [];
|
|
1231
|
+
var visiting = new Set();
|
|
1232
|
+
var done = new Set();
|
|
1233
|
+
var visit = function (id) {
|
|
1234
|
+
// if already done, return
|
|
1235
|
+
if (done.has(id))
|
|
1236
|
+
return;
|
|
1237
|
+
// if being visited, we have a loop
|
|
1238
|
+
if (visiting.has(id)) {
|
|
1239
|
+
throw new Error("Circular dependency detected: ".concat(id));
|
|
1240
|
+
}
|
|
1241
|
+
// mark as visiting
|
|
1242
|
+
visiting.add(id);
|
|
1243
|
+
var fnCall = fn.body[id];
|
|
1244
|
+
if (!fnCall) {
|
|
1245
|
+
console.warn("Function ".concat(id, " not found in body"), fn);
|
|
1246
|
+
}
|
|
1247
|
+
// check the inputs and visit them
|
|
1248
|
+
Object.values(fnCall.inputs || {}).forEach(function (input) {
|
|
1249
|
+
if (isReference(input)) {
|
|
1250
|
+
var _a = __read(parseReference(input).split("."), 1), id_1 = _a[0];
|
|
1251
|
+
// visit inputs
|
|
1252
|
+
if (id_1 !== "inputs") {
|
|
1253
|
+
visit(id_1);
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
});
|
|
1257
|
+
// push to end of the list
|
|
1258
|
+
ordered.push(id);
|
|
1259
|
+
// finish visting
|
|
1260
|
+
visiting.delete(id);
|
|
1261
|
+
// mark as done
|
|
1262
|
+
done.add(id);
|
|
1263
|
+
};
|
|
1264
|
+
Object.keys(fn.body).forEach(visit);
|
|
1265
|
+
var orderedBody = Object.fromEntries(ordered.map(function (id) { return [id, fn.body[id]]; }));
|
|
1266
|
+
return __assign(__assign({}, fn), { body: orderedBody });
|
|
1267
|
+
}
|
|
1268
|
+
var Engine = /** @class */ (function () {
|
|
1269
|
+
function Engine(morePrimitives) {
|
|
1270
|
+
if (morePrimitives === void 0) { morePrimitives = {}; }
|
|
1271
|
+
this.listeners = {};
|
|
1272
|
+
this.cache = {};
|
|
1273
|
+
this.cacheMiss = Symbol("cacheMiss");
|
|
1274
|
+
this.fnIndex = __assign(__assign({}, primitives), morePrimitives);
|
|
1275
|
+
}
|
|
1276
|
+
Engine.prototype.checkCache = function (fnName, inputs) {
|
|
1277
|
+
if (!this.cache[fnName]) {
|
|
1278
|
+
return this.cacheMiss;
|
|
1279
|
+
}
|
|
1280
|
+
// TODO: need to check the interpolated inputs, as the references can be the same
|
|
1281
|
+
// however, the interpolation does create new objects
|
|
1282
|
+
return {};
|
|
1283
|
+
};
|
|
1284
|
+
Engine.prototype.runGraph = function (node, inputs, params) {
|
|
1285
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1286
|
+
var _a, body, executionId, context, startTime, _b, _c, _d, _i, name_1, fnCall, fn, inputs_1, params_1, result_1, cacheResult, startTime_1, endTime_1, result, endTime;
|
|
1287
|
+
return __generator(this, function (_e) {
|
|
1288
|
+
switch (_e.label) {
|
|
1289
|
+
case 0:
|
|
1290
|
+
_a = node.body, body = _a === void 0 ? {} : _a;
|
|
1291
|
+
executionId = uid$1();
|
|
1292
|
+
context = {
|
|
1293
|
+
scope: { inputs: inputs },
|
|
1294
|
+
timings: {},
|
|
1295
|
+
activations: {},
|
|
1296
|
+
};
|
|
1297
|
+
startTime = performance.now();
|
|
1298
|
+
_b = body;
|
|
1299
|
+
_c = [];
|
|
1300
|
+
for (_d in _b)
|
|
1301
|
+
_c.push(_d);
|
|
1302
|
+
_i = 0;
|
|
1303
|
+
_e.label = 1;
|
|
1304
|
+
case 1:
|
|
1305
|
+
if (!(_i < _c.length)) return [3 /*break*/, 5];
|
|
1306
|
+
_d = _c[_i];
|
|
1307
|
+
if (!(_d in _b)) return [3 /*break*/, 4];
|
|
1308
|
+
name_1 = _d;
|
|
1309
|
+
fnCall = body[name_1];
|
|
1310
|
+
fn = this.fnIndex[fnCall.name];
|
|
1311
|
+
if (!fn) {
|
|
1312
|
+
throw new Error("function ".concat(fnCall.name, " not found in index"));
|
|
1313
|
+
}
|
|
1314
|
+
inputs_1 = interpolate(fnCall.inputs || {}, context.scope);
|
|
1315
|
+
params_1 = fnCall.params || {};
|
|
1316
|
+
result_1 = {};
|
|
1317
|
+
cacheResult = this.checkCache(name_1, inputs_1);
|
|
1318
|
+
if (!(cacheResult !== this.cacheMiss)) return [3 /*break*/, 2];
|
|
1319
|
+
// use as result
|
|
1320
|
+
result_1 = cacheResult;
|
|
1321
|
+
// store the result in the scope
|
|
1322
|
+
context.scope[name_1] = result_1;
|
|
1323
|
+
return [3 /*break*/, 4];
|
|
1324
|
+
case 2:
|
|
1325
|
+
// cache miss, call the function implementation
|
|
1326
|
+
context.activations[name_1] = true;
|
|
1327
|
+
startTime_1 = performance.now();
|
|
1328
|
+
this.fireEvent("functionCall", {
|
|
1329
|
+
name: name_1,
|
|
1330
|
+
inputs: inputs_1,
|
|
1331
|
+
params: params_1,
|
|
1332
|
+
executionId: executionId,
|
|
1333
|
+
context: context,
|
|
1334
|
+
startTime: startTime_1,
|
|
1335
|
+
});
|
|
1336
|
+
return [4 /*yield*/, fn.impl(inputs_1, params_1)];
|
|
1337
|
+
case 3:
|
|
1338
|
+
result_1 = _e.sent();
|
|
1339
|
+
endTime_1 = performance.now();
|
|
1340
|
+
context.activations[name_1] = false;
|
|
1341
|
+
context.timings[name_1] = endTime_1 - startTime_1;
|
|
1342
|
+
// store the result in the scope
|
|
1343
|
+
context.scope[name_1] = result_1;
|
|
1344
|
+
this.fireEvent("functionResult", {
|
|
1345
|
+
name: name_1,
|
|
1346
|
+
inputs: inputs_1,
|
|
1347
|
+
params: params_1,
|
|
1348
|
+
executionId: executionId,
|
|
1349
|
+
context: context,
|
|
1350
|
+
result: result_1,
|
|
1351
|
+
startTime: startTime_1,
|
|
1352
|
+
endTime: endTime_1,
|
|
1353
|
+
duration: endTime_1 - startTime_1,
|
|
1354
|
+
});
|
|
1355
|
+
_e.label = 4;
|
|
1356
|
+
case 4:
|
|
1357
|
+
_i++;
|
|
1358
|
+
return [3 /*break*/, 1];
|
|
1359
|
+
case 5:
|
|
1360
|
+
result = interpolate(node.outputs || {}, context.scope);
|
|
1361
|
+
endTime = performance.now();
|
|
1362
|
+
// fire event
|
|
1363
|
+
this.fireEvent("result", {
|
|
1364
|
+
name: node.name,
|
|
1365
|
+
inputs: inputs,
|
|
1366
|
+
params: params,
|
|
1367
|
+
executionId: executionId,
|
|
1368
|
+
context: context,
|
|
1369
|
+
result: result,
|
|
1370
|
+
startTime: startTime,
|
|
1371
|
+
endTime: endTime,
|
|
1372
|
+
duration: endTime - startTime,
|
|
1373
|
+
});
|
|
1374
|
+
// console.log("Engine run done", result, context, endTime - startTime);
|
|
1375
|
+
return [2 /*return*/, result];
|
|
1376
|
+
}
|
|
1377
|
+
});
|
|
1378
|
+
});
|
|
1379
|
+
};
|
|
1380
|
+
/**
|
|
1381
|
+
* creates an function that can be evaluated by calling its implementation
|
|
1382
|
+
* @param node
|
|
1383
|
+
* @returns
|
|
1384
|
+
*/
|
|
1385
|
+
Engine.prototype.build = function (node, id) {
|
|
1386
|
+
var _this = this;
|
|
1387
|
+
if (isPrimitive(node)) {
|
|
1388
|
+
// already a primitive
|
|
1389
|
+
return node;
|
|
1390
|
+
}
|
|
1391
|
+
var _a = node.body, body = _a === void 0 ? {} : _a, props = __rest(node, ["body"]);
|
|
1392
|
+
var primitive = __assign(__assign({}, props), { outputs: {}, mount: function (trigger) {
|
|
1393
|
+
// mount all the nodes
|
|
1394
|
+
var unsubs = [];
|
|
1395
|
+
Object.values(body).forEach(function (n) {
|
|
1396
|
+
var _a;
|
|
1397
|
+
var fn = _this.fnIndex[n.name];
|
|
1398
|
+
if (!fn) {
|
|
1399
|
+
console.warn("function ".concat(n.name, " not found in index"));
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
var unsub = (_a = fn.mount) === null || _a === void 0 ? void 0 : _a.call(fn, trigger);
|
|
1403
|
+
if (unsub) {
|
|
1404
|
+
unsubs.push(unsub);
|
|
1405
|
+
}
|
|
1406
|
+
});
|
|
1407
|
+
return function () {
|
|
1408
|
+
var e_1, _a;
|
|
1409
|
+
try {
|
|
1410
|
+
// console.log(`Unmounting function: ${node.name}`);
|
|
1411
|
+
// unmount all the nodes
|
|
1412
|
+
for (var unsubs_1 = __values(unsubs), unsubs_1_1 = unsubs_1.next(); !unsubs_1_1.done; unsubs_1_1 = unsubs_1.next()) {
|
|
1413
|
+
var unsub = unsubs_1_1.value;
|
|
1414
|
+
unsub();
|
|
1415
|
+
}
|
|
1416
|
+
}
|
|
1417
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
1418
|
+
finally {
|
|
1419
|
+
try {
|
|
1420
|
+
if (unsubs_1_1 && !unsubs_1_1.done && (_a = unsubs_1.return)) _a.call(unsubs_1);
|
|
1421
|
+
}
|
|
1422
|
+
finally { if (e_1) throw e_1.error; }
|
|
1423
|
+
}
|
|
1424
|
+
};
|
|
1425
|
+
}, impl: function (inputs, params) { return __awaiter(_this, void 0, void 0, function () {
|
|
1426
|
+
return __generator(this, function (_a) {
|
|
1427
|
+
// console.log(
|
|
1428
|
+
// `Running function: ${node.name}`,
|
|
1429
|
+
// node,
|
|
1430
|
+
// inputs,
|
|
1431
|
+
// params
|
|
1432
|
+
// );
|
|
1433
|
+
return [2 /*return*/, this.runGraph(node, inputs, params)];
|
|
1434
|
+
});
|
|
1435
|
+
}); } });
|
|
1436
|
+
this.fnIndex[primitive.name] = primitive;
|
|
1437
|
+
return primitive;
|
|
1438
|
+
};
|
|
1439
|
+
Engine.prototype.mount = function (node) {
|
|
1440
|
+
var _this = this;
|
|
1441
|
+
var _a;
|
|
1442
|
+
return (_a = node.mount) === null || _a === void 0 ? void 0 : _a.call(node, function () {
|
|
1443
|
+
// console.log(`Triggering node: ${node.name}`);
|
|
1444
|
+
_this.run(node);
|
|
1445
|
+
});
|
|
1446
|
+
};
|
|
1447
|
+
Engine.prototype.subscribe = function (eventName, listener) {
|
|
1448
|
+
var _this = this;
|
|
1449
|
+
if (!this.listeners[eventName]) {
|
|
1450
|
+
this.listeners[eventName] = new Set();
|
|
1451
|
+
}
|
|
1452
|
+
if (!this.listeners[eventName].has(listener)) {
|
|
1453
|
+
this.listeners[eventName].add(listener);
|
|
1454
|
+
}
|
|
1455
|
+
return function () {
|
|
1456
|
+
_this.listeners[eventName].delete(listener);
|
|
1457
|
+
};
|
|
1458
|
+
};
|
|
1459
|
+
Engine.prototype.fireEvent = function (eventName, event) {
|
|
1460
|
+
var e_2, _a;
|
|
1461
|
+
try {
|
|
1462
|
+
for (var _b = __values(this.listeners[eventName] || []), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
1463
|
+
var listener = _c.value;
|
|
1464
|
+
listener(event);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
1468
|
+
finally {
|
|
1469
|
+
try {
|
|
1470
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
1471
|
+
}
|
|
1472
|
+
finally { if (e_2) throw e_2.error; }
|
|
1473
|
+
}
|
|
1474
|
+
};
|
|
1475
|
+
Engine.prototype.run = function (node_1) {
|
|
1476
|
+
return __awaiter(this, arguments, void 0, function (node, inputs) {
|
|
1477
|
+
var builtFn, mappedInputs, result;
|
|
1478
|
+
var _a;
|
|
1479
|
+
if (inputs === void 0) { inputs = {}; }
|
|
1480
|
+
return __generator(this, function (_b) {
|
|
1481
|
+
switch (_b.label) {
|
|
1482
|
+
case 0:
|
|
1483
|
+
if (!isPrimitive(node)) {
|
|
1484
|
+
builtFn = this.getFunction(node.name);
|
|
1485
|
+
if (!builtFn) {
|
|
1486
|
+
// not yet build
|
|
1487
|
+
builtFn = this.build(node);
|
|
1488
|
+
}
|
|
1489
|
+
return [2 /*return*/, this.run(builtFn, inputs)];
|
|
1490
|
+
}
|
|
1491
|
+
mappedInputs = interpolate(inputs, {});
|
|
1492
|
+
return [4 /*yield*/, ((_a = node.impl) === null || _a === void 0 ? void 0 : _a.call(node, mappedInputs, {}))];
|
|
1493
|
+
case 1:
|
|
1494
|
+
result = _b.sent();
|
|
1495
|
+
// console.log("Engine run complete", result);
|
|
1496
|
+
return [2 /*return*/, result];
|
|
1497
|
+
}
|
|
1498
|
+
});
|
|
1499
|
+
});
|
|
1500
|
+
};
|
|
1501
|
+
Engine.prototype.getFunction = function (name) {
|
|
1502
|
+
return this.fnIndex[name];
|
|
1503
|
+
};
|
|
1504
|
+
// node handlers
|
|
1505
|
+
Engine.prototype.applyNodeDelete = function (oldFn, nodeIds) {
|
|
1506
|
+
var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
|
|
1507
|
+
nodeIds.forEach(function (id) {
|
|
1508
|
+
delete newFn.body[id];
|
|
1509
|
+
});
|
|
1510
|
+
// remove any references to the deleted nodes
|
|
1511
|
+
Object.entries(newFn.body).forEach(function (_a) {
|
|
1512
|
+
var _b = __read(_a, 2), id = _b[0], node = _b[1];
|
|
1513
|
+
Object.entries(node.inputs || {}).forEach(function (_a) {
|
|
1514
|
+
var _b;
|
|
1515
|
+
var _c = __read(_a, 2), name = _c[0], value = _c[1];
|
|
1516
|
+
if (isReference(value)) {
|
|
1517
|
+
var path = parseReference(value);
|
|
1518
|
+
var parts = path.split(".");
|
|
1519
|
+
if (nodeIds.includes(parts[0])) {
|
|
1520
|
+
(_b = newFn.body[id].inputs) === null || _b === void 0 ? true : delete _b[name];
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
});
|
|
1524
|
+
});
|
|
1525
|
+
return newFn;
|
|
1526
|
+
};
|
|
1527
|
+
Engine.prototype.applyEdgeDelete = function (oldFn, fromNode, toNode, fromField, toField) {
|
|
1528
|
+
var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
|
|
1529
|
+
var from = newFn.body[fromNode];
|
|
1530
|
+
var to = newFn.body[toNode];
|
|
1531
|
+
if (!from || !to) {
|
|
1532
|
+
console.warn("Invalid nodes for edge deletion");
|
|
1533
|
+
return newFn;
|
|
1534
|
+
}
|
|
1535
|
+
if (to.inputs && to.inputs[toField] === "<".concat(fromNode, ".").concat(fromField, ">")) {
|
|
1536
|
+
delete to.inputs[toField];
|
|
1537
|
+
}
|
|
1538
|
+
else {
|
|
1539
|
+
console.warn("No matching edge found to delete");
|
|
1540
|
+
}
|
|
1541
|
+
return newFn;
|
|
1542
|
+
};
|
|
1543
|
+
Engine.prototype.applyNodeAdd = function (oldFn, nodeName, callback) {
|
|
1544
|
+
var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
|
|
1545
|
+
var id = uid$1();
|
|
1546
|
+
// ensure unique id
|
|
1547
|
+
while (newFn.body[id]) {
|
|
1548
|
+
id = uid$1();
|
|
1549
|
+
}
|
|
1550
|
+
// get inputs defaults
|
|
1551
|
+
var inputs = {};
|
|
1552
|
+
Object.entries(this.fnIndex[nodeName].inputs || {}).forEach(function (_a) {
|
|
1553
|
+
var _b = __read(_a, 2), key = _b[0], def = _b[1];
|
|
1554
|
+
var normDef = normalizeVarDef(def);
|
|
1555
|
+
if (normDef.default !== undefined) {
|
|
1556
|
+
inputs[key] = normDef.default;
|
|
1557
|
+
}
|
|
1558
|
+
});
|
|
1559
|
+
// get params defaults
|
|
1560
|
+
var params = {};
|
|
1561
|
+
Object.entries(this.fnIndex[nodeName].params || {}).forEach(function (_a) {
|
|
1562
|
+
var _b = __read(_a, 2), key = _b[0], def = _b[1];
|
|
1563
|
+
var normDef = normalizeVarDef(def);
|
|
1564
|
+
if (normDef.default !== undefined) {
|
|
1565
|
+
params[key] = normDef.default;
|
|
1566
|
+
}
|
|
1567
|
+
});
|
|
1568
|
+
var newCall = {
|
|
1569
|
+
name: nodeName,
|
|
1570
|
+
inputs: inputs,
|
|
1571
|
+
params: params,
|
|
1572
|
+
};
|
|
1573
|
+
newFn.body[id] = newCall;
|
|
1574
|
+
callback === null || callback === void 0 ? void 0 : callback(newCall, id);
|
|
1575
|
+
return topSort(newFn);
|
|
1576
|
+
};
|
|
1577
|
+
Engine.prototype.applyNodeConnect = function (oldFn, fromNode, toNode, fromField, toField) {
|
|
1578
|
+
var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
|
|
1579
|
+
var from = newFn.body[fromNode];
|
|
1580
|
+
var to = __assign({}, newFn.body[toNode]);
|
|
1581
|
+
if (!from || !to) {
|
|
1582
|
+
console.warn("Invalid nodes for connection");
|
|
1583
|
+
return newFn;
|
|
1584
|
+
}
|
|
1585
|
+
if (!to.inputs) {
|
|
1586
|
+
to.inputs = {};
|
|
1587
|
+
}
|
|
1588
|
+
// add the reference
|
|
1589
|
+
to.inputs[toField] = "<".concat(fromNode, ".").concat(fromField, ">");
|
|
1590
|
+
newFn.body[toNode] = to;
|
|
1591
|
+
return topSort(newFn);
|
|
1592
|
+
};
|
|
1593
|
+
return Engine;
|
|
1594
|
+
}());
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* Create the React Context
|
|
1598
|
+
*/ const DndContext = createContext({
|
|
1599
|
+
dragDropManager: undefined
|
|
1600
|
+
});
|
|
1601
|
+
|
|
1602
|
+
/**
|
|
1603
|
+
* Use invariant() to assert state which your program assumes to be true.
|
|
1604
|
+
*
|
|
1605
|
+
* Provide sprintf-style format (only %s is supported) and arguments
|
|
1606
|
+
* to provide information about what broke and what you were
|
|
1607
|
+
* expecting.
|
|
1608
|
+
*
|
|
1609
|
+
* The invariant message will be stripped in production, but the invariant
|
|
1610
|
+
* will remain to ensure logic does not differ in production.
|
|
1611
|
+
*/ function invariant(condition, format, ...args) {
|
|
1612
|
+
if (isProduction()) {
|
|
1613
|
+
if (format === undefined) {
|
|
1614
|
+
throw new Error('invariant requires an error message argument');
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
if (!condition) {
|
|
1618
|
+
let error;
|
|
1619
|
+
if (format === undefined) {
|
|
1620
|
+
error = new Error('Minified exception occurred; use the non-minified dev environment ' + 'for the full error message and additional helpful warnings.');
|
|
1621
|
+
} else {
|
|
1622
|
+
let argIndex = 0;
|
|
1623
|
+
error = new Error(format.replace(/%s/g, function() {
|
|
1624
|
+
return args[argIndex++];
|
|
1625
|
+
}));
|
|
1626
|
+
error.name = 'Invariant Violation';
|
|
1627
|
+
}
|
|
1628
|
+
error.framesToPop = 1 // we don't care about invariant's own frame
|
|
1629
|
+
;
|
|
1630
|
+
throw error;
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
function isProduction() {
|
|
1634
|
+
return typeof process !== 'undefined' && process.env['NODE_ENV'] === 'production';
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
function getDefaultExportFromCjs (x) {
|
|
1638
|
+
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
var fastDeepEqual;
|
|
1642
|
+
var hasRequiredFastDeepEqual;
|
|
1643
|
+
|
|
1644
|
+
function requireFastDeepEqual () {
|
|
1645
|
+
if (hasRequiredFastDeepEqual) return fastDeepEqual;
|
|
1646
|
+
hasRequiredFastDeepEqual = 1;
|
|
1647
|
+
|
|
1648
|
+
// do not edit .js files directly - edit src/index.jst
|
|
1649
|
+
|
|
1650
|
+
|
|
1651
|
+
|
|
1652
|
+
fastDeepEqual = function equal(a, b) {
|
|
1653
|
+
if (a === b) return true;
|
|
1654
|
+
|
|
1655
|
+
if (a && b && typeof a == 'object' && typeof b == 'object') {
|
|
1656
|
+
if (a.constructor !== b.constructor) return false;
|
|
1657
|
+
|
|
1658
|
+
var length, i, keys;
|
|
1659
|
+
if (Array.isArray(a)) {
|
|
1660
|
+
length = a.length;
|
|
1661
|
+
if (length != b.length) return false;
|
|
1662
|
+
for (i = length; i-- !== 0;)
|
|
1663
|
+
if (!equal(a[i], b[i])) return false;
|
|
1664
|
+
return true;
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
|
|
1668
|
+
|
|
1669
|
+
if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;
|
|
1670
|
+
if (a.valueOf !== Object.prototype.valueOf) return a.valueOf() === b.valueOf();
|
|
1671
|
+
if (a.toString !== Object.prototype.toString) return a.toString() === b.toString();
|
|
1672
|
+
|
|
1673
|
+
keys = Object.keys(a);
|
|
1674
|
+
length = keys.length;
|
|
1675
|
+
if (length !== Object.keys(b).length) return false;
|
|
1676
|
+
|
|
1677
|
+
for (i = length; i-- !== 0;)
|
|
1678
|
+
if (!Object.prototype.hasOwnProperty.call(b, keys[i])) return false;
|
|
1679
|
+
|
|
1680
|
+
for (i = length; i-- !== 0;) {
|
|
1681
|
+
var key = keys[i];
|
|
1682
|
+
|
|
1683
|
+
if (!equal(a[key], b[key])) return false;
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
return true;
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
// true if both NaN, false otherwise
|
|
1690
|
+
return a!==a && b!==b;
|
|
1691
|
+
};
|
|
1692
|
+
return fastDeepEqual;
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
var fastDeepEqualExports = requireFastDeepEqual();
|
|
1696
|
+
var equal = /*@__PURE__*/getDefaultExportFromCjs(fastDeepEqualExports);
|
|
1697
|
+
|
|
1698
|
+
// suppress the useLayoutEffect warning on server side.
|
|
1699
|
+
const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;
|
|
1700
|
+
|
|
1701
|
+
/**
|
|
1702
|
+
*
|
|
1703
|
+
* @param monitor The monitor to collect state from
|
|
1704
|
+
* @param collect The collecting function
|
|
1705
|
+
* @param onUpdate A method to invoke when updates occur
|
|
1706
|
+
*/ function useCollector(monitor, collect, onUpdate) {
|
|
1707
|
+
const [collected, setCollected] = useState(()=>collect(monitor)
|
|
1708
|
+
);
|
|
1709
|
+
const updateCollected = useCallback(()=>{
|
|
1710
|
+
const nextValue = collect(monitor);
|
|
1711
|
+
// This needs to be a deep-equality check because some monitor-collected values
|
|
1712
|
+
// include XYCoord objects that may be equivalent, but do not have instance equality.
|
|
1713
|
+
if (!equal(collected, nextValue)) {
|
|
1714
|
+
setCollected(nextValue);
|
|
1715
|
+
if (onUpdate) {
|
|
1716
|
+
onUpdate();
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
}, [
|
|
1720
|
+
collected,
|
|
1721
|
+
monitor,
|
|
1722
|
+
onUpdate
|
|
1723
|
+
]);
|
|
1724
|
+
// update the collected properties after react renders.
|
|
1725
|
+
// Note that the "Dustbin Stress Test" fails if this is not
|
|
1726
|
+
// done when the component updates
|
|
1727
|
+
useIsomorphicLayoutEffect(updateCollected);
|
|
1728
|
+
return [
|
|
1729
|
+
collected,
|
|
1730
|
+
updateCollected
|
|
1731
|
+
];
|
|
1732
|
+
}
|
|
1733
|
+
|
|
1734
|
+
function useMonitorOutput(monitor, collect, onCollect) {
|
|
1735
|
+
const [collected, updateCollected] = useCollector(monitor, collect, onCollect);
|
|
1736
|
+
useIsomorphicLayoutEffect(function subscribeToMonitorStateChange() {
|
|
1737
|
+
const handlerId = monitor.getHandlerId();
|
|
1738
|
+
if (handlerId == null) {
|
|
1739
|
+
return;
|
|
1740
|
+
}
|
|
1741
|
+
return monitor.subscribeToStateChange(updateCollected, {
|
|
1742
|
+
handlerIds: [
|
|
1743
|
+
handlerId
|
|
1744
|
+
]
|
|
1745
|
+
});
|
|
1746
|
+
}, [
|
|
1747
|
+
monitor,
|
|
1748
|
+
updateCollected
|
|
1749
|
+
]);
|
|
1750
|
+
return collected;
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
function useCollectedProps(collector, monitor, connector) {
|
|
1754
|
+
return useMonitorOutput(monitor, collector || (()=>({})
|
|
1755
|
+
), ()=>connector.reconnect()
|
|
1756
|
+
);
|
|
1757
|
+
}
|
|
1758
|
+
|
|
1759
|
+
function useOptionalFactory(arg, deps) {
|
|
1760
|
+
const memoDeps = [
|
|
1761
|
+
...[]
|
|
1762
|
+
];
|
|
1763
|
+
if (typeof arg !== 'function') {
|
|
1764
|
+
memoDeps.push(arg);
|
|
1765
|
+
}
|
|
1766
|
+
return useMemo(()=>{
|
|
1767
|
+
return typeof arg === 'function' ? arg() : arg;
|
|
1768
|
+
}, memoDeps);
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
function useConnectDragSource(connector) {
|
|
1772
|
+
return useMemo(()=>connector.hooks.dragSource()
|
|
1773
|
+
, [
|
|
1774
|
+
connector
|
|
1775
|
+
]);
|
|
1776
|
+
}
|
|
1777
|
+
function useConnectDragPreview(connector) {
|
|
1778
|
+
return useMemo(()=>connector.hooks.dragPreview()
|
|
1779
|
+
, [
|
|
1780
|
+
connector
|
|
1781
|
+
]);
|
|
1782
|
+
}
|
|
1783
|
+
|
|
1784
|
+
let isCallingCanDrag = false;
|
|
1785
|
+
let isCallingIsDragging = false;
|
|
1786
|
+
class DragSourceMonitorImpl {
|
|
1787
|
+
receiveHandlerId(sourceId) {
|
|
1788
|
+
this.sourceId = sourceId;
|
|
1789
|
+
}
|
|
1790
|
+
getHandlerId() {
|
|
1791
|
+
return this.sourceId;
|
|
1792
|
+
}
|
|
1793
|
+
canDrag() {
|
|
1794
|
+
invariant(!isCallingCanDrag, 'You may not call monitor.canDrag() inside your canDrag() implementation. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs/api/drag-source-monitor');
|
|
1795
|
+
try {
|
|
1796
|
+
isCallingCanDrag = true;
|
|
1797
|
+
return this.internalMonitor.canDragSource(this.sourceId);
|
|
1798
|
+
} finally{
|
|
1799
|
+
isCallingCanDrag = false;
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
isDragging() {
|
|
1803
|
+
if (!this.sourceId) {
|
|
1804
|
+
return false;
|
|
1805
|
+
}
|
|
1806
|
+
invariant(!isCallingIsDragging, 'You may not call monitor.isDragging() inside your isDragging() implementation. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs/api/drag-source-monitor');
|
|
1807
|
+
try {
|
|
1808
|
+
isCallingIsDragging = true;
|
|
1809
|
+
return this.internalMonitor.isDraggingSource(this.sourceId);
|
|
1810
|
+
} finally{
|
|
1811
|
+
isCallingIsDragging = false;
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
subscribeToStateChange(listener, options) {
|
|
1815
|
+
return this.internalMonitor.subscribeToStateChange(listener, options);
|
|
1816
|
+
}
|
|
1817
|
+
isDraggingSource(sourceId) {
|
|
1818
|
+
return this.internalMonitor.isDraggingSource(sourceId);
|
|
1819
|
+
}
|
|
1820
|
+
isOverTarget(targetId, options) {
|
|
1821
|
+
return this.internalMonitor.isOverTarget(targetId, options);
|
|
1822
|
+
}
|
|
1823
|
+
getTargetIds() {
|
|
1824
|
+
return this.internalMonitor.getTargetIds();
|
|
1825
|
+
}
|
|
1826
|
+
isSourcePublic() {
|
|
1827
|
+
return this.internalMonitor.isSourcePublic();
|
|
1828
|
+
}
|
|
1829
|
+
getSourceId() {
|
|
1830
|
+
return this.internalMonitor.getSourceId();
|
|
1831
|
+
}
|
|
1832
|
+
subscribeToOffsetChange(listener) {
|
|
1833
|
+
return this.internalMonitor.subscribeToOffsetChange(listener);
|
|
1834
|
+
}
|
|
1835
|
+
canDragSource(sourceId) {
|
|
1836
|
+
return this.internalMonitor.canDragSource(sourceId);
|
|
1837
|
+
}
|
|
1838
|
+
canDropOnTarget(targetId) {
|
|
1839
|
+
return this.internalMonitor.canDropOnTarget(targetId);
|
|
1840
|
+
}
|
|
1841
|
+
getItemType() {
|
|
1842
|
+
return this.internalMonitor.getItemType();
|
|
1843
|
+
}
|
|
1844
|
+
getItem() {
|
|
1845
|
+
return this.internalMonitor.getItem();
|
|
1846
|
+
}
|
|
1847
|
+
getDropResult() {
|
|
1848
|
+
return this.internalMonitor.getDropResult();
|
|
1849
|
+
}
|
|
1850
|
+
didDrop() {
|
|
1851
|
+
return this.internalMonitor.didDrop();
|
|
1852
|
+
}
|
|
1853
|
+
getInitialClientOffset() {
|
|
1854
|
+
return this.internalMonitor.getInitialClientOffset();
|
|
1855
|
+
}
|
|
1856
|
+
getInitialSourceClientOffset() {
|
|
1857
|
+
return this.internalMonitor.getInitialSourceClientOffset();
|
|
1858
|
+
}
|
|
1859
|
+
getSourceClientOffset() {
|
|
1860
|
+
return this.internalMonitor.getSourceClientOffset();
|
|
1861
|
+
}
|
|
1862
|
+
getClientOffset() {
|
|
1863
|
+
return this.internalMonitor.getClientOffset();
|
|
1864
|
+
}
|
|
1865
|
+
getDifferenceFromInitialOffset() {
|
|
1866
|
+
return this.internalMonitor.getDifferenceFromInitialOffset();
|
|
1867
|
+
}
|
|
1868
|
+
constructor(manager){
|
|
1869
|
+
this.sourceId = null;
|
|
1870
|
+
this.internalMonitor = manager.getMonitor();
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
let isCallingCanDrop = false;
|
|
1875
|
+
class DropTargetMonitorImpl {
|
|
1876
|
+
receiveHandlerId(targetId) {
|
|
1877
|
+
this.targetId = targetId;
|
|
1878
|
+
}
|
|
1879
|
+
getHandlerId() {
|
|
1880
|
+
return this.targetId;
|
|
1881
|
+
}
|
|
1882
|
+
subscribeToStateChange(listener, options) {
|
|
1883
|
+
return this.internalMonitor.subscribeToStateChange(listener, options);
|
|
1884
|
+
}
|
|
1885
|
+
canDrop() {
|
|
1886
|
+
// Cut out early if the target id has not been set. This should prevent errors
|
|
1887
|
+
// where the user has an older version of dnd-core like in
|
|
1888
|
+
// https://github.com/react-dnd/react-dnd/issues/1310
|
|
1889
|
+
if (!this.targetId) {
|
|
1890
|
+
return false;
|
|
1891
|
+
}
|
|
1892
|
+
invariant(!isCallingCanDrop, 'You may not call monitor.canDrop() inside your canDrop() implementation. ' + 'Read more: http://react-dnd.github.io/react-dnd/docs/api/drop-target-monitor');
|
|
1893
|
+
try {
|
|
1894
|
+
isCallingCanDrop = true;
|
|
1895
|
+
return this.internalMonitor.canDropOnTarget(this.targetId);
|
|
1896
|
+
} finally{
|
|
1897
|
+
isCallingCanDrop = false;
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
isOver(options) {
|
|
1901
|
+
if (!this.targetId) {
|
|
1902
|
+
return false;
|
|
1903
|
+
}
|
|
1904
|
+
return this.internalMonitor.isOverTarget(this.targetId, options);
|
|
1905
|
+
}
|
|
1906
|
+
getItemType() {
|
|
1907
|
+
return this.internalMonitor.getItemType();
|
|
1908
|
+
}
|
|
1909
|
+
getItem() {
|
|
1910
|
+
return this.internalMonitor.getItem();
|
|
1911
|
+
}
|
|
1912
|
+
getDropResult() {
|
|
1913
|
+
return this.internalMonitor.getDropResult();
|
|
1914
|
+
}
|
|
1915
|
+
didDrop() {
|
|
1916
|
+
return this.internalMonitor.didDrop();
|
|
1917
|
+
}
|
|
1918
|
+
getInitialClientOffset() {
|
|
1919
|
+
return this.internalMonitor.getInitialClientOffset();
|
|
1920
|
+
}
|
|
1921
|
+
getInitialSourceClientOffset() {
|
|
1922
|
+
return this.internalMonitor.getInitialSourceClientOffset();
|
|
1923
|
+
}
|
|
1924
|
+
getSourceClientOffset() {
|
|
1925
|
+
return this.internalMonitor.getSourceClientOffset();
|
|
1926
|
+
}
|
|
1927
|
+
getClientOffset() {
|
|
1928
|
+
return this.internalMonitor.getClientOffset();
|
|
1929
|
+
}
|
|
1930
|
+
getDifferenceFromInitialOffset() {
|
|
1931
|
+
return this.internalMonitor.getDifferenceFromInitialOffset();
|
|
1932
|
+
}
|
|
1933
|
+
constructor(manager){
|
|
1934
|
+
this.targetId = null;
|
|
1935
|
+
this.internalMonitor = manager.getMonitor();
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
function registerTarget(type, target, manager) {
|
|
1940
|
+
const registry = manager.getRegistry();
|
|
1941
|
+
const targetId = registry.addTarget(type, target);
|
|
1942
|
+
return [
|
|
1943
|
+
targetId,
|
|
1944
|
+
()=>registry.removeTarget(targetId)
|
|
1945
|
+
];
|
|
1946
|
+
}
|
|
1947
|
+
function registerSource(type, source, manager) {
|
|
1948
|
+
const registry = manager.getRegistry();
|
|
1949
|
+
const sourceId = registry.addSource(type, source);
|
|
1950
|
+
return [
|
|
1951
|
+
sourceId,
|
|
1952
|
+
()=>registry.removeSource(sourceId)
|
|
1953
|
+
];
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
function shallowEqual(objA, objB, compare, compareContext) {
|
|
1957
|
+
let compareResult = void 0;
|
|
1958
|
+
if (compareResult !== void 0) {
|
|
1959
|
+
return !!compareResult;
|
|
1960
|
+
}
|
|
1961
|
+
if (objA === objB) {
|
|
1962
|
+
return true;
|
|
1963
|
+
}
|
|
1964
|
+
if (typeof objA !== 'object' || !objA || typeof objB !== 'object' || !objB) {
|
|
1965
|
+
return false;
|
|
1966
|
+
}
|
|
1967
|
+
const keysA = Object.keys(objA);
|
|
1968
|
+
const keysB = Object.keys(objB);
|
|
1969
|
+
if (keysA.length !== keysB.length) {
|
|
1970
|
+
return false;
|
|
1971
|
+
}
|
|
1972
|
+
const bHasOwnProperty = Object.prototype.hasOwnProperty.bind(objB);
|
|
1973
|
+
// Test for A's keys different from B.
|
|
1974
|
+
for(let idx = 0; idx < keysA.length; idx++){
|
|
1975
|
+
const key = keysA[idx];
|
|
1976
|
+
if (!bHasOwnProperty(key)) {
|
|
1977
|
+
return false;
|
|
1978
|
+
}
|
|
1979
|
+
const valueA = objA[key];
|
|
1980
|
+
const valueB = objB[key];
|
|
1981
|
+
compareResult = void 0;
|
|
1982
|
+
if (compareResult === false || compareResult === void 0 && valueA !== valueB) {
|
|
1983
|
+
return false;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
return true;
|
|
1987
|
+
}
|
|
1988
|
+
|
|
1989
|
+
function isRef(obj) {
|
|
1990
|
+
return(// eslint-disable-next-line no-prototype-builtins
|
|
1991
|
+
obj !== null && typeof obj === 'object' && Object.prototype.hasOwnProperty.call(obj, 'current'));
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
function throwIfCompositeComponentElement(element) {
|
|
1995
|
+
// Custom components can no longer be wrapped directly in React DnD 2.0
|
|
1996
|
+
// so that we don't need to depend on findDOMNode() from react-dom.
|
|
1997
|
+
if (typeof element.type === 'string') {
|
|
1998
|
+
return;
|
|
1999
|
+
}
|
|
2000
|
+
const displayName = element.type.displayName || element.type.name || 'the component';
|
|
2001
|
+
throw new Error('Only native element nodes can now be passed to React DnD connectors.' + `You can either wrap ${displayName} into a <div>, or turn it into a ` + 'drag source or a drop target itself.');
|
|
2002
|
+
}
|
|
2003
|
+
function wrapHookToRecognizeElement(hook) {
|
|
2004
|
+
return (elementOrNode = null, options = null)=>{
|
|
2005
|
+
// When passed a node, call the hook straight away.
|
|
2006
|
+
if (!isValidElement(elementOrNode)) {
|
|
2007
|
+
const node = elementOrNode;
|
|
2008
|
+
hook(node, options);
|
|
2009
|
+
// return the node so it can be chained (e.g. when within callback refs
|
|
2010
|
+
// <div ref={node => connectDragSource(connectDropTarget(node))}/>
|
|
2011
|
+
return node;
|
|
2012
|
+
}
|
|
2013
|
+
// If passed a ReactElement, clone it and attach this function as a ref.
|
|
2014
|
+
// This helps us achieve a neat API where user doesn't even know that refs
|
|
2015
|
+
// are being used under the hood.
|
|
2016
|
+
const element = elementOrNode;
|
|
2017
|
+
throwIfCompositeComponentElement(element);
|
|
2018
|
+
// When no options are passed, use the hook directly
|
|
2019
|
+
const ref = options ? (node)=>hook(node, options)
|
|
2020
|
+
: hook;
|
|
2021
|
+
return cloneWithRef(element, ref);
|
|
2022
|
+
};
|
|
2023
|
+
}
|
|
2024
|
+
function wrapConnectorHooks(hooks) {
|
|
2025
|
+
const wrappedHooks = {};
|
|
2026
|
+
Object.keys(hooks).forEach((key)=>{
|
|
2027
|
+
const hook = hooks[key];
|
|
2028
|
+
// ref objects should be passed straight through without wrapping
|
|
2029
|
+
if (key.endsWith('Ref')) {
|
|
2030
|
+
wrappedHooks[key] = hooks[key];
|
|
2031
|
+
} else {
|
|
2032
|
+
const wrappedHook = wrapHookToRecognizeElement(hook);
|
|
2033
|
+
wrappedHooks[key] = ()=>wrappedHook
|
|
2034
|
+
;
|
|
2035
|
+
}
|
|
2036
|
+
});
|
|
2037
|
+
return wrappedHooks;
|
|
2038
|
+
}
|
|
2039
|
+
function setRef(ref, node) {
|
|
2040
|
+
if (typeof ref === 'function') {
|
|
2041
|
+
ref(node);
|
|
2042
|
+
} else {
|
|
2043
|
+
ref.current = node;
|
|
2044
|
+
}
|
|
2045
|
+
}
|
|
2046
|
+
function cloneWithRef(element, newRef) {
|
|
2047
|
+
const previousRef = element.ref;
|
|
2048
|
+
invariant(typeof previousRef !== 'string', 'Cannot connect React DnD to an element with an existing string ref. ' + 'Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. ' + 'Read more: https://reactjs.org/docs/refs-and-the-dom.html#callback-refs');
|
|
2049
|
+
if (!previousRef) {
|
|
2050
|
+
// When there is no ref on the element, use the new ref directly
|
|
2051
|
+
return cloneElement(element, {
|
|
2052
|
+
ref: newRef
|
|
2053
|
+
});
|
|
2054
|
+
} else {
|
|
2055
|
+
return cloneElement(element, {
|
|
2056
|
+
ref: (node)=>{
|
|
2057
|
+
setRef(previousRef, node);
|
|
2058
|
+
setRef(newRef, node);
|
|
2059
|
+
}
|
|
2060
|
+
});
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
|
|
2064
|
+
class SourceConnector {
|
|
2065
|
+
receiveHandlerId(newHandlerId) {
|
|
2066
|
+
if (this.handlerId === newHandlerId) {
|
|
2067
|
+
return;
|
|
2068
|
+
}
|
|
2069
|
+
this.handlerId = newHandlerId;
|
|
2070
|
+
this.reconnect();
|
|
2071
|
+
}
|
|
2072
|
+
get connectTarget() {
|
|
2073
|
+
return this.dragSource;
|
|
2074
|
+
}
|
|
2075
|
+
get dragSourceOptions() {
|
|
2076
|
+
return this.dragSourceOptionsInternal;
|
|
2077
|
+
}
|
|
2078
|
+
set dragSourceOptions(options) {
|
|
2079
|
+
this.dragSourceOptionsInternal = options;
|
|
2080
|
+
}
|
|
2081
|
+
get dragPreviewOptions() {
|
|
2082
|
+
return this.dragPreviewOptionsInternal;
|
|
2083
|
+
}
|
|
2084
|
+
set dragPreviewOptions(options) {
|
|
2085
|
+
this.dragPreviewOptionsInternal = options;
|
|
2086
|
+
}
|
|
2087
|
+
reconnect() {
|
|
2088
|
+
const didChange = this.reconnectDragSource();
|
|
2089
|
+
this.reconnectDragPreview(didChange);
|
|
2090
|
+
}
|
|
2091
|
+
reconnectDragSource() {
|
|
2092
|
+
const dragSource = this.dragSource;
|
|
2093
|
+
// if nothing has changed then don't resubscribe
|
|
2094
|
+
const didChange = this.didHandlerIdChange() || this.didConnectedDragSourceChange() || this.didDragSourceOptionsChange();
|
|
2095
|
+
if (didChange) {
|
|
2096
|
+
this.disconnectDragSource();
|
|
2097
|
+
}
|
|
2098
|
+
if (!this.handlerId) {
|
|
2099
|
+
return didChange;
|
|
2100
|
+
}
|
|
2101
|
+
if (!dragSource) {
|
|
2102
|
+
this.lastConnectedDragSource = dragSource;
|
|
2103
|
+
return didChange;
|
|
2104
|
+
}
|
|
2105
|
+
if (didChange) {
|
|
2106
|
+
this.lastConnectedHandlerId = this.handlerId;
|
|
2107
|
+
this.lastConnectedDragSource = dragSource;
|
|
2108
|
+
this.lastConnectedDragSourceOptions = this.dragSourceOptions;
|
|
2109
|
+
this.dragSourceUnsubscribe = this.backend.connectDragSource(this.handlerId, dragSource, this.dragSourceOptions);
|
|
2110
|
+
}
|
|
2111
|
+
return didChange;
|
|
2112
|
+
}
|
|
2113
|
+
reconnectDragPreview(forceDidChange = false) {
|
|
2114
|
+
const dragPreview = this.dragPreview;
|
|
2115
|
+
// if nothing has changed then don't resubscribe
|
|
2116
|
+
const didChange = forceDidChange || this.didHandlerIdChange() || this.didConnectedDragPreviewChange() || this.didDragPreviewOptionsChange();
|
|
2117
|
+
if (didChange) {
|
|
2118
|
+
this.disconnectDragPreview();
|
|
2119
|
+
}
|
|
2120
|
+
if (!this.handlerId) {
|
|
2121
|
+
return;
|
|
2122
|
+
}
|
|
2123
|
+
if (!dragPreview) {
|
|
2124
|
+
this.lastConnectedDragPreview = dragPreview;
|
|
2125
|
+
return;
|
|
2126
|
+
}
|
|
2127
|
+
if (didChange) {
|
|
2128
|
+
this.lastConnectedHandlerId = this.handlerId;
|
|
2129
|
+
this.lastConnectedDragPreview = dragPreview;
|
|
2130
|
+
this.lastConnectedDragPreviewOptions = this.dragPreviewOptions;
|
|
2131
|
+
this.dragPreviewUnsubscribe = this.backend.connectDragPreview(this.handlerId, dragPreview, this.dragPreviewOptions);
|
|
2132
|
+
}
|
|
2133
|
+
}
|
|
2134
|
+
didHandlerIdChange() {
|
|
2135
|
+
return this.lastConnectedHandlerId !== this.handlerId;
|
|
2136
|
+
}
|
|
2137
|
+
didConnectedDragSourceChange() {
|
|
2138
|
+
return this.lastConnectedDragSource !== this.dragSource;
|
|
2139
|
+
}
|
|
2140
|
+
didConnectedDragPreviewChange() {
|
|
2141
|
+
return this.lastConnectedDragPreview !== this.dragPreview;
|
|
2142
|
+
}
|
|
2143
|
+
didDragSourceOptionsChange() {
|
|
2144
|
+
return !shallowEqual(this.lastConnectedDragSourceOptions, this.dragSourceOptions);
|
|
2145
|
+
}
|
|
2146
|
+
didDragPreviewOptionsChange() {
|
|
2147
|
+
return !shallowEqual(this.lastConnectedDragPreviewOptions, this.dragPreviewOptions);
|
|
2148
|
+
}
|
|
2149
|
+
disconnectDragSource() {
|
|
2150
|
+
if (this.dragSourceUnsubscribe) {
|
|
2151
|
+
this.dragSourceUnsubscribe();
|
|
2152
|
+
this.dragSourceUnsubscribe = undefined;
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
disconnectDragPreview() {
|
|
2156
|
+
if (this.dragPreviewUnsubscribe) {
|
|
2157
|
+
this.dragPreviewUnsubscribe();
|
|
2158
|
+
this.dragPreviewUnsubscribe = undefined;
|
|
2159
|
+
this.dragPreviewNode = null;
|
|
2160
|
+
this.dragPreviewRef = null;
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
get dragSource() {
|
|
2164
|
+
return this.dragSourceNode || this.dragSourceRef && this.dragSourceRef.current;
|
|
2165
|
+
}
|
|
2166
|
+
get dragPreview() {
|
|
2167
|
+
return this.dragPreviewNode || this.dragPreviewRef && this.dragPreviewRef.current;
|
|
2168
|
+
}
|
|
2169
|
+
clearDragSource() {
|
|
2170
|
+
this.dragSourceNode = null;
|
|
2171
|
+
this.dragSourceRef = null;
|
|
2172
|
+
}
|
|
2173
|
+
clearDragPreview() {
|
|
2174
|
+
this.dragPreviewNode = null;
|
|
2175
|
+
this.dragPreviewRef = null;
|
|
2176
|
+
}
|
|
2177
|
+
constructor(backend){
|
|
2178
|
+
this.hooks = wrapConnectorHooks({
|
|
2179
|
+
dragSource: (node, options)=>{
|
|
2180
|
+
this.clearDragSource();
|
|
2181
|
+
this.dragSourceOptions = options || null;
|
|
2182
|
+
if (isRef(node)) {
|
|
2183
|
+
this.dragSourceRef = node;
|
|
2184
|
+
} else {
|
|
2185
|
+
this.dragSourceNode = node;
|
|
2186
|
+
}
|
|
2187
|
+
this.reconnectDragSource();
|
|
2188
|
+
},
|
|
2189
|
+
dragPreview: (node, options)=>{
|
|
2190
|
+
this.clearDragPreview();
|
|
2191
|
+
this.dragPreviewOptions = options || null;
|
|
2192
|
+
if (isRef(node)) {
|
|
2193
|
+
this.dragPreviewRef = node;
|
|
2194
|
+
} else {
|
|
2195
|
+
this.dragPreviewNode = node;
|
|
2196
|
+
}
|
|
2197
|
+
this.reconnectDragPreview();
|
|
2198
|
+
}
|
|
2199
|
+
});
|
|
2200
|
+
this.handlerId = null;
|
|
2201
|
+
// The drop target may either be attached via ref or connect function
|
|
2202
|
+
this.dragSourceRef = null;
|
|
2203
|
+
this.dragSourceOptionsInternal = null;
|
|
2204
|
+
// The drag preview may either be attached via ref or connect function
|
|
2205
|
+
this.dragPreviewRef = null;
|
|
2206
|
+
this.dragPreviewOptionsInternal = null;
|
|
2207
|
+
this.lastConnectedHandlerId = null;
|
|
2208
|
+
this.lastConnectedDragSource = null;
|
|
2209
|
+
this.lastConnectedDragSourceOptions = null;
|
|
2210
|
+
this.lastConnectedDragPreview = null;
|
|
2211
|
+
this.lastConnectedDragPreviewOptions = null;
|
|
2212
|
+
this.backend = backend;
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
class TargetConnector {
|
|
2217
|
+
get connectTarget() {
|
|
2218
|
+
return this.dropTarget;
|
|
2219
|
+
}
|
|
2220
|
+
reconnect() {
|
|
2221
|
+
// if nothing has changed then don't resubscribe
|
|
2222
|
+
const didChange = this.didHandlerIdChange() || this.didDropTargetChange() || this.didOptionsChange();
|
|
2223
|
+
if (didChange) {
|
|
2224
|
+
this.disconnectDropTarget();
|
|
2225
|
+
}
|
|
2226
|
+
const dropTarget = this.dropTarget;
|
|
2227
|
+
if (!this.handlerId) {
|
|
2228
|
+
return;
|
|
2229
|
+
}
|
|
2230
|
+
if (!dropTarget) {
|
|
2231
|
+
this.lastConnectedDropTarget = dropTarget;
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
if (didChange) {
|
|
2235
|
+
this.lastConnectedHandlerId = this.handlerId;
|
|
2236
|
+
this.lastConnectedDropTarget = dropTarget;
|
|
2237
|
+
this.lastConnectedDropTargetOptions = this.dropTargetOptions;
|
|
2238
|
+
this.unsubscribeDropTarget = this.backend.connectDropTarget(this.handlerId, dropTarget, this.dropTargetOptions);
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
receiveHandlerId(newHandlerId) {
|
|
2242
|
+
if (newHandlerId === this.handlerId) {
|
|
2243
|
+
return;
|
|
2244
|
+
}
|
|
2245
|
+
this.handlerId = newHandlerId;
|
|
2246
|
+
this.reconnect();
|
|
2247
|
+
}
|
|
2248
|
+
get dropTargetOptions() {
|
|
2249
|
+
return this.dropTargetOptionsInternal;
|
|
2250
|
+
}
|
|
2251
|
+
set dropTargetOptions(options) {
|
|
2252
|
+
this.dropTargetOptionsInternal = options;
|
|
2253
|
+
}
|
|
2254
|
+
didHandlerIdChange() {
|
|
2255
|
+
return this.lastConnectedHandlerId !== this.handlerId;
|
|
2256
|
+
}
|
|
2257
|
+
didDropTargetChange() {
|
|
2258
|
+
return this.lastConnectedDropTarget !== this.dropTarget;
|
|
2259
|
+
}
|
|
2260
|
+
didOptionsChange() {
|
|
2261
|
+
return !shallowEqual(this.lastConnectedDropTargetOptions, this.dropTargetOptions);
|
|
2262
|
+
}
|
|
2263
|
+
disconnectDropTarget() {
|
|
2264
|
+
if (this.unsubscribeDropTarget) {
|
|
2265
|
+
this.unsubscribeDropTarget();
|
|
2266
|
+
this.unsubscribeDropTarget = undefined;
|
|
2267
|
+
}
|
|
2268
|
+
}
|
|
2269
|
+
get dropTarget() {
|
|
2270
|
+
return this.dropTargetNode || this.dropTargetRef && this.dropTargetRef.current;
|
|
2271
|
+
}
|
|
2272
|
+
clearDropTarget() {
|
|
2273
|
+
this.dropTargetRef = null;
|
|
2274
|
+
this.dropTargetNode = null;
|
|
2275
|
+
}
|
|
2276
|
+
constructor(backend){
|
|
2277
|
+
this.hooks = wrapConnectorHooks({
|
|
2278
|
+
dropTarget: (node, options)=>{
|
|
2279
|
+
this.clearDropTarget();
|
|
2280
|
+
this.dropTargetOptions = options;
|
|
2281
|
+
if (isRef(node)) {
|
|
2282
|
+
this.dropTargetRef = node;
|
|
2283
|
+
} else {
|
|
2284
|
+
this.dropTargetNode = node;
|
|
2285
|
+
}
|
|
2286
|
+
this.reconnect();
|
|
2287
|
+
}
|
|
2288
|
+
});
|
|
2289
|
+
this.handlerId = null;
|
|
2290
|
+
// The drop target may either be attached via ref or connect function
|
|
2291
|
+
this.dropTargetRef = null;
|
|
2292
|
+
this.dropTargetOptionsInternal = null;
|
|
2293
|
+
this.lastConnectedHandlerId = null;
|
|
2294
|
+
this.lastConnectedDropTarget = null;
|
|
2295
|
+
this.lastConnectedDropTargetOptions = null;
|
|
2296
|
+
this.backend = backend;
|
|
2297
|
+
}
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
/**
|
|
2301
|
+
* A hook to retrieve the DragDropManager from Context
|
|
2302
|
+
*/ function useDragDropManager() {
|
|
2303
|
+
const { dragDropManager } = useContext(DndContext);
|
|
2304
|
+
invariant(dragDropManager != null, 'Expected drag drop context');
|
|
2305
|
+
return dragDropManager;
|
|
2306
|
+
}
|
|
2307
|
+
|
|
2308
|
+
function useDragSourceConnector(dragSourceOptions, dragPreviewOptions) {
|
|
2309
|
+
const manager = useDragDropManager();
|
|
2310
|
+
const connector = useMemo(()=>new SourceConnector(manager.getBackend())
|
|
2311
|
+
, [
|
|
2312
|
+
manager
|
|
2313
|
+
]);
|
|
2314
|
+
useIsomorphicLayoutEffect(()=>{
|
|
2315
|
+
connector.dragSourceOptions = dragSourceOptions || null;
|
|
2316
|
+
connector.reconnect();
|
|
2317
|
+
return ()=>connector.disconnectDragSource()
|
|
2318
|
+
;
|
|
2319
|
+
}, [
|
|
2320
|
+
connector,
|
|
2321
|
+
dragSourceOptions
|
|
2322
|
+
]);
|
|
2323
|
+
useIsomorphicLayoutEffect(()=>{
|
|
2324
|
+
connector.dragPreviewOptions = dragPreviewOptions || null;
|
|
2325
|
+
connector.reconnect();
|
|
2326
|
+
return ()=>connector.disconnectDragPreview()
|
|
2327
|
+
;
|
|
2328
|
+
}, [
|
|
2329
|
+
connector,
|
|
2330
|
+
dragPreviewOptions
|
|
2331
|
+
]);
|
|
2332
|
+
return connector;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
function useDragSourceMonitor() {
|
|
2336
|
+
const manager = useDragDropManager();
|
|
2337
|
+
return useMemo(()=>new DragSourceMonitorImpl(manager)
|
|
2338
|
+
, [
|
|
2339
|
+
manager
|
|
2340
|
+
]);
|
|
2341
|
+
}
|
|
2342
|
+
|
|
2343
|
+
class DragSourceImpl {
|
|
2344
|
+
beginDrag() {
|
|
2345
|
+
const spec = this.spec;
|
|
2346
|
+
const monitor = this.monitor;
|
|
2347
|
+
let result = null;
|
|
2348
|
+
if (typeof spec.item === 'object') {
|
|
2349
|
+
result = spec.item;
|
|
2350
|
+
} else if (typeof spec.item === 'function') {
|
|
2351
|
+
result = spec.item(monitor);
|
|
2352
|
+
} else {
|
|
2353
|
+
result = {};
|
|
2354
|
+
}
|
|
2355
|
+
return result !== null && result !== void 0 ? result : null;
|
|
2356
|
+
}
|
|
2357
|
+
canDrag() {
|
|
2358
|
+
const spec = this.spec;
|
|
2359
|
+
const monitor = this.monitor;
|
|
2360
|
+
if (typeof spec.canDrag === 'boolean') {
|
|
2361
|
+
return spec.canDrag;
|
|
2362
|
+
} else if (typeof spec.canDrag === 'function') {
|
|
2363
|
+
return spec.canDrag(monitor);
|
|
2364
|
+
} else {
|
|
2365
|
+
return true;
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
2368
|
+
isDragging(globalMonitor, target) {
|
|
2369
|
+
const spec = this.spec;
|
|
2370
|
+
const monitor = this.monitor;
|
|
2371
|
+
const { isDragging } = spec;
|
|
2372
|
+
return isDragging ? isDragging(monitor) : target === globalMonitor.getSourceId();
|
|
2373
|
+
}
|
|
2374
|
+
endDrag() {
|
|
2375
|
+
const spec = this.spec;
|
|
2376
|
+
const monitor = this.monitor;
|
|
2377
|
+
const connector = this.connector;
|
|
2378
|
+
const { end } = spec;
|
|
2379
|
+
if (end) {
|
|
2380
|
+
end(monitor.getItem(), monitor);
|
|
2381
|
+
}
|
|
2382
|
+
connector.reconnect();
|
|
2383
|
+
}
|
|
2384
|
+
constructor(spec, monitor, connector){
|
|
2385
|
+
this.spec = spec;
|
|
2386
|
+
this.monitor = monitor;
|
|
2387
|
+
this.connector = connector;
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
function useDragSource(spec, monitor, connector) {
|
|
2392
|
+
const handler = useMemo(()=>new DragSourceImpl(spec, monitor, connector)
|
|
2393
|
+
, [
|
|
2394
|
+
monitor,
|
|
2395
|
+
connector
|
|
2396
|
+
]);
|
|
2397
|
+
useEffect(()=>{
|
|
2398
|
+
handler.spec = spec;
|
|
2399
|
+
}, [
|
|
2400
|
+
spec
|
|
2401
|
+
]);
|
|
2402
|
+
return handler;
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
function useDragType(spec) {
|
|
2406
|
+
return useMemo(()=>{
|
|
2407
|
+
const result = spec.type;
|
|
2408
|
+
invariant(result != null, 'spec.type must be defined');
|
|
2409
|
+
return result;
|
|
2410
|
+
}, [
|
|
2411
|
+
spec
|
|
2412
|
+
]);
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2415
|
+
function useRegisteredDragSource(spec, monitor, connector) {
|
|
2416
|
+
const manager = useDragDropManager();
|
|
2417
|
+
const handler = useDragSource(spec, monitor, connector);
|
|
2418
|
+
const itemType = useDragType(spec);
|
|
2419
|
+
useIsomorphicLayoutEffect(function registerDragSource() {
|
|
2420
|
+
if (itemType != null) {
|
|
2421
|
+
const [handlerId, unregister] = registerSource(itemType, handler, manager);
|
|
2422
|
+
monitor.receiveHandlerId(handlerId);
|
|
2423
|
+
connector.receiveHandlerId(handlerId);
|
|
2424
|
+
return unregister;
|
|
2425
|
+
}
|
|
2426
|
+
return;
|
|
2427
|
+
}, [
|
|
2428
|
+
manager,
|
|
2429
|
+
monitor,
|
|
2430
|
+
connector,
|
|
2431
|
+
handler,
|
|
2432
|
+
itemType
|
|
2433
|
+
]);
|
|
2434
|
+
}
|
|
2435
|
+
|
|
2436
|
+
/**
|
|
2437
|
+
* useDragSource hook
|
|
2438
|
+
* @param sourceSpec The drag source specification (object or function, function preferred)
|
|
2439
|
+
* @param deps The memoization deps array to use when evaluating spec changes
|
|
2440
|
+
*/ function useDrag(specArg, deps) {
|
|
2441
|
+
const spec = useOptionalFactory(specArg);
|
|
2442
|
+
invariant(!spec.begin, `useDrag::spec.begin was deprecated in v14. Replace spec.begin() with spec.item(). (see more here - https://react-dnd.github.io/react-dnd/docs/api/use-drag)`);
|
|
2443
|
+
const monitor = useDragSourceMonitor();
|
|
2444
|
+
const connector = useDragSourceConnector(spec.options, spec.previewOptions);
|
|
2445
|
+
useRegisteredDragSource(spec, monitor, connector);
|
|
2446
|
+
return [
|
|
2447
|
+
useCollectedProps(spec.collect, monitor, connector),
|
|
2448
|
+
useConnectDragSource(connector),
|
|
2449
|
+
useConnectDragPreview(connector),
|
|
2450
|
+
];
|
|
2451
|
+
}
|
|
2452
|
+
|
|
2453
|
+
function useConnectDropTarget(connector) {
|
|
2454
|
+
return useMemo(()=>connector.hooks.dropTarget()
|
|
2455
|
+
, [
|
|
2456
|
+
connector
|
|
2457
|
+
]);
|
|
2458
|
+
}
|
|
2459
|
+
|
|
2460
|
+
function useDropTargetConnector(options) {
|
|
2461
|
+
const manager = useDragDropManager();
|
|
2462
|
+
const connector = useMemo(()=>new TargetConnector(manager.getBackend())
|
|
2463
|
+
, [
|
|
2464
|
+
manager
|
|
2465
|
+
]);
|
|
2466
|
+
useIsomorphicLayoutEffect(()=>{
|
|
2467
|
+
connector.dropTargetOptions = options || null;
|
|
2468
|
+
connector.reconnect();
|
|
2469
|
+
return ()=>connector.disconnectDropTarget()
|
|
2470
|
+
;
|
|
2471
|
+
}, [
|
|
2472
|
+
options
|
|
2473
|
+
]);
|
|
2474
|
+
return connector;
|
|
2475
|
+
}
|
|
2476
|
+
|
|
2477
|
+
function useDropTargetMonitor() {
|
|
2478
|
+
const manager = useDragDropManager();
|
|
2479
|
+
return useMemo(()=>new DropTargetMonitorImpl(manager)
|
|
2480
|
+
, [
|
|
2481
|
+
manager
|
|
2482
|
+
]);
|
|
2483
|
+
}
|
|
2484
|
+
|
|
2485
|
+
/**
|
|
2486
|
+
* Internal utility hook to get an array-version of spec.accept.
|
|
2487
|
+
* The main utility here is that we aren't creating a new array on every render if a non-array spec.accept is passed in.
|
|
2488
|
+
* @param spec
|
|
2489
|
+
*/ function useAccept(spec) {
|
|
2490
|
+
const { accept } = spec;
|
|
2491
|
+
return useMemo(()=>{
|
|
2492
|
+
invariant(spec.accept != null, 'accept must be defined');
|
|
2493
|
+
return Array.isArray(accept) ? accept : [
|
|
2494
|
+
accept
|
|
2495
|
+
];
|
|
2496
|
+
}, [
|
|
2497
|
+
accept
|
|
2498
|
+
]);
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
class DropTargetImpl {
|
|
2502
|
+
canDrop() {
|
|
2503
|
+
const spec = this.spec;
|
|
2504
|
+
const monitor = this.monitor;
|
|
2505
|
+
return spec.canDrop ? spec.canDrop(monitor.getItem(), monitor) : true;
|
|
2506
|
+
}
|
|
2507
|
+
hover() {
|
|
2508
|
+
const spec = this.spec;
|
|
2509
|
+
const monitor = this.monitor;
|
|
2510
|
+
if (spec.hover) {
|
|
2511
|
+
spec.hover(monitor.getItem(), monitor);
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
drop() {
|
|
2515
|
+
const spec = this.spec;
|
|
2516
|
+
const monitor = this.monitor;
|
|
2517
|
+
if (spec.drop) {
|
|
2518
|
+
return spec.drop(monitor.getItem(), monitor);
|
|
2519
|
+
}
|
|
2520
|
+
return;
|
|
2521
|
+
}
|
|
2522
|
+
constructor(spec, monitor){
|
|
2523
|
+
this.spec = spec;
|
|
2524
|
+
this.monitor = monitor;
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2527
|
+
|
|
2528
|
+
function useDropTarget(spec, monitor) {
|
|
2529
|
+
const dropTarget = useMemo(()=>new DropTargetImpl(spec, monitor)
|
|
2530
|
+
, [
|
|
2531
|
+
monitor
|
|
2532
|
+
]);
|
|
2533
|
+
useEffect(()=>{
|
|
2534
|
+
dropTarget.spec = spec;
|
|
2535
|
+
}, [
|
|
2536
|
+
spec
|
|
2537
|
+
]);
|
|
2538
|
+
return dropTarget;
|
|
2539
|
+
}
|
|
2540
|
+
|
|
2541
|
+
function useRegisteredDropTarget(spec, monitor, connector) {
|
|
2542
|
+
const manager = useDragDropManager();
|
|
2543
|
+
const dropTarget = useDropTarget(spec, monitor);
|
|
2544
|
+
const accept = useAccept(spec);
|
|
2545
|
+
useIsomorphicLayoutEffect(function registerDropTarget() {
|
|
2546
|
+
const [handlerId, unregister] = registerTarget(accept, dropTarget, manager);
|
|
2547
|
+
monitor.receiveHandlerId(handlerId);
|
|
2548
|
+
connector.receiveHandlerId(handlerId);
|
|
2549
|
+
return unregister;
|
|
2550
|
+
}, [
|
|
2551
|
+
manager,
|
|
2552
|
+
monitor,
|
|
2553
|
+
dropTarget,
|
|
2554
|
+
connector,
|
|
2555
|
+
accept.map((a)=>a.toString()
|
|
2556
|
+
).join('|'),
|
|
2557
|
+
]);
|
|
2558
|
+
}
|
|
2559
|
+
|
|
2560
|
+
/**
|
|
2561
|
+
* useDropTarget Hook
|
|
2562
|
+
* @param spec The drop target specification (object or function, function preferred)
|
|
2563
|
+
* @param deps The memoization deps array to use when evaluating spec changes
|
|
2564
|
+
*/ function useDrop(specArg, deps) {
|
|
2565
|
+
const spec = useOptionalFactory(specArg);
|
|
2566
|
+
const monitor = useDropTargetMonitor();
|
|
2567
|
+
const connector = useDropTargetConnector(spec.options);
|
|
2568
|
+
useRegisteredDropTarget(spec, monitor, connector);
|
|
2569
|
+
return [
|
|
2570
|
+
useCollectedProps(spec.collect, monitor, connector),
|
|
2571
|
+
useConnectDropTarget(connector),
|
|
2572
|
+
];
|
|
2573
|
+
}
|
|
2574
|
+
|
|
2575
|
+
var dummyEngine = new Engine();
|
|
2576
|
+
var FlowContext = React.createContext({
|
|
2577
|
+
engine: dummyEngine,
|
|
2578
|
+
});
|
|
2579
|
+
var useEngine = function () {
|
|
2580
|
+
return React.useContext(FlowContext).engine;
|
|
2581
|
+
};
|
|
2582
|
+
var useControls = function () {
|
|
2583
|
+
return React.useContext(FlowContext).controls || {};
|
|
2584
|
+
};
|
|
2585
|
+
|
|
2586
|
+
var _a;
|
|
2587
|
+
// normalizing import
|
|
2588
|
+
var InputNumber = (_a = rcin.default) !== null && _a !== void 0 ? _a : rcin;
|
|
2589
|
+
function Control(_a) {
|
|
2590
|
+
var value = _a.value, vardef = _a.vardef, onChange = _a.onChange;
|
|
2591
|
+
var customControls = useControls();
|
|
2592
|
+
if (vardef.type in customControls) {
|
|
2593
|
+
var ControlComponent = customControls[vardef.type];
|
|
2594
|
+
return (React.createElement(ControlComponent, { value: value, vardef: vardef, onChange: onChange }));
|
|
2595
|
+
}
|
|
2596
|
+
if (vardef.options) {
|
|
2597
|
+
return (React.createElement("select", { value: value || "", onChange: function (e) { return onChange === null || onChange === void 0 ? void 0 : onChange(e.target.value); } }, vardef.options.map(function (option) { return (React.createElement("option", { key: option, value: option }, option)); })));
|
|
2598
|
+
}
|
|
2599
|
+
switch (vardef.type) {
|
|
2600
|
+
case "string":
|
|
2601
|
+
return (React.createElement("input", { type: "text", value: value !== null && value !== void 0 ? value : "", onChange: function (e) { return onChange === null || onChange === void 0 ? void 0 : onChange(e.target.value); } }));
|
|
2602
|
+
case "number":
|
|
2603
|
+
return (React.createElement(InputNumber, { value: Number(value) || 0, onChange: onChange, step: vardef.step, min: vardef.min, max: vardef.max, inputMode: "numeric" }));
|
|
2604
|
+
case "boolean":
|
|
2605
|
+
return (React.createElement("input", { type: "checkbox", checked: Boolean(value), onChange: function (e) { return onChange === null || onChange === void 0 ? void 0 : onChange(e.target.checked); } }));
|
|
2606
|
+
default:
|
|
2607
|
+
return (React.createElement("span", null,
|
|
2608
|
+
String(value),
|
|
2609
|
+
" (",
|
|
2610
|
+
vardef.type,
|
|
2611
|
+
")"));
|
|
2612
|
+
}
|
|
2613
|
+
}
|
|
2614
|
+
|
|
2615
|
+
var styles$1 = {"Port":"Port_XWl7K","required":"required_ofB6J","Icon":"Icon_YJDC5","Input":"Input_PHp3D","Output":"Output_FMRNv","ConnectionPath":"ConnectionPath_XkXr1","valid":"valid_LGNvc","Edge":"Edge_aNtpH","Hover":"Hover_1v8QT"};
|
|
2616
|
+
|
|
2617
|
+
var Input = function (_a) {
|
|
2618
|
+
var _b;
|
|
2619
|
+
var name = _a.name, varDef = _a.varDef, id = _a.id, data = _a.data, onClick = _a.onClick;
|
|
2620
|
+
var normalized = normalizeVarDef(varDef);
|
|
2621
|
+
var value = (_b = data.inputs) === null || _b === void 0 ? void 0 : _b[name];
|
|
2622
|
+
var isRef = isReference(value);
|
|
2623
|
+
var handleInputChange = function (param) { return function (value) {
|
|
2624
|
+
var _a;
|
|
2625
|
+
data.onChange({
|
|
2626
|
+
inputs: __assign(__assign({}, data.inputs), (_a = {}, _a[param] = value, _a)),
|
|
2627
|
+
});
|
|
2628
|
+
}; };
|
|
2629
|
+
return (React.createElement(Handle, { type: "target", position: Position.Left, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
|
|
2630
|
+
"--type-color": "var(--color-".concat(normalized.type, ")"),
|
|
2631
|
+
}, onClick: onClick },
|
|
2632
|
+
normalized.label || name,
|
|
2633
|
+
!isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleInputChange(name) }))));
|
|
2634
|
+
};
|
|
2635
|
+
|
|
2636
|
+
var Param = function (_a) {
|
|
2637
|
+
var _b;
|
|
2638
|
+
_a.id; var name = _a.name, varDef = _a.varDef, data = _a.data, onClick = _a.onClick;
|
|
2639
|
+
var normalized = normalizeVarDef(varDef);
|
|
2640
|
+
var value = (_b = data.params) === null || _b === void 0 ? void 0 : _b[name];
|
|
2641
|
+
var isRef = isReference(value);
|
|
2642
|
+
var handleParamChange = function (param) { return function (value) {
|
|
2643
|
+
var _a;
|
|
2644
|
+
data.onChange({
|
|
2645
|
+
params: __assign(__assign({}, data.params), (_a = {}, _a[param] = value, _a)),
|
|
2646
|
+
});
|
|
2647
|
+
}; };
|
|
2648
|
+
return (React.createElement("div", { className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
|
|
2649
|
+
"--type-color": "var(--color-".concat(normalized.type, ")"),
|
|
2650
|
+
}, onClick: onClick },
|
|
2651
|
+
React.createElement("span", null, normalized.label || name),
|
|
2652
|
+
!isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleParamChange(name) }))));
|
|
2653
|
+
};
|
|
2654
|
+
|
|
2655
|
+
var Output = function (_a) {
|
|
2656
|
+
var name = _a.name, varDef = _a.varDef, id = _a.id, onClick = _a.onClick, focus = _a.focus;
|
|
2657
|
+
var normalized = normalizeVarDef(varDef);
|
|
2658
|
+
return (React.createElement(Handle, { type: "source", position: Position.Right, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Output, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
|
|
2659
|
+
"--type-color": "var(--color-".concat(normalized.type, ")"),
|
|
2660
|
+
}, onClick: onClick },
|
|
2661
|
+
React.createElement("span", null,
|
|
2662
|
+
focus && "👁 ",
|
|
2663
|
+
normalized.label || name)));
|
|
2664
|
+
};
|
|
2665
|
+
|
|
2666
|
+
var styles = {"DefaultNode":"DefaultNode_R2P6c","selected":"selected_VmB-F","active":"active_zjUzx","Log":"Log_dsq5e","Label":"Label_wBwWA","Description":"Description_f9s4l","PreviewHead":"PreviewHead_oFT4K","expanded":"expanded_3vnbi","Preview":"Preview_HbkZ6","Duration":"Duration_0jqWx","GroupHead":"GroupHead_QJhST","GroupChildren":"GroupChildren_NKx-B","GroupLeaf":"GroupLeaf_eliT8"};
|
|
2667
|
+
|
|
2668
|
+
function NodeContainer(_a) {
|
|
2669
|
+
var _b;
|
|
2670
|
+
var id = _a.id, label = _a.label, className = _a.className, selected = _a.selected, children = _a.children;
|
|
2671
|
+
var engine = useEngine();
|
|
2672
|
+
var _c = __read(React.useState(false), 2), active = _c[0], setActive = _c[1];
|
|
2673
|
+
React.useEffect(function () {
|
|
2674
|
+
if (!id)
|
|
2675
|
+
return;
|
|
2676
|
+
var timer;
|
|
2677
|
+
var removeOnCall = engine.subscribe("functionCall", function (event) {
|
|
2678
|
+
if (event.name === id) {
|
|
2679
|
+
setActive(true);
|
|
2680
|
+
}
|
|
2681
|
+
});
|
|
2682
|
+
var removeOnResult = engine.subscribe("functionResult", function (event) {
|
|
2683
|
+
if (event.name === id) {
|
|
2684
|
+
clearTimeout(timer);
|
|
2685
|
+
timer = setTimeout(function () { return setActive(false); }, Math.max(0, 1000 - (event.duration || 0)));
|
|
2686
|
+
}
|
|
2687
|
+
});
|
|
2688
|
+
return function () {
|
|
2689
|
+
removeOnCall();
|
|
2690
|
+
removeOnResult();
|
|
2691
|
+
};
|
|
2692
|
+
}, [id]);
|
|
2693
|
+
return (React.createElement("div", { className: classNames(className, styles.DefaultNode, (_b = {},
|
|
2694
|
+
_b[styles.selected] = selected,
|
|
2695
|
+
_b[styles.active] = active,
|
|
2696
|
+
_b)) },
|
|
2697
|
+
React.createElement("div", { className: styles.Label }, label),
|
|
2698
|
+
children));
|
|
2699
|
+
}
|
|
2700
|
+
|
|
2701
|
+
function createTreeStructure(tree) {
|
|
2702
|
+
var keys = Object.keys(tree);
|
|
2703
|
+
var structure = [];
|
|
2704
|
+
keys.forEach(function (key) {
|
|
2705
|
+
var path = key.split(";");
|
|
2706
|
+
var parent = structure;
|
|
2707
|
+
for (var i = 0; i < path.length; i++) {
|
|
2708
|
+
var part = parseInt(path[i], 10);
|
|
2709
|
+
if (!parent[part]) {
|
|
2710
|
+
parent[part] = [];
|
|
2711
|
+
}
|
|
2712
|
+
parent = parent[part];
|
|
2713
|
+
}
|
|
2714
|
+
parent.push({ items: tree[key] });
|
|
2715
|
+
});
|
|
2716
|
+
return structure;
|
|
2717
|
+
}
|
|
2718
|
+
function TreeBranchView(_a) {
|
|
2719
|
+
var branch = _a.branch, name = _a.name;
|
|
2720
|
+
return (React.createElement("div", null,
|
|
2721
|
+
React.createElement("div", { className: styles.GroupHead },
|
|
2722
|
+
React.createElement("span", null,
|
|
2723
|
+
"group ",
|
|
2724
|
+
name)),
|
|
2725
|
+
React.createElement("div", { className: styles.GroupChildren }, branch.map(function (item, index) {
|
|
2726
|
+
// sub branch
|
|
2727
|
+
if (Array.isArray(item)) {
|
|
2728
|
+
return (React.createElement(TreeBranchView, { key: index, branch: item, name: "".concat(name, ";").concat(index) }));
|
|
2729
|
+
}
|
|
2730
|
+
// items
|
|
2731
|
+
return (React.createElement("div", { key: index }, item.items.map(function (it, i) {
|
|
2732
|
+
var _a;
|
|
2733
|
+
var label = (_a = it === null || it === void 0 ? void 0 : it.toString) === null || _a === void 0 ? void 0 : _a.call(it);
|
|
2734
|
+
if (label === "[object Object]") {
|
|
2735
|
+
label = "{...}";
|
|
2736
|
+
}
|
|
2737
|
+
return (React.createElement("div", { key: i, className: styles.GroupLeaf, title: JSON.stringify(it, null, 2) }, label));
|
|
2738
|
+
})));
|
|
2739
|
+
}))));
|
|
2740
|
+
}
|
|
2741
|
+
function TreeView(_a) {
|
|
2742
|
+
var value = _a.value;
|
|
2743
|
+
var structure = createTreeStructure(value || {});
|
|
2744
|
+
return (React.createElement("div", null, structure.map(function (branch, index) { return (React.createElement(TreeBranchView, { key: index, branch: branch, name: index })); })));
|
|
2745
|
+
}
|
|
2746
|
+
var Log = React.memo(function (_a) {
|
|
2747
|
+
var id = _a.id, data = _a.data, selected = _a.selected;
|
|
2748
|
+
var engine = useEngine();
|
|
2749
|
+
var _b = __read(React.useState(null), 2), value = _b[0], setValue = _b[1];
|
|
2750
|
+
React.useEffect(function () {
|
|
2751
|
+
return engine.subscribe("functionResult", function (event) {
|
|
2752
|
+
var _a;
|
|
2753
|
+
if (event.name === id) {
|
|
2754
|
+
setValue(((_a = event.context.scope[id]) === null || _a === void 0 ? void 0 : _a.data) || null);
|
|
2755
|
+
}
|
|
2756
|
+
});
|
|
2757
|
+
}, []);
|
|
2758
|
+
return (React.createElement(NodeContainer, { id: id, label: data.name, selected: selected, className: styles.Log },
|
|
2759
|
+
React.createElement(TreeView, { value: value }),
|
|
2760
|
+
React.createElement(Handle, { type: "target", position: Position.Left, id: "".concat(id, "-data") }),
|
|
2761
|
+
React.createElement(Handle, { type: "source", position: Position.Right, id: "".concat(id, "-data") })));
|
|
2762
|
+
});
|
|
2763
|
+
Log.displayName = "LogNode";
|
|
2764
|
+
|
|
2765
|
+
function GenericNode(_a) {
|
|
2766
|
+
var _b;
|
|
2767
|
+
var _c;
|
|
2768
|
+
var id = _a.id, data = _a.data, selected = _a.selected, inputs = _a.inputs, outputs = _a.outputs, params = _a.params, children = _a.children;
|
|
2769
|
+
var inputEntries = Object.entries(inputs || data.inputDefs || {});
|
|
2770
|
+
var outputEntries = Object.entries(outputs || data.outputDefs || {});
|
|
2771
|
+
var paramEntries = Object.entries(params || data.paramDefs || {});
|
|
2772
|
+
var engine = useEngine();
|
|
2773
|
+
var _d = __read(React.useState({}), 2), result = _d[0], setResult = _d[1];
|
|
2774
|
+
var _e = __read(React.useState(null), 2), duration = _e[0], setDuration = _e[1];
|
|
2775
|
+
var _f = __read(React.useState(((_c = outputEntries[0]) === null || _c === void 0 ? void 0 : _c[0]) || null), 2), focus = _f[0], setFocus = _f[1];
|
|
2776
|
+
var _g = __read(React.useState(false), 2), showPreview = _g[0], setShowPreview = _g[1];
|
|
2777
|
+
var previewValue = focus ? result[focus] : Object.values(result)[0];
|
|
2778
|
+
React.useEffect(function () {
|
|
2779
|
+
return engine.subscribe("functionResult", function (event) {
|
|
2780
|
+
if (event.name === id) {
|
|
2781
|
+
setResult(event.result || {});
|
|
2782
|
+
setDuration(event.duration || null);
|
|
2783
|
+
}
|
|
2784
|
+
});
|
|
2785
|
+
}, []);
|
|
2786
|
+
return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id },
|
|
2787
|
+
React.createElement("div", { className: styles.Description }, data.description),
|
|
2788
|
+
children,
|
|
2789
|
+
React.createElement("div", { className: styles.Ports },
|
|
2790
|
+
outputEntries.map(function (_a) {
|
|
2791
|
+
var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
|
|
2792
|
+
return (React.createElement(Output, { key: name, name: name, varDef: varDef, id: id, onClick: function () { return setFocus(name); }, focus: showPreview && focus === name }));
|
|
2793
|
+
}),
|
|
2794
|
+
inputEntries.map(function (_a) {
|
|
2795
|
+
var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
|
|
2796
|
+
return (React.createElement(Input, { key: name, name: name, varDef: varDef, id: id, data: data }));
|
|
2797
|
+
}),
|
|
2798
|
+
paramEntries.map(function (_a) {
|
|
2799
|
+
var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
|
|
2800
|
+
return (React.createElement(Param, { key: name, name: name, varDef: varDef, id: id, data: data }));
|
|
2801
|
+
})),
|
|
2802
|
+
React.createElement("div", { className: classNames(styles.PreviewHead, (_b = {},
|
|
2803
|
+
_b[styles.expanded] = showPreview,
|
|
2804
|
+
_b)), onClick: function () { return setShowPreview(!showPreview); } }, "Preview"),
|
|
2805
|
+
showPreview && (React.createElement("div", { className: styles.Preview },
|
|
2806
|
+
React.createElement(TreeView, { value: previewValue }),
|
|
2807
|
+
duration && (React.createElement("div", { className: styles.Duration },
|
|
2808
|
+
duration.toFixed(2),
|
|
2809
|
+
" ms"))))));
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
var math = create(all, {});
|
|
2813
|
+
function MathView(_a) {
|
|
2814
|
+
var expr = _a.expr, _b = _a.displayMode, displayMode = _b === void 0 ? false : _b;
|
|
2815
|
+
var latex = "";
|
|
2816
|
+
try {
|
|
2817
|
+
latex = math.parse(expr).toTex({ parenthesis: "auto" });
|
|
2818
|
+
}
|
|
2819
|
+
catch (e) {
|
|
2820
|
+
latex = String(expr); // fallback on parse error
|
|
2821
|
+
}
|
|
2822
|
+
var html = katex.renderToString(latex, {
|
|
2823
|
+
throwOnError: false,
|
|
2824
|
+
displayMode: displayMode,
|
|
2825
|
+
});
|
|
2826
|
+
return React.createElement("span", { dangerouslySetInnerHTML: { __html: html } });
|
|
2827
|
+
}
|
|
2828
|
+
var Calc = React.memo(function (_a) {
|
|
2829
|
+
var _b;
|
|
2830
|
+
var id = _a.id, data = _a.data, selected = _a.selected;
|
|
2831
|
+
var expression = (((_b = data.params) === null || _b === void 0 ? void 0 : _b.expression) || "").toString();
|
|
2832
|
+
// add free variables to inputs
|
|
2833
|
+
var inputs = __assign({}, (data.inputDefs || {}));
|
|
2834
|
+
var symbols = getUnboundSymbols(expression);
|
|
2835
|
+
symbols.forEach(function (symbol) {
|
|
2836
|
+
if (!inputs[symbol]) {
|
|
2837
|
+
inputs[symbol] = {
|
|
2838
|
+
type: "number",
|
|
2839
|
+
};
|
|
2840
|
+
}
|
|
2841
|
+
});
|
|
2842
|
+
return (React.createElement(GenericNode, { id: id, data: data, selected: selected, inputs: inputs },
|
|
2843
|
+
React.createElement(MathView, { expr: expression, displayMode: true })));
|
|
2844
|
+
});
|
|
2845
|
+
Calc.displayName = "CalcNode";
|
|
2846
|
+
|
|
2847
|
+
var Default = React.memo(function (_a) {
|
|
2848
|
+
var id = _a.id, data = _a.data, selected = _a.selected;
|
|
2849
|
+
return React.createElement(GenericNode, { id: id, data: data, selected: selected });
|
|
2850
|
+
});
|
|
2851
|
+
Default.displayName = "DefaultNode";
|
|
2852
|
+
|
|
2853
|
+
function uid() {
|
|
2854
|
+
return Math.random().toString(36).substring(2, 15);
|
|
2855
|
+
}
|
|
2856
|
+
var Merge = React.memo(function (_a) {
|
|
2857
|
+
var id = _a.id, data = _a.data, selected = _a.selected;
|
|
2858
|
+
var inputs = Object.fromEntries(Object.keys(data.inputs || {}).map(function (name, i) { return [
|
|
2859
|
+
name,
|
|
2860
|
+
{ type: "any", label: "D".concat(i + 1) },
|
|
2861
|
+
]; }));
|
|
2862
|
+
var inputCount = Object.keys(inputs).length;
|
|
2863
|
+
inputs[uid()] = { type: "any", label: "D".concat(inputCount + 1) };
|
|
2864
|
+
var outputs = __assign({}, (data.outputDefs || {}));
|
|
2865
|
+
return (React.createElement(NodeContainer, { label: data.name, selected: selected, id: id },
|
|
2866
|
+
React.createElement("div", { className: styles.Ports },
|
|
2867
|
+
Object.entries(outputs || {}).map(function (_a) {
|
|
2868
|
+
var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
|
|
2869
|
+
return (React.createElement(Output, { key: name, name: name, varDef: varDef, id: id }));
|
|
2870
|
+
}),
|
|
2871
|
+
Object.entries(inputs || {}).map(function (_a) {
|
|
2872
|
+
var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
|
|
2873
|
+
return (React.createElement(Input, { key: name, name: name, varDef: varDef, id: id, data: data }));
|
|
2874
|
+
}))));
|
|
2875
|
+
});
|
|
2876
|
+
Merge.displayName = "MergeNode";
|
|
2877
|
+
|
|
2878
|
+
var nodeTypes = {
|
|
2879
|
+
Default: Default,
|
|
2880
|
+
Calc: Calc,
|
|
2881
|
+
Log: Log,
|
|
2882
|
+
Merge: Merge,
|
|
2883
|
+
};
|
|
2884
|
+
|
|
2885
|
+
var variableStore = create$1(function (set, get, api) {
|
|
2886
|
+
return persist(function (pset, pget) { return ({
|
|
2887
|
+
data: {},
|
|
2888
|
+
getVar: function (scope, name, persist) {
|
|
2889
|
+
if (persist) {
|
|
2890
|
+
return pget().data["".concat(scope, "|").concat(name)];
|
|
2891
|
+
}
|
|
2892
|
+
else {
|
|
2893
|
+
return get().data["".concat(scope, "|").concat(name)];
|
|
2894
|
+
}
|
|
2895
|
+
},
|
|
2896
|
+
setVar: function (scope, name, value, persist) {
|
|
2897
|
+
var _a, _b;
|
|
2898
|
+
var key = "".concat(scope, "|").concat(name);
|
|
2899
|
+
if (persist) {
|
|
2900
|
+
var oldData = pget().data;
|
|
2901
|
+
var newValue = typeof value === "function"
|
|
2902
|
+
? value(oldData[key])
|
|
2903
|
+
: value;
|
|
2904
|
+
var data = __assign(__assign({}, oldData), (_a = {}, _a[key] = newValue, _a));
|
|
2905
|
+
pset({ data: data });
|
|
2906
|
+
}
|
|
2907
|
+
else {
|
|
2908
|
+
var oldData = get().data;
|
|
2909
|
+
var newValue = typeof value === "function"
|
|
2910
|
+
? value(oldData[key])
|
|
2911
|
+
: value;
|
|
2912
|
+
var data = __assign(__assign({}, oldData), (_b = {}, _b[key] = newValue, _b));
|
|
2913
|
+
set({ data: data });
|
|
2914
|
+
}
|
|
2915
|
+
},
|
|
2916
|
+
}); }, {
|
|
2917
|
+
name: "variable-storage", // unique name
|
|
2918
|
+
// getStorage: () => sessionStorage, // (optional) by default the 'localStorage' is used
|
|
2919
|
+
})(set, get, api);
|
|
2920
|
+
});
|
|
2921
|
+
/**
|
|
2922
|
+
* scope restricts the variable scope, for example to a view id
|
|
2923
|
+
* @param scope
|
|
2924
|
+
* @param name
|
|
2925
|
+
*/
|
|
2926
|
+
var useVariable = function (scope, name, initial, persist) {
|
|
2927
|
+
if (persist === void 0) { persist = true; }
|
|
2928
|
+
var value = variableStore(function (_a) {
|
|
2929
|
+
var getVar = _a.getVar;
|
|
2930
|
+
return getVar(scope, name, persist);
|
|
2931
|
+
});
|
|
2932
|
+
if (typeof value === "undefined")
|
|
2933
|
+
value = initial;
|
|
2934
|
+
var setVar = variableStore(function (_a) {
|
|
2935
|
+
var setVar = _a.setVar;
|
|
2936
|
+
return setVar;
|
|
2937
|
+
});
|
|
2938
|
+
var setValue = function (value) { return setVar(scope, name, value, persist); };
|
|
2939
|
+
return [value, setValue];
|
|
2940
|
+
};
|
|
2941
|
+
|
|
2942
|
+
function usePositions(fn) {
|
|
2943
|
+
return useVariable("rules", "".concat(fn.name, ".positions"), {});
|
|
2944
|
+
}
|
|
2945
|
+
function useUpdatePositions(fn) {
|
|
2946
|
+
var _a = __read(usePositions(fn), 2), positions = _a[0], setPositions = _a[1];
|
|
2947
|
+
return React.useCallback(function (id, position) {
|
|
2948
|
+
positions[id] = position;
|
|
2949
|
+
setPositions(positions);
|
|
2950
|
+
}, [positions, setPositions]);
|
|
2951
|
+
}
|
|
2952
|
+
|
|
2953
|
+
// helpers
|
|
2954
|
+
function updateNodeArguments(node, inputs, params) {
|
|
2955
|
+
return __assign(__assign({}, node), { data: __assign(__assign({}, node.data), { inputs: inputs, params: params }) });
|
|
2956
|
+
}
|
|
2957
|
+
/**
|
|
2958
|
+
* flow state stores nodes and edges for the flow graph these can be updated when the function changes
|
|
2959
|
+
* using setFn
|
|
2960
|
+
*/
|
|
2961
|
+
var useFlowState = create$1(function (set, get) { return ({
|
|
2962
|
+
engine: null,
|
|
2963
|
+
fn: null,
|
|
2964
|
+
options: {},
|
|
2965
|
+
nodes: [],
|
|
2966
|
+
edges: [],
|
|
2967
|
+
setNodes: function (updater) {
|
|
2968
|
+
set(function (state) { return ({ nodes: updater(state.nodes) }); });
|
|
2969
|
+
},
|
|
2970
|
+
setEdges: function (updater) {
|
|
2971
|
+
set(function (state) { return ({ edges: updater(state.edges) }); });
|
|
2972
|
+
},
|
|
2973
|
+
setFn: function (fn) {
|
|
2974
|
+
// console.log("setFn", fn);
|
|
2975
|
+
var _a = get(), oldFn = _a.fn, nodes = _a.nodes, edges = _a.edges, engine = _a.engine, options = _a.options;
|
|
2976
|
+
// update the graph by shallow comparison
|
|
2977
|
+
if (!engine) {
|
|
2978
|
+
throw new Error("Engine not set");
|
|
2979
|
+
}
|
|
2980
|
+
var oldKeys = Object.keys((oldFn === null || oldFn === void 0 ? void 0 : oldFn.body) || {});
|
|
2981
|
+
var newKeys = Object.keys(fn.body || {});
|
|
2982
|
+
// remove all the nodes that were removed
|
|
2983
|
+
nodes = nodes === null || nodes === void 0 ? void 0 : nodes.filter(function (node) { return newKeys.includes(node.id); });
|
|
2984
|
+
// remove all the edges that were removed
|
|
2985
|
+
edges = edges === null || edges === void 0 ? void 0 : edges.filter(function (edge) {
|
|
2986
|
+
return (newKeys.includes(edge.source) && newKeys.includes(edge.target));
|
|
2987
|
+
});
|
|
2988
|
+
// create the new nodes that were added
|
|
2989
|
+
var addedKeys = newKeys.filter(function (key) { return !oldKeys.includes(key); });
|
|
2990
|
+
addedKeys.forEach(function (key, index) {
|
|
2991
|
+
var _a;
|
|
2992
|
+
var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
|
|
2993
|
+
// add the node
|
|
2994
|
+
nodes.push(createNode(engine, key, fncall, options));
|
|
2995
|
+
// add edges
|
|
2996
|
+
var refs = getReferences(fncall.inputs || {});
|
|
2997
|
+
refs.forEach(function (ref) {
|
|
2998
|
+
edges.push(createEdge(key, ref));
|
|
2999
|
+
});
|
|
3000
|
+
});
|
|
3001
|
+
// update the changed nodes
|
|
3002
|
+
var changedKeys = newKeys.filter(function (key) {
|
|
3003
|
+
var _a, _b;
|
|
3004
|
+
// shallow comparison
|
|
3005
|
+
return ((_a = fn.body) === null || _a === void 0 ? void 0 : _a[key]) !== ((_b = oldFn === null || oldFn === void 0 ? void 0 : oldFn.body) === null || _b === void 0 ? void 0 : _b[key]);
|
|
3006
|
+
});
|
|
3007
|
+
// console.log("changed keys", changedKeys);
|
|
3008
|
+
changedKeys.forEach(function (key) {
|
|
3009
|
+
var _a;
|
|
3010
|
+
// update the nodes that were changed
|
|
3011
|
+
var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
|
|
3012
|
+
nodes = nodes === null || nodes === void 0 ? void 0 : nodes.map(function (n) {
|
|
3013
|
+
if (n.id === key) {
|
|
3014
|
+
return updateNodeArguments(n, fncall.inputs, fncall.params);
|
|
3015
|
+
}
|
|
3016
|
+
return n;
|
|
3017
|
+
});
|
|
3018
|
+
// update incoming edges
|
|
3019
|
+
var refs = getReferences(fncall.inputs || {});
|
|
3020
|
+
// remove all incoming edges
|
|
3021
|
+
edges = edges === null || edges === void 0 ? void 0 : edges.filter(function (e) { return e.target !== key; });
|
|
3022
|
+
// add the edge
|
|
3023
|
+
refs.forEach(function (ref) {
|
|
3024
|
+
edges.push(createEdge(key, ref));
|
|
3025
|
+
});
|
|
3026
|
+
});
|
|
3027
|
+
// console.log("final graph", nodes, edges);
|
|
3028
|
+
// update the function, nodes and edges
|
|
3029
|
+
set({ fn: fn, nodes: nodes, edges: edges });
|
|
3030
|
+
},
|
|
3031
|
+
init: function (fn, engine, options) {
|
|
3032
|
+
// console.log("initing");
|
|
3033
|
+
set({ engine: engine, options: options });
|
|
3034
|
+
get().setFn(fn);
|
|
3035
|
+
},
|
|
3036
|
+
}); });
|
|
3037
|
+
/**
|
|
3038
|
+
* creates a flow state and provides handlers to update nodes and edges
|
|
3039
|
+
*/
|
|
3040
|
+
function useFlow(fn, engine, options) {
|
|
3041
|
+
var state = useFlowState();
|
|
3042
|
+
React.useEffect(function () {
|
|
3043
|
+
state.init(fn, engine, options);
|
|
3044
|
+
}, [engine]);
|
|
3045
|
+
React.useEffect(function () {
|
|
3046
|
+
state.setFn(fn);
|
|
3047
|
+
}, [fn]);
|
|
3048
|
+
return React.useMemo(function () { return ({
|
|
3049
|
+
nodes: state.nodes,
|
|
3050
|
+
edges: state.edges,
|
|
3051
|
+
setNodes: state.setNodes,
|
|
3052
|
+
setEdges: state.setEdges,
|
|
3053
|
+
}); }, [state]);
|
|
3054
|
+
}
|
|
3055
|
+
function createNode(engine, id, fncall, options) {
|
|
3056
|
+
var _a, _b;
|
|
3057
|
+
var calledFn = engine.getFunction(fncall.name);
|
|
3058
|
+
// create a node for each function call
|
|
3059
|
+
return {
|
|
3060
|
+
id: id,
|
|
3061
|
+
data: {
|
|
3062
|
+
name: fncall.name,
|
|
3063
|
+
label: fncall.label || (calledFn === null || calledFn === void 0 ? void 0 : calledFn.label) || fncall.name,
|
|
3064
|
+
description: calledFn === null || calledFn === void 0 ? void 0 : calledFn.description,
|
|
3065
|
+
inputs: fncall.inputs,
|
|
3066
|
+
params: fncall.params,
|
|
3067
|
+
inputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs) || {},
|
|
3068
|
+
paramDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.params) || {},
|
|
3069
|
+
outputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) || {},
|
|
3070
|
+
onChange: ((_a = options.onDataChange) === null || _a === void 0 ? void 0 : _a.call(options, id)) || (function () { }),
|
|
3071
|
+
},
|
|
3072
|
+
position: ((_b = options.positions) === null || _b === void 0 ? void 0 : _b[id]) || { x: 0, y: 0 },
|
|
3073
|
+
dragHandle: options.dragHandle,
|
|
3074
|
+
type: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.nodeType) || "Default",
|
|
3075
|
+
};
|
|
3076
|
+
}
|
|
3077
|
+
function createEdge(nodeId, ref) {
|
|
3078
|
+
return {
|
|
3079
|
+
id: "".concat(ref.path, "-").concat(nodeId, ".").concat(ref.name),
|
|
3080
|
+
source: ref.refNode,
|
|
3081
|
+
target: nodeId,
|
|
3082
|
+
sourceHandle: "".concat(ref.refNode, "-").concat(ref.refField),
|
|
3083
|
+
targetHandle: "".concat(nodeId, "-").concat(ref.name),
|
|
3084
|
+
};
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
var ConnectionLine = (function (_a) {
|
|
3088
|
+
var _b;
|
|
3089
|
+
var fromX = _a.fromX, fromY = _a.fromY, toX = _a.toX, toY = _a.toY, fromHandle = _a.fromHandle, fromNode = _a.fromNode, connectionStatus = _a.connectionStatus;
|
|
3090
|
+
var _c = useConnection(), toNode = _c.toNode, toHandle = _c.toHandle;
|
|
3091
|
+
// correcting the start point as we moved the nodes in our custom ports
|
|
3092
|
+
if (fromNode && fromHandle) {
|
|
3093
|
+
fromX = (fromNode === null || fromNode === void 0 ? void 0 : fromNode.position.x) + fromHandle.x;
|
|
3094
|
+
if (fromHandle.position === "right") {
|
|
3095
|
+
fromX += fromHandle.width;
|
|
3096
|
+
}
|
|
3097
|
+
}
|
|
3098
|
+
if (toNode && toHandle) {
|
|
3099
|
+
toX = toHandle.x - toHandle.width / 2; // align to left side of the handle
|
|
3100
|
+
if (toHandle.position === "right") {
|
|
3101
|
+
toX += toHandle.width;
|
|
3102
|
+
}
|
|
3103
|
+
}
|
|
3104
|
+
var d = getSimpleBezierPath({
|
|
3105
|
+
sourceX: fromX,
|
|
3106
|
+
sourceY: fromY,
|
|
3107
|
+
targetX: toX,
|
|
3108
|
+
targetY: toY,
|
|
3109
|
+
sourcePosition: Position.Right,
|
|
3110
|
+
targetPosition: Position.Left,
|
|
3111
|
+
});
|
|
3112
|
+
return (React.createElement("g", { className: classNames(styles$1.ConnectionPath, "react-flow__connection-path", (_b = {}, _b[styles$1.valid] = connectionStatus === "valid", _b)) },
|
|
3113
|
+
React.createElement("path", { d: d[0] })));
|
|
3114
|
+
});
|
|
3115
|
+
|
|
3116
|
+
function FlowInner(_a) {
|
|
3117
|
+
var fn = _a.function, engine = _a.engine, onChange = _a.onChange, onClick = _a.onClick, onSelect = _a.onSelect;
|
|
3118
|
+
var _b = __read(React.useState(null), 2), instance = _b[0], setInstance = _b[1];
|
|
3119
|
+
var updatePosition = useUpdatePositions(fn);
|
|
3120
|
+
var _c = __read(usePositions(fn), 1), positions = _c[0];
|
|
3121
|
+
var handleDataChange = function (id) { return function (newData) {
|
|
3122
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
|
|
3123
|
+
// update the function definition
|
|
3124
|
+
// console.log("before fn call update", Object.keys(fn.body));
|
|
3125
|
+
var body = __assign({}, fn.body);
|
|
3126
|
+
var fncall = __assign(__assign({}, (body[id] || {})), newData);
|
|
3127
|
+
body[id] = fncall;
|
|
3128
|
+
// console.log("after fn call update", Object.keys(body));
|
|
3129
|
+
var updatedFn = __assign(__assign({}, fn), { body: body });
|
|
3130
|
+
return updatedFn;
|
|
3131
|
+
});
|
|
3132
|
+
}; };
|
|
3133
|
+
var _d = useFlow(fn, engine, {
|
|
3134
|
+
dragHandle: ".".concat(styles.Label),
|
|
3135
|
+
positions: positions,
|
|
3136
|
+
onDataChange: handleDataChange,
|
|
3137
|
+
}), nodes = _d.nodes, edges = _d.edges, setNodes = _d.setNodes, setEdges = _d.setEdges;
|
|
3138
|
+
// node selection handler
|
|
3139
|
+
var handleSelect = React.useCallback(function (_a) {
|
|
3140
|
+
var nodes = _a.nodes;
|
|
3141
|
+
onSelect === null || onSelect === void 0 ? void 0 : onSelect(nodes.map(function (n) { return n.id; }) || []);
|
|
3142
|
+
}, [onSelect]);
|
|
3143
|
+
useOnSelectionChange({
|
|
3144
|
+
onChange: handleSelect,
|
|
3145
|
+
});
|
|
3146
|
+
// dropping nodes
|
|
3147
|
+
var _e = __read(useDrop({
|
|
3148
|
+
accept: "flow-node",
|
|
3149
|
+
drop: function (item, monitor) {
|
|
3150
|
+
var pos = monitor.getClientOffset();
|
|
3151
|
+
if (instance && pos) {
|
|
3152
|
+
var position_1 = instance.screenToFlowPosition(pos);
|
|
3153
|
+
// console.log("handle drop", position);
|
|
3154
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
|
|
3155
|
+
return engine.applyNodeAdd(fn, item.name, function (fnCall, id) {
|
|
3156
|
+
// console.log("node added", id, position);
|
|
3157
|
+
updatePosition(id, position_1);
|
|
3158
|
+
});
|
|
3159
|
+
});
|
|
3160
|
+
}
|
|
3161
|
+
},
|
|
3162
|
+
collect: function (monitor) {
|
|
3163
|
+
return {
|
|
3164
|
+
isOver: monitor.isOver(),
|
|
3165
|
+
};
|
|
3166
|
+
},
|
|
3167
|
+
}), 2); _e[0].isOver; var drop = _e[1];
|
|
3168
|
+
// connecting nodes
|
|
3169
|
+
var onConnect = React.useCallback(function (params) {
|
|
3170
|
+
var _a, _b;
|
|
3171
|
+
var sourceField = (_a = params.sourceHandle) === null || _a === void 0 ? void 0 : _a.split("-")[1];
|
|
3172
|
+
var targetField = (_b = params.targetHandle) === null || _b === void 0 ? void 0 : _b.split("-")[1];
|
|
3173
|
+
if (!sourceField || !targetField) {
|
|
3174
|
+
console.warn("Invalid connection handles", params);
|
|
3175
|
+
return;
|
|
3176
|
+
}
|
|
3177
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
|
|
3178
|
+
return engine.applyNodeConnect(fn, params.source, params.target, sourceField, targetField);
|
|
3179
|
+
});
|
|
3180
|
+
}, [fn, onChange]);
|
|
3181
|
+
// deleting nodes
|
|
3182
|
+
var onNodesDelete = React.useCallback(function (nodesToDelete) {
|
|
3183
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
|
|
3184
|
+
return engine.applyNodeDelete(fn, nodesToDelete.map(function (n) { return n.id; }));
|
|
3185
|
+
});
|
|
3186
|
+
}, [onChange, fn]);
|
|
3187
|
+
// deleting edges
|
|
3188
|
+
var onEdgesDelete = React.useCallback(function (edgesToDelete) {
|
|
3189
|
+
onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
|
|
3190
|
+
var newFn = fn;
|
|
3191
|
+
edgesToDelete.forEach(function (edge) {
|
|
3192
|
+
var _a, _b;
|
|
3193
|
+
var sourceField = (_a = edge.sourceHandle) === null || _a === void 0 ? void 0 : _a.split("-")[1];
|
|
3194
|
+
var targetField = (_b = edge.targetHandle) === null || _b === void 0 ? void 0 : _b.split("-")[1];
|
|
3195
|
+
if (!sourceField || !targetField) {
|
|
3196
|
+
console.warn("Invalid connection handles", edge);
|
|
3197
|
+
return;
|
|
3198
|
+
}
|
|
3199
|
+
newFn = engine.applyEdgeDelete(newFn, edge.source, edge.target, sourceField, targetField);
|
|
3200
|
+
});
|
|
3201
|
+
return newFn;
|
|
3202
|
+
});
|
|
3203
|
+
}, [onChange, fn]);
|
|
3204
|
+
// handle canvas clicks to be able to place nodes
|
|
3205
|
+
var handleClick = function (e) {
|
|
3206
|
+
if (instance) {
|
|
3207
|
+
var position = instance.screenToFlowPosition({
|
|
3208
|
+
x: e.clientX,
|
|
3209
|
+
y: e.clientY,
|
|
3210
|
+
});
|
|
3211
|
+
onClick === null || onClick === void 0 ? void 0 : onClick(e, position);
|
|
3212
|
+
}
|
|
3213
|
+
};
|
|
3214
|
+
var onNodesChange = React.useCallback(function (changes) {
|
|
3215
|
+
return setNodes(function (nodesSnapshot) {
|
|
3216
|
+
return applyNodeChanges(changes, nodesSnapshot);
|
|
3217
|
+
});
|
|
3218
|
+
}, []);
|
|
3219
|
+
var onEdgesChange = React.useCallback(function (changes) {
|
|
3220
|
+
return setEdges(function (edgesSnapshot) {
|
|
3221
|
+
return applyEdgeChanges(changes, edgesSnapshot);
|
|
3222
|
+
});
|
|
3223
|
+
}, []);
|
|
3224
|
+
return (React.createElement(ReactFlow, { ref: drop, nodes: nodes, edges: edges, nodeTypes: nodeTypes, onNodesChange: onNodesChange, onEdgesChange: onEdgesChange, onNodesDelete: onNodesDelete, onEdgesDelete: onEdgesDelete, onNodeDragStop: function (e, n) { return updatePosition(n.id, n.position); }, onConnect: onConnect, onInit: setInstance, onPaneClick: handleClick, fitView: true,
|
|
3225
|
+
// panOnDrag={false}
|
|
3226
|
+
selectionOnDrag: true, minZoom: 0.1, maxZoom: 4, deleteKeyCode: "Delete", connectionLineComponent: ConnectionLine },
|
|
3227
|
+
React.createElement(Background, { variant: BackgroundVariant.Dots }),
|
|
3228
|
+
React.createElement(Controls, null)));
|
|
3229
|
+
}
|
|
3230
|
+
function Flow(_a) {
|
|
3231
|
+
var fn = _a.function, engine = _a.engine, _b = _a.onChange, onChange = _b === void 0 ? function () { } : _b, onClick = _a.onClick, onSelect = _a.onSelect, customControls = _a.customControls;
|
|
3232
|
+
return (React.createElement(FlowContext.Provider, { value: { engine: engine, controls: customControls } },
|
|
3233
|
+
React.createElement(ReactFlowProvider, null,
|
|
3234
|
+
React.createElement(FlowInner, { function: fn, engine: engine, onChange: onChange, onClick: onClick, onSelect: onSelect }))));
|
|
3235
|
+
}
|
|
3236
|
+
|
|
3237
|
+
function useDraggableNode(name, fn) {
|
|
3238
|
+
var _a = __read(useDrag({
|
|
3239
|
+
type: "flow-node",
|
|
3240
|
+
item: { name: name, fn: fn },
|
|
3241
|
+
collect: function (monitor) { return ({
|
|
3242
|
+
isDragging: monitor.isDragging(),
|
|
3243
|
+
}); },
|
|
3244
|
+
}), 2), drag = _a[1];
|
|
3245
|
+
return function (ref) { return drag(ref); };
|
|
3246
|
+
}
|
|
3247
|
+
|
|
3248
|
+
/**
|
|
3249
|
+
* using functions
|
|
3250
|
+
*
|
|
3251
|
+
* in React context
|
|
3252
|
+
* - use useFunction. This will build the function, mount it if needed and provides a run function.
|
|
3253
|
+
*
|
|
3254
|
+
*
|
|
3255
|
+
* in Vanilla context
|
|
3256
|
+
* - use engine.build(fn) to build the function
|
|
3257
|
+
* - use engine.mount(fn) to mount the function (if it has a mount function)
|
|
3258
|
+
* - use await engine.run(fn, inputs) to run the function and get the result
|
|
3259
|
+
*/
|
|
3260
|
+
function useFunction(engine, fn, mount) {
|
|
3261
|
+
var _this = this;
|
|
3262
|
+
var _a = __read(React.useState(null), 2), result = _a[0], setResult = _a[1];
|
|
3263
|
+
var builtFn = React.useMemo(function () { return engine.build(fn); }, [fn]);
|
|
3264
|
+
React.useEffect(function () {
|
|
3265
|
+
if (mount) {
|
|
3266
|
+
return engine.mount(builtFn);
|
|
3267
|
+
}
|
|
3268
|
+
}, [builtFn, mount]);
|
|
3269
|
+
var run = React.useCallback(function (inputs) { return __awaiter(_this, void 0, void 0, function () {
|
|
3270
|
+
var result;
|
|
3271
|
+
return __generator(this, function (_a) {
|
|
3272
|
+
switch (_a.label) {
|
|
3273
|
+
case 0: return [4 /*yield*/, engine.run(builtFn, inputs)];
|
|
3274
|
+
case 1:
|
|
3275
|
+
result = _a.sent();
|
|
3276
|
+
setResult(result);
|
|
3277
|
+
return [2 /*return*/, result];
|
|
3278
|
+
}
|
|
3279
|
+
});
|
|
3280
|
+
}); }, [builtFn]);
|
|
3281
|
+
return React.useMemo(function () { return ({
|
|
3282
|
+
fn: builtFn,
|
|
3283
|
+
run: run,
|
|
3284
|
+
result: result,
|
|
3285
|
+
}); }, [builtFn, run, result]);
|
|
3286
|
+
}
|
|
3287
|
+
|
|
3288
|
+
export { DISCARD, Engine, Flow, Lib, binaryOnTree, binaryOnTreeBranch, broadCast, expandTree, getBranch, getReferences, getValue$1 as getValue, graftTree, interpolate, isReference, isSingleTon, isTree, mapTree, mapTreeBranch, nAryOnTree, nAryOnTreeBranch, normalizeVarDef, parseReference, primitives, sameShape, simplifyTree, toArray, topSort, treeSize, trimTree, uid$1 as uid, useDraggableNode, useFlow, useFunction, usePositions, useUpdatePositions, useVariable };
|
|
3289
|
+
//# sourceMappingURL=index.esm.js.map
|