2022-11-20 11:59:11 -06:00
|
|
|
/**
|
|
|
|
* Command pattern to allow for editing history
|
|
|
|
*/
|
2022-11-21 23:16:17 -06:00
|
|
|
|
|
|
|
const _commands_events = new Observer();
|
|
|
|
|
2022-11-20 11:59:11 -06:00
|
|
|
const commands = {
|
2022-11-20 17:10:03 -06:00
|
|
|
current: -1,
|
|
|
|
history: [],
|
|
|
|
undo(n = 1) {
|
|
|
|
for (var i = 0; i < n && this.current > -1; i++) {
|
|
|
|
this.history[this.current--].undo();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
redo(n = 1) {
|
|
|
|
for (var i = 0; i < n && this.current + 1 < this.history.length; i++) {
|
|
|
|
this.history[++this.current].redo();
|
|
|
|
}
|
|
|
|
},
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
/**
|
|
|
|
* These are basic commands that can be done/undone
|
|
|
|
*
|
|
|
|
* They must contain a 'run' method that performs the action the first time,
|
|
|
|
* a 'undo' method that undoes that action and a 'redo' method that does the
|
|
|
|
* action again, but without requiring parameters. 'redo' is by default the
|
|
|
|
* same as 'run'.
|
|
|
|
*
|
|
|
|
* The 'run' and 'redo' functions will receive a 'options' parameter which will be
|
|
|
|
* forwarded directly to the operation, and a 'state' parameter that
|
|
|
|
* can be used to store state for undoing things.
|
|
|
|
*
|
|
|
|
* The 'state' object will be passed to the 'undo' function as well.
|
|
|
|
*/
|
|
|
|
createCommand(name, run, undo, redo = run) {
|
2022-11-21 23:16:17 -06:00
|
|
|
const command = function runWrapper(title, options) {
|
2022-11-20 17:10:03 -06:00
|
|
|
// Create copy of options and state object
|
|
|
|
const copy = {};
|
|
|
|
Object.assign(copy, options);
|
|
|
|
const state = {};
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-23 16:06:59 -06:00
|
|
|
const entry = {
|
|
|
|
id: guid(),
|
|
|
|
title,
|
|
|
|
state,
|
|
|
|
};
|
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
// Attempt to run command
|
|
|
|
try {
|
2022-11-21 23:16:17 -06:00
|
|
|
run(title, copy, state);
|
2022-11-20 17:10:03 -06:00
|
|
|
} catch (e) {
|
|
|
|
console.warn(`Error while running command '${name}' with options:`);
|
|
|
|
console.warn(copy);
|
|
|
|
console.warn(e);
|
|
|
|
return;
|
|
|
|
}
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
const undoWrapper = () => {
|
|
|
|
console.debug(`Undoing ${name}, currently ${commands.current}`);
|
2022-11-21 23:16:17 -06:00
|
|
|
undo(title, state);
|
|
|
|
_commands_events.emit({
|
2022-11-23 16:06:59 -06:00
|
|
|
id: entry.id,
|
2022-11-21 23:16:17 -06:00
|
|
|
name,
|
|
|
|
action: "undo",
|
|
|
|
state,
|
|
|
|
current: commands.current,
|
|
|
|
});
|
2022-11-20 17:10:03 -06:00
|
|
|
};
|
|
|
|
const redoWrapper = () => {
|
|
|
|
console.debug(`Redoing ${name}, currently ${commands.current}`);
|
2022-11-21 23:16:17 -06:00
|
|
|
redo(title, copy, state);
|
|
|
|
_commands_events.emit({
|
2022-11-23 16:06:59 -06:00
|
|
|
id: entry.id,
|
2022-11-21 23:16:17 -06:00
|
|
|
name,
|
|
|
|
action: "redo",
|
|
|
|
state,
|
|
|
|
current: commands.current,
|
|
|
|
});
|
2022-11-20 17:10:03 -06:00
|
|
|
};
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
// Add to history
|
2022-11-23 16:06:59 -06:00
|
|
|
if (commands.history.length > commands.current + 1) {
|
|
|
|
commands.history.forEach((entry, index) => {
|
|
|
|
if (index >= commands.current + 1)
|
|
|
|
_commands_events.emit({
|
|
|
|
id: entry.id,
|
|
|
|
name,
|
|
|
|
action: "deleted",
|
|
|
|
state,
|
|
|
|
current: commands.current,
|
|
|
|
});
|
|
|
|
});
|
2022-11-21 23:16:17 -06:00
|
|
|
|
2022-11-23 16:06:59 -06:00
|
|
|
commands.history.splice(commands.current + 1);
|
|
|
|
}
|
2022-11-21 23:16:17 -06:00
|
|
|
|
|
|
|
commands.history.push(entry);
|
2022-11-20 17:10:03 -06:00
|
|
|
commands.current++;
|
2022-11-21 23:16:17 -06:00
|
|
|
|
2022-11-23 16:06:59 -06:00
|
|
|
entry.undo = undoWrapper;
|
|
|
|
entry.redo = redoWrapper;
|
|
|
|
|
2022-11-21 23:16:17 -06:00
|
|
|
_commands_events.emit({
|
2022-11-23 16:06:59 -06:00
|
|
|
id: entry.id,
|
2022-11-21 23:16:17 -06:00
|
|
|
name,
|
|
|
|
action: "run",
|
|
|
|
state,
|
|
|
|
current: commands.current,
|
|
|
|
});
|
|
|
|
|
|
|
|
return entry;
|
2022-11-20 17:10:03 -06:00
|
|
|
};
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
this.types[name] = command;
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
return command;
|
|
|
|
},
|
2022-11-21 23:16:17 -06:00
|
|
|
runCommand(name, title, options) {
|
|
|
|
this.types[name](title, options);
|
2022-11-20 17:10:03 -06:00
|
|
|
},
|
|
|
|
types: {},
|
2022-11-20 11:59:11 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw Image Command, used to draw a Image to a context
|
|
|
|
*/
|
|
|
|
commands.createCommand(
|
2022-11-20 17:10:03 -06:00
|
|
|
"drawImage",
|
2022-11-21 23:16:17 -06:00
|
|
|
(title, options, state) => {
|
2022-11-20 17:10:03 -06:00
|
|
|
if (
|
|
|
|
!options ||
|
|
|
|
options.image === undefined ||
|
|
|
|
options.x === undefined ||
|
|
|
|
options.y === undefined
|
|
|
|
)
|
|
|
|
throw "Command drawImage requires options in the format: {image, x, y, ctx?}";
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
// Check if we have state
|
|
|
|
if (!state.context) {
|
|
|
|
const context = options.ctx || imgCtx;
|
|
|
|
state.context = context;
|
2022-11-20 11:59:11 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
// Saving what was in the canvas before the command
|
|
|
|
const imgData = context.getImageData(
|
|
|
|
options.x,
|
|
|
|
options.y,
|
|
|
|
options.image.width,
|
|
|
|
options.image.height
|
|
|
|
);
|
|
|
|
state.box = {
|
|
|
|
x: options.x,
|
|
|
|
y: options.y,
|
|
|
|
w: options.image.width,
|
|
|
|
h: options.image.height,
|
|
|
|
};
|
|
|
|
// Create Image
|
|
|
|
const cutout = document.createElement("canvas");
|
|
|
|
cutout.width = state.box.w;
|
|
|
|
cutout.height = state.box.h;
|
|
|
|
cutout.getContext("2d").putImageData(imgData, 0, 0);
|
|
|
|
state.original = new Image();
|
|
|
|
state.original.src = cutout.toDataURL();
|
|
|
|
}
|
2022-11-20 15:39:24 -06:00
|
|
|
|
2022-11-20 17:10:03 -06:00
|
|
|
// Apply command
|
|
|
|
state.context.drawImage(options.image, state.box.x, state.box.y);
|
|
|
|
},
|
2022-11-21 23:16:17 -06:00
|
|
|
(title, state) => {
|
2022-11-20 17:10:03 -06:00
|
|
|
// Clear destination area
|
|
|
|
state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h);
|
|
|
|
// Undo
|
|
|
|
state.context.drawImage(state.original, state.box.x, state.box.y);
|
|
|
|
}
|
2022-11-20 11:59:11 -06:00
|
|
|
);
|
2022-11-22 16:24:55 -06:00
|
|
|
|
|
|
|
commands.createCommand(
|
|
|
|
"eraseImage",
|
|
|
|
(title, options, state) => {
|
|
|
|
if (
|
|
|
|
!options ||
|
|
|
|
options.x === undefined ||
|
|
|
|
options.y === undefined ||
|
|
|
|
options.w === undefined ||
|
|
|
|
options.h === undefined
|
|
|
|
)
|
|
|
|
throw "Command eraseImage requires options in the format: {x, y, w, h, ctx?}";
|
|
|
|
|
|
|
|
// Check if we have state
|
|
|
|
if (!state.context) {
|
|
|
|
const context = options.ctx || imgCtx;
|
|
|
|
state.context = context;
|
|
|
|
|
|
|
|
// Saving what was in the canvas before the command
|
|
|
|
const imgData = context.getImageData(
|
|
|
|
options.x,
|
|
|
|
options.y,
|
|
|
|
options.w,
|
|
|
|
options.h
|
|
|
|
);
|
|
|
|
state.box = {
|
|
|
|
x: options.x,
|
|
|
|
y: options.y,
|
|
|
|
w: options.w,
|
|
|
|
h: options.h,
|
|
|
|
};
|
|
|
|
// Create Image
|
|
|
|
const cutout = document.createElement("canvas");
|
|
|
|
cutout.width = state.box.w;
|
|
|
|
cutout.height = state.box.h;
|
|
|
|
cutout.getContext("2d").putImageData(imgData, 0, 0);
|
|
|
|
state.original = new Image();
|
|
|
|
state.original.src = cutout.toDataURL();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply command
|
|
|
|
state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h);
|
|
|
|
},
|
|
|
|
(title, state) => {
|
|
|
|
// Clear destination area
|
|
|
|
state.context.clearRect(state.box.x, state.box.y, state.box.w, state.box.h);
|
|
|
|
// Undo
|
|
|
|
state.context.drawImage(state.original, state.box.x, state.box.y);
|
|
|
|
}
|
|
|
|
);
|