Prettified Code!
This commit is contained in:
parent
54ad744d61
commit
32f38d9b1d
4 changed files with 264 additions and 206 deletions
|
@ -1,4 +1,4 @@
|
|||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
|
|
|
@ -23,11 +23,15 @@ const commands = makeReadOnly(
|
|||
_types: {},
|
||||
|
||||
/** @type {Observer<{n: int, cancel: function}>} */
|
||||
get onundo() { return this._onundo; },
|
||||
get onundo() {
|
||||
return this._onundo;
|
||||
},
|
||||
_onundo: new Observer(),
|
||||
|
||||
/** @type {Observer<{n: int, cancel: function}>} */
|
||||
get onredo() { return this._onredo; },
|
||||
get onredo() {
|
||||
return this._onredo;
|
||||
},
|
||||
_onredo: new Observer(),
|
||||
|
||||
/**
|
||||
|
@ -38,8 +42,10 @@ const commands = makeReadOnly(
|
|||
async undo(n = 1) {
|
||||
var cancelled = false;
|
||||
await this._onundo.emit({
|
||||
n:n,
|
||||
cancel: ()=>{cancelled=true;},
|
||||
n: n,
|
||||
cancel: () => {
|
||||
cancelled = true;
|
||||
},
|
||||
});
|
||||
if (cancelled) return;
|
||||
for (var i = 0; i < n && this.current > -1; i++) {
|
||||
|
@ -61,8 +67,10 @@ const commands = makeReadOnly(
|
|||
async redo(n = 1) {
|
||||
let cancelled = false;
|
||||
await this._onredo.emit({
|
||||
n:n,
|
||||
cancel: ()=>{cancelled=true;},
|
||||
n: n,
|
||||
cancel: () => {
|
||||
cancelled = true;
|
||||
},
|
||||
});
|
||||
if (cancelled) return;
|
||||
for (var i = 0; i < n && this.current + 1 < this._history.length; i++) {
|
||||
|
|
|
@ -45,7 +45,10 @@ const selectTransformTool = () =>
|
|||
keyboard.onShortcut({key: "Enter"}, state.entercb);
|
||||
keyboard.onShortcut({shift: true, key: "Enter"}, state.sentercb);
|
||||
keyboard.onShortcut({ctrl: true, key: "Enter"}, state.ctentercb);
|
||||
keyboard.onShortcut({ctrl: true, shift: true, key: "Enter"}, state.sctentercb);
|
||||
keyboard.onShortcut(
|
||||
{ctrl: true, shift: true, key: "Enter"},
|
||||
state.sctentercb
|
||||
);
|
||||
keyboard.onShortcut({key: "Delete"}, state.delcb);
|
||||
keyboard.onShortcut({shift: true, key: "Delete"}, state.sdelcb);
|
||||
|
||||
|
@ -82,13 +85,13 @@ const selectTransformTool = () =>
|
|||
keyboard.deleteShortcut(state.ctrlvcb, "KeyV");
|
||||
keyboard.deleteShortcut(state.ctrlxcb, "KeyX");
|
||||
keyboard.deleteShortcut(state.togglemirror, "Equal");
|
||||
keyboard.deleteShortcut(state.entercb,"Enter");
|
||||
keyboard.deleteShortcut(state.sentercb,"Enter");
|
||||
keyboard.deleteShortcut(state.ctentercb,"Enter");
|
||||
keyboard.deleteShortcut(state.sctentercb,"Enter");
|
||||
keyboard.deleteShortcut(state.delcb,"Delete");
|
||||
keyboard.deleteShortcut(state.sdelcb,"Delete");
|
||||
keyboard.deleteShortcut(state.escapecb,"Escape");
|
||||
keyboard.deleteShortcut(state.entercb, "Enter");
|
||||
keyboard.deleteShortcut(state.sentercb, "Enter");
|
||||
keyboard.deleteShortcut(state.ctentercb, "Enter");
|
||||
keyboard.deleteShortcut(state.sctentercb, "Enter");
|
||||
keyboard.deleteShortcut(state.delcb, "Delete");
|
||||
keyboard.deleteShortcut(state.sdelcb, "Delete");
|
||||
keyboard.deleteShortcut(state.escapecb, "Escape");
|
||||
|
||||
uil.onactive.clear(state.uilayeractivecb);
|
||||
|
||||
|
@ -214,17 +217,18 @@ const selectTransformTool = () =>
|
|||
};
|
||||
|
||||
// Undo/Redo Handling, reset state before Undo/Redo
|
||||
state.undocb= (undo)=>{
|
||||
if (state.selected){
|
||||
state.undocb = (undo) => {
|
||||
if (state.selected) {
|
||||
// Cancel so undo shortcut effectively undoes the current transform, unless requesting multiple steps
|
||||
if (state.selectionTransformed() && undo.n<=1)
|
||||
undo.cancel();
|
||||
if (state.selectionTransformed() && undo.n <= 1) undo.cancel();
|
||||
state.reset(false);
|
||||
}
|
||||
};
|
||||
state.redocb = (redo) => {
|
||||
if (state.selected) {
|
||||
state.reset(false);
|
||||
}
|
||||
state.redocb= (redo)=>{
|
||||
if (state.selected){ state.reset(false); }
|
||||
}
|
||||
};
|
||||
|
||||
// Mirroring
|
||||
state.togglemirror = () => {
|
||||
|
@ -305,8 +309,9 @@ const selectTransformTool = () =>
|
|||
};
|
||||
|
||||
// Check if selection has been transformed in any way.
|
||||
state.selectionTransformed = ()=>{
|
||||
return state.selected &&
|
||||
state.selectionTransformed = () => {
|
||||
return (
|
||||
state.selected &&
|
||||
!(
|
||||
state.selected.rotation === 0 &&
|
||||
state.selected.scale.x === 1 &&
|
||||
|
@ -315,8 +320,9 @@ const selectTransformTool = () =>
|
|||
state.selected.position.y === state.original.sy &&
|
||||
!state.mirrorSelection &&
|
||||
state.original.layer === uil.layer
|
||||
)
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
// Handles left mouse double clicks - Select All Ctrl-A
|
||||
// Holding shift key - Ctrl-Shift-A
|
||||
|
@ -325,13 +331,15 @@ const selectTransformTool = () =>
|
|||
if (state.selected || evn.evn.ctrlKey) return;
|
||||
let shift = evn.evn.shiftKey;
|
||||
// Wait so clickcb doesn't immediately deselect.
|
||||
state.dclickcb_timeout = state.dclickcb_timeout ?? window.setTimeout(async ()=>{
|
||||
state.dclickcb_timeout =
|
||||
state.dclickcb_timeout ??
|
||||
window.setTimeout(async () => {
|
||||
state.dclickcb_timeout = null;
|
||||
if (!state.selected && !selection.exists) {
|
||||
if (shift) state.ctrlsacb(evn);
|
||||
else state.ctrlacb(evn);
|
||||
}
|
||||
},300);
|
||||
}, 300);
|
||||
};
|
||||
|
||||
// Handles right mouse double clicks - Select topmost layer with content under pointer
|
||||
|
@ -340,25 +348,32 @@ const selectTransformTool = () =>
|
|||
if (state.selected) return;
|
||||
// If shift key is held, and current layer is has visible pixels under pointer
|
||||
// select topmost visible layer beneath the active layer
|
||||
let shift = evn.evn.shiftKey
|
||||
&& !uil.active.hidden
|
||||
&& !isCanvasBlank(evn.x,evn.y,2,2,uil.active.canvas);
|
||||
let shift =
|
||||
evn.evn.shiftKey &&
|
||||
!uil.active.hidden &&
|
||||
!isCanvasBlank(evn.x, evn.y, 2, 2, uil.active.canvas);
|
||||
let layer = shift ? uil.active : null;
|
||||
for (let l of uil.layers.toReversed()) {
|
||||
if (shift) {
|
||||
if (layer==l) shift = false;
|
||||
}
|
||||
else if (!l.hidden && !isCanvasBlank(evn.x,evn.y,2,2,l.canvas)){
|
||||
if (layer == l) shift = false;
|
||||
} else if (
|
||||
!l.hidden &&
|
||||
!isCanvasBlank(evn.x, evn.y, 2, 2, l.canvas)
|
||||
) {
|
||||
layer = l;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (layer) {
|
||||
uil.active=layer;
|
||||
state.dclickcb_timeout = state.dclickcb_timeout ?? window.setTimeout(async ()=>{
|
||||
uil.active = layer;
|
||||
state.dclickcb_timeout =
|
||||
state.dclickcb_timeout ??
|
||||
window.setTimeout(async () => {
|
||||
state.dclickcb_timeout = null;
|
||||
if (!state.selected && !selection.exists) { state.ctrlacb(evn); }
|
||||
},300);
|
||||
if (!state.selected && !selection.exists) {
|
||||
state.ctrlacb(evn);
|
||||
}
|
||||
}, 300);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -536,50 +551,57 @@ const selectTransformTool = () =>
|
|||
state.reset();
|
||||
};
|
||||
|
||||
state.keydowncb = (evn) => { };
|
||||
state.keydowncb = (evn) => {};
|
||||
|
||||
// Keyboard callbacks
|
||||
state.keyclickcb = (evn) => { };
|
||||
state.keyclickcb = (evn) => {};
|
||||
|
||||
// Register Delete Shortcut
|
||||
state.delcb = (evn) => { state.applyTransform(true,false,false,false); };
|
||||
state.delcb = (evn) => {
|
||||
state.applyTransform(true, false, false, false);
|
||||
};
|
||||
|
||||
// Register Escape Shortcut
|
||||
state.escapecb = (evn) => { state.reset(false); };
|
||||
state.escapecb = (evn) => {
|
||||
state.reset(false);
|
||||
};
|
||||
|
||||
// Register Shift-Delete Shortcut - Delete Outside Selection and Apply
|
||||
state.sdelcb = (evn) => { state.applyTransform(false,true,false,false); };
|
||||
state.sdelcb = (evn) => {
|
||||
state.applyTransform(false, true, false, false);
|
||||
};
|
||||
|
||||
// Register Enter Shortcut - Apply Transform (Delegates to clickcb)
|
||||
state.entercb = (evn) => { state.clickcb(evn); };
|
||||
state.entercb = (evn) => {
|
||||
state.clickcb(evn);
|
||||
};
|
||||
|
||||
// Register Ctrl-Enter Shortcut - Copy Selection to new layer, restore original
|
||||
state.ctentercb = (evn) => { state.applyTransform(false,false,true,true); };
|
||||
state.ctentercb = (evn) => {
|
||||
state.applyTransform(false, false, true, true);
|
||||
};
|
||||
|
||||
// Register Shift-Enter Shortcut - Move Selection to new layer
|
||||
state.sentercb = (evn) => { state.applyTransform(false,false,true,false); };
|
||||
state.sentercb = (evn) => {
|
||||
state.applyTransform(false, false, true, false);
|
||||
};
|
||||
|
||||
// Register Ctrl-Shift-Enter Shortcut - Copy Visible Selection to new layer
|
||||
state.sctentercb = async (evn) => {
|
||||
var selectBB =
|
||||
state.selected.bb != undefined
|
||||
? state.selected.bb
|
||||
: state.backupBB;
|
||||
state.selected.bb != undefined ? state.selected.bb : state.backupBB;
|
||||
const canvas = uil.getVisible(selectBB, {
|
||||
categories: ["image", "user", "select-display"],
|
||||
});
|
||||
await commands.runCommand("addLayer", "Added Layer");
|
||||
|
||||
await commands.runCommand("drawImage", "Transform Tool Apply",
|
||||
{
|
||||
await commands.runCommand("drawImage", "Transform Tool Apply", {
|
||||
image: canvas,
|
||||
...selectBB,
|
||||
}
|
||||
);
|
||||
});
|
||||
state.reset(false);
|
||||
};
|
||||
|
||||
|
||||
// Register Ctrl-A Shortcut
|
||||
state.ctrlacb = () => {
|
||||
state.reset(false); // Reset to preserve selected content
|
||||
|
@ -717,12 +739,24 @@ const selectTransformTool = () =>
|
|||
// newLayer defaults to null, overriding the forced variants if explicitly set to false
|
||||
// Only checks if Selection exists and content has been selected
|
||||
// Does not check if content has been transformed, eg for deletion/applying to new layer
|
||||
state.applyTransform = async (eraseSelected = false, clearLayer = false, newLayer = null, keepOriginal = false) => {
|
||||
const isBlank = !state.selected ||
|
||||
isCanvasBlank( 0, 0, state.selected.canvas.width, state.selected.canvas.height, state.selected.canvas);
|
||||
state.applyTransform = async (
|
||||
eraseSelected = false,
|
||||
clearLayer = false,
|
||||
newLayer = null,
|
||||
keepOriginal = false
|
||||
) => {
|
||||
const isBlank =
|
||||
!state.selected ||
|
||||
isCanvasBlank(
|
||||
0,
|
||||
0,
|
||||
state.selected.canvas.width,
|
||||
state.selected.canvas.height,
|
||||
state.selected.canvas
|
||||
);
|
||||
|
||||
// Just reset state if nothing is selected, unless Clearing layer
|
||||
if (!state.selected || !clearLayer && isBlank ){
|
||||
if (!state.selected || (!clearLayer && isBlank)) {
|
||||
state.reset(false);
|
||||
return;
|
||||
}
|
||||
|
@ -735,7 +769,8 @@ const selectTransformTool = () =>
|
|||
);
|
||||
|
||||
// Erase Entire Layer
|
||||
if (clearLayer) await commands.runCommand(
|
||||
if (clearLayer)
|
||||
await commands.runCommand(
|
||||
"eraseImage",
|
||||
"Transform Tool Erase",
|
||||
{
|
||||
|
@ -749,7 +784,8 @@ const selectTransformTool = () =>
|
|||
}
|
||||
);
|
||||
// Erase Original Selection Area
|
||||
else if (eraseSelected || !keepOriginal) await commands.runCommand(
|
||||
else if (eraseSelected || !keepOriginal)
|
||||
await commands.runCommand(
|
||||
"eraseImage",
|
||||
"Transform Tool Erase",
|
||||
{
|
||||
|
@ -767,7 +803,7 @@ const selectTransformTool = () =>
|
|||
);
|
||||
|
||||
// Selection erased or was blank, no need to draw anything
|
||||
if (eraseSelected || isBlank){
|
||||
if (eraseSelected || isBlank) {
|
||||
state.reset(true);
|
||||
return;
|
||||
}
|
||||
|
@ -777,7 +813,7 @@ const selectTransformTool = () =>
|
|||
border: 10,
|
||||
});
|
||||
|
||||
if ( (newLayer ?? state.toNewLayer) && !clearLayer)
|
||||
if ((newLayer ?? state.toNewLayer) && !clearLayer)
|
||||
await commands.runCommand("addLayer", "Added Layer");
|
||||
|
||||
let commandLog = "";
|
||||
|
@ -815,8 +851,7 @@ const selectTransformTool = () =>
|
|||
);
|
||||
|
||||
state.reset(true);
|
||||
}
|
||||
|
||||
};
|
||||
},
|
||||
populateContextMenu: (menu, state) => {
|
||||
if (!state.ctxmenu) {
|
||||
|
@ -942,17 +977,20 @@ const selectTransformTool = () =>
|
|||
const copyNewLayerButton = document.createElement("button");
|
||||
copyNewLayerButton.classList.add("button", "tool");
|
||||
copyNewLayerButton.textContent = "Layer";
|
||||
copyNewLayerButton.title = "Copies selection to a new Layer (Ctrl+Enter)";
|
||||
copyNewLayerButton.onclick = () => { state.applyTransform(false,false,true,true); };
|
||||
copyNewLayerButton.title =
|
||||
"Copies selection to a new Layer (Ctrl+Enter)";
|
||||
copyNewLayerButton.onclick = () => {
|
||||
state.applyTransform(false, false, true, true);
|
||||
};
|
||||
|
||||
// Dummy button for saving active selection
|
||||
const ActiveSelectionButton = document.createElement("button");
|
||||
ActiveSelectionButton.classList.add("button", "tool");
|
||||
ActiveSelectionButton.textContent = "📄";
|
||||
ActiveSelectionButton.title = "Commands Applied to the Current Selection";
|
||||
ActiveSelectionButton.title =
|
||||
"Commands Applied to the Current Selection";
|
||||
ActiveSelectionButton.disabled = true;
|
||||
|
||||
|
||||
actionArray.appendChild(saveSelectionButton);
|
||||
actionArray.appendChild(createResourceButton);
|
||||
actionArray.appendChild(copyNewLayerButton);
|
||||
|
@ -966,7 +1004,8 @@ const selectTransformTool = () =>
|
|||
const saveVisibleSelectionButton = document.createElement("button");
|
||||
saveVisibleSelectionButton.classList.add("button", "tool");
|
||||
saveVisibleSelectionButton.innerHTML = "Save Vis."; // nbsp as a quick hack for unwanted text wrapping
|
||||
saveVisibleSelectionButton.title = "Saves Visible Selection And Download";
|
||||
saveVisibleSelectionButton.title =
|
||||
"Saves Visible Selection And Download";
|
||||
saveVisibleSelectionButton.onclick = () => {
|
||||
console.debug(state.selected);
|
||||
console.debug(state.selected.bb);
|
||||
|
@ -1009,14 +1048,18 @@ const selectTransformTool = () =>
|
|||
const copyVisNewLayerButton = document.createElement("button");
|
||||
copyVisNewLayerButton.classList.add("button", "tool");
|
||||
copyVisNewLayerButton.textContent = "Layer";
|
||||
copyVisNewLayerButton.title = "Copies Visible Selection to a new Layer (Ctrl+Shift+Enter)";
|
||||
copyVisNewLayerButton.onclick = (e) => { state.sctentercb(e); };
|
||||
copyVisNewLayerButton.title =
|
||||
"Copies Visible Selection to a new Layer (Ctrl+Shift+Enter)";
|
||||
copyVisNewLayerButton.onclick = (e) => {
|
||||
state.sctentercb(e);
|
||||
};
|
||||
|
||||
// Dummy button for saving visible Selection
|
||||
const VisibleSelectionButton = document.createElement("button");
|
||||
VisibleSelectionButton.classList.add("button", "tool");
|
||||
VisibleSelectionButton.textContent = "👁";
|
||||
VisibleSelectionButton.title = "Commands Applied to All Visible Content In the Selected Area";
|
||||
VisibleSelectionButton.title =
|
||||
"Commands Applied to All Visible Content In the Selected Area";
|
||||
VisibleSelectionButton.disabled = true;
|
||||
|
||||
visibleActionArray.appendChild(saveVisibleSelectionButton);
|
||||
|
@ -1031,22 +1074,30 @@ const selectTransformTool = () =>
|
|||
const applyClearButton = document.createElement("button");
|
||||
applyClearButton.classList.add("button", "tool");
|
||||
applyClearButton.textContent = "Isolate";
|
||||
applyClearButton.title = "Erases everything in the current layer outside the selection (Shift+Delete)";
|
||||
applyClearButton.onclick = () => { state.applyTransform(false,true,false,false); };
|
||||
applyClearButton.title =
|
||||
"Erases everything in the current layer outside the selection (Shift+Delete)";
|
||||
applyClearButton.onclick = () => {
|
||||
state.applyTransform(false, true, false, false);
|
||||
};
|
||||
|
||||
// Erase Button
|
||||
const eraseSelectionButton = document.createElement("button");
|
||||
eraseSelectionButton.classList.add("button", "tool");
|
||||
eraseSelectionButton.textContent = "Erase";
|
||||
eraseSelectionButton.title = "Erases current selection (Delete)";
|
||||
eraseSelectionButton.onclick = () => { state.applyTransform(true,false,false,false); };
|
||||
eraseSelectionButton.onclick = () => {
|
||||
state.applyTransform(true, false, false, false);
|
||||
};
|
||||
|
||||
// Apply To New Layer button
|
||||
const applyNewLayerButton = document.createElement("button");
|
||||
applyNewLayerButton.classList.add("button", "tool");
|
||||
applyNewLayerButton.textContent = "Extract";
|
||||
applyNewLayerButton.title = "Moves Selection to a New Layer (Shift+Enter)";
|
||||
applyNewLayerButton.onclick = () => { state.applyTransform(false,false,true,false); };
|
||||
applyNewLayerButton.title =
|
||||
"Moves Selection to a New Layer (Shift+Enter)";
|
||||
applyNewLayerButton.onclick = () => {
|
||||
state.applyTransform(false, false, true, false);
|
||||
};
|
||||
|
||||
actionArrayRow3.appendChild(applyClearButton);
|
||||
actionArrayRow3.appendChild(eraseSelectionButton);
|
||||
|
|
|
@ -72,4 +72,3 @@
|
|||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
|
Loading…
Reference in a new issue