From ef860d7556b2cfaf087817aa52f11994293b723e Mon Sep 17 00:00:00 2001 From: chenos Date: Sun, 18 Dec 2022 09:20:56 +0800 Subject: [PATCH] feat: page tabs (#1261) * feat: page tabs * feat: hide page title * fix: style --- .../antd/menu/MenuItemInitializers/index.tsx | 3 +- .../src/schema-component/antd/page/Page.tsx | 255 +++++++++++++++++- .../antd/page/PageTabDesigner.tsx | 176 ++++++++++++ .../ui-schema-storage/src/repository.ts | 13 +- 4 files changed, 434 insertions(+), 13 deletions(-) create mode 100644 packages/core/client/src/schema-component/antd/page/PageTabDesigner.tsx diff --git a/packages/core/client/src/schema-component/antd/menu/MenuItemInitializers/index.tsx b/packages/core/client/src/schema-component/antd/menu/MenuItemInitializers/index.tsx index b35d976f9..4bbc10ff3 100644 --- a/packages/core/client/src/schema-component/antd/menu/MenuItemInitializers/index.tsx +++ b/packages/core/client/src/schema-component/antd/menu/MenuItemInitializers/index.tsx @@ -1,5 +1,6 @@ import { FormDialog, FormLayout } from '@formily/antd'; import { SchemaOptionsContext } from '@formily/react'; +import { uid } from '@formily/shared'; import React, { useContext } from 'react'; import { useTranslation } from 'react-i18next'; import { SchemaComponent, SchemaComponentOptions } from '../../..'; @@ -151,7 +152,7 @@ export const PageMenuItem = itemWrap((props) => { 'x-component': 'Page', 'x-async': true, properties: { - grid: { + [uid()]: { type: 'void', 'x-component': 'Grid', 'x-initializer': 'BlockInitializers', diff --git a/packages/core/client/src/schema-component/antd/page/Page.tsx b/packages/core/client/src/schema-component/antd/page/Page.tsx index 6055565eb..f3d1c29a9 100644 --- a/packages/core/client/src/schema-component/antd/page/Page.tsx +++ b/packages/core/client/src/schema-component/antd/page/Page.tsx @@ -1,23 +1,262 @@ -import { useField } from '@formily/react'; -import { PageHeader as AntdPageHeader } from 'antd'; -import React, { useEffect } from 'react'; +import { css } from '@emotion/css'; +import { FormDialog, FormLayout } from '@formily/antd'; +import { RecursionField, SchemaOptionsContext, useField, useFieldSchema } from '@formily/react'; +import { Button, PageHeader as AntdPageHeader, Spin, Tabs } from 'antd'; +import classNames from 'classnames'; +import React, { useContext, useEffect, useState } from 'react'; +import { useTranslation } from 'react-i18next'; +import { useHistory, useLocation } from 'react-router-dom'; import { useDocumentTitle } from '../../../document-title'; -import { useCompile } from '../../hooks'; +import { Icon } from '../../../icon'; +import { SchemaComponent, SchemaComponentOptions } from '../../core'; +import { useCompile, useDesignable } from '../../hooks'; +import { PageDesigner, PageTabDesigner } from './PageTabDesigner'; + +const designerCss = css` + position: relative; + &:hover { + > .general-schema-designer { + display: block; + } + } + &.nb-action-link { + > .general-schema-designer { + top: -10px; + bottom: -10px; + left: -10px; + right: -10px; + } + } + > .general-schema-designer { + position: absolute; + z-index: 999; + top: 0; + bottom: 0; + left: 0; + right: 0; + display: none; + background: rgba(241, 139, 98, 0.06); + border: 0; + top: 0; + bottom: 0; + left: 0; + right: 0; + pointer-events: none; + > .general-schema-designer-icons { + position: absolute; + right: 2px; + top: 2px; + line-height: 16px; + pointer-events: all; + .ant-space-item { + background-color: #f18b62; + color: #fff; + line-height: 16px; + width: 16px; + padding-left: 1px; + } + } + } +`; + +const pageDesignerCss = css` + position: relative; + z-index: 20; + padding-top: 1px; + &:hover { + > .general-schema-designer { + display: block; + } + } + .ant-page-header { + z-index: 1; + position: relative; + } + > .general-schema-designer { + position: absolute; + z-index: 999; + top: 0; + bottom: 0; + left: 0; + right: 0; + display: none; + /* background: rgba(241, 139, 98, 0.06); */ + border: 0; + top: 0; + bottom: 0; + left: 0; + right: 0; + pointer-events: none; + > .general-schema-designer-icons { + z-index: 9999; + position: absolute; + right: 2px; + top: 2px; + line-height: 16px; + pointer-events: all; + .ant-space-item { + background-color: #f18b62; + color: #fff; + line-height: 16px; + width: 16px; + padding-left: 1px; + } + } + } +`; export const Page = (props) => { const { children, ...others } = props; const field = useField(); const compile = useCompile(); const { title, setTitle } = useDocumentTitle(); + const fieldSchema = useFieldSchema(); + const history = useHistory(); + const dn = useDesignable(); useEffect(() => { if (!title) { - setTitle(field.title); + setTitle(fieldSchema.title); } - }, [field.title, title]); + }, [fieldSchema.title, title]); + const disablePageHeader = fieldSchema['x-component-props']?.disablePageHeader; + const enablePageTabs = fieldSchema['x-component-props']?.enablePageTabs; + const hidePageTitle = fieldSchema['x-component-props']?.hidePageTitle; + const { t } = useTranslation(); + const options = useContext(SchemaOptionsContext); + const location = useLocation(); + const [loading, setLoading] = useState(false); + const [activeKey, setActiveKey] = useState(() => { + // @ts-ignore + return location?.query?.tab || Object.keys(fieldSchema.properties).shift(); + }); return ( <> - -
{children}
+
+ + {!disablePageHeader && ( + { + setLoading(true); + setActiveKey(activeKey); + window.history.pushState({}, '', location.pathname + `?tab=` + activeKey); + setTimeout(() => { + setLoading(false); + }, 50); + }} + tabBarExtraContent={ + + } + > + {fieldSchema.mapProperties((schema) => { + return ( + + {schema['x-icon'] && } + {schema.title || t('Unnamed')} + + + } + key={schema.name} + /> + ); + })} + + ) + } + /> + )} +
+ {loading ? ( + + ) : !disablePageHeader && enablePageTabs ? ( + { + return s.name === activeKey; + }} + /> + ) : ( +
.nb-schema-initializer-button { + display: none; + } + } + `} + > + {props.children} +
+ )} +
+
); }; diff --git a/packages/core/client/src/schema-component/antd/page/PageTabDesigner.tsx b/packages/core/client/src/schema-component/antd/page/PageTabDesigner.tsx new file mode 100644 index 000000000..667ef26b6 --- /dev/null +++ b/packages/core/client/src/schema-component/antd/page/PageTabDesigner.tsx @@ -0,0 +1,176 @@ +import { MenuOutlined } from '@ant-design/icons'; +import { ISchema, useField, useFieldSchema } from '@formily/react'; +import { Modal, Space } from 'antd'; +import React from 'react'; +import { useTranslation } from 'react-i18next'; +import { useDesignable } from '../..'; +import { SchemaSettings } from '../../../schema-settings'; + +export const PageDesigner = ({ title }) => { + const { dn } = useDesignable(); + const { t } = useTranslation(); + const field = useField(); + const fieldSchema = useFieldSchema(); + const hidePageTitle = fieldSchema['x-component-props']?.hidePageTitle; + const disablePageHeader = fieldSchema['x-component-props']?.disablePageHeader; + return ( +
+
+ + }> + { + fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {}; + fieldSchema['x-component-props']['disablePageHeader'] = !v; + dn.emit('patch', { + schema: { + ['x-uid']: fieldSchema['x-uid'], + ['x-component-props']: fieldSchema['x-component-props'], + }, + }); + dn.refresh(); + }} + /> + {!disablePageHeader && } + {!disablePageHeader && ( + { + fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {}; + fieldSchema['x-component-props']['hidePageTitle'] = !v; + dn.emit('patch', { + schema: { + ['x-uid']: fieldSchema['x-uid'], + ['x-component-props']: fieldSchema['x-component-props'], + }, + }); + dn.refresh(); + }} + /> + )} + {!disablePageHeader && !hidePageTitle && ( + { + field.title = title; + fieldSchema['title'] = title; + dn.emit('patch', { + schema: { + ['x-uid']: fieldSchema['x-uid'], + title, + }, + }); + }} + /> + )} + {!disablePageHeader && ( + { + fieldSchema['x-component-props'] = fieldSchema['x-component-props'] || {}; + fieldSchema['x-component-props']['enablePageTabs'] = v; + dn.emit('patch', { + schema: { + ['x-uid']: fieldSchema['x-uid'], + ['x-component-props']: fieldSchema['x-component-props'], + }, + }); + dn.refresh(); + }} + /> + )} + + +
+
+ ); +}; + +export const PageTabDesigner = ({ schema }) => { + const { dn } = useDesignable(); + const { t } = useTranslation(); + return ( +
+
+ + }> + { + schema.title = title; + schema['x-icon'] = icon; + dn.emit('patch', { + schema: { + ['x-uid']: schema['x-uid'], + title, + 'x-icon': icon, + }, + }); + dn.refresh(); + }} + /> + + { + Modal.confirm({ + title: t('Delete block'), + content: t('Are you sure you want to delete it?'), + ...confirm, + onOk() { + dn.remove(schema); + }, + }); + }} + > + {t('Delete')} + + + +
+
+ ); +}; diff --git a/packages/plugins/ui-schema-storage/src/repository.ts b/packages/plugins/ui-schema-storage/src/repository.ts index 4ab8e1d5f..0d3d28e8c 100644 --- a/packages/plugins/ui-schema-storage/src/repository.ts +++ b/packages/plugins/ui-schema-storage/src/repository.ts @@ -1,5 +1,5 @@ -import { Repository, Transactionable } from '@nocobase/database'; import { Cache } from '@nocobase/cache'; +import { Repository, Transactionable } from '@nocobase/database'; import { uid } from '@nocobase/utils'; import lodash from 'lodash'; import { Transaction } from 'sequelize'; @@ -336,11 +336,16 @@ export class UiSchemaRepository extends Repository { @transaction() async patch(newSchema: any, options?) { const { transaction } = options; - const rootUid = newSchema['x-uid']; await this.clearXUidPathCache(rootUid, transaction); - const oldTree = await this.getJsonSchema(rootUid); - + if (!newSchema['properties']) { + const s = await this.model.findByPk(rootUid, { transaction }); + s.set({ ...s.toJSON(), ...newSchema }); + // console.log(s.toJSON()); + await s.save({ transaction, hooks: false }); + return; + } + const oldTree = await this.getJsonSchema(rootUid, { transaction }); const traverSchemaTree = async (schema, path = []) => { const node = schema; const oldNode = path.length == 0 ? oldTree : lodash.get(oldTree, path);