|
1 | 1 | import { transformAll } from "../collaborative/ot/ot";
|
2 | 2 | import { Revision } from "../collaborative/revisions";
|
3 | 3 | import { inverseCommand } from "../helpers/inverse_commands";
|
4 |
| -import { createEmptyStructure } from "../helpers/state_manager_helpers"; |
5 | 4 | import { StateObserver } from "../state_observer";
|
6 | 5 | import { CoreCommand, HistoryChange, UID } from "../types";
|
7 | 6 | import { SelectiveHistory } from "./selective_history";
|
@@ -56,28 +55,21 @@ function revertChanges(revisions: readonly Revision[]) {
|
56 | 55 | for (const revision of revisions.slice().reverse()) {
|
57 | 56 | for (let i = revision.changes.length - 1; i >= 0; i--) {
|
58 | 57 | const change = revision.changes[i];
|
59 |
| - applyChange(change, "before"); |
| 58 | + applyChange(change); |
60 | 59 | }
|
61 | 60 | }
|
62 | 61 | }
|
63 | 62 |
|
64 | 63 | /**
|
65 | 64 | * Apply the changes of the given HistoryChange to the state
|
66 | 65 | */
|
67 |
| -function applyChange(change: HistoryChange, target: "before" | "after") { |
68 |
| - let val = change.path[0]; |
69 |
| - const key = change.path.at(-1); |
70 |
| - for (let pathIndex = 1; pathIndex < change.path.slice(0, -1).length; pathIndex++) { |
71 |
| - const p = change.path[pathIndex]; |
72 |
| - if (val[p] === undefined) { |
73 |
| - const nextPath = change.path[pathIndex + 1]; |
74 |
| - val[p] = createEmptyStructure(nextPath); |
75 |
| - } |
76 |
| - val = val[p]; |
77 |
| - } |
78 |
| - if (change[target] === undefined) { |
79 |
| - delete val[key]; |
| 66 | +function applyChange(change: HistoryChange) { |
| 67 | + const target = change.target; |
| 68 | + const key = change.key; |
| 69 | + const before = change.before; |
| 70 | + if (before === undefined) { |
| 71 | + delete target[key]; |
80 | 72 | } else {
|
81 |
| - val[key] = change[target]; |
| 73 | + target[key] = before; |
82 | 74 | }
|
83 | 75 | }
|
0 commit comments