Skip to content

Commit 0b20285

Browse files
committed
controlByName, save API
1 parent 260c27d commit 0b20285

4 files changed

Lines changed: 75 additions & 35 deletions

File tree

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ Painterro({
141141
|`dragOverBarColor`| Color of bar when dropping file to painterro | '#899dff' |
142142

143143

144+
Methods
145+
-------
146+
147+
148+
**.show(openImage)** - Show painterro instance. `openImage` can have next values:
149+
* `false` - will open image that already was drawn before last close
150+
* `some string value`, e.g. `'http://placehold.it/120x120&text=image1'` - will try to load image from url
151+
* all another values - will clear content before open
152+
153+
**.hide()** - hide instance
154+
155+
**.save()** - call save. Can be used if save button is hidden (`hiddenTools: ['save']`)
156+
144157
Translation
145158
-----------
146159

build/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
// });
4646

4747
Painterro({
48-
49-
}).show();
48+
// hiddenTools: ['save']
49+
}).show().save();
5050
</script>
5151
</body>
5252
</html>

js/main.js

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -295,20 +295,7 @@ class PainterroProc {
295295
name: 'save',
296296
right: true,
297297
activate: () => {
298-
const icon = document.querySelector(`#${this.activeTool.buttonId} > i`);
299-
icon.className = 'ptro-icon ptro-icon-loading ptro-spinning';
300-
301-
if (this.params.saveHandler !== undefined) {
302-
this.params.saveHandler(this.imageSaver, (hide) => {
303-
if (hide === true) {
304-
this.hide()
305-
}
306-
icon.className = 'ptro-icon ptro-icon-save';
307-
})
308-
} else {
309-
console.error("No saveHandler defined, please check documentation")
310-
icon.className = 'ptro-icon ptro-icon-save';
311-
}
298+
this.save();
312299
this.closeActiveTool();
313300
},
314301
}, {
@@ -334,6 +321,10 @@ class PainterroProc {
334321
},
335322
},];
336323

324+
this.toolByName = {};
325+
for (let t of this.tools) {
326+
this.toolByName[t.name] = t;
327+
}
337328
this.activeTool = undefined;
338329
this.zoom = false;
339330
this.ratioRelation = undefined;
@@ -385,11 +376,13 @@ class PainterroProc {
385376
'</div>' +
386377
`<style>${this.params.styles}</style>`;
387378

388-
this.saveBtn = document.getElementById(this.tools.filter((t) => t.name === 'save')[0].buttonId);
389-
this.saveBtn.setAttribute('disabled', true);
390-
this.changedHandler = () => {
391-
this.saveBtn.removeAttribute('disabled');
392-
};
379+
this.saveBtn = document.getElementById(this.toolByName.save.buttonId);
380+
if (this.saveBtn) {
381+
this.saveBtn.setAttribute('disabled', true);
382+
this.changedHandler = () => {
383+
this.saveBtn.removeAttribute('disabled');
384+
};
385+
}
393386

394387
this.body = document.body;
395388
this.wrapper = document.querySelector(`#${this.id} .ptro-wrapper`);
@@ -402,9 +395,9 @@ class PainterroProc {
402395
this.zoomHelper = new ZoomHelper(this);
403396
this.cropper = new PainterroCropper(this, (notEmpty) => {
404397
if (notEmpty) {
405-
document.getElementById(this.tools[0].controls[0].id).removeAttribute('disabled');
398+
document.getElementById(this.toolByName.crop.controls[0].id).removeAttribute('disabled');
406399
} else {
407-
document.getElementById(this.tools[0].controls[0].id).setAttribute('disabled', 'true');
400+
document.getElementById(this.toolByName.crop.controls[0].id).setAttribute('disabled', 'true');
408401
}
409402
});
410403
this.resizer = new Resizer(this);
@@ -507,7 +500,29 @@ class PainterroProc {
507500
this.hide();
508501
}
509502

510-
closeActiveTool() {
503+
save () {
504+
const btn = document.getElementById(this.toolByName.save.buttonId);
505+
const icon = document.querySelector(`#${this.toolByName.save.buttonId} > i`);
506+
btn && (btn.setAttribute('disabled', 'true'));
507+
icon && (icon.className = 'ptro-icon ptro-icon-loading ptro-spinning')
508+
509+
if (this.params.saveHandler !== undefined) {
510+
this.params.saveHandler(this.imageSaver, (hide) => {
511+
if (hide === true) {
512+
this.hide()
513+
}
514+
icon && (icon.className = 'ptro-icon ptro-icon-save');
515+
//btn && (btn.removeAttribute('disabled'));
516+
})
517+
} else {
518+
console.error("No saveHandler defined, please check documentation")
519+
icon && (icon.className = 'ptro-icon ptro-icon-save');
520+
//btn && (btn.removeAttribute('disabled'))
521+
}
522+
return this;
523+
}
524+
525+
closeActiveTool () {
511526
if (this.activeTool !== undefined) {
512527
if (this.activeTool.close !== undefined) {
513528
this.activeTool.close();
@@ -620,15 +635,31 @@ class PainterroProc {
620635
}
621636
}
622637

623-
show (clear) {
638+
loadImage (source) {
639+
const img = new Image;
640+
img.onload = () => {
641+
this.resize(img.naturalWidth, img.naturalHeight);
642+
this.ctx.drawImage(img, 0, 0);
643+
this.adjustSizeFull();
644+
this.worklog.captureState();
645+
};
646+
img.src = source;
647+
}
648+
649+
show (openImage) {
624650
this.shown = true;
625651
this.baseEl.removeAttribute('hidden');
626652
if (this.holderEl) {
627653
this.holderEl.removeAttribute('hidden');
628654
}
629-
if (clear !== false) {
630-
this.clear();
655+
if (typeof openImage === 'string') {
656+
this.loadImage(openImage)
657+
} else {
658+
if (openImage !== false) {
659+
this.clear();
660+
}
631661
}
662+
return this;
632663
}
633664

634665
hide () {
@@ -637,17 +668,11 @@ class PainterroProc {
637668
if (this.holderEl) {
638669
this.holderEl.setAttribute('hidden', true);
639670
}
671+
return this;
640672
}
641673

642674
openFile(f) {
643-
const img = new Image;
644-
img.onload = () => {
645-
this.resize(img.naturalWidth, img.naturalHeight);
646-
this.ctx.drawImage(img, 0, 0);
647-
this.adjustSizeFull();
648-
this.worklog.captureState();
649-
};
650-
img.src = URL.createObjectURL(f);
675+
this.loadImage(URL.createObjectURL(f));
651676
}
652677

653678
getScale() {

js/worklog.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ export class WorkLog {
3838
if (this.current.prev !== null) {
3939
this.current = this.current.prev;
4040
this.applyState(this.current);
41+
this.main.changedHandler();
4142
}
4243
}
4344

4445
redoState() {
4546
if (this.current.next !== null) {
4647
this.current = this.current.next;
4748
this.applyState(this.current);
49+
this.main.changedHandler();
4850
}
4951
}
5052
}

0 commit comments

Comments
 (0)