/** * CropRect.js * * Released under LGPL License. * Copyright (c) 1999-2017 Ephox Corp. All rights reserved * * License: http://www.tinymce.com/license * Contributing: http://www.tinymce.com/contributing */ import DomQuery from 'tinymce/core/api/dom/DomQuery'; import Rect from 'tinymce/core/api/geom/Rect'; import Factory from 'tinymce/core/api/ui/Factory'; import Observable from 'tinymce/core/api/util/Observable'; import Tools from 'tinymce/core/api/util/Tools'; import VK from 'tinymce/core/api/util/VK'; let count = 0; export default function (currentRect, viewPortRect, clampRect, containerElm, action) { let instance; let handles; let dragHelpers; let blockers; const prefix = 'mce-'; const id = prefix + 'crid-' + count++; handles = [ { name: 'move', xMul: 0, yMul: 0, deltaX: 1, deltaY: 1, deltaW: 0, deltaH: 0, label: 'Crop Mask' }, { name: 'nw', xMul: 0, yMul: 0, deltaX: 1, deltaY: 1, deltaW: -1, deltaH: -1, label: 'Top Left Crop Handle' }, { name: 'ne', xMul: 1, yMul: 0, deltaX: 0, deltaY: 1, deltaW: 1, deltaH: -1, label: 'Top Right Crop Handle' }, { name: 'sw', xMul: 0, yMul: 1, deltaX: 1, deltaY: 0, deltaW: -1, deltaH: 1, label: 'Bottom Left Crop Handle' }, { name: 'se', xMul: 1, yMul: 1, deltaX: 0, deltaY: 0, deltaW: 1, deltaH: 1, label: 'Bottom Right Crop Handle' } ]; blockers = ['top', 'right', 'bottom', 'left']; function getAbsoluteRect(outerRect, relativeRect) { return { x: relativeRect.x + outerRect.x, y: relativeRect.y + outerRect.y, w: relativeRect.w, h: relativeRect.h }; } function getRelativeRect(outerRect, innerRect) { return { x: innerRect.x - outerRect.x, y: innerRect.y - outerRect.y, w: innerRect.w, h: innerRect.h }; } function getInnerRect() { return getRelativeRect(clampRect, currentRect); } function moveRect(handle, startRect, deltaX, deltaY) { let x, y, w, h, rect; x = startRect.x; y = startRect.y; w = startRect.w; h = startRect.h; x += deltaX * handle.deltaX; y += deltaY * handle.deltaY; w += deltaX * handle.deltaW; h += deltaY * handle.deltaH; if (w < 20) { w = 20; } if (h < 20) { h = 20; } rect = currentRect = Rect.clamp({ x, y, w, h }, clampRect, handle.name === 'move'); rect = getRelativeRect(clampRect, rect); instance.fire('updateRect', { rect }); setInnerRect(rect); } function render() { function createDragHelper(handle) { let startRect; const DragHelper = Factory.get('DragHelper'); return new DragHelper(id, { document: containerElm.ownerDocument, handle: id + '-' + handle.name, start () { startRect = currentRect; }, drag (e) { moveRect(handle, startRect, e.deltaX, e.deltaY); } }); } DomQuery( '
' ).appendTo(containerElm); Tools.each(blockers, function (blocker) { DomQuery('#' + id, containerElm).append( '