redhotmagma-graphics-editor 1.58.4 → 1.58.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.
- package/CanvasAdapters/PaperJs/Canvas.js +7 -9
- package/CanvasAdapters/PaperJs/Utils/__tests__/hitTestPick.test.js +393 -0
- package/CanvasAdapters/PaperJs/Utils/hitTestPick.js +208 -0
- package/Components/InlineToolbox/InlineToolbox.js +38 -1
- package/package.json +1 -1
- package/src/CanvasAdapters/PaperJs/Canvas.ts +9 -10
- package/src/CanvasAdapters/PaperJs/Utils/__tests__/hitTestPick.test.ts +291 -0
- package/src/CanvasAdapters/PaperJs/Utils/hitTestPick.ts +193 -0
- package/src/Components/InlineToolbox/InlineToolbox.js +39 -1
|
@@ -15,6 +15,7 @@ var _TextToSVGRepository = require("./Utils/TextToSVGRepository");
|
|
|
15
15
|
var _HTML2Paper = require("./Utils/HTML2Paper");
|
|
16
16
|
var _SVGfix = require("./Utils/SVGfix");
|
|
17
17
|
var _UseColorLayer = require("./Utils/UseColorLayer");
|
|
18
|
+
var _hitTestPick = require("./Utils/hitTestPick");
|
|
18
19
|
var _excluded = ["type"];
|
|
19
20
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
20
21
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
@@ -385,17 +386,14 @@ var Canvas = exports.Canvas = /*#__PURE__*/function (_AbstractCanvas) {
|
|
|
385
386
|
key: "onMouseDown",
|
|
386
387
|
value: function onMouseDown(event) {
|
|
387
388
|
var point = event.point;
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
if (this.selectedObject && wo !== this.selectedObject) {
|
|
389
|
+
// the design area mask clips the layer, so its invisible part must not be pickable
|
|
390
|
+
var wrapperAtPoint = (0, _hitTestPick.isPointInLayerClip)(this._project.layers.designerLayer, point) ? (0, _hitTestPick.findWrapperObjectAtPoint)(this.objects, point) : null;
|
|
391
|
+
if (wrapperAtPoint) {
|
|
392
|
+
if (this.selectedObject && wrapperAtPoint !== this.selectedObject) {
|
|
393
393
|
this.clearSelection();
|
|
394
394
|
}
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
wo.grabbed = true;
|
|
398
|
-
}
|
|
395
|
+
wrapperAtPoint.selected = true;
|
|
396
|
+
wrapperAtPoint.grabbed = true;
|
|
399
397
|
} else if (this.selectedObject) {
|
|
400
398
|
this.clearSelection();
|
|
401
399
|
}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _hitTestPick = require("../hitTestPick");
|
|
4
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
5
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
6
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
7
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
8
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
9
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
10
|
+
var point = function point(x, y) {
|
|
11
|
+
return {
|
|
12
|
+
x: x,
|
|
13
|
+
y: y
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Fake 1x1 probe canvas: reports the alpha of the source pixel that was drawn into it. */
|
|
18
|
+
var probe = {
|
|
19
|
+
image: null,
|
|
20
|
+
x: 0,
|
|
21
|
+
y: 0
|
|
22
|
+
};
|
|
23
|
+
var probeContext = {
|
|
24
|
+
clearRect: jest.fn(),
|
|
25
|
+
drawImage: jest.fn(function (image, sx, sy) {
|
|
26
|
+
Object.assign(probe, {
|
|
27
|
+
image: image,
|
|
28
|
+
x: sx,
|
|
29
|
+
y: sy
|
|
30
|
+
});
|
|
31
|
+
}),
|
|
32
|
+
getImageData: jest.fn(function () {
|
|
33
|
+
return {
|
|
34
|
+
data: [0, 0, 0, probe.image.alphaAt(probe.x, probe.y)]
|
|
35
|
+
};
|
|
36
|
+
})
|
|
37
|
+
};
|
|
38
|
+
var createElement = document.createElement.bind(document);
|
|
39
|
+
beforeAll(function () {
|
|
40
|
+
jest.spyOn(document, 'createElement').mockReturnValue({
|
|
41
|
+
getContext: function getContext() {
|
|
42
|
+
return probeContext;
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
beforeEach(function () {
|
|
47
|
+
jest.clearAllMocks();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/** 10x10 raster centred on the origin; the pixel alpha is sampled from its source image, not via getPixel. */
|
|
51
|
+
var makeRaster = function makeRaster(alphaAt) {
|
|
52
|
+
var overrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
53
|
+
return _objectSpread({
|
|
54
|
+
bounds: {
|
|
55
|
+
contains: function contains(p) {
|
|
56
|
+
return p.x >= -5 && p.x <= 5 && p.y >= -5 && p.y <= 5;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
loaded: true,
|
|
60
|
+
visible: true,
|
|
61
|
+
size: {
|
|
62
|
+
width: 10,
|
|
63
|
+
height: 10
|
|
64
|
+
},
|
|
65
|
+
image: {
|
|
66
|
+
naturalWidth: 10,
|
|
67
|
+
naturalHeight: 10,
|
|
68
|
+
alphaAt: alphaAt
|
|
69
|
+
},
|
|
70
|
+
globalToLocal: function globalToLocal(p) {
|
|
71
|
+
return p;
|
|
72
|
+
},
|
|
73
|
+
getPixel: jest.fn()
|
|
74
|
+
}, overrides);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** Group holding a placeholder rect (always hit) and a PointText whose ink is hit or not. */
|
|
78
|
+
var makeText = function makeText(inkHit) {
|
|
79
|
+
var pointText = {
|
|
80
|
+
className: 'PointText',
|
|
81
|
+
content: 'A',
|
|
82
|
+
view: null,
|
|
83
|
+
data: {},
|
|
84
|
+
hitTest: function hitTest() {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var wrapped = {
|
|
89
|
+
className: 'Group',
|
|
90
|
+
data: {},
|
|
91
|
+
children: [{
|
|
92
|
+
className: 'Shape',
|
|
93
|
+
data: {
|
|
94
|
+
placeholder: true
|
|
95
|
+
},
|
|
96
|
+
hitTest: function hitTest() {
|
|
97
|
+
return {
|
|
98
|
+
type: 'fill'
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}, pointText],
|
|
102
|
+
hitTest: function hitTest() {
|
|
103
|
+
return {
|
|
104
|
+
type: 'fill'
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
if (!inkHit) {
|
|
109
|
+
pointText.content = ' ';
|
|
110
|
+
}
|
|
111
|
+
return wrapped;
|
|
112
|
+
};
|
|
113
|
+
describe('hitTestPick', function () {
|
|
114
|
+
describe('isOpaqueRasterPixel', function () {
|
|
115
|
+
test('is false outside the raster bounds', function () {
|
|
116
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(makeRaster(function () {
|
|
117
|
+
return 255;
|
|
118
|
+
}), point(20, 20))).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
test('reads the pixel at the click position in image coordinates', function () {
|
|
121
|
+
var raster = makeRaster(function () {
|
|
122
|
+
return 255;
|
|
123
|
+
});
|
|
124
|
+
(0, _hitTestPick.isOpaqueRasterPixel)(raster, point(-3.5, 2.5));
|
|
125
|
+
expect(probeContext.drawImage).toHaveBeenCalledWith(raster.image, 1, 7, 1, 1, 0, 0, 1, 1);
|
|
126
|
+
});
|
|
127
|
+
test('never touches the raster pixel data, which would replace its image by a canvas copy', function () {
|
|
128
|
+
var raster = makeRaster(function () {
|
|
129
|
+
return 255;
|
|
130
|
+
});
|
|
131
|
+
(0, _hitTestPick.isOpaqueRasterPixel)(raster, point(0, 0));
|
|
132
|
+
expect(raster.getPixel).not.toHaveBeenCalled();
|
|
133
|
+
});
|
|
134
|
+
test('maps the pixel to the source size when it differs from the raster size', function () {
|
|
135
|
+
var raster = makeRaster(function () {
|
|
136
|
+
return 255;
|
|
137
|
+
}, {
|
|
138
|
+
image: {
|
|
139
|
+
naturalWidth: 100,
|
|
140
|
+
naturalHeight: 50,
|
|
141
|
+
alphaAt: function alphaAt() {
|
|
142
|
+
return 255;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
(0, _hitTestPick.isOpaqueRasterPixel)(raster, point(-3.5, 2.5));
|
|
147
|
+
expect(probeContext.drawImage).toHaveBeenCalledWith(raster.image, 10, 35, 1, 1, 0, 0, 1, 1);
|
|
148
|
+
});
|
|
149
|
+
test('is true at or above the alpha threshold', function () {
|
|
150
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(makeRaster(function () {
|
|
151
|
+
return _hitTestPick.ALPHA_HIT_THRESHOLD;
|
|
152
|
+
}), point(0, 0))).toBe(true);
|
|
153
|
+
});
|
|
154
|
+
test('is false for a hidden raster', function () {
|
|
155
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(makeRaster(function () {
|
|
156
|
+
return 255;
|
|
157
|
+
}, {
|
|
158
|
+
visible: false
|
|
159
|
+
}), point(0, 0))).toBe(false);
|
|
160
|
+
});
|
|
161
|
+
test('is false below the alpha threshold, e.g. a soft shadow edge', function () {
|
|
162
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(makeRaster(function () {
|
|
163
|
+
return _hitTestPick.ALPHA_HIT_THRESHOLD - 1;
|
|
164
|
+
}), point(0, 0))).toBe(false);
|
|
165
|
+
});
|
|
166
|
+
test('only the opaque half of the image is hit', function () {
|
|
167
|
+
var raster = makeRaster(function (x) {
|
|
168
|
+
return x >= 5 ? 255 : 0;
|
|
169
|
+
});
|
|
170
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(raster, point(-2, 0))).toBe(false);
|
|
171
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(raster, point(2, 0))).toBe(true);
|
|
172
|
+
});
|
|
173
|
+
test('is true while the raster is not loaded yet', function () {
|
|
174
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(makeRaster(function () {
|
|
175
|
+
return 0;
|
|
176
|
+
}, {
|
|
177
|
+
loaded: false
|
|
178
|
+
}), point(0, 0))).toBe(true);
|
|
179
|
+
});
|
|
180
|
+
test('falls back to a bounds hit when pixel data is not readable', function () {
|
|
181
|
+
var raster = makeRaster(function () {
|
|
182
|
+
return 0;
|
|
183
|
+
});
|
|
184
|
+
probeContext.getImageData.mockImplementationOnce(function () {
|
|
185
|
+
throw new Error('SecurityError');
|
|
186
|
+
});
|
|
187
|
+
expect((0, _hitTestPick.isOpaqueRasterPixel)(raster, point(0, 0))).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
describe('hitTestsWrapperAtPoint', function () {
|
|
191
|
+
test('uses the alpha test for image wrappers', function () {
|
|
192
|
+
var wrapper = {
|
|
193
|
+
type: 'Image',
|
|
194
|
+
wrappedObject: makeRaster(function () {
|
|
195
|
+
return 0;
|
|
196
|
+
})
|
|
197
|
+
};
|
|
198
|
+
expect((0, _hitTestPick.hitTestsWrapperAtPoint)(wrapper, point(0, 0))).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
test('uses the plain item hit test for other wrappers', function () {
|
|
201
|
+
var hitTest = jest.fn(function () {
|
|
202
|
+
return {
|
|
203
|
+
type: 'fill'
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
var wrapper = {
|
|
207
|
+
type: 'Text',
|
|
208
|
+
wrappedObject: {
|
|
209
|
+
hitTest: hitTest
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
expect((0, _hitTestPick.hitTestsWrapperAtPoint)(wrapper, point(1, 1))).toBe(true);
|
|
213
|
+
expect(hitTest).toHaveBeenCalledWith(point(1, 1));
|
|
214
|
+
});
|
|
215
|
+
test('is false for a wrapper without a wrapped item', function () {
|
|
216
|
+
expect((0, _hitTestPick.hitTestsWrapperAtPoint)({
|
|
217
|
+
type: 'Text'
|
|
218
|
+
}, point(1, 1))).toBe(false);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
describe('findWrapperObjectAtPoint', function () {
|
|
222
|
+
var text = {
|
|
223
|
+
type: 'Text',
|
|
224
|
+
wrappedObject: {
|
|
225
|
+
hitTest: function hitTest() {
|
|
226
|
+
return {
|
|
227
|
+
type: 'fill'
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
var transparentImage = {
|
|
233
|
+
type: 'Image',
|
|
234
|
+
wrappedObject: makeRaster(function () {
|
|
235
|
+
return 0;
|
|
236
|
+
})
|
|
237
|
+
};
|
|
238
|
+
var opaqueImage = {
|
|
239
|
+
type: 'Image',
|
|
240
|
+
wrappedObject: makeRaster(function () {
|
|
241
|
+
return 255;
|
|
242
|
+
})
|
|
243
|
+
};
|
|
244
|
+
test('clicks through a transparent image to the object below', function () {
|
|
245
|
+
expect((0, _hitTestPick.findWrapperObjectAtPoint)([text, transparentImage], point(0, 0))).toBe(text);
|
|
246
|
+
});
|
|
247
|
+
test('picks the topmost opaque object', function () {
|
|
248
|
+
expect((0, _hitTestPick.findWrapperObjectAtPoint)([text, opaqueImage], point(0, 0))).toBe(opaqueImage);
|
|
249
|
+
});
|
|
250
|
+
test('clicks through the empty part of a text box to an opaque image below', function () {
|
|
251
|
+
var emptyText = {
|
|
252
|
+
type: 'Text',
|
|
253
|
+
wrappedObject: makeText(false)
|
|
254
|
+
};
|
|
255
|
+
expect((0, _hitTestPick.findWrapperObjectAtPoint)([opaqueImage, emptyText], point(0, 0))).toBe(opaqueImage);
|
|
256
|
+
});
|
|
257
|
+
test('selects text on its ink even above an opaque image', function () {
|
|
258
|
+
var inkText = {
|
|
259
|
+
type: 'Text',
|
|
260
|
+
wrappedObject: makeText(true)
|
|
261
|
+
};
|
|
262
|
+
expect((0, _hitTestPick.findWrapperObjectAtPoint)([opaqueImage, inkText], point(0, 0))).toBe(inkText);
|
|
263
|
+
});
|
|
264
|
+
test('selects the text box when nothing lies below', function () {
|
|
265
|
+
var emptyText = {
|
|
266
|
+
type: 'Text',
|
|
267
|
+
wrappedObject: makeText(false)
|
|
268
|
+
};
|
|
269
|
+
expect((0, _hitTestPick.findWrapperObjectAtPoint)([transparentImage, emptyText], point(0, 0))).toBe(emptyText);
|
|
270
|
+
});
|
|
271
|
+
test('does not blow up with many stacked texts that are all missed', function () {
|
|
272
|
+
var hitTest = jest.fn(function () {
|
|
273
|
+
return {
|
|
274
|
+
type: 'fill'
|
|
275
|
+
};
|
|
276
|
+
});
|
|
277
|
+
var texts = Array.from({
|
|
278
|
+
length: 30
|
|
279
|
+
}, function () {
|
|
280
|
+
return {
|
|
281
|
+
type: 'Text',
|
|
282
|
+
wrappedObject: _objectSpread(_objectSpread({}, makeText(false)), {}, {
|
|
283
|
+
hitTest: hitTest
|
|
284
|
+
})
|
|
285
|
+
};
|
|
286
|
+
});
|
|
287
|
+
(0, _hitTestPick.findWrapperObjectAtPoint)(texts, point(0, 0));
|
|
288
|
+
expect(hitTest.mock.calls.length).toBeLessThan(2000);
|
|
289
|
+
});
|
|
290
|
+
test('returns null when nothing is hit', function () {
|
|
291
|
+
expect((0, _hitTestPick.findWrapperObjectAtPoint)([transparentImage], point(0, 0))).toBeNull();
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
describe('pointHitsPointTextInk', function () {
|
|
295
|
+
var makeInkText = function makeInkText(rotated) {
|
|
296
|
+
var ctx = {
|
|
297
|
+
font: '',
|
|
298
|
+
measureText: function measureText() {
|
|
299
|
+
return {
|
|
300
|
+
width: 10,
|
|
301
|
+
actualBoundingBoxLeft: 0,
|
|
302
|
+
actualBoundingBoxRight: 10,
|
|
303
|
+
actualBoundingBoxAscent: 10,
|
|
304
|
+
actualBoundingBoxDescent: 0
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
return {
|
|
309
|
+
content: 'A',
|
|
310
|
+
view: {
|
|
311
|
+
_context: ctx,
|
|
312
|
+
getTextWidth: function getTextWidth() {
|
|
313
|
+
return 10;
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
style: {
|
|
317
|
+
getFontStyle: function getFontStyle() {
|
|
318
|
+
return '';
|
|
319
|
+
},
|
|
320
|
+
getLeading: function getLeading() {
|
|
321
|
+
return 12;
|
|
322
|
+
},
|
|
323
|
+
getJustification: function getJustification() {
|
|
324
|
+
return 'left';
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
// rotated by 45deg: a click at global (7.07, 7.07) is local (10, 0) which is the right glyph edge
|
|
328
|
+
globalToLocal: function globalToLocal(p) {
|
|
329
|
+
return rotated ? {
|
|
330
|
+
x: p.x * Math.SQRT1_2 + p.y * Math.SQRT1_2,
|
|
331
|
+
y: p.y * Math.SQRT1_2 - p.x * Math.SQRT1_2
|
|
332
|
+
} : p;
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
test('hits a glyph in text space', function () {
|
|
337
|
+
expect((0, _hitTestPick.pointHitsPointTextInk)(makeInkText(false), point(5, -5))).toBe(true);
|
|
338
|
+
expect((0, _hitTestPick.pointHitsPointTextInk)(makeInkText(false), point(5, 5))).toBe(false);
|
|
339
|
+
});
|
|
340
|
+
test('hits a glyph of rotated text', function () {
|
|
341
|
+
expect((0, _hitTestPick.pointHitsPointTextInk)(makeInkText(true), point(-5, 5))).toBe(false);
|
|
342
|
+
expect((0, _hitTestPick.pointHitsPointTextInk)(makeInkText(true), point(5, -2))).toBe(true);
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
describe('hitsTextInk', function () {
|
|
346
|
+
test('ignores the placeholder rect', function () {
|
|
347
|
+
expect((0, _hitTestPick.hitsTextInk)(makeText(false).children[0], point(0, 0))).toBe(false);
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
describe('isPointInLayerClip', function () {
|
|
351
|
+
// real paper items: the layer is clipped the way UseMask does it (mask inserted at index 0)
|
|
352
|
+
// paper needs a 2D context while loading, which jsdom does not provide; geometry does not draw
|
|
353
|
+
var stubContext = new Proxy({}, {
|
|
354
|
+
get: function get() {
|
|
355
|
+
return function () {
|
|
356
|
+
return {};
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
var getContext = jest.spyOn(HTMLCanvasElement.prototype, 'getContext').mockReturnValue(stubContext);
|
|
361
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
362
|
+
var real = require('paper/dist/paper-core');
|
|
363
|
+
afterAll(function () {
|
|
364
|
+
return getContext.mockRestore();
|
|
365
|
+
});
|
|
366
|
+
var makeLayer = function makeLayer(clipped) {
|
|
367
|
+
var spy = document.createElement;
|
|
368
|
+
spy.mockImplementation(createElement);
|
|
369
|
+
real.setup(new real.Size(200, 200));
|
|
370
|
+
spy.mockReturnValue({
|
|
371
|
+
getContext: function getContext() {
|
|
372
|
+
return probeContext;
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
var layer = real.project.activeLayer;
|
|
376
|
+
layer.name = 'designerLayer';
|
|
377
|
+
var mask = new real.Path.Rectangle(new real.Point(50, 50), new real.Size(100, 100));
|
|
378
|
+
mask.name = 'clipGroup';
|
|
379
|
+
layer.insertChild(0, mask);
|
|
380
|
+
layer.clipped = clipped;
|
|
381
|
+
return layer;
|
|
382
|
+
};
|
|
383
|
+
test('is true inside the mask and false outside of it', function () {
|
|
384
|
+
var layer = makeLayer(true);
|
|
385
|
+
expect((0, _hitTestPick.isPointInLayerClip)(layer, new real.Point(100, 100))).toBe(true);
|
|
386
|
+
expect((0, _hitTestPick.isPointInLayerClip)(layer, new real.Point(10, 10))).toBe(false);
|
|
387
|
+
});
|
|
388
|
+
test('is always true for a layer without a mask', function () {
|
|
389
|
+
expect((0, _hitTestPick.isPointInLayerClip)(makeLayer(false), new real.Point(10, 10))).toBe(true);
|
|
390
|
+
expect((0, _hitTestPick.isPointInLayerClip)(undefined, new real.Point(10, 10))).toBe(true);
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
});
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ALPHA_HIT_THRESHOLD = void 0;
|
|
7
|
+
exports.findWrapperObjectAtPoint = findWrapperObjectAtPoint;
|
|
8
|
+
exports.hitTestsWrapperAtPoint = hitTestsWrapperAtPoint;
|
|
9
|
+
exports.hitsTextInk = hitsTextInk;
|
|
10
|
+
exports.isOpaqueRasterPixel = isOpaqueRasterPixel;
|
|
11
|
+
exports.isPointInLayerClip = isPointInLayerClip;
|
|
12
|
+
exports.pointHitsPointTextInk = pointHitsPointTextInk;
|
|
13
|
+
var IMAGE_OBJECT_TYPE = 'Image';
|
|
14
|
+
var TEXT_OBJECT_TYPE = 'Text';
|
|
15
|
+
var DRAWABLE_TEXT_CLASS_NAMES = new Set(['Path', 'CompoundPath', 'Shape']);
|
|
16
|
+
|
|
17
|
+
/** Alpha in 0–255; pixels below this are treated as transparent for selection. */
|
|
18
|
+
var ALPHA_HIT_THRESHOLD = exports.ALPHA_HIT_THRESHOLD = 10;
|
|
19
|
+
var pixelProbeContext;
|
|
20
|
+
|
|
21
|
+
/** Shared 1x1 canvas for alpha sampling. */
|
|
22
|
+
function getPixelProbe() {
|
|
23
|
+
if (pixelProbeContext === undefined) {
|
|
24
|
+
var canvas = document.createElement('canvas');
|
|
25
|
+
canvas.width = 1;
|
|
26
|
+
canvas.height = 1;
|
|
27
|
+
pixelProbeContext = canvas.getContext('2d', {
|
|
28
|
+
willReadFrequently: true
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return pixelProbeContext;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Reads the alpha of the raster pixel under a point in project coordinates.
|
|
36
|
+
* The pixel is drawn into a separate 1x1 canvas: `Raster#getPixel` would permanently replace the
|
|
37
|
+
* raster's image by a natural-size canvas copy (blurry when scaled up, plus extra memory per image), and
|
|
38
|
+
* `Raster#getAverageColor` drops the alpha channel.
|
|
39
|
+
*/
|
|
40
|
+
function isOpaqueRasterPixel(raster, point) {
|
|
41
|
+
if (!raster.visible || !raster.bounds.contains(point)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
if (!raster.loaded) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
var _raster$size = raster.size,
|
|
48
|
+
width = _raster$size.width,
|
|
49
|
+
height = _raster$size.height;
|
|
50
|
+
var local = raster.globalToLocal(point);
|
|
51
|
+
var x = Math.floor(local.x + width / 2);
|
|
52
|
+
var y = Math.floor(local.y + height / 2);
|
|
53
|
+
if (x < 0 || y < 0 || x >= width || y >= height) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
var image = raster.image;
|
|
58
|
+
var probe = getPixelProbe();
|
|
59
|
+
if (!image || !probe) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
// the source can differ from raster.size (e.g. SVG); map the pixel to the source's own size
|
|
63
|
+
var sourceWidth = image.naturalWidth || image.width || width;
|
|
64
|
+
var sourceHeight = image.naturalHeight || image.height || height;
|
|
65
|
+
var sx = Math.min(sourceWidth - 1, Math.floor(x * sourceWidth / width));
|
|
66
|
+
var sy = Math.min(sourceHeight - 1, Math.floor(y * sourceHeight / height));
|
|
67
|
+
probe.clearRect(0, 0, 1, 1);
|
|
68
|
+
probe.drawImage(image, sx, sy, 1, 1, 0, 0, 1, 1);
|
|
69
|
+
return probe.getImageData(0, 0, 1, 1).data[3] >= ALPHA_HIT_THRESHOLD;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
// pixel data not readable (e.g. tainted canvas): fall back to the bounds hit
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Paper.js reports PointText fill hits for the whole line box, not per glyph. The click point is moved
|
|
77
|
+
* into the text's own coordinate space (so rotation and scaling are covered) and compared with the
|
|
78
|
+
* ink box of each glyph.
|
|
79
|
+
*/
|
|
80
|
+
function pointHitsPointTextInk(text, point) {
|
|
81
|
+
var content = text.content;
|
|
82
|
+
if (!content || !/\S/.test(content)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
var view = text.view;
|
|
86
|
+
if (!(view !== null && view !== void 0 && view._context)) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
var ctx = view._context;
|
|
90
|
+
var style = text.style;
|
|
91
|
+
var fontStyle = style.getFontStyle();
|
|
92
|
+
ctx.font = fontStyle;
|
|
93
|
+
var leading = style.getLeading();
|
|
94
|
+
var justification = style.getJustification();
|
|
95
|
+
var local = text.globalToLocal(point);
|
|
96
|
+
var lines = content.split(/\r\n|\n|\r/gm);
|
|
97
|
+
for (var lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
|
|
98
|
+
var line = lines[lineIndex];
|
|
99
|
+
var baseline = lineIndex * leading;
|
|
100
|
+
var fullWidth = view.getTextWidth(fontStyle, [line]);
|
|
101
|
+
var lineStart = 0;
|
|
102
|
+
if (justification === 'center') {
|
|
103
|
+
lineStart = -fullWidth / 2;
|
|
104
|
+
} else if (justification === 'right') {
|
|
105
|
+
lineStart = -fullWidth;
|
|
106
|
+
}
|
|
107
|
+
for (var i = 0; i < line.length; i += 1) {
|
|
108
|
+
var _m$actualBoundingBoxL, _m$actualBoundingBoxR, _m$actualBoundingBoxA, _m$actualBoundingBoxD;
|
|
109
|
+
var ch = line.charAt(i);
|
|
110
|
+
if (!/\S/.test(ch)) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
var origin = lineStart + (i ? view.getTextWidth(fontStyle, [line.slice(0, i)]) : 0);
|
|
114
|
+
var m = ctx.measureText(ch);
|
|
115
|
+
var left = origin - ((_m$actualBoundingBoxL = m.actualBoundingBoxLeft) !== null && _m$actualBoundingBoxL !== void 0 ? _m$actualBoundingBoxL : 0);
|
|
116
|
+
var right = origin + ((_m$actualBoundingBoxR = m.actualBoundingBoxRight) !== null && _m$actualBoundingBoxR !== void 0 ? _m$actualBoundingBoxR : m.width);
|
|
117
|
+
var top = baseline - ((_m$actualBoundingBoxA = m.actualBoundingBoxAscent) !== null && _m$actualBoundingBoxA !== void 0 ? _m$actualBoundingBoxA : 0);
|
|
118
|
+
var bottom = baseline + ((_m$actualBoundingBoxD = m.actualBoundingBoxDescent) !== null && _m$actualBoundingBoxD !== void 0 ? _m$actualBoundingBoxD : 0);
|
|
119
|
+
if (local.x >= left && local.x <= right && local.y >= top && local.y <= bottom) {
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Hit on visible glyphs or outlines; not the group box and not the HTML2Paper placeholder rect. */
|
|
128
|
+
function hitsTextInk(item, point) {
|
|
129
|
+
var _item$data, _item$children;
|
|
130
|
+
if ((_item$data = item.data) !== null && _item$data !== void 0 && _item$data.placeholder) {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
if (item.className === 'PointText') {
|
|
134
|
+
return pointHitsPointTextInk(item, point);
|
|
135
|
+
}
|
|
136
|
+
if ((_item$children = item.children) !== null && _item$children !== void 0 && _item$children.length) {
|
|
137
|
+
return item.children.some(function (child) {
|
|
138
|
+
return hitsTextInk(child, point);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
if (DRAWABLE_TEXT_CLASS_NAMES.has(item.className)) {
|
|
142
|
+
return !!item.hitTest(point, {
|
|
143
|
+
fill: true,
|
|
144
|
+
stroke: true,
|
|
145
|
+
segments: true
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
function hitTestsWrapperAtPoint(wrapper, point) {
|
|
151
|
+
var item = wrapper.wrappedObject;
|
|
152
|
+
if (!item) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
if (wrapper.type === IMAGE_OBJECT_TYPE) {
|
|
156
|
+
return isOpaqueRasterPixel(item, point);
|
|
157
|
+
}
|
|
158
|
+
return !!item.hitTest(point);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* False when the layer is clipped by a mask (the design area) and the point lies outside of it.
|
|
163
|
+
* Paper only exposes the mask through the private `_getClipItem()`, so it is the first child with
|
|
164
|
+
* `clipMask` set, which is what `Group#clipped` toggles.
|
|
165
|
+
*/
|
|
166
|
+
function isPointInLayerClip(layer, point) {
|
|
167
|
+
if (!(layer !== null && layer !== void 0 && layer.clipped)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
var clipItem = layer.children.find(function (child) {
|
|
171
|
+
return child.clipMask;
|
|
172
|
+
});
|
|
173
|
+
return !clipItem || clipItem.contains(point);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Visible content at the point: an opaque pixel, a glyph, or a hit on any other item. */
|
|
177
|
+
function hasContentAtPoint(wrapper, point) {
|
|
178
|
+
if (!(wrapper !== null && wrapper !== void 0 && wrapper.wrappedObject)) {
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
if (wrapper.type === TEXT_OBJECT_TYPE) {
|
|
182
|
+
return hitsTextInk(wrapper.wrappedObject, point);
|
|
183
|
+
}
|
|
184
|
+
return hitTestsWrapperAtPoint(wrapper, point);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Topmost object with visible content at the point. Transparent image pixels and the empty part of a
|
|
189
|
+
* text box click through to what lies below; with nothing below, the text box still selects the text.
|
|
190
|
+
*/
|
|
191
|
+
function findWrapperObjectAtPoint(objects, point) {
|
|
192
|
+
for (var i = objects.length - 1; i >= 0; i -= 1) {
|
|
193
|
+
var wrapper = objects[i];
|
|
194
|
+
if (!wrapper) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (hasContentAtPoint(wrapper, point)) {
|
|
198
|
+
return wrapper;
|
|
199
|
+
}
|
|
200
|
+
var isTextBox = wrapper.type === TEXT_OBJECT_TYPE && hitTestsWrapperAtPoint(wrapper, point);
|
|
201
|
+
if (isTextBox && !objects.slice(0, i).some(function (below) {
|
|
202
|
+
return hasContentAtPoint(below, point);
|
|
203
|
+
})) {
|
|
204
|
+
return wrapper;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
@@ -137,6 +137,18 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
137
137
|
});
|
|
138
138
|
document.addEventListener(clientPosition.isTouchEvent ? 'touchend' : 'mouseup', mouseUp);
|
|
139
139
|
});
|
|
140
|
+
_defineProperty(_assertThisInitialized(_this), "onMoveUpClick", function () {
|
|
141
|
+
var selectedObject = _this.props.canvas.selectedObject;
|
|
142
|
+
if (selectedObject && !selectedObject.isLast) {
|
|
143
|
+
selectedObject.setIndex(selectedObject.getIndex() + 1);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
_defineProperty(_assertThisInitialized(_this), "onMoveDownClick", function () {
|
|
147
|
+
var selectedObject = _this.props.canvas.selectedObject;
|
|
148
|
+
if (selectedObject && !selectedObject.isFirst) {
|
|
149
|
+
selectedObject.setIndex(selectedObject.getIndex() - 1);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
140
152
|
_defineProperty(_assertThisInitialized(_this), "onDeleteClick", function () {
|
|
141
153
|
var selectedObject = _this.props.canvas.selectedObject;
|
|
142
154
|
if (selectedObject) {
|
|
@@ -156,6 +168,7 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
156
168
|
});
|
|
157
169
|
_defineProperty(_assertThisInitialized(_this), "bottomRightContainerRef", /*#__PURE__*/_react["default"].createRef());
|
|
158
170
|
_defineProperty(_assertThisInitialized(_this), "topRightContainerRef", /*#__PURE__*/_react["default"].createRef());
|
|
171
|
+
_defineProperty(_assertThisInitialized(_this), "topCenterContainerRef", /*#__PURE__*/_react["default"].createRef());
|
|
159
172
|
_defineProperty(_assertThisInitialized(_this), "bottomLeftContainerRef", /*#__PURE__*/_react["default"].createRef());
|
|
160
173
|
_defineProperty(_assertThisInitialized(_this), "topLeftContainerRef", /*#__PURE__*/_react["default"].createRef());
|
|
161
174
|
_defineProperty(_assertThisInitialized(_this), "angleLabelRef", /*#__PURE__*/_react["default"].createRef());
|
|
@@ -167,7 +180,9 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
167
180
|
var _assertThisInitialize = _assertThisInitialized(_this),
|
|
168
181
|
onScalingChange = _assertThisInitialize.onScalingChange,
|
|
169
182
|
onRotationChange = _assertThisInitialize.onRotationChange,
|
|
170
|
-
onDeleteClick = _assertThisInitialize.onDeleteClick
|
|
183
|
+
onDeleteClick = _assertThisInitialize.onDeleteClick,
|
|
184
|
+
onMoveUpClick = _assertThisInitialize.onMoveUpClick,
|
|
185
|
+
onMoveDownClick = _assertThisInitialize.onMoveDownClick;
|
|
171
186
|
if (renderToolboxButton) {
|
|
172
187
|
// call the render prop method with the position name and the available functions
|
|
173
188
|
return renderToolboxButton({
|
|
@@ -177,6 +192,10 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
177
192
|
onScalingChange: onScalingChange,
|
|
178
193
|
onRotationChange: onRotationChange,
|
|
179
194
|
onDeleteClick: onDeleteClick,
|
|
195
|
+
onMoveUpClick: onMoveUpClick,
|
|
196
|
+
onMoveDownClick: onMoveDownClick,
|
|
197
|
+
isFirst: selectedObject ? selectedObject.isFirst : false,
|
|
198
|
+
isLast: selectedObject ? selectedObject.isLast : false,
|
|
180
199
|
components: {
|
|
181
200
|
InlineToolboxButton: _InlineToolboxButton["default"]
|
|
182
201
|
}
|
|
@@ -209,6 +228,8 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
209
228
|
},
|
|
210
229
|
onClick: onDeleteClick
|
|
211
230
|
});
|
|
231
|
+
case 'topCenter':
|
|
232
|
+
return null;
|
|
212
233
|
case 'topLeft':
|
|
213
234
|
default:
|
|
214
235
|
return null;
|
|
@@ -267,6 +288,10 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
267
288
|
bottomRight = _selectedObject$getTr2.bottomRight,
|
|
268
289
|
bottomLeft = _selectedObject$getTr2.bottomLeft,
|
|
269
290
|
topLeft = _selectedObject$getTr2.topLeft;
|
|
291
|
+
var topCenter = {
|
|
292
|
+
x: (topLeft.x + topRight.x) / 2,
|
|
293
|
+
y: (topLeft.y + topRight.y) / 2
|
|
294
|
+
};
|
|
270
295
|
this.bottomRightContainerRef.current && this.bottomRightContainerRef.current.updateTransform({
|
|
271
296
|
translate: {
|
|
272
297
|
x: bottomRight.x,
|
|
@@ -295,6 +320,13 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
295
320
|
},
|
|
296
321
|
rotate: selectedObject.rotation
|
|
297
322
|
});
|
|
323
|
+
this.topCenterContainerRef.current && this.topCenterContainerRef.current.updateTransform({
|
|
324
|
+
translate: {
|
|
325
|
+
x: topCenter.x,
|
|
326
|
+
y: topCenter.y
|
|
327
|
+
},
|
|
328
|
+
rotate: selectedObject.rotation
|
|
329
|
+
});
|
|
298
330
|
var anglePosition = topRight;
|
|
299
331
|
var radAngle = selectedObject.rotation * Math.PI / 180;
|
|
300
332
|
if (selectedObject.rotation >= 270) {
|
|
@@ -334,6 +366,7 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
334
366
|
var AngleLabel = this.AngleLabel;
|
|
335
367
|
var renderToolboxButton = this.renderToolboxButton,
|
|
336
368
|
topRightContainerRef = this.topRightContainerRef,
|
|
369
|
+
topCenterContainerRef = this.topCenterContainerRef,
|
|
337
370
|
bottomRightContainerRef = this.bottomRightContainerRef,
|
|
338
371
|
bottomLeftContainerRef = this.bottomLeftContainerRef,
|
|
339
372
|
topLeftContainerRef = this.topLeftContainerRef,
|
|
@@ -343,6 +376,7 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
343
376
|
return children({
|
|
344
377
|
refs: {
|
|
345
378
|
topRightContainerRef: topRightContainerRef,
|
|
379
|
+
topCenterContainerRef: topCenterContainerRef,
|
|
346
380
|
bottomRightContainerRef: bottomRightContainerRef,
|
|
347
381
|
bottomLeftContainerRef: bottomLeftContainerRef,
|
|
348
382
|
topLeftContainerRef: topLeftContainerRef,
|
|
@@ -372,6 +406,9 @@ var InlineToolBox = /*#__PURE__*/function (_React$Component) {
|
|
|
372
406
|
ref: topRightContainerRef,
|
|
373
407
|
className: classes.buttonContainer
|
|
374
408
|
}, renderToolboxButton('topRight')), /*#__PURE__*/_react["default"].createElement(_ManipulableContainer["default"], {
|
|
409
|
+
ref: topCenterContainerRef,
|
|
410
|
+
className: classes.buttonContainer
|
|
411
|
+
}, renderToolboxButton('topCenter')), /*#__PURE__*/_react["default"].createElement(_ManipulableContainer["default"], {
|
|
375
412
|
ref: bottomRightContainerRef,
|
|
376
413
|
className: classes.buttonContainer
|
|
377
414
|
}, renderToolboxButton('bottomRight')), /*#__PURE__*/_react["default"].createElement(_ManipulableContainer["default"], {
|
package/package.json
CHANGED
|
@@ -11,6 +11,7 @@ import { transformTextToPath } from './Utils/TextToSVGRepository';
|
|
|
11
11
|
import { removePlaceholders } from './Utils/HTML2Paper';
|
|
12
12
|
import { fixSvgExportResult, prepareSvgStr, wrapWithViewGroup } from './Utils/SVGfix';
|
|
13
13
|
import { useColorLayer } from './Utils/UseColorLayer';
|
|
14
|
+
import { findWrapperObjectAtPoint, isPointInLayerClip } from './Utils/hitTestPick';
|
|
14
15
|
|
|
15
16
|
const logger = new Logger('Canvas');
|
|
16
17
|
|
|
@@ -176,18 +177,16 @@ export class Canvas extends AbstractCanvas {
|
|
|
176
177
|
|
|
177
178
|
onMouseDown(event) {
|
|
178
179
|
const { point } = event;
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
if (this.selectedObject &&
|
|
180
|
+
// the design area mask clips the layer, so its invisible part must not be pickable
|
|
181
|
+
const wrapperAtPoint = isPointInLayerClip(this._project.layers.designerLayer, point)
|
|
182
|
+
? findWrapperObjectAtPoint(this.objects, point)
|
|
183
|
+
: null;
|
|
184
|
+
if (wrapperAtPoint) {
|
|
185
|
+
if (this.selectedObject && wrapperAtPoint !== this.selectedObject) {
|
|
185
186
|
this.clearSelection();
|
|
186
187
|
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
wo.grabbed = true;
|
|
190
|
-
}
|
|
188
|
+
wrapperAtPoint.selected = true;
|
|
189
|
+
wrapperAtPoint.grabbed = true;
|
|
191
190
|
} else if (this.selectedObject) {
|
|
192
191
|
this.clearSelection();
|
|
193
192
|
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import paper from 'paper';
|
|
2
|
+
import {
|
|
3
|
+
ALPHA_HIT_THRESHOLD,
|
|
4
|
+
findWrapperObjectAtPoint,
|
|
5
|
+
hitTestsWrapperAtPoint,
|
|
6
|
+
hitsTextInk,
|
|
7
|
+
pointHitsPointTextInk,
|
|
8
|
+
isOpaqueRasterPixel,
|
|
9
|
+
isPointInLayerClip,
|
|
10
|
+
} from '../hitTestPick';
|
|
11
|
+
|
|
12
|
+
const point = (x: number, y: number) => ({ x, y } as paper.Point);
|
|
13
|
+
|
|
14
|
+
/** Fake 1x1 probe canvas: reports the alpha of the source pixel that was drawn into it. */
|
|
15
|
+
const probe = { image: null as any, x: 0, y: 0 };
|
|
16
|
+
const probeContext = {
|
|
17
|
+
clearRect: jest.fn(),
|
|
18
|
+
drawImage: jest.fn((image: unknown, sx: number, sy: number) => {
|
|
19
|
+
Object.assign(probe, { image, x: sx, y: sy });
|
|
20
|
+
}),
|
|
21
|
+
getImageData: jest.fn(() => ({ data: [0, 0, 0, probe.image.alphaAt(probe.x, probe.y)] })),
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const createElement = document.createElement.bind(document);
|
|
25
|
+
|
|
26
|
+
beforeAll(() => {
|
|
27
|
+
jest.spyOn(document, 'createElement').mockReturnValue({ getContext: () => probeContext } as any);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
jest.clearAllMocks();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
/** 10x10 raster centred on the origin; the pixel alpha is sampled from its source image, not via getPixel. */
|
|
35
|
+
const makeRaster = (alphaAt: (x: number, y: number) => number, overrides = {}) => {
|
|
36
|
+
return ({
|
|
37
|
+
bounds: { contains: (p: paper.Point) => p.x >= -5 && p.x <= 5 && p.y >= -5 && p.y <= 5 },
|
|
38
|
+
loaded: true,
|
|
39
|
+
visible: true,
|
|
40
|
+
size: { width: 10, height: 10 },
|
|
41
|
+
image: { naturalWidth: 10, naturalHeight: 10, alphaAt },
|
|
42
|
+
globalToLocal: (p: paper.Point) => p,
|
|
43
|
+
getPixel: jest.fn(),
|
|
44
|
+
...overrides,
|
|
45
|
+
} as unknown) as paper.Raster;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** Group holding a placeholder rect (always hit) and a PointText whose ink is hit or not. */
|
|
49
|
+
const makeText = (inkHit: boolean) => {
|
|
50
|
+
const pointText = {
|
|
51
|
+
className: 'PointText',
|
|
52
|
+
content: 'A',
|
|
53
|
+
view: null,
|
|
54
|
+
data: {},
|
|
55
|
+
hitTest: () => null,
|
|
56
|
+
};
|
|
57
|
+
const wrapped: any = {
|
|
58
|
+
className: 'Group',
|
|
59
|
+
data: {},
|
|
60
|
+
children: [
|
|
61
|
+
{ className: 'Shape', data: { placeholder: true }, hitTest: () => ({ type: 'fill' }) },
|
|
62
|
+
pointText,
|
|
63
|
+
],
|
|
64
|
+
hitTest: () => ({ type: 'fill' }),
|
|
65
|
+
};
|
|
66
|
+
if (!inkHit) {
|
|
67
|
+
pointText.content = ' ';
|
|
68
|
+
}
|
|
69
|
+
return wrapped;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
describe('hitTestPick', () => {
|
|
73
|
+
describe('isOpaqueRasterPixel', () => {
|
|
74
|
+
test('is false outside the raster bounds', () => {
|
|
75
|
+
expect(
|
|
76
|
+
isOpaqueRasterPixel(
|
|
77
|
+
makeRaster(() => 255),
|
|
78
|
+
point(20, 20)
|
|
79
|
+
)
|
|
80
|
+
).toBe(false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('reads the pixel at the click position in image coordinates', () => {
|
|
84
|
+
const raster = makeRaster(() => 255);
|
|
85
|
+
isOpaqueRasterPixel(raster, point(-3.5, 2.5));
|
|
86
|
+
expect(probeContext.drawImage).toHaveBeenCalledWith(raster.image, 1, 7, 1, 1, 0, 0, 1, 1);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('never touches the raster pixel data, which would replace its image by a canvas copy', () => {
|
|
90
|
+
const raster = makeRaster(() => 255);
|
|
91
|
+
isOpaqueRasterPixel(raster, point(0, 0));
|
|
92
|
+
expect(raster.getPixel).not.toHaveBeenCalled();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('maps the pixel to the source size when it differs from the raster size', () => {
|
|
96
|
+
const raster = makeRaster(() => 255, {
|
|
97
|
+
image: { naturalWidth: 100, naturalHeight: 50, alphaAt: () => 255 },
|
|
98
|
+
});
|
|
99
|
+
isOpaqueRasterPixel(raster, point(-3.5, 2.5));
|
|
100
|
+
expect(probeContext.drawImage).toHaveBeenCalledWith(raster.image, 10, 35, 1, 1, 0, 0, 1, 1);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('is true at or above the alpha threshold', () => {
|
|
104
|
+
expect(
|
|
105
|
+
isOpaqueRasterPixel(
|
|
106
|
+
makeRaster(() => ALPHA_HIT_THRESHOLD),
|
|
107
|
+
point(0, 0)
|
|
108
|
+
)
|
|
109
|
+
).toBe(true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test('is false for a hidden raster', () => {
|
|
113
|
+
expect(
|
|
114
|
+
isOpaqueRasterPixel(
|
|
115
|
+
makeRaster(() => 255, { visible: false }),
|
|
116
|
+
point(0, 0)
|
|
117
|
+
)
|
|
118
|
+
).toBe(false);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('is false below the alpha threshold, e.g. a soft shadow edge', () => {
|
|
122
|
+
expect(
|
|
123
|
+
isOpaqueRasterPixel(
|
|
124
|
+
makeRaster(() => ALPHA_HIT_THRESHOLD - 1),
|
|
125
|
+
point(0, 0)
|
|
126
|
+
)
|
|
127
|
+
).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test('only the opaque half of the image is hit', () => {
|
|
131
|
+
const raster = makeRaster(x => (x >= 5 ? 255 : 0));
|
|
132
|
+
expect(isOpaqueRasterPixel(raster, point(-2, 0))).toBe(false);
|
|
133
|
+
expect(isOpaqueRasterPixel(raster, point(2, 0))).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
test('is true while the raster is not loaded yet', () => {
|
|
137
|
+
expect(
|
|
138
|
+
isOpaqueRasterPixel(
|
|
139
|
+
makeRaster(() => 0, { loaded: false }),
|
|
140
|
+
point(0, 0)
|
|
141
|
+
)
|
|
142
|
+
).toBe(true);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
test('falls back to a bounds hit when pixel data is not readable', () => {
|
|
146
|
+
const raster = makeRaster(() => 0);
|
|
147
|
+
probeContext.getImageData.mockImplementationOnce(() => {
|
|
148
|
+
throw new Error('SecurityError');
|
|
149
|
+
});
|
|
150
|
+
expect(isOpaqueRasterPixel(raster, point(0, 0))).toBe(true);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
describe('hitTestsWrapperAtPoint', () => {
|
|
155
|
+
test('uses the alpha test for image wrappers', () => {
|
|
156
|
+
const wrapper = { type: 'Image', wrappedObject: makeRaster(() => 0) };
|
|
157
|
+
expect(hitTestsWrapperAtPoint(wrapper as any, point(0, 0))).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('uses the plain item hit test for other wrappers', () => {
|
|
161
|
+
const hitTest = jest.fn(() => ({ type: 'fill' }));
|
|
162
|
+
const wrapper = { type: 'Text', wrappedObject: { hitTest } };
|
|
163
|
+
expect(hitTestsWrapperAtPoint(wrapper as any, point(1, 1))).toBe(true);
|
|
164
|
+
expect(hitTest).toHaveBeenCalledWith(point(1, 1));
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
test('is false for a wrapper without a wrapped item', () => {
|
|
168
|
+
expect(hitTestsWrapperAtPoint({ type: 'Text' } as any, point(1, 1))).toBe(false);
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('findWrapperObjectAtPoint', () => {
|
|
173
|
+
const text = { type: 'Text', wrappedObject: { hitTest: () => ({ type: 'fill' }) } };
|
|
174
|
+
const transparentImage = { type: 'Image', wrappedObject: makeRaster(() => 0) };
|
|
175
|
+
const opaqueImage = { type: 'Image', wrappedObject: makeRaster(() => 255) };
|
|
176
|
+
|
|
177
|
+
test('clicks through a transparent image to the object below', () => {
|
|
178
|
+
expect(findWrapperObjectAtPoint([text, transparentImage] as any[], point(0, 0))).toBe(text);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test('picks the topmost opaque object', () => {
|
|
182
|
+
expect(findWrapperObjectAtPoint([text, opaqueImage] as any[], point(0, 0))).toBe(opaqueImage);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('clicks through the empty part of a text box to an opaque image below', () => {
|
|
186
|
+
const emptyText = { type: 'Text', wrappedObject: makeText(false) };
|
|
187
|
+
expect(findWrapperObjectAtPoint([opaqueImage, emptyText] as any[], point(0, 0))).toBe(opaqueImage);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('selects text on its ink even above an opaque image', () => {
|
|
191
|
+
const inkText = { type: 'Text', wrappedObject: makeText(true) };
|
|
192
|
+
expect(findWrapperObjectAtPoint([opaqueImage, inkText] as any[], point(0, 0))).toBe(inkText);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test('selects the text box when nothing lies below', () => {
|
|
196
|
+
const emptyText = { type: 'Text', wrappedObject: makeText(false) };
|
|
197
|
+
expect(findWrapperObjectAtPoint([transparentImage, emptyText] as any[], point(0, 0))).toBe(emptyText);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('does not blow up with many stacked texts that are all missed', () => {
|
|
201
|
+
const hitTest = jest.fn(() => ({ type: 'fill' }));
|
|
202
|
+
const texts = Array.from({ length: 30 }, () => ({
|
|
203
|
+
type: 'Text',
|
|
204
|
+
wrappedObject: { ...makeText(false), hitTest },
|
|
205
|
+
}));
|
|
206
|
+
findWrapperObjectAtPoint(texts as any[], point(0, 0));
|
|
207
|
+
expect(hitTest.mock.calls.length).toBeLessThan(2000);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test('returns null when nothing is hit', () => {
|
|
211
|
+
expect(findWrapperObjectAtPoint([transparentImage] as any[], point(0, 0))).toBeNull();
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
describe('pointHitsPointTextInk', () => {
|
|
216
|
+
const makeInkText = (rotated: boolean) => {
|
|
217
|
+
const ctx = {
|
|
218
|
+
font: '',
|
|
219
|
+
measureText: () => ({
|
|
220
|
+
width: 10,
|
|
221
|
+
actualBoundingBoxLeft: 0,
|
|
222
|
+
actualBoundingBoxRight: 10,
|
|
223
|
+
actualBoundingBoxAscent: 10,
|
|
224
|
+
actualBoundingBoxDescent: 0,
|
|
225
|
+
}),
|
|
226
|
+
};
|
|
227
|
+
return ({
|
|
228
|
+
content: 'A',
|
|
229
|
+
view: { _context: ctx, getTextWidth: () => 10 },
|
|
230
|
+
style: { getFontStyle: () => '', getLeading: () => 12, getJustification: () => 'left' },
|
|
231
|
+
// rotated by 45deg: a click at global (7.07, 7.07) is local (10, 0) which is the right glyph edge
|
|
232
|
+
globalToLocal: (p: paper.Point) =>
|
|
233
|
+
rotated
|
|
234
|
+
? { x: p.x * Math.SQRT1_2 + p.y * Math.SQRT1_2, y: p.y * Math.SQRT1_2 - p.x * Math.SQRT1_2 }
|
|
235
|
+
: p,
|
|
236
|
+
} as unknown) as paper.PointText;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
test('hits a glyph in text space', () => {
|
|
240
|
+
expect(pointHitsPointTextInk(makeInkText(false), point(5, -5))).toBe(true);
|
|
241
|
+
expect(pointHitsPointTextInk(makeInkText(false), point(5, 5))).toBe(false);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test('hits a glyph of rotated text', () => {
|
|
245
|
+
expect(pointHitsPointTextInk(makeInkText(true), point(-5, 5))).toBe(false);
|
|
246
|
+
expect(pointHitsPointTextInk(makeInkText(true), point(5, -2))).toBe(true);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe('hitsTextInk', () => {
|
|
251
|
+
test('ignores the placeholder rect', () => {
|
|
252
|
+
expect(hitsTextInk(makeText(false).children[0], point(0, 0))).toBe(false);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe('isPointInLayerClip', () => {
|
|
257
|
+
// real paper items: the layer is clipped the way UseMask does it (mask inserted at index 0)
|
|
258
|
+
// paper needs a 2D context while loading, which jsdom does not provide; geometry does not draw
|
|
259
|
+
const stubContext = new Proxy({}, { get: () => () => ({}) });
|
|
260
|
+
const getContext = jest
|
|
261
|
+
.spyOn(HTMLCanvasElement.prototype, 'getContext')
|
|
262
|
+
.mockReturnValue(stubContext as any);
|
|
263
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
264
|
+
const real = require('paper/dist/paper-core');
|
|
265
|
+
afterAll(() => getContext.mockRestore());
|
|
266
|
+
const makeLayer = (clipped: boolean) => {
|
|
267
|
+
const spy = document.createElement as jest.Mock;
|
|
268
|
+
spy.mockImplementation(createElement);
|
|
269
|
+
real.setup(new real.Size(200, 200));
|
|
270
|
+
spy.mockReturnValue({ getContext: () => probeContext });
|
|
271
|
+
const layer = real.project.activeLayer;
|
|
272
|
+
layer.name = 'designerLayer';
|
|
273
|
+
const mask = new real.Path.Rectangle(new real.Point(50, 50), new real.Size(100, 100));
|
|
274
|
+
mask.name = 'clipGroup';
|
|
275
|
+
layer.insertChild(0, mask);
|
|
276
|
+
layer.clipped = clipped;
|
|
277
|
+
return layer;
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
test('is true inside the mask and false outside of it', () => {
|
|
281
|
+
const layer = makeLayer(true);
|
|
282
|
+
expect(isPointInLayerClip(layer, new real.Point(100, 100))).toBe(true);
|
|
283
|
+
expect(isPointInLayerClip(layer, new real.Point(10, 10))).toBe(false);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test('is always true for a layer without a mask', () => {
|
|
287
|
+
expect(isPointInLayerClip(makeLayer(false), new real.Point(10, 10))).toBe(true);
|
|
288
|
+
expect(isPointInLayerClip(undefined, new real.Point(10, 10))).toBe(true);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import paper from 'paper';
|
|
2
|
+
import { AbstractCanvasObject } from '../../../CanvasInterface/Objects/CanvasObject';
|
|
3
|
+
|
|
4
|
+
const IMAGE_OBJECT_TYPE = 'Image';
|
|
5
|
+
const TEXT_OBJECT_TYPE = 'Text';
|
|
6
|
+
const DRAWABLE_TEXT_CLASS_NAMES = new Set(['Path', 'CompoundPath', 'Shape']);
|
|
7
|
+
|
|
8
|
+
/** Alpha in 0–255; pixels below this are treated as transparent for selection. */
|
|
9
|
+
export const ALPHA_HIT_THRESHOLD = 10;
|
|
10
|
+
|
|
11
|
+
let pixelProbeContext: CanvasRenderingContext2D | null | undefined;
|
|
12
|
+
|
|
13
|
+
/** Shared 1x1 canvas for alpha sampling. */
|
|
14
|
+
function getPixelProbe(): CanvasRenderingContext2D | null {
|
|
15
|
+
if (pixelProbeContext === undefined) {
|
|
16
|
+
const canvas = document.createElement('canvas');
|
|
17
|
+
canvas.width = 1;
|
|
18
|
+
canvas.height = 1;
|
|
19
|
+
pixelProbeContext = canvas.getContext('2d', { willReadFrequently: true });
|
|
20
|
+
}
|
|
21
|
+
return pixelProbeContext;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Reads the alpha of the raster pixel under a point in project coordinates.
|
|
26
|
+
* The pixel is drawn into a separate 1x1 canvas: `Raster#getPixel` would permanently replace the
|
|
27
|
+
* raster's image by a natural-size canvas copy (blurry when scaled up, plus extra memory per image), and
|
|
28
|
+
* `Raster#getAverageColor` drops the alpha channel.
|
|
29
|
+
*/
|
|
30
|
+
export function isOpaqueRasterPixel(raster: paper.Raster, point: paper.Point): boolean {
|
|
31
|
+
if (!raster.visible || !raster.bounds.contains(point)) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if (!raster.loaded) {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
const { width, height } = raster.size;
|
|
38
|
+
const local = raster.globalToLocal(point);
|
|
39
|
+
const x = Math.floor(local.x + width / 2);
|
|
40
|
+
const y = Math.floor(local.y + height / 2);
|
|
41
|
+
if (x < 0 || y < 0 || x >= width || y >= height) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const image = raster.image as CanvasImageSource & { naturalWidth?: number; naturalHeight?: number };
|
|
46
|
+
const probe = getPixelProbe();
|
|
47
|
+
if (!image || !probe) {
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
// the source can differ from raster.size (e.g. SVG); map the pixel to the source's own size
|
|
51
|
+
const sourceWidth = image.naturalWidth || (image as HTMLCanvasElement).width || width;
|
|
52
|
+
const sourceHeight = image.naturalHeight || (image as HTMLCanvasElement).height || height;
|
|
53
|
+
const sx = Math.min(sourceWidth - 1, Math.floor((x * sourceWidth) / width));
|
|
54
|
+
const sy = Math.min(sourceHeight - 1, Math.floor((y * sourceHeight) / height));
|
|
55
|
+
probe.clearRect(0, 0, 1, 1);
|
|
56
|
+
probe.drawImage(image, sx, sy, 1, 1, 0, 0, 1, 1);
|
|
57
|
+
return probe.getImageData(0, 0, 1, 1).data[3] >= ALPHA_HIT_THRESHOLD;
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// pixel data not readable (e.g. tainted canvas): fall back to the bounds hit
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type PaperViewWithContext = paper.View & { _context?: CanvasRenderingContext2D };
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Paper.js reports PointText fill hits for the whole line box, not per glyph. The click point is moved
|
|
68
|
+
* into the text's own coordinate space (so rotation and scaling are covered) and compared with the
|
|
69
|
+
* ink box of each glyph.
|
|
70
|
+
*/
|
|
71
|
+
export function pointHitsPointTextInk(text: paper.PointText, point: paper.Point): boolean {
|
|
72
|
+
const content = text.content;
|
|
73
|
+
if (!content || !/\S/.test(content)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const view = text.view as PaperViewWithContext | null;
|
|
77
|
+
if (!view?._context) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
const ctx = view._context;
|
|
81
|
+
const style = text.style;
|
|
82
|
+
const fontStyle = style.getFontStyle();
|
|
83
|
+
ctx.font = fontStyle;
|
|
84
|
+
const leading = style.getLeading();
|
|
85
|
+
const justification = style.getJustification();
|
|
86
|
+
const local = text.globalToLocal(point);
|
|
87
|
+
|
|
88
|
+
const lines = content.split(/\r\n|\n|\r/gm);
|
|
89
|
+
for (let lineIndex = 0; lineIndex < lines.length; lineIndex += 1) {
|
|
90
|
+
const line = lines[lineIndex];
|
|
91
|
+
const baseline = lineIndex * leading;
|
|
92
|
+
const fullWidth = view.getTextWidth(fontStyle, [line]);
|
|
93
|
+
let lineStart = 0;
|
|
94
|
+
if (justification === 'center') {
|
|
95
|
+
lineStart = -fullWidth / 2;
|
|
96
|
+
} else if (justification === 'right') {
|
|
97
|
+
lineStart = -fullWidth;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (let i = 0; i < line.length; i += 1) {
|
|
101
|
+
const ch = line.charAt(i);
|
|
102
|
+
if (!/\S/.test(ch)) {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const origin = lineStart + (i ? view.getTextWidth(fontStyle, [line.slice(0, i)]) : 0);
|
|
106
|
+
const m = ctx.measureText(ch);
|
|
107
|
+
const left = origin - (m.actualBoundingBoxLeft ?? 0);
|
|
108
|
+
const right = origin + (m.actualBoundingBoxRight ?? m.width);
|
|
109
|
+
const top = baseline - (m.actualBoundingBoxAscent ?? 0);
|
|
110
|
+
const bottom = baseline + (m.actualBoundingBoxDescent ?? 0);
|
|
111
|
+
if (local.x >= left && local.x <= right && local.y >= top && local.y <= bottom) {
|
|
112
|
+
return true;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Hit on visible glyphs or outlines; not the group box and not the HTML2Paper placeholder rect. */
|
|
120
|
+
export function hitsTextInk(item: paper.Item, point: paper.Point): boolean {
|
|
121
|
+
if (item.data?.placeholder) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (item.className === 'PointText') {
|
|
125
|
+
return pointHitsPointTextInk(item as paper.PointText, point);
|
|
126
|
+
}
|
|
127
|
+
if (item.children?.length) {
|
|
128
|
+
return item.children.some(child => hitsTextInk(child, point));
|
|
129
|
+
}
|
|
130
|
+
if (DRAWABLE_TEXT_CLASS_NAMES.has(item.className)) {
|
|
131
|
+
return !!item.hitTest(point, { fill: true, stroke: true, segments: true });
|
|
132
|
+
}
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function hitTestsWrapperAtPoint(wrapper: AbstractCanvasObject, point: paper.Point): boolean {
|
|
137
|
+
const item = wrapper.wrappedObject;
|
|
138
|
+
if (!item) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
if (wrapper.type === IMAGE_OBJECT_TYPE) {
|
|
142
|
+
return isOpaqueRasterPixel(item as paper.Raster, point);
|
|
143
|
+
}
|
|
144
|
+
return !!item.hitTest(point);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* False when the layer is clipped by a mask (the design area) and the point lies outside of it.
|
|
149
|
+
* Paper only exposes the mask through the private `_getClipItem()`, so it is the first child with
|
|
150
|
+
* `clipMask` set, which is what `Group#clipped` toggles.
|
|
151
|
+
*/
|
|
152
|
+
export function isPointInLayerClip(layer: paper.Layer | undefined, point: paper.Point): boolean {
|
|
153
|
+
if (!layer?.clipped) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
const clipItem = layer.children.find(child => child.clipMask);
|
|
157
|
+
return !clipItem || clipItem.contains(point);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Visible content at the point: an opaque pixel, a glyph, or a hit on any other item. */
|
|
161
|
+
function hasContentAtPoint(wrapper: AbstractCanvasObject | undefined, point: paper.Point): boolean {
|
|
162
|
+
if (!wrapper?.wrappedObject) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
if (wrapper.type === TEXT_OBJECT_TYPE) {
|
|
166
|
+
return hitsTextInk(wrapper.wrappedObject, point);
|
|
167
|
+
}
|
|
168
|
+
return hitTestsWrapperAtPoint(wrapper, point);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Topmost object with visible content at the point. Transparent image pixels and the empty part of a
|
|
173
|
+
* text box click through to what lies below; with nothing below, the text box still selects the text.
|
|
174
|
+
*/
|
|
175
|
+
export function findWrapperObjectAtPoint(
|
|
176
|
+
objects: AbstractCanvasObject[],
|
|
177
|
+
point: paper.Point
|
|
178
|
+
): AbstractCanvasObject | null {
|
|
179
|
+
for (let i = objects.length - 1; i >= 0; i -= 1) {
|
|
180
|
+
const wrapper = objects[i];
|
|
181
|
+
if (!wrapper) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
if (hasContentAtPoint(wrapper, point)) {
|
|
185
|
+
return wrapper;
|
|
186
|
+
}
|
|
187
|
+
const isTextBox = wrapper.type === TEXT_OBJECT_TYPE && hitTestsWrapperAtPoint(wrapper, point);
|
|
188
|
+
if (isTextBox && !objects.slice(0, i).some(below => hasContentAtPoint(below, point))) {
|
|
189
|
+
return wrapper;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
@@ -168,6 +168,20 @@ class InlineToolBox extends React.Component {
|
|
|
168
168
|
document.addEventListener(clientPosition.isTouchEvent ? 'touchend' : 'mouseup', mouseUp);
|
|
169
169
|
};
|
|
170
170
|
|
|
171
|
+
onMoveUpClick = () => {
|
|
172
|
+
const { selectedObject } = this.props.canvas;
|
|
173
|
+
if (selectedObject && !selectedObject.isLast) {
|
|
174
|
+
selectedObject.setIndex(selectedObject.getIndex() + 1);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
onMoveDownClick = () => {
|
|
179
|
+
const { selectedObject } = this.props.canvas;
|
|
180
|
+
if (selectedObject && !selectedObject.isFirst) {
|
|
181
|
+
selectedObject.setIndex(selectedObject.getIndex() - 1);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
171
185
|
onDeleteClick = () => {
|
|
172
186
|
const { selectedObject } = this.props.canvas;
|
|
173
187
|
if (selectedObject) {
|
|
@@ -191,6 +205,10 @@ class InlineToolBox extends React.Component {
|
|
|
191
205
|
return;
|
|
192
206
|
}
|
|
193
207
|
const { topRight, bottomRight, bottomLeft, topLeft } = selectedObject.getTransformedRect();
|
|
208
|
+
const topCenter = {
|
|
209
|
+
x: (topLeft.x + topRight.x) / 2,
|
|
210
|
+
y: (topLeft.y + topRight.y) / 2,
|
|
211
|
+
};
|
|
194
212
|
this.bottomRightContainerRef.current &&
|
|
195
213
|
this.bottomRightContainerRef.current.updateTransform({
|
|
196
214
|
translate: {
|
|
@@ -223,6 +241,14 @@ class InlineToolBox extends React.Component {
|
|
|
223
241
|
},
|
|
224
242
|
rotate: selectedObject.rotation,
|
|
225
243
|
});
|
|
244
|
+
this.topCenterContainerRef.current &&
|
|
245
|
+
this.topCenterContainerRef.current.updateTransform({
|
|
246
|
+
translate: {
|
|
247
|
+
x: topCenter.x,
|
|
248
|
+
y: topCenter.y,
|
|
249
|
+
},
|
|
250
|
+
rotate: selectedObject.rotation,
|
|
251
|
+
});
|
|
226
252
|
|
|
227
253
|
const anglePosition = topRight;
|
|
228
254
|
const radAngle = (selectedObject.rotation * Math.PI) / 180;
|
|
@@ -253,13 +279,14 @@ class InlineToolBox extends React.Component {
|
|
|
253
279
|
|
|
254
280
|
bottomRightContainerRef = React.createRef();
|
|
255
281
|
topRightContainerRef = React.createRef();
|
|
282
|
+
topCenterContainerRef = React.createRef();
|
|
256
283
|
bottomLeftContainerRef = React.createRef();
|
|
257
284
|
topLeftContainerRef = React.createRef();
|
|
258
285
|
angleLabelRef = React.createRef();
|
|
259
286
|
|
|
260
287
|
renderToolboxButton = positionName => {
|
|
261
288
|
const { renderToolboxButton, canvas, selectedObject } = this.props;
|
|
262
|
-
const { onScalingChange, onRotationChange, onDeleteClick } = this;
|
|
289
|
+
const { onScalingChange, onRotationChange, onDeleteClick, onMoveUpClick, onMoveDownClick } = this;
|
|
263
290
|
if (renderToolboxButton) {
|
|
264
291
|
// call the render prop method with the position name and the available functions
|
|
265
292
|
return renderToolboxButton({
|
|
@@ -269,6 +296,10 @@ class InlineToolBox extends React.Component {
|
|
|
269
296
|
onScalingChange,
|
|
270
297
|
onRotationChange,
|
|
271
298
|
onDeleteClick,
|
|
299
|
+
onMoveUpClick,
|
|
300
|
+
onMoveDownClick,
|
|
301
|
+
isFirst: selectedObject ? selectedObject.isFirst : false,
|
|
302
|
+
isLast: selectedObject ? selectedObject.isLast : false,
|
|
272
303
|
components: {
|
|
273
304
|
InlineToolboxButton,
|
|
274
305
|
},
|
|
@@ -312,6 +343,8 @@ class InlineToolBox extends React.Component {
|
|
|
312
343
|
onClick={onDeleteClick}
|
|
313
344
|
/>
|
|
314
345
|
);
|
|
346
|
+
case 'topCenter':
|
|
347
|
+
return null;
|
|
315
348
|
case 'topLeft':
|
|
316
349
|
default:
|
|
317
350
|
return null;
|
|
@@ -348,6 +381,7 @@ class InlineToolBox extends React.Component {
|
|
|
348
381
|
const {
|
|
349
382
|
renderToolboxButton,
|
|
350
383
|
topRightContainerRef,
|
|
384
|
+
topCenterContainerRef,
|
|
351
385
|
bottomRightContainerRef,
|
|
352
386
|
bottomLeftContainerRef,
|
|
353
387
|
topLeftContainerRef,
|
|
@@ -359,6 +393,7 @@ class InlineToolBox extends React.Component {
|
|
|
359
393
|
return children({
|
|
360
394
|
refs: {
|
|
361
395
|
topRightContainerRef,
|
|
396
|
+
topCenterContainerRef,
|
|
362
397
|
bottomRightContainerRef,
|
|
363
398
|
bottomLeftContainerRef,
|
|
364
399
|
topLeftContainerRef,
|
|
@@ -383,6 +418,9 @@ class InlineToolBox extends React.Component {
|
|
|
383
418
|
<ManipulableContainer ref={topRightContainerRef} className={classes.buttonContainer}>
|
|
384
419
|
{renderToolboxButton('topRight')}
|
|
385
420
|
</ManipulableContainer>
|
|
421
|
+
<ManipulableContainer ref={topCenterContainerRef} className={classes.buttonContainer}>
|
|
422
|
+
{renderToolboxButton('topCenter')}
|
|
423
|
+
</ManipulableContainer>
|
|
386
424
|
<ManipulableContainer ref={bottomRightContainerRef} className={classes.buttonContainer}>
|
|
387
425
|
{renderToolboxButton('bottomRight')}
|
|
388
426
|
</ManipulableContainer>
|