Skip to content

Commit 6d76d65

Browse files
devlandmakmentins
andauthored
Ji 5682 align cropper popup (#201)
* JI-5684 fix cropper rotation * JI-5684 update crop limit logic * JI-5684 fix scroll into view * JI-5684 remove scroll into view * JI-5682 align cropper popup * JI-5682 update cropper align to work in cors iframe * JI-5682 revert align cropper popup * JI-5682 add new alignPopup function for crop tool * JI-5682 update function description with new param --------- Co-authored-by: makmentins <maris.akmentins@h5p.group>
1 parent 5a197af commit 6d76d65

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

libs/cropper.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
}
5858
.cropper-canvas-container {
5959
position: relative;
60+
border: 1px solid #9e9e9e;
6061
}
6162
.cropper-canvas-container canvas {
6263
display: block;

libs/cropper.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,19 @@ function Cropper(options) {
294294
*
295295
* @param {Blob} blob Raw image data.
296296
*/
297-
const handleBlob = (blob) => {
298-
const url = URL.createObjectURL(blob);
299-
this.image = new Image();
300-
this.image.onload = () => {
301-
URL.revokeObjectURL(url);
302-
this.loadImage();
297+
const handleBlob = (callback) => {
298+
return (blob) => {
299+
const url = URL.createObjectURL(blob);
300+
this.image = new Image();
301+
this.image.onload = () => {
302+
URL.revokeObjectURL(url);
303+
this.loadImage();
304+
if (callback) {
305+
callback();
306+
}
307+
}
308+
this.image.src = url;
303309
}
304-
this.image.src = url;
305310
}
306311

307312
/**
@@ -332,7 +337,7 @@ function Cropper(options) {
332337
* Crops image based on selector size & position relative to image location within canvas.
333338
* The output is generated from the mirror canvas based on scaled selector dimensions.
334339
*/
335-
this.crop = () => {
340+
this.crop = (callback) => {
336341
let painted = this.fit(this.image);
337342
let selectedX = this.selector.offsetLeft - this.margins.left;
338343
let selectedY = this.selector.offsetTop - this.margins.top;
@@ -361,15 +366,15 @@ function Cropper(options) {
361366
this.mirror.width = width;
362367
this.mirror.height = height;
363368
this.mirrorContext.drawImage(this.image, selectedX, selectedY, selectedWidth, selectedHeight, 0, 0, width, height);
364-
this.mirror.toBlob(handleBlob, 'image/png', 1);
369+
this.mirror.toBlob(handleBlob(callback), 'image/png', 1);
365370
}
366371

367372
/**
368373
* Rotates image in increments of 90 degrees.
369374
*
370375
* @param {integer} rotation Clockwise rotation for positive numbers. Counterclockwise for negative numbers.
371376
*/
372-
this.rotate = (rotation) => {
377+
this.rotate = (rotation, callback) => {
373378
rotation %= 4;
374379
if (rotation % 2) {
375380
this.mirror.width = this.image.height;
@@ -383,7 +388,7 @@ function Cropper(options) {
383388
this.mirrorContext.rotate(90 * rotation * Math.PI / 180);
384389
this.mirrorContext.translate(-this.image.width / 2, -this.image.height / 2);
385390
this.mirrorContext.drawImage(this.image, 0, 0);
386-
this.mirror.toBlob(handleBlob, 'image/png', 1);
391+
this.mirror.toBlob(handleBlob(callback), 'image/png', 1);
387392
}
388393

389394
/**

scripts/h5peditor-image-popup.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ H5PEditor.ImageEditingPopup = (function ($, EventDispatcher) {
218218
*/
219219
this.resizeCropper = () => {
220220
setCropperDimensions();
221-
this.cropper.canvas.width = maxWidth;
221+
this.cropper.canvas.width = maxWidth - 2; // leave out 2px for container css border
222222
this.cropper.canvas.height = maxHeight;
223223
this.cropper.loadImage();
224224
this.cropper.loadMirror();
@@ -290,20 +290,31 @@ H5PEditor.ImageEditingPopup = (function ($, EventDispatcher) {
290290
*
291291
* @param {Object} [offset] Offset that popup should center on.
292292
* @param {string} [imageSrc] Source of image that will be edited
293+
* @param {Event} [event] Event object (button) for positioning the popup
293294
*/
294-
this.show = function (offset, imageSrc) {
295+
this.show = function (offset, imageSrc, event) {
295296
const openImageEditor = () => {
296297
H5P.$body.get(0).classList.add('h5p-editor-image-popup');
297298
background.classList.remove('hidden');
298299
self.trigger('initialized');
299300
}
301+
const alignPopup = () => {
302+
if (event) {
303+
let top = event.target.getBoundingClientRect().top + window.scrollY;
304+
if (window.innerHeight - top < popup.offsetHeight) {
305+
top = window.innerHeight - popup.offsetHeight - 58; // 48px background padding + 10px so that the popup does not touch the bottom
306+
}
307+
popup.style.top = top + 'px';
308+
}
309+
}
300310
const imageLoaded = () => {
301311
if (offset) {
302312
self.adjustPopupOffset(offset);
303313
openImageEditor();
304314
self.resizeCropper();
305315
window.addEventListener('resize', this.resizeCropper);
306316
}
317+
alignPopup();
307318
}
308319
H5P.$body.get(0).appendChild(background);
309320
background.classList.remove('hidden');
@@ -320,6 +331,7 @@ H5PEditor.ImageEditingPopup = (function ($, EventDispatcher) {
320331
}
321332
else {
322333
openImageEditor();
334+
alignPopup();
323335
}
324336
isShowing = true;
325337
};
@@ -358,9 +370,17 @@ H5PEditor.ImageEditingPopup = (function ($, EventDispatcher) {
358370
self.cropper.toggleSelector(false);
359371
});
360372
createButton('saveLabel', 'h5p-editing-image-save-button h5p-done', function () {
361-
saveImage();
362-
self.hide();
363-
self.cropper.toggleSelector(false);
373+
if (self.cropper.selector.style.display !== 'none') {
374+
self.cropper.crop(() => {
375+
self.cropper.toggleSelector(false);
376+
saveImage();
377+
self.hide();
378+
});
379+
}
380+
else {
381+
saveImage();
382+
self.hide();
383+
}
364384
});
365385
}
366386

scripts/h5peditor-image.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ ns.widgets.image.prototype.appendTo = function ($wrapper) {
150150
self.$editImage.removeClass('loading');
151151
});
152152

153-
$container.find('.h5p-editing-image-button').click(function () {
153+
$container.find('.h5p-editing-image-button').click(function (event) {
154154
if (self.params && self.params.path) {
155155
var imageSrc;
156156
if (!self.isEditing) {
@@ -160,7 +160,7 @@ ns.widgets.image.prototype.appendTo = function ($wrapper) {
160160
self.$editImage.toggleClass('loading');
161161

162162
// Add throbber to image
163-
editImagePopup.show(ns.$(this).offset(), imageSrc);
163+
editImagePopup.show(ns.$(this).offset(), imageSrc, event);
164164
}
165165
});
166166

0 commit comments

Comments
 (0)