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