Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: undo/redo Middleware #74

Merged
merged 15 commits into from
Sep 5, 2023
Merged

feat: undo/redo Middleware #74

merged 15 commits into from
Sep 5, 2023

Conversation

arvinxx
Copy link
Collaborator

@arvinxx arvinxx commented Aug 31, 2023

背景

撤销重做是编辑器场景保障用户体验的一个重要特性。ProEditor作为编辑器框架需要提供原子化的能力支持。

现状

ProTableEditor 的撤销重做能力已经实现,但当前需要通过 ProBuilder 特定的使用方式才可以获取,缺少独立使用的方法。

因此需要将该能力抽取成独立使用的模式。

使用方式

初始化

  1. 外层包裹 ProEditorProvider,传入相应的 zustand store
import { ProEditorProvider, ProEditorStoreUpdater } from '@ant-design/pro-editor';

import { useStore } from './store';


export default () => {
  return (
    <ProEditorProvider store=[useStore]>
      <App />
    </ProEditorProvider>
  );
};
  1. zustand store 包裹 ProEditorMiddleware
import { proEditorMiddleware, ProEditorOptions } from '@ant-design/pro-editor';

interface ProEditorStore extends Partial<Store> { }


const proEditorOptions: ProEditorOptions<Store, ProEditorStore> = {
  name: 'store-name',
  partialize: (s) => ({ data: s.data }), // 支持按需接入
};

export const useStore = create<Store>()(

  // createStore 是 StoreCreator 类型的对象
  proEditorMiddleware(createStore, proEditorOptions)),
);

多个 Store 使用的方式:

import { ProEditorProvider, ProEditorStoreUpdater } from '@ant-design/pro-editor';

import { useAStore } from './storeA';
import { useBStore } from './storeB';


export default () => {
  return (
    <ProEditorProvider  store=[useAStore,useBStore]>
      <App />
    </ProEditorProvider>
  );
};

多 Store 撤销重做互相隔离

<>
  <ProEditorProvider store=[cStore]>
    <A />
  </ProEditorProvider>

  <ProEditorProvider store=[dStore]>
    <B />
  </ProEditorProvider>
</>

设定历史记录

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,
});

实现效果

  • 基础的撤销重做;
  • 撤销栈 debounce(按时间智能切分);
  • 按需加入撤销栈;
  • devtools 可视化变更;
  • 撤销、回退栈长度;
  • 历史栈明细;

@arvinxx arvinxx changed the title feat: undo/redo Middleware WIP:feat: undo/redo Middleware Aug 31, 2023
@github-actions
Copy link

github-actions bot commented Aug 31, 2023

🎊 PR Preview ff18649 has been successfully built and deployed to https://ant-design-pro-editor-preview-pr-74.surge.sh

🕐 Build time: 130.636s

🤖 By surge-preview

@arvinxx arvinxx marked this pull request as draft August 31, 2023 12:07
@arvinxx arvinxx changed the title WIP:feat: undo/redo Middleware feat: undo/redo Middleware Aug 31, 2023
@codecov-commenter
Copy link

codecov-commenter commented Sep 5, 2023

Codecov Report

Patch coverage: 88.69% and project coverage change: +0.03% 🎉

Comparison is base (7a8e560) 87.66% compared to head (ff18649) 87.69%.

Additional details and impacted files
@@            Coverage Diff             @@
##            alpha      #74      +/-   ##
==========================================
+ Coverage   87.66%   87.69%   +0.03%     
==========================================
  Files         338      349      +11     
  Lines       21815    22451     +636     
  Branches      977     1022      +45     
==========================================
+ Hits        19124    19689     +565     
- Misses       2691     2762      +71     
Files Changed Coverage Δ
src/IconPicker/features/PickerPanel.tsx 25.88% <ø> (+0.30%) ⬆️
src/ProEditor/store/slices/general.ts 72.97% <72.97%> (ø)
src/ProEditor/middleware/pro-editor/index.ts 79.48% <79.48%> (ø)
src/ProEditor/ProEditorProvider/StoreUpdater.tsx 80.80% <80.80%> (ø)
src/ProEditor/utils/yjs.ts 90.24% <90.24%> (ø)
src/ProEditor/store/slices/config.ts 93.83% <93.83%> (ø)
src/ProEditor/ProEditorProvider/index.tsx 100.00% <100.00%> (ø)
src/ProEditor/hooks/useProEditor.ts 100.00% <100.00%> (ø)
src/ProEditor/index.ts 100.00% <100.00%> (ø)
src/ProEditor/middleware/index.ts 100.00% <100.00%> (ø)
... and 3 more

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@arvinxx arvinxx changed the base branch from main to alpha September 5, 2023 03:38
@arvinxx arvinxx marked this pull request as ready for review September 5, 2023 03:38
@rdmclin2 rdmclin2 self-requested a review September 5, 2023 03:46
Copy link
Member

@rdmclin2 rdmclin2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@arvinxx arvinxx merged commit 7699738 into alpha Sep 5, 2023
4 checks passed
@arvinxx arvinxx deleted the undo branch September 5, 2023 03:48
@arvinxx arvinxx mentioned this pull request Sep 5, 2023
2 tasks
rdmclin2 pushed a commit that referenced this pull request Sep 5, 2023
## [Version&nbsp;0.19.0-alpha.1](v0.18.1...v0.19.0-alpha.1)
<sup>Released on **2023-09-05**</sup>

#### ✨ 新特性

- Undo/redo Middleware.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* Undo/redo Middleware, closes [#74](#74) ([7699738](7699738))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@github-actions
Copy link

github-actions bot commented Sep 5, 2023

🎉 This PR is included in version 0.19.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

rdmclin2 pushed a commit that referenced this pull request Sep 14, 2023
## [Version&nbsp;0.21.0-alpha.1](v0.20.2...v0.21.0-alpha.1)
<sup>Released on **2023-09-14**</sup>

#### ✨ 新特性

- Undo/redo Middleware.

#### 🐛 修复

- Fix compatible with subscribeWithSelector middleware.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* Undo/redo Middleware, closes [#74](#74) ([7699738](7699738))

#### What's fixed

* Fix compatible with subscribeWithSelector middleware, closes [#89](#89) ([24ffb15](24ffb15))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@github-actions
Copy link

🎉 This PR is included in version 0.21.0-alpha.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

arvinxx added a commit that referenced this pull request Nov 24, 2023
* 🚧 wip: undo ing

* ✨ feat: 初步实现 undo redo 方法

* ✨ feat: 支持多个 Store 共存的能力

* ✨ feat: 支持历史记录的操作明细

* ♻️ refactor: refactor the ProEditorProvider

* ✅ test: fix test

* 📝 docs: 更新 redo undo 文档

* 🏷️ feat: 兼容 internal type

* ✅ test: update test

* 📸 test: update snapshot

* ✅ test: 补充测试

* 📝 docs: 补充使用文档

* 🚨 chore: fix lint

* 📝 docs: 更新文档

* ⏪ chore: revert ProBuilder 的变更
rdmclin2 pushed a commit that referenced this pull request Nov 24, 2023
## [Version&nbsp;0.28.0-alpha.1](v0.27.0...v0.28.0-alpha.1)
<sup>Released on **2023-11-24**</sup>

#### ✨ 新特性

- Undo/redo Middleware.

#### 🐛 修复

- Fix compatible with subscribeWithSelector middleware.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* Undo/redo Middleware, closes [#74](#74) ([44551aa](44551aa))

#### What's fixed

* Fix compatible with subscribeWithSelector middleware, closes [#89](#89) ([b11cb36](b11cb36))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
rdmclin2 pushed a commit that referenced this pull request Nov 24, 2023
## [Version&nbsp;0.28.0](v0.27.0...v0.28.0)
<sup>Released on **2023-11-24**</sup>

#### ✨ 新特性

- Undo/redo Middleware.

#### 🐛 修复

- Fix compatible with subscribeWithSelector middleware.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* Undo/redo Middleware, closes [#74](#74) ([44551aa](44551aa))

#### What's fixed

* Fix compatible with subscribeWithSelector middleware, closes [#89](#89) ([b11cb36](b11cb36))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants