perf(InternalSchemaToolbar): improve style performance

This commit is contained in:
Zeke Zhang 2024-10-30 09:03:08 +08:00
parent 64afd986bc
commit aa41d3bfcc
2 changed files with 47 additions and 51 deletions

View File

@ -216,9 +216,8 @@ const InternalSchemaToolbar: FC<SchemaToolbarProps> = (props) => {
...props, ...props,
...(fieldSchema?.['x-toolbar-props'] || {}), ...(fieldSchema?.['x-toolbar-props'] || {}),
} as SchemaToolbarProps; } as SchemaToolbarProps;
const { designable } = useDesignable();
const compile = useCompile(); const compile = useCompile();
const { styles } = useStyles(); const { componentCls, hashId } = useStyles();
const { t } = useTranslation(); const { t } = useTranslation();
const { getAriaLabel } = useGetAriaLabelOfDesigner(); const { getAriaLabel } = useGetAriaLabelOfDesigner();
const dm = useDataSourceManager(); const dm = useDataSourceManager();
@ -229,8 +228,9 @@ const InternalSchemaToolbar: FC<SchemaToolbarProps> = (props) => {
const titleArr = useMemo(() => { const titleArr = useMemo(() => {
if (!title) return undefined; if (!title) return undefined;
if (typeof title === 'string') return [compile(title)]; if (typeof title === 'string') return [compile(title)];
if (Array.isArray(title)) return title.map((item) => compile(item)); if (Array.isArray(title)) return compile(title);
}, [compile, title]); }, [title]);
const { render: schemaSettingsRender, exists: schemaSettingsExists } = useSchemaSettingsRender( const { render: schemaSettingsRender, exists: schemaSettingsExists } = useSchemaSettingsRender(
settings || fieldSchema?.['x-settings'], settings || fieldSchema?.['x-settings'],
fieldSchema?.['x-settings-props'], fieldSchema?.['x-settings-props'],
@ -282,9 +282,7 @@ const InternalSchemaToolbar: FC<SchemaToolbarProps> = (props) => {
const settingsElement = useMemo(() => { const settingsElement = useMemo(() => {
return settings !== false && schemaSettingsExists ? schemaSettingsRender() : null; return settings !== false && schemaSettingsExists ? schemaSettingsRender() : null;
}, [schemaSettingsExists, schemaSettingsRender, settings]); }, [schemaSettingsExists, schemaSettingsRender, settings]);
const toolbarRef = useRef<HTMLDivElement>(null); const toolbarRef = useRef<HTMLDivElement>(null);
useEffect(() => { useEffect(() => {
const toolbarElement = toolbarRef.current; const toolbarElement = toolbarRef.current;
let parentElement = toolbarElement?.parentElement; let parentElement = toolbarElement?.parentElement;
@ -314,38 +312,33 @@ const InternalSchemaToolbar: FC<SchemaToolbarProps> = (props) => {
parentElement.addEventListener('mouseenter', show); parentElement.addEventListener('mouseenter', show);
parentElement.addEventListener('mouseleave', hide); parentElement.addEventListener('mouseleave', hide);
return () => { return () => {
parentElement.removeEventListener('mouseenter', show); parentElement.removeEventListener('mouseenter', show);
parentElement.removeEventListener('mouseleave', hide); parentElement.removeEventListener('mouseleave', hide);
}; };
}, []); }, []);
if (!designable) {
return null;
}
return ( return (
<div <div
ref={toolbarRef} ref={toolbarRef}
className={classNames(styles.toolbar, toolbarClassName, 'schema-toolbar')} className={classNames(componentCls, hashId, toolbarClassName, 'schema-toolbar')}
style={{ border: showBorder ? 'auto' : 0, background: showBackground ? 'auto' : 0, ...toolbarStyle }} style={{ border: showBorder ? 'auto' : 0, background: showBackground ? 'auto' : 0, ...toolbarStyle }}
> >
{titleArr && ( {titleArr && (
<div className={styles.toolbarTitle}> <div className={'toolbar-title'}>
<Space size={2}> <Space size={2}>
<span key={titleArr[0]} className={styles.toolbarTitleTag}> <span key={titleArr[0]} className={'toolbar-title-tag'}>
{dataSource ? `${compile(dataSource?.displayName)} > ${titleArr[0]}` : titleArr[0]} {dataSource ? `${compile(dataSource?.displayName)} > ${titleArr[0]}` : titleArr[0]}
</span> </span>
{titleArr[1] && ( {titleArr[1] && (
<span className={styles.toolbarTitleTag}> <span className={'toolbar-title-tag'}>
{`${t('Reference template')}: ${`${titleArr[1]}` || t('Untitled')}`} {`${t('Reference template')}: ${`${titleArr[1]}` || t('Untitled')}`}
</span> </span>
)} )}
</Space> </Space>
</div> </div>
)} )}
<div className={classNames(styles.toolbarIcons, spaceWrapperClassName)} style={spaceWrapperStyle}> <div className={classNames('toolbar-icons', spaceWrapperClassName)} style={spaceWrapperStyle}>
<Space size={3} align={'center'} className={spaceClassName} style={spaceStyle}> <Space size={3} align={'center'} className={spaceClassName} style={spaceStyle}>
{dragElement} {dragElement}
{initializerElement} {initializerElement}

View File

@ -7,11 +7,13 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import { createStyles } from 'antd-style'; import { genStyleHook } from '../schema-component/antd/__builtins__/style';
export const useStyles = genStyleHook('nb-schema-toolbar', (token) => {
const { componentCls } = token;
export const useStyles = createStyles(() => {
return { return {
toolbar: { [componentCls]: {
position: 'absolute', position: 'absolute',
zIndex: 999, zIndex: 999,
display: 'none', display: 'none',
@ -26,8 +28,8 @@ export const useStyles = createStyles(() => {
'.ant-space-item .anticon': { '.ant-space-item .anticon': {
margin: 0, margin: 0,
}, },
},
toolbarTitle: { '.toolbar-title': {
pointerEvents: 'none', pointerEvents: 'none',
position: 'absolute', position: 'absolute',
fontSize: 12, fontSize: 12,
@ -39,14 +41,14 @@ export const useStyles = createStyles(() => {
top: 2, top: 2,
left: 2, left: 2,
}, },
toolbarTitleTag: { '.toolbar-title-tag': {
padding: '0 3px', padding: '0 3px',
borderRadius: 2, borderRadius: 2,
background: 'var(--colorSettings)', background: 'var(--colorSettings)',
color: '#fff', color: '#fff',
display: 'block', display: 'block',
}, },
toolbarIcons: { '.toolbar-icons': {
position: 'absolute', position: 'absolute',
right: '2px', right: '2px',
top: '2px', top: '2px',
@ -61,5 +63,6 @@ export const useStyles = createStyles(() => {
alignSelf: 'stretch', alignSelf: 'stretch',
}, },
}, },
},
}; };
}); });