Skip to content

Commit 81c0b8c

Browse files
committed
paste over and extend right
1 parent b2b2b4f commit 81c0b8c

5 files changed

Lines changed: 96 additions & 19 deletions

File tree

js/inserter.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,56 @@ export default class Inserter {
2121
this.main.clearBackground();
2222
this.ctx.putImageData(tmpData, 0, 0);
2323
this.main.adjustSizeFull();
24-
this.worklog.captureState();
2524
if (img.naturalWidth < oldW) {
2625
const offset = Math.round((oldW - img.naturalWidth) / 2);
2726
this.main.select.placeAt(offset, oldH, offset, 0, img);
2827
} else {
2928
this.main.select.placeAt(0, oldH, 0, 0, img);
3029
}
30+
this.worklog.captureState();
31+
},
32+
},
33+
extend_right: {
34+
handle: (img) => {
35+
this.tmpImg = img;
36+
const oldH = this.main.size.h;
37+
const oldW = this.main.size.w;
38+
const newW = oldW + img.naturalWidth;
39+
const newH = Math.max(oldH, img.naturalHeight);
40+
const tmpData = this.ctx.getImageData(0, 0, this.main.size.w, this.main.size.h);
41+
this.main.resize(newW, newH);
42+
this.main.clearBackground();
43+
this.ctx.putImageData(tmpData, 0, 0);
44+
this.main.adjustSizeFull();
45+
if (img.naturalHeight < oldH) {
46+
const offset = Math.round((oldH - img.naturalHeight) / 2);
47+
this.main.select.placeAt(oldW, offset, 0, offset, img);
48+
} else {
49+
this.main.select.placeAt(oldW, 0, 0, 0, img);
50+
}
51+
this.worklog.captureState();
52+
},
53+
},
54+
over: {
55+
handle: (img) => {
56+
this.tmpImg = img;
57+
const oldH = this.main.size.h;
58+
const oldW = this.main.size.w;
59+
if (img.naturalHeight <= oldH && img.naturalWidth <= oldW) {
60+
this.main.select.placeAt(
61+
0, 0,
62+
oldW - img.naturalWidth,
63+
oldH - img.naturalHeight, img);
64+
} else if (img.naturalWidth / img.naturalHeight > oldW / oldH) {
65+
const newH = oldW * (img.naturalHeight / img.naturalWidth);
66+
this.main.select.placeAt(0, 0, 0, oldH - newH, img);
67+
} else {
68+
const newW = oldH * (img.naturalWidth / img.naturalHeight);
69+
this.main.select.placeAt(0, 0, oldW - newW, 0, img);
70+
}
71+
this.worklog.captureState();
3172
},
3273
},
33-
extend_right: {},
34-
over: {},
3574
};
3675
}
3776

@@ -58,8 +97,8 @@ export default class Inserter {
5897
}
5998

6099
insert(x, y, w, h) {
61-
console.log("inserting ", this.tmpImg, x, y, w, h);
62100
this.main.ctx.drawImage(this.tmpImg, x, y, w, h);
101+
this.main.worklog.reCaptureState();
63102
}
64103

65104
loaded(img) {

js/main.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '../css/icons/iconfont.css';
44

55
import PainterroSelecter from './selecter';
66
import WorkLog from './worklog';
7-
import { genId, addDocumentObjectHelpers } from './utils';
7+
import { genId, addDocumentObjectHelpers, KEYS } from './utils';
88
import PrimitiveTool from './primitive';
99
import ColorPicker from './colorPicker';
1010
import setDefaults from './params';
@@ -280,13 +280,15 @@ class PainterroProc {
280280
right: true,
281281
activate: () => {
282282
this.closeActiveTool();
283-
document.getElementById('ptro-file-input').click();
284-
document.getElementById('ptro-file-input').onchange = (event) => {
283+
const input = document.getElementById('ptro-file-input');
284+
input.click();
285+
input.onchange = (event) => {
285286
const files = event.target.files || event.dataTransfer.files;
286287
if (!files.length) {
287288
return;
288289
}
289290
this.openFile(files[0]);
291+
input.value = ''; // to allow reopen
290292
};
291293
},
292294
}, {
@@ -382,12 +384,10 @@ class PainterroProc {
382384
this.primitiveTool = new PrimitiveTool(this);
383385
this.primitiveTool.setLineWidth(this.params.defaultLineWidth);
384386
this.primitiveTool.setPixelSize(this.params.defaultPixelSize);
385-
this.worklog = new WorkLog(this, {
386-
changedHandler() {
387-
if (this.saveBtn) {
388-
this.saveBtn.removeAttribute('disabled');
389-
}
390-
},
387+
this.worklog = new WorkLog(this, () => {
388+
if (this.saveBtn) {
389+
this.saveBtn.removeAttribute('disabled');
390+
}
391391
});
392392
this.inserter.init(this);
393393
this.textTool = new TextTool(this);
@@ -564,16 +564,17 @@ class PainterroProc {
564564
keydown: (e) => {
565565
if (this.shown) {
566566
const evt = window.event ? event : e;
567+
this.handleToolEvent('handleKeyDown', evt);
567568
if (
568-
(evt.keyCode === 89 && evt.ctrlKey) || // 89 is 'y' key
569-
(evt.keyCode === 90 && evt.ctrlKey && evt.shiftKey)) { // 90 is 'z' key
569+
(evt.keyCode === KEYS.y && evt.ctrlKey) ||
570+
(evt.keyCode === KEYS.z && evt.ctrlKey && evt.shiftKey)) {
570571
this.worklog.redoState();
571-
} else if (evt.keyCode === 90 && evt.ctrlKey) {
572+
} else if (evt.keyCode === KEYS.z && evt.ctrlKey) {
572573
this.worklog.undoState();
573574
}
574575

575576
if (this.saveBtn) {
576-
if (evt.keyCode === 83 && evt.ctrlKey) { // 's' key pressed
577+
if (evt.keyCode === KEYS.s && evt.ctrlKey) {
577578
this.save();
578579
evt.preventDefault();
579580
}

js/selecter.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { clearSelection } from './utils';
1+
import { clearSelection, KEYS } from './utils';
22

33
export default class PainterroSelecter {
44
constructor(main, selectionCallback) {
@@ -148,6 +148,23 @@ export default class PainterroSelecter {
148148
this.area.bottoml[1] - this.area.topl[1]);
149149
}
150150

151+
cancelPlacing() {
152+
this.imagePlaced = false;
153+
this.main.select.area.rect.style['background-image'] = 'none';
154+
this.hide();
155+
this.main.worklog.undoState();
156+
}
157+
158+
handleKeyDown(evt) {
159+
if (this.shown && this.imagePlaced) {
160+
if (evt.keyCode === KEYS.enter) {
161+
this.finishPlacing();
162+
} else if (evt.keyCode === KEYS.esc) {
163+
this.cancelPlacing();
164+
}
165+
}
166+
}
167+
151168
handleMouseDown(event) {
152169
const mainClass = event.target.classList[0];
153170
const mousDownCallbacks = {
@@ -386,6 +403,9 @@ export default class PainterroSelecter {
386403
}
387404

388405
close() {
406+
if (this.imagePlaced) {
407+
this.finishPlacing();
408+
}
389409
this.area.activated = false;
390410
this.hide();
391411
}

js/utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,12 @@ export function distance(p1, p2) {
7676
export function trim() {
7777
return String(this).replace(/^\s+|\s+$/g, '');
7878
}
79+
80+
export const KEYS = {
81+
y: 89,
82+
z: 90,
83+
s: 83,
84+
enter: 13,
85+
esc: 27,
86+
};
87+

js/worklog.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export default class WorkLog {
2-
constructor(main, { changedHandler } = {}) {
2+
constructor(main, changedHandler) {
33
this.main = main;
44
this.current = null;
55
this.changedHandler = changedHandler;
@@ -30,12 +30,20 @@ export default class WorkLog {
3030
}
3131
}
3232

33+
reCaptureState() {
34+
if (this.current.prev !== null) {
35+
this.current = this.current.prev;
36+
}
37+
this.captureState();
38+
}
39+
3340
applyState(state) {
3441
const img = new Image();
3542
img.onload = () => {
3643
this.main.resize(state.sizew, state.sizeh);
3744
this.main.ctx.drawImage(img, 0, 0);
3845
this.main.adjustSizeFull();
46+
this.main.select.hide();
3947
};
4048
img.src = state.data;
4149
}

0 commit comments

Comments
 (0)