|
2 | 2 | * Command pattern to allow for editing history |
3 | 3 | */ |
4 | 4 | const commands = { |
5 | | - current: -1, |
6 | | - history: [], |
7 | | - undo(n = 1) { |
8 | | - for (var i = 0; i < n && this.current > -1; i++) { |
9 | | - this.history[this.current--].undo(); |
10 | | - } |
11 | | - }, |
12 | | - redo(n = 1) { |
13 | | - for (var i = 0; i < n && this.current + 1 < this.history.length; i++) { |
14 | | - this.history[++this.current].redo(); |
15 | | - } |
16 | | - }, |
| 5 | + current: -1, |
| 6 | + history: [], |
| 7 | + undo(n = 1) { |
| 8 | + for (var i = 0; i < n && this.current > -1; i++) { |
| 9 | + this.history[this.current--].undo(); |
| 10 | + } |
| 11 | + }, |
| 12 | + redo(n = 1) { |
| 13 | + for (var i = 0; i < n && this.current + 1 < this.history.length; i++) { |
| 14 | + this.history[++this.current].redo(); |
| 15 | + } |
| 16 | + }, |
17 | 17 |
|
18 | | - /** |
19 | | - * These are basic commands that can be done/undone |
20 | | - * |
21 | | - * They must contain a 'run' method that performs the action the first time, |
22 | | - * a 'undo' method that undoes that action and a 'redo' method that does the |
23 | | - * action again, but without requiring parameters. 'redo' is by default the |
24 | | - * same as 'run'. |
25 | | - * |
26 | | - * The 'run' and 'redo' functions will receive a 'options' parameter which will be |
27 | | - * forwarded directly to the operation, and a 'state' parameter that |
28 | | - * can be used to store state for undoing things. |
29 | | - * |
30 | | - * The 'state' object will be passed to the 'undo' function as well. |
31 | | - */ |
32 | | - createCommand(name, run, undo, redo = run) { |
33 | | - const command = function runWrapper(options) { |
34 | | - // Create copy of options and state object |
35 | | - const copy = {}; |
36 | | - Object.assign(copy, options); |
37 | | - const state = {}; |
| 18 | + /** |
| 19 | + * These are basic commands that can be done/undone |
| 20 | + * |
| 21 | + * They must contain a 'run' method that performs the action the first time, |
| 22 | + * a 'undo' method that undoes that action and a 'redo' method that does the |
| 23 | + * action again, but without requiring parameters. 'redo' is by default the |
| 24 | + * same as 'run'. |
| 25 | + * |
| 26 | + * The 'run' and 'redo' functions will receive a 'options' parameter which will be |
| 27 | + * forwarded directly to the operation, and a 'state' parameter that |
| 28 | + * can be used to store state for undoing things. |
| 29 | + * |
| 30 | + * The 'state' object will be passed to the 'undo' function as well. |
| 31 | + */ |
| 32 | + createCommand(name, run, undo, redo = run) { |
| 33 | + const command = function runWrapper(options) { |
| 34 | + // Create copy of options and state object |
| 35 | + const copy = {}; |
| 36 | + Object.assign(copy, options); |
| 37 | + const state = {}; |
38 | 38 |
|
39 | | - // Attempt to run command |
40 | | - try { |
41 | | - run(copy, state); |
42 | | - } catch (e) { |
43 | | - console.warn(`Error while running command '${name}' with options:`); |
44 | | - console.warn(copy); |
45 | | - console.warn(e); |
46 | | - return; |
47 | | - } |
| 39 | + // Attempt to run command |
| 40 | + try { |
| 41 | + run(copy, state); |
| 42 | + } catch (e) { |
| 43 | + console.warn(`Error while running command '${name}' with options:`); |
| 44 | + console.warn(copy); |
| 45 | + console.warn(e); |
| 46 | + return; |
| 47 | + } |
48 | 48 |
|
49 | | - const undoWrapper = () => { |
50 | | - console.debug(`Undoing ${name}, currently ${commands.current}`); |
51 | | - undo(state); |
52 | | - }; |
53 | | - const redoWrapper = () => { |
54 | | - console.debug(`Redoing ${name}, currently ${commands.current}`); |
55 | | - redo(copy, state); |
56 | | - }; |
| 49 | + const undoWrapper = () => { |
| 50 | + console.debug(`Undoing ${name}, currently ${commands.current}`); |
| 51 | + undo(state); |
| 52 | + }; |
| 53 | + const redoWrapper = () => { |
| 54 | + console.debug(`Redoing ${name}, currently ${commands.current}`); |
| 55 | + redo(copy, state); |
| 56 | + }; |
57 | 57 |
|
58 | | - // Add to history |
59 | | - if (commands.history.length > commands.current + 1) |
60 | | - commands.history.splice(commands.current + 1); |
61 | | - commands.history.push({undo: undoWrapper, redo: redoWrapper}); |
62 | | - commands.current++; |
63 | | - }; |
| 58 | + // Add to history |
| 59 | + if (commands.history.length > commands.current + 1) |
| 60 | + commands.history.splice(commands.current + 1); |
| 61 | + commands.history.push({undo: undoWrapper, redo: redoWrapper}); |
| 62 | + commands.current++; |
| 63 | + }; |
64 | 64 |
|
65 | | - this.types[name] = command; |
| 65 | + this.types[name] = command; |
66 | 66 |
|
67 | | - return command; |
68 | | - }, |
69 | | - runCommand(name, options) { |
70 | | - this.types[name](options); |
71 | | - }, |
72 | | - types: {}, |
| 67 | + return command; |
| 68 | + }, |
| 69 | + runCommand(name, options) { |
| 70 | + this.types[name](options); |
| 71 | + }, |
| 72 | + types: {}, |
73 | 73 | }; |
74 | 74 |
|
75 | 75 | /** |
76 | 76 | * Draw Image Command, used to draw a Image to a context |
77 | 77 | */ |
78 | 78 | commands.createCommand( |
79 | | - "drawImage", |
80 | | - (options, state) => { |
81 | | - if ( |
82 | | - !options || |
83 | | - options.image === undefined || |
84 | | - options.x === undefined || |
85 | | - options.y === undefined |
86 | | - ) |
87 | | - throw "Command drawImage requires options in the format: {image, x, y, ctx?}"; |
| 79 | + "drawImage", |
| 80 | + (options, state) => { |
| 81 | + if ( |
| 82 | + !options || |
| 83 | + options.image === undefined || |
| 84 | + options.x === undefined || |
| 85 | + options.y === undefined |
| 86 | + ) |
| 87 | + throw "Command drawImage requires options in the format: {image, x, y, ctx?}"; |
88 | 88 |
|
89 | | - // Check if we have state |
90 | | - if (!state.context) { |
91 | | - const context = options.ctx || imgCtx; |
92 | | - state.context = context; |
| 89 | + // Check if we have state |
| 90 | + if (!state.context) { |
| 91 | + const context = options.ctx || imgCtx; |
| 92 | + state.context = context; |
93 | 93 |
|
94 | | - // Saving what was in the canvas before the command |
95 | | - const imgData = context.getImageData( |
96 | | - options.x, |
97 | | - options.y, |
98 | | - options.image.width, |
99 | | - options.image.height |
100 | | - ); |
101 | | - state.box = { |
102 | | - x: options.x, |
103 | | - y: options.y, |
104 | | - w: options.image.width, |
105 | | - h: options.image.height, |
106 | | - }; |
107 | | - // Create Image |
108 | | - const cutout = document.createElement("canvas"); |
109 | | - cutout.width = state.box.w; |
110 | | - cutout.height = state.box.h; |
111 | | - cutout.getContext("2d").putImageData(imgData, 0, 0); |
112 | | - state.original = new Image(); |
113 | | - state.original.src = cutout.toDataURL(); |
114 | | - } |
| 94 | + // Saving what was in the canvas before the command |
| 95 | + const imgData = context.getImageData( |
| 96 | + options.x, |
| 97 | + options.y, |
| 98 | + options.image.width, |
| 99 | + options.image.height |
| 100 | + ); |
| 101 | + state.box = { |
| 102 | + x: options.x, |
| 103 | + y: options.y, |
| 104 | + w: options.image.width, |
| 105 | + h: options.image.height, |
| 106 | + }; |
| 107 | + // Create Image |
| 108 | + const cutout = document.createElement("canvas"); |
| 109 | + cutout.width = state.box.w; |
| 110 | + cutout.height = state.box.h; |
| 111 | + cutout.getContext("2d").putImageData(imgData, 0, 0); |
| 112 | + state.original = new Image(); |
| 113 | + state.original.src = cutout.toDataURL(); |
| 114 | + } |
115 | 115 |
|
116 | | - // Apply command |
117 | | - state.context.drawImage(options.image, state.box.x, state.box.y); |
118 | | - }, |
119 | | - (state) => { |
120 | | - // Clear destination area |
121 | | - state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h); |
122 | | - // Undo |
123 | | - state.context.drawImage(state.original, state.box.x, state.box.y); |
124 | | - } |
| 116 | + // Apply command |
| 117 | + state.context.drawImage(options.image, state.box.x, state.box.y); |
| 118 | + }, |
| 119 | + (state) => { |
| 120 | + // Clear destination area |
| 121 | + state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h); |
| 122 | + // Undo |
| 123 | + state.context.drawImage(state.original, state.box.x, state.box.y); |
| 124 | + } |
125 | 125 | ); |
0 commit comments