@tmagic/editor 1.4.8 → 1.4.10

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.
Files changed (166) hide show
  1. package/dist/style.css +26 -0
  2. package/dist/tmagic-editor.js +3213 -2820
  3. package/dist/tmagic-editor.umd.cjs +3237 -2839
  4. package/package.json +14 -12
  5. package/src/Editor.vue +212 -0
  6. package/src/components/CodeBlockEditor.vue +268 -0
  7. package/src/components/CodeParams.vue +60 -0
  8. package/src/components/ContentMenu.vue +193 -0
  9. package/src/components/FloatingBox.vue +178 -0
  10. package/src/components/Icon.vue +27 -0
  11. package/src/components/Resizer.vue +23 -0
  12. package/src/components/ScrollBar.vue +151 -0
  13. package/src/components/ScrollViewer.vue +128 -0
  14. package/src/components/SearchInput.vue +37 -0
  15. package/src/components/SplitView.vue +186 -0
  16. package/src/components/ToolButton.vue +138 -0
  17. package/src/components/Tree.vue +71 -0
  18. package/src/components/TreeNode.vue +150 -0
  19. package/src/editorProps.ts +120 -0
  20. package/src/fields/Code.vue +50 -0
  21. package/src/fields/CodeLink.vue +80 -0
  22. package/src/fields/CodeSelect.vue +132 -0
  23. package/src/fields/CodeSelectCol.vue +139 -0
  24. package/src/fields/CondOpSelect.vue +87 -0
  25. package/src/fields/DataSourceFieldSelect/FieldSelect.vue +152 -0
  26. package/src/fields/DataSourceFieldSelect/Index.vue +168 -0
  27. package/src/fields/DataSourceFields.vue +329 -0
  28. package/src/fields/DataSourceInput.vue +339 -0
  29. package/src/fields/DataSourceMethodSelect.vue +153 -0
  30. package/src/fields/DataSourceMethods.vue +104 -0
  31. package/src/fields/DataSourceMocks.vue +255 -0
  32. package/src/fields/DataSourceSelect.vue +98 -0
  33. package/src/fields/DisplayConds.vue +145 -0
  34. package/src/fields/EventSelect.vue +375 -0
  35. package/src/fields/KeyValue.vue +137 -0
  36. package/src/fields/PageFragmentSelect.vue +63 -0
  37. package/src/fields/UISelect.vue +135 -0
  38. package/src/hooks/index.ts +24 -0
  39. package/src/hooks/use-code-block-edit.ts +83 -0
  40. package/src/hooks/use-data-source-edit.ts +46 -0
  41. package/src/hooks/use-data-source-method.ts +100 -0
  42. package/src/hooks/use-editor-content-height.ts +26 -0
  43. package/src/hooks/use-filter.ts +55 -0
  44. package/src/hooks/use-float-box.ts +76 -0
  45. package/src/hooks/use-getso.ts +35 -0
  46. package/src/hooks/use-next-float-box-position.ts +29 -0
  47. package/src/hooks/use-node-status.ts +48 -0
  48. package/src/hooks/use-stage.ts +129 -0
  49. package/src/hooks/use-window-rect.ts +20 -0
  50. package/src/icons/AppManageIcon.vue +15 -0
  51. package/src/icons/CenterIcon.vue +13 -0
  52. package/src/icons/CodeIcon.vue +28 -0
  53. package/src/icons/FolderMinusIcon.vue +22 -0
  54. package/src/icons/PinIcon.vue +25 -0
  55. package/src/icons/PinnedIcon.vue +25 -0
  56. package/src/index.ts +124 -0
  57. package/src/initService.ts +448 -0
  58. package/src/layouts/AddPageBox.vue +55 -0
  59. package/src/layouts/CodeEditor.vue +226 -0
  60. package/src/layouts/Framework.vue +165 -0
  61. package/src/layouts/NavMenu.vue +200 -0
  62. package/src/layouts/PropsPanel.vue +131 -0
  63. package/src/layouts/page-bar/AddButton.vue +48 -0
  64. package/src/layouts/page-bar/PageBar.vue +165 -0
  65. package/src/layouts/page-bar/PageBarScrollContainer.vue +192 -0
  66. package/src/layouts/page-bar/PageList.vue +55 -0
  67. package/src/layouts/page-bar/SwitchTypeButton.vue +45 -0
  68. package/src/layouts/sidebar/ComponentListPanel.vue +134 -0
  69. package/src/layouts/sidebar/Sidebar.vue +278 -0
  70. package/src/layouts/sidebar/code-block/CodeBlockList.vue +160 -0
  71. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +70 -0
  72. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +84 -0
  73. package/src/layouts/sidebar/data-source/DataSourceList.vue +172 -0
  74. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +106 -0
  75. package/src/layouts/sidebar/layer/LayerMenu.vue +128 -0
  76. package/src/layouts/sidebar/layer/LayerNodeTool.vue +32 -0
  77. package/src/layouts/sidebar/layer/LayerPanel.vue +119 -0
  78. package/src/layouts/sidebar/layer/use-click.ts +113 -0
  79. package/src/layouts/sidebar/layer/use-drag.ts +167 -0
  80. package/src/layouts/sidebar/layer/use-keybinding.ts +54 -0
  81. package/src/layouts/sidebar/layer/use-node-status.ts +114 -0
  82. package/src/layouts/workspace/Breadcrumb.vue +35 -0
  83. package/src/layouts/workspace/Workspace.vue +46 -0
  84. package/src/layouts/workspace/viewer/NodeListMenu.vue +115 -0
  85. package/src/layouts/workspace/viewer/Stage.vue +246 -0
  86. package/src/layouts/workspace/viewer/StageOverlay.vue +68 -0
  87. package/src/layouts/workspace/viewer/ViewerMenu.vue +145 -0
  88. package/src/services/BaseService.ts +230 -0
  89. package/src/services/codeBlock.ts +327 -0
  90. package/src/services/componentList.ts +58 -0
  91. package/src/services/dataSource.ts +216 -0
  92. package/src/services/dep.ts +152 -0
  93. package/src/services/editor.ts +1135 -0
  94. package/src/services/events.ts +93 -0
  95. package/src/services/history.ts +126 -0
  96. package/src/services/keybinding.ts +220 -0
  97. package/src/services/props.ts +286 -0
  98. package/src/services/stageOverlay.ts +212 -0
  99. package/src/services/storage.ts +155 -0
  100. package/src/services/ui.ts +157 -0
  101. package/src/shims-vue.d.ts +6 -0
  102. package/src/theme/breadcrumb.scss +6 -0
  103. package/src/theme/code-block.scss +6 -0
  104. package/src/theme/code-editor.scss +38 -0
  105. package/src/theme/common/var.scss +14 -0
  106. package/src/theme/component-list-panel.scss +95 -0
  107. package/src/theme/content-menu.scss +76 -0
  108. package/src/theme/data-source-field-select.scss +16 -0
  109. package/src/theme/data-source-field.scss +12 -0
  110. package/src/theme/data-source-fields.scss +13 -0
  111. package/src/theme/data-source-input.scss +18 -0
  112. package/src/theme/data-source-methods.scss +13 -0
  113. package/src/theme/data-source.scss +28 -0
  114. package/src/theme/event.scss +37 -0
  115. package/src/theme/floating-box.scss +32 -0
  116. package/src/theme/framework.scss +69 -0
  117. package/src/theme/icon.scss +12 -0
  118. package/src/theme/index.scss +10 -0
  119. package/src/theme/key-value.scss +20 -0
  120. package/src/theme/layer-panel.scss +26 -0
  121. package/src/theme/layout.scss +9 -0
  122. package/src/theme/nav-menu.scss +78 -0
  123. package/src/theme/page-bar.scss +102 -0
  124. package/src/theme/page-fragment-select.scss +14 -0
  125. package/src/theme/props-panel.scss +55 -0
  126. package/src/theme/resizer.scss +66 -0
  127. package/src/theme/ruler.scss +38 -0
  128. package/src/theme/search-input.scss +14 -0
  129. package/src/theme/sidebar.scss +79 -0
  130. package/src/theme/stage.scss +76 -0
  131. package/src/theme/theme.scss +28 -0
  132. package/src/theme/tree.scss +89 -0
  133. package/src/theme/workspace.scss +9 -0
  134. package/src/type.ts +780 -0
  135. package/src/utils/compose.ts +52 -0
  136. package/src/utils/config.ts +29 -0
  137. package/src/utils/content-menu.ts +95 -0
  138. package/src/utils/data-source/formConfigs/base.ts +30 -0
  139. package/src/utils/data-source/formConfigs/http.ts +60 -0
  140. package/src/utils/data-source/index.ts +249 -0
  141. package/src/utils/editor.ts +313 -0
  142. package/src/utils/idle-task.ts +72 -0
  143. package/src/utils/index.ts +24 -0
  144. package/src/utils/keybinding-config.ts +125 -0
  145. package/src/utils/logger.ts +47 -0
  146. package/src/utils/monaco-editor.ts +8 -0
  147. package/src/utils/operator.ts +106 -0
  148. package/src/utils/props.ts +407 -0
  149. package/src/utils/scroll-viewer.ts +165 -0
  150. package/src/utils/tree.ts +15 -0
  151. package/src/utils/undo-redo.ts +76 -0
  152. package/types/editorProps.d.ts +3 -1
  153. package/types/fields/CondOpSelect.vue.d.ts +17 -0
  154. package/types/fields/DataSourceFieldSelect/FieldSelect.vue.d.ts +36 -0
  155. package/types/fields/{DataSourceFieldSelect.vue.d.ts → DataSourceFieldSelect/Index.vue.d.ts} +1 -1
  156. package/types/fields/DisplayConds.vue.d.ts +32 -0
  157. package/types/index.d.ts +3 -1
  158. package/types/layouts/Framework.vue.d.ts +3 -1
  159. package/types/layouts/PropsPanel.vue.d.ts +21 -21
  160. package/types/layouts/page-bar/PageBar.vue.d.ts +6 -0
  161. package/types/layouts/page-bar/PageBarScrollContainer.vue.d.ts +3 -0
  162. package/types/layouts/page-bar/PageList.vue.d.ts +25 -0
  163. package/types/services/ui.d.ts +1 -0
  164. package/types/type.d.ts +30 -4
  165. package/types/utils/data-source/index.d.ts +1 -0
  166. package/types/utils/props.d.ts +12 -0
@@ -0,0 +1,339 @@
1
+ <template>
2
+ <component
3
+ v-if="disabled || isFocused"
4
+ :is="getConfig('components')?.autocomplete.component || 'el-autocomplete'"
5
+ class="tmagic-design-auto-complete"
6
+ ref="autocomplete"
7
+ v-model="state"
8
+ v-bind="
9
+ getConfig('components')?.autocomplete.props({
10
+ disabled,
11
+ size,
12
+ fetchSuggestions: querySearch,
13
+ triggerOnFocus: false,
14
+ clearable: true,
15
+ }) || {}
16
+ "
17
+ style="width: 100%"
18
+ @blur="blurHandler"
19
+ @input="inputHandler"
20
+ @select="selectHandler"
21
+ >
22
+ <template #suffix>
23
+ <Icon :icon="Coin" />
24
+ </template>
25
+ <template #default="{ item }">
26
+ <div style="display: flex; flex-direction: column; line-height: 1.2em">
27
+ <div>{{ item.text }}</div>
28
+ <span style="font-size: 10px; color: rgba(0, 0, 0, 0.6)">{{ item.value }}</span>
29
+ </div>
30
+ </template>
31
+ </component>
32
+ <div
33
+ :class="`tmagic-data-source-input-text el-input t-input t-size-${size?.[0]} el-input--${size}`"
34
+ @mouseup="mouseupHandler"
35
+ v-else
36
+ >
37
+ <div :class="`tmagic-data-source-input-text-wrapper el-input__wrapper ${isFocused ? ' is-focus' : ''}`">
38
+ <div class="el-input__inner t-input__inner">
39
+ <template v-for="(item, index) in displayState">
40
+ <span :key="index" v-if="item.type === 'text'" style="margin-right: 2px">{{ item.value }}</span>
41
+ <TMagicTag :key="index" :size="size" v-if="item.type === 'var'">{{ item.value }}</TMagicTag>
42
+ </template>
43
+
44
+ <Icon class="tmagic-data-source-input-icon" :icon="Coin" />
45
+ </div>
46
+ </div>
47
+ </div>
48
+ </template>
49
+
50
+ <script setup lang="ts">
51
+ import { computed, inject, nextTick, ref, watch } from 'vue';
52
+ import { Coin } from '@element-plus/icons-vue';
53
+
54
+ import { getConfig, TMagicAutocomplete, TMagicTag } from '@tmagic/design';
55
+ import type { FieldProps, FormItem } from '@tmagic/form';
56
+ import type { DataSchema, DataSourceSchema } from '@tmagic/schema';
57
+ import { isNumber } from '@tmagic/utils';
58
+
59
+ import Icon from '@editor/components/Icon.vue';
60
+ import type { Services } from '@editor/type';
61
+ import { getDisplayField } from '@editor/utils/data-source';
62
+
63
+ defineOptions({
64
+ name: 'MFieldsDataSourceInput',
65
+ });
66
+
67
+ const props = withDefaults(
68
+ defineProps<
69
+ FieldProps<
70
+ {
71
+ type: 'data-source-input';
72
+ } & FormItem
73
+ >
74
+ >(),
75
+ {
76
+ disabled: false,
77
+ },
78
+ );
79
+
80
+ const emit = defineEmits<{
81
+ change: [value: string];
82
+ }>();
83
+
84
+ const { dataSourceService } = inject<Services>('services') || {};
85
+
86
+ const autocomplete = ref<InstanceType<typeof TMagicAutocomplete>>();
87
+ const isFocused = ref(false);
88
+ const state = ref('');
89
+ const displayState = ref<{ value: string; type: 'var' | 'text' }[]>([]);
90
+
91
+ const input = computed<HTMLInputElement>(() => autocomplete.value?.inputRef?.input);
92
+ const dataSources = computed(() => dataSourceService?.get('dataSources') || []);
93
+
94
+ const setDisplayState = () => {
95
+ displayState.value = getDisplayField(dataSources.value, state.value);
96
+ };
97
+
98
+ watch(
99
+ () => props.model[props.name],
100
+ (value = '') => {
101
+ state.value = value;
102
+
103
+ setDisplayState();
104
+ },
105
+ { immediate: true },
106
+ );
107
+
108
+ const mouseupHandler = async () => {
109
+ const selection = globalThis.document.getSelection();
110
+ const anchorOffset = selection?.anchorOffset || 0;
111
+ const focusOffset = selection?.focusOffset || 0;
112
+
113
+ isFocused.value = true;
114
+ await nextTick();
115
+ autocomplete.value?.focus();
116
+
117
+ if (focusOffset && input.value) {
118
+ input.value.setSelectionRange(anchorOffset, focusOffset);
119
+ }
120
+ };
121
+
122
+ const blurHandler = () => {
123
+ isFocused.value = false;
124
+
125
+ setDisplayState();
126
+
127
+ emit('change', state.value);
128
+ };
129
+
130
+ const changeHandler = (v: string) => {
131
+ emit('change', v);
132
+ };
133
+
134
+ let inputText = '';
135
+
136
+ const inputHandler = (v: string) => {
137
+ if (!v) {
138
+ inputText = v;
139
+ }
140
+ };
141
+
142
+ /**
143
+ * 光标位置是不是}
144
+ * @param selectionStart 光标位置
145
+ */
146
+ const isRightCurlyBracket = (selectionStart = 0) => {
147
+ const lastChar = inputText.substring(selectionStart - 1, selectionStart);
148
+ return lastChar === '}';
149
+ };
150
+
151
+ /**
152
+ * 获取光标位置
153
+ */
154
+ const getSelectionStart = () => {
155
+ let selectionStart = input.value?.selectionStart || 0;
156
+
157
+ // 输入法可能会自动补全},如果当前光标前面一个字符是},则光标前移一位
158
+ if (isRightCurlyBracket(selectionStart)) {
159
+ selectionStart -= 1;
160
+ }
161
+
162
+ return selectionStart;
163
+ };
164
+
165
+ /**
166
+ * 当前输入的是{
167
+ * @param leftCurlyBracketIndex {字符索引
168
+ */
169
+ const curCharIsLeftCurlyBracket = (leftCurlyBracketIndex: number) =>
170
+ leftCurlyBracketIndex > 0 && leftCurlyBracketIndex === getSelectionStart() - 1;
171
+
172
+ /**
173
+ * 当前输入的是.
174
+ * @param leftCurlyBracketIndex .字符索引
175
+ */
176
+ const curCharIsDot = (dotIndex: number) => dotIndex > -1 && dotIndex === getSelectionStart() - 1;
177
+
178
+ /**
179
+ * @param leftCurlyBracketIndex 左大括号字符索引
180
+ * @param cb 建议的方法
181
+ */
182
+ const dsQuerySearch = (queryString: string, leftCurlyBracketIndex: number, cb: (data: { value: string }[]) => void) => {
183
+ let result: DataSourceSchema[] = [];
184
+
185
+ if (curCharIsLeftCurlyBracket(leftCurlyBracketIndex)) {
186
+ // 当前输入的是{
187
+ result = dataSources.value;
188
+ } else if (leftCurlyBracketIndex > 0) {
189
+ // 当前输入的是{xx
190
+ const queryName = queryString.substring(leftCurlyBracketIndex + 1).toLowerCase();
191
+ result = dataSources.value.filter((ds) => ds.title?.toLowerCase().includes(queryName) || ds.id.includes(queryName));
192
+ }
193
+
194
+ cb(
195
+ result.map((ds) => ({
196
+ value: ds.id,
197
+ text: ds.title,
198
+ type: 'dataSource',
199
+ })),
200
+ );
201
+ };
202
+
203
+ /**
204
+ * 字段提示
205
+ * @param queryString 当前输入框内的字符串
206
+ * @param leftAngleIndex {字符索引
207
+ * @param dotIndex .字符索引
208
+ * @param cb 建议回调
209
+ */
210
+ const fieldQuerySearch = (
211
+ queryString: string,
212
+ leftAngleIndex: number,
213
+ dotIndex: number,
214
+ cb: (data: { value: string }[]) => void,
215
+ ) => {
216
+ let result: DataSchema[] = [];
217
+
218
+ const dsKey = queryString.substring(leftAngleIndex + 1, dotIndex);
219
+
220
+ // 可能是xx.xx.xx,存在链式调用
221
+ const keys = dsKey.replaceAll(/\[(\d+)\]/g, '.$1').split('.');
222
+
223
+ // 最前的是数据源id
224
+ const dsId = keys.shift();
225
+ const ds = dataSources.value.find((ds) => ds.id === dsId);
226
+ if (!ds) {
227
+ cb([]);
228
+ return;
229
+ }
230
+
231
+ let fields = ds.fields || [];
232
+
233
+ // 后面这些是字段
234
+ let key = keys.shift();
235
+ while (key) {
236
+ if (isNumber(key)) {
237
+ key = keys.shift();
238
+ continue;
239
+ }
240
+
241
+ for (const field of fields) {
242
+ if (field.name === key) {
243
+ fields = field.fields || [];
244
+ key = keys.shift();
245
+ break;
246
+ }
247
+ }
248
+ }
249
+
250
+ if (curCharIsDot(dotIndex)) {
251
+ // 当前输入的是.
252
+ result = fields || [];
253
+ } else if (dotIndex > -1) {
254
+ const queryName = queryString.substring(dotIndex + 1).toLowerCase();
255
+ result =
256
+ fields.filter(
257
+ (field) => field.name?.toLowerCase().includes(queryName) || field.title?.toLowerCase().includes(queryName),
258
+ ) || [];
259
+ }
260
+
261
+ cb(
262
+ result.map((field) => ({
263
+ value: field.name,
264
+ text: field.title,
265
+ type: 'field',
266
+ })),
267
+ );
268
+ };
269
+
270
+ /**
271
+ * 数据源提示
272
+ * @param queryString 当前输入框内的字符串
273
+ * @param cb 建议回调
274
+ */
275
+ const querySearch = (queryString: string, cb: (data: { value: string }[]) => void) => {
276
+ inputText = queryString;
277
+
278
+ const selectionStart = getSelectionStart();
279
+
280
+ const curQueryString = queryString.substring(0, selectionStart);
281
+
282
+ const fieldKeyStringLastIndex = curQueryString.lastIndexOf('.');
283
+ const dsKeyStringLastIndex = curQueryString.lastIndexOf('${') + 1;
284
+
285
+ const isFieldTip = fieldKeyStringLastIndex > dsKeyStringLastIndex;
286
+
287
+ if (isFieldTip) {
288
+ fieldQuerySearch(curQueryString, dsKeyStringLastIndex, fieldKeyStringLastIndex, cb);
289
+ } else {
290
+ dsQuerySearch(curQueryString, dsKeyStringLastIndex, cb);
291
+ }
292
+ };
293
+
294
+ /**
295
+ * 选择建议
296
+ * @param value 建议值
297
+ * @param type 建议类型,是数据源还是字段
298
+ */
299
+ const selectHandler = async ({ value, type }: { value: string; type: 'dataSource' | 'field' }) => {
300
+ const isDataSource = type === 'dataSource';
301
+ const selectionStart = input.value?.selectionStart || 0;
302
+ let startText = inputText.substring(0, selectionStart);
303
+
304
+ const dotIndex = startText.lastIndexOf('.');
305
+ const leftCurlyBracketIndex = startText.lastIndexOf('${') + 1;
306
+
307
+ const endText = inputText.substring(selectionStart);
308
+
309
+ let suggestText = value;
310
+
311
+ if (isDataSource) {
312
+ if (!curCharIsLeftCurlyBracket(leftCurlyBracketIndex)) {
313
+ startText = startText.substring(0, leftCurlyBracketIndex + 1);
314
+ }
315
+
316
+ // 当前光标后一位是否为},不是的话需要补上
317
+ if (!isRightCurlyBracket(selectionStart + 1)) {
318
+ suggestText = `${suggestText}}`;
319
+ }
320
+ } else if (!curCharIsDot(dotIndex)) {
321
+ startText = startText.substring(0, dotIndex + 1);
322
+ }
323
+
324
+ state.value = `${startText}${suggestText}${endText}`;
325
+
326
+ await nextTick();
327
+
328
+ // 由于选择数据源时会在后面补全}, 所以光标要前移2位
329
+ let newSelectionStart = 0;
330
+ if (isDataSource) {
331
+ newSelectionStart = leftCurlyBracketIndex + suggestText.length;
332
+ } else {
333
+ newSelectionStart = dotIndex + suggestText.length + 1;
334
+ }
335
+ input.value?.setSelectionRange(newSelectionStart, newSelectionStart);
336
+
337
+ changeHandler(state.value);
338
+ };
339
+ </script>
@@ -0,0 +1,153 @@
1
+ <template>
2
+ <div class="m-fields-data-source-method-select">
3
+ <div class="data-source-method-select-container">
4
+ <MContainer
5
+ class="select"
6
+ :config="cascaderConfig"
7
+ :model="model"
8
+ :size="size"
9
+ @change="onChangeHandler"
10
+ ></MContainer>
11
+
12
+ <TMagicButton
13
+ v-if="model[name] && isCustomMethod && hasDataSourceSidePanel"
14
+ class="m-fields-select-action-button"
15
+ :size="size"
16
+ @click="editCodeHandler"
17
+ >
18
+ <MIcon :icon="!notEditable ? Edit : View"></MIcon>
19
+ </TMagicButton>
20
+ </div>
21
+
22
+ <CodeParams
23
+ v-if="paramsConfig.length"
24
+ name="params"
25
+ :model="model"
26
+ :size="size"
27
+ :disabled="disabled"
28
+ :params-config="paramsConfig"
29
+ @change="onChangeHandler"
30
+ ></CodeParams>
31
+ </div>
32
+ </template>
33
+
34
+ <script lang="ts" setup name="">
35
+ import { computed, inject, ref } from 'vue';
36
+ import { Edit, View } from '@element-plus/icons-vue';
37
+
38
+ import { TMagicButton } from '@tmagic/design';
39
+ import { createValues, type FieldProps, filterFunction, type FormState, MContainer } from '@tmagic/form';
40
+ import type { Id } from '@tmagic/schema';
41
+
42
+ import CodeParams from '@editor/components/CodeParams.vue';
43
+ import MIcon from '@editor/components/Icon.vue';
44
+ import type { CodeParamStatement, DataSourceMethodSelectConfig, EventBus, Services } from '@editor/type';
45
+ import { SideItemKey } from '@editor/type';
46
+
47
+ defineOptions({
48
+ name: 'MFieldsDataSourceMethodSelect',
49
+ });
50
+
51
+ const mForm = inject<FormState | undefined>('mForm');
52
+ const services = inject<Services>('services');
53
+ const eventBus = inject<EventBus>('eventBus');
54
+
55
+ const emit = defineEmits(['change']);
56
+
57
+ const dataSourceService = services?.dataSourceService;
58
+
59
+ const props = withDefaults(defineProps<FieldProps<DataSourceMethodSelectConfig>>(), {
60
+ disabled: false,
61
+ });
62
+
63
+ const hasDataSourceSidePanel = computed(() =>
64
+ (services?.uiService.get('sideBarItems') || []).find((item) => item.$key === SideItemKey.DATA_SOURCE),
65
+ );
66
+
67
+ const notEditable = computed(() => filterFunction(mForm, props.config.notEditable, props));
68
+
69
+ const dataSources = computed(() => dataSourceService?.get('dataSources'));
70
+
71
+ const isCustomMethod = computed(() => {
72
+ const [id, name] = props.model[props.name];
73
+
74
+ const dataSource = dataSourceService?.getDataSourceById(id);
75
+
76
+ return Boolean(dataSource?.methods.find((method) => method.name === name));
77
+ });
78
+
79
+ const getParamItemsConfig = ([dataSourceId, methodName]: [Id, string] = ['', '']): CodeParamStatement[] => {
80
+ if (!dataSourceId) return [];
81
+
82
+ const paramStatements = dataSources.value
83
+ ?.find((item) => item.id === dataSourceId)
84
+ ?.methods?.find((item) => item.name === methodName)?.params;
85
+
86
+ if (!paramStatements) return [];
87
+
88
+ return paramStatements.map((paramState: CodeParamStatement) => ({
89
+ text: paramState.name,
90
+ ...paramState,
91
+ }));
92
+ };
93
+
94
+ const paramsConfig = ref<CodeParamStatement[]>(getParamItemsConfig(props.model.dataSourceMethod));
95
+
96
+ const setParamsConfig = (dataSourceMethod: [Id, string], formState: any = {}) => {
97
+ // 通过下拉框选择的codeId变化后修正model的值,避免写入其他codeId的params
98
+ paramsConfig.value = dataSourceMethod ? getParamItemsConfig(dataSourceMethod) : [];
99
+
100
+ if (paramsConfig.value.length) {
101
+ props.model.params = createValues(formState, paramsConfig.value, {}, props.model.params);
102
+ } else {
103
+ props.model.params = {};
104
+ }
105
+ };
106
+
107
+ const methodsOptions = computed(
108
+ () =>
109
+ dataSources.value
110
+ ?.filter((ds) => ds.methods?.length || dataSourceService?.getFormMethod(ds.type).length)
111
+ ?.map((ds) => ({
112
+ label: ds.title || ds.id,
113
+ value: ds.id,
114
+ children: [
115
+ ...(dataSourceService?.getFormMethod(ds.type) || []),
116
+ ...(ds.methods || []).map((method) => ({
117
+ label: method.name,
118
+ value: method.name,
119
+ })),
120
+ ],
121
+ })) || [],
122
+ );
123
+
124
+ const cascaderConfig = computed(() => ({
125
+ type: 'cascader',
126
+ name: props.name,
127
+ options: methodsOptions.value,
128
+ disable: props.disabled,
129
+ onChange: (formState: any, dataSourceMethod: [Id, string]) => {
130
+ setParamsConfig(dataSourceMethod, formState);
131
+
132
+ return dataSourceMethod;
133
+ },
134
+ }));
135
+
136
+ /**
137
+ * 参数值修改更新
138
+ */
139
+ const onChangeHandler = (value: any) => {
140
+ props.model.params = value.params;
141
+ emit('change', props.model);
142
+ };
143
+
144
+ const editCodeHandler = () => {
145
+ const [id] = props.model[props.name];
146
+
147
+ const dataSource = dataSourceService?.getDataSourceById(id);
148
+
149
+ if (!dataSource) return;
150
+
151
+ eventBus?.emit('edit-data-source', id);
152
+ };
153
+ </script>
@@ -0,0 +1,104 @@
1
+ <template>
2
+ <div class="m-editor-data-source-methods">
3
+ <MagicTable :data="model[name]" :columns="methodColumns"></MagicTable>
4
+
5
+ <div class="m-editor-data-source-methods-footer">
6
+ <TMagicButton size="small" type="primary" :disabled="disabled" plain @click="createCodeHandler"
7
+ >添加</TMagicButton
8
+ >
9
+ </div>
10
+
11
+ <CodeBlockEditor
12
+ v-if="codeConfig"
13
+ ref="codeBlockEditor"
14
+ :disabled="disabled"
15
+ :content="codeConfig"
16
+ :is-data-source="true"
17
+ :data-source-type="model.type"
18
+ @submit="submitCodeHandler"
19
+ ></CodeBlockEditor>
20
+ </div>
21
+ </template>
22
+
23
+ <script setup lang="ts">
24
+ import { TMagicButton } from '@tmagic/design';
25
+ import type { FieldProps } from '@tmagic/form';
26
+ import type { CodeBlockContent } from '@tmagic/schema';
27
+ import { type ColumnConfig, MagicTable } from '@tmagic/table';
28
+
29
+ import CodeBlockEditor from '@editor/components/CodeBlockEditor.vue';
30
+ import { useDataSourceMethod } from '@editor/hooks/use-data-source-method';
31
+ import type { CodeParamStatement } from '@editor/type';
32
+
33
+ defineOptions({
34
+ name: 'MFieldsDataSourceMethods',
35
+ });
36
+
37
+ const props = withDefaults(
38
+ defineProps<
39
+ FieldProps<{
40
+ type: 'data-source-methods';
41
+ }>
42
+ >(),
43
+ {
44
+ disabled: false,
45
+ },
46
+ );
47
+
48
+ const emit = defineEmits(['change']);
49
+
50
+ const { codeConfig, codeBlockEditor, createCode, editCode, deleteCode, submitCode } = useDataSourceMethod();
51
+
52
+ const methodColumns: ColumnConfig[] = [
53
+ {
54
+ label: '名称',
55
+ prop: 'name',
56
+ },
57
+ {
58
+ label: '描述',
59
+ prop: 'desc',
60
+ },
61
+ {
62
+ label: '执行时机',
63
+ prop: 'timing',
64
+ },
65
+ {
66
+ label: '参数',
67
+ prop: 'params',
68
+ formatter: (params: CodeParamStatement[]) => params.map((item) => item.name).join(', '),
69
+ },
70
+ {
71
+ label: '操作',
72
+ fixed: 'right',
73
+ actions: [
74
+ {
75
+ text: '编辑',
76
+ handler: (row: CodeBlockContent) => {
77
+ editCode(props.model, row.name);
78
+ emit('change', props.model[props.name]);
79
+ },
80
+ },
81
+ {
82
+ text: '删除',
83
+ buttonType: 'danger',
84
+ handler: (row: CodeBlockContent) => {
85
+ deleteCode(props.model, row.name);
86
+ emit('change', props.model[props.name]);
87
+ },
88
+ },
89
+ ],
90
+ },
91
+ ];
92
+
93
+ const createCodeHandler = () => {
94
+ createCode(props.model);
95
+
96
+ emit('change', props.model[props.name]);
97
+ };
98
+
99
+ const submitCodeHandler = (values: CodeBlockContent) => {
100
+ submitCode(values);
101
+
102
+ emit('change', props.model[props.name]);
103
+ };
104
+ </script>