-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.js
146 lines (123 loc) · 2.94 KB
/
history.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { DEFAULT_FILL_COLOR } from './constants';
const DEFAULT_CELL = {
config: {
fillColor: DEFAULT_FILL_COLOR,
fontSize: 11,
},
value: '',
};
const DEFAULT_HISTORY_LENGTH = 10;
export class History {
constructor(settings = {}) {
this.commits = [];
this.length = settings.length || DEFAULT_HISTORY_LENGTH;
}
get head() {
return this.commits[this.commits.length - 1] ? this.commits[this.commits.length - 1] : undefined;
}
get tail() {
return this.commits[0];
}
flush() {
const commits = [...this.commits];
this.commits = [];
return commits;
}
shift() {
return this.commits.shift();
}
pop() {
if (this.commits.length > 0) {
return this.commits.pop();
}
}
addCommit(commit) {
if (commit) {
this.commits.push(commit);
}
}
addData(data) {
if (this.commits.length === this.length) {
this.shift();
}
this.commits.push(new Commit(this.head, data));
}
}
class Commit {
constructor(previous, next) {
this.data = next;
if (previous) {
this.diffs = this.generateDiffs(previous, next);
} else {
this.diffs = this.generateOrigin(next);
}
}
generateOrigin(next) {
const diffs = [];
next.forEach((row, rowIdx) => {
row.forEach((col, colIdx) => {
const diff = Diff.createDiffFromOrigin(rowIdx, colIdx, col);
diffs.push(diff);
});
});
return diffs;
}
generateDiffs(head, next) {
const diffs = [];
next.forEach((row, rowIdx) => {
row.forEach((col, colIdx) => {
const { from, to } = Diff.getDiff(head.data[rowIdx][colIdx], col);
if (from && to) {
diffs.push(new Diff(rowIdx, colIdx, from, to));
}
});
});
return diffs;
}
* toIter() {
for (let i = 0; i < this.diffs.length; i++) {
yield [i, this.diffs[i]];
}
}
}
class Diff {
constructor(row, col, from, to) {
this.row = row;
this.col = col;
this.from = from;
this.to = to;
}
static getDiff(previous, { config, value }) {
const from = { config: {} };
const to = { config: {} };
let hasDiff = false;
if (previous.value !== value) {
from.value = previous.value;
to.value = value;
hasDiff = true;
}
for (let prop in config) {
if (config.hasOwnProperty(prop)) {
if (config[prop] !== previous.config[prop]) {
to.config[prop] = config[prop];
from.config[prop] = previous.config[prop];
hasDiff = true;
}
}
}
if (hasDiff) return { from, to };
return { from: false, to: false };
}
static createDiffFromOrigin(row, col, { config, value }) {
const to = { config: {}, value };
const from = DEFAULT_CELL;
for (let prop in config) {
if (config.hasOwnProperty(prop)) {
if (config[prop] !== from.config[prop]) {
to.config[prop] = config[prop];
}
}
}
return new Diff(row, col, from, to);
}
}