@rkmodules/rules 0.0.145 → 0.0.147

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
@@ -607,9 +607,14 @@ function getValue$1(obj, path) {
607
607
  return;
608
608
  }
609
609
  var parts = path.split(".");
610
- var value = obj[parts.shift() || ""];
611
- while (parts.length && value) {
612
- value = value[parts.shift() || ""];
610
+ var key = parts.shift() || "";
611
+ var safeObj = Object.fromEntries(Object.entries(obj).map(function (_a) {
612
+ var _b = __read(_a, 2), k = _b[0], v = _b[1];
613
+ return [safeName(k), v];
614
+ }));
615
+ var value = safeObj[key];
616
+ if (parts.length) {
617
+ return getValue$1(value, parts.join("."));
613
618
  }
614
619
  return value;
615
620
  }
@@ -2575,7 +2580,6 @@ var explodeObject = {
2575
2580
  var _b = __read(_a, 2), key = _b[0], value = _b[1];
2576
2581
  if (key[0] === "_")
2577
2582
  return;
2578
- key = safeName(key);
2579
2583
  if (!outputs[key]) {
2580
2584
  outputs[key] = {};
2581
2585
  }
@@ -6286,23 +6290,48 @@ function Control(_a) {
6286
6290
  }
6287
6291
  }
6288
6292
 
6293
+ function Menu(_a) {
6294
+ var handle = _a.handle, children = _a.children;
6295
+ var _b = __read(React.useState(false), 2), open = _b[0], setOpen = _b[1];
6296
+ var handleOpen = function () {
6297
+ setOpen(!open);
6298
+ };
6299
+ var handleClose = function () {
6300
+ setOpen(false);
6301
+ };
6302
+ return (React.createElement("div", { className: styles$1.Menu },
6303
+ React.createElement("div", { onClick: handleOpen }, handle),
6304
+ open && (React.createElement("div", { className: styles$1.MenuCanvas, onClick: handleClose }, children))));
6305
+ }
6306
+ function MenuItem(_a) {
6307
+ var children = _a.children, props = __rest(_a, ["children"]);
6308
+ return (React.createElement("div", __assign({ className: styles$1.MenuItem }, props), children));
6309
+ }
6310
+
6289
6311
  var Input = function (_a) {
6290
6312
  var _b;
6291
- var name = _a.name, varDef = _a.varDef, id = _a.id, data = _a.data, onClick = _a.onClick;
6313
+ var name = _a.name, varDef = _a.varDef, id = _a.id, data = _a.data, onClick = _a.onClick, onRename = _a.onRename, onDelete = _a.onDelete;
6292
6314
  var normalized = normalizeVarDef(varDef);
6293
6315
  var value = (_b = data.inputs) === null || _b === void 0 ? void 0 : _b[name];
6294
6316
  var isRef = isReference(value);
6317
+ var portId = "".concat(id, "-").concat(safeName(name));
6318
+ var showMenu = onRename || onDelete;
6295
6319
  var handleInputChange = function (param) { return function (value) {
6296
6320
  var _a;
6297
6321
  data.onChange({
6298
6322
  inputs: __assign(__assign({}, data.inputs), (_a = {}, _a[param] = value, _a)),
6299
6323
  });
6300
6324
  }; };
6301
- 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: {
6325
+ return (React.createElement(Handle, { type: "target", position: Position.Left, id: portId, className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
6302
6326
  "--type-color": "var(--color-".concat(normalized.type, ")"),
6303
6327
  }, onClick: onClick },
6304
6328
  React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name),
6305
- !isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleInputChange(name), data: data }))));
6329
+ !isRef && !showMenu && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleInputChange(name), data: data })),
6330
+ showMenu && (React.createElement("div", { style: { display: "flex" } },
6331
+ React.createElement("span", { style: { flex: 1 } }),
6332
+ React.createElement(Menu, { handle: React.createElement("button", null, "...") },
6333
+ onRename && (React.createElement(MenuItem, { onClick: function () { return onRename(name, normalized); } }, "Rename")),
6334
+ onDelete && (React.createElement(MenuItem, { onClick: function () { return onDelete(name); } }, "Delete")))))));
6306
6335
  };
6307
6336
 
6308
6337
  var Param = function (_a) {
@@ -6327,7 +6356,8 @@ var Param = function (_a) {
6327
6356
  var Output = function (_a) {
6328
6357
  var name = _a.name, varDef = _a.varDef, id = _a.id, onClick = _a.onClick, focus = _a.focus;
6329
6358
  var normalized = normalizeVarDef(varDef);
6330
- 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: {
6359
+ var portId = "".concat(id, "-").concat(safeName(name));
6360
+ return (React.createElement(Handle, { type: "source", position: Position.Right, id: portId, className: classNames(styles$1.Output, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
6331
6361
  "--type-color": "var(--color-".concat(normalized.type, ")"),
6332
6362
  }, onClick: onClick },
6333
6363
  React.createElement("span", { className: styles$1.PortLabel },
@@ -6560,24 +6590,6 @@ var Default = React.memo(function (_a) {
6560
6590
  });
6561
6591
  Default.displayName = "DefaultNode";
6562
6592
 
6563
- function Menu(_a) {
6564
- var handle = _a.handle, children = _a.children;
6565
- var _b = __read(React.useState(false), 2), open = _b[0], setOpen = _b[1];
6566
- var handleOpen = function () {
6567
- setOpen(!open);
6568
- };
6569
- var handleClose = function () {
6570
- setOpen(false);
6571
- };
6572
- return (React.createElement("div", { className: styles$1.Menu },
6573
- React.createElement("div", { onClick: handleOpen }, handle),
6574
- open && (React.createElement("div", { className: styles$1.MenuCanvas, onClick: handleClose }, children))));
6575
- }
6576
- function MenuItem(_a) {
6577
- var children = _a.children, props = __rest(_a, ["children"]);
6578
- return (React.createElement("div", __assign({ className: styles$1.MenuItem }, props), children));
6579
- }
6580
-
6581
6593
  /**
6582
6594
  * todo: get input data from graphed function
6583
6595
  */
@@ -6803,6 +6815,8 @@ var OutputNode = React.memo(function (_a) {
6803
6815
  }
6804
6816
  });
6805
6817
  }); };
6818
+ // TODO: this seems specifically for the output node only, adjusting the main rule IO spec,
6819
+ // we need to change the current function spec instead
6806
6820
  var onAdd = function () { return __awaiter(void 0, void 0, void 0, function () {
6807
6821
  var newName;
6808
6822
  var _a;
@@ -6822,6 +6836,8 @@ var OutputNode = React.memo(function (_a) {
6822
6836
  }
6823
6837
  });
6824
6838
  }); };
6839
+ // TODO: this seems specifically for the output node only, adjusting the main rule IO spec,
6840
+ // we need to change the current function spec instead
6825
6841
  var onDelete = function (name) {
6826
6842
  var _a;
6827
6843
  (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
@@ -6837,17 +6853,9 @@ var OutputNode = React.memo(function (_a) {
6837
6853
  var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
6838
6854
  return (React.createElement(Output, { key: "".concat(name, "_").concat(index), name: name, varDef: varDef, id: id, onClick: function () { return setFocus(name); }, focus: showPreview && focus === name }));
6839
6855
  }),
6840
- Object.entries(inputs || {}).map(function (_a) {
6856
+ Object.entries(inputs || {}).map(function (_a, index) {
6841
6857
  var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
6842
- 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: {
6843
- "--type-color": "var(--color-".concat(varDef.type, ")"),
6844
- } },
6845
- React.createElement("span", { className: styles$1.PortLabel }, name),
6846
- React.createElement("div", { style: { display: "flex" } },
6847
- React.createElement("span", { style: { flex: 1 } }),
6848
- React.createElement(Menu, { handle: React.createElement("button", null, "...") },
6849
- React.createElement(MenuItem, { onClick: function () { return onRename(name); } }, "Rename"),
6850
- React.createElement(MenuItem, { onClick: function () { return onDelete(name); } }, "Delete")))));
6858
+ return (React.createElement(Input, { key: "".concat(name, "_").concat(index), name: name, varDef: varDef, id: id, data: data, onRename: onRename, onDelete: onDelete }));
6851
6859
  }),
6852
6860
  paramEntries.map(function (_a, index) {
6853
6861
  var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
@@ -6875,7 +6883,7 @@ var DynamicOutput = React.memo(function (_a) {
6875
6883
  if (event.name === id) {
6876
6884
  var result = event.result || {};
6877
6885
  setOutputs(Object.fromEntries(Object.keys(result).map(function (name, i) { return [
6878
- safeName(name),
6886
+ name,
6879
6887
  { type: "any", label: name },
6880
6888
  ]; })));
6881
6889
  }
@@ -6885,12 +6893,104 @@ var DynamicOutput = React.memo(function (_a) {
6885
6893
  });
6886
6894
  DynamicOutput.displayName = "DynamicOutputNode";
6887
6895
 
6896
+ var NamedInput = function (_a) {
6897
+ var _b;
6898
+ var _c;
6899
+ var id = _a.id, data = _a.data, selected = _a.selected;
6900
+ var engine = useEngine();
6901
+ // let fn = useGraphedFunction();
6902
+ var prompt = usePrompts().prompt;
6903
+ var inputEntries = Object.entries(data.inputDefs || {});
6904
+ var outputEntries = Object.entries(data.outputDefs || {});
6905
+ var paramEntries = Object.entries(data.paramDefs || {});
6906
+ var _d = __read(React.useState({}), 2), result = _d[0], setResult = _d[1];
6907
+ var _e = __read(React.useState(null), 2), duration = _e[0], setDuration = _e[1];
6908
+ var _f = __read(useInner(((_c = outputEntries[0]) === null || _c === void 0 ? void 0 : _c[0]) || null, [data.outputDefs]), 2), focus = _f[0], setFocus = _f[1];
6909
+ var _g = __read(React.useState(false), 2), showPreview = _g[0], setShowPreview = _g[1];
6910
+ var previewValue = focus ? result[focus] : Object.values(result)[0];
6911
+ React.useEffect(function () {
6912
+ return engine.subscribe("functionResult", function (event) {
6913
+ var _a;
6914
+ if (event.name === id) {
6915
+ setResult(event.result || {});
6916
+ setDuration((_a = event.duration) !== null && _a !== void 0 ? _a : null);
6917
+ }
6918
+ });
6919
+ }, []);
6920
+ var onRename = function (name, def) { return __awaiter(void 0, void 0, void 0, function () {
6921
+ var newLabel, inputDefs;
6922
+ return __generator(this, function (_a) {
6923
+ switch (_a.label) {
6924
+ case 0: return [4 /*yield*/, prompt("Enter new name", def.label || name)];
6925
+ case 1:
6926
+ newLabel = _a.sent();
6927
+ if (newLabel !== null && newLabel !== def.label) {
6928
+ inputDefs = __assign({}, data.inputDefs);
6929
+ inputDefs[name] = __assign(__assign({}, def), { label: newLabel });
6930
+ data.onChange({ inputDefs: inputDefs });
6931
+ }
6932
+ return [2 /*return*/];
6933
+ }
6934
+ });
6935
+ }); };
6936
+ var onAdd = function () { return __awaiter(void 0, void 0, void 0, function () {
6937
+ var newName, inputDefs;
6938
+ var _a;
6939
+ return __generator(this, function (_b) {
6940
+ switch (_b.label) {
6941
+ case 0: return [4 /*yield*/, prompt("Enter new name")];
6942
+ case 1:
6943
+ newName = _b.sent();
6944
+ if (newName !== null) {
6945
+ inputDefs = __assign(__assign({}, data.inputDefs), (_a = {}, _a[newName] = { type: "any", label: newName }, _a));
6946
+ data.onChange({ inputDefs: inputDefs });
6947
+ }
6948
+ return [2 /*return*/];
6949
+ }
6950
+ });
6951
+ }); };
6952
+ var onDelete = function (name) {
6953
+ var inputDefs = __assign({}, data.inputDefs);
6954
+ var inputs = __assign({}, data.inputs);
6955
+ delete inputDefs[name];
6956
+ delete inputs[name];
6957
+ data.onChange({ inputs: inputs, inputDefs: inputDefs });
6958
+ };
6959
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id, noDelete: true },
6960
+ React.createElement("div", { className: styles.Description }, data.description),
6961
+ React.createElement("div", { className: styles.Ports },
6962
+ outputEntries.map(function (_a, index) {
6963
+ var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
6964
+ return (React.createElement(Output, { key: "".concat(name, "_").concat(index), name: name, varDef: varDef, id: id, onClick: function () { return setFocus(name); }, focus: showPreview && focus === name }));
6965
+ }),
6966
+ inputEntries.map(function (_a, index) {
6967
+ var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
6968
+ return (React.createElement(Input, { key: "".concat(name, "_").concat(index), name: name, varDef: varDef, id: id, data: data, onRename: onRename, onDelete: onDelete }));
6969
+ }),
6970
+ paramEntries.map(function (_a, index) {
6971
+ var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
6972
+ return (React.createElement(Param, { key: "".concat(name, "_").concat(index), name: name, varDef: varDef, id: id, data: data }));
6973
+ })),
6974
+ React.createElement("div", { className: styles.Body },
6975
+ React.createElement("button", { onClick: function () { return onAdd(); } }, "+")),
6976
+ !!outputEntries.length && (React.createElement("div", { className: classNames(styles.PreviewHead, (_b = {},
6977
+ _b[styles.expanded] = showPreview,
6978
+ _b)), onClick: function () { return setShowPreview(!showPreview); } }, "Preview")),
6979
+ showPreview && (React.createElement("div", { className: styles.Preview },
6980
+ React.createElement(TreeView, { value: previewValue }),
6981
+ duration !== null && (React.createElement("div", { className: styles.Duration },
6982
+ duration.toFixed(2),
6983
+ " ms"))))));
6984
+ };
6985
+ NamedInput.displayName = "NamedInput";
6986
+
6888
6987
  var nodeTypes = {
6889
6988
  Default: Default,
6890
6989
  Calc: Calc,
6891
6990
  Log: Log,
6892
6991
  DynamicInput: DynamicInput,
6893
6992
  DynamicOutput: DynamicOutput,
6993
+ NamedInput: NamedInput,
6894
6994
  Input: InputNode,
6895
6995
  Output: OutputNode,
6896
6996
  };
@@ -6972,8 +7072,8 @@ function useUpdatePositions(fn) {
6972
7072
  }
6973
7073
 
6974
7074
  // helpers
6975
- function updateNodeArguments(node, inputs, params) {
6976
- return __assign(__assign({}, node), { data: __assign(__assign({}, node.data), { inputs: inputs, params: params }) });
7075
+ function updateNodeArguments(node, inputs, params, inputDefs, outputDefs) {
7076
+ return __assign(__assign({}, node), { data: __assign(__assign({}, node.data), { inputs: inputs, params: params, inputDefs: inputDefs !== null && inputDefs !== void 0 ? inputDefs : node.data.inputDefs, outputDefs: outputDefs !== null && outputDefs !== void 0 ? outputDefs : node.data.outputDefs }) });
6977
7077
  }
6978
7078
  /**
6979
7079
  * gets the source and target node and field from an edge by de-referencing the handles
@@ -6981,9 +7081,9 @@ function updateNodeArguments(node, inputs, params) {
6981
7081
  * returns an object with the source node id, target node id, source field name and target field name
6982
7082
  */
6983
7083
  function deRefEdgeHandle(handle) {
6984
- var match = handle.match(/^(.+)-(.+)$/);
7084
+ var match = handle.match(/^(.+?)-(.+)$/);
6985
7085
  if (!match) {
6986
- throw new Error("Invalid handle format");
7086
+ throw new Error("Invalid handle format" + handle);
6987
7087
  }
6988
7088
  return { node: match[1], field: match[2] };
6989
7089
  }
@@ -6999,7 +7099,7 @@ function createNode(engine, id, fncall, options) {
6999
7099
  description: calledFn === null || calledFn === void 0 ? void 0 : calledFn.description,
7000
7100
  inputs: fncall.inputs,
7001
7101
  params: fncall.params,
7002
- inputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs) || {},
7102
+ inputDefs: __assign(__assign({}, fncall.inputDefs), calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs),
7003
7103
  paramDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.params) || {},
7004
7104
  outputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) || {},
7005
7105
  onChange: ((_a = options.onDataChange) === null || _a === void 0 ? void 0 : _a.call(options, id)) || (function () { }),
@@ -7015,8 +7115,8 @@ function createEdge(nodeId, ref) {
7015
7115
  id: "".concat(ref.path, "-").concat(nodeId, ".").concat(ref.name),
7016
7116
  source: ref.refNode,
7017
7117
  target: nodeId,
7018
- sourceHandle: "".concat(ref.refNode, "-").concat(ref.refField),
7019
- targetHandle: "".concat(nodeId, "-").concat(ref.name),
7118
+ sourceHandle: "".concat(ref.refNode, "-").concat(safeName(ref.refField)),
7119
+ targetHandle: "".concat(nodeId, "-").concat(safeName(ref.name)),
7020
7120
  };
7021
7121
  }
7022
7122
  /**
@@ -7057,8 +7157,15 @@ var useFlowState = create$1(function (set, get) { return ({
7057
7157
  });
7058
7158
  // remove all the edges that were removed
7059
7159
  edges = edges === null || edges === void 0 ? void 0 : edges.filter(function (edge) {
7060
- return ((newKeys.includes(edge.source) || edge.source === "inputs") &&
7061
- newKeys.includes(edge.target));
7160
+ var _a, _b;
7161
+ var target = deRefEdgeHandle(edge.targetHandle);
7162
+ return (
7163
+ // source node of the edge must be in the document
7164
+ (newKeys.includes(edge.source) || edge.source === "inputs") &&
7165
+ // target node of the edge must be in the document
7166
+ newKeys.includes(edge.target) &&
7167
+ // target field of the edge must be in the target node definition
7168
+ ((_b = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[edge.target].inputs) === null || _b === void 0 ? void 0 : _b[target.field]) !== undefined);
7062
7169
  });
7063
7170
  // make sure there is an output node if outputs are defined
7064
7171
  if (fn.outputs && !nodes["output"]) {
@@ -7107,13 +7214,13 @@ var useFlowState = create$1(function (set, get) { return ({
7107
7214
  // shallow comparison
7108
7215
  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]);
7109
7216
  });
7110
- // console.log("changed keys", changedKeys);
7217
+ console.log("changed keys", changedKeys);
7111
7218
  changedKeys.forEach(function (key) {
7112
7219
  var _a;
7113
7220
  // update the nodes that were changed
7114
7221
  var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
7115
7222
  var node = nodes[key];
7116
- nodes[key] = updateNodeArguments(node, fncall.inputs, fncall.params);
7223
+ nodes[key] = updateNodeArguments(node, fncall.inputs, fncall.params, fncall.inputDefs, fncall.outputDefs);
7117
7224
  // update incoming edges
7118
7225
  var refs = getReferences(fncall.inputs || {});
7119
7226
  // remove all incoming edges