@pie-element/hotspot 5.15.0 → 5.15.1-next.6

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.
@@ -31,10 +31,16 @@ var _reactKonva = require("react-konva");
31
31
 
32
32
  var _index = require("@material-ui/core/styles/index");
33
33
 
34
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
35
+
36
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
37
+
34
38
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
35
39
 
36
40
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
37
41
 
42
+ var HOVERED_COLOR = '#00BFFF';
43
+
38
44
  var PolComponent = /*#__PURE__*/function (_React$Component) {
39
45
  (0, _inherits2["default"])(PolComponent, _React$Component);
40
46
 
@@ -50,7 +56,49 @@ var PolComponent = /*#__PURE__*/function (_React$Component) {
50
56
  }
51
57
 
52
58
  _this = _super.call.apply(_super, [this].concat(args));
53
- (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "state", {});
59
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getOffset", function (points) {
60
+ var xList = points.map(function (p) {
61
+ return p.x;
62
+ });
63
+ var yList = points.map(function (p) {
64
+ return p.y;
65
+ });
66
+ return {
67
+ x: Math.min.apply(Math, (0, _toConsumableArray2["default"])(xList)),
68
+ y: Math.max.apply(Math, (0, _toConsumableArray2["default"])(yList))
69
+ };
70
+ });
71
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "serialize", function (points) {
72
+ var _this$getOffset = _this.getOffset(points),
73
+ xOffset = _this$getOffset.x,
74
+ yOffset = _this$getOffset.y;
75
+
76
+ return points.reduce(function (acc, point) {
77
+ return [].concat((0, _toConsumableArray2["default"])(acc), [point.x - xOffset, point.y - yOffset]);
78
+ }, []);
79
+ });
80
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "getInitialState", function (points) {
81
+ if (points.length) {
82
+ var _this$getOffset2 = _this.getOffset(points),
83
+ x = _this$getOffset2.x,
84
+ y = _this$getOffset2.y;
85
+
86
+ return {
87
+ x: x,
88
+ y: y,
89
+ points: points
90
+ };
91
+ }
92
+
93
+ return {
94
+ x: 0,
95
+ y: 0,
96
+ points: []
97
+ };
98
+ });
99
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "state", _objectSpread({
100
+ hovered: false
101
+ }, _this.getInitialState(_this.props.points)));
54
102
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleClick", function (e) {
55
103
  var points = _this.props.points;
56
104
  var xList = points.map(function (p) {
@@ -74,32 +122,69 @@ var PolComponent = /*#__PURE__*/function (_React$Component) {
74
122
  onClick(id);
75
123
  });
76
124
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseEnter", function () {
125
+ _this.setState({
126
+ hovered: true
127
+ });
128
+
77
129
  document.body.style.cursor = 'pointer';
78
130
  });
79
131
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseLeave", function () {
132
+ _this.setState({
133
+ hovered: false
134
+ });
135
+
80
136
  document.body.style.cursor = 'default';
81
137
  });
82
138
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleOnDragEnd", function (e) {
139
+ var updateModel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
83
140
  var _this$props2 = _this.props,
84
141
  onDragEnd = _this$props2.onDragEnd,
85
142
  id = _this$props2.id;
86
143
 
87
- var points = e.target.points() || _this.state.points;
144
+ var points = e.target.points() || _this.serialize(_this.state.points);
88
145
 
89
146
  var newPoints = points.reduce(function (acc, currentPointCoordinate, index) {
90
147
  if (index % 2 === 0 && index + 1 < points.length) {
91
- acc.push({
148
+ return [].concat((0, _toConsumableArray2["default"])(acc), [{
92
149
  x: currentPointCoordinate + e.target.x(),
93
150
  y: points[index + 1] + e.target.y()
94
- });
95
- return acc;
96
- } else {
97
- return acc;
151
+ }]);
98
152
  }
153
+
154
+ return acc;
99
155
  }, []);
100
- onDragEnd(id, {
156
+
157
+ _this.setState(_objectSpread({
101
158
  points: newPoints
159
+ }, _this.getOffset(newPoints)));
160
+
161
+ if (updateModel) {
162
+ onDragEnd(id, {
163
+ points: newPoints
164
+ });
165
+ }
166
+ });
167
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleOnDragVertex", function (e, changedIndex, updateModel) {
168
+ var _this$props3 = _this.props,
169
+ onDragEnd = _this$props3.onDragEnd,
170
+ id = _this$props3.id;
171
+ var points = _this.state.points;
172
+ var newPoints = points.map(function (point, index) {
173
+ return index === changedIndex ? {
174
+ x: e.target.x(),
175
+ y: e.target.y()
176
+ } : point;
102
177
  });
178
+
179
+ _this.setState(_objectSpread({
180
+ points: newPoints
181
+ }, _this.getOffset(newPoints)));
182
+
183
+ if (updateModel) {
184
+ onDragEnd(id, {
185
+ points: newPoints
186
+ });
187
+ }
103
188
  });
104
189
  return _this;
105
190
  }
@@ -107,63 +192,62 @@ var PolComponent = /*#__PURE__*/function (_React$Component) {
107
192
  (0, _createClass2["default"])(PolComponent, [{
108
193
  key: "render",
109
194
  value: function render() {
110
- var _this$props3 = this.props,
111
- classes = _this$props3.classes,
112
- correct = _this$props3.correct,
113
- hotspotColor = _this$props3.hotspotColor,
114
- outlineColor = _this$props3.outlineColor,
115
- _this$props3$strokeWi = _this$props3.strokeWidth,
116
- strokeWidth = _this$props3$strokeWi === void 0 ? 5 : _this$props3$strokeWi;
195
+ var _this2 = this;
196
+
197
+ var _this$props4 = this.props,
198
+ classes = _this$props4.classes,
199
+ correct = _this$props4.correct,
200
+ hotspotColor = _this$props4.hotspotColor,
201
+ outlineColor = _this$props4.outlineColor,
202
+ _this$props4$strokeWi = _this$props4.strokeWidth,
203
+ strokeWidth = _this$props4$strokeWi === void 0 ? 5 : _this$props4$strokeWi;
117
204
  var _this$state = this.state,
118
205
  points = _this$state.points,
119
206
  x = _this$state.x,
120
- y = _this$state.y;
121
- return /*#__PURE__*/_react["default"].createElement(_reactKonva.Group, null, /*#__PURE__*/_react["default"].createElement(_reactKonva.Line, {
207
+ y = _this$state.y,
208
+ hovered = _this$state.hovered;
209
+ var calculatedStrokeWidth = correct ? strokeWidth : hovered ? 1 : 0;
210
+ var calculatedStroke = correct ? outlineColor : hovered ? HOVERED_COLOR : '';
211
+ return /*#__PURE__*/_react["default"].createElement(_reactKonva.Group, {
212
+ classes: classes.group,
213
+ onMouseLeave: this.handleMouseLeave,
214
+ onMouseEnter: this.handleMouseEnter
215
+ }, /*#__PURE__*/_react["default"].createElement(_reactKonva.Line, {
122
216
  classes: classes.base,
123
- points: points,
217
+ points: this.serialize(points),
124
218
  closed: true,
125
219
  fill: hotspotColor,
126
220
  onClick: this.handleClick,
127
221
  onTap: this.handleClick,
128
222
  draggable: true,
129
- stroke: outlineColor,
130
- strokeWidth: correct ? strokeWidth : 0,
131
- onMouseLeave: this.handleMouseLeave,
132
- onMouseEnter: this.handleMouseEnter,
133
- onDragEnd: this.handleOnDragEnd,
223
+ stroke: calculatedStroke,
224
+ strokeWidth: calculatedStrokeWidth,
225
+ onDragMove: this.handleOnDragEnd,
226
+ onDragEnd: function onDragEnd(e) {
227
+ return _this2.handleOnDragEnd(e, true);
228
+ },
134
229
  x: x,
135
230
  y: y
231
+ }), hovered && points.map(function (point, index) {
232
+ return /*#__PURE__*/_react["default"].createElement(_reactKonva.Circle, {
233
+ key: index,
234
+ className: classes.circle,
235
+ x: point.x,
236
+ y: point.y,
237
+ radius: 5,
238
+ fill: 'white',
239
+ stroke: HOVERED_COLOR,
240
+ strokeWidth: 1,
241
+ draggable: true,
242
+ onDragMove: function onDragMove(e) {
243
+ _this2.handleOnDragVertex(e, index);
244
+ },
245
+ onDragEnd: function onDragEnd(e) {
246
+ _this2.handleOnDragVertex(e, index, true);
247
+ }
248
+ });
136
249
  }));
137
250
  }
138
- }], [{
139
- key: "getDerivedStateFromProps",
140
- value: function getDerivedStateFromProps(nextProps) {
141
- var points = nextProps.points;
142
- var xList = points.map(function (p) {
143
- return p.x;
144
- });
145
- var yList = points.map(function (p) {
146
- return p.y;
147
- });
148
- var xOffset = Math.min.apply(Math, (0, _toConsumableArray2["default"])(xList));
149
- var yOffset = Math.max.apply(Math, (0, _toConsumableArray2["default"])(yList));
150
-
151
- if (points.length) {
152
- return {
153
- x: xOffset,
154
- y: yOffset,
155
- points: points.reduce(function (acc, point) {
156
- return acc.concat([point.x - xOffset, point.y - yOffset]);
157
- }, [])
158
- };
159
- }
160
-
161
- return {
162
- x: 0,
163
- y: 0,
164
- points: []
165
- };
166
- }
167
251
  }]);
168
252
  return PolComponent;
169
253
  }(_react["default"].Component);
@@ -173,6 +257,12 @@ var styles = function styles() {
173
257
  base: {
174
258
  cursor: 'pointer',
175
259
  opacity: 0.5
260
+ },
261
+ circle: {
262
+ opacity: 4
263
+ },
264
+ group: {
265
+ padding: '12px'
176
266
  }
177
267
  };
178
268
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hotspot-polygon.jsx"],"names":["PolComponent","e","points","props","xList","map","p","x","yList","y","width","Math","max","min","height","isDrawing","onClick","id","cancelBubble","document","body","style","cursor","onDragEnd","target","state","newPoints","reduce","acc","currentPointCoordinate","index","length","push","classes","correct","hotspotColor","outlineColor","strokeWidth","base","handleClick","handleMouseLeave","handleMouseEnter","handleOnDragEnd","nextProps","xOffset","yOffset","point","concat","React","Component","styles","opacity","propTypes","PropTypes","object","isRequired","bool","string","func","arrayOf","shape","number","defaultProps"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;IAEMA,Y;;;;;;;;;;;;;;;8FA0BI,E;oGAEM,UAACC,CAAD,EAAO;AACnB,UAAQC,MAAR,GAAmB,MAAKC,KAAxB,CAAQD,MAAR;AACA,UAAME,KAAK,GAAGF,MAAM,CAACG,GAAP,CAAW,UAACC,CAAD;AAAA,eAAOA,CAAC,CAACC,CAAT;AAAA,OAAX,CAAd;AACA,UAAMC,KAAK,GAAGN,MAAM,CAACG,GAAP,CAAW,UAACC,CAAD;AAAA,eAAOA,CAAC,CAACG,CAAT;AAAA,OAAX,CAAd;AAEA,UAAMC,KAAK,GAAGC,IAAI,CAACC,GAAL,OAAAD,IAAI,sCAAQP,KAAR,EAAJ,GAAqBO,IAAI,CAACE,GAAL,OAAAF,IAAI,sCAAQP,KAAR,EAAvC;AACA,UAAMU,MAAM,GAAGH,IAAI,CAACC,GAAL,OAAAD,IAAI,sCAAQH,KAAR,EAAJ,GAAqBG,IAAI,CAACE,GAAL,OAAAF,IAAI,sCAAQH,KAAR,EAAxC;AAEA,wBAAmC,MAAKL,KAAxC;AAAA,UAAQY,SAAR,eAAQA,SAAR;AAAA,UAAmBC,OAAnB,eAAmBA,OAAnB;AAAA,UAA4BC,EAA5B,eAA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AAiBF,UALR;AAMvBvC,EAAAA,OAAO,EAAEqC,sBAAUK,IAAV,CAAeH,UAND;AAOvBhC,EAAAA,SAAS,EAAE8B,sBAAUK,IAAV,CAAeH,UAPH;AAQvBnB,EAAAA,YAAY,EAAEiB,sBAAUI,MAAV,CAAiBF,UARR;AASvBrD,EAAAA,MAAM,EAAEmD,sBAAUM,OAAV,CACNN,sBAAUO,KAAV,CAAgB;AACdrD,IAAAA,CAAC,EAAE8C,sBAAUQ,MADC;AAEdpD,IAAAA,CAAC,EAAE4C,sBAAUQ;AAFC,GAAhB,CADM,EAKNN,UAdqB;AAevBlB,EAAAA,WAAW,EAAEgB,sBAAUQ;AAfA,CAAzB;AAkBA7D,YAAY,CAAC8D,YAAb,GAA4B;AAC1B5B,EAAAA,OAAO,EAAE;AADiB,CAA5B;;eAIe,uBAAWgB,MAAX,EAAmBlD,YAAnB,C","sourcesContent":["import React from 'react';\nimport PropTypes from 'prop-types';\nimport { Group, Line } from 'react-konva';\nimport { withStyles } from '@material-ui/core/styles/index';\n\nclass PolComponent extends React.Component {\n static getDerivedStateFromProps(nextProps) {\n const { points } = nextProps;\n const xList = points.map((p) => p.x);\n const yList = points.map((p) => p.y);\n\n const xOffset = Math.min(...xList);\n const yOffset = Math.max(...yList);\n\n if (points.length) {\n return {\n x: xOffset,\n y: yOffset,\n points: points.reduce((acc, point) => {\n return acc.concat([point.x - xOffset, point.y - yOffset]);\n }, []),\n };\n }\n\n return {\n x: 0,\n y: 0,\n points: [],\n };\n }\n\n state = {};\n\n handleClick = (e) => {\n const { points } = this.props;\n const xList = points.map((p) => p.x);\n const yList = points.map((p) => p.y);\n\n const width = Math.max(...xList) - Math.min(...xList);\n const height = Math.max(...yList) - Math.min(...yList);\n\n const { isDrawing, onClick, id } = this.props;\n\n if (width < 0 && height < 0 && isDrawing) {\n return;\n }\n\n e.cancelBubble = true;\n onClick(id);\n };\n\n handleMouseEnter = () => {\n document.body.style.cursor = 'pointer';\n };\n\n handleMouseLeave = () => {\n document.body.style.cursor = 'default';\n };\n\n handleOnDragEnd = (e) => {\n const { onDragEnd, id } = this.props;\n const points = e.target.points() || this.state.points;\n\n const newPoints = points.reduce((acc, currentPointCoordinate, index) => {\n if (index % 2 === 0 && index + 1 < points.length) {\n acc.push({\n x: currentPointCoordinate + e.target.x(),\n y: points[index + 1] + e.target.y(),\n });\n\n return acc;\n } else {\n return acc;\n }\n }, []);\n\n onDragEnd(id, {\n points: newPoints,\n });\n };\n\n render() {\n const { classes, correct, hotspotColor, outlineColor, strokeWidth = 5 } = this.props;\n const { points, x, y } = this.state;\n\n return (\n <Group>\n <Line\n classes={classes.base}\n points={points}\n closed={true}\n fill={hotspotColor}\n onClick={this.handleClick}\n onTap={this.handleClick}\n draggable\n stroke={outlineColor}\n strokeWidth={correct ? strokeWidth : 0}\n onMouseLeave={this.handleMouseLeave}\n onMouseEnter={this.handleMouseEnter}\n onDragEnd={this.handleOnDragEnd}\n x={x}\n y={y}\n />\n </Group>\n );\n }\n}\n\nconst styles = () => ({\n base: {\n cursor: 'pointer',\n opacity: 0.5,\n },\n});\n\nPolComponent.propTypes = {\n classes: PropTypes.object.isRequired,\n correct: PropTypes.bool,\n isDrawing: PropTypes.bool.isRequired,\n id: PropTypes.string.isRequired,\n hotspotColor: PropTypes.string.isRequired,\n onClick: PropTypes.func.isRequired,\n onDragEnd: PropTypes.func.isRequired,\n outlineColor: PropTypes.string.isRequired,\n points: PropTypes.arrayOf(\n PropTypes.shape({\n x: PropTypes.number,\n y: PropTypes.number,\n }),\n ).isRequired,\n strokeWidth: PropTypes.number,\n};\n\nPolComponent.defaultProps = {\n correct: false,\n};\n\nexport default withStyles(styles)(PolComponent);\n"],"file":"hotspot-polygon.js"}
1
+ {"version":3,"sources":["../src/hotspot-polygon.jsx"],"names":["HOVERED_COLOR","PolComponent","points","xList","map","p","x","yList","y","Math","min","max","getOffset","xOffset","yOffset","reduce","acc","point","length","hovered","getInitialState","props","e","width","height","isDrawing","onClick","id","cancelBubble","setState","document","body","style","cursor","updateModel","onDragEnd","target","serialize","state","newPoints","currentPointCoordinate","index","changedIndex","classes","correct","hotspotColor","outlineColor","strokeWidth","calculatedStrokeWidth","calculatedStroke","group","handleMouseLeave","handleMouseEnter","base","handleClick","handleOnDragEnd","circle","handleOnDragVertex","React","Component","styles","opacity","padding","propTypes","PropTypes","object","isRequired","bool","string","func","arrayOf","shape","number","defaultProps"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;AAEA,IAAMA,aAAa,GAAG,SAAtB;;IAEMC,Y;;;;;;;;;;;;;;;kGACQ,UAACC,MAAD,EAAY;AACtB,UAAMC,KAAK,GAAGD,MAAM,CAACE,GAAP,CAAW,UAACC,CAAD;AAAA,eAAOA,CAAC,CAACC,CAAT;AAAA,OAAX,CAAd;AACA,UAAMC,KAAK,GAAGL,MAAM,CAACE,GAAP,CAAW,UAACC,CAAD;AAAA,eAAOA,CAAC,CAACG,CAAT;AAAA,OAAX,CAAd;AAEA,aAAO;AACLF,QAAAA,CAAC,EAAEG,IAAI,CAACC,GAAL,OAAAD,IAAI,sCAAQN,KAAR,EADF;AAELK,QAAAA,CAAC,EAAEC,IAAI,CAACE,GAAL,OAAAF,IAAI,sCAAQF,KAAR;AAFF,OAAP;AAID,K;kGAEW,UAACL,MAAD,EAAY;AACtB,4BAAmC,MAAKU,SAAL,CAAeV,MAAf,CAAnC;AAAA,UAAWW,OAAX,mBAAQP,CAAR;AAAA,UAAuBQ,OAAvB,mBAAoBN,CAApB;;AAEA,aAAON,MAAM,CAACa,MAAP,CAAc,UAACC,GAAD,EAAMC,KAAN;AAAA,6DAAoBD,GAApB,IAAyBC,KAAK,CAACX,CAAN,GAAUO,OAAnC,EAA4CI,KAAK,CAACT,CAAN,GAAUM,OAAtD;AAAA,OAAd,EAA8E,EAA9E,CAAP;AACD,K;wGAEiB,UAACZ,MAAD,EAAY;AAC5B,UAAIA,MAAM,CAACgB,MAAX,EAAm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OvB/B,EAAAA,SAAS,EAAE6B,sBAAUK,IAAV,CAAeH,UAPH;AAQvBpB,EAAAA,YAAY,EAAEkB,sBAAUI,MAAV,CAAiBF,UARR;AASvBhE,EAAAA,MAAM,EAAE8D,sBAAUM,OAAV,CACNN,sBAAUO,KAAV,CAAgB;AACdjE,IAAAA,CAAC,EAAE0D,sBAAUQ,MADC;AAEdhE,IAAAA,CAAC,EAAEwD,sBAAUQ;AAFC,GAAhB,CADM,EAKNN,UAdqB;AAevBnB,EAAAA,WAAW,EAAEiB,sBAAUQ;AAfA,CAAzB;AAkBAvE,YAAY,CAACwE,YAAb,GAA4B;AAC1B7B,EAAAA,OAAO,EAAE;AADiB,CAA5B;;eAIe,uBAAWgB,MAAX,EAAmB3D,YAAnB,C","sourcesContent":["import React from 'react';\nimport PropTypes from 'prop-types';\nimport { Group, Line, Circle } from 'react-konva';\nimport { withStyles } from '@material-ui/core/styles/index';\n\nconst HOVERED_COLOR = '#00BFFF';\n\nclass PolComponent extends React.Component {\n getOffset = (points) => {\n const xList = points.map((p) => p.x);\n const yList = points.map((p) => p.y);\n\n return {\n x: Math.min(...xList),\n y: Math.max(...yList),\n };\n };\n\n serialize = (points) => {\n const { x: xOffset, y: yOffset } = this.getOffset(points);\n\n return points.reduce((acc, point) => [...acc, point.x - xOffset, point.y - yOffset], []);\n };\n\n getInitialState = (points) => {\n if (points.length) {\n const { x, y } = this.getOffset(points);\n\n return {\n x,\n y,\n points,\n };\n }\n\n return {\n x: 0,\n y: 0,\n points: [],\n };\n };\n\n state = {\n hovered: false,\n ...this.getInitialState(this.props.points),\n };\n\n handleClick = (e) => {\n const { points } = this.props;\n const xList = points.map((p) => p.x);\n const yList = points.map((p) => p.y);\n\n const width = Math.max(...xList) - Math.min(...xList);\n const height = Math.max(...yList) - Math.min(...yList);\n\n const { isDrawing, onClick, id } = this.props;\n\n if (width < 0 && height < 0 && isDrawing) {\n return;\n }\n\n e.cancelBubble = true;\n onClick(id);\n };\n\n handleMouseEnter = () => {\n this.setState({ hovered: true });\n document.body.style.cursor = 'pointer';\n };\n\n handleMouseLeave = () => {\n this.setState({ hovered: false });\n document.body.style.cursor = 'default';\n };\n\n handleOnDragEnd = (e, updateModel = false) => {\n const { onDragEnd, id } = this.props;\n const points = e.target.points() || this.serialize(this.state.points);\n\n const newPoints = points.reduce((acc, currentPointCoordinate, index) => {\n if (index % 2 === 0 && index + 1 < points.length) {\n return [\n ...acc,\n {\n x: currentPointCoordinate + e.target.x(),\n y: points[index + 1] + e.target.y(),\n },\n ];\n }\n\n return acc;\n }, []);\n\n this.setState({ points: newPoints, ...this.getOffset(newPoints) });\n\n if (updateModel) {\n onDragEnd(id, { points: newPoints });\n }\n };\n\n handleOnDragVertex = (e, changedIndex, updateModel) => {\n const { onDragEnd, id } = this.props;\n const { points } = this.state;\n\n const newPoints = points.map((point, index) =>\n index === changedIndex ? { x: e.target.x(), y: e.target.y() } : point,\n );\n\n this.setState({ points: newPoints, ...this.getOffset(newPoints) });\n\n if (updateModel) {\n onDragEnd(id, { points: newPoints });\n }\n };\n\n render() {\n const { classes, correct, hotspotColor, outlineColor, strokeWidth = 5 } = this.props;\n const { points, x, y, hovered } = this.state;\n\n const calculatedStrokeWidth = correct ? strokeWidth : hovered ? 1 : 0;\n const calculatedStroke = correct ? outlineColor : hovered ? HOVERED_COLOR : '';\n\n return (\n <Group classes={classes.group} onMouseLeave={this.handleMouseLeave} onMouseEnter={this.handleMouseEnter}>\n <Line\n classes={classes.base}\n points={this.serialize(points)}\n closed={true}\n fill={hotspotColor}\n onClick={this.handleClick}\n onTap={this.handleClick}\n draggable\n stroke={calculatedStroke}\n strokeWidth={calculatedStrokeWidth}\n onDragMove={this.handleOnDragEnd}\n onDragEnd={(e) => this.handleOnDragEnd(e, true)}\n x={x}\n y={y}\n />\n\n {hovered &&\n points.map((point, index) => (\n <Circle\n key={index}\n className={classes.circle}\n x={point.x}\n y={point.y}\n radius={5}\n fill={'white'}\n stroke={HOVERED_COLOR}\n strokeWidth={1}\n draggable\n onDragMove={(e) => {\n this.handleOnDragVertex(e, index);\n }}\n onDragEnd={(e) => {\n this.handleOnDragVertex(e, index, true);\n }}\n />\n ))}\n </Group>\n );\n }\n}\n\nconst styles = () => ({\n base: {\n cursor: 'pointer',\n opacity: 0.5,\n },\n circle: {\n opacity: 4,\n },\n group: {\n padding: '12px',\n },\n});\n\nPolComponent.propTypes = {\n classes: PropTypes.object.isRequired,\n correct: PropTypes.bool,\n isDrawing: PropTypes.bool.isRequired,\n id: PropTypes.string.isRequired,\n hotspotColor: PropTypes.string.isRequired,\n onClick: PropTypes.func.isRequired,\n onDragEnd: PropTypes.func.isRequired,\n outlineColor: PropTypes.string.isRequired,\n points: PropTypes.arrayOf(\n PropTypes.shape({\n x: PropTypes.number,\n y: PropTypes.number,\n }),\n ).isRequired,\n strokeWidth: PropTypes.number,\n};\n\nPolComponent.defaultProps = {\n correct: false,\n};\n\nexport default withStyles(styles)(PolComponent);\n"],"file":"hotspot-polygon.js"}
@@ -38,16 +38,11 @@ var RectComponent = /*#__PURE__*/function (_React$Component) {
38
38
 
39
39
  var _super = _createSuper(RectComponent);
40
40
 
41
- function RectComponent() {
41
+ function RectComponent(props) {
42
42
  var _this;
43
43
 
44
44
  (0, _classCallCheck2["default"])(this, RectComponent);
45
-
46
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
47
- args[_key] = arguments[_key];
48
- }
49
-
50
- _this = _super.call.apply(_super, [this].concat(args));
45
+ _this = _super.call(this, props);
51
46
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleClick", function (e) {
52
47
  var _this$props = _this.props,
53
48
  width = _this$props.width,
@@ -65,8 +60,20 @@ var RectComponent = /*#__PURE__*/function (_React$Component) {
65
60
  });
66
61
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseEnter", function () {
67
62
  document.body.style.cursor = 'pointer';
63
+
64
+ _this.setState({
65
+ hovered: true
66
+ });
67
+
68
+ _this.trRef.current.setNode(_this.shapeRef.current);
69
+
70
+ _this.trRef.current.getLayer().batchDraw();
68
71
  });
69
72
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleMouseLeave", function () {
73
+ _this.setState({
74
+ hovered: false
75
+ });
76
+
70
77
  document.body.style.cursor = 'default';
71
78
  });
72
79
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleOnDragEnd", function (e) {
@@ -78,25 +85,57 @@ var RectComponent = /*#__PURE__*/function (_React$Component) {
78
85
  y: e.target.y()
79
86
  });
80
87
  });
88
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onResizeEnd", function () {
89
+ var _this$props3 = _this.props,
90
+ onDragEnd = _this$props3.onDragEnd,
91
+ id = _this$props3.id; // transformer is changing scale of the node
92
+ // and NOT its width or height
93
+ // but in the store we have only width and height
94
+ // to match the data better we will reset scale on transform end
95
+
96
+ var node = _this.shapeRef.current;
97
+ var scaleX = node.scaleX();
98
+ var scaleY = node.scaleY(); // we will reset it back
99
+
100
+ node.scaleX(1);
101
+ node.scaleY(1);
102
+ onDragEnd(id, {
103
+ x: node.x(),
104
+ y: node.y(),
105
+ // set minimal value
106
+ width: Math.max(5, node.width() * scaleX),
107
+ height: Math.max(node.height() * scaleY)
108
+ });
109
+ });
110
+ _this.state = {
111
+ hovered: false
112
+ };
113
+ _this.shapeRef = /*#__PURE__*/_react["default"].createRef();
114
+ _this.trRef = /*#__PURE__*/_react["default"].createRef();
81
115
  return _this;
82
116
  }
83
117
 
84
118
  (0, _createClass2["default"])(RectComponent, [{
85
119
  key: "render",
86
120
  value: function render() {
87
- var _this$props3 = this.props,
88
- classes = _this$props3.classes,
89
- correct = _this$props3.correct,
90
- height = _this$props3.height,
91
- hotspotColor = _this$props3.hotspotColor,
92
- outlineColor = _this$props3.outlineColor,
93
- width = _this$props3.width,
94
- x = _this$props3.x,
95
- y = _this$props3.y,
96
- _this$props3$strokeWi = _this$props3.strokeWidth,
97
- strokeWidth = _this$props3$strokeWi === void 0 ? 5 : _this$props3$strokeWi;
98
- return /*#__PURE__*/_react["default"].createElement(_reactKonva.Group, null, /*#__PURE__*/_react["default"].createElement(_reactKonva.Rect, {
121
+ var _this$props4 = this.props,
122
+ classes = _this$props4.classes,
123
+ correct = _this$props4.correct,
124
+ height = _this$props4.height,
125
+ hotspotColor = _this$props4.hotspotColor,
126
+ outlineColor = _this$props4.outlineColor,
127
+ width = _this$props4.width,
128
+ x = _this$props4.x,
129
+ y = _this$props4.y,
130
+ _this$props4$strokeWi = _this$props4.strokeWidth,
131
+ strokeWidth = _this$props4$strokeWi === void 0 ? 5 : _this$props4$strokeWi;
132
+ return /*#__PURE__*/_react["default"].createElement(_reactKonva.Group, {
133
+ classes: classes.group,
134
+ onMouseLeave: this.handleMouseLeave,
135
+ onMouseEnter: this.handleMouseEnter
136
+ }, /*#__PURE__*/_react["default"].createElement(_reactKonva.Rect, {
99
137
  classes: classes.base,
138
+ ref: this.shapeRef,
100
139
  width: width,
101
140
  height: height,
102
141
  fill: hotspotColor,
@@ -105,11 +144,21 @@ var RectComponent = /*#__PURE__*/function (_React$Component) {
105
144
  draggable: true,
106
145
  stroke: outlineColor,
107
146
  strokeWidth: correct ? strokeWidth : 0,
108
- onMouseLeave: this.handleMouseLeave,
109
- onMouseEnter: this.handleMouseEnter,
110
147
  onDragEnd: this.handleOnDragEnd,
148
+ onTransformEnd: this.onResizeEnd,
111
149
  x: x,
112
150
  y: y
151
+ }), this.state.hovered && /*#__PURE__*/_react["default"].createElement(_reactKonva.Transformer, {
152
+ ref: this.trRef,
153
+ rotateEnabled: false,
154
+ boundBoxFunc: function boundBoxFunc(oldBox, newBox) {
155
+ // limit resize
156
+ if (newBox.width < 10 || newBox.height < 10) {
157
+ return oldBox;
158
+ }
159
+
160
+ return newBox;
161
+ }
113
162
  }));
114
163
  }
115
164
  }]);
@@ -121,6 +170,9 @@ var styles = function styles() {
121
170
  base: {
122
171
  cursor: 'pointer',
123
172
  opacity: 0.5
173
+ },
174
+ group: {
175
+ padding: '12px'
124
176
  }
125
177
  };
126
178
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hotspot-rectangle.jsx"],"names":["RectComponent","e","props","width","height","isDrawing","onClick","id","cancelBubble","document","body","style","cursor","onDragEnd","x","target","y","classes","correct","hotspotColor","outlineColor","strokeWidth","base","handleClick","handleMouseLeave","handleMouseEnter","handleOnDragEnd","React","Component","styles","opacity","propTypes","PropTypes","object","isRequired","bool","string","number","func","defaultProps"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;IAEMA,a;;;;;;;;;;;;;;;oGACU,UAACC,CAAD,EAAO;AACnB,wBAAkD,MAAKC,KAAvD;AAAA,UAAQC,KAAR,eAAQA,KAAR;AAAA,UAAeC,MAAf,eAAeA,MAAf;AAAA,UAAuBC,SAAvB,eAAuBA,SAAvB;AAAA,UAAkCC,OAAlC,eAAkCA,OAAlC;AAAA,UAA2CC,EAA3C,eAA2CA,EAA3C;;AACA,UAAIJ,KAAK,GAAG,CAAR,IAAaC,MAAM,GAAG,CAAtB,IAA2BC,SAA/B,EAA0C;AACxC;AACD;;AACDJ,MAAAA,CAAC,CAACO,YAAF,GAAiB,IAAjB;AACAF,MAAAA,OAAO,CAACC,EAAD,CAAP;AACD,K;yGAEkB,YAAM;AACvBE,MAAAA,QAAQ,CAACC,IAAT,CAAcC,KAAd,CAAoBC,MAApB,GAA6B,SAA7B;AACD,K;yGAEkB,YAAM;AACvBH,MAAAA,QAAQ,CAACC,IAAT,CAAcC,KAAd,CAAoBC,MAApB,GAA6B,SAA7B;AACD,K;wGAEiB,UAACX,CAAD,EAAO;AACvB,yBAA0B,MAAKC,KAA/B;AAAA,UAAQW,SAAR,gBAAQA,SAAR;AAAA,UAAmBN,EAAnB,gBAAmBA,EAAnB;AAEAM,MAAAA,SAAS,CAACN,EAAD,EAAK;AACZO,QAAAA,CAAC,EAAEb,CAAC,C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A,OAAO,EAAE;AAFL;AADc,GAAP;AAAA,CAAf;;AAOA9B,aAAa,CAAC+B,SAAd,GAA0B;AACxBd,EAAAA,OAAO,EAAEe,sBAAUC,MAAV,CAAiBC,UADF;AAExBhB,EAAAA,OAAO,EAAEc,sBAAUG,IAFK;AAGxB9B,EAAAA,SAAS,EAAE2B,sBAAUG,IAAV,CAAeD,UAHF;AAIxB3B,EAAAA,EAAE,EAAEyB,sBAAUI,MAAV,CAAiBF,UAJG;AAKxB9B,EAAAA,MAAM,EAAE4B,sBAAUK,MAAV,CAAiBH,UALD;AAMxBf,EAAAA,YAAY,EAAEa,sBAAUI,MAAV,CAAiBF,UANP;AAOxB5B,EAAAA,OAAO,EAAE0B,sBAAUM,IAAV,CAAeJ,UAPA;AAQxBrB,EAAAA,SAAS,EAAEmB,sBAAUM,IAAV,CAAeJ,UARF;AASxBd,EAAAA,YAAY,EAAEY,sBAAUI,MAAV,CAAiBF,UATP;AAUxB/B,EAAAA,KAAK,EAAE6B,sBAAUK,MAAV,CAAiBH,UAVA;AAWxBpB,EAAAA,CAAC,EAAEkB,sBAAUK,MAAV,CAAiBH,UAXI;AAYxBlB,EAAAA,CAAC,EAAEgB,sBAAUK,MAAV,CAAiBH,UAZI;AAaxBb,EAAAA,WAAW,EAAEW,sBAAUK;AAbC,CAA1B;AAgBArC,aAAa,CAACuC,YAAd,GAA6B;AAC3BrB,EAAAA,OAAO,EAAE;AADkB,CAA7B;;eAIe,uBAAWW,MAAX,EAAmB7B,aAAnB,C","sourcesContent":["import React from 'react';\nimport PropTypes from 'prop-types';\nimport { Rect, Group } from 'react-konva';\nimport { withStyles } from '@material-ui/core/styles/index';\n\nclass RectComponent extends React.Component {\n handleClick = (e) => {\n const { width, height, isDrawing, onClick, id } = this.props;\n if (width < 0 && height < 0 && isDrawing) {\n return;\n }\n e.cancelBubble = true;\n onClick(id);\n };\n\n handleMouseEnter = () => {\n document.body.style.cursor = 'pointer';\n };\n\n handleMouseLeave = () => {\n document.body.style.cursor = 'default';\n };\n\n handleOnDragEnd = (e) => {\n const { onDragEnd, id } = this.props;\n\n onDragEnd(id, {\n x: e.target.x(),\n y: e.target.y(),\n });\n };\n\n render() {\n const { classes, correct, height, hotspotColor, outlineColor, width, x, y, strokeWidth = 5 } = this.props;\n\n return (\n <Group>\n <Rect\n classes={classes.base}\n width={width}\n height={height}\n fill={hotspotColor}\n onClick={this.handleClick}\n onTap={this.handleClick}\n draggable\n stroke={outlineColor}\n strokeWidth={correct ? strokeWidth : 0}\n onMouseLeave={this.handleMouseLeave}\n onMouseEnter={this.handleMouseEnter}\n onDragEnd={this.handleOnDragEnd}\n x={x}\n y={y}\n />\n </Group>\n );\n }\n}\n\nconst styles = () => ({\n base: {\n cursor: 'pointer',\n opacity: 0.5,\n },\n});\n\nRectComponent.propTypes = {\n classes: PropTypes.object.isRequired,\n correct: PropTypes.bool,\n isDrawing: PropTypes.bool.isRequired,\n id: PropTypes.string.isRequired,\n height: PropTypes.number.isRequired,\n hotspotColor: PropTypes.string.isRequired,\n onClick: PropTypes.func.isRequired,\n onDragEnd: PropTypes.func.isRequired,\n outlineColor: PropTypes.string.isRequired,\n width: PropTypes.number.isRequired,\n x: PropTypes.number.isRequired,\n y: PropTypes.number.isRequired,\n strokeWidth: PropTypes.number,\n};\n\nRectComponent.defaultProps = {\n correct: false,\n};\n\nexport default withStyles(styles)(RectComponent);\n"],"file":"hotspot-rectangle.js"}
1
+ {"version":3,"sources":["../src/hotspot-rectangle.jsx"],"names":["RectComponent","props","e","width","height","isDrawing","onClick","id","cancelBubble","document","body","style","cursor","setState","hovered","trRef","current","setNode","shapeRef","getLayer","batchDraw","onDragEnd","x","target","y","node","scaleX","scaleY","Math","max","state","React","createRef","classes","correct","hotspotColor","outlineColor","strokeWidth","group","handleMouseLeave","handleMouseEnter","base","handleClick","handleOnDragEnd","onResizeEnd","oldBox","newBox","Component","styles","opacity","padding","propTypes","PropTypes","object","isRequired","bool","string","number","func","defaultProps"],"mappings":";;;;;;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X,EAAmBhD,aAAnB,C","sourcesContent":["import React from 'react';\nimport PropTypes from 'prop-types';\nimport { Rect, Group, Transformer } from 'react-konva';\nimport { withStyles } from '@material-ui/core/styles/index';\n\nclass RectComponent extends React.Component {\n constructor(props) {\n super(props);\n this.state = {\n hovered: false,\n };\n this.shapeRef = React.createRef();\n this.trRef = React.createRef();\n }\n\n handleClick = (e) => {\n const { width, height, isDrawing, onClick, id } = this.props;\n if (width < 0 && height < 0 && isDrawing) {\n return;\n }\n e.cancelBubble = true;\n onClick(id);\n };\n\n handleMouseEnter = () => {\n document.body.style.cursor = 'pointer';\n this.setState({ hovered: true });\n this.trRef.current.setNode(this.shapeRef.current);\n this.trRef.current.getLayer().batchDraw();\n };\n\n handleMouseLeave = () => {\n this.setState({ hovered: false });\n document.body.style.cursor = 'default';\n };\n\n handleOnDragEnd = (e) => {\n const { onDragEnd, id } = this.props;\n\n onDragEnd(id, {\n x: e.target.x(),\n y: e.target.y(),\n });\n };\n\n onResizeEnd = () => {\n const { onDragEnd, id } = this.props;\n // transformer is changing scale of the node\n // and NOT its width or height\n // but in the store we have only width and height\n // to match the data better we will reset scale on transform end\n const node = this.shapeRef.current;\n const scaleX = node.scaleX();\n const scaleY = node.scaleY();\n\n // we will reset it back\n node.scaleX(1);\n node.scaleY(1);\n onDragEnd(id, {\n x: node.x(),\n y: node.y(),\n // set minimal value\n width: Math.max(5, node.width() * scaleX),\n height: Math.max(node.height() * scaleY),\n });\n };\n\n render() {\n const { classes, correct, height, hotspotColor, outlineColor, width, x, y, strokeWidth = 5 } = this.props;\n\n return (\n <Group classes={classes.group} onMouseLeave={this.handleMouseLeave} onMouseEnter={this.handleMouseEnter}>\n <Rect\n classes={classes.base}\n ref={this.shapeRef}\n width={width}\n height={height}\n fill={hotspotColor}\n onClick={this.handleClick}\n onTap={this.handleClick}\n draggable\n stroke={outlineColor}\n strokeWidth={correct ? strokeWidth : 0}\n onDragEnd={this.handleOnDragEnd}\n onTransformEnd={this.onResizeEnd}\n x={x}\n y={y}\n />\n {this.state.hovered && (\n <Transformer\n ref={this.trRef}\n rotateEnabled={false}\n boundBoxFunc={(oldBox, newBox) => {\n // limit resize\n if (newBox.width < 10 || newBox.height < 10) {\n return oldBox;\n }\n return newBox;\n }}\n />\n )}\n </Group>\n );\n }\n}\n\nconst styles = () => ({\n base: {\n cursor: 'pointer',\n opacity: 0.5,\n },\n\n group: {\n padding: '12px',\n },\n});\n\nRectComponent.propTypes = {\n classes: PropTypes.object.isRequired,\n correct: PropTypes.bool,\n isDrawing: PropTypes.bool.isRequired,\n id: PropTypes.string.isRequired,\n height: PropTypes.number.isRequired,\n hotspotColor: PropTypes.string.isRequired,\n onClick: PropTypes.func.isRequired,\n onDragEnd: PropTypes.func.isRequired,\n outlineColor: PropTypes.string.isRequired,\n width: PropTypes.number.isRequired,\n x: PropTypes.number.isRequired,\n y: PropTypes.number.isRequired,\n strokeWidth: PropTypes.number,\n};\n\nRectComponent.defaultProps = {\n correct: false,\n};\n\nexport default withStyles(styles)(RectComponent);\n"],"file":"hotspot-rectangle.js"}
@@ -10,8 +10,8 @@
10
10
  "@material-ui/core": "^3.9.2",
11
11
  "@material-ui/icons": "^3.0.2",
12
12
  "@pie-framework/pie-configure-events": "^1.3.0",
13
- "@pie-lib/config-ui": "^11.9.7",
14
- "@pie-lib/editable-html": "^10.0.2",
13
+ "@pie-lib/config-ui": "^11.9.8",
14
+ "@pie-lib/editable-html": "^10.0.3",
15
15
  "classnames": "^2.2.6",
16
16
  "debug": "^3.1.0",
17
17
  "konva": "^3.2.4",
@@ -1,24 +1,35 @@
1
1
  import React from 'react';
2
2
  import PropTypes from 'prop-types';
3
- import { Group, Line } from 'react-konva';
3
+ import { Group, Line, Circle } from 'react-konva';
4
4
  import { withStyles } from '@material-ui/core/styles/index';
5
5
 
6
+ const HOVERED_COLOR = '#00BFFF';
7
+
6
8
  class PolComponent extends React.Component {
7
- static getDerivedStateFromProps(nextProps) {
8
- const { points } = nextProps;
9
+ getOffset = (points) => {
9
10
  const xList = points.map((p) => p.x);
10
11
  const yList = points.map((p) => p.y);
11
12
 
12
- const xOffset = Math.min(...xList);
13
- const yOffset = Math.max(...yList);
13
+ return {
14
+ x: Math.min(...xList),
15
+ y: Math.max(...yList),
16
+ };
17
+ };
18
+
19
+ serialize = (points) => {
20
+ const { x: xOffset, y: yOffset } = this.getOffset(points);
21
+
22
+ return points.reduce((acc, point) => [...acc, point.x - xOffset, point.y - yOffset], []);
23
+ };
14
24
 
25
+ getInitialState = (points) => {
15
26
  if (points.length) {
27
+ const { x, y } = this.getOffset(points);
28
+
16
29
  return {
17
- x: xOffset,
18
- y: yOffset,
19
- points: points.reduce((acc, point) => {
20
- return acc.concat([point.x - xOffset, point.y - yOffset]);
21
- }, []),
30
+ x,
31
+ y,
32
+ points,
22
33
  };
23
34
  }
24
35
 
@@ -27,9 +38,12 @@ class PolComponent extends React.Component {
27
38
  y: 0,
28
39
  points: [],
29
40
  };
30
- }
41
+ };
31
42
 
32
- state = {};
43
+ state = {
44
+ hovered: false,
45
+ ...this.getInitialState(this.props.points),
46
+ };
33
47
 
34
48
  handleClick = (e) => {
35
49
  const { points } = this.props;
@@ -50,57 +64,100 @@ class PolComponent extends React.Component {
50
64
  };
51
65
 
52
66
  handleMouseEnter = () => {
67
+ this.setState({ hovered: true });
53
68
  document.body.style.cursor = 'pointer';
54
69
  };
55
70
 
56
71
  handleMouseLeave = () => {
72
+ this.setState({ hovered: false });
57
73
  document.body.style.cursor = 'default';
58
74
  };
59
75
 
60
- handleOnDragEnd = (e) => {
76
+ handleOnDragEnd = (e, updateModel = false) => {
61
77
  const { onDragEnd, id } = this.props;
62
- const points = e.target.points() || this.state.points;
78
+ const points = e.target.points() || this.serialize(this.state.points);
63
79
 
64
80
  const newPoints = points.reduce((acc, currentPointCoordinate, index) => {
65
81
  if (index % 2 === 0 && index + 1 < points.length) {
66
- acc.push({
67
- x: currentPointCoordinate + e.target.x(),
68
- y: points[index + 1] + e.target.y(),
69
- });
70
-
71
- return acc;
72
- } else {
73
- return acc;
82
+ return [
83
+ ...acc,
84
+ {
85
+ x: currentPointCoordinate + e.target.x(),
86
+ y: points[index + 1] + e.target.y(),
87
+ },
88
+ ];
74
89
  }
90
+
91
+ return acc;
75
92
  }, []);
76
93
 
77
- onDragEnd(id, {
78
- points: newPoints,
79
- });
94
+ this.setState({ points: newPoints, ...this.getOffset(newPoints) });
95
+
96
+ if (updateModel) {
97
+ onDragEnd(id, { points: newPoints });
98
+ }
99
+ };
100
+
101
+ handleOnDragVertex = (e, changedIndex, updateModel) => {
102
+ const { onDragEnd, id } = this.props;
103
+ const { points } = this.state;
104
+
105
+ const newPoints = points.map((point, index) =>
106
+ index === changedIndex ? { x: e.target.x(), y: e.target.y() } : point,
107
+ );
108
+
109
+ this.setState({ points: newPoints, ...this.getOffset(newPoints) });
110
+
111
+ if (updateModel) {
112
+ onDragEnd(id, { points: newPoints });
113
+ }
80
114
  };
81
115
 
82
116
  render() {
83
117
  const { classes, correct, hotspotColor, outlineColor, strokeWidth = 5 } = this.props;
84
- const { points, x, y } = this.state;
118
+ const { points, x, y, hovered } = this.state;
119
+
120
+ const calculatedStrokeWidth = correct ? strokeWidth : hovered ? 1 : 0;
121
+ const calculatedStroke = correct ? outlineColor : hovered ? HOVERED_COLOR : '';
85
122
 
86
123
  return (
87
- <Group>
124
+ <Group classes={classes.group} onMouseLeave={this.handleMouseLeave} onMouseEnter={this.handleMouseEnter}>
88
125
  <Line
89
126
  classes={classes.base}
90
- points={points}
127
+ points={this.serialize(points)}
91
128
  closed={true}
92
129
  fill={hotspotColor}
93
130
  onClick={this.handleClick}
94
131
  onTap={this.handleClick}
95
132
  draggable
96
- stroke={outlineColor}
97
- strokeWidth={correct ? strokeWidth : 0}
98
- onMouseLeave={this.handleMouseLeave}
99
- onMouseEnter={this.handleMouseEnter}
100
- onDragEnd={this.handleOnDragEnd}
133
+ stroke={calculatedStroke}
134
+ strokeWidth={calculatedStrokeWidth}
135
+ onDragMove={this.handleOnDragEnd}
136
+ onDragEnd={(e) => this.handleOnDragEnd(e, true)}
101
137
  x={x}
102
138
  y={y}
103
139
  />
140
+
141
+ {hovered &&
142
+ points.map((point, index) => (
143
+ <Circle
144
+ key={index}
145
+ className={classes.circle}
146
+ x={point.x}
147
+ y={point.y}
148
+ radius={5}
149
+ fill={'white'}
150
+ stroke={HOVERED_COLOR}
151
+ strokeWidth={1}
152
+ draggable
153
+ onDragMove={(e) => {
154
+ this.handleOnDragVertex(e, index);
155
+ }}
156
+ onDragEnd={(e) => {
157
+ this.handleOnDragVertex(e, index, true);
158
+ }}
159
+ />
160
+ ))}
104
161
  </Group>
105
162
  );
106
163
  }
@@ -111,6 +168,12 @@ const styles = () => ({
111
168
  cursor: 'pointer',
112
169
  opacity: 0.5,
113
170
  },
171
+ circle: {
172
+ opacity: 4,
173
+ },
174
+ group: {
175
+ padding: '12px',
176
+ },
114
177
  });
115
178
 
116
179
  PolComponent.propTypes = {
@@ -1,9 +1,18 @@
1
1
  import React from 'react';
2
2
  import PropTypes from 'prop-types';
3
- import { Rect, Group } from 'react-konva';
3
+ import { Rect, Group, Transformer } from 'react-konva';
4
4
  import { withStyles } from '@material-ui/core/styles/index';
5
5
 
6
6
  class RectComponent extends React.Component {
7
+ constructor(props) {
8
+ super(props);
9
+ this.state = {
10
+ hovered: false,
11
+ };
12
+ this.shapeRef = React.createRef();
13
+ this.trRef = React.createRef();
14
+ }
15
+
7
16
  handleClick = (e) => {
8
17
  const { width, height, isDrawing, onClick, id } = this.props;
9
18
  if (width < 0 && height < 0 && isDrawing) {
@@ -15,9 +24,13 @@ class RectComponent extends React.Component {
15
24
 
16
25
  handleMouseEnter = () => {
17
26
  document.body.style.cursor = 'pointer';
27
+ this.setState({ hovered: true });
28
+ this.trRef.current.setNode(this.shapeRef.current);
29
+ this.trRef.current.getLayer().batchDraw();
18
30
  };
19
31
 
20
32
  handleMouseLeave = () => {
33
+ this.setState({ hovered: false });
21
34
  document.body.style.cursor = 'default';
22
35
  };
23
36
 
@@ -30,13 +43,36 @@ class RectComponent extends React.Component {
30
43
  });
31
44
  };
32
45
 
46
+ onResizeEnd = () => {
47
+ const { onDragEnd, id } = this.props;
48
+ // transformer is changing scale of the node
49
+ // and NOT its width or height
50
+ // but in the store we have only width and height
51
+ // to match the data better we will reset scale on transform end
52
+ const node = this.shapeRef.current;
53
+ const scaleX = node.scaleX();
54
+ const scaleY = node.scaleY();
55
+
56
+ // we will reset it back
57
+ node.scaleX(1);
58
+ node.scaleY(1);
59
+ onDragEnd(id, {
60
+ x: node.x(),
61
+ y: node.y(),
62
+ // set minimal value
63
+ width: Math.max(5, node.width() * scaleX),
64
+ height: Math.max(node.height() * scaleY),
65
+ });
66
+ };
67
+
33
68
  render() {
34
69
  const { classes, correct, height, hotspotColor, outlineColor, width, x, y, strokeWidth = 5 } = this.props;
35
70
 
36
71
  return (
37
- <Group>
72
+ <Group classes={classes.group} onMouseLeave={this.handleMouseLeave} onMouseEnter={this.handleMouseEnter}>
38
73
  <Rect
39
74
  classes={classes.base}
75
+ ref={this.shapeRef}
40
76
  width={width}
41
77
  height={height}
42
78
  fill={hotspotColor}
@@ -45,12 +81,24 @@ class RectComponent extends React.Component {
45
81
  draggable
46
82
  stroke={outlineColor}
47
83
  strokeWidth={correct ? strokeWidth : 0}
48
- onMouseLeave={this.handleMouseLeave}
49
- onMouseEnter={this.handleMouseEnter}
50
84
  onDragEnd={this.handleOnDragEnd}
85
+ onTransformEnd={this.onResizeEnd}
51
86
  x={x}
52
87
  y={y}
53
88
  />
89
+ {this.state.hovered && (
90
+ <Transformer
91
+ ref={this.trRef}
92
+ rotateEnabled={false}
93
+ boundBoxFunc={(oldBox, newBox) => {
94
+ // limit resize
95
+ if (newBox.width < 10 || newBox.height < 10) {
96
+ return oldBox;
97
+ }
98
+ return newBox;
99
+ }}
100
+ />
101
+ )}
54
102
  </Group>
55
103
  );
56
104
  }
@@ -61,6 +109,10 @@ const styles = () => ({
61
109
  cursor: 'pointer',
62
110
  opacity: 0.5,
63
111
  },
112
+
113
+ group: {
114
+ padding: '12px',
115
+ },
64
116
  });
65
117
 
66
118
  RectComponent.propTypes = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-element/hotspot",
3
- "version": "5.15.0",
3
+ "version": "5.15.1-next.6+516922dd4",
4
4
  "description": "",
5
5
  "repository": "pie-framework/pie-elements",
6
6
  "publishConfig": {
@@ -22,7 +22,7 @@
22
22
  },
23
23
  "author": "pie framework developers",
24
24
  "license": "ISC",
25
- "gitHead": "601ddbccecfff533d0670698fe99ce91759165fe",
25
+ "gitHead": "516922dd4bda626da213aaa2c2ea0fde17acedc0",
26
26
  "scripts": {
27
27
  "postpublish": "../../scripts/postpublish"
28
28
  },