Skip to content

Commit

Permalink
✨ feat(pro-editor): 支持配置 ProEditorStore 在devtools 中的名称
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jun 15, 2023
1 parent 4f52be5 commit 2cedbb3
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/ComponentAsset/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ReactNode } from 'react';
import { StateCreator, StoreApi, create } from 'zustand';
import { UseContextStore, createContext, optionalDevtools } from 'zustand-utils';
import { DevtoolsOptions } from 'zustand/middleware';

import type { ProEditorInstance } from '@/ProEditor';
import { ReactNode } from 'react';

export interface AssetStoreOptions<T = any> {
devtools?: boolean | DevtoolsOptions;
Expand Down
1 change: 0 additions & 1 deletion src/ProEditor/container/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export interface ProEditorAppProps {
* 自定义错误兜底形态
*/
ErrorBoundary?: FC;
__STORE_DEVTOOLS__?: boolean;
/**
* 代码复制回调
*/
Expand Down
9 changes: 4 additions & 5 deletions src/ProEditor/container/Provider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { FC, ReactNode } from 'react';
import { DevtoolsOptions } from 'zustand/middleware';

import { createStore, Provider, useStoreApi } from '../store';

export const ProEditorProvider: FC<{
children: ReactNode;
showDevtools?: boolean;
}> = ({ children, showDevtools }) => {
devtoolOptions?: boolean | DevtoolsOptions;
}> = ({ children, devtoolOptions }) => {
let isWrapped = true;

const Content = <>{children}</>;
Expand All @@ -20,9 +21,7 @@ export const ProEditorProvider: FC<{
return Content;
}

return (
<Provider createStore={() => createStore(showDevtools)}>{Content}</Provider>
);
return <Provider createStore={() => createStore(devtoolOptions)}>{Content}</Provider>;
};

export default ProEditorProvider;
9 changes: 7 additions & 2 deletions src/ProEditor/container/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppContainer } from '@ant-design/pro-editor';
import { App } from 'antd';
import type { FC } from 'react';
import { memo } from 'react';
import { DevtoolsOptions } from 'zustand/middleware';

import type { ProEditorAppProps } from './App';
import Content from './App';
Expand All @@ -11,14 +12,18 @@ import type { StoreUpdaterProps } from './StoreUpdater';
import StoreUpdater from './StoreUpdater';
import { useStyle } from './style';

export type ProEditorProps = ProEditorAppProps & AppContainerProps & StoreUpdaterProps;
export type ProEditorProps = ProEditorAppProps &
AppContainerProps &
StoreUpdaterProps & {
__STORE_DEVTOOLS__?: boolean | DevtoolsOptions;
};

export const ProEditor: FC<ProEditorProps> = memo((props) => {
const { themeMode, theme, style, __STORE_DEVTOOLS__, editorRef, ...res } = props;
const { styles } = useStyle();

return (
<Provider showDevtools={__STORE_DEVTOOLS__}>
<Provider devtoolOptions={__STORE_DEVTOOLS__}>
<AppContainer themeMode={themeMode} theme={theme}>
<App className={styles.app} style={style}>
<Content {...res} />
Expand Down
2 changes: 1 addition & 1 deletion src/ProEditor/demos/buttonAsset/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const buttonAssetParams: ComponentAssetParams<ButtonConfig> = {
},

storeOptions: {
devtools: true,
devtools: { name: 'ButtonAssetStore' },
},

codeEmitter: () => '',
Expand Down
6 changes: 5 additions & 1 deletion src/ProEditor/demos/buttonAssets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ import { buttonAssetParams } from './buttonAsset';
const ButtonComponentAsset = new ComponentAsset(buttonAssetParams);

export default () => (
<ProEditor componentAsset={ButtonComponentAsset} style={{ height: '100vh' }} __STORE_DEVTOOLS__ />
<ProEditor
componentAsset={ButtonComponentAsset}
style={{ height: '100vh' }}
__STORE_DEVTOOLS__={{ name: 'ButtonEditor' }}
/>
);
10 changes: 7 additions & 3 deletions src/ProEditor/store/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { StateCreator } from 'zustand';
import { create } from 'zustand';
import { optionalDevtools } from 'zustand-utils';
import { DevtoolsOptions } from 'zustand/middleware';

import {
AwarenessPublicAction,
Expand Down Expand Up @@ -52,8 +53,11 @@ const vanillaStore: StateCreator<InternalProEditorStore, [['zustand/devtools', n
...awarenessSlice(...params),
});

export const createStore = (showDevTools: boolean = false) => {
const devtools = optionalDevtools(showDevTools);
export const createStore = (options: boolean | DevtoolsOptions = false) => {
const devtools = optionalDevtools(options !== false);

return create<InternalProEditorStore>()(devtools(vanillaStore, { name: 'ProEditorStore' }));
const devtoolOptions =
options === false ? undefined : options === true ? { name: 'ProEditorStore' } : options;

return create<InternalProEditorStore>()(devtools(vanillaStore, devtoolOptions));
};

0 comments on commit 2cedbb3

Please sign in to comment.