@tmagic/editor 1.4.8 → 1.4.9

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 (143) hide show
  1. package/package.json +11 -11
  2. package/src/Editor.vue +211 -0
  3. package/src/components/CodeBlockEditor.vue +268 -0
  4. package/src/components/CodeParams.vue +60 -0
  5. package/src/components/ContentMenu.vue +193 -0
  6. package/src/components/FloatingBox.vue +178 -0
  7. package/src/components/Icon.vue +27 -0
  8. package/src/components/Resizer.vue +23 -0
  9. package/src/components/ScrollBar.vue +151 -0
  10. package/src/components/ScrollViewer.vue +128 -0
  11. package/src/components/SearchInput.vue +37 -0
  12. package/src/components/SplitView.vue +186 -0
  13. package/src/components/ToolButton.vue +138 -0
  14. package/src/components/Tree.vue +71 -0
  15. package/src/components/TreeNode.vue +150 -0
  16. package/src/editorProps.ts +117 -0
  17. package/src/fields/Code.vue +50 -0
  18. package/src/fields/CodeLink.vue +80 -0
  19. package/src/fields/CodeSelect.vue +132 -0
  20. package/src/fields/CodeSelectCol.vue +139 -0
  21. package/src/fields/DataSourceFieldSelect.vue +149 -0
  22. package/src/fields/DataSourceFields.vue +323 -0
  23. package/src/fields/DataSourceInput.vue +339 -0
  24. package/src/fields/DataSourceMethodSelect.vue +153 -0
  25. package/src/fields/DataSourceMethods.vue +104 -0
  26. package/src/fields/DataSourceMocks.vue +255 -0
  27. package/src/fields/DataSourceSelect.vue +98 -0
  28. package/src/fields/EventSelect.vue +375 -0
  29. package/src/fields/KeyValue.vue +137 -0
  30. package/src/fields/PageFragmentSelect.vue +63 -0
  31. package/src/fields/UISelect.vue +135 -0
  32. package/src/hooks/index.ts +24 -0
  33. package/src/hooks/use-code-block-edit.ts +83 -0
  34. package/src/hooks/use-data-source-edit.ts +46 -0
  35. package/src/hooks/use-data-source-method.ts +100 -0
  36. package/src/hooks/use-editor-content-height.ts +26 -0
  37. package/src/hooks/use-filter.ts +55 -0
  38. package/src/hooks/use-float-box.ts +76 -0
  39. package/src/hooks/use-getso.ts +35 -0
  40. package/src/hooks/use-next-float-box-position.ts +29 -0
  41. package/src/hooks/use-node-status.ts +48 -0
  42. package/src/hooks/use-stage.ts +129 -0
  43. package/src/hooks/use-window-rect.ts +20 -0
  44. package/src/icons/AppManageIcon.vue +15 -0
  45. package/src/icons/CenterIcon.vue +13 -0
  46. package/src/icons/CodeIcon.vue +28 -0
  47. package/src/icons/FolderMinusIcon.vue +22 -0
  48. package/src/icons/PinIcon.vue +25 -0
  49. package/src/icons/PinnedIcon.vue +25 -0
  50. package/src/index.ts +118 -0
  51. package/src/initService.ts +448 -0
  52. package/src/layouts/AddPageBox.vue +55 -0
  53. package/src/layouts/CodeEditor.vue +226 -0
  54. package/src/layouts/Framework.vue +163 -0
  55. package/src/layouts/NavMenu.vue +200 -0
  56. package/src/layouts/PropsPanel.vue +131 -0
  57. package/src/layouts/page-bar/AddButton.vue +48 -0
  58. package/src/layouts/page-bar/PageBar.vue +159 -0
  59. package/src/layouts/page-bar/PageBarScrollContainer.vue +156 -0
  60. package/src/layouts/page-bar/SwitchTypeButton.vue +45 -0
  61. package/src/layouts/sidebar/ComponentListPanel.vue +134 -0
  62. package/src/layouts/sidebar/Sidebar.vue +278 -0
  63. package/src/layouts/sidebar/code-block/CodeBlockList.vue +160 -0
  64. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +70 -0
  65. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +84 -0
  66. package/src/layouts/sidebar/data-source/DataSourceList.vue +172 -0
  67. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +106 -0
  68. package/src/layouts/sidebar/layer/LayerMenu.vue +128 -0
  69. package/src/layouts/sidebar/layer/LayerNodeTool.vue +32 -0
  70. package/src/layouts/sidebar/layer/LayerPanel.vue +119 -0
  71. package/src/layouts/sidebar/layer/use-click.ts +113 -0
  72. package/src/layouts/sidebar/layer/use-drag.ts +167 -0
  73. package/src/layouts/sidebar/layer/use-keybinding.ts +54 -0
  74. package/src/layouts/sidebar/layer/use-node-status.ts +114 -0
  75. package/src/layouts/workspace/Breadcrumb.vue +35 -0
  76. package/src/layouts/workspace/Workspace.vue +46 -0
  77. package/src/layouts/workspace/viewer/NodeListMenu.vue +115 -0
  78. package/src/layouts/workspace/viewer/Stage.vue +246 -0
  79. package/src/layouts/workspace/viewer/StageOverlay.vue +68 -0
  80. package/src/layouts/workspace/viewer/ViewerMenu.vue +145 -0
  81. package/src/services/BaseService.ts +230 -0
  82. package/src/services/codeBlock.ts +327 -0
  83. package/src/services/componentList.ts +58 -0
  84. package/src/services/dataSource.ts +216 -0
  85. package/src/services/dep.ts +152 -0
  86. package/src/services/editor.ts +1135 -0
  87. package/src/services/events.ts +93 -0
  88. package/src/services/history.ts +126 -0
  89. package/src/services/keybinding.ts +220 -0
  90. package/src/services/props.ts +286 -0
  91. package/src/services/stageOverlay.ts +212 -0
  92. package/src/services/storage.ts +155 -0
  93. package/src/services/ui.ts +156 -0
  94. package/src/shims-vue.d.ts +6 -0
  95. package/src/theme/breadcrumb.scss +6 -0
  96. package/src/theme/code-block.scss +6 -0
  97. package/src/theme/code-editor.scss +38 -0
  98. package/src/theme/common/var.scss +14 -0
  99. package/src/theme/component-list-panel.scss +95 -0
  100. package/src/theme/content-menu.scss +76 -0
  101. package/src/theme/data-source-field.scss +12 -0
  102. package/src/theme/data-source-fields.scss +13 -0
  103. package/src/theme/data-source-input.scss +18 -0
  104. package/src/theme/data-source-methods.scss +13 -0
  105. package/src/theme/data-source.scss +28 -0
  106. package/src/theme/event.scss +37 -0
  107. package/src/theme/floating-box.scss +32 -0
  108. package/src/theme/framework.scss +69 -0
  109. package/src/theme/icon.scss +12 -0
  110. package/src/theme/index.scss +10 -0
  111. package/src/theme/key-value.scss +20 -0
  112. package/src/theme/layer-panel.scss +26 -0
  113. package/src/theme/layout.scss +9 -0
  114. package/src/theme/nav-menu.scss +78 -0
  115. package/src/theme/page-bar.scss +89 -0
  116. package/src/theme/page-fragment-select.scss +14 -0
  117. package/src/theme/props-panel.scss +55 -0
  118. package/src/theme/resizer.scss +66 -0
  119. package/src/theme/ruler.scss +38 -0
  120. package/src/theme/search-input.scss +14 -0
  121. package/src/theme/sidebar.scss +79 -0
  122. package/src/theme/stage.scss +76 -0
  123. package/src/theme/theme.scss +27 -0
  124. package/src/theme/tree.scss +89 -0
  125. package/src/theme/workspace.scss +9 -0
  126. package/src/type.ts +749 -0
  127. package/src/utils/compose.ts +52 -0
  128. package/src/utils/config.ts +29 -0
  129. package/src/utils/content-menu.ts +95 -0
  130. package/src/utils/data-source/formConfigs/base.ts +30 -0
  131. package/src/utils/data-source/formConfigs/http.ts +60 -0
  132. package/src/utils/data-source/index.ts +243 -0
  133. package/src/utils/editor.ts +313 -0
  134. package/src/utils/idle-task.ts +72 -0
  135. package/src/utils/index.ts +24 -0
  136. package/src/utils/keybinding-config.ts +125 -0
  137. package/src/utils/logger.ts +47 -0
  138. package/src/utils/monaco-editor.ts +8 -0
  139. package/src/utils/operator.ts +106 -0
  140. package/src/utils/props.ts +504 -0
  141. package/src/utils/scroll-viewer.ts +165 -0
  142. package/src/utils/tree.ts +15 -0
  143. package/src/utils/undo-redo.ts +76 -0
@@ -0,0 +1,323 @@
1
+ <template>
2
+ <div class="m-editor-data-source-fields">
3
+ <MagicTable :data="model[name]" :columns="fieldColumns"></MagicTable>
4
+
5
+ <div class="m-editor-data-source-fields-footer">
6
+ <TMagicButton size="small" :disabled="disabled" plain @click="newFromJsonHandler()">快速添加</TMagicButton>
7
+ <TMagicButton size="small" type="primary" :disabled="disabled" plain @click="newHandler()">添加</TMagicButton>
8
+ </div>
9
+
10
+ <FloatingBox
11
+ v-model:visible="addDialogVisible"
12
+ v-model:width="width"
13
+ v-model:height="editorHeight"
14
+ :title="fieldTitle"
15
+ :position="boxPosition"
16
+ >
17
+ <template #body>
18
+ <MFormBox
19
+ label-width="80px"
20
+ :title="fieldTitle"
21
+ :config="dataSourceFieldsConfig"
22
+ :values="fieldValues"
23
+ :parentValues="model[name]"
24
+ :disabled="disabled"
25
+ @submit="fieldChange"
26
+ ></MFormBox>
27
+ </template>
28
+ </FloatingBox>
29
+
30
+ <FloatingBox
31
+ v-model:visible="addFromJsonDialogVisible"
32
+ v-model:width="width"
33
+ v-model:height="editorHeight"
34
+ title="快速添加数据定义"
35
+ :position="boxPosition"
36
+ >
37
+ <template #body>
38
+ <MFormBox
39
+ :config="jsonFromConfig"
40
+ :values="jsonFromValues"
41
+ :disabled="disabled"
42
+ @submit="addFromJsonFromChange"
43
+ ></MFormBox>
44
+ </template>
45
+ </FloatingBox>
46
+ </div>
47
+ </template>
48
+
49
+ <script setup lang="ts">
50
+ import { inject, Ref, ref } from 'vue';
51
+
52
+ import { TMagicButton, tMagicMessage, tMagicMessageBox } from '@tmagic/design';
53
+ import { type FieldProps, type FormConfig, type FormState, MFormBox } from '@tmagic/form';
54
+ import type { DataSchema } from '@tmagic/schema';
55
+ import { type ColumnConfig, MagicTable } from '@tmagic/table';
56
+ import { getDefaultValueFromFields } from '@tmagic/utils';
57
+
58
+ import FloatingBox from '@editor/components/FloatingBox.vue';
59
+ import { useEditorContentHeight } from '@editor/hooks';
60
+ import { useNextFloatBoxPosition } from '@editor/hooks/use-next-float-box-position';
61
+ import type { Services } from '@editor/type';
62
+
63
+ defineOptions({
64
+ name: 'MFieldsDataSourceFields',
65
+ });
66
+
67
+ const props = withDefaults(
68
+ defineProps<
69
+ FieldProps<{
70
+ type: 'data-source-fields';
71
+ }>
72
+ >(),
73
+ {
74
+ disabled: false,
75
+ },
76
+ );
77
+
78
+ const emit = defineEmits(['change']);
79
+
80
+ const services = inject<Services>('services');
81
+
82
+ const fieldValues = ref<Record<string, any>>({});
83
+ const fieldTitle = ref('');
84
+
85
+ const width = defineModel<number>('width', { default: 670 });
86
+
87
+ const newHandler = () => {
88
+ fieldValues.value = {};
89
+ fieldTitle.value = '新增属性';
90
+ calcBoxPosition();
91
+ addDialogVisible.value = true;
92
+ };
93
+
94
+ const fieldChange = ({ index, ...value }: Record<string, any>) => {
95
+ if (index > -1) {
96
+ props.model[props.name][index] = value;
97
+ } else {
98
+ props.model[props.name].push(value);
99
+ }
100
+
101
+ addDialogVisible.value = false;
102
+
103
+ emit('change', props.model[props.name]);
104
+ };
105
+
106
+ const fieldColumns: ColumnConfig[] = [
107
+ {
108
+ label: '属性名称',
109
+ prop: 'title',
110
+ },
111
+ {
112
+ label: '属性key',
113
+ prop: 'name',
114
+ },
115
+ {
116
+ label: '属性描述',
117
+ prop: 'description',
118
+ },
119
+ {
120
+ label: '默认值',
121
+ prop: 'defaultValue',
122
+ formatter(item, row) {
123
+ try {
124
+ return JSON.stringify(row.defaultValue);
125
+ } catch (e) {
126
+ return row.defaultValue;
127
+ }
128
+ },
129
+ },
130
+ {
131
+ label: '操作',
132
+ fixed: 'right',
133
+ actions: [
134
+ {
135
+ text: '编辑',
136
+ handler: (row: Record<string, any>, index: number) => {
137
+ fieldValues.value = {
138
+ ...row,
139
+ index,
140
+ };
141
+ fieldTitle.value = `编辑${row.title}`;
142
+ calcBoxPosition();
143
+ addDialogVisible.value = true;
144
+ },
145
+ },
146
+ {
147
+ text: '删除',
148
+ buttonType: 'danger',
149
+ handler: async (row: Record<string, any>, index: number) => {
150
+ await tMagicMessageBox.confirm(`确定删除${row.title}(${row.name})?`, '提示');
151
+ props.model[props.name].splice(index, 1);
152
+ emit('change', props.model[props.name]);
153
+ },
154
+ },
155
+ ],
156
+ },
157
+ ];
158
+
159
+ const dataSourceFieldsConfig: FormConfig = [
160
+ { name: 'index', type: 'hidden', filter: 'number', defaultValue: -1 },
161
+ {
162
+ name: 'type',
163
+ text: '数据类型',
164
+ type: 'select',
165
+ defaultValue: 'string',
166
+ options: [
167
+ { text: '字符串', value: 'string' },
168
+ { text: '数字', value: 'number' },
169
+ { text: '布尔值', value: 'boolean' },
170
+ { text: '对象', value: 'object' },
171
+ { text: '数组', value: 'array' },
172
+ { text: 'null', value: 'null' },
173
+ { text: 'any', value: 'any' },
174
+ ],
175
+ },
176
+ {
177
+ name: 'name',
178
+ text: '字段名称',
179
+ rules: [
180
+ {
181
+ required: true,
182
+ message: '请输入字段名称',
183
+ },
184
+ {
185
+ validator: ({ value, callback }, { model, parent }) => {
186
+ const index = parent.findIndex((item: Record<string, any>) => item.name === value);
187
+ if ((model.index === -1 && index > -1) || (model.index > -1 && index > -1 && index !== model.index)) {
188
+ return callback(`属性key(${value})已存在`);
189
+ }
190
+ callback();
191
+ },
192
+ },
193
+ ],
194
+ },
195
+ {
196
+ name: 'title',
197
+ text: '展示名称',
198
+ rules: [
199
+ {
200
+ required: true,
201
+ message: '请输入展示名称',
202
+ },
203
+ ],
204
+ },
205
+ {
206
+ name: 'description',
207
+ text: '描述',
208
+ type: 'textarea',
209
+ },
210
+ {
211
+ name: 'defaultValue',
212
+ text: '默认值',
213
+ height: '200px',
214
+ parse: true,
215
+ type: (mForm: FormState | undefined, { model }: any) => {
216
+ if (model.type === 'number') return 'number';
217
+ if (model.type === 'boolean') return 'select';
218
+ if (model.type === 'string') return 'text';
219
+
220
+ return 'vs-code';
221
+ },
222
+ options: [
223
+ { text: 'true', value: true },
224
+ { text: 'false', value: false },
225
+ ],
226
+ },
227
+ {
228
+ name: 'enable',
229
+ text: '是否可用',
230
+ type: 'switch',
231
+ defaultValue: true,
232
+ },
233
+ {
234
+ name: 'fields',
235
+ type: 'data-source-fields',
236
+ defaultValue: [],
237
+ display: (formState: FormState | undefined, { model }: any) => model.type === 'object' || model.type === 'array',
238
+ },
239
+ ];
240
+
241
+ const jsonFromConfig: FormConfig = [
242
+ {
243
+ name: 'data',
244
+ type: 'vs-code',
245
+ labelWidth: '0',
246
+ language: 'json',
247
+ height: '600px',
248
+ options: inject('codeOptions', {}),
249
+ },
250
+ ];
251
+
252
+ const jsonFromValues = ref({
253
+ data: {},
254
+ });
255
+
256
+ const newFromJsonHandler = () => {
257
+ jsonFromValues.value.data = getDefaultValueFromFields(props.model[props.name]);
258
+ calcBoxPosition();
259
+ addFromJsonDialogVisible.value = true;
260
+ };
261
+
262
+ const getValueType = (value: any) => {
263
+ if (Array.isArray(value)) return 'array';
264
+ if (value === null) return 'null';
265
+ if (typeof value === 'object') return 'object';
266
+ if (typeof value === 'number') return 'number';
267
+ if (typeof value === 'boolean') return 'boolean';
268
+ if (typeof value === 'string') return 'string';
269
+
270
+ return 'any';
271
+ };
272
+
273
+ const getFieldsConfig = (value: any, fields: DataSchema[] = []) => {
274
+ if (!value || typeof value !== 'object') throw new Error('数据格式错误');
275
+
276
+ const newFields: DataSchema[] = [];
277
+ Object.entries(value).forEach(([key, value]) => {
278
+ const type = getValueType(value);
279
+
280
+ const oldField = fields.find((field) => field.name === key);
281
+
282
+ let childFields: DataSchema[] = [];
283
+ if (Array.isArray(value) && value.length > 0) {
284
+ childFields = getFieldsConfig(value[0], oldField?.fields);
285
+ } else if (type === 'object') {
286
+ childFields = getFieldsConfig(value, oldField?.fields);
287
+ }
288
+
289
+ newFields.push({
290
+ name: key,
291
+ title: oldField?.title || key,
292
+ type,
293
+ description: oldField?.description || '',
294
+ enable: oldField?.enable ?? true,
295
+ defaultValue: value,
296
+ fields: childFields,
297
+ });
298
+ });
299
+
300
+ return newFields;
301
+ };
302
+
303
+ const addFromJsonFromChange = ({ data }: { data: string }) => {
304
+ try {
305
+ const value = JSON.parse(data);
306
+
307
+ props.model[props.name] = getFieldsConfig(value, props.model[props.name]);
308
+
309
+ addFromJsonDialogVisible.value = false;
310
+
311
+ emit('change', props.model[props.name]);
312
+ } catch (e: any) {
313
+ tMagicMessage.error(e.message);
314
+ }
315
+ };
316
+
317
+ const addDialogVisible = defineModel<boolean>('visible', { default: false });
318
+ const addFromJsonDialogVisible = defineModel<boolean>('visible1', { default: false });
319
+ const { height: editorHeight } = useEditorContentHeight();
320
+
321
+ const parentFloating = inject<Ref<HTMLDivElement | null>>('parentFloating', ref(null));
322
+ const { boxPosition, calcBoxPosition } = useNextFloatBoxPosition(services?.uiService, parentFloating);
323
+ </script>
@@ -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>