From a596a8ee8de76af31b977ada4b1aea185a33eee4 Mon Sep 17 00:00:00 2001 From: sealday Date: Tue, 7 May 2024 15:17:28 +0800 Subject: [PATCH] feat: support default setting items (#918) Reviewed-on: https://git.daoyoucloud.com/daoyoucloud/tachybase/pulls/918 --- .../schema-settings/SchemaSettings.tsx | 2 + .../SchemaSettingsDefaults.tsx | 96 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 packages/core/client/src/application/schema-settings/SchemaSettingsDefaults.tsx diff --git a/packages/core/client/src/application/schema-settings/SchemaSettings.tsx b/packages/core/client/src/application/schema-settings/SchemaSettings.tsx index d56aa81fe..45f6d7969 100644 --- a/packages/core/client/src/application/schema-settings/SchemaSettings.tsx +++ b/packages/core/client/src/application/schema-settings/SchemaSettings.tsx @@ -1,4 +1,5 @@ import { SchemaSettingOptions, SchemaSettingsItemType, SchemaSettingsItemTypeWithoutName } from './types'; +import { defaultSettingItems } from './SchemaSettingsDefaults'; export class SchemaSettings { options: SchemaSettingOptions; @@ -10,6 +11,7 @@ export class SchemaSettings { constructor(options: SchemaSettingOptions) { this.options = Object.assign({ items: [] }, options); + this.options.items = defaultSettingItems.concat(this.items); this.name = options.name; } diff --git a/packages/core/client/src/application/schema-settings/SchemaSettingsDefaults.tsx b/packages/core/client/src/application/schema-settings/SchemaSettingsDefaults.tsx new file mode 100644 index 000000000..ee1ab060f --- /dev/null +++ b/packages/core/client/src/application/schema-settings/SchemaSettingsDefaults.tsx @@ -0,0 +1,96 @@ +import { ISchema, useFieldSchema } from '@tachybase/schema'; +import { SchemaSettingsItemType } from './types'; +import { useTranslation } from 'react-i18next'; +import { createDesignable, useDesignable } from '../../schema-component'; +import { saveAs } from 'file-saver'; +import { useAPIClient } from '../../api-client'; +import { useMemo } from 'react'; + +export const defaultSettingItems = [ + { + name: 'designer', + type: 'itemGroup', + hideIfNoChildren: true, + useComponentProps() { + const { t } = useTranslation(); + return { + title: t('Designer properties'), + }; + }, + children: [ + { + name: 'dump', + type: 'item', + useComponentProps() { + const { t } = useTranslation(); + const { dn } = useDesignable(); + const api = useAPIClient(); + + const fieldSchema = useFieldSchema(); + return { + onClick: async () => { + const KEYS = ['name', 'x-uid', '_isJSONSchemaObject', 'version', 'x-index', 'x-async']; + const deleteUid = (s: ISchema) => { + KEYS.forEach((key) => delete s[key]); + Object.keys(s.properties || {}).forEach((key) => { + deleteUid(s.properties[key]); + }); + }; + const { data } = await api.request({ + url: `/uiSchemas:getJsonSchema/${fieldSchema['x-uid']}?includeAsyncNode=true`, + }); + const s = data?.data || {}; + deleteUid(s); + const blob = new Blob([JSON.stringify(s, null, 2)], { type: 'application/json' }); + saveAs(blob, fieldSchema['x-uid'] + '.schema.json'); + }, + title: t('Dump'), + }; + }, + }, + { + name: 'load', + type: 'modal', + useComponentProps() { + const { t } = useTranslation(); + const { refresh } = useDesignable(); + const api = useAPIClient(); + const fieldSchema = useFieldSchema(); + const dn = useMemo(() => { + return createDesignable({ t, api, refresh, current: fieldSchema.parent }); + }, [t, api, refresh, fieldSchema]); + + return { + schema: { + type: 'object', + title: t('Load'), + properties: { + file: { + type: 'object', + title: '{{ t("File") }}', + required: true, + 'x-decorator': 'FormItem', + 'x-component': 'Upload.Attachment', + 'x-component-props': { + action: 'attachments:create', + multiple: false, + }, + }, + } as ISchema, + }, + title: t('Load'), + onSubmit: async ({ file }) => { + const { data } = await api.request({ + url: file.url, + baseURL: '/', + }); + const s = data ?? {}; + dn.insertBeforeEnd(s); + dn.refresh(); + }, + }; + }, + }, + ], + } as SchemaSettingsItemType, +];