-
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.
Merge pull request #80 from ant-design/alpha
✨ feat: 撤销重做中间件
- Loading branch information
Showing
32 changed files
with
1,406 additions
and
14 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,43 @@ | ||
import { Button, Card, Divider, Tabs } from 'antd'; | ||
import { useTheme } from 'antd-style'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import Toolbar from './Toolbar'; | ||
import { useStore } from './store'; | ||
|
||
const App = () => { | ||
const { data, plus, tabs, switchTabs, plusWithoutHistory } = useStore(); | ||
|
||
const theme = useTheme(); | ||
|
||
return ( | ||
<Flexbox padding={16} style={{ background: theme.colorBgLayout }}> | ||
<Toolbar /> | ||
<Divider /> | ||
<Flexbox gap={12}> | ||
<Card title={'历史记录里的数据'}> | ||
<Button onClick={plus}>+1</Button> | ||
|
||
<div>data: {data}</div> | ||
</Card> | ||
<Card title={'不在历史记录里的数据'}> | ||
<div> | ||
<Tabs | ||
activeKey={tabs} | ||
onChange={switchTabs} | ||
items={[ | ||
{ key: '1', label: '数据' }, | ||
{ key: '2', label: '配置' }, | ||
]} | ||
/> | ||
|
||
<div>下面的 +2 可使得 在历史记录外添加让 data +2 </div> | ||
<Button onClick={plusWithoutHistory}>+2</Button> | ||
</div> | ||
</Card> | ||
</Flexbox> | ||
</Flexbox> | ||
); | ||
}; | ||
|
||
export default App; |
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,45 @@ | ||
import { RedoOutlined, UndoOutlined } from '@ant-design/icons'; | ||
import { useProEditor } from '@ant-design/pro-editor'; | ||
import { Badge, Button } from 'antd'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
const Toolbar = () => { | ||
const { undo, redo, undoStack, redoStack } = useProEditor(); | ||
|
||
const undoStackList = undoStack(); | ||
const redoStackList = redoStack(); | ||
|
||
const lastAction = undoStackList.at(-1); | ||
|
||
return ( | ||
<Flexbox padding={12} gap={8}> | ||
<Flexbox horizontal gap={8}> | ||
<Badge count={undoStackList.length}> | ||
<Button icon={<UndoOutlined />} onClick={undo} disabled={undoStackList.length === 0}> | ||
撤销 | ||
</Button> | ||
</Badge> | ||
|
||
<Badge count={redoStackList.length}> | ||
<Button icon={<RedoOutlined />} onClick={redo} disabled={redoStackList.length === 0}> | ||
重做 | ||
</Button> | ||
</Badge> | ||
</Flexbox> | ||
<Flexbox> | ||
上次操作时间: | ||
{lastAction ? new Date(lastAction.timestamp).toLocaleTimeString() : '-'} | ||
</Flexbox> | ||
<Flexbox> | ||
上次操作名称: | ||
{lastAction?.name ?? '-'} | ||
</Flexbox>{' '} | ||
<Flexbox> | ||
上次操作类型: | ||
{lastAction?.type ?? '-'} | ||
</Flexbox> | ||
</Flexbox> | ||
); | ||
}; | ||
|
||
export default Toolbar; |
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,15 @@ | ||
/** | ||
* compact: true | ||
*/ | ||
import { ProEditorProvider } from '@ant-design/pro-editor'; | ||
import App from './App'; | ||
|
||
import { useStore } from './store'; | ||
|
||
export default () => { | ||
return ( | ||
<ProEditorProvider devtoolOptions={{ name: 'redo-pro-editor-store' }} store={[useStore]}> | ||
<App /> | ||
</ProEditorProvider> | ||
); | ||
}; |
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,57 @@ | ||
import { proEditorMiddleware, ProEditorOptions } from '@ant-design/pro-editor'; | ||
import { create, StateCreator } from 'zustand'; | ||
import { devtools, subscribeWithSelector } from 'zustand/middleware'; | ||
|
||
interface Store { | ||
tabs: string; | ||
plus: () => void; | ||
plusWithoutHistory: () => void; | ||
data: number; | ||
switchTabs: (key: string) => void; | ||
} | ||
|
||
const createStore: StateCreator<Store, [['zustand/devtools', never], ['pro-editor', never]]> = ( | ||
set, | ||
get, | ||
) => ({ | ||
tabs: '1', | ||
switchTabs: (key) => { | ||
set({ tabs: key }); | ||
}, | ||
plusWithoutHistory: () => { | ||
set((s) => ({ ...s, data: s.data + 2 }), false, { | ||
type: 'plusWithoutHistory', | ||
recordHistory: false, | ||
}); | ||
}, | ||
|
||
plus: () => { | ||
const nextData = get().data + 1; | ||
|
||
set({ data: nextData }, false, { | ||
type: 'plus', | ||
payload: nextData, | ||
name: '+1', | ||
}); | ||
}, | ||
data: 3, | ||
}); | ||
|
||
interface ProEditorStore { | ||
data: number; | ||
} | ||
|
||
const storeName = 'redo-demo-app'; | ||
|
||
const proEditorOptions: ProEditorOptions<Store, ProEditorStore> = { | ||
name: storeName, | ||
partialize: (s) => ({ data: s.data }), | ||
}; | ||
|
||
export const useStore = create<Store>()( | ||
devtools(proEditorMiddleware(subscribeWithSelector(createStore), proEditorOptions), { | ||
name: storeName, | ||
}), | ||
); | ||
|
||
useStore.subscribe((s) => s.data, console.log); |
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,113 @@ | ||
--- | ||
title: 撤销重做 | ||
group: 状态管理研发 | ||
order: 2 | ||
--- | ||
|
||
# 撤销重做 | ||
|
||
撤销重做是编辑器场景保障用户体验的一个重要特性。ProEditor 作为编辑器框架,为上层的应用编辑器提供了撤销重做的原子化能力。 | ||
|
||
## 立即上手 | ||
|
||
<code src="./demos/Redo/index.tsx" ></code> | ||
|
||
## 使用方式 | ||
|
||
### 初始化 | ||
|
||
1. 外层包裹 ProEditorProvider,传入相应的 zustand store | ||
|
||
```tsx | pure | ||
import { ProEditorProvider } from '@ant-design/pro-editor'; | ||
|
||
import { useStore } from './store'; | ||
|
||
export default () => { | ||
return ( | ||
<ProEditorProvider store={[useStore]}> | ||
<App /> | ||
</ProEditorProvider> | ||
); | ||
}; | ||
``` | ||
|
||
2. zustand store 包裹 ProEditorMiddleware | ||
|
||
```ts | ||
import { proEditorMiddleware, ProEditorOptions } from '@ant-design/pro-editor'; | ||
|
||
interface ProEditorStore extends Partial<Store> { } | ||
|
||
|
||
const proEditorOptions: ProEditorOptions<Store, ProEditorStore> = { | ||
name: 'store-name', // 每个 store 需要有自己的唯一名称 | ||
partialize: (s) => ({ data: s.data }), // 支持按需接入 | ||
}; | ||
|
||
export const useStore = create<Store>()( | ||
|
||
// createStore 是 StoreCreator 类型的对象 | ||
proEditorMiddleware(createStore, proEditorOptions)), | ||
); | ||
``` | ||
|
||
多个 Store 使用的方式: | ||
|
||
```tsx | pure | ||
import { ProEditorProvider } from '@ant-design/pro-editor'; | ||
|
||
import { useAStore } from './storeA'; | ||
import { useBStore } from './storeB'; | ||
|
||
export default () => { | ||
return ( | ||
<ProEditorProvider store={[useAStore, useBStore]}> | ||
<App /> | ||
</ProEditorProvider> | ||
); | ||
}; | ||
``` | ||
|
||
多 Store 撤销重做互相隔离 | ||
|
||
```tsx | pure | ||
<div> | ||
<ProEditorProvider store={[cStore]}> | ||
<A /> | ||
</ProEditorProvider> | ||
|
||
<ProEditorProvider store={[dStore]}> | ||
<B /> | ||
</ProEditorProvider> | ||
</div> | ||
``` | ||
|
||
### 设定历史记录 | ||
|
||
```ts | ||
const createStore: StateCreator<Store, [['zustand/devtools', never], ['pro-editor', never]]> = ( | ||
set, | ||
get, | ||
) => ({ | ||
tabs: '1', | ||
switchTabs: (key) => { | ||
set({ tabs: key }); | ||
}, | ||
plusWithoutHistory: () => { | ||
set((s) => ({ ...s, data: s.data + 2 }), false, { | ||
type: 'plusWithoutHistory', | ||
// 不进入历史记录 | ||
recordHistory: false, | ||
}); | ||
}, | ||
|
||
plus: () => { | ||
const nextData = get().data + 1; | ||
|
||
// 默认进入历史记录 | ||
set({ data: nextData }, false, { type: 'plus' }); | ||
}, | ||
data: 3, | ||
}); | ||
``` |
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
Oops, something went wrong.