From 4ab89f68ff1b6e7e8c2550e3b65cee55c414245e Mon Sep 17 00:00:00 2001 From: chenshenhai Date: Sat, 27 Jan 2024 15:27:14 +0800 Subject: [PATCH 1/2] feat: improve studio context --- packages/studio-base/src/hooks/theme.ts | 15 +++++ .../src/icons/util/icon-wrapper.tsx | 2 +- packages/studio-base/src/index.ts | 1 + .../src/modules/config-provider/index.tsx | 14 ++--- packages/studio/src/modules/context.ts | 10 +-- .../studio/src/modules/panel-layer/index.tsx | 10 +-- packages/studio/src/modules/sketch/index.tsx | 63 ++++++++++--------- packages/studio/src/studio.tsx | 24 ++----- packages/studio/src/types/lib/context.ts | 2 +- packages/studio/src/types/lib/shared.ts | 2 +- 10 files changed, 72 insertions(+), 71 deletions(-) create mode 100644 packages/studio-base/src/hooks/theme.ts diff --git a/packages/studio-base/src/hooks/theme.ts b/packages/studio-base/src/hooks/theme.ts new file mode 100644 index 0000000..b8b985a --- /dev/null +++ b/packages/studio-base/src/hooks/theme.ts @@ -0,0 +1,15 @@ +import { generateClassName } from '../css'; +import classnames from 'classnames'; +import type { ThemeMode } from '../types'; + +export const useThemeClassName = (props: { themeMode?: ThemeMode }) => { + const { themeMode } = props; + const themeName = 'theme'; + const themeClassName = generateClassName(themeName); + const themeDarkClassName = generateClassName(themeName, 'dark'); + const className = classnames({ + [themeClassName]: true, + [themeDarkClassName]: themeMode === 'dark' + }); + return { themeMode, className }; +}; diff --git a/packages/studio-base/src/icons/util/icon-wrapper.tsx b/packages/studio-base/src/icons/util/icon-wrapper.tsx index a52317e..581acb7 100644 --- a/packages/studio-base/src/icons/util/icon-wrapper.tsx +++ b/packages/studio-base/src/icons/util/icon-wrapper.tsx @@ -25,5 +25,5 @@ export const IconWrapper = (props: IconWrapperProps) => { {children} ); - }, [iconClassName]); + }, [iconClassName, style, className]); }; diff --git a/packages/studio-base/src/index.ts b/packages/studio-base/src/index.ts index bd42dee..6b0ac1b 100644 --- a/packages/studio-base/src/index.ts +++ b/packages/studio-base/src/index.ts @@ -11,6 +11,7 @@ export { ElementDetail, type ElementDetailProps } from './modules/element-detail export { getElementTree } from './tools'; export { setClassNameTopPrefix, generateClassName } from './css/index'; +export { useThemeClassName } from './hooks/theme'; export * from './locale/types'; export { diff --git a/packages/studio-base/src/modules/config-provider/index.tsx b/packages/studio-base/src/modules/config-provider/index.tsx index e4898e7..9bdda6b 100644 --- a/packages/studio-base/src/modules/config-provider/index.tsx +++ b/packages/studio-base/src/modules/config-provider/index.tsx @@ -1,11 +1,11 @@ import React from 'react'; -import { createContext, useEffect, useState, useContext } from 'react'; +import { createContext, useEffect, useState } from 'react'; import { ConfigProvider as AntdConfigProvider, theme } from 'antd'; -import classnames from 'classnames'; import { createPrefixName, generateClassName, getClassNameTopPrefix, setClassNameTopPrefix } from '../../css'; import { LocaleCode } from '../../locale'; import { DEFAULT_LOCALE_CODE } from '../../locale'; import { ThemeMode } from '../../types'; +import { useThemeClassName } from '../../hooks/theme'; export interface ConfigContextValue { topPrefix: string; @@ -39,19 +39,15 @@ export const ConfigContext: React.Context = createContext, 'topPrefix' | 'localeCode' | 'container' | 'themeMode'> { children?: React.ReactNode; } + export const ConfigProvider: React.FC = (props) => { const { children, topPrefix, localeCode, container, themeMode } = props; const contextValue: ConfigContextValue = getDefaultConfigValue({ topPrefix, localeCode }); - - const themeName = 'theme'; const [context, setContext] = useState(contextValue); - const { createPrefixName } = useContext(ConfigContext); - const themePrefixName = createPrefixName(themeName); - const themeClassName = themePrefixName(); - const themeDarkClassName = themePrefixName('dark'); + const { className } = useThemeClassName({ themeMode }); useEffect(() => { setContext({ @@ -63,7 +59,7 @@ export const ConfigProvider: React.FC = (props) => { return ( -
{children}
+
{children}
); diff --git a/packages/studio/src/modules/context.ts b/packages/studio/src/modules/context.ts index 8fc17f3..5ad758d 100644 --- a/packages/studio/src/modules/context.ts +++ b/packages/studio/src/modules/context.ts @@ -13,12 +13,12 @@ export function createStudioContextStateByProps(props?: StudioProps): StudioStat elements: [], ...(props?.data || {}) }; - let editingDataPostion: ElementPosition = []; + let editingDataPosition: ElementPosition = []; let editingData: Data = data; if (props?.defaultEditingGroupUUID) { - editingDataPostion = getElementPositionFromList(props.defaultEditingGroupUUID, data.elements); + editingDataPosition = getElementPositionFromList(props.defaultEditingGroupUUID, data.elements); } - editingData = cloneEditingDataByPosition(editingDataPostion, data); + editingData = cloneEditingDataByPosition(editingDataPosition, data); const treeData = getElementTree(editingData); return { @@ -26,7 +26,7 @@ export function createStudioContextStateByProps(props?: StudioProps): StudioStat themeMode: props?.defaultThemeMode || defaultThemeMode, data, editingData, - editingDataPostion, + editingDataPosition, treeData, selectedUUIDs: props?.defaultSelectedElementUUIDs || [], scaleInfo: { @@ -51,7 +51,7 @@ export function createStudioContextState(opts?: Partial): StudioSta ...(opts?.data || {}) }, editingData: cloneEditingDataByPosition([], data), - editingDataPostion: [], + editingDataPosition: [], treeData: [], selectedUUIDs: [], scaleInfo: { diff --git a/packages/studio/src/modules/panel-layer/index.tsx b/packages/studio/src/modules/panel-layer/index.tsx index 74f2b23..c4f68d3 100644 --- a/packages/studio/src/modules/panel-layer/index.tsx +++ b/packages/studio/src/modules/panel-layer/index.tsx @@ -39,10 +39,10 @@ export const PanelLayer = (props: PanelLayerProps) => { const [contextMenuOptions] = useContextMenuOptions({ sharedEvent, sharedStore }); const getCurrentName = () => { - if (state.editingDataPostion.length === 0) { + if (state.editingDataPosition.length === 0) { return ''; } - const elem = findElementFromListByPosition(state.editingDataPostion, state.data.elements); + const elem = findElementFromListByPosition(state.editingDataPosition, state.data.elements); return elem?.name || elem?.type || ''; }; @@ -123,10 +123,10 @@ export const PanelLayer = (props: PanelLayerProps) => { className={headerBtnClassName} size="small" icon={} - disabled={!(state.editingDataPostion.length > 0)} + disabled={!(state.editingDataPosition.length > 0)} onClick={onClickBackRootEdit} /> -