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