Files
2024-12-17 17:34:10 +01:00

5 lines
2.1 KiB
JavaScript

import p from"cropper-module";/**
* @copyright (C) 2018 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/let t,n=!1,a;const d=()=>{t.cropX.addEventListener("change",({currentTarget:e})=>{a.setData({x:parseInt(e.value,10)})}),t.cropY.addEventListener("change",({currentTarget:e})=>{a.setData({y:parseInt(e.value,10)})}),t.cropWidth.addEventListener("change",({currentTarget:e})=>{a.setData({width:parseInt(e.value,10)})}),t.cropHeight.addEventListener("change",({currentTarget:e})=>{a.setData({height:parseInt(e.value,10)})}),t.aspectRatio.addEventListener("change",({currentTarget:e})=>{a.setAspectRatio(e.value)}),n=!0},s=e=>{const o=e.naturalWidth/e.naturalHeight;!Number.isNaN(o)&&Number.isFinite(o)&&(t.cropAspectRatioOption.value=o),a=new p(e,{viewMode:1,responsive:!0,restore:!0,autoCrop:!0,movable:!1,zoomable:!1,rotatable:!1,autoCropArea:1,crop(r){t.cropX.value=Math.round(r.detail.x),t.cropY.value=Math.round(r.detail.y),t.cropWidth.value=Math.round(r.detail.width),t.cropHeight.value=Math.round(r.detail.height);const i=Joomla.MediaManager.Edit.original.extension.toLowerCase()==="jpg"?"jpeg":Joomla.MediaManager.Edit.original.extension.toLowerCase(),c=t.cropQuality.value;Joomla.MediaManager.Edit.current.contents=this.cropper.getCroppedCanvas().toDataURL(`image/${i}`,c),window.dispatchEvent(new Event("mediaManager.history.point"))}}),n||d(),a.setAspectRatio(t.cropAspectRatioOption.value)};window.addEventListener("media-manager-edit-init",()=>{t={aspectRatio:document.getElementById("jform_aspectRatio"),cropHeight:document.getElementById("jform_crop_height"),cropWidth:document.getElementById("jform_crop_width"),cropY:document.getElementById("jform_crop_y"),cropX:document.getElementById("jform_crop_x"),cropQuality:document.getElementById("jform_crop_quality"),cropAspectRatioOption:document.querySelector(".crop-aspect-ratio-option")},Joomla.MediaManager.Edit.plugins.crop={Activate(e){return new Promise(o=>{s(e),o()})},Deactivate(e){return new Promise(o=>{e.cropper&&(e.cropper.destroy(),a=null),o()})}}},{once:!0});