@tmagic/editor 1.4.7 → 1.4.8

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 (152) hide show
  1. package/dist/style.css +1 -1
  2. package/dist/tmagic-editor.js +314 -452
  3. package/dist/tmagic-editor.umd.cjs +313 -450
  4. package/package.json +15 -11
  5. package/types/layouts/sidebar/layer/use-click.d.ts +1 -1
  6. package/types/services/editor.d.ts +19 -2
  7. package/types/services/props.d.ts +5 -5
  8. package/types/type.d.ts +6 -0
  9. package/types/utils/editor.d.ts +1 -0
  10. package/src/Editor.vue +0 -211
  11. package/src/components/CodeBlockEditor.vue +0 -268
  12. package/src/components/CodeParams.vue +0 -60
  13. package/src/components/ContentMenu.vue +0 -193
  14. package/src/components/FloatingBox.vue +0 -178
  15. package/src/components/Icon.vue +0 -27
  16. package/src/components/Resizer.vue +0 -23
  17. package/src/components/ScrollBar.vue +0 -151
  18. package/src/components/ScrollViewer.vue +0 -128
  19. package/src/components/SearchInput.vue +0 -37
  20. package/src/components/SplitView.vue +0 -186
  21. package/src/components/ToolButton.vue +0 -138
  22. package/src/components/Tree.vue +0 -71
  23. package/src/components/TreeNode.vue +0 -150
  24. package/src/editorProps.ts +0 -117
  25. package/src/fields/Code.vue +0 -50
  26. package/src/fields/CodeLink.vue +0 -80
  27. package/src/fields/CodeSelect.vue +0 -132
  28. package/src/fields/CodeSelectCol.vue +0 -139
  29. package/src/fields/DataSourceFieldSelect.vue +0 -149
  30. package/src/fields/DataSourceFields.vue +0 -323
  31. package/src/fields/DataSourceInput.vue +0 -331
  32. package/src/fields/DataSourceMethodSelect.vue +0 -153
  33. package/src/fields/DataSourceMethods.vue +0 -104
  34. package/src/fields/DataSourceMocks.vue +0 -255
  35. package/src/fields/DataSourceSelect.vue +0 -98
  36. package/src/fields/EventSelect.vue +0 -375
  37. package/src/fields/KeyValue.vue +0 -137
  38. package/src/fields/PageFragmentSelect.vue +0 -63
  39. package/src/fields/UISelect.vue +0 -135
  40. package/src/hooks/index.ts +0 -24
  41. package/src/hooks/use-code-block-edit.ts +0 -83
  42. package/src/hooks/use-data-source-edit.ts +0 -46
  43. package/src/hooks/use-data-source-method.ts +0 -100
  44. package/src/hooks/use-editor-content-height.ts +0 -26
  45. package/src/hooks/use-filter.ts +0 -55
  46. package/src/hooks/use-float-box.ts +0 -76
  47. package/src/hooks/use-getso.ts +0 -35
  48. package/src/hooks/use-next-float-box-position.ts +0 -29
  49. package/src/hooks/use-node-status.ts +0 -48
  50. package/src/hooks/use-stage.ts +0 -129
  51. package/src/hooks/use-window-rect.ts +0 -20
  52. package/src/icons/AppManageIcon.vue +0 -15
  53. package/src/icons/CenterIcon.vue +0 -13
  54. package/src/icons/CodeIcon.vue +0 -28
  55. package/src/icons/FolderMinusIcon.vue +0 -22
  56. package/src/icons/PinIcon.vue +0 -25
  57. package/src/icons/PinnedIcon.vue +0 -25
  58. package/src/index.ts +0 -118
  59. package/src/initService.ts +0 -443
  60. package/src/layouts/AddPageBox.vue +0 -55
  61. package/src/layouts/CodeEditor.vue +0 -226
  62. package/src/layouts/Framework.vue +0 -163
  63. package/src/layouts/NavMenu.vue +0 -200
  64. package/src/layouts/PropsPanel.vue +0 -130
  65. package/src/layouts/page-bar/AddButton.vue +0 -48
  66. package/src/layouts/page-bar/PageBar.vue +0 -159
  67. package/src/layouts/page-bar/PageBarScrollContainer.vue +0 -156
  68. package/src/layouts/page-bar/SwitchTypeButton.vue +0 -45
  69. package/src/layouts/sidebar/ComponentListPanel.vue +0 -134
  70. package/src/layouts/sidebar/Sidebar.vue +0 -278
  71. package/src/layouts/sidebar/code-block/CodeBlockList.vue +0 -160
  72. package/src/layouts/sidebar/code-block/CodeBlockListPanel.vue +0 -70
  73. package/src/layouts/sidebar/data-source/DataSourceConfigPanel.vue +0 -84
  74. package/src/layouts/sidebar/data-source/DataSourceList.vue +0 -172
  75. package/src/layouts/sidebar/data-source/DataSourceListPanel.vue +0 -106
  76. package/src/layouts/sidebar/layer/LayerMenu.vue +0 -128
  77. package/src/layouts/sidebar/layer/LayerNodeTool.vue +0 -32
  78. package/src/layouts/sidebar/layer/LayerPanel.vue +0 -119
  79. package/src/layouts/sidebar/layer/use-click.ts +0 -110
  80. package/src/layouts/sidebar/layer/use-drag.ts +0 -161
  81. package/src/layouts/sidebar/layer/use-keybinding.ts +0 -54
  82. package/src/layouts/sidebar/layer/use-node-status.ts +0 -114
  83. package/src/layouts/workspace/Breadcrumb.vue +0 -35
  84. package/src/layouts/workspace/Workspace.vue +0 -46
  85. package/src/layouts/workspace/viewer/NodeListMenu.vue +0 -115
  86. package/src/layouts/workspace/viewer/Stage.vue +0 -246
  87. package/src/layouts/workspace/viewer/StageOverlay.vue +0 -68
  88. package/src/layouts/workspace/viewer/ViewerMenu.vue +0 -145
  89. package/src/services/BaseService.ts +0 -230
  90. package/src/services/codeBlock.ts +0 -327
  91. package/src/services/componentList.ts +0 -58
  92. package/src/services/dataSource.ts +0 -216
  93. package/src/services/dep.ts +0 -152
  94. package/src/services/editor.ts +0 -1096
  95. package/src/services/events.ts +0 -93
  96. package/src/services/history.ts +0 -126
  97. package/src/services/keybinding.ts +0 -220
  98. package/src/services/props.ts +0 -271
  99. package/src/services/stageOverlay.ts +0 -212
  100. package/src/services/storage.ts +0 -155
  101. package/src/services/ui.ts +0 -156
  102. package/src/shims-vue.d.ts +0 -6
  103. package/src/theme/breadcrumb.scss +0 -6
  104. package/src/theme/code-block.scss +0 -6
  105. package/src/theme/code-editor.scss +0 -38
  106. package/src/theme/common/var.scss +0 -14
  107. package/src/theme/component-list-panel.scss +0 -95
  108. package/src/theme/content-menu.scss +0 -76
  109. package/src/theme/data-source-field.scss +0 -12
  110. package/src/theme/data-source-fields.scss +0 -13
  111. package/src/theme/data-source-input.scss +0 -18
  112. package/src/theme/data-source-methods.scss +0 -13
  113. package/src/theme/data-source.scss +0 -28
  114. package/src/theme/event.scss +0 -37
  115. package/src/theme/floating-box.scss +0 -32
  116. package/src/theme/framework.scss +0 -69
  117. package/src/theme/icon.scss +0 -12
  118. package/src/theme/index.scss +0 -10
  119. package/src/theme/key-value.scss +0 -20
  120. package/src/theme/layer-panel.scss +0 -26
  121. package/src/theme/layout.scss +0 -9
  122. package/src/theme/nav-menu.scss +0 -78
  123. package/src/theme/page-bar.scss +0 -89
  124. package/src/theme/page-fragment-select.scss +0 -14
  125. package/src/theme/props-panel.scss +0 -55
  126. package/src/theme/resizer.scss +0 -66
  127. package/src/theme/ruler.scss +0 -38
  128. package/src/theme/search-input.scss +0 -14
  129. package/src/theme/sidebar.scss +0 -79
  130. package/src/theme/stage.scss +0 -76
  131. package/src/theme/theme.scss +0 -27
  132. package/src/theme/tree.scss +0 -89
  133. package/src/theme/workspace.scss +0 -9
  134. package/src/type.ts +0 -746
  135. package/src/utils/compose.ts +0 -52
  136. package/src/utils/config.ts +0 -29
  137. package/src/utils/content-menu.ts +0 -95
  138. package/src/utils/data-source/formConfigs/base.ts +0 -30
  139. package/src/utils/data-source/formConfigs/http.ts +0 -60
  140. package/src/utils/data-source/index.ts +0 -243
  141. package/src/utils/editor.ts +0 -294
  142. package/src/utils/idle-task.ts +0 -72
  143. package/src/utils/index.ts +0 -24
  144. package/src/utils/keybinding-config.ts +0 -125
  145. package/src/utils/logger.ts +0 -47
  146. package/src/utils/monaco-editor.ts +0 -8
  147. package/src/utils/operator.ts +0 -106
  148. package/src/utils/props.ts +0 -504
  149. package/src/utils/scroll-viewer.ts +0 -165
  150. package/src/utils/tree.ts +0 -15
  151. package/src/utils/undo-redo.ts +0 -76
  152. package/tsconfig.build.json +0 -16
@@ -1,323 +0,0 @@
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>
@@ -1,331 +0,0 @@
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
- isFocused.value = true;
110
- await nextTick();
111
- autocomplete.value?.focus();
112
- };
113
-
114
- const blurHandler = () => {
115
- isFocused.value = false;
116
-
117
- setDisplayState();
118
-
119
- emit('change', state.value);
120
- };
121
-
122
- const changeHandler = (v: string) => {
123
- emit('change', v);
124
- };
125
-
126
- let inputText = '';
127
-
128
- const inputHandler = (v: string) => {
129
- if (!v) {
130
- inputText = v;
131
- }
132
- };
133
-
134
- /**
135
- * 光标位置是不是}
136
- * @param selectionStart 光标位置
137
- */
138
- const isRightCurlyBracket = (selectionStart = 0) => {
139
- const lastChar = inputText.substring(selectionStart - 1, selectionStart);
140
- return lastChar === '}';
141
- };
142
-
143
- /**
144
- * 获取光标位置
145
- */
146
- const getSelectionStart = () => {
147
- let selectionStart = input.value?.selectionStart || 0;
148
-
149
- // 输入法可能会自动补全},如果当前光标前面一个字符是},则光标前移一位
150
- if (isRightCurlyBracket(selectionStart)) {
151
- selectionStart -= 1;
152
- }
153
-
154
- return selectionStart;
155
- };
156
-
157
- /**
158
- * 当前输入的是{
159
- * @param leftCurlyBracketIndex {字符索引
160
- */
161
- const curCharIsLeftCurlyBracket = (leftCurlyBracketIndex: number) =>
162
- leftCurlyBracketIndex > 0 && leftCurlyBracketIndex === getSelectionStart() - 1;
163
-
164
- /**
165
- * 当前输入的是.
166
- * @param leftCurlyBracketIndex .字符索引
167
- */
168
- const curCharIsDot = (dotIndex: number) => dotIndex > -1 && dotIndex === getSelectionStart() - 1;
169
-
170
- /**
171
- * @param leftCurlyBracketIndex 左大括号字符索引
172
- * @param cb 建议的方法
173
- */
174
- const dsQuerySearch = (queryString: string, leftCurlyBracketIndex: number, cb: (data: { value: string }[]) => void) => {
175
- let result: DataSourceSchema[] = [];
176
-
177
- if (curCharIsLeftCurlyBracket(leftCurlyBracketIndex)) {
178
- // 当前输入的是{
179
- result = dataSources.value;
180
- } else if (leftCurlyBracketIndex > 0) {
181
- // 当前输入的是{xx
182
- const queryName = queryString.substring(leftCurlyBracketIndex + 1).toLowerCase();
183
- result = dataSources.value.filter((ds) => ds.title?.toLowerCase().includes(queryName) || ds.id.includes(queryName));
184
- }
185
-
186
- cb(
187
- result.map((ds) => ({
188
- value: ds.id,
189
- text: ds.title,
190
- type: 'dataSource',
191
- })),
192
- );
193
- };
194
-
195
- /**
196
- * 字段提示
197
- * @param queryString 当前输入框内的字符串
198
- * @param leftAngleIndex {字符索引
199
- * @param dotIndex .字符索引
200
- * @param cb 建议回调
201
- */
202
- const fieldQuerySearch = (
203
- queryString: string,
204
- leftAngleIndex: number,
205
- dotIndex: number,
206
- cb: (data: { value: string }[]) => void,
207
- ) => {
208
- let result: DataSchema[] = [];
209
-
210
- const dsKey = queryString.substring(leftAngleIndex + 1, dotIndex);
211
-
212
- // 可能是xx.xx.xx,存在链式调用
213
- const keys = dsKey.replaceAll(/\[(\d+)\]/g, '.$1').split('.');
214
-
215
- // 最前的是数据源id
216
- const dsId = keys.shift();
217
- const ds = dataSources.value.find((ds) => ds.id === dsId);
218
- if (!ds) {
219
- cb([]);
220
- return;
221
- }
222
-
223
- let fields = ds.fields || [];
224
-
225
- // 后面这些是字段
226
- let key = keys.shift();
227
- while (key) {
228
- if (isNumber(key)) {
229
- key = keys.shift();
230
- continue;
231
- }
232
-
233
- for (const field of fields) {
234
- if (field.name === key) {
235
- fields = field.fields || [];
236
- key = keys.shift();
237
- break;
238
- }
239
- }
240
- }
241
-
242
- if (curCharIsDot(dotIndex)) {
243
- // 当前输入的是.
244
- result = fields || [];
245
- } else if (dotIndex > -1) {
246
- const queryName = queryString.substring(dotIndex + 1).toLowerCase();
247
- result =
248
- fields.filter(
249
- (field) => field.name?.toLowerCase().includes(queryName) || field.title?.toLowerCase().includes(queryName),
250
- ) || [];
251
- }
252
-
253
- cb(
254
- result.map((field) => ({
255
- value: field.name,
256
- text: field.title,
257
- type: 'field',
258
- })),
259
- );
260
- };
261
-
262
- /**
263
- * 数据源提示
264
- * @param queryString 当前输入框内的字符串
265
- * @param cb 建议回调
266
- */
267
- const querySearch = (queryString: string, cb: (data: { value: string }[]) => void) => {
268
- inputText = queryString;
269
-
270
- const selectionStart = getSelectionStart();
271
-
272
- const curQueryString = queryString.substring(0, selectionStart);
273
-
274
- const fieldKeyStringLastIndex = curQueryString.lastIndexOf('.');
275
- const dsKeyStringLastIndex = curQueryString.lastIndexOf('${') + 1;
276
-
277
- const isFieldTip = fieldKeyStringLastIndex > dsKeyStringLastIndex;
278
-
279
- if (isFieldTip) {
280
- fieldQuerySearch(curQueryString, dsKeyStringLastIndex, fieldKeyStringLastIndex, cb);
281
- } else {
282
- dsQuerySearch(curQueryString, dsKeyStringLastIndex, cb);
283
- }
284
- };
285
-
286
- /**
287
- * 选择建议
288
- * @param value 建议值
289
- * @param type 建议类型,是数据源还是字段
290
- */
291
- const selectHandler = async ({ value, type }: { value: string; type: 'dataSource' | 'field' }) => {
292
- const isDataSource = type === 'dataSource';
293
- const selectionStart = input.value?.selectionStart || 0;
294
- let startText = inputText.substring(0, selectionStart);
295
-
296
- const dotIndex = startText.lastIndexOf('.');
297
- const leftCurlyBracketIndex = startText.lastIndexOf('${') + 1;
298
-
299
- const endText = inputText.substring(selectionStart);
300
-
301
- let suggestText = value;
302
-
303
- if (isDataSource) {
304
- if (!curCharIsLeftCurlyBracket(leftCurlyBracketIndex)) {
305
- startText = startText.substring(0, leftCurlyBracketIndex + 1);
306
- }
307
-
308
- // 当前光标后一位是否为},不是的话需要补上
309
- if (!isRightCurlyBracket(selectionStart + 1)) {
310
- suggestText = `${suggestText}}`;
311
- }
312
- } else if (!curCharIsDot(dotIndex)) {
313
- startText = startText.substring(0, dotIndex + 1);
314
- }
315
-
316
- state.value = `${startText}${suggestText}${endText}`;
317
-
318
- await nextTick();
319
-
320
- // 由于选择数据源时会在后面补全}, 所以光标要前移2位
321
- let newSelectionStart = 0;
322
- if (isDataSource) {
323
- newSelectionStart = leftCurlyBracketIndex + suggestText.length;
324
- } else {
325
- newSelectionStart = dotIndex + suggestText.length + 1;
326
- }
327
- input.value?.setSelectionRange(newSelectionStart, newSelectionStart);
328
-
329
- changeHandler(state.value);
330
- };
331
- </script>