@flowgram.ai/form-materials 0.2.26 → 0.2.27

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.js CHANGED
@@ -3174,9 +3174,30 @@ var autoRenameRefEffect = [
3174
3174
  after.key
3175
3175
  ];
3176
3176
  traverseRef(name, form.getValueIn(name), (_drilldownName, _v) => {
3177
- if (isRefMatch(_v, beforeKeyPath)) {
3178
- _v.content = [...afterKeyPath, ...(_v.content || [])?.slice(beforeKeyPath.length)];
3179
- form.setValueIn(_drilldownName, _v);
3177
+ if (_v.type === "ref") {
3178
+ if (isKeyPathMatch(_v.content, beforeKeyPath)) {
3179
+ _v.content = [...afterKeyPath, ...(_v.content || [])?.slice(beforeKeyPath.length)];
3180
+ form.setValueIn(_drilldownName, _v);
3181
+ }
3182
+ } else if (_v.type === "template") {
3183
+ const templateKeyPaths = getTemplateKeyPaths(_v);
3184
+ let hasMatch = false;
3185
+ templateKeyPaths.forEach((_keyPath) => {
3186
+ if (isKeyPathMatch(_keyPath, beforeKeyPath)) {
3187
+ hasMatch = true;
3188
+ const nextKeyPath = [
3189
+ ...afterKeyPath,
3190
+ ...(_keyPath || [])?.slice(beforeKeyPath.length)
3191
+ ];
3192
+ _v.content = _v.content?.replace(
3193
+ `{{${_keyPath.join(".")}}`,
3194
+ `{{${nextKeyPath.join(".")}}`
3195
+ );
3196
+ }
3197
+ });
3198
+ if (hasMatch) {
3199
+ form.setValueIn(_drilldownName, { ..._v });
3200
+ }
3180
3201
  }
3181
3202
  });
3182
3203
  });
@@ -3186,18 +3207,31 @@ var autoRenameRefEffect = [
3186
3207
  }
3187
3208
  }
3188
3209
  ];
3189
- function isRefMatch(value, targetKeyPath) {
3190
- return targetKeyPath.every((_key, index) => _key === value.content?.[index]);
3210
+ function isKeyPathMatch(keyPath = [], targetKeyPath) {
3211
+ return targetKeyPath.every((_key, index) => _key === keyPath[index]);
3212
+ }
3213
+ function getTemplateKeyPaths(value) {
3214
+ const keyPathReg = /{{(.*?)}}/g;
3215
+ return (0, import_lodash8.uniq)(value.content?.match(keyPathReg) || []).map(
3216
+ (_keyPath) => _keyPath.slice(2, -2).split(".")
3217
+ );
3191
3218
  }
3192
3219
  function isRef(value) {
3193
3220
  return value?.type === "ref" && Array.isArray(value?.content) && typeof value?.content[0] === "string";
3194
3221
  }
3222
+ function isTemplate(value) {
3223
+ return value?.type === "template" && typeof value?.content === "string";
3224
+ }
3195
3225
  function traverseRef(name, value, cb) {
3196
3226
  if ((0, import_lodash8.isObject)(value)) {
3197
3227
  if (isRef(value)) {
3198
3228
  cb(name, value);
3199
3229
  return;
3200
3230
  }
3231
+ if (isTemplate(value)) {
3232
+ cb(name, value);
3233
+ return;
3234
+ }
3201
3235
  Object.entries(value).forEach(([_key, _value]) => {
3202
3236
  traverseRef(`${name}.${_key}`, _value, cb);
3203
3237
  });