@rkmodules/rules 0.0.71 → 0.0.72

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.js CHANGED
@@ -1547,8 +1547,36 @@ var primitives$1 = (_a$1 = {},
1547
1547
  _a$1[groupOr.name] = groupOr,
1548
1548
  _a$1);
1549
1549
 
1550
+ var output = {
1551
+ name: "output",
1552
+ label: "Output",
1553
+ description: "Data output.",
1554
+ nodeType: "Output",
1555
+ inputs: {},
1556
+ outputs: {},
1557
+ impl: function () { return __awaiter(void 0, void 0, void 0, function () {
1558
+ return __generator(this, function (_a) {
1559
+ return [2 /*return*/, {}];
1560
+ });
1561
+ }); },
1562
+ };
1563
+
1564
+ var inputs = {
1565
+ name: "inputs",
1566
+ label: "Input",
1567
+ description: "Data input.",
1568
+ nodeType: "Input",
1569
+ inputs: {},
1570
+ outputs: {},
1571
+ impl: function () { return __awaiter(void 0, void 0, void 0, function () {
1572
+ return __generator(this, function (_a) {
1573
+ return [2 /*return*/, {}];
1574
+ });
1575
+ }); },
1576
+ };
1577
+
1550
1578
  var Lib = { Util: primitives$6, Math: primitives$5, List: primitives$4, Sequence: primitives$3, Tree: primitives$2, Logic: primitives$1 };
1551
- var primitives = __assign(__assign(__assign(__assign(__assign(__assign({}, primitives$6), primitives$5), primitives$4), primitives$3), primitives$2), primitives$1);
1579
+ var primitives = __assign(__assign(__assign(__assign(__assign(__assign({ inputs: inputs, output: output }, primitives$6), primitives$5), primitives$4), primitives$3), primitives$2), primitives$1);
1552
1580
 
1553
1581
  function isPrimitive(node) {
1554
1582
  return node.impl !== undefined;
@@ -1647,6 +1675,14 @@ var Engine = /** @class */ (function () {
1647
1675
  case 0:
1648
1676
  _a = node.body, body = _a === void 0 ? {} : _a;
1649
1677
  executionId = uid$1();
1678
+ // add input defaults
1679
+ Object.entries(node.inputs || {}).forEach(function (_a) {
1680
+ var _b = __read(_a, 2), name = _b[0], def = _b[1];
1681
+ var normDef = normalizeVarDef(def);
1682
+ if (!inputs[name] && normDef.default !== undefined) {
1683
+ inputs[name] = normDef.default;
1684
+ }
1685
+ });
1650
1686
  context = {
1651
1687
  scope: { inputs: inputs },
1652
1688
  timings: {},
@@ -1862,6 +1898,13 @@ var Engine = /** @class */ (function () {
1862
1898
  // node handlers
1863
1899
  Engine.prototype.applyNodeDelete = function (oldFn, nodeIds) {
1864
1900
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1901
+ if (nodeIds.every(function (id) { return id === "output"; })) {
1902
+ // cannot delete output node
1903
+ return oldFn;
1904
+ }
1905
+ if (nodeIds.includes("inputs")) {
1906
+ delete newFn.inputs;
1907
+ }
1865
1908
  nodeIds.forEach(function (id) {
1866
1909
  delete newFn.body[id];
1867
1910
  });
@@ -1885,10 +1928,13 @@ var Engine = /** @class */ (function () {
1885
1928
  Engine.prototype.applyEdgeDelete = function (oldFn, fromNode, toNode, fromField, toField) {
1886
1929
  var _a;
1887
1930
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1888
- var from = newFn.body[fromNode];
1931
+ if (toNode === "output") {
1932
+ // no deleting of output edges
1933
+ return oldFn;
1934
+ }
1889
1935
  var to = newFn.body[toNode];
1890
1936
  var fn = this.fnIndex[to === null || to === void 0 ? void 0 : to.name];
1891
- if (!from || !to) {
1937
+ if (!to) {
1892
1938
  console.warn("Invalid nodes for edge deletion");
1893
1939
  return newFn;
1894
1940
  }
@@ -1909,6 +1955,11 @@ var Engine = /** @class */ (function () {
1909
1955
  };
1910
1956
  Engine.prototype.applyNodeAdd = function (oldFn, nodeName, callback) {
1911
1957
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1958
+ if (nodeName === "inputs") {
1959
+ // ensure inputs node
1960
+ newFn.inputs = newFn.inputs || {};
1961
+ return newFn;
1962
+ }
1912
1963
  var id = uid$1();
1913
1964
  // ensure unique id
1914
1965
  while (newFn.body[id]) {
@@ -1942,10 +1993,16 @@ var Engine = /** @class */ (function () {
1942
1993
  return topSort(newFn);
1943
1994
  };
1944
1995
  Engine.prototype.applyNodeConnect = function (oldFn, fromNode, toNode, fromField, toField) {
1996
+ var _a;
1945
1997
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1946
- var from = newFn.body[fromNode];
1998
+ if (toNode === "output") {
1999
+ newFn.outputs = __assign(__assign({}, oldFn.outputs), (_a = {}, _a[toField] = "<".concat(fromNode, ".").concat(fromField, ">"), _a));
2000
+ // no need to sort, as only output has changed
2001
+ return newFn;
2002
+ }
2003
+ // const from = newFn.body[fromNode];
1947
2004
  var to = __assign({}, newFn.body[toNode]);
1948
- if (!from || !to) {
2005
+ if (!to) {
1949
2006
  console.warn("Invalid nodes for connection");
1950
2007
  return newFn;
1951
2008
  }
@@ -4727,6 +4784,7 @@ function useRegisteredDropTarget(spec, monitor, connector) {
4727
4784
 
4728
4785
  var dummyEngine = new Engine();
4729
4786
  var FlowContext = React.createContext({
4787
+ graphedFn: {},
4730
4788
  engine: dummyEngine,
4731
4789
  });
4732
4790
  var useEngine = function () {
@@ -4736,6 +4794,9 @@ var empty = {};
4736
4794
  var useControls = function () {
4737
4795
  return React.useContext(FlowContext).controls || empty;
4738
4796
  };
4797
+ var useGraphedFunction = function () {
4798
+ return React.useContext(FlowContext).graphedFn;
4799
+ };
4739
4800
 
4740
4801
  var _a;
4741
4802
  // normalizing import
@@ -4770,7 +4831,7 @@ function Control(_a) {
4770
4831
  }
4771
4832
  }
4772
4833
 
4773
- 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"};
4834
+ var styles$1 = {"Port":"Port_XWl7K","required":"required_ofB6J","Icon":"Icon_YJDC5","PortLabel":"PortLabel_wwLN0","Input":"Input_PHp3D","Output":"Output_FMRNv","ConnectionPath":"ConnectionPath_XkXr1","valid":"valid_LGNvc","Edge":"Edge_aNtpH","Hover":"Hover_1v8QT","Menu":"Menu_cusza","MenuCanvas":"MenuCanvas_3M2Ek","MenuItem":"MenuItem_UWLzD"};
4774
4835
 
4775
4836
  var Input = function (_a) {
4776
4837
  var _b;
@@ -4787,7 +4848,7 @@ var Input = function (_a) {
4787
4848
  return (React.createElement(react.Handle, { type: "target", position: react.Position.Left, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4788
4849
  "--type-color": "var(--color-".concat(normalized.type, ")"),
4789
4850
  }, onClick: onClick },
4790
- normalized.label || name,
4851
+ React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name),
4791
4852
  !isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleInputChange(name) }))));
4792
4853
  };
4793
4854
 
@@ -4803,10 +4864,10 @@ var Param = function (_a) {
4803
4864
  params: __assign(__assign({}, data.params), (_a = {}, _a[param] = value, _a)),
4804
4865
  });
4805
4866
  }; };
4806
- return (React.createElement("div", { className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4867
+ return (React.createElement("div", { className: classNames(styles$1.Param, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4807
4868
  "--type-color": "var(--color-".concat(normalized.type, ")"),
4808
4869
  }, onClick: onClick },
4809
- React.createElement("span", null, normalized.label || name),
4870
+ React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name),
4810
4871
  !isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleParamChange(name) }))));
4811
4872
  };
4812
4873
 
@@ -4816,16 +4877,16 @@ var Output = function (_a) {
4816
4877
  return (React.createElement(react.Handle, { type: "source", position: react.Position.Right, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Output, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4817
4878
  "--type-color": "var(--color-".concat(normalized.type, ")"),
4818
4879
  }, onClick: onClick },
4819
- React.createElement("span", null,
4880
+ React.createElement("span", { className: styles$1.PortLabel },
4820
4881
  focus && "👁 ",
4821
4882
  normalized.label || name)));
4822
4883
  };
4823
4884
 
4824
- var styles = {"DefaultNode":"DefaultNode_R2P6c","selected":"selected_VmB-F","active":"active_zjUzx","Log":"Log_dsq5e","Label":"Label_wBwWA","DeleteButton":"DeleteButton_kglTP","Description":"Description_f9s4l","PreviewHead":"PreviewHead_oFT4K","expanded":"expanded_3vnbi","Preview":"Preview_HbkZ6","Duration":"Duration_0jqWx","GroupHead":"GroupHead_QJhST","GroupName":"GroupName_Qvu4r","GroupCnt":"GroupCnt_3ctdd","GroupMore":"GroupMore_D1Kiz","GroupChildren":"GroupChildren_NKx-B","GroupLeaf":"GroupLeaf_eliT8"};
4885
+ var styles = {"NodeContainer":"NodeContainer_gSdHY","selected":"selected_VmB-F","active":"active_zjUzx","Log":"Log_dsq5e","Label":"Label_wBwWA","Description":"Description_f9s4l","Body":"Body_ptEZP","PreviewHead":"PreviewHead_oFT4K","expanded":"expanded_3vnbi","Preview":"Preview_HbkZ6","Duration":"Duration_0jqWx","GroupHead":"GroupHead_QJhST","GroupName":"GroupName_Qvu4r","GroupCnt":"GroupCnt_3ctdd","GroupMore":"GroupMore_D1Kiz","GroupChildren":"GroupChildren_NKx-B","GroupLeaf":"GroupLeaf_eliT8"};
4825
4886
 
4826
4887
  function NodeContainer(_a) {
4827
4888
  var _b;
4828
- var id = _a.id, label = _a.label, className = _a.className, selected = _a.selected, children = _a.children;
4889
+ var id = _a.id, label = _a.label, className = _a.className, selected = _a.selected, children = _a.children, noDelete = _a.noDelete;
4829
4890
  var engine = useEngine();
4830
4891
  var _c = __read(React.useState(false), 2), active = _c[0], setActive = _c[1];
4831
4892
  var _d = __read(React.useState(false), 2), deleteMode = _d[0], setDeleteMode = _d[1];
@@ -4856,12 +4917,14 @@ function NodeContainer(_a) {
4856
4917
  };
4857
4918
  }, [id]);
4858
4919
  var handlers = useLongPress.useLongPress(function () {
4859
- setDeleteMode(true);
4920
+ if (!noDelete) {
4921
+ setDeleteMode(true);
4922
+ }
4860
4923
  });
4861
4924
  var handleDelete = function () {
4862
4925
  instance.deleteElements({ nodes: [{ id: id }] });
4863
4926
  };
4864
- return (React.createElement("div", { className: classNames(className, styles.DefaultNode, (_b = {},
4927
+ return (React.createElement("div", { className: classNames(className, styles.NodeContainer, (_b = {},
4865
4928
  _b[styles.selected] = selected,
4866
4929
  _b[styles.active] = active,
4867
4930
  _b)) },
@@ -5044,6 +5107,118 @@ var Default = React.memo(function (_a) {
5044
5107
  });
5045
5108
  Default.displayName = "DefaultNode";
5046
5109
 
5110
+ function Menu(_a) {
5111
+ var handle = _a.handle, children = _a.children;
5112
+ var _b = __read(React.useState(false), 2), open = _b[0], setOpen = _b[1];
5113
+ var handleOpen = function () {
5114
+ setOpen(!open);
5115
+ };
5116
+ var handleClose = function () {
5117
+ setOpen(false);
5118
+ };
5119
+ return (React.createElement("div", { className: styles$1.Menu },
5120
+ React.createElement("div", { onClick: handleOpen }, handle),
5121
+ open && (React.createElement("div", { className: styles$1.MenuCanvas, onClick: handleClose }, children))));
5122
+ }
5123
+ function MenuItem(_a) {
5124
+ var children = _a.children, props = __rest(_a, ["children"]);
5125
+ return (React.createElement("div", __assign({ className: styles$1.MenuItem }, props), children));
5126
+ }
5127
+
5128
+ /**
5129
+ * todo: get input data from graphed function
5130
+ */
5131
+ var InputNode = React.memo(function (_a) {
5132
+ var id = _a.id, data = _a.data, selected = _a.selected;
5133
+ var fn = useGraphedFunction();
5134
+ var outputs = Object.fromEntries(Object.keys(fn.inputs || {}).map(function (name) { return [
5135
+ name,
5136
+ normalizeVarDef(fn.inputs[name]),
5137
+ ]; }));
5138
+ var onRename = function (id, oldLabel) {
5139
+ var _a;
5140
+ var label = prompt("Enter new name", oldLabel);
5141
+ if (label !== null && label !== oldLabel) {
5142
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5143
+ var inputs = __assign({}, fn.inputs);
5144
+ var n = normalizeVarDef(inputs[id]);
5145
+ inputs[id] = __assign(__assign({}, n), { label: label });
5146
+ return { inputs: inputs };
5147
+ });
5148
+ }
5149
+ };
5150
+ var onAdd = function () {
5151
+ var _a;
5152
+ var label = prompt("Enter new name");
5153
+ if (label !== null) {
5154
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5155
+ var inputs = __assign({}, fn.inputs);
5156
+ inputs[uid$1()] = { type: "any", label: label };
5157
+ return { inputs: inputs };
5158
+ });
5159
+ }
5160
+ };
5161
+ var onDelete = function (name) {
5162
+ var _a;
5163
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5164
+ var inputs = __assign({}, fn.inputs);
5165
+ delete inputs[name];
5166
+ return { inputs: inputs };
5167
+ });
5168
+ };
5169
+ var onDefault = function (id) {
5170
+ var _a, _b;
5171
+ var value = prompt("Enter default value");
5172
+ if (value !== null && ((_a = fn.inputs) === null || _a === void 0 ? void 0 : _a[id])) {
5173
+ var n_1 = normalizeVarDef(fn.inputs[id]);
5174
+ if (n_1.type === "number") {
5175
+ value = Number(value);
5176
+ }
5177
+ else if (n_1.type === "boolean") {
5178
+ value = value.toLowerCase() === "true";
5179
+ }
5180
+ (_b = data.onIOChange) === null || _b === void 0 ? void 0 : _b.call(data, function (fn) {
5181
+ var inputs = __assign({}, fn.inputs);
5182
+ inputs[id] = __assign(__assign({}, n_1), { default: value });
5183
+ return { inputs: inputs };
5184
+ });
5185
+ }
5186
+ };
5187
+ var onType = function (id) {
5188
+ var _a, _b;
5189
+ var type = prompt("Enter type");
5190
+ if (type !== null && ((_a = fn.inputs) === null || _a === void 0 ? void 0 : _a[id])) {
5191
+ var n_2 = normalizeVarDef(fn.inputs[id]);
5192
+ (_b = data.onIOChange) === null || _b === void 0 ? void 0 : _b.call(data, function (fn) {
5193
+ var inputs = __assign({}, fn.inputs);
5194
+ inputs[id] = __assign(__assign({}, n_2), { type: type });
5195
+ return { inputs: inputs };
5196
+ });
5197
+ }
5198
+ };
5199
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id, noDelete: true },
5200
+ React.createElement("div", { className: styles.Description }, data.description),
5201
+ React.createElement("div", { className: styles.Ports },
5202
+ Object.entries(outputs || {}).map(function (_a) {
5203
+ var _b = __read(_a, 2), name = _b[0], normalized = _b[1];
5204
+ return (React.createElement(react.Handle, { key: name, type: "source", position: react.Position.Right, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Output, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
5205
+ "--type-color": "var(--color-".concat(normalized.type, ")"),
5206
+ } },
5207
+ React.createElement(Menu, { handle: React.createElement("button", null, "...") },
5208
+ React.createElement(MenuItem, { onClick: function () {
5209
+ return onRename(name, normalized.label || name);
5210
+ } }, "Rename"),
5211
+ React.createElement(MenuItem, { onClick: function () { return onDelete(name); } }, "Delete"),
5212
+ React.createElement(MenuItem, { onClick: function () { return onDefault(name); } }, "Set Default"),
5213
+ React.createElement(MenuItem, { onClick: function () { return onType(name); } }, "Set Type")),
5214
+ React.createElement("span", { style: { flex: 1 } }),
5215
+ React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name)));
5216
+ }),
5217
+ React.createElement("div", { className: styles.Body },
5218
+ React.createElement("button", { onClick: function () { return onAdd(); } }, "+")))));
5219
+ });
5220
+ InputNode.displayName = "InputNode";
5221
+
5047
5222
  function uid() {
5048
5223
  return Math.random().toString(36).substring(2, 15);
5049
5224
  }
@@ -5056,7 +5231,8 @@ var Merge = React.memo(function (_a) {
5056
5231
  var inputCount = Object.keys(inputs).length;
5057
5232
  inputs[uid()] = { type: "any", label: "D".concat(inputCount + 1) };
5058
5233
  var outputs = __assign({}, (data.outputDefs || {}));
5059
- return (React.createElement(NodeContainer, { label: data.name, selected: selected, id: id },
5234
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id },
5235
+ React.createElement("div", { className: styles.Description }, data.description),
5060
5236
  React.createElement("div", { className: styles.Ports },
5061
5237
  Object.entries(outputs || {}).map(function (_a) {
5062
5238
  var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
@@ -5069,11 +5245,93 @@ var Merge = React.memo(function (_a) {
5069
5245
  });
5070
5246
  Merge.displayName = "MergeNode";
5071
5247
 
5248
+ /**
5249
+ * todo: create a factory for output component
5250
+ * - allow to pass prompt handler (async)
5251
+ * - allow to pass custom UI for renaming/adding/deleting
5252
+ *
5253
+ * - create input node
5254
+ */
5255
+ var OutputNode = React.memo(function (_a) {
5256
+ var id = _a.id, data = _a.data, selected = _a.selected;
5257
+ var engine = useEngine();
5258
+ var fn = useGraphedFunction();
5259
+ var inputs = Object.fromEntries(Object.entries(data.inputs || {}).map(function (_a, i) {
5260
+ var _b, _c;
5261
+ var _d = __read(_a, 2), name = _d[0], ref = _d[1];
5262
+ var type = "any";
5263
+ if (isReference(ref)) {
5264
+ var _e = __read(parseReference(ref).split("."), 2), fnId = _e[0], outputName = _e[1];
5265
+ var fnName = (_b = fn.body[fnId]) === null || _b === void 0 ? void 0 : _b.name;
5266
+ if (fnName) {
5267
+ var calledFn = engine.getFunction(fnName);
5268
+ var outputDef = (_c = calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) === null || _c === void 0 ? void 0 : _c[outputName];
5269
+ if (outputDef) {
5270
+ var n = normalizeVarDef(outputDef);
5271
+ if (n === null || n === void 0 ? void 0 : n.type) {
5272
+ type = n.type;
5273
+ }
5274
+ }
5275
+ }
5276
+ }
5277
+ return [name, { type: type, label: name }];
5278
+ }));
5279
+ var onRename = function (oldName) {
5280
+ var _a;
5281
+ var newName = prompt("Enter new name", oldName);
5282
+ if (newName !== null && newName !== oldName) {
5283
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5284
+ var outputs = __assign({}, fn.outputs);
5285
+ outputs[newName] = outputs[oldName];
5286
+ delete outputs[oldName];
5287
+ return { outputs: outputs };
5288
+ });
5289
+ }
5290
+ };
5291
+ var onAdd = function () {
5292
+ var _a;
5293
+ var newName = prompt("Enter new name");
5294
+ if (newName !== null) {
5295
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5296
+ var outputs = __assign({}, fn.outputs);
5297
+ outputs[newName] = "";
5298
+ return { outputs: outputs };
5299
+ });
5300
+ }
5301
+ };
5302
+ var onDelete = function (name) {
5303
+ var _a;
5304
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5305
+ var outputs = __assign({}, fn.outputs);
5306
+ delete outputs[name];
5307
+ return { outputs: outputs };
5308
+ });
5309
+ };
5310
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id, noDelete: true },
5311
+ React.createElement("div", { className: styles.Description }, data.description),
5312
+ React.createElement("div", { className: styles.Ports }, Object.entries(inputs || {}).map(function (_a) {
5313
+ var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
5314
+ return (React.createElement(react.Handle, { key: name, type: "target", position: react.Position.Left, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(varDef.type)]), style: {
5315
+ "--type-color": "var(--color-".concat(varDef.type, ")"),
5316
+ } },
5317
+ React.createElement("span", { className: styles$1.PortLabel }, name),
5318
+ React.createElement("span", { style: { flex: 1 } }),
5319
+ React.createElement(Menu, { handle: React.createElement("button", null, "...") },
5320
+ React.createElement(MenuItem, { onClick: function () { return onRename(name); } }, "Rename"),
5321
+ React.createElement(MenuItem, { onClick: function () { return onDelete(name); } }, "Delete"))));
5322
+ })),
5323
+ React.createElement("div", { className: styles.Body },
5324
+ React.createElement("button", { onClick: function () { return onAdd(); } }, "+"))));
5325
+ });
5326
+ OutputNode.displayName = "OutputNode";
5327
+
5072
5328
  var nodeTypes = {
5073
5329
  Default: Default,
5074
5330
  Calc: Calc,
5075
5331
  Log: Log,
5076
5332
  Merge: Merge,
5333
+ Input: InputNode,
5334
+ Output: OutputNode,
5077
5335
  };
5078
5336
 
5079
5337
  var variableStore = zustand.create(function (set, get, api) {
@@ -5148,6 +5406,38 @@ function useUpdatePositions(fn) {
5148
5406
  function updateNodeArguments(node, inputs, params) {
5149
5407
  return __assign(__assign({}, node), { data: __assign(__assign({}, node.data), { inputs: inputs, params: params }) });
5150
5408
  }
5409
+ function createNode(engine, id, fncall, options) {
5410
+ var _a, _b;
5411
+ var calledFn = engine.getFunction(fncall.name);
5412
+ // create a node for each function call
5413
+ return {
5414
+ id: id,
5415
+ data: {
5416
+ name: fncall.name,
5417
+ label: fncall.label || (calledFn === null || calledFn === void 0 ? void 0 : calledFn.label) || fncall.name,
5418
+ description: calledFn === null || calledFn === void 0 ? void 0 : calledFn.description,
5419
+ inputs: fncall.inputs,
5420
+ params: fncall.params,
5421
+ inputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs) || {},
5422
+ paramDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.params) || {},
5423
+ outputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) || {},
5424
+ onChange: ((_a = options.onDataChange) === null || _a === void 0 ? void 0 : _a.call(options, id)) || (function () { }),
5425
+ onIOChange: options.onIOChange || (function (fn) { return fn; }),
5426
+ },
5427
+ position: ((_b = options.positions) === null || _b === void 0 ? void 0 : _b[id]) || { x: 0, y: 0 },
5428
+ dragHandle: options.dragHandle,
5429
+ type: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.nodeType) || "Default",
5430
+ };
5431
+ }
5432
+ function createEdge(nodeId, ref) {
5433
+ return {
5434
+ id: "".concat(ref.path, "-").concat(nodeId, ".").concat(ref.name),
5435
+ source: ref.refNode,
5436
+ target: nodeId,
5437
+ sourceHandle: "".concat(ref.refNode, "-").concat(ref.refField),
5438
+ targetHandle: "".concat(nodeId, "-").concat(ref.name),
5439
+ };
5440
+ }
5151
5441
  /**
5152
5442
  * flow state stores nodes and edges for the flow graph these can be updated when the function changes
5153
5443
  * using setFn
@@ -5156,17 +5446,22 @@ var useFlowState = zustand.create(function (set, get) { return ({
5156
5446
  engine: null,
5157
5447
  fn: null,
5158
5448
  options: {},
5159
- nodes: [],
5449
+ nodes: {},
5160
5450
  edges: [],
5161
5451
  setNodes: function (updater) {
5162
- set(function (state) { return ({ nodes: updater(state.nodes) }); });
5452
+ set(function (state) {
5453
+ var currentNodes = Object.values(state.nodes);
5454
+ var newNodes = updater(currentNodes);
5455
+ var nodes = Object.fromEntries(newNodes.map(function (n) { return [n.id, n]; }));
5456
+ return { nodes: nodes };
5457
+ });
5163
5458
  },
5164
5459
  setEdges: function (updater) {
5165
5460
  set(function (state) { return ({ edges: updater(state.edges) }); });
5166
5461
  },
5167
- setFn: function (fn) {
5462
+ setFn: function (fn, options) {
5168
5463
  // console.log("setFn", fn);
5169
- var _a = get(), oldFn = _a.fn, nodes = _a.nodes, edges = _a.edges, engine = _a.engine, options = _a.options;
5464
+ var _a = get(), oldFn = _a.fn, nodes = _a.nodes, edges = _a.edges, engine = _a.engine;
5170
5465
  // update the graph by shallow comparison
5171
5466
  if (!engine) {
5172
5467
  throw new Error("Engine not set");
@@ -5174,18 +5469,51 @@ var useFlowState = zustand.create(function (set, get) { return ({
5174
5469
  var oldKeys = Object.keys((oldFn === null || oldFn === void 0 ? void 0 : oldFn.body) || {});
5175
5470
  var newKeys = Object.keys(fn.body || {});
5176
5471
  // remove all the nodes that were removed
5177
- nodes = nodes === null || nodes === void 0 ? void 0 : nodes.filter(function (node) { return newKeys.includes(node.id); });
5472
+ Object.keys(nodes).forEach(function (key) {
5473
+ if (!newKeys.includes(key)) {
5474
+ delete nodes[key];
5475
+ }
5476
+ });
5178
5477
  // remove all the edges that were removed
5179
5478
  edges = edges === null || edges === void 0 ? void 0 : edges.filter(function (edge) {
5180
- return (newKeys.includes(edge.source) && newKeys.includes(edge.target));
5479
+ return ((newKeys.includes(edge.source) || edge.source === "inputs") &&
5480
+ newKeys.includes(edge.target));
5181
5481
  });
5482
+ // make sure there is an output node if outputs are defined
5483
+ if (fn.outputs && !nodes["output"]) {
5484
+ var key_1 = "output";
5485
+ var fncall = {
5486
+ name: "output",
5487
+ inputs: fn.outputs || {},
5488
+ params: {},
5489
+ };
5490
+ var newNode = createNode(engine, key_1, fncall, options);
5491
+ nodes[key_1] = newNode;
5492
+ // add edges
5493
+ var refs = getReferences(fncall.inputs || {});
5494
+ refs.forEach(function (ref) {
5495
+ edges.push(createEdge(key_1, ref));
5496
+ });
5497
+ }
5498
+ // make sure there is an input node if inputs are defined
5499
+ if (fn.inputs && !nodes["inputs"]) {
5500
+ var key = "inputs";
5501
+ var fncall = {
5502
+ name: "inputs",
5503
+ inputs: {},
5504
+ params: {},
5505
+ };
5506
+ var newNode = createNode(engine, key, fncall, options);
5507
+ nodes[key] = newNode;
5508
+ }
5182
5509
  // create the new nodes that were added
5183
5510
  var addedKeys = newKeys.filter(function (key) { return !oldKeys.includes(key); });
5184
5511
  addedKeys.forEach(function (key, index) {
5185
5512
  var _a;
5186
5513
  var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
5187
5514
  // add the node
5188
- nodes.push(createNode(engine, key, fncall, options));
5515
+ var newNode = createNode(engine, key, fncall, options);
5516
+ nodes[newNode.id] = newNode;
5189
5517
  // add edges
5190
5518
  var refs = getReferences(fncall.inputs || {});
5191
5519
  refs.forEach(function (ref) {
@@ -5203,12 +5531,8 @@ var useFlowState = zustand.create(function (set, get) { return ({
5203
5531
  var _a;
5204
5532
  // update the nodes that were changed
5205
5533
  var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
5206
- nodes = nodes === null || nodes === void 0 ? void 0 : nodes.map(function (n) {
5207
- if (n.id === key) {
5208
- return updateNodeArguments(n, fncall.inputs, fncall.params);
5209
- }
5210
- return n;
5211
- });
5534
+ var node = nodes[key];
5535
+ nodes[key] = updateNodeArguments(node, fncall.inputs, fncall.params);
5212
5536
  // update incoming edges
5213
5537
  var refs = getReferences(fncall.inputs || {});
5214
5538
  // remove all incoming edges
@@ -5225,7 +5549,7 @@ var useFlowState = zustand.create(function (set, get) { return ({
5225
5549
  init: function (fn, engine, options) {
5226
5550
  // console.log("initing");
5227
5551
  set({ engine: engine, options: options });
5228
- get().setFn(fn);
5552
+ get().setFn(fn, options);
5229
5553
  },
5230
5554
  }); });
5231
5555
  /**
@@ -5237,46 +5561,15 @@ function useFlow(fn, engine, options) {
5237
5561
  state.init(fn, engine, options);
5238
5562
  }, [engine]);
5239
5563
  React.useEffect(function () {
5240
- state.setFn(fn);
5241
- }, [fn]);
5564
+ state.setFn(fn, options);
5565
+ }, [fn, options]);
5242
5566
  return React.useMemo(function () { return ({
5243
- nodes: state.nodes,
5567
+ nodes: Object.values(state.nodes),
5244
5568
  edges: state.edges,
5245
5569
  setNodes: state.setNodes,
5246
5570
  setEdges: state.setEdges,
5247
5571
  }); }, [state]);
5248
5572
  }
5249
- function createNode(engine, id, fncall, options) {
5250
- var _a, _b;
5251
- var calledFn = engine.getFunction(fncall.name);
5252
- // create a node for each function call
5253
- return {
5254
- id: id,
5255
- data: {
5256
- name: fncall.name,
5257
- label: fncall.label || (calledFn === null || calledFn === void 0 ? void 0 : calledFn.label) || fncall.name,
5258
- description: calledFn === null || calledFn === void 0 ? void 0 : calledFn.description,
5259
- inputs: fncall.inputs,
5260
- params: fncall.params,
5261
- inputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs) || {},
5262
- paramDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.params) || {},
5263
- outputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) || {},
5264
- onChange: ((_a = options.onDataChange) === null || _a === void 0 ? void 0 : _a.call(options, id)) || (function () { }),
5265
- },
5266
- position: ((_b = options.positions) === null || _b === void 0 ? void 0 : _b[id]) || { x: 0, y: 0 },
5267
- dragHandle: options.dragHandle,
5268
- type: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.nodeType) || "Default",
5269
- };
5270
- }
5271
- function createEdge(nodeId, ref) {
5272
- return {
5273
- id: "".concat(ref.path, "-").concat(nodeId, ".").concat(ref.name),
5274
- source: ref.refNode,
5275
- target: nodeId,
5276
- sourceHandle: "".concat(ref.refNode, "-").concat(ref.refField),
5277
- targetHandle: "".concat(nodeId, "-").concat(ref.name),
5278
- };
5279
- }
5280
5573
 
5281
5574
  var ConnectionLine = (function (_a) {
5282
5575
  var _b;
@@ -5308,27 +5601,37 @@ var ConnectionLine = (function (_a) {
5308
5601
  });
5309
5602
 
5310
5603
  function FlowInner(_a) {
5311
- var fn = _a.function, engine = _a.engine, onChange = _a.onChange, onClick = _a.onClick, onSelect = _a.onSelect;
5312
- var _b = __read(React.useState(null), 2), instance = _b[0], setInstance = _b[1];
5604
+ var fn = _a.function, engine = _a.engine, _b = _a.center, center = _b === void 0 ? true : _b, fitView = _a.fitView, onChange = _a.onChange, onClick = _a.onClick, onSelect = _a.onSelect;
5605
+ var _c = __read(React.useState(null), 2), instance = _c[0], setInstance = _c[1];
5313
5606
  var updatePosition = useUpdatePositions(fn);
5314
- var _c = __read(usePositions(fn), 1), positions = _c[0];
5315
- var handleDataChange = function (id) { return function (newData) {
5316
- onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
5317
- // update the function definition
5318
- // console.log("before fn call update", Object.keys(fn.body));
5319
- var body = __assign({}, fn.body);
5320
- var fncall = __assign(__assign({}, (body[id] || {})), newData);
5321
- body[id] = fncall;
5322
- // console.log("after fn call update", Object.keys(body));
5323
- var updatedFn = __assign(__assign({}, fn), { body: body });
5324
- return updatedFn;
5325
- });
5326
- }; };
5327
- var _d = useFlow(fn, engine, {
5328
- dragHandle: ".".concat(styles.Label),
5329
- positions: positions,
5330
- onDataChange: handleDataChange,
5331
- }), nodes = _d.nodes, edges = _d.edges, setNodes = _d.setNodes, setEdges = _d.setEdges;
5607
+ var _d = __read(usePositions(fn), 1), positions = _d[0];
5608
+ var options = React.useMemo(function () {
5609
+ var handleDataChange = function (id) { return function (newData) {
5610
+ onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
5611
+ // update the function definition
5612
+ // console.log("before fn call update", Object.keys(fn.body));
5613
+ var body = __assign({}, fn.body);
5614
+ var fncall = __assign(__assign({}, (body[id] || {})), newData);
5615
+ body[id] = fncall;
5616
+ // console.log("after fn call update", Object.keys(body));
5617
+ var updatedFn = __assign(__assign({}, fn), { body: body });
5618
+ return updatedFn;
5619
+ });
5620
+ }; };
5621
+ var handleIOChange = function (callback) {
5622
+ var changes = callback(fn);
5623
+ onChange === null || onChange === void 0 ? void 0 : onChange(function (oldFn) {
5624
+ return __assign(__assign({}, oldFn), changes);
5625
+ });
5626
+ };
5627
+ return {
5628
+ dragHandle: ".".concat(styles.Label),
5629
+ positions: positions,
5630
+ onDataChange: handleDataChange,
5631
+ onIOChange: handleIOChange,
5632
+ };
5633
+ }, [fn, onChange]);
5634
+ var _e = useFlow(fn, engine, options), nodes = _e.nodes, edges = _e.edges, setNodes = _e.setNodes, setEdges = _e.setEdges;
5332
5635
  // node selection handler
5333
5636
  var handleSelect = React.useCallback(function (_a) {
5334
5637
  var nodes = _a.nodes;
@@ -5338,7 +5641,7 @@ function FlowInner(_a) {
5338
5641
  onChange: handleSelect,
5339
5642
  });
5340
5643
  // dropping nodes
5341
- var _e = __read(useDrop({
5644
+ var _f = __read(useDrop({
5342
5645
  accept: "flow-node",
5343
5646
  drop: function (item, monitor) {
5344
5647
  var pos = monitor.getClientOffset();
@@ -5358,7 +5661,7 @@ function FlowInner(_a) {
5358
5661
  isOver: monitor.isOver(),
5359
5662
  };
5360
5663
  },
5361
- }), 2); _e[0].isOver; var drop = _e[1];
5664
+ }), 2); _f[0].isOver; var drop = _f[1];
5362
5665
  // connecting nodes
5363
5666
  var onConnect = React.useCallback(function (params) {
5364
5667
  var _a, _b;
@@ -5406,27 +5709,43 @@ function FlowInner(_a) {
5406
5709
  }
5407
5710
  };
5408
5711
  var onNodesChange = React.useCallback(function (changes) {
5712
+ changes = changes.filter(function (change) {
5713
+ if (change.id === "output" && change.type === "remove")
5714
+ return false;
5715
+ return true;
5716
+ });
5409
5717
  return setNodes(function (nodesSnapshot) {
5410
5718
  return react.applyNodeChanges(changes, nodesSnapshot);
5411
5719
  });
5412
5720
  }, []);
5413
5721
  var onEdgesChange = React.useCallback(function (changes) {
5722
+ changes = changes.filter(function (change) {
5723
+ if (change.id.includes("-output.") && change.type === "remove")
5724
+ return false;
5725
+ return true;
5726
+ });
5414
5727
  return setEdges(function (edgesSnapshot) {
5415
5728
  return react.applyEdgeChanges(changes, edgesSnapshot);
5416
5729
  });
5417
5730
  }, []);
5418
- return (React.createElement(react.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,
5731
+ return (React.createElement(react.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: center || fitView, fitViewOptions: {
5732
+ minZoom: fitView ? undefined : 1,
5733
+ maxZoom: fitView ? undefined : 1,
5734
+ },
5419
5735
  // panOnDrag={false}
5420
5736
  selectionOnDrag: true, minZoom: 0.1, maxZoom: 4, deleteKeyCode: "Delete", connectionLineComponent: ConnectionLine },
5421
5737
  React.createElement(react.Background, { variant: react.BackgroundVariant.Dots }),
5422
5738
  React.createElement(react.Controls, null)));
5423
5739
  }
5424
- function Flow(_a) {
5425
- 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;
5426
- var contextValue = React.useMemo(function () { return ({ engine: engine, controls: customControls }); }, [engine, customControls]);
5740
+ function Flow(props) {
5741
+ var contextValue = React.useMemo(function () { return ({
5742
+ engine: props.engine,
5743
+ controls: props.customControls,
5744
+ graphedFn: props.function,
5745
+ }); }, [props.engine, props.customControls, props.function]);
5427
5746
  return (React.createElement(FlowContext.Provider, { value: contextValue },
5428
5747
  React.createElement(react.ReactFlowProvider, null,
5429
- React.createElement(FlowInner, { function: fn, engine: engine, onChange: onChange, onClick: onClick, onSelect: onSelect }))));
5748
+ React.createElement(FlowInner, __assign({}, props)))));
5430
5749
  }
5431
5750
 
5432
5751
  function useDraggableNode(name, fn) {