easy-richtextarea 4.0.19 → 4.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example.js +239 -138
- package/lib/eventTypes.js +53 -0
- package/lib/example/richTextarea.js +2 -2
- package/lib/example/view.js +21 -26
- package/lib/keyCodes.js +13 -4
- package/lib/richTextarea.js +83 -49
- package/lib/undoBuffer.js +56 -51
- package/package.json +1 -1
- package/src/eventTypes.js +17 -0
- package/src/example/richTextarea.js +3 -11
- package/src/example/view.js +21 -36
- package/src/keyCodes.js +1 -0
- package/src/richTextarea.js +78 -35
- package/src/undoBuffer.js +67 -53
package/src/undoBuffer.js
CHANGED
|
@@ -7,99 +7,113 @@ import InsertOperation from "./operation/insert";
|
|
|
7
7
|
import DeleteOperation from "./operation/delete";
|
|
8
8
|
import transformOperations from "./transform/operations";
|
|
9
9
|
|
|
10
|
-
const {
|
|
10
|
+
const { clear, filter, unshift } = arrayUtilities;
|
|
11
11
|
|
|
12
12
|
export default class UndoBuffer {
|
|
13
|
-
constructor(
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
13
|
+
constructor(undoOperations, redoOperations) {
|
|
14
|
+
this.undoOperations = undoOperations;
|
|
15
|
+
this.redoOperations = redoOperations;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
return this.
|
|
18
|
+
getUndoOperations() {
|
|
19
|
+
return this.undoOperations;
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return this.
|
|
21
|
+
|
|
22
|
+
getRedoOperations() {
|
|
23
|
+
return this.redoOperations;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
undo() {
|
|
27
|
-
let
|
|
27
|
+
let undoOperation = null;
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
this.position--;
|
|
29
|
+
const undoOperationsLength = this.undoOperations.length;
|
|
31
30
|
|
|
32
|
-
|
|
31
|
+
if (undoOperationsLength > 0) {
|
|
32
|
+
undoOperation = this.undoOperations.shift();
|
|
33
33
|
|
|
34
|
-
const
|
|
34
|
+
const invertedUndoOperation = undoOperation.invert(InsertOperation, DeleteOperation),
|
|
35
|
+
redoOperation = invertedUndoOperation; ///
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
this.redoOperations.unshift(redoOperation);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
return
|
|
40
|
+
return undoOperation;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
43
|
redo() {
|
|
43
|
-
let
|
|
44
|
+
let redoOperation = null;
|
|
45
|
+
|
|
46
|
+
const redoOperationsLength = this.redoOperations.length;
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
if (redoOperationsLength > 0) {
|
|
49
|
+
redoOperation = this.redoOperations.shift();
|
|
46
50
|
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
const invertedRedoOperation = redoOperation.invert(InsertOperation, DeleteOperation),
|
|
52
|
+
undoOperation = invertedRedoOperation; ///
|
|
49
53
|
|
|
50
|
-
this.
|
|
54
|
+
this.undoOperations.unshift(undoOperation);
|
|
51
55
|
}
|
|
52
56
|
|
|
53
|
-
return
|
|
57
|
+
return redoOperation;
|
|
54
58
|
}
|
|
55
59
|
|
|
56
60
|
transform(operations) {
|
|
57
|
-
this.
|
|
61
|
+
this.undoOperations = transformOperations(this.undoOperations, operations);
|
|
58
62
|
|
|
59
|
-
this.
|
|
63
|
+
this.redoOperations = transformOperations(this.redoOperations, operations);
|
|
64
|
+
|
|
65
|
+
filterEmptyOperations(this.undoOperations);
|
|
66
|
+
|
|
67
|
+
filterEmptyOperations(this.redoOperations);
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
addOperations(operations) {
|
|
63
|
-
const
|
|
71
|
+
const invertedOperations = invertOperations(operations),
|
|
72
|
+
reversedInvertedOperations = reverseOperations(invertedOperations),
|
|
73
|
+
undoOperations = reversedInvertedOperations; ///
|
|
64
74
|
|
|
65
|
-
this.
|
|
75
|
+
unshift(this.undoOperations, undoOperations);
|
|
66
76
|
|
|
67
|
-
|
|
77
|
+
clear(this.redoOperations);
|
|
78
|
+
}
|
|
68
79
|
|
|
69
|
-
|
|
80
|
+
static fromNothing() {
|
|
81
|
+
const undoOperations = [],
|
|
82
|
+
redoOperations = [],
|
|
83
|
+
undoBuffer = new UndoBuffer(undoOperations, redoOperations);
|
|
70
84
|
|
|
71
|
-
|
|
85
|
+
return undoBuffer;
|
|
72
86
|
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function invertOperations(operations) {
|
|
90
|
+
const invertedOperations = operations.map((operation) => {
|
|
91
|
+
const invertedOperation = operation.invert(InsertOperation, DeleteOperation);
|
|
73
92
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
position = 0;
|
|
93
|
+
return invertedOperation;
|
|
94
|
+
});
|
|
77
95
|
|
|
78
|
-
|
|
79
|
-
|
|
96
|
+
return invertedOperations;
|
|
97
|
+
}
|
|
80
98
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
99
|
+
function reverseOperations(operations) {
|
|
100
|
+
const reversedOperations = operations.reduce((reversedOperations, operation) => {
|
|
101
|
+
const reversedOperation = operation; ///
|
|
84
102
|
|
|
85
|
-
|
|
103
|
+
reversedOperations.unshift(reversedOperation);
|
|
86
104
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
} else {
|
|
91
|
-
position++;
|
|
92
|
-
}
|
|
105
|
+
return reversedOperations;
|
|
106
|
+
}, []);
|
|
93
107
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
108
|
+
return reversedOperations;
|
|
109
|
+
}
|
|
97
110
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
undoBuffer = new UndoBuffer(position, operations);
|
|
111
|
+
function filterEmptyOperations(operations) {
|
|
112
|
+
filter(operations, (operation) => {
|
|
113
|
+
const operationEmptyOperation = (operation instanceof EmptyOperation);
|
|
102
114
|
|
|
103
|
-
|
|
104
|
-
|
|
115
|
+
if (!operationEmptyOperation) {
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
});
|
|
105
119
|
}
|