sp-image-editor
Version:
React component version of filerobot image editor (FIE).
1 lines • 5.06 kB
JavaScript
import _objectWithoutProperties from"@babel/runtime/helpers/objectWithoutProperties";import _defineProperty from"@babel/runtime/helpers/defineProperty";var _excluded=["id","x","y","points"],_excluded2=["startedX","startedY","offsetX","offsetY","width","height"];function ownKeys(a,b){var c=Object.keys(a);if(Object.getOwnPropertySymbols){var d=Object.getOwnPropertySymbols(a);b&&(d=d.filter(function(b){return Object.getOwnPropertyDescriptor(a,b).enumerable})),c.push.apply(c,d)}return c}function _objectSpread(a){for(var b,c=1;c<arguments.length;c++)b=null==arguments[c]?{}:arguments[c],c%2?ownKeys(Object(b),!0).forEach(function(c){_defineProperty(a,c,b[c])}):Object.getOwnPropertyDescriptors?Object.defineProperties(a,Object.getOwnPropertyDescriptors(b)):ownKeys(Object(b)).forEach(function(c){Object.defineProperty(a,c,Object.getOwnPropertyDescriptor(b,c))});return a}import{TOOLS_IDS}from"../../utils/constants";import getElemDocumentCoords from"../../utils/getElemDocumentCoords";import getPointerOffsetPositionBoundedToObject from"../../utils/getPointerOffsetPositionBoundedToObject";import getBoundingRectUnScaled from"./getBoundingRectUnScaled";import getNewAnnotationPreview,{dimensToProperAnnotationDimens,NO_WIDTH_HEIGHT_ANNOTATIONS}from"./getNewAnnotationPreview";var pointerDown={startedX:void 0,startedY:void 0,isOutOfCanvas:!1},eventsOptions={passive:!0},MIN_PIXELS=1,shownAnnotationPreview=null,textAnnotationWrappedRect=null,latestAnnotationProps=null,previewThenCallAnnotationAdding=function(a,b,c,d){var f=function b(){return getElemDocumentCoords(a.content)},g=function b(a){textAnnotationWrappedRect=getNewAnnotationPreview(_objectSpread(_objectSpread({},a),{},{name:TOOLS_IDS.RECT,fill:"",stroke:"#000000",strokeWidth:2,shadowColor:"#ffffff",shadowBlur:1,shadowOpacity:.7})),c.add(textAnnotationWrappedRect)},h=function b(a){shownAnnotationPreview=getNewAnnotationPreview(a),c.add(shownAnnotationPreview),a.name===TOOLS_IDS.TEXT&&g(a),latestAnnotationProps=a},i=function c(a,b){if(latestAnnotationProps){var d=dimensToProperAnnotationDimens(a,latestAnnotationProps.name,b);textAnnotationWrappedRect&&textAnnotationWrappedRect.setAttrs(d),shownAnnotationPreview.setAttrs(d),latestAnnotationProps=_objectSpread(_objectSpread({},latestAnnotationProps),d)}},j=function b(a){var d=getPointerOffsetPositionBoundedToObject(c,f());i(getBoundingRectUnScaled(d,pointerDown,c),a.shiftKey)},k=function a(){c&&shownAnnotationPreview&&c.destroyChildren()},l=function e(d){var g;if(!(1<(null===(g=d.evt.touches)||void 0===g?void 0:g.length))){var k=getPointerOffsetPositionBoundedToObject(c,f());pointerDown.isOutOfCanvas&&(document.removeEventListener("mousemove",j,eventsOptions),document.removeEventListener("touchmove",j,eventsOptions),pointerDown.isOutOfCanvas=!1);var l=getBoundingRectUnScaled(k,pointerDown,c);if(shownAnnotationPreview)i(l,d.evt.shiftKey);else{var m=b.id,n=b.x,o=b.y,p=b.points,q=_objectWithoutProperties(b,_excluded);h(_objectSpread(_objectSpread({},q),l))}a.setAttrs({isDrawing:!0})}},m=function a(){pointerDown.isOutOfCanvas||(document.addEventListener("mousemove",j,eventsOptions),document.addEventListener("touchmove",j,eventsOptions),pointerDown.isOutOfCanvas=!0)},n=function c(){var e,f;if(k(),latestAnnotationProps&&(latestAnnotationProps.width>=MIN_PIXELS&&latestAnnotationProps.height>=MIN_PIXELS||latestAnnotationProps.radiusX>=MIN_PIXELS&&latestAnnotationProps.radiusY>=MIN_PIXELS||null!==(e=latestAnnotationProps.points)&&void 0!==e&&e[2]||null!==(f=latestAnnotationProps.points)&&void 0!==f&&f[3]||latestAnnotationProps.radius>=MIN_PIXELS)){var g=latestAnnotationProps,h=g.startedX,i=g.startedY,o=g.offsetX,p=g.offsetY,q=g.width,r=g.height,s=_objectWithoutProperties(g,_excluded2);NO_WIDTH_HEIGHT_ANNOTATIONS.includes(b.name)||(s.width=q,s.height=r),d(s,!0)}shownAnnotationPreview=null,textAnnotationWrappedRect=null,latestAnnotationProps=null,a.setAttrs({isDrawing:!1}),a.off("mousemove touchmove",l),a.off("mouseleave touchcancel",m),document.removeEventListener("mouseup",n,eventsOptions),document.removeEventListener("touchend",n,eventsOptions),document.removeEventListener("mouseleave",n,eventsOptions),document.removeEventListener("touchcancel",n,eventsOptions),document.removeEventListener("mousemove",j,eventsOptions),document.removeEventListener("touchmove",j,eventsOptions),pointerDown.isOutOfCanvas=!1},o=function d(b){var e;if(b.evt.preventDefault(),!(b.target.attrs.draggable||1<(null===(e=b.evt.touches)||void 0===e?void 0:e.length))){k();var g=getPointerOffsetPositionBoundedToObject(c,f());pointerDown.startedX=g.offsetX,pointerDown.startedY=g.offsetY,pointerDown.isOutOfCanvas=!1,a.on("mousemove touchmove",l),a.on("mouseleave touchcancel",m),document.addEventListener("mouseup",n,eventsOptions),document.addEventListener("touchend",n,eventsOptions),document.addEventListener("mouseleave",n,eventsOptions),document.addEventListener("touchcancel",n,eventsOptions)}};return a.on("mousedown touchstart",o),function(){k(),a.off("mousedown touchstart",o)}};export default previewThenCallAnnotationAdding;