Skip to content

Commit 48c5dd0

Browse files
committed
fix: 继续修复历史记录相关bug
1 parent 760c965 commit 48c5dd0

File tree

1 file changed

+56
-16
lines changed

1 file changed

+56
-16
lines changed

app/src/core/stage/stageManager/StageHistoryManager.tsx

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Project, ProjectState, service } from "@/core/Project";
22
import { Settings } from "@/core/service/Settings";
33
import { deserialize, serialize } from "@graphif/serializer";
4+
import { cn } from "@udecode/cn";
45
import { Delta, diff, patch } from "jsondiffpatch";
56
import _ from "lodash";
67
import { toast } from "sonner";
@@ -16,6 +17,7 @@ import { toast } from "sonner";
1617
export class HistoryManager {
1718
/**
1819
* 历史记录列表数组
20+
* 每一项都是变化的delta,不是完整的舞台数据!
1921
*/
2022
deltas: Delta[] = [];
2123
/**
@@ -47,6 +49,7 @@ export class HistoryManager {
4749
* @param file
4850
*/
4951
recordStep() {
52+
// this.deltas = this.deltas.splice(this.currentIndex + 1);
5053
this.deltas.splice(this.currentIndex + 1);
5154
// 上面一行的含义:删除从 currentIndex + 1 开始的所有元素。
5255
// [a, b, c, d, e]
@@ -63,28 +66,33 @@ export class HistoryManager {
6366
// [a, b, c]
6467
// 0 1 2 3
6568
// ^
66-
const prev = serialize(this.get(this.currentIndex - 1));
67-
const current = serialize(this.project.stage);
68-
const patch_ = diff(prev, current);
69+
const prev = serialize(this.get(this.currentIndex - 1)); // [C stage]
70+
const current = serialize(this.project.stage); // [D stage]
71+
const patch_ = diff(prev, current); // [D stage] - [C stage] = [d]
6972
if (!patch_) {
7073
this.currentIndex--; // 没有变化,当指针回退到当前位置
7174
return;
7275
}
7376

74-
this.deltas.push(patch_); // D
75-
// [a, b, c, D]
77+
this.deltas.push(patch_);
78+
// [a, b, c, d]
7679
// 0 1 2 3
7780
// ^
78-
if (this.deltas.length > Settings.historySize) {
81+
while (this.deltas.length > Settings.historySize) {
7982
// 当历史记录超过限制时,需要删除最旧的记录
8083
// 但是不能简单删除,因为get方法依赖于从initialStage开始应用所有delta
8184
// 所以我们需要将第一个delta合并到initialStage中,然后删除这个delta
8285
const firstDelta = _.cloneDeep(this.deltas[0]);
8386
this.initialStage = patch(_.cloneDeep(this.initialStage), firstDelta) as any;
87+
this.deltas.shift(); // 删除第一个delta [a]
88+
// [b, c, d]
89+
// 0 1 2 3
90+
// ^
91+
8492
this.currentIndex--;
85-
toast.warning(
86-
"历史记录已满!此时按 ctrl z 撤销可能会触发 严重的 全部内容重叠bug!请调大历史记录容量、或者关闭重新打开文件、或者等待新版本bug修复",
87-
);
93+
// [b, c, d]
94+
// 0 1 2
95+
// ^
8896
}
8997
// 检测index是否越界
9098
if (this.currentIndex >= this.deltas.length) {
@@ -101,10 +109,25 @@ export class HistoryManager {
101109
if (this.currentIndex >= 0) {
102110
this.currentIndex--;
103111
this.project.stage = this.get(this.currentIndex);
104-
toast(`当前进度:${this.currentIndex + 1} / ${this.deltas.length}`);
105-
} else {
106-
toast(`已到撤回到底!${this.currentIndex + 1} / ${this.deltas.length},默认 ctrl + y 反撤销`);
107112
}
113+
toast(
114+
<div className="flex text-sm">
115+
<span className="m-2 flex flex-col justify-center">
116+
<span>当前历史位置</span>
117+
<span className={cn(this.currentIndex === -1 && "text-red-500")}>{this.currentIndex + 1}</span>
118+
</span>
119+
<span className="m-2 flex flex-col justify-center">
120+
<span>当前历史长度</span>
121+
<span className={cn(this.deltas.length === Settings.historySize && "text-yellow-500")}>
122+
{this.deltas.length}
123+
</span>
124+
</span>
125+
<span className="m-2 flex flex-col justify-center">
126+
<span>限定历史长度</span>
127+
<span className="opacity-50">{Settings.historySize}</span>
128+
</span>
129+
</div>,
130+
);
108131
}
109132

110133
/**
@@ -114,10 +137,27 @@ export class HistoryManager {
114137
if (this.currentIndex < this.deltas.length - 1) {
115138
this.currentIndex++;
116139
this.project.stage = this.get(this.currentIndex);
117-
toast(`当前进度:${this.currentIndex + 1} / ${this.deltas.length}`);
118-
} else {
119-
toast(`已到最新状态!${this.currentIndex + 1} / ${this.deltas.length}`);
120140
}
141+
toast(
142+
<div className="flex text-sm">
143+
<span className="m-2 flex flex-col justify-center">
144+
<span>当前历史位置</span>
145+
<span className={cn(this.currentIndex === this.deltas.length - 1 && "text-green-500")}>
146+
{this.currentIndex + 1}
147+
</span>
148+
</span>
149+
<span className="m-2 flex flex-col justify-center">
150+
<span>当前历史长度</span>
151+
<span className={cn(this.deltas.length === Settings.historySize && "text-yellow-500")}>
152+
{this.deltas.length}
153+
</span>
154+
</span>
155+
<span className="m-2 flex flex-col justify-center">
156+
<span>限定历史长度</span>
157+
<span className="opacity-50">{Settings.historySize}</span>
158+
</span>
159+
</div>,
160+
);
121161
}
122162

123163
get(index: number) {
@@ -132,7 +172,7 @@ export class HistoryManager {
132172
// const data = deltas.reduce((acc, delta) => {
133173
// return patch(_.cloneDeep(acc), _.cloneDeep(delta)) as any;
134174
// }, _.cloneDeep(this.initialStage));
135-
let data = _.cloneDeep(this.initialStage);
175+
let data = _.cloneDeep(this.initialStage); // 迭代这个data
136176
for (const delta of deltas) {
137177
data = patch(data, _.cloneDeep(delta)) as any;
138178
}

0 commit comments

Comments
 (0)