|
1052 | 1052 | Object.defineProperty(exports, "__esModule", { value: true }); |
1053 | 1053 | exports.redo = exports.undo = void 0; |
1054 | 1054 | function undo(numUndo) { |
1055 | | - var _this = this; |
1056 | 1055 | if (numUndo === void 0) { numUndo = 1; } |
1057 | | - if (this._pathIndex >= numUndo - 1 && this._pathIndex - numUndo < this._drawnPaths.length) { |
1058 | | - this.graphPixels = this._blankGraph(); // Start with a blank graph |
1059 | | - var originalMode = this.mode, originalBrushColor = this.brushColor, originalBrushSize = this.brushSize, originalEraserSize = this.eraserSize; |
1060 | | - this._removeDOMEvents(); |
1061 | | - this._drawnPaths.slice(0, this._pathIndex - numUndo + 1).forEach(function (path) { |
1062 | | - _this.mode = path.mode; |
1063 | | - _this.brushColor = path.color; |
1064 | | - _this.brushSize = path.brushSize; |
1065 | | - _this.eraserSize = path.eraserSize; |
1066 | | - _this._lastCoords.delete('temp'); |
1067 | | - path.pathCoords.forEach(function (coord) { |
1068 | | - if (coord[2] === false) { |
1069 | | - _this._stroke(coord[0], coord[1], 'temp'); // Replay all strokes |
1070 | | - _this._lastCoords.set('temp', [coord[0], coord[1]]); |
1071 | | - } |
1072 | | - else |
1073 | | - _this._plot(coord[0], coord[1]); |
1074 | | - }); |
1075 | | - }); |
1076 | | - this.mode = originalMode; |
1077 | | - this.brushColor = originalBrushColor; |
1078 | | - this.brushSize = originalBrushSize; |
1079 | | - this.eraserSize = originalEraserSize; |
1080 | | - this._pathIndex -= numUndo; |
1081 | | - this._lastCoords.delete('temp'); |
| 1056 | + if (this._currentSnapshotIndex - numUndo >= 0 && |
| 1057 | + this._currentSnapshotIndex - numUndo < this._snapshots.length) { |
| 1058 | + var wasDrawing = this._isDrawing; |
| 1059 | + this.stopRender(); |
| 1060 | + this.graphPixels = this._loadData(this._snapshots[this._currentSnapshotIndex - numUndo]); |
| 1061 | + this._currentSnapshotIndex -= numUndo; |
1082 | 1062 | this._display(this.graphPixels); |
1083 | | - if (this._isDrawing) |
| 1063 | + if (wasDrawing) |
1084 | 1064 | this.startRender(); |
1085 | 1065 | } |
1086 | 1066 | return this; |
|
1118 | 1098 | } |
1119 | 1099 | exports.changeMode = changeMode; |
1120 | 1100 | function clear() { |
1121 | | - this._drawnPaths = []; |
1122 | | - this._pathIndex = -1; |
| 1101 | + this._snapshots = []; |
| 1102 | + this._currentSnapshotIndex = 0; |
1123 | 1103 | this._lastCoords.clear(); |
1124 | 1104 | this.graphPixels = this._blankGraph(); |
| 1105 | + this._snapshots[0] = this.getData(); |
1125 | 1106 | this._display(this.graphPixels); |
1126 | 1107 | return this; |
1127 | 1108 | } |
|
1135 | 1116 | this.eraserSize = this.options.eraserSize; |
1136 | 1117 | this.mode = this.options.mode; |
1137 | 1118 | this._isDrawing = false; |
1138 | | - this._drawnPaths = []; |
1139 | | - this._pathIndex = -1; |
| 1119 | + this._currentSnapshotIndex = 0; |
| 1120 | + this._snapshots = [this.getData()]; |
1140 | 1121 | this._lastCoords.clear(); |
1141 | 1122 | this.stopRender(); |
1142 | 1123 | } |
|
1169 | 1150 | }); |
1170 | 1151 |
|
1171 | 1152 | var stroke = createCommonjsModule(function (module, exports) { |
1172 | | - var __spreadArrays = (commonjsGlobal && commonjsGlobal.__spreadArrays) || function () { |
1173 | | - for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; |
1174 | | - for (var r = Array(s), k = 0, i = 0; i < il; i++) |
1175 | | - for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) |
1176 | | - r[k] = a[j]; |
1177 | | - return r; |
1178 | | - }; |
1179 | 1153 | Object.defineProperty(exports, "__esModule", { value: true }); |
1180 | 1154 | exports._doStroke = exports._endStroke = exports._startStroke = void 0; |
1181 | 1155 | function _startStroke(coords, identifier) { |
1182 | | - this._drawnPaths[this._pathIndex + 1] = { |
1183 | | - pathCoords: [], |
1184 | | - color: this.brushColor.map(function (x) { return x; }), |
1185 | | - mode: this.mode, |
1186 | | - brushSize: this.brushSize, |
1187 | | - eraserSize: this.eraserSize |
1188 | | - }; |
1189 | | - this._plot.apply(this, coords); |
| 1156 | + if (this._currentSnapshotIndex < this._snapshots.length - 1) |
| 1157 | + this._snapshots.splice(this._currentSnapshotIndex + 1); // Delete all redo snapshots |
| 1158 | + this._plot.apply(// Delete all redo snapshots |
| 1159 | + this, coords); |
1190 | 1160 | this._lastCoords.set(identifier, coords); |
1191 | 1161 | } |
1192 | 1162 | exports._startStroke = _startStroke; |
1193 | 1163 | function _endStroke(endCoords, identifier) { |
1194 | 1164 | this._plot.apply(this, endCoords); |
1195 | | - if (this._drawnPaths[this._pathIndex + 1]) |
1196 | | - this._drawnPaths[this._pathIndex + 1].pathCoords.push(__spreadArrays(endCoords, [true])); |
1197 | 1165 | this._lastCoords.delete(identifier); |
1198 | | - if (this._drawnPaths[this._pathIndex + 1].pathCoords.length === 0) |
1199 | | - this._drawnPaths.splice(-1, 1); |
1200 | | - else { |
1201 | | - this._drawnPaths = this._drawnPaths.slice(0, this._pathIndex + 2); // Overwrite further paths to prevent wrong redos |
1202 | | - this._pathIndex++; |
1203 | | - } |
| 1166 | + this._snapshots[++this._currentSnapshotIndex] = this.getData(); |
1204 | 1167 | } |
1205 | 1168 | exports._endStroke = _endStroke; |
1206 | 1169 | function _doStroke(coords, identifier) { |
1207 | | - this._drawnPaths[this._pathIndex + 1].pathCoords.push(__spreadArrays(coords, [false])); |
1208 | 1170 | this._plot.apply(this, coords); |
1209 | 1171 | this._stroke(coords[0], coords[1], identifier); |
1210 | 1172 | this._lastCoords.set(identifier, coords); |
|
1293 | 1255 | // *****DEFAULTS***** |
1294 | 1256 | _super.call(this, options) || this; |
1295 | 1257 | _this._isDrawing = false; |
1296 | | - _this._drawnPaths = []; |
1297 | | - _this._pathIndex = -1; // Index of path in _drawnPaths |
| 1258 | + _this._snapshots = []; // Undo snapshots |
| 1259 | + _this._currentSnapshotIndex = 0; // Current snapshot |
1298 | 1260 | /** key -> identifier, value -> coordinate |
1299 | 1261 | * For mouse, the key is 'mouse', for touches, stringified identifier -> https://developer.mozilla.org/en-US/docs/Web/API/Touch/identifier |
1300 | 1262 | */ |
|
1375 | 1337 | _this.mode = options.mode; |
1376 | 1338 | // *****DEFAULTS***** |
1377 | 1339 | _this._initializeKernels(); |
| 1340 | + _this._snapshots[0] = _this.getData(); |
1378 | 1341 | return _this; |
1379 | 1342 | } |
1380 | 1343 | // --- Touch Events --- |
|
0 commit comments