@berviantoleo/react-multi-crop 0.31.0 → 0.31.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.31.2](https://github.com/berviantoleo/react-multi-crop/compare/@berviantoleo/react-multi-crop@0.31.1...@berviantoleo/react-multi-crop@0.31.2) (2023-05-04)
7
+
8
+ ### Bug Fixes
9
+
10
+ - update dependencies ([#1805](https://github.com/berviantoleo/react-multi-crop/issues/1805)) ([0255b07](https://github.com/berviantoleo/react-multi-crop/commit/0255b07afd4f2ccb06df16cb0e969407b189f2ac))
11
+
12
+ ## [0.31.1](https://github.com/berviantoleo/react-multi-crop/compare/@berviantoleo/react-multi-crop@0.31.0...@berviantoleo/react-multi-crop@0.31.1) (2023-05-04)
13
+
14
+ ### Bug Fixes
15
+
16
+ - color not applied for initial object ([#1803](https://github.com/berviantoleo/react-multi-crop/issues/1803)) ([8a0041d](https://github.com/berviantoleo/react-multi-crop/commit/8a0041dd3d2500c65b99d60db21429ea33590b70))
17
+
6
18
  # [0.31.0](https://github.com/berviantoleo/react-multi-crop/compare/@berviantoleo/react-multi-crop@0.30.0...@berviantoleo/react-multi-crop@0.31.0) (2023-04-28)
7
19
 
8
20
  ### Features
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";var t=require("react"),e=require("fabric"),o=require("uuid");function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}var i=function(t,e){return i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},i(t,e)};function n(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var a=function(t){function e(e){var o=t.call(this,e)||this;return o.id=null,o.objectId="",e&&(o.id=e.id,o.objectId=e.objectId),o}return n(e,t),e}(e.fabric.Rect);function c(e){var o=e.children,r=e.height,i=e.width,n=e.row,a=void 0!==n&&n;return t.createElement("div",{style:{display:"flex",flexDirection:a?"row":"column",gap:"10px",height:r,width:i}},o)}function s(e){var o=e.addButton,r=e.addNew,i=e.deleteButton,n=e.deleteShapes,a=e.discardActiveObject,s=e.discardButton;return t.createElement(c,null,t.createElement("div",null,o&&t.cloneElement(o,{onClick:r})),t.createElement("div",null,i&&t.cloneElement(i,{onClick:n})),t.createElement("div",null,s&&t.cloneElement(s,{onClick:a})))}var l=function(i){function l(t){var e=i.call(this,t)||this;return e.REGEXP_ORIGINS=/^(\w+:)\/\/([^:/?#]*):?(\d*)/i,e.state={canvas:null,initial:!0},e.keyboardHandler=e.keyboardHandler.bind(e),e.addNew=e.addNew.bind(e),e.deleteShapes=e.deleteShapes.bind(e),e.discardActiveObject=e.discardActiveObject.bind(e),e}return n(l,i),l.prototype.componentDidMount=function(){this.state.canvas||this.initialCanvas()},l.prototype.componentDidUpdate=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.zoomLevel,i=o.activeObject,n=o.height,a=o.width,c=o.borderColor,s=o.cornerColor,l=o.cornerSize,d=o.transparentCorners,p=o.cropBackgroundColor,h=o.cropBackgroundOpacity,u=o.disableZoom,v=void 0!==u&&u,g=t.zoomLevel,f=t.activeObject,b=t.height,y=t.width,C=t.borderColor,m=t.cornerColor,w=t.cornerSize,O=t.transparentCorners,k=t.cropBackgroundColor,S=t.cropBackgroundOpacity,B=!1;if(g!==r&&r&&r>0&&(e.setZoom(r),B=!0),f!==i&&i){var j=e.getObjects().filter((function(t){return t.objectId===i}));e.discardActiveObject();for(var A=0,I=j;A<I.length;A++){var E=I[A];e.setActiveObject(E)}B=!0}if(e.off("mouse:wheel"),!v){var z=this.zoom.bind(this);e.on("mouse:wheel",z)}if(n&&n!==b&&(e.setHeight(n),B=!0),a&&a!==y&&(e.setWidth(a),B=!0),c!==C||s!==m||l!==w||d!==O||p!==k||h!==S){var H={borderColor:c,cornerColor:s,cornerSize:l,transparentCorners:d,cropBackgroundColor:p,cropBackgroundOpacity:h};this.updateCropAttributes(e,H),B=!0}B&&e.requestRenderAll()}},l.prototype.updateCropAttributes=function(t,e){var o=t.getObjects("rect");Array.isArray(o)&&o.length>0&&o.forEach((function(t){t.borderColor=e.borderColor,t.cornerColor=e.cornerColor,t.cornerSize=e.cornerSize,t.transparentCorners=e.transparentCorners,t.opacity=e.cropBackgroundOpacity,t.set("fill",e.cropBackgroundColor)}))},l.prototype.changeImage=function(){var t=this.state.canvas;t&&(t.backgroundImage||this.initialImage())},l.prototype.loadImage=function(t){var e=this.state,o=e.initial,r=e.canvas;if(r&&r.width&&r.height&&t.height&&t.width){var i=this.props.zoomLevel,n=t.height/t.width,a=r.width*n;r.setHeight(a),i?r.setZoom(i):r.setZoom(r.width/t.width),r.setBackgroundImage(t,r.renderAll.bind(r)),o&&this.setState({initial:!1},this.initialObjects.bind(this))}},l.prototype.isCrossOriginURL=function(t){var e=t.match(this.REGEXP_ORIGINS);return null!==e&&(e[1]!==window.location.protocol||e[2]!==window.location.hostname||e[3]!==window.location.port)},l.prototype.initialImage=function(){var t=this.props.image,o=this.loadImage.bind(this);if("string"==typeof t){var r={};this.isCrossOriginURL(t)&&(r.crossOrigin="Anonymous"),e.fabric.Image.fromURL(t,o,r)}},l.prototype.initialObjects=function(){var t=this.state.canvas;if(t){var e=this.props,o=e.record,i=e.readonly,n=e.borderColor,a=e.cornerColor,c=e.cornerSize,s=e.transparentCorners;if(o){var l=o.clippings;if(Array.isArray(l)&&l.length>0&&"object"===r(l[0])){for(var d={borderColor:n,cornerColor:a,cornerSize:c,transparentCorners:s},p=0,h=0,u=l;h<u.length;h++){var v=u[h],g=this.createObject(t,v,d,i||!1);g&&(t.add(g),p+=1)}p>0&&this.setOutput()}}else console.log("Not have any record. Skipped.")}},l.prototype.zoom=function(t){var e=this.state.canvas;if(e){var o=t.e.deltaY,r=e.getZoom();(r*=Math.pow(.999,o))>20&&(r=20),r<.01&&(r=.01),e.setZoom(r),t.e.preventDefault(),t.e.stopPropagation();var i=this.props.zoomChanged;i&&i(r)}},l.prototype.mouseHover=function(t){var e=this.props.onHover,o=this.shapetoStructureData.bind(this),r=t.target;r&&"rect"===r.type&&e&&e(o(r))},l.prototype.mouseOut=function(t){var e=this.props.onHover,o=t.target;o&&"rect"===o.type&&e&&e(null)},l.prototype.selectionHandler=function(t){var e=this.props.onSelect,o=this.shapetoStructureData.bind(this),r=t.selected;if(Array.isArray(r)&&0!=r.length){var i=r[0];if(i&&"rect"===i.type&&e)e(o(i))}},l.prototype.selectionClearHandler=function(){var t=this.props.onSelect;t&&t(null)},l.prototype.initialCanvas=function(){var t=this.props,o=t.id,r=t.width,i=t.height,n=t.readonly,a=new e.fabric.Canvas(o||"canvas",{width:r,height:i});if(n){a.selectionKey=void 0;var c=this.mouseHover.bind(this),s=this.mouseOut.bind(this);a.on("mouse:over",c),a.on("mouse:out",s)}else{var l=this.doubleClickEvent.bind(this),d=this.setOutput.bind(this);a.on("mouse:dblclick",l),a.on("object:modified",d)}var p=this.selectionHandler.bind(this),h=this.selectionClearHandler.bind(this);a.on("selection:created",p),a.on("selection:updated",p),a.on("selection:cleared",h),a.on("mouse:down",(function(t){var e=t.e;!0===e.altKey&&(this.isDragging=!0,this.selection=!1,this.lastPosX=e.clientX,this.lastPosY=e.clientY)})),a.on("mouse:move",(function(t){if(this.isDragging){var e=t.e,o=this.viewportTransform;o[4]+=e.clientX-this.lastPosX,o[5]+=e.clientY-this.lastPosY,this.requestRenderAll(),this.lastPosX=e.clientX,this.lastPosY=e.clientY}})),a.on("mouse:up",(function(){this.setViewportTransform(this.viewportTransform),this.isDragging=!1,this.selection=!0})),this.setState({canvas:a},this.initialImage.bind(this))},l.prototype.addNew=function(){var t=this.state.canvas;if(t){var e=this.props,o={borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,cropBackgroundColor:e.cropBackgroundColor,cropBackgroundOpacity:e.cropBackgroundOpacity},r=this.createObject(t,{id:null,rect:{x1:0,y1:0,x2:.2,y2:.2}},o,!1);r&&(t.add(r),this.setOutput())}},l.prototype.doubleClickEvent=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.readonly,i=o.borderColor,n=o.cornerColor,a=o.cornerSize,c=o.transparentCorners,s=o.cropBackgroundColor,l=o.cropBackgroundOpacity;if(t&&t.target){var d=t.target.left,p=t.target.top,h=t.target.width,u=t.target.height,v={left:d+50,top:p+50,width:h*t.target.scaleX,height:u*t.target.scaleY,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}else if(t&&t.pointer){v={left:d=t.absolutePointer.x,top:t.absolutePointer.y,width:100,height:100,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}}},l.prototype.createObjectByAttribute=function(t,e,r){return new a({left:e.left,top:e.top,width:e.width,height:e.height,borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,fill:e.cropBackgroundColor,opacity:e.cropBackgroundOpacity,id:t,strokeWidth:0,strokeUniform:!0,lockRotation:!0,lockMovementX:r,lockMovementY:r,lockScalingX:r,lockScalingY:r,objectId:o.v4()})},l.prototype.convertLeftTop=function(t){var e,o,r,i;return void 0===t.left||void 0===t.top||void 0===(null===(e=t.group)||void 0===e?void 0:e.left)||void 0===(null===(o=t.group)||void 0===o?void 0:o.top)||void 0===(null===(r=t.group)||void 0===r?void 0:r.width)||void 0===(null===(i=t.group)||void 0===i?void 0:i.height)?{left:0,top:0}:{left:t.left+t.group.left+t.group.width/2,top:t.top+t.group.top+t.group.height/2}},l.prototype.shapetoStructureData=function(t){var o=this.state.canvas;if(!o||!o.backgroundImage)return null;var r=o.backgroundImage;if(!(r instanceof e.fabric.Image))return null;if(void 0===t.left||void 0===t.top||void 0===t.width||void 0===t.height||void 0===t.scaleX||void 0===t.scaleY||void 0===r.width||void 0===r.height)return null;var i=this.props,n=i.includeDataUrl,a=i.includeHtmlCanvas,c=t.group?this.convertLeftTop(t):{left:t.left,top:t.top},s=c.left/r.width,l=c.top/r.height,d=(c.left+t.width*t.scaleX)/r.width,p=(c.top+t.height*t.scaleY)/r.height,h={x1:s,y1:l,x2:d,y2:p},u={id:t.id,objectId:t.objectId,rect:JSON.stringify(h)};if(n){var v=null;try{v=r.toDataURL({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top,format:"jpeg"})}catch(t){console.error(t)}u.dataUrl=v}if(a){var g=null;try{g=r.toCanvasElement({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top})}catch(t){console.error(t)}u.canvasElement=g}var f=r.width,b=r.height,y=s*f,C=d*f,m=l*b,w=p*b,O={x:y,y:m,x2:C,y2:w,w:C-y,h:w-m,boundX:f,boundY:b};return u.crop=JSON.stringify(O),u.deletedAt="-1",u},l.prototype.deleteShapes=function(){var t=this.props.readonly,e=this.state.canvas;e&&!t&&(e.getActiveObjects().forEach((function(t){e.remove(t)})),this.setOutput())},l.prototype.setOutput=function(){var t=this.state.canvas;if(t){var e=this.shapetoStructureData.bind(this),o=[];t.getObjects("rect").forEach((function(t){var r=e(t);r&&o.push(r)}));var r=this.props.input;r&&r.onChange(o)}},l.prototype.createObject=function(t,o,r,i){if(!t||!t.backgroundImage)return null;var n,a=t.backgroundImage;if(!(a instanceof e.fabric.Image))return null;if(!a.width||!a.height)return null;n="string"==typeof o.rect?JSON.parse(o.rect):o.rect;var c=a.width*n.x1,s=a.height*n.y1,l={left:c,top:s,width:a.width*n.x2-c,height:a.height*n.y2-s,borderColor:r.borderColor,cornerColor:r.cornerColor,cornerSize:r.cornerSize,transparentCorners:r.transparentCorners,cropBackgroundColor:r.cropBackgroundColor,cropBackgroundOpacity:r.cropBackgroundOpacity};return this.createObjectByAttribute(o.id,l,i)},l.prototype.discardActiveObject=function(){var t=this.state.canvas;t?(t.discardActiveObject(),t.requestRenderAll()):console.log("Canvas not defined")},l.prototype.keyboardHandler=function(t){if(!t.defaultPrevented){var e=!1,o=t.key||t.keyCode;"Delete"!==o&&46!==o||(this.deleteShapes(),e=!0),e&&t.preventDefault()}},l.prototype.render=function(){var e=this.props,o=e.addButton,r=e.deleteButton,i=e.discardButton,n=e.height,a=e.id,l=e.readonly,d=e.showButton,p=e.style,h=e.width;return t.createElement("div",{id:"canvas-wrapper",style:p},t.createElement(c,{row:!0,width:h,height:n},t.createElement("div",{onKeyDown:l?void 0:this.keyboardHandler,tabIndex:0},t.createElement("canvas",{id:a,height:n,style:{border:"0px solid #aaa"},width:h})),d&&!l&&t.createElement(s,{addButton:o,addNew:this.addNew,deleteButton:r,deleteShapes:this.deleteShapes,discardActiveObject:this.discardActiveObject,discardButton:i})))},l.defaultProps={borderColor:"black",cornerColor:"black",cornerSize:13,cropBackgroundColor:"yellow",cropBackgroundOpacity:.5,disableZoom:!1,height:800,id:"canvas",image:void 0,includeDataUrl:!1,includeHtmlCanvas:!1,input:void 0,readonly:!1,record:{clippings:[]},showButton:!1,transparentCorners:!0,width:800,zoomChanged:void 0},l}(t.Component);exports.CustomFabricRect=a,exports.ReactMultiCrop=l;
1
+ "use strict";var t=require("react"),e=require("fabric"),o=require("uuid");function r(t){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},r(t)}var i=function(t,e){return i=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},i(t,e)};function n(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}i(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var a=function(t){function e(e){var o=t.call(this,e)||this;return o.id=null,o.objectId="",e&&(o.id=e.id,o.objectId=e.objectId),o}return n(e,t),e}(e.fabric.Rect);function c(e){var o=e.children,r=e.height,i=e.width,n=e.row,a=void 0!==n&&n;return t.createElement("div",{style:{display:"flex",flexDirection:a?"row":"column",gap:"10px",height:r,width:i}},o)}function s(e){var o=e.addButton,r=e.addNew,i=e.deleteButton,n=e.deleteShapes,a=e.discardActiveObject,s=e.discardButton;return t.createElement(c,null,t.createElement("div",null,o&&t.cloneElement(o,{onClick:r})),t.createElement("div",null,i&&t.cloneElement(i,{onClick:n})),t.createElement("div",null,s&&t.cloneElement(s,{onClick:a})))}var l=function(i){function l(t){var e=i.call(this,t)||this;return e.REGEXP_ORIGINS=/^(\w+:)\/\/([^:/?#]*):?(\d*)/i,e.state={canvas:null,initial:!0},e.keyboardHandler=e.keyboardHandler.bind(e),e.addNew=e.addNew.bind(e),e.deleteShapes=e.deleteShapes.bind(e),e.discardActiveObject=e.discardActiveObject.bind(e),e}return n(l,i),l.prototype.componentDidMount=function(){this.state.canvas||this.initialCanvas()},l.prototype.componentDidUpdate=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.zoomLevel,i=o.activeObject,n=o.height,a=o.width,c=o.borderColor,s=o.cornerColor,l=o.cornerSize,d=o.transparentCorners,p=o.cropBackgroundColor,h=o.cropBackgroundOpacity,u=o.disableZoom,v=void 0!==u&&u,g=t.zoomLevel,f=t.activeObject,b=t.height,y=t.width,C=t.borderColor,m=t.cornerColor,w=t.cornerSize,O=t.transparentCorners,k=t.cropBackgroundColor,S=t.cropBackgroundOpacity,B=!1;if(g!==r&&r&&r>0&&(e.setZoom(r),B=!0),f!==i&&i){var j=e.getObjects().filter((function(t){return t.objectId===i}));e.discardActiveObject();for(var A=0,I=j;A<I.length;A++){var E=I[A];e.setActiveObject(E)}B=!0}if(e.off("mouse:wheel"),!v){var z=this.zoom.bind(this);e.on("mouse:wheel",z)}if(n&&n!==b&&(e.setHeight(n),B=!0),a&&a!==y&&(e.setWidth(a),B=!0),c!==C||s!==m||l!==w||d!==O||p!==k||h!==S){var H={borderColor:c,cornerColor:s,cornerSize:l,transparentCorners:d,cropBackgroundColor:p,cropBackgroundOpacity:h};this.updateCropAttributes(e,H),B=!0}B&&e.requestRenderAll()}},l.prototype.updateCropAttributes=function(t,e){var o=t.getObjects("rect");Array.isArray(o)&&o.length>0&&o.forEach((function(t){t.borderColor=e.borderColor,t.cornerColor=e.cornerColor,t.cornerSize=e.cornerSize,t.transparentCorners=e.transparentCorners,t.opacity=e.cropBackgroundOpacity,t.set("fill",e.cropBackgroundColor)}))},l.prototype.changeImage=function(){var t=this.state.canvas;t&&(t.backgroundImage||this.initialImage())},l.prototype.loadImage=function(t){var e=this.state,o=e.initial,r=e.canvas;if(r&&r.width&&r.height&&t.height&&t.width){var i=this.props.zoomLevel,n=t.height/t.width,a=r.width*n;r.setHeight(a),i?r.setZoom(i):r.setZoom(r.width/t.width),r.setBackgroundImage(t,r.renderAll.bind(r)),o&&this.setState({initial:!1},this.initialObjects.bind(this))}},l.prototype.isCrossOriginURL=function(t){var e=t.match(this.REGEXP_ORIGINS);return null!==e&&(e[1]!==window.location.protocol||e[2]!==window.location.hostname||e[3]!==window.location.port)},l.prototype.initialImage=function(){var t=this.props.image,o=this.loadImage.bind(this);if("string"==typeof t){var r={};this.isCrossOriginURL(t)&&(r.crossOrigin="Anonymous"),e.fabric.Image.fromURL(t,o,r)}},l.prototype.initialObjects=function(){var t=this.state.canvas;if(t){var e=this.props,o=e.record,i=e.readonly,n=e.borderColor,a=e.cornerColor,c=e.cornerSize,s=e.transparentCorners,l=e.cropBackgroundColor,d=e.cropBackgroundOpacity;if(o){var p=o.clippings;if(Array.isArray(p)&&p.length>0&&"object"===r(p[0])){for(var h={borderColor:n,cornerColor:a,cornerSize:c,transparentCorners:s,cropBackgroundColor:l,cropBackgroundOpacity:d},u=0,v=0,g=p;v<g.length;v++){var f=g[v],b=this.createObject(t,f,h,i||!1);b&&(t.add(b),u+=1)}u>0&&this.setOutput()}}else console.log("Not have any record. Skipped.")}},l.prototype.zoom=function(t){var e=this.state.canvas;if(e){var o=t.e.deltaY,r=e.getZoom();(r*=Math.pow(.999,o))>20&&(r=20),r<.01&&(r=.01),e.setZoom(r),t.e.preventDefault(),t.e.stopPropagation();var i=this.props.zoomChanged;i&&i(r)}},l.prototype.mouseHover=function(t){var e=this.props.onHover,o=this.shapetoStructureData.bind(this),r=t.target;r&&"rect"===r.type&&e&&e(o(r))},l.prototype.mouseOut=function(t){var e=this.props.onHover,o=t.target;o&&"rect"===o.type&&e&&e(null)},l.prototype.selectionHandler=function(t){var e=this.props.onSelect,o=this.shapetoStructureData.bind(this),r=t.selected;if(Array.isArray(r)&&0!=r.length){var i=r[0];if(i&&"rect"===i.type&&e)e(o(i))}},l.prototype.selectionClearHandler=function(){var t=this.props.onSelect;t&&t(null)},l.prototype.initialCanvas=function(){var t=this.props,o=t.id,r=t.width,i=t.height,n=t.readonly,a=new e.fabric.Canvas(o||"canvas",{width:r,height:i});if(n){a.selectionKey=void 0;var c=this.mouseHover.bind(this),s=this.mouseOut.bind(this);a.on("mouse:over",c),a.on("mouse:out",s)}else{var l=this.doubleClickEvent.bind(this),d=this.setOutput.bind(this);a.on("mouse:dblclick",l),a.on("object:modified",d)}var p=this.selectionHandler.bind(this),h=this.selectionClearHandler.bind(this);a.on("selection:created",p),a.on("selection:updated",p),a.on("selection:cleared",h),a.on("mouse:down",(function(t){var e=t.e;!0===e.altKey&&(this.isDragging=!0,this.selection=!1,this.lastPosX=e.clientX,this.lastPosY=e.clientY)})),a.on("mouse:move",(function(t){if(this.isDragging){var e=t.e,o=this.viewportTransform;o[4]+=e.clientX-this.lastPosX,o[5]+=e.clientY-this.lastPosY,this.requestRenderAll(),this.lastPosX=e.clientX,this.lastPosY=e.clientY}})),a.on("mouse:up",(function(){this.setViewportTransform(this.viewportTransform),this.isDragging=!1,this.selection=!0})),this.setState({canvas:a},this.initialImage.bind(this))},l.prototype.addNew=function(){var t=this.state.canvas;if(t){var e=this.props,o={borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,cropBackgroundColor:e.cropBackgroundColor,cropBackgroundOpacity:e.cropBackgroundOpacity},r=this.createObject(t,{id:null,rect:{x1:0,y1:0,x2:.2,y2:.2}},o,!1);r&&(t.add(r),this.setOutput())}},l.prototype.doubleClickEvent=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.readonly,i=o.borderColor,n=o.cornerColor,a=o.cornerSize,c=o.transparentCorners,s=o.cropBackgroundColor,l=o.cropBackgroundOpacity;if(t&&t.target){var d=t.target.left,p=t.target.top,h=t.target.width,u=t.target.height,v={left:d+50,top:p+50,width:h*t.target.scaleX,height:u*t.target.scaleY,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}else if(t&&t.pointer){v={left:d=t.absolutePointer.x,top:t.absolutePointer.y,width:100,height:100,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}}},l.prototype.createObjectByAttribute=function(t,e,r){return new a({left:e.left,top:e.top,width:e.width,height:e.height,borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,fill:e.cropBackgroundColor,opacity:e.cropBackgroundOpacity,id:t,strokeWidth:0,strokeUniform:!0,lockRotation:!0,lockMovementX:r,lockMovementY:r,lockScalingX:r,lockScalingY:r,objectId:o.v4()})},l.prototype.convertLeftTop=function(t){var e,o,r,i;return void 0===t.left||void 0===t.top||void 0===(null===(e=t.group)||void 0===e?void 0:e.left)||void 0===(null===(o=t.group)||void 0===o?void 0:o.top)||void 0===(null===(r=t.group)||void 0===r?void 0:r.width)||void 0===(null===(i=t.group)||void 0===i?void 0:i.height)?{left:0,top:0}:{left:t.left+t.group.left+t.group.width/2,top:t.top+t.group.top+t.group.height/2}},l.prototype.shapetoStructureData=function(t){var o=this.state.canvas;if(!o||!o.backgroundImage)return null;var r=o.backgroundImage;if(!(r instanceof e.fabric.Image))return null;if(void 0===t.left||void 0===t.top||void 0===t.width||void 0===t.height||void 0===t.scaleX||void 0===t.scaleY||void 0===r.width||void 0===r.height)return null;var i=this.props,n=i.includeDataUrl,a=i.includeHtmlCanvas,c=t.group?this.convertLeftTop(t):{left:t.left,top:t.top},s=c.left/r.width,l=c.top/r.height,d=(c.left+t.width*t.scaleX)/r.width,p=(c.top+t.height*t.scaleY)/r.height,h={x1:s,y1:l,x2:d,y2:p},u={id:t.id,objectId:t.objectId,rect:JSON.stringify(h)};if(n){var v=null;try{v=r.toDataURL({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top,format:"jpeg"})}catch(t){console.error(t)}u.dataUrl=v}if(a){var g=null;try{g=r.toCanvasElement({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top})}catch(t){console.error(t)}u.canvasElement=g}var f=r.width,b=r.height,y=s*f,C=d*f,m=l*b,w=p*b,O={x:y,y:m,x2:C,y2:w,w:C-y,h:w-m,boundX:f,boundY:b};return u.crop=JSON.stringify(O),u.deletedAt="-1",u},l.prototype.deleteShapes=function(){var t=this.props.readonly,e=this.state.canvas;e&&!t&&(e.getActiveObjects().forEach((function(t){e.remove(t)})),this.setOutput())},l.prototype.setOutput=function(){var t=this.state.canvas;if(t){var e=this.shapetoStructureData.bind(this),o=[];t.getObjects("rect").forEach((function(t){var r=e(t);r&&o.push(r)}));var r=this.props.input;r&&r.onChange(o)}},l.prototype.createObject=function(t,o,r,i){if(!t||!t.backgroundImage)return null;var n,a=t.backgroundImage;if(!(a instanceof e.fabric.Image))return null;if(!a.width||!a.height)return null;n="string"==typeof o.rect?JSON.parse(o.rect):o.rect;var c=a.width*n.x1,s=a.height*n.y1,l={left:c,top:s,width:a.width*n.x2-c,height:a.height*n.y2-s,borderColor:r.borderColor,cornerColor:r.cornerColor,cornerSize:r.cornerSize,transparentCorners:r.transparentCorners,cropBackgroundColor:r.cropBackgroundColor,cropBackgroundOpacity:r.cropBackgroundOpacity};return this.createObjectByAttribute(o.id,l,i)},l.prototype.discardActiveObject=function(){var t=this.state.canvas;t?(t.discardActiveObject(),t.requestRenderAll()):console.log("Canvas not defined")},l.prototype.keyboardHandler=function(t){if(!t.defaultPrevented){var e=!1,o=t.key||t.keyCode;"Delete"!==o&&46!==o||(this.deleteShapes(),e=!0),e&&t.preventDefault()}},l.prototype.render=function(){var e=this.props,o=e.addButton,r=e.deleteButton,i=e.discardButton,n=e.height,a=e.id,l=e.readonly,d=e.showButton,p=e.style,h=e.width;return t.createElement("div",{id:"canvas-wrapper",style:p},t.createElement(c,{row:!0,width:h,height:n},t.createElement("div",{onKeyDown:l?void 0:this.keyboardHandler,tabIndex:0},t.createElement("canvas",{id:a,height:n,style:{border:"0px solid #aaa"},width:h})),d&&!l&&t.createElement(s,{addButton:o,addNew:this.addNew,deleteButton:r,deleteShapes:this.deleteShapes,discardActiveObject:this.discardActiveObject,discardButton:i})))},l.defaultProps={borderColor:"black",cornerColor:"black",cornerSize:13,cropBackgroundColor:"yellow",cropBackgroundOpacity:.5,disableZoom:!1,height:800,id:"canvas",image:void 0,includeDataUrl:!1,includeHtmlCanvas:!1,input:void 0,readonly:!1,record:{clippings:[]},showButton:!1,transparentCorners:!0,width:800,zoomChanged:void 0},l}(t.Component);exports.CustomFabricRect=a,exports.ReactMultiCrop=l;
package/dist/index.esm.js CHANGED
@@ -1 +1 @@
1
- import t,{Component as e}from"react";import{fabric as o}from"fabric";import{v4 as r}from"uuid";function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},n(t,e)};function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var c=function(t){function e(e){var o=t.call(this,e)||this;return o.id=null,o.objectId="",e&&(o.id=e.id,o.objectId=e.objectId),o}return a(e,t),e}(o.Rect);function s(e){var o=e.children,r=e.height,i=e.width,n=e.row,a=void 0!==n&&n;return t.createElement("div",{style:{display:"flex",flexDirection:a?"row":"column",gap:"10px",height:r,width:i}},o)}function l(e){var o=e.addButton,r=e.addNew,i=e.deleteButton,n=e.deleteShapes,a=e.discardActiveObject,c=e.discardButton;return t.createElement(s,null,t.createElement("div",null,o&&t.cloneElement(o,{onClick:r})),t.createElement("div",null,i&&t.cloneElement(i,{onClick:n})),t.createElement("div",null,c&&t.cloneElement(c,{onClick:a})))}var d=function(e){function n(t){var o=e.call(this,t)||this;return o.REGEXP_ORIGINS=/^(\w+:)\/\/([^:/?#]*):?(\d*)/i,o.state={canvas:null,initial:!0},o.keyboardHandler=o.keyboardHandler.bind(o),o.addNew=o.addNew.bind(o),o.deleteShapes=o.deleteShapes.bind(o),o.discardActiveObject=o.discardActiveObject.bind(o),o}return a(n,e),n.prototype.componentDidMount=function(){this.state.canvas||this.initialCanvas()},n.prototype.componentDidUpdate=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.zoomLevel,i=o.activeObject,n=o.height,a=o.width,c=o.borderColor,s=o.cornerColor,l=o.cornerSize,d=o.transparentCorners,p=o.cropBackgroundColor,h=o.cropBackgroundOpacity,u=o.disableZoom,v=void 0!==u&&u,g=t.zoomLevel,f=t.activeObject,y=t.height,b=t.width,m=t.borderColor,C=t.cornerColor,w=t.cornerSize,O=t.transparentCorners,k=t.cropBackgroundColor,S=t.cropBackgroundOpacity,B=!1;if(g!==r&&r&&r>0&&(e.setZoom(r),B=!0),f!==i&&i){var j=e.getObjects().filter((function(t){return t.objectId===i}));e.discardActiveObject();for(var A=0,I=j;A<I.length;A++){var E=I[A];e.setActiveObject(E)}B=!0}if(e.off("mouse:wheel"),!v){var z=this.zoom.bind(this);e.on("mouse:wheel",z)}if(n&&n!==y&&(e.setHeight(n),B=!0),a&&a!==b&&(e.setWidth(a),B=!0),c!==m||s!==C||l!==w||d!==O||p!==k||h!==S){var H={borderColor:c,cornerColor:s,cornerSize:l,transparentCorners:d,cropBackgroundColor:p,cropBackgroundOpacity:h};this.updateCropAttributes(e,H),B=!0}B&&e.requestRenderAll()}},n.prototype.updateCropAttributes=function(t,e){var o=t.getObjects("rect");Array.isArray(o)&&o.length>0&&o.forEach((function(t){t.borderColor=e.borderColor,t.cornerColor=e.cornerColor,t.cornerSize=e.cornerSize,t.transparentCorners=e.transparentCorners,t.opacity=e.cropBackgroundOpacity,t.set("fill",e.cropBackgroundColor)}))},n.prototype.changeImage=function(){var t=this.state.canvas;t&&(t.backgroundImage||this.initialImage())},n.prototype.loadImage=function(t){var e=this.state,o=e.initial,r=e.canvas;if(r&&r.width&&r.height&&t.height&&t.width){var i=this.props.zoomLevel,n=t.height/t.width,a=r.width*n;r.setHeight(a),i?r.setZoom(i):r.setZoom(r.width/t.width),r.setBackgroundImage(t,r.renderAll.bind(r)),o&&this.setState({initial:!1},this.initialObjects.bind(this))}},n.prototype.isCrossOriginURL=function(t){var e=t.match(this.REGEXP_ORIGINS);return null!==e&&(e[1]!==window.location.protocol||e[2]!==window.location.hostname||e[3]!==window.location.port)},n.prototype.initialImage=function(){var t=this.props.image,e=this.loadImage.bind(this);if("string"==typeof t){var r={};this.isCrossOriginURL(t)&&(r.crossOrigin="Anonymous"),o.Image.fromURL(t,e,r)}},n.prototype.initialObjects=function(){var t=this.state.canvas;if(t){var e=this.props,o=e.record,r=e.readonly,n=e.borderColor,a=e.cornerColor,c=e.cornerSize,s=e.transparentCorners;if(o){var l=o.clippings;if(Array.isArray(l)&&l.length>0&&"object"===i(l[0])){for(var d={borderColor:n,cornerColor:a,cornerSize:c,transparentCorners:s},p=0,h=0,u=l;h<u.length;h++){var v=u[h],g=this.createObject(t,v,d,r||!1);g&&(t.add(g),p+=1)}p>0&&this.setOutput()}}else console.log("Not have any record. Skipped.")}},n.prototype.zoom=function(t){var e=this.state.canvas;if(e){var o=t.e.deltaY,r=e.getZoom();(r*=Math.pow(.999,o))>20&&(r=20),r<.01&&(r=.01),e.setZoom(r),t.e.preventDefault(),t.e.stopPropagation();var i=this.props.zoomChanged;i&&i(r)}},n.prototype.mouseHover=function(t){var e=this.props.onHover,o=this.shapetoStructureData.bind(this),r=t.target;r&&"rect"===r.type&&e&&e(o(r))},n.prototype.mouseOut=function(t){var e=this.props.onHover,o=t.target;o&&"rect"===o.type&&e&&e(null)},n.prototype.selectionHandler=function(t){var e=this.props.onSelect,o=this.shapetoStructureData.bind(this),r=t.selected;if(Array.isArray(r)&&0!=r.length){var i=r[0];if(i&&"rect"===i.type&&e)e(o(i))}},n.prototype.selectionClearHandler=function(){var t=this.props.onSelect;t&&t(null)},n.prototype.initialCanvas=function(){var t=this.props,e=t.id,r=t.width,i=t.height,n=t.readonly,a=new o.Canvas(e||"canvas",{width:r,height:i});if(n){a.selectionKey=void 0;var c=this.mouseHover.bind(this),s=this.mouseOut.bind(this);a.on("mouse:over",c),a.on("mouse:out",s)}else{var l=this.doubleClickEvent.bind(this),d=this.setOutput.bind(this);a.on("mouse:dblclick",l),a.on("object:modified",d)}var p=this.selectionHandler.bind(this),h=this.selectionClearHandler.bind(this);a.on("selection:created",p),a.on("selection:updated",p),a.on("selection:cleared",h),a.on("mouse:down",(function(t){var e=t.e;!0===e.altKey&&(this.isDragging=!0,this.selection=!1,this.lastPosX=e.clientX,this.lastPosY=e.clientY)})),a.on("mouse:move",(function(t){if(this.isDragging){var e=t.e,o=this.viewportTransform;o[4]+=e.clientX-this.lastPosX,o[5]+=e.clientY-this.lastPosY,this.requestRenderAll(),this.lastPosX=e.clientX,this.lastPosY=e.clientY}})),a.on("mouse:up",(function(){this.setViewportTransform(this.viewportTransform),this.isDragging=!1,this.selection=!0})),this.setState({canvas:a},this.initialImage.bind(this))},n.prototype.addNew=function(){var t=this.state.canvas;if(t){var e=this.props,o={borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,cropBackgroundColor:e.cropBackgroundColor,cropBackgroundOpacity:e.cropBackgroundOpacity},r=this.createObject(t,{id:null,rect:{x1:0,y1:0,x2:.2,y2:.2}},o,!1);r&&(t.add(r),this.setOutput())}},n.prototype.doubleClickEvent=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.readonly,i=o.borderColor,n=o.cornerColor,a=o.cornerSize,c=o.transparentCorners,s=o.cropBackgroundColor,l=o.cropBackgroundOpacity;if(t&&t.target){var d=t.target.left,p=t.target.top,h=t.target.width,u=t.target.height,v={left:d+50,top:p+50,width:h*t.target.scaleX,height:u*t.target.scaleY,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}else if(t&&t.pointer){v={left:d=t.absolutePointer.x,top:t.absolutePointer.y,width:100,height:100,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}}},n.prototype.createObjectByAttribute=function(t,e,o){return new c({left:e.left,top:e.top,width:e.width,height:e.height,borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,fill:e.cropBackgroundColor,opacity:e.cropBackgroundOpacity,id:t,strokeWidth:0,strokeUniform:!0,lockRotation:!0,lockMovementX:o,lockMovementY:o,lockScalingX:o,lockScalingY:o,objectId:r()})},n.prototype.convertLeftTop=function(t){var e,o,r,i;return void 0===t.left||void 0===t.top||void 0===(null===(e=t.group)||void 0===e?void 0:e.left)||void 0===(null===(o=t.group)||void 0===o?void 0:o.top)||void 0===(null===(r=t.group)||void 0===r?void 0:r.width)||void 0===(null===(i=t.group)||void 0===i?void 0:i.height)?{left:0,top:0}:{left:t.left+t.group.left+t.group.width/2,top:t.top+t.group.top+t.group.height/2}},n.prototype.shapetoStructureData=function(t){var e=this.state.canvas;if(!e||!e.backgroundImage)return null;var r=e.backgroundImage;if(!(r instanceof o.Image))return null;if(void 0===t.left||void 0===t.top||void 0===t.width||void 0===t.height||void 0===t.scaleX||void 0===t.scaleY||void 0===r.width||void 0===r.height)return null;var i=this.props,n=i.includeDataUrl,a=i.includeHtmlCanvas,c=t.group?this.convertLeftTop(t):{left:t.left,top:t.top},s=c.left/r.width,l=c.top/r.height,d=(c.left+t.width*t.scaleX)/r.width,p=(c.top+t.height*t.scaleY)/r.height,h={x1:s,y1:l,x2:d,y2:p},u={id:t.id,objectId:t.objectId,rect:JSON.stringify(h)};if(n){var v=null;try{v=r.toDataURL({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top,format:"jpeg"})}catch(t){console.error(t)}u.dataUrl=v}if(a){var g=null;try{g=r.toCanvasElement({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top})}catch(t){console.error(t)}u.canvasElement=g}var f=r.width,y=r.height,b=s*f,m=d*f,C=l*y,w=p*y,O={x:b,y:C,x2:m,y2:w,w:m-b,h:w-C,boundX:f,boundY:y};return u.crop=JSON.stringify(O),u.deletedAt="-1",u},n.prototype.deleteShapes=function(){var t=this.props.readonly,e=this.state.canvas;e&&!t&&(e.getActiveObjects().forEach((function(t){e.remove(t)})),this.setOutput())},n.prototype.setOutput=function(){var t=this.state.canvas;if(t){var e=this.shapetoStructureData.bind(this),o=[];t.getObjects("rect").forEach((function(t){var r=e(t);r&&o.push(r)}));var r=this.props.input;r&&r.onChange(o)}},n.prototype.createObject=function(t,e,r,i){if(!t||!t.backgroundImage)return null;var n,a=t.backgroundImage;if(!(a instanceof o.Image))return null;if(!a.width||!a.height)return null;n="string"==typeof e.rect?JSON.parse(e.rect):e.rect;var c=a.width*n.x1,s=a.height*n.y1,l={left:c,top:s,width:a.width*n.x2-c,height:a.height*n.y2-s,borderColor:r.borderColor,cornerColor:r.cornerColor,cornerSize:r.cornerSize,transparentCorners:r.transparentCorners,cropBackgroundColor:r.cropBackgroundColor,cropBackgroundOpacity:r.cropBackgroundOpacity};return this.createObjectByAttribute(e.id,l,i)},n.prototype.discardActiveObject=function(){var t=this.state.canvas;t?(t.discardActiveObject(),t.requestRenderAll()):console.log("Canvas not defined")},n.prototype.keyboardHandler=function(t){if(!t.defaultPrevented){var e=!1,o=t.key||t.keyCode;"Delete"!==o&&46!==o||(this.deleteShapes(),e=!0),e&&t.preventDefault()}},n.prototype.render=function(){var e=this.props,o=e.addButton,r=e.deleteButton,i=e.discardButton,n=e.height,a=e.id,c=e.readonly,d=e.showButton,p=e.style,h=e.width;return t.createElement("div",{id:"canvas-wrapper",style:p},t.createElement(s,{row:!0,width:h,height:n},t.createElement("div",{onKeyDown:c?void 0:this.keyboardHandler,tabIndex:0},t.createElement("canvas",{id:a,height:n,style:{border:"0px solid #aaa"},width:h})),d&&!c&&t.createElement(l,{addButton:o,addNew:this.addNew,deleteButton:r,deleteShapes:this.deleteShapes,discardActiveObject:this.discardActiveObject,discardButton:i})))},n.defaultProps={borderColor:"black",cornerColor:"black",cornerSize:13,cropBackgroundColor:"yellow",cropBackgroundOpacity:.5,disableZoom:!1,height:800,id:"canvas",image:void 0,includeDataUrl:!1,includeHtmlCanvas:!1,input:void 0,readonly:!1,record:{clippings:[]},showButton:!1,transparentCorners:!0,width:800,zoomChanged:void 0},n}(e);export{c as CustomFabricRect,d as ReactMultiCrop};
1
+ import t,{Component as e}from"react";import{fabric as o}from"fabric";import{v4 as r}from"uuid";function i(t){return i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},i(t)}var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])},n(t,e)};function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function o(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var c=function(t){function e(e){var o=t.call(this,e)||this;return o.id=null,o.objectId="",e&&(o.id=e.id,o.objectId=e.objectId),o}return a(e,t),e}(o.Rect);function s(e){var o=e.children,r=e.height,i=e.width,n=e.row,a=void 0!==n&&n;return t.createElement("div",{style:{display:"flex",flexDirection:a?"row":"column",gap:"10px",height:r,width:i}},o)}function l(e){var o=e.addButton,r=e.addNew,i=e.deleteButton,n=e.deleteShapes,a=e.discardActiveObject,c=e.discardButton;return t.createElement(s,null,t.createElement("div",null,o&&t.cloneElement(o,{onClick:r})),t.createElement("div",null,i&&t.cloneElement(i,{onClick:n})),t.createElement("div",null,c&&t.cloneElement(c,{onClick:a})))}var d=function(e){function n(t){var o=e.call(this,t)||this;return o.REGEXP_ORIGINS=/^(\w+:)\/\/([^:/?#]*):?(\d*)/i,o.state={canvas:null,initial:!0},o.keyboardHandler=o.keyboardHandler.bind(o),o.addNew=o.addNew.bind(o),o.deleteShapes=o.deleteShapes.bind(o),o.discardActiveObject=o.discardActiveObject.bind(o),o}return a(n,e),n.prototype.componentDidMount=function(){this.state.canvas||this.initialCanvas()},n.prototype.componentDidUpdate=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.zoomLevel,i=o.activeObject,n=o.height,a=o.width,c=o.borderColor,s=o.cornerColor,l=o.cornerSize,d=o.transparentCorners,p=o.cropBackgroundColor,h=o.cropBackgroundOpacity,u=o.disableZoom,v=void 0!==u&&u,g=t.zoomLevel,f=t.activeObject,y=t.height,b=t.width,m=t.borderColor,C=t.cornerColor,w=t.cornerSize,O=t.transparentCorners,k=t.cropBackgroundColor,S=t.cropBackgroundOpacity,B=!1;if(g!==r&&r&&r>0&&(e.setZoom(r),B=!0),f!==i&&i){var j=e.getObjects().filter((function(t){return t.objectId===i}));e.discardActiveObject();for(var A=0,I=j;A<I.length;A++){var E=I[A];e.setActiveObject(E)}B=!0}if(e.off("mouse:wheel"),!v){var z=this.zoom.bind(this);e.on("mouse:wheel",z)}if(n&&n!==y&&(e.setHeight(n),B=!0),a&&a!==b&&(e.setWidth(a),B=!0),c!==m||s!==C||l!==w||d!==O||p!==k||h!==S){var H={borderColor:c,cornerColor:s,cornerSize:l,transparentCorners:d,cropBackgroundColor:p,cropBackgroundOpacity:h};this.updateCropAttributes(e,H),B=!0}B&&e.requestRenderAll()}},n.prototype.updateCropAttributes=function(t,e){var o=t.getObjects("rect");Array.isArray(o)&&o.length>0&&o.forEach((function(t){t.borderColor=e.borderColor,t.cornerColor=e.cornerColor,t.cornerSize=e.cornerSize,t.transparentCorners=e.transparentCorners,t.opacity=e.cropBackgroundOpacity,t.set("fill",e.cropBackgroundColor)}))},n.prototype.changeImage=function(){var t=this.state.canvas;t&&(t.backgroundImage||this.initialImage())},n.prototype.loadImage=function(t){var e=this.state,o=e.initial,r=e.canvas;if(r&&r.width&&r.height&&t.height&&t.width){var i=this.props.zoomLevel,n=t.height/t.width,a=r.width*n;r.setHeight(a),i?r.setZoom(i):r.setZoom(r.width/t.width),r.setBackgroundImage(t,r.renderAll.bind(r)),o&&this.setState({initial:!1},this.initialObjects.bind(this))}},n.prototype.isCrossOriginURL=function(t){var e=t.match(this.REGEXP_ORIGINS);return null!==e&&(e[1]!==window.location.protocol||e[2]!==window.location.hostname||e[3]!==window.location.port)},n.prototype.initialImage=function(){var t=this.props.image,e=this.loadImage.bind(this);if("string"==typeof t){var r={};this.isCrossOriginURL(t)&&(r.crossOrigin="Anonymous"),o.Image.fromURL(t,e,r)}},n.prototype.initialObjects=function(){var t=this.state.canvas;if(t){var e=this.props,o=e.record,r=e.readonly,n=e.borderColor,a=e.cornerColor,c=e.cornerSize,s=e.transparentCorners,l=e.cropBackgroundColor,d=e.cropBackgroundOpacity;if(o){var p=o.clippings;if(Array.isArray(p)&&p.length>0&&"object"===i(p[0])){for(var h={borderColor:n,cornerColor:a,cornerSize:c,transparentCorners:s,cropBackgroundColor:l,cropBackgroundOpacity:d},u=0,v=0,g=p;v<g.length;v++){var f=g[v],y=this.createObject(t,f,h,r||!1);y&&(t.add(y),u+=1)}u>0&&this.setOutput()}}else console.log("Not have any record. Skipped.")}},n.prototype.zoom=function(t){var e=this.state.canvas;if(e){var o=t.e.deltaY,r=e.getZoom();(r*=Math.pow(.999,o))>20&&(r=20),r<.01&&(r=.01),e.setZoom(r),t.e.preventDefault(),t.e.stopPropagation();var i=this.props.zoomChanged;i&&i(r)}},n.prototype.mouseHover=function(t){var e=this.props.onHover,o=this.shapetoStructureData.bind(this),r=t.target;r&&"rect"===r.type&&e&&e(o(r))},n.prototype.mouseOut=function(t){var e=this.props.onHover,o=t.target;o&&"rect"===o.type&&e&&e(null)},n.prototype.selectionHandler=function(t){var e=this.props.onSelect,o=this.shapetoStructureData.bind(this),r=t.selected;if(Array.isArray(r)&&0!=r.length){var i=r[0];if(i&&"rect"===i.type&&e)e(o(i))}},n.prototype.selectionClearHandler=function(){var t=this.props.onSelect;t&&t(null)},n.prototype.initialCanvas=function(){var t=this.props,e=t.id,r=t.width,i=t.height,n=t.readonly,a=new o.Canvas(e||"canvas",{width:r,height:i});if(n){a.selectionKey=void 0;var c=this.mouseHover.bind(this),s=this.mouseOut.bind(this);a.on("mouse:over",c),a.on("mouse:out",s)}else{var l=this.doubleClickEvent.bind(this),d=this.setOutput.bind(this);a.on("mouse:dblclick",l),a.on("object:modified",d)}var p=this.selectionHandler.bind(this),h=this.selectionClearHandler.bind(this);a.on("selection:created",p),a.on("selection:updated",p),a.on("selection:cleared",h),a.on("mouse:down",(function(t){var e=t.e;!0===e.altKey&&(this.isDragging=!0,this.selection=!1,this.lastPosX=e.clientX,this.lastPosY=e.clientY)})),a.on("mouse:move",(function(t){if(this.isDragging){var e=t.e,o=this.viewportTransform;o[4]+=e.clientX-this.lastPosX,o[5]+=e.clientY-this.lastPosY,this.requestRenderAll(),this.lastPosX=e.clientX,this.lastPosY=e.clientY}})),a.on("mouse:up",(function(){this.setViewportTransform(this.viewportTransform),this.isDragging=!1,this.selection=!0})),this.setState({canvas:a},this.initialImage.bind(this))},n.prototype.addNew=function(){var t=this.state.canvas;if(t){var e=this.props,o={borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,cropBackgroundColor:e.cropBackgroundColor,cropBackgroundOpacity:e.cropBackgroundOpacity},r=this.createObject(t,{id:null,rect:{x1:0,y1:0,x2:.2,y2:.2}},o,!1);r&&(t.add(r),this.setOutput())}},n.prototype.doubleClickEvent=function(t){var e=this.state.canvas;if(e){var o=this.props,r=o.readonly,i=o.borderColor,n=o.cornerColor,a=o.cornerSize,c=o.transparentCorners,s=o.cropBackgroundColor,l=o.cropBackgroundOpacity;if(t&&t.target){var d=t.target.left,p=t.target.top,h=t.target.width,u=t.target.height,v={left:d+50,top:p+50,width:h*t.target.scaleX,height:u*t.target.scaleY,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}else if(t&&t.pointer){v={left:d=t.absolutePointer.x,top:t.absolutePointer.y,width:100,height:100,borderColor:i,cornerColor:n,cornerSize:a,transparentCorners:c,cropBackgroundColor:s,cropBackgroundOpacity:l},g=this.createObjectByAttribute(null,v,r||!1);e.add(g),e.discardActiveObject(),e.setActiveObject(g),e.requestRenderAll(),this.setOutput()}}},n.prototype.createObjectByAttribute=function(t,e,o){return new c({left:e.left,top:e.top,width:e.width,height:e.height,borderColor:e.borderColor,cornerColor:e.cornerColor,cornerSize:e.cornerSize,transparentCorners:e.transparentCorners,fill:e.cropBackgroundColor,opacity:e.cropBackgroundOpacity,id:t,strokeWidth:0,strokeUniform:!0,lockRotation:!0,lockMovementX:o,lockMovementY:o,lockScalingX:o,lockScalingY:o,objectId:r()})},n.prototype.convertLeftTop=function(t){var e,o,r,i;return void 0===t.left||void 0===t.top||void 0===(null===(e=t.group)||void 0===e?void 0:e.left)||void 0===(null===(o=t.group)||void 0===o?void 0:o.top)||void 0===(null===(r=t.group)||void 0===r?void 0:r.width)||void 0===(null===(i=t.group)||void 0===i?void 0:i.height)?{left:0,top:0}:{left:t.left+t.group.left+t.group.width/2,top:t.top+t.group.top+t.group.height/2}},n.prototype.shapetoStructureData=function(t){var e=this.state.canvas;if(!e||!e.backgroundImage)return null;var r=e.backgroundImage;if(!(r instanceof o.Image))return null;if(void 0===t.left||void 0===t.top||void 0===t.width||void 0===t.height||void 0===t.scaleX||void 0===t.scaleY||void 0===r.width||void 0===r.height)return null;var i=this.props,n=i.includeDataUrl,a=i.includeHtmlCanvas,c=t.group?this.convertLeftTop(t):{left:t.left,top:t.top},s=c.left/r.width,l=c.top/r.height,d=(c.left+t.width*t.scaleX)/r.width,p=(c.top+t.height*t.scaleY)/r.height,h={x1:s,y1:l,x2:d,y2:p},u={id:t.id,objectId:t.objectId,rect:JSON.stringify(h)};if(n){var v=null;try{v=r.toDataURL({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top,format:"jpeg"})}catch(t){console.error(t)}u.dataUrl=v}if(a){var g=null;try{g=r.toCanvasElement({height:t.getScaledHeight(),width:t.getScaledWidth(),left:c.left,top:c.top})}catch(t){console.error(t)}u.canvasElement=g}var f=r.width,y=r.height,b=s*f,m=d*f,C=l*y,w=p*y,O={x:b,y:C,x2:m,y2:w,w:m-b,h:w-C,boundX:f,boundY:y};return u.crop=JSON.stringify(O),u.deletedAt="-1",u},n.prototype.deleteShapes=function(){var t=this.props.readonly,e=this.state.canvas;e&&!t&&(e.getActiveObjects().forEach((function(t){e.remove(t)})),this.setOutput())},n.prototype.setOutput=function(){var t=this.state.canvas;if(t){var e=this.shapetoStructureData.bind(this),o=[];t.getObjects("rect").forEach((function(t){var r=e(t);r&&o.push(r)}));var r=this.props.input;r&&r.onChange(o)}},n.prototype.createObject=function(t,e,r,i){if(!t||!t.backgroundImage)return null;var n,a=t.backgroundImage;if(!(a instanceof o.Image))return null;if(!a.width||!a.height)return null;n="string"==typeof e.rect?JSON.parse(e.rect):e.rect;var c=a.width*n.x1,s=a.height*n.y1,l={left:c,top:s,width:a.width*n.x2-c,height:a.height*n.y2-s,borderColor:r.borderColor,cornerColor:r.cornerColor,cornerSize:r.cornerSize,transparentCorners:r.transparentCorners,cropBackgroundColor:r.cropBackgroundColor,cropBackgroundOpacity:r.cropBackgroundOpacity};return this.createObjectByAttribute(e.id,l,i)},n.prototype.discardActiveObject=function(){var t=this.state.canvas;t?(t.discardActiveObject(),t.requestRenderAll()):console.log("Canvas not defined")},n.prototype.keyboardHandler=function(t){if(!t.defaultPrevented){var e=!1,o=t.key||t.keyCode;"Delete"!==o&&46!==o||(this.deleteShapes(),e=!0),e&&t.preventDefault()}},n.prototype.render=function(){var e=this.props,o=e.addButton,r=e.deleteButton,i=e.discardButton,n=e.height,a=e.id,c=e.readonly,d=e.showButton,p=e.style,h=e.width;return t.createElement("div",{id:"canvas-wrapper",style:p},t.createElement(s,{row:!0,width:h,height:n},t.createElement("div",{onKeyDown:c?void 0:this.keyboardHandler,tabIndex:0},t.createElement("canvas",{id:a,height:n,style:{border:"0px solid #aaa"},width:h})),d&&!c&&t.createElement(l,{addButton:o,addNew:this.addNew,deleteButton:r,deleteShapes:this.deleteShapes,discardActiveObject:this.discardActiveObject,discardButton:i})))},n.defaultProps={borderColor:"black",cornerColor:"black",cornerSize:13,cropBackgroundColor:"yellow",cropBackgroundOpacity:.5,disableZoom:!1,height:800,id:"canvas",image:void 0,includeDataUrl:!1,includeHtmlCanvas:!1,input:void 0,readonly:!1,record:{clippings:[]},showButton:!1,transparentCorners:!0,width:800,zoomChanged:void 0},n}(e);export{c as CustomFabricRect,d as ReactMultiCrop};
@@ -1,3 +1,2 @@
1
- /// <reference types="react" />
2
1
  import { IActionComponentProps } from '../interfaces';
3
2
  export declare function ActionsComponent({ addButton, addNew, deleteButton, deleteShapes, discardActiveObject, discardButton, }: IActionComponentProps): JSX.Element;
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  import { fabric } from 'fabric';
3
2
  import CSS from 'csstype';
4
3
  export interface IRecordProps {
package/package.json CHANGED
@@ -25,14 +25,14 @@
25
25
  "url": "https://github.com/berviantoleo"
26
26
  },
27
27
  "devDependencies": {
28
- "@babel/cli": "^7.21.0",
29
- "@babel/core": "^7.21.4",
28
+ "@babel/cli": "^7.21.5",
29
+ "@babel/core": "^7.21.8",
30
30
  "@babel/plugin-proposal-class-properties": "^7.18.6",
31
31
  "@babel/plugin-transform-runtime": "^7.21.4",
32
- "@babel/preset-env": "^7.21.4",
32
+ "@babel/preset-env": "^7.21.5",
33
33
  "@babel/preset-react": "^7.18.6",
34
- "@babel/preset-typescript": "^7.21.4",
35
- "@babel/runtime": "^7.21.0",
34
+ "@babel/preset-typescript": "^7.21.5",
35
+ "@babel/runtime": "^7.21.5",
36
36
  "@rollup/plugin-babel": "^6.0.3",
37
37
  "@rollup/plugin-node-resolve": "^15.0.2",
38
38
  "@storybook/addon-essentials": "^6.5.16",
@@ -76,6 +76,6 @@
76
76
  "uuid": "^9.0.0"
77
77
  },
78
78
  "license": "MIT",
79
- "version": "0.31.0",
80
- "gitHead": "c27dee99f4773bd21b7a2794e0250da04c1f5054"
79
+ "version": "0.31.2",
80
+ "gitHead": "a73e794689e7b5e5afd96cd3a6221791f59cdc0c"
81
81
  }