@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flowgram.ai/form-materials",
3
- "version": "0.2.26",
3
+ "version": "0.2.27",
4
4
  "homepage": "https://flowgram.ai/",
5
5
  "repository": "https://github.com/bytedance/flowgram.ai",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "@coze-editor/editor": "0.1.0-alpha.879fbb",
34
34
  "@codemirror/view": "~6.38.0",
35
35
  "@codemirror/state": "~6.5.2",
36
- "@flowgram.ai/editor": "0.2.26"
36
+ "@flowgram.ai/editor": "0.2.27"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@types/lodash": "^4.14.137",
@@ -49,8 +49,8 @@
49
49
  "tsup": "^8.0.1",
50
50
  "typescript": "^5.0.4",
51
51
  "vitest": "^0.34.6",
52
- "@flowgram.ai/ts-config": "0.2.26",
53
- "@flowgram.ai/eslint-config": "0.2.26"
52
+ "@flowgram.ai/eslint-config": "0.2.27",
53
+ "@flowgram.ai/ts-config": "0.2.27"
54
54
  },
55
55
  "peerDependencies": {
56
56
  "react": ">=16.8",
@@ -3,7 +3,7 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import { isArray, isObject } from 'lodash';
6
+ import { isArray, isObject, uniq } from 'lodash';
7
7
  import {
8
8
  DataEvent,
9
9
  Effect,
@@ -11,7 +11,7 @@ import {
11
11
  VariableFieldKeyRenameService,
12
12
  } from '@flowgram.ai/editor';
13
13
 
14
- import { IFlowRefValue } from '../../typings';
14
+ import { IFlowRefValue, IFlowTemplateValue } from '../../typings';
15
15
 
16
16
  /**
17
17
  * Auto rename ref when form item's key is renamed
@@ -44,9 +44,34 @@ export const autoRenameRefEffect: EffectOptions[] = [
44
44
 
45
45
  // traverse rename refs inside form item 'name'
46
46
  traverseRef(name, form.getValueIn(name), (_drilldownName, _v) => {
47
- if (isRefMatch(_v, beforeKeyPath)) {
48
- _v.content = [...afterKeyPath, ...(_v.content || [])?.slice(beforeKeyPath.length)];
49
- form.setValueIn(_drilldownName, _v);
47
+ if (_v.type === 'ref') {
48
+ // ref auto rename
49
+ if (isKeyPathMatch(_v.content, beforeKeyPath)) {
50
+ _v.content = [...afterKeyPath, ...(_v.content || [])?.slice(beforeKeyPath.length)];
51
+ form.setValueIn(_drilldownName, _v);
52
+ }
53
+ } else if (_v.type === 'template') {
54
+ // template auto rename
55
+ const templateKeyPaths = getTemplateKeyPaths(_v);
56
+ let hasMatch = false;
57
+
58
+ templateKeyPaths.forEach((_keyPath) => {
59
+ if (isKeyPathMatch(_keyPath, beforeKeyPath)) {
60
+ hasMatch = true;
61
+ const nextKeyPath = [
62
+ ...afterKeyPath,
63
+ ...(_keyPath || [])?.slice(beforeKeyPath.length),
64
+ ];
65
+ _v.content = _v.content?.replace(
66
+ `{{${_keyPath.join('.')}}`,
67
+ `{{${nextKeyPath.join('.')}}`
68
+ );
69
+ }
70
+ });
71
+
72
+ if (hasMatch) {
73
+ form.setValueIn(_drilldownName, { ..._v });
74
+ }
50
75
  }
51
76
  });
52
77
  });
@@ -64,8 +89,21 @@ export const autoRenameRefEffect: EffectOptions[] = [
64
89
  * @param targetKeyPath
65
90
  * @returns
66
91
  */
67
- function isRefMatch(value: IFlowRefValue, targetKeyPath: string[]) {
68
- return targetKeyPath.every((_key, index) => _key === value.content?.[index]);
92
+ function isKeyPathMatch(keyPath: string[] = [], targetKeyPath: string[]) {
93
+ return targetKeyPath.every((_key, index) => _key === keyPath[index]);
94
+ }
95
+
96
+ /**
97
+ * get template key paths
98
+ * @param value
99
+ * @returns
100
+ */
101
+ function getTemplateKeyPaths(value: IFlowTemplateValue) {
102
+ // find all keyPath wrapped in {{}}
103
+ const keyPathReg = /{{(.*?)}}/g;
104
+ return uniq(value.content?.match(keyPathReg) || []).map((_keyPath) =>
105
+ _keyPath.slice(2, -2).split('.')
106
+ );
69
107
  }
70
108
 
71
109
  /**
@@ -79,19 +117,32 @@ function isRef(value: any): value is IFlowRefValue {
79
117
  );
80
118
  }
81
119
 
120
+ function isTemplate(value: any): value is IFlowTemplateValue {
121
+ return value?.type === 'template' && typeof value?.content === 'string';
122
+ }
123
+
82
124
  /**
83
125
  * Traverse value to find ref
84
126
  * @param value
85
127
  * @param options
86
128
  * @returns
87
129
  */
88
- function traverseRef(name: string, value: any, cb: (name: string, _v: IFlowRefValue) => void) {
130
+ function traverseRef(
131
+ name: string,
132
+ value: any,
133
+ cb: (name: string, _v: IFlowRefValue | IFlowTemplateValue) => void
134
+ ) {
89
135
  if (isObject(value)) {
90
136
  if (isRef(value)) {
91
137
  cb(name, value);
92
138
  return;
93
139
  }
94
140
 
141
+ if (isTemplate(value)) {
142
+ cb(name, value);
143
+ return;
144
+ }
145
+
95
146
  Object.entries(value).forEach(([_key, _value]) => {
96
147
  traverseRef(`${name}.${_key}`, _value, cb);
97
148
  });