-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: 实现 AssetStore 与 EditorStore 的状态自动同步
- Loading branch information
Showing
5 changed files
with
93 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import isEqual from 'fast-deep-equal'; | ||
import { memo, useEffect } from 'react'; | ||
import { createStoreUpdater } from 'zustand-utils'; | ||
import { shallow } from 'zustand/shallow'; | ||
|
||
import { useProEditor } from '../../hooks/useProEditor'; | ||
import { useStore, useStoreApi } from '../../store'; | ||
|
||
const AssetStoreUpdater = memo(() => { | ||
const instance = useProEditor(); | ||
|
||
const [useAssetStoreApi, useAssetStore, configSelector] = useStore( | ||
(s) => [ | ||
s.componentAsset.componentStoreApi, | ||
s.componentAsset.componentStore, | ||
s.componentAsset.configSelector, | ||
], | ||
shallow, | ||
); | ||
const assetStoreApi = useAssetStoreApi(); | ||
|
||
// 将 instance 的方法全部同步到 assetStore | ||
useEffect(() => { | ||
assetStoreApi.setState({ ...instance }); | ||
}, []); | ||
|
||
// 将计算后的默认值传给面板 | ||
// 用等式做一次优化,不然每次都会重新计算 | ||
const defaultConfig = useStore((s) => s.componentAsset.getDefaultConfig(s.mode), isEqual); | ||
|
||
const proEditorStoreApi = useStoreApi(); | ||
const useStoreUpdater = createStoreUpdater(proEditorStoreApi); | ||
// 用 defaultConfig 更新一次config | ||
useStoreUpdater('config', defaultConfig, []); | ||
|
||
// 将 assetStore 的 config 自动同步到 proEditorStore | ||
const assetConfig = useAssetStore(configSelector, isEqual); | ||
useEffect(() => { | ||
if (typeof assetConfig === 'undefined') return; | ||
if (isEqual(assetConfig, proEditorStoreApi.getState().config)) return; | ||
|
||
proEditorStoreApi.setState({ config: assetConfig }); | ||
}, [assetConfig]); | ||
|
||
return null; | ||
}); | ||
|
||
export default AssetStoreUpdater; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters