easy-richtextarea 4.0.19 → 4.0.20

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/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 { push } = arrayUtilities;
10
+ const { clear, filter, unshift } = arrayUtilities;
11
11
 
12
12
  export default class UndoBuffer {
13
- constructor(position, operations) {
14
- this.position = position;
15
- this.operations = operations;
13
+ constructor(undoOperations, redoOperations) {
14
+ this.undoOperations = undoOperations;
15
+ this.redoOperations = redoOperations;
16
16
  }
17
17
 
18
- getPosition() {
19
- return this.position;
18
+ getUndoOperations() {
19
+ return this.undoOperations;
20
20
  }
21
-
22
- getOperations() {
23
- return this.operations;
21
+
22
+ getRedoOperations() {
23
+ return this.redoOperations;
24
24
  }
25
25
 
26
26
  undo() {
27
- let operation = null;
27
+ let undoOperation = null;
28
28
 
29
- if (this.position > 0) {
30
- this.position--;
29
+ const undoOperationsLength = this.undoOperations.length;
31
30
 
32
- operation = this.operations[this.position];
31
+ if (undoOperationsLength > 0) {
32
+ undoOperation = this.undoOperations.shift();
33
33
 
34
- const invertedOperation = operation.invert(InsertOperation, DeleteOperation);
34
+ const invertedUndoOperation = undoOperation.invert(InsertOperation, DeleteOperation),
35
+ redoOperation = invertedUndoOperation; ///
35
36
 
36
- operation = invertedOperation; ///
37
+ this.redoOperations.unshift(redoOperation);
37
38
  }
38
39
 
39
- return operation;
40
+ return undoOperation;
40
41
  }
41
42
 
42
43
  redo() {
43
- let operation = null;
44
+ let redoOperation = null;
45
+
46
+ const redoOperationsLength = this.redoOperations.length;
44
47
 
45
- const operationsLength = this.operations.length;
48
+ if (redoOperationsLength > 0) {
49
+ redoOperation = this.redoOperations.shift();
46
50
 
47
- if (this.position < operationsLength) {
48
- operation = this.operations[this.position];
51
+ const invertedRedoOperation = redoOperation.invert(InsertOperation, DeleteOperation),
52
+ undoOperation = invertedRedoOperation; ///
49
53
 
50
- this.position++;
54
+ this.undoOperations.unshift(undoOperation);
51
55
  }
52
56
 
53
- return operation;
57
+ return redoOperation;
54
58
  }
55
59
 
56
60
  transform(operations) {
57
- this.operations = transformOperations(this.operations, operations);
61
+ this.undoOperations = transformOperations(this.undoOperations, operations);
58
62
 
59
- this.filterEmptyOperations();
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 start = this.position; ///
71
+ const invertedOperations = invertOperations(operations),
72
+ reversedInvertedOperations = reverseOperations(invertedOperations),
73
+ undoOperations = reversedInvertedOperations; ///
64
74
 
65
- this.operations.splice(start);
75
+ unshift(this.undoOperations, undoOperations);
66
76
 
67
- push(this.operations, operations);
77
+ clear(this.redoOperations);
78
+ }
68
79
 
69
- const operationsLength = this.operations.length;
80
+ static fromNothing() {
81
+ const undoOperations = [],
82
+ redoOperations = [],
83
+ undoBuffer = new UndoBuffer(undoOperations, redoOperations);
70
84
 
71
- this.position = operationsLength; ///
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
- filterEmptyOperations() {
75
- let length = this.operations.length,
76
- position = 0;
93
+ return invertedOperation;
94
+ });
77
95
 
78
- while (position < length) {
79
- const operation = this.operations[position];
96
+ return invertedOperations;
97
+ }
80
98
 
81
- if (operation instanceof EmptyOperation) {
82
- const start = position, ///
83
- deleteCount = 1;
99
+ function reverseOperations(operations) {
100
+ const reversedOperations = operations.reduce((reversedOperations, operation) => {
101
+ const reversedOperation = operation; ///
84
102
 
85
- this.operations.splice(start, deleteCount);
103
+ reversedOperations.unshift(reversedOperation);
86
104
 
87
- if (this.position > position) {
88
- this.position--;
89
- }
90
- } else {
91
- position++;
92
- }
105
+ return reversedOperations;
106
+ }, []);
93
107
 
94
- length = this.operations.length;
95
- }
96
- }
108
+ return reversedOperations;
109
+ }
97
110
 
98
- static fromNothing() {
99
- const position = 0,
100
- operations = [],
101
- undoBuffer = new UndoBuffer(position, operations);
111
+ function filterEmptyOperations(operations) {
112
+ filter(operations, (operation) => {
113
+ const operationEmptyOperation = (operation instanceof EmptyOperation);
102
114
 
103
- return undoBuffer;
104
- }
115
+ if (!operationEmptyOperation) {
116
+ return true;
117
+ }
118
+ });
105
119
  }