From 9f2fa1a9c8354b42ff25170fb5958af8420f5509 Mon Sep 17 00:00:00 2001 From: roymondchen Date: Thu, 18 Jun 2026 17:04:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(editor):=20=E6=8A=BD=E7=A6=BB=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E8=AE=B0=E5=BD=95=E5=9B=9E=E6=BB=9A=E4=BA=A4=E4=BA=92?= =?UTF-8?q?=E5=B9=B6=E5=BC=80=E6=94=BE=E5=A4=8D=E7=94=A8=E5=85=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/guide/advanced/history-list.md | 38 ++ .../editor/src/components/CompareForm.vue | 14 +- packages/editor/src/index.ts | 2 + .../history-list/HistoryDiffDialog.vue | 7 +- .../layouts/history-list/HistoryListPanel.vue | 255 +---------- .../src/layouts/history-list/composables.ts | 78 +--- .../layouts/history-list/useHistoryList.ts | 60 +++ .../layouts/history-list/useHistoryRevert.ts | 400 ++++++++++++++++++ packages/editor/src/type.ts | 50 ++- .../tests/unit/components/CompareForm.spec.ts | 25 +- .../history-list/HistoryDiffDialog.spec.ts | 6 +- .../history-list/HistoryListPanel.spec.ts | 70 ++- .../layouts/history-list/composables.spec.ts | 2 +- .../history-list/useHistoryRevert.spec.ts | 111 +++++ 14 files changed, 764 insertions(+), 354 deletions(-) create mode 100644 packages/editor/src/layouts/history-list/useHistoryList.ts create mode 100644 packages/editor/src/layouts/history-list/useHistoryRevert.ts create mode 100644 packages/editor/tests/unit/layouts/history-list/useHistoryRevert.spec.ts diff --git a/docs/guide/advanced/history-list.md b/docs/guide/advanced/history-list.md index c263850e..68330156 100644 --- a/docs/guide/advanced/history-list.md +++ b/docs/guide/advanced/history-list.md @@ -67,6 +67,44 @@ const menu = ref({ - 数据源:`dataSourceService.revertById(uuids)` - 代码块:`codeBlockService.revertById(uuids)` +#### 复用面板的「交互式回滚 / 查看差异」流程 + +上面的 `revert*` 方法是**静默**的:它们直接执行反向应用,不做任何校验与二次确认。如果业务方想在自己的入口(自定义按钮、右键菜单等)里复用历史面板那一套**完整交互流程**——即「目标数据已删除的前置校验 + 失败提示」「可差异步骤弹差异确认弹窗 / 其余步骤弹普通二次确认框」「用户确认后才回滚」,以及「查看差异」弹窗——可以直接 import `useHistoryRevert`: + +```ts +import { useHistoryRevert } from '@tmagic/editor'; + +// editorRef 为 组件实例,其 expose 出的即各 service 集合(Services) +const { + onPageRevert, + onDataSourceRevert, + onCodeBlockRevert, + onPageDiff, + onDataSourceDiff, + onCodeBlockDiff, +} = useHistoryRevert(editorRef.value); + +// 回滚:可差异步骤弹出差异确认弹窗、其余步骤弹普通二次确认框;用户点「确定」后回滚第 index 步, +// 命中前置校验或用户取消时不执行,返回 null +await onPageRevert(index); +await onDataSourceRevert(id, index); +await onCodeBlockRevert(id, index); + +// 查看差异:可差异步骤弹出只读差异弹窗展示前后值差异,无可对比内容时不弹窗 +onPageDiff(index); +onDataSourceDiff(id, index); +onCodeBlockDiff(id, index); +``` + +回滚确认弹窗与查看差异弹窗均由 `useHistoryRevert` 内部**按需动态挂载** `HistoryDiffDialog` 实现,业务方无需自行挂载任何弹窗组件。第二个参数 `options` 可选: + +| 字段 | 必填 | 说明 | +| --- | --- | --- | +| `appContext` | 否 | 父级应用上下文,用于让动态挂载的差异确认弹窗继承全局组件 / 指令 / provide / 插件(Element Plus、`@tmagic/form` 字段组件等)。在组件 `setup` 中调用时会自动取当前组件的 `appContext`,无需手动传;仅当在组件 setup 之外调用时才需显式传入(如 `editorApp._context`)。 | +| `extendState` | 否 | 透传给差异确认弹窗的 `extendState`(同 Editor 的 [`extendFormState`](#自定义对比判断)),使对比表单中依赖业务上下文的 `display` / `disabled` 等 `filterFunction` 正常工作。 | + +> 若只需要无确认、无校验的静默回滚,直接用上面的 `editorService.revertPageStep` 等即可,无需 `useHistoryRevert`。 + ### 4. 差异对比 在前后值都存在的 `update` 步骤上提供「查看差异」入口,点击后弹出差异对话框。对话框支持两个维度的切换: diff --git a/packages/editor/src/components/CompareForm.vue b/packages/editor/src/components/CompareForm.vue index 893fae9e..8180c38a 100644 --- a/packages/editor/src/components/CompareForm.vue +++ b/packages/editor/src/components/CompareForm.vue @@ -17,14 +17,14 @@