diff --git a/packages/editor/src/fields/DataSourceFieldSelect.vue b/packages/editor/src/fields/DataSourceFieldSelect.vue index f44e956a..a7568e75 100644 --- a/packages/editor/src/fields/DataSourceFieldSelect.vue +++ b/packages/editor/src/fields/DataSourceFieldSelect.vue @@ -14,6 +14,14 @@ :prop="`${prop}${prop ? '.' : ''}${name}`" @change="onChangeHandler" > + + + diff --git a/packages/editor/src/hooks/use-data-source-edit.ts b/packages/editor/src/hooks/use-data-source-edit.ts new file mode 100644 index 00000000..07afb830 --- /dev/null +++ b/packages/editor/src/hooks/use-data-source-edit.ts @@ -0,0 +1,46 @@ +import { computed, ref } from 'vue'; + +import type { DataSourceSchema } from '@tmagic/schema'; + +import DataSourceConfigPanel from '@editor/layouts/sidebar/data-source/DataSourceConfigPanel.vue'; +import type { DataSourceService } from '@editor/services/dataSource'; + +export const useDataSourceEdit = (dataSourceService?: DataSourceService) => { + const dialogTitle = ref(''); + const editDialog = ref>(); + const dataSourceValues = ref>({}); + + const editable = computed(() => dataSourceService?.get('editable') ?? true); + + const editHandler = (id: string) => { + if (!editDialog.value) return; + + dataSourceValues.value = { + ...dataSourceService?.getDataSourceById(id), + }; + + dialogTitle.value = `编辑${dataSourceValues.value.title || ''}`; + + editDialog.value.show(); + }; + + const submitDataSourceHandler = (value: DataSourceSchema) => { + if (value.id) { + dataSourceService?.update(value); + } else { + dataSourceService?.add(value); + } + + editDialog.value?.hide(); + }; + + return { + dialogTitle, + editDialog, + dataSourceValues, + editable, + + editHandler, + submitDataSourceHandler, + }; +}; diff --git a/packages/editor/src/layouts/sidebar/data-source/DataSourceListPanel.vue b/packages/editor/src/layouts/sidebar/data-source/DataSourceListPanel.vue index 0f4195ab..1b67035f 100644 --- a/packages/editor/src/layouts/sidebar/data-source/DataSourceListPanel.vue +++ b/packages/editor/src/layouts/sidebar/data-source/DataSourceListPanel.vue @@ -42,10 +42,10 @@ import { computed, inject, ref } from 'vue'; import { mergeWith } from 'lodash-es'; import { TMagicButton, TMagicPopover, TMagicScrollbar } from '@tmagic/design'; -import type { DataSourceSchema } from '@tmagic/schema'; import SearchInput from '@editor/components/SearchInput.vue'; import ToolButton from '@editor/components/ToolButton.vue'; +import { useDataSourceEdit } from '@editor/hooks/use-data-source-edit'; import type { DataSourceListSlots, Services } from '@editor/type'; import DataSourceConfigPanel from './DataSourceConfigPanel.vue'; @@ -59,13 +59,9 @@ defineOptions({ const { dataSourceService } = inject('services') || {}; -const editDialog = ref>(); +const { editDialog, dataSourceValues, dialogTitle, editable, editHandler, submitDataSourceHandler } = + useDataSourceEdit(dataSourceService); -const dataSourceValues = ref>({}); - -const dialogTitle = ref(''); - -const editable = computed(() => dataSourceService?.get('editable') ?? true); const datasourceTypeList = computed(() => [ { text: '基础', type: 'base' }, @@ -93,32 +89,10 @@ const addHandler = (type: string) => { editDialog.value.show(); }; -const editHandler = (id: string) => { - if (!editDialog.value) return; - - dataSourceValues.value = { - ...dataSourceService?.getDataSourceById(id), - }; - - dialogTitle.value = `编辑${dataSourceValues.value.title || ''}`; - - editDialog.value.show(); -}; - const removeHandler = (id: string) => { dataSourceService?.remove(id); }; -const submitDataSourceHandler = (value: DataSourceSchema) => { - if (value.id) { - dataSourceService?.update(value); - } else { - dataSourceService?.add(value); - } - - editDialog.value?.hide(); -}; - const dataSourceList = ref>(); const filterTextChangeHandler = (val: string) => {