@paperless/core 1.9.4 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/dist/build/p-2540301a.entry.js +3 -0
- package/dist/build/p-2540301a.entry.js.map +1 -0
- package/dist/build/p-2cb55f24.entry.js +2 -0
- package/dist/build/p-2cb55f24.entry.js.map +1 -0
- package/dist/build/p-fac13366.entry.js +2 -0
- package/dist/build/p-fac13366.entry.js.map +1 -0
- package/dist/build/paperless.css +1 -1
- package/dist/build/paperless.esm.js +1 -1
- package/dist/build/paperless.esm.js.map +1 -1
- package/dist/cjs/index-1fde8b14.js +4 -0
- package/dist/cjs/loader.cjs.js +1 -1
- package/dist/cjs/p-cropper.cjs.entry.js +2815 -0
- package/dist/cjs/p-cropper.cjs.entry.js.map +1 -0
- package/dist/cjs/p-modal-body_4.cjs.entry.js +3 -2
- package/dist/cjs/p-modal-body_4.cjs.entry.js.map +1 -1
- package/dist/cjs/p-modal.cjs.entry.js +2 -1
- package/dist/cjs/p-modal.cjs.entry.js.map +1 -1
- package/dist/cjs/paperless.cjs.js +1 -1
- package/dist/collection/collection-manifest.json +1 -0
- package/dist/collection/components/atoms/modal-body/modal-body.component.css +1 -1
- package/dist/collection/components/atoms/modal-body/modal-body.component.js +20 -1
- package/dist/collection/components/atoms/modal-body/modal-body.component.js.map +1 -1
- package/dist/collection/components/molecules/cropper/cropper.component.css +1 -0
- package/dist/collection/components/molecules/cropper/cropper.component.js +235 -0
- package/dist/collection/components/molecules/cropper/cropper.component.js.map +1 -0
- package/dist/collection/components/molecules/cropper/test/cropper.component.e2e.js +26 -0
- package/dist/collection/components/molecules/cropper/test/cropper.component.e2e.js.map +1 -0
- package/dist/collection/components/molecules/cropper/test/cropper.component.spec.js +17 -0
- package/dist/collection/components/molecules/cropper/test/cropper.component.spec.js.map +1 -0
- package/dist/collection/components/organisms/modal/modal.component.js +21 -2
- package/dist/collection/components/organisms/modal/modal.component.js.map +1 -1
- package/dist/components/modal-body.component.js +5 -3
- package/dist/components/modal-body.component.js.map +1 -1
- package/dist/components/p-cropper.d.ts +11 -0
- package/dist/components/p-cropper.js +2841 -0
- package/dist/components/p-cropper.js.map +1 -0
- package/dist/components/p-modal.js +3 -1
- package/dist/components/p-modal.js.map +1 -1
- package/dist/esm/index-7b917f6b.js +4 -0
- package/dist/esm/loader.js +1 -1
- package/dist/esm/p-cropper.entry.js +2811 -0
- package/dist/esm/p-cropper.entry.js.map +1 -0
- package/dist/esm/p-modal-body_4.entry.js +3 -2
- package/dist/esm/p-modal-body_4.entry.js.map +1 -1
- package/dist/esm/p-modal.entry.js +2 -1
- package/dist/esm/p-modal.entry.js.map +1 -1
- package/dist/esm/paperless.js +1 -1
- package/dist/index.html +1 -1
- package/dist/paperless/p-2540301a.entry.js +3 -0
- package/dist/paperless/p-2540301a.entry.js.map +1 -0
- package/dist/paperless/p-2cb55f24.entry.js +2 -0
- package/dist/paperless/p-2cb55f24.entry.js.map +1 -0
- package/dist/paperless/p-fac13366.entry.js +2 -0
- package/dist/paperless/p-fac13366.entry.js.map +1 -0
- package/dist/paperless/paperless.css +35 -0
- package/dist/paperless/paperless.esm.js +1 -1
- package/dist/paperless/paperless.esm.js.map +1 -1
- package/dist/style/form/_range.scss +13 -0
- package/dist/style/form/index.scss +1 -0
- package/dist/sw.js +1 -1
- package/dist/sw.js.map +1 -1
- package/dist/types/components/atoms/modal-body/modal-body.component.d.ts +4 -0
- package/dist/types/components/molecules/cropper/cropper.component.d.ts +38 -0
- package/dist/types/components/organisms/modal/modal.component.d.ts +4 -0
- package/dist/types/components.d.ts +61 -0
- package/hydrate/index.js +2834 -9
- package/package.json +3 -1
- package/dist/build/p-997962db.entry.js +0 -2
- package/dist/build/p-997962db.entry.js.map +0 -1
- package/dist/build/p-b7893a63.entry.js +0 -2
- package/dist/build/p-b7893a63.entry.js.map +0 -1
- package/dist/paperless/p-997962db.entry.js +0 -2
- package/dist/paperless/p-997962db.entry.js.map +0 -1
- package/dist/paperless/p-b7893a63.entry.js +0 -2
- package/dist/paperless/p-b7893a63.entry.js.map +0 -1
|
@@ -0,0 +1,2815 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const index = require('./index-1fde8b14.js');
|
|
6
|
+
|
|
7
|
+
function r(e){var t,f,n="";if("string"==typeof e||"number"==typeof e)n+=e;else if("object"==typeof e)if(Array.isArray(e))for(t=0;t<e.length;t++)e[t]&&(f=r(e[t]))&&(n&&(n+=" "),n+=f);else for(t in e)e[t]&&(n&&(n+=" "),n+=t);return n}function clsx(){for(var e,t,f=0,n="";f<arguments.length;)(e=arguments[f++])&&(t=r(e))&&(n&&(n+=" "),n+=t);return n}
|
|
8
|
+
|
|
9
|
+
const IS_BROWSER = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
10
|
+
const WINDOW = IS_BROWSER ? window : {};
|
|
11
|
+
const IS_TOUCH_DEVICE = IS_BROWSER ? 'ontouchstart' in WINDOW.document.documentElement : false;
|
|
12
|
+
const HAS_POINTER_EVENT = IS_BROWSER ? 'PointerEvent' in WINDOW : false;
|
|
13
|
+
const NAMESPACE = 'cropper';
|
|
14
|
+
const CROPPER_CANVAS = `${NAMESPACE}-canvas`;
|
|
15
|
+
const CROPPER_CROSSHAIR = `${NAMESPACE}-crosshair`;
|
|
16
|
+
const CROPPER_GIRD = `${NAMESPACE}-grid`;
|
|
17
|
+
const CROPPER_HANDLE = `${NAMESPACE}-handle`;
|
|
18
|
+
const CROPPER_IMAGE = `${NAMESPACE}-image`;
|
|
19
|
+
const CROPPER_SELECTION = `${NAMESPACE}-selection`;
|
|
20
|
+
const CROPPER_SHADE = `${NAMESPACE}-shade`;
|
|
21
|
+
const CROPPER_VIEWER = `${NAMESPACE}-viewer`;
|
|
22
|
+
// Actions
|
|
23
|
+
const ACTION_SELECT = 'select';
|
|
24
|
+
const ACTION_MOVE = 'move';
|
|
25
|
+
const ACTION_SCALE = 'scale';
|
|
26
|
+
const ACTION_ROTATE = 'rotate';
|
|
27
|
+
const ACTION_TRANSFORM = 'transform';
|
|
28
|
+
const ACTION_NONE = 'none';
|
|
29
|
+
const ACTION_RESIZE_NORTH = 'n-resize';
|
|
30
|
+
const ACTION_RESIZE_EAST = 'e-resize';
|
|
31
|
+
const ACTION_RESIZE_SOUTH = 's-resize';
|
|
32
|
+
const ACTION_RESIZE_WEST = 'w-resize';
|
|
33
|
+
const ACTION_RESIZE_NORTHEAST = 'ne-resize';
|
|
34
|
+
const ACTION_RESIZE_NORTHWEST = 'nw-resize';
|
|
35
|
+
const ACTION_RESIZE_SOUTHEAST = 'se-resize';
|
|
36
|
+
const ACTION_RESIZE_SOUTHWEST = 'sw-resize';
|
|
37
|
+
// Attributes
|
|
38
|
+
const ATTRIBUTE_ACTION = 'action';
|
|
39
|
+
// Native events
|
|
40
|
+
const EVENT_TOUCH_END = IS_TOUCH_DEVICE ? 'touchend touchcancel' : 'mouseup';
|
|
41
|
+
const EVENT_TOUCH_MOVE = IS_TOUCH_DEVICE ? 'touchmove' : 'mousemove';
|
|
42
|
+
const EVENT_TOUCH_START = IS_TOUCH_DEVICE ? 'touchstart' : 'mousedown';
|
|
43
|
+
const EVENT_POINTER_DOWN = HAS_POINTER_EVENT ? 'pointerdown' : EVENT_TOUCH_START;
|
|
44
|
+
const EVENT_POINTER_MOVE = HAS_POINTER_EVENT ? 'pointermove' : EVENT_TOUCH_MOVE;
|
|
45
|
+
const EVENT_POINTER_UP = HAS_POINTER_EVENT ? 'pointerup pointercancel' : EVENT_TOUCH_END;
|
|
46
|
+
const EVENT_ERROR = 'error';
|
|
47
|
+
const EVENT_KEYDOWN = 'keydown';
|
|
48
|
+
const EVENT_LOAD = 'load';
|
|
49
|
+
const EVENT_WHEEL = 'wheel';
|
|
50
|
+
// Custom events
|
|
51
|
+
const EVENT_ACTION = 'action';
|
|
52
|
+
const EVENT_ACTION_END = 'actionend';
|
|
53
|
+
const EVENT_ACTION_MOVE = 'actionmove';
|
|
54
|
+
const EVENT_ACTION_START = 'actionstart';
|
|
55
|
+
const EVENT_CHANGE = 'change';
|
|
56
|
+
const EVENT_TRANSFORM = 'transform';
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Check if the given value is a string.
|
|
60
|
+
* @param {*} value The value to check.
|
|
61
|
+
* @returns {boolean} Returns `true` if the given value is a string, else `false`.
|
|
62
|
+
*/
|
|
63
|
+
function isString(value) {
|
|
64
|
+
return typeof value === 'string';
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check if the given value is not a number.
|
|
68
|
+
*/
|
|
69
|
+
const isNaN = Number.isNaN || WINDOW.isNaN;
|
|
70
|
+
/**
|
|
71
|
+
* Check if the given value is a number.
|
|
72
|
+
* @param {*} value The value to check.
|
|
73
|
+
* @returns {boolean} Returns `true` if the given value is a number, else `false`.
|
|
74
|
+
*/
|
|
75
|
+
function isNumber(value) {
|
|
76
|
+
return typeof value === 'number' && !isNaN(value);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if the given value is a positive number.
|
|
80
|
+
* @param {*} value The value to check.
|
|
81
|
+
* @returns {boolean} Returns `true` if the given value is a positive number, else `false`.
|
|
82
|
+
*/
|
|
83
|
+
function isPositiveNumber(value) {
|
|
84
|
+
return isNumber(value) && value > 0 && value < Infinity;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Check if the given value is undefined.
|
|
88
|
+
* @param {*} value The value to check.
|
|
89
|
+
* @returns {boolean} Returns `true` if the given value is undefined, else `false`.
|
|
90
|
+
*/
|
|
91
|
+
function isUndefined(value) {
|
|
92
|
+
return typeof value === 'undefined';
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if the given value is an object.
|
|
96
|
+
* @param {*} value - The value to check.
|
|
97
|
+
* @returns {boolean} Returns `true` if the given value is an object, else `false`.
|
|
98
|
+
*/
|
|
99
|
+
function isObject(value) {
|
|
100
|
+
return typeof value === 'object' && value !== null;
|
|
101
|
+
}
|
|
102
|
+
const { hasOwnProperty } = Object.prototype;
|
|
103
|
+
/**
|
|
104
|
+
* Check if the given value is a plain object.
|
|
105
|
+
* @param {*} value - The value to check.
|
|
106
|
+
* @returns {boolean} Returns `true` if the given value is a plain object, else `false`.
|
|
107
|
+
*/
|
|
108
|
+
function isPlainObject(value) {
|
|
109
|
+
if (!isObject(value)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const { constructor } = value;
|
|
114
|
+
const { prototype } = constructor;
|
|
115
|
+
return constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf');
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if the given value is a function.
|
|
123
|
+
* @param {*} value The value to check.
|
|
124
|
+
* @returns {boolean} Returns `true` if the given value is a function, else `false`.
|
|
125
|
+
*/
|
|
126
|
+
function isFunction(value) {
|
|
127
|
+
return typeof value === 'function';
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Check if the given node is an element.
|
|
131
|
+
* @param {*} node The node to check.
|
|
132
|
+
* @returns {boolean} Returns `true` if the given node is an element; otherwise, `false`.
|
|
133
|
+
*/
|
|
134
|
+
function isElement(node) {
|
|
135
|
+
return typeof node === 'object' && node !== null && node.nodeType === 1;
|
|
136
|
+
}
|
|
137
|
+
const REGEXP_CAMEL_CASE = /([a-z\d])([A-Z])/g;
|
|
138
|
+
/**
|
|
139
|
+
* Transform the given string from camelCase to kebab-case.
|
|
140
|
+
* @param {string} value The value to transform.
|
|
141
|
+
* @returns {string} Returns the transformed value.
|
|
142
|
+
*/
|
|
143
|
+
function toKebabCase(value) {
|
|
144
|
+
return String(value).replace(REGEXP_CAMEL_CASE, '$1-$2').toLowerCase();
|
|
145
|
+
}
|
|
146
|
+
const REGEXP_KEBAB_CASE = /-[A-z\d]/g;
|
|
147
|
+
/**
|
|
148
|
+
* Transform the given string from kebab-case to camelCase.
|
|
149
|
+
* @param {string} value The value to transform.
|
|
150
|
+
* @returns {string} Returns the transformed value.
|
|
151
|
+
*/
|
|
152
|
+
function toCamelCase(value) {
|
|
153
|
+
return value.replace(REGEXP_KEBAB_CASE, (substring) => substring.slice(1).toUpperCase());
|
|
154
|
+
}
|
|
155
|
+
const REGEXP_SPACES = /\s\s*/;
|
|
156
|
+
/**
|
|
157
|
+
* Remove event listener from the event target.
|
|
158
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener}
|
|
159
|
+
* @param {EventTarget} target The target of the event.
|
|
160
|
+
* @param {string} types The types of the event.
|
|
161
|
+
* @param {EventListenerOrEventListenerObject} listener The listener of the event.
|
|
162
|
+
* @param {EventListenerOptions} [options] The options specify characteristics about the event listener.
|
|
163
|
+
*/
|
|
164
|
+
function off(target, types, listener, options) {
|
|
165
|
+
types.trim().split(REGEXP_SPACES).forEach((type) => {
|
|
166
|
+
target.removeEventListener(type, listener, options);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Add event listener to the event target.
|
|
171
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener}
|
|
172
|
+
* @param {EventTarget} target The target of the event.
|
|
173
|
+
* @param {string} types The types of the event.
|
|
174
|
+
* @param {EventListenerOrEventListenerObject} listener The listener of the event.
|
|
175
|
+
* @param {AddEventListenerOptions} [options] The options specify characteristics about the event listener.
|
|
176
|
+
*/
|
|
177
|
+
function on(target, types, listener, options) {
|
|
178
|
+
types.trim().split(REGEXP_SPACES).forEach((type) => {
|
|
179
|
+
target.addEventListener(type, listener, options);
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Add once event listener to the event target.
|
|
184
|
+
* @param {EventTarget} target The target of the event.
|
|
185
|
+
* @param {string} types The types of the event.
|
|
186
|
+
* @param {EventListenerOrEventListenerObject} listener The listener of the event.
|
|
187
|
+
* @param {AddEventListenerOptions} [options] The options specify characteristics about the event listener.
|
|
188
|
+
*/
|
|
189
|
+
function once(target, types, listener, options) {
|
|
190
|
+
on(target, types, listener, Object.assign(Object.assign({}, options), { once: true }));
|
|
191
|
+
}
|
|
192
|
+
const defaultEventOptions = {
|
|
193
|
+
bubbles: true,
|
|
194
|
+
cancelable: true,
|
|
195
|
+
composed: true,
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Dispatch event on the event target.
|
|
199
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent}
|
|
200
|
+
* @param {EventTarget} target The target of the event.
|
|
201
|
+
* @param {string} type The name of the event.
|
|
202
|
+
* @param {*} [detail] The data passed when initializing the event.
|
|
203
|
+
* @param {CustomEventInit} [options] The other event options.
|
|
204
|
+
* @returns {boolean} Returns the result value.
|
|
205
|
+
*/
|
|
206
|
+
function emit(target, type, detail, options) {
|
|
207
|
+
return target.dispatchEvent(new CustomEvent(type, Object.assign(Object.assign(Object.assign({}, defaultEventOptions), { detail }), options)));
|
|
208
|
+
}
|
|
209
|
+
const resolvedPromise = Promise.resolve();
|
|
210
|
+
/**
|
|
211
|
+
* Defers the callback to be executed after the next DOM update cycle.
|
|
212
|
+
* @param {*} [context] The `this` context.
|
|
213
|
+
* @param {Function} [callback] The callback to execute after the next DOM update cycle.
|
|
214
|
+
* @returns {Promise} A promise that resolves to nothing.
|
|
215
|
+
*/
|
|
216
|
+
function nextTick(context, callback) {
|
|
217
|
+
return callback
|
|
218
|
+
? resolvedPromise.then(context ? callback.bind(context) : callback)
|
|
219
|
+
: resolvedPromise;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get the offset base on the document.
|
|
223
|
+
* @param {Element} element The target element.
|
|
224
|
+
* @returns {object} The offset data.
|
|
225
|
+
*/
|
|
226
|
+
function getOffset(element) {
|
|
227
|
+
const { documentElement } = element.ownerDocument;
|
|
228
|
+
const box = element.getBoundingClientRect();
|
|
229
|
+
return {
|
|
230
|
+
left: box.left + (WINDOW.pageXOffset - documentElement.clientLeft),
|
|
231
|
+
top: box.top + (WINDOW.pageYOffset - documentElement.clientTop),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
const REGEXP_ANGLE_UNIT = /deg|g?rad|turn$/i;
|
|
235
|
+
/**
|
|
236
|
+
* Convert an angle to a radian number.
|
|
237
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/angle}
|
|
238
|
+
* @param {number|string} angle The angle to convert.
|
|
239
|
+
* @returns {number} Returns the radian number.
|
|
240
|
+
*/
|
|
241
|
+
function toAngleInRadian(angle) {
|
|
242
|
+
const value = parseFloat(angle) || 0;
|
|
243
|
+
if (value !== 0) {
|
|
244
|
+
const [unit = 'rad'] = String(angle).match(REGEXP_ANGLE_UNIT) || [];
|
|
245
|
+
switch (unit.toLowerCase()) {
|
|
246
|
+
case 'deg':
|
|
247
|
+
return (value / 360) * (Math.PI * 2);
|
|
248
|
+
case 'grad':
|
|
249
|
+
return (value / 400) * (Math.PI * 2);
|
|
250
|
+
case 'turn':
|
|
251
|
+
return value * (Math.PI * 2);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return value;
|
|
255
|
+
}
|
|
256
|
+
const SIZE_ADJUSTMENT_TYPE_CONTAIN = 'contain';
|
|
257
|
+
const SIZE_ADJUSTMENT_TYPE_COVER = 'cover';
|
|
258
|
+
/**
|
|
259
|
+
* Get the max sizes in a rectangle under the given aspect ratio.
|
|
260
|
+
* @param {object} data The original sizes.
|
|
261
|
+
* @param {string} [type] The adjust type.
|
|
262
|
+
* @returns {object} Returns the result sizes.
|
|
263
|
+
*/
|
|
264
|
+
function getAdjustedSizes(data, type = SIZE_ADJUSTMENT_TYPE_CONTAIN) {
|
|
265
|
+
const { aspectRatio } = data;
|
|
266
|
+
let { width, height } = data;
|
|
267
|
+
const isValidWidth = isPositiveNumber(width);
|
|
268
|
+
const isValidHeight = isPositiveNumber(height);
|
|
269
|
+
if (isValidWidth && isValidHeight) {
|
|
270
|
+
const adjustedWidth = height * aspectRatio;
|
|
271
|
+
if ((type === SIZE_ADJUSTMENT_TYPE_CONTAIN && adjustedWidth > width)
|
|
272
|
+
|| (type === SIZE_ADJUSTMENT_TYPE_COVER && adjustedWidth < width)) {
|
|
273
|
+
height = width / aspectRatio;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
width = height * aspectRatio;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
else if (isValidWidth) {
|
|
280
|
+
height = width / aspectRatio;
|
|
281
|
+
}
|
|
282
|
+
else if (isValidHeight) {
|
|
283
|
+
width = height * aspectRatio;
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
width,
|
|
287
|
+
height,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Multiply multiple matrices.
|
|
292
|
+
* @param {Array} matrix The first matrix.
|
|
293
|
+
* @param {Array} args The rest matrices.
|
|
294
|
+
* @returns {Array} Returns the result matrix.
|
|
295
|
+
*/
|
|
296
|
+
function multiplyMatrices(matrix, ...args) {
|
|
297
|
+
if (args.length === 0) {
|
|
298
|
+
return matrix;
|
|
299
|
+
}
|
|
300
|
+
const [a1, b1, c1, d1, e1, f1] = matrix;
|
|
301
|
+
const [a2, b2, c2, d2, e2, f2] = args[0];
|
|
302
|
+
// ┌ a1 c1 e1 ┐ ┌ a2 c2 e2 ┐
|
|
303
|
+
// │ b1 d1 f1 │ × │ b2 d2 f2 │
|
|
304
|
+
// └ 0 0 1 ┘ └ 0 0 1 ┘
|
|
305
|
+
matrix = [
|
|
306
|
+
a1 * a2 + c1 * b2 /* + e1 * 0 */,
|
|
307
|
+
b1 * a2 + d1 * b2 /* + f1 * 0 */,
|
|
308
|
+
a1 * c2 + c1 * d2 /* + e1 * 0 */,
|
|
309
|
+
b1 * c2 + d1 * d2 /* + f1 * 0 */,
|
|
310
|
+
a1 * e2 + c1 * f2 + e1 /* * 1 */,
|
|
311
|
+
b1 * e2 + d1 * f2 + f1 /* * 1 */,
|
|
312
|
+
];
|
|
313
|
+
return multiplyMatrices(matrix, ...args.slice(1));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
var style$8 = `:host([hidden]){display:none!important}`;
|
|
317
|
+
|
|
318
|
+
const REGEXP_SUFFIX = /left|top|width|height/i;
|
|
319
|
+
const DEFAULT_SHADOW_ROOT_MODE = 'open';
|
|
320
|
+
const shadowRoots = new WeakMap();
|
|
321
|
+
const styleSheets = new WeakMap();
|
|
322
|
+
const tagNames = new Map();
|
|
323
|
+
const supportsAdoptedStyleSheets = WINDOW.document && Array.isArray(WINDOW.document.adoptedStyleSheets) && 'replaceSync' in WINDOW.CSSStyleSheet.prototype;
|
|
324
|
+
class CropperElement extends HTMLElement {
|
|
325
|
+
get $sharedStyle() {
|
|
326
|
+
return `${this.themeColor ? `:host{--theme-color: ${this.themeColor};}` : ''}${style$8}`;
|
|
327
|
+
}
|
|
328
|
+
constructor() {
|
|
329
|
+
var _a, _b;
|
|
330
|
+
super();
|
|
331
|
+
this.shadowRootMode = DEFAULT_SHADOW_ROOT_MODE;
|
|
332
|
+
this.slottable = true;
|
|
333
|
+
const name = (_b = (_a = Object.getPrototypeOf(this)) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.$name;
|
|
334
|
+
if (name) {
|
|
335
|
+
tagNames.set(name, this.tagName.toLowerCase());
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
static get observedAttributes() {
|
|
339
|
+
return [
|
|
340
|
+
'shadow-root-mode',
|
|
341
|
+
'slottable',
|
|
342
|
+
'theme-color',
|
|
343
|
+
];
|
|
344
|
+
}
|
|
345
|
+
// Convert attribute to property
|
|
346
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
347
|
+
if (Object.is(newValue, oldValue)) {
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const propertyName = toCamelCase(name);
|
|
351
|
+
const oldPropertyValue = this[propertyName];
|
|
352
|
+
let newPropertyValue = newValue;
|
|
353
|
+
switch (typeof oldPropertyValue) {
|
|
354
|
+
case 'boolean':
|
|
355
|
+
newPropertyValue = newValue !== null && newValue !== 'false';
|
|
356
|
+
break;
|
|
357
|
+
case 'number':
|
|
358
|
+
newPropertyValue = Number(newValue);
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
this[propertyName] = newPropertyValue;
|
|
362
|
+
switch (name) {
|
|
363
|
+
case 'theme-color': {
|
|
364
|
+
const styleSheet = styleSheets.get(this);
|
|
365
|
+
const styles = this.$sharedStyle;
|
|
366
|
+
if (styleSheet && styles) {
|
|
367
|
+
if (supportsAdoptedStyleSheets) {
|
|
368
|
+
styleSheet.replaceSync(styles);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
styleSheet.textContent = styles;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// Convert property to attribute
|
|
379
|
+
$propertyChangedCallback(name, oldValue, newValue) {
|
|
380
|
+
if (Object.is(newValue, oldValue)) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
name = toKebabCase(name);
|
|
384
|
+
switch (typeof newValue) {
|
|
385
|
+
case 'boolean':
|
|
386
|
+
if (newValue === true) {
|
|
387
|
+
if (!this.hasAttribute(name)) {
|
|
388
|
+
this.setAttribute(name, '');
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else {
|
|
392
|
+
this.removeAttribute(name);
|
|
393
|
+
}
|
|
394
|
+
break;
|
|
395
|
+
case 'number':
|
|
396
|
+
if (isNaN(newValue)) {
|
|
397
|
+
newValue = '';
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
newValue = String(newValue);
|
|
401
|
+
}
|
|
402
|
+
// Fall through
|
|
403
|
+
// case 'string':
|
|
404
|
+
// eslint-disable-next-line no-fallthrough
|
|
405
|
+
default:
|
|
406
|
+
if (newValue) {
|
|
407
|
+
if (this.getAttribute(name) !== newValue) {
|
|
408
|
+
this.setAttribute(name, newValue);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
this.removeAttribute(name);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
connectedCallback() {
|
|
417
|
+
// Observe properties after observed attributes
|
|
418
|
+
Object.getPrototypeOf(this).constructor.observedAttributes.forEach((attribute) => {
|
|
419
|
+
const property = toCamelCase(attribute);
|
|
420
|
+
let value = this[property];
|
|
421
|
+
if (!isUndefined(value)) {
|
|
422
|
+
this.$propertyChangedCallback(property, undefined, value);
|
|
423
|
+
}
|
|
424
|
+
Object.defineProperty(this, property, {
|
|
425
|
+
enumerable: true,
|
|
426
|
+
configurable: true,
|
|
427
|
+
get() {
|
|
428
|
+
return value;
|
|
429
|
+
},
|
|
430
|
+
set(newValue) {
|
|
431
|
+
const oldValue = value;
|
|
432
|
+
value = newValue;
|
|
433
|
+
this.$propertyChangedCallback(property, oldValue, newValue);
|
|
434
|
+
},
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
const shadow = this.attachShadow({
|
|
438
|
+
mode: this.shadowRootMode || DEFAULT_SHADOW_ROOT_MODE,
|
|
439
|
+
});
|
|
440
|
+
if (!this.shadowRoot) {
|
|
441
|
+
shadowRoots.set(this, shadow);
|
|
442
|
+
}
|
|
443
|
+
styleSheets.set(this, this.$addStyles(this.$sharedStyle));
|
|
444
|
+
if (this.$style) {
|
|
445
|
+
this.$addStyles(this.$style);
|
|
446
|
+
}
|
|
447
|
+
if (this.$template) {
|
|
448
|
+
const template = document.createElement('template');
|
|
449
|
+
template.innerHTML = this.$template;
|
|
450
|
+
shadow.appendChild(template.content);
|
|
451
|
+
}
|
|
452
|
+
if (this.slottable) {
|
|
453
|
+
const slot = document.createElement('slot');
|
|
454
|
+
shadow.appendChild(slot);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
disconnectedCallback() {
|
|
458
|
+
if (styleSheets.has(this)) {
|
|
459
|
+
styleSheets.delete(this);
|
|
460
|
+
}
|
|
461
|
+
if (shadowRoots.has(this)) {
|
|
462
|
+
shadowRoots.delete(this);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
// eslint-disable-next-line class-methods-use-this
|
|
466
|
+
$getTagNameOf(name) {
|
|
467
|
+
var _a;
|
|
468
|
+
return (_a = tagNames.get(name)) !== null && _a !== void 0 ? _a : name;
|
|
469
|
+
}
|
|
470
|
+
$setStyles(properties) {
|
|
471
|
+
Object.keys(properties).forEach((property) => {
|
|
472
|
+
let value = properties[property];
|
|
473
|
+
if (isNumber(value)) {
|
|
474
|
+
if (value !== 0 && REGEXP_SUFFIX.test(property)) {
|
|
475
|
+
value = `${value}px`;
|
|
476
|
+
}
|
|
477
|
+
else {
|
|
478
|
+
value = String(value);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
this.style[property] = value;
|
|
482
|
+
});
|
|
483
|
+
return this;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Outputs the shadow root of the element.
|
|
487
|
+
* @returns {ShadowRoot} Returns the shadow root.
|
|
488
|
+
*/
|
|
489
|
+
$getShadowRoot() {
|
|
490
|
+
return this.shadowRoot || shadowRoots.get(this);
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Adds styles to the shadow root.
|
|
494
|
+
* @param {string} styles The styles to add.
|
|
495
|
+
* @returns {CSSStyleSheet|HTMLStyleElement} Returns the generated style sheet.
|
|
496
|
+
*/
|
|
497
|
+
$addStyles(styles) {
|
|
498
|
+
let styleSheet;
|
|
499
|
+
const shadow = this.$getShadowRoot();
|
|
500
|
+
if (supportsAdoptedStyleSheets) {
|
|
501
|
+
styleSheet = new CSSStyleSheet();
|
|
502
|
+
styleSheet.replaceSync(styles);
|
|
503
|
+
shadow.adoptedStyleSheets = shadow.adoptedStyleSheets.concat(styleSheet);
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
styleSheet = document.createElement('style');
|
|
507
|
+
styleSheet.textContent = styles;
|
|
508
|
+
shadow.appendChild(styleSheet);
|
|
509
|
+
}
|
|
510
|
+
return styleSheet;
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Dispatches an event at the element.
|
|
514
|
+
* @param {string} type The name of the event.
|
|
515
|
+
* @param {*} [detail] The data passed when initializing the event.
|
|
516
|
+
* @param {CustomEventInit} [options] The other event options.
|
|
517
|
+
* @returns {boolean} Returns the result value.
|
|
518
|
+
*/
|
|
519
|
+
$emit(type, detail, options) {
|
|
520
|
+
return emit(this, type, detail, options);
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Defers the callback to be executed after the next DOM update cycle.
|
|
524
|
+
* @param {Function} [callback] The callback to execute after the next DOM update cycle.
|
|
525
|
+
* @returns {Promise} A promise that resolves to nothing.
|
|
526
|
+
*/
|
|
527
|
+
$nextTick(callback) {
|
|
528
|
+
return nextTick(this, callback);
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Defines the constructor as a new custom element.
|
|
532
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define}
|
|
533
|
+
* @param {string|object} [name] The element name.
|
|
534
|
+
* @param {object} [options] The element definition options.
|
|
535
|
+
*/
|
|
536
|
+
static $define(name, options) {
|
|
537
|
+
if (isObject(name)) {
|
|
538
|
+
options = name;
|
|
539
|
+
name = '';
|
|
540
|
+
}
|
|
541
|
+
if (!name) {
|
|
542
|
+
name = this.$name || this.name;
|
|
543
|
+
}
|
|
544
|
+
name = toKebabCase(name);
|
|
545
|
+
if (IS_BROWSER && WINDOW.customElements && !WINDOW.customElements.get(name)) {
|
|
546
|
+
customElements.define(name, this, options);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
CropperElement.$version = '2.0.0-beta.4';
|
|
551
|
+
|
|
552
|
+
var style$7 = `:host{display:block;min-height:100px;min-width:200px;overflow:hidden;position:relative;touch-action:none;-webkit-user-select:none;-moz-user-select:none;user-select:none}:host([background]){background-color:#fff;background-image:repeating-linear-gradient(45deg,#ccc 25%,transparent 0,transparent 75%,#ccc 0,#ccc),repeating-linear-gradient(45deg,#ccc 25%,transparent 0,transparent 75%,#ccc 0,#ccc);background-image:repeating-conic-gradient(#ccc 0 25%,#fff 0 50%);background-position:0 0,.5rem .5rem;background-size:1rem 1rem}:host([disabled]){pointer-events:none}:host([disabled]):after{bottom:0;content:"";cursor:not-allowed;display:block;left:0;pointer-events:none;position:absolute;right:0;top:0}`;
|
|
553
|
+
|
|
554
|
+
class CropperCanvas extends CropperElement {
|
|
555
|
+
constructor() {
|
|
556
|
+
super(...arguments);
|
|
557
|
+
this.$onPointerDown = null;
|
|
558
|
+
this.$onPointerMove = null;
|
|
559
|
+
this.$onPointerUp = null;
|
|
560
|
+
this.$onWheel = null;
|
|
561
|
+
this.$wheeling = false;
|
|
562
|
+
this.$pointers = new Map();
|
|
563
|
+
this.$style = style$7;
|
|
564
|
+
this.$action = ACTION_NONE;
|
|
565
|
+
this.background = false;
|
|
566
|
+
this.disabled = false;
|
|
567
|
+
this.scaleStep = 0.1;
|
|
568
|
+
this.themeColor = '#39f';
|
|
569
|
+
}
|
|
570
|
+
static get observedAttributes() {
|
|
571
|
+
return super.observedAttributes.concat([
|
|
572
|
+
'background',
|
|
573
|
+
'disabled',
|
|
574
|
+
'scale-step',
|
|
575
|
+
]);
|
|
576
|
+
}
|
|
577
|
+
connectedCallback() {
|
|
578
|
+
super.connectedCallback();
|
|
579
|
+
if (!this.disabled) {
|
|
580
|
+
this.$bind();
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
disconnectedCallback() {
|
|
584
|
+
if (!this.disabled) {
|
|
585
|
+
this.$unbind();
|
|
586
|
+
}
|
|
587
|
+
super.disconnectedCallback();
|
|
588
|
+
}
|
|
589
|
+
$propertyChangedCallback(name, oldValue, newValue) {
|
|
590
|
+
if (Object.is(newValue, oldValue)) {
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
super.$propertyChangedCallback(name, oldValue, newValue);
|
|
594
|
+
switch (name) {
|
|
595
|
+
case 'disabled':
|
|
596
|
+
if (newValue) {
|
|
597
|
+
this.$unbind();
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
this.$bind();
|
|
601
|
+
}
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
$bind() {
|
|
606
|
+
if (!this.$onPointerDown) {
|
|
607
|
+
this.$onPointerDown = this.$handlePointerDown.bind(this);
|
|
608
|
+
on(this, EVENT_POINTER_DOWN, this.$onPointerDown);
|
|
609
|
+
}
|
|
610
|
+
if (!this.$onPointerMove) {
|
|
611
|
+
this.$onPointerMove = this.$handlePointerMove.bind(this);
|
|
612
|
+
on(this.ownerDocument, EVENT_POINTER_MOVE, this.$onPointerMove);
|
|
613
|
+
}
|
|
614
|
+
if (!this.$onPointerUp) {
|
|
615
|
+
this.$onPointerUp = this.$handlePointerUp.bind(this);
|
|
616
|
+
on(this.ownerDocument, EVENT_POINTER_UP, this.$onPointerUp);
|
|
617
|
+
}
|
|
618
|
+
if (!this.$onWheel) {
|
|
619
|
+
this.$onWheel = this.$handleWheel.bind(this);
|
|
620
|
+
on(this, EVENT_WHEEL, this.$onWheel, {
|
|
621
|
+
passive: false,
|
|
622
|
+
capture: true,
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
$unbind() {
|
|
627
|
+
if (this.$onPointerDown) {
|
|
628
|
+
off(this, EVENT_POINTER_DOWN, this.$onPointerDown);
|
|
629
|
+
this.$onPointerDown = null;
|
|
630
|
+
}
|
|
631
|
+
if (this.$onPointerMove) {
|
|
632
|
+
off(this.ownerDocument, EVENT_POINTER_MOVE, this.$onPointerMove);
|
|
633
|
+
this.$onPointerMove = null;
|
|
634
|
+
}
|
|
635
|
+
if (this.$onPointerUp) {
|
|
636
|
+
off(this.ownerDocument, EVENT_POINTER_UP, this.$onPointerUp);
|
|
637
|
+
this.$onPointerUp = null;
|
|
638
|
+
}
|
|
639
|
+
if (this.$onWheel) {
|
|
640
|
+
off(this, EVENT_WHEEL, this.$onWheel, {
|
|
641
|
+
capture: true,
|
|
642
|
+
});
|
|
643
|
+
this.$onWheel = null;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
$handlePointerDown(event) {
|
|
647
|
+
const { buttons, button, type } = event;
|
|
648
|
+
if (this.disabled || (
|
|
649
|
+
// Handle pointer or mouse event, and ignore touch event
|
|
650
|
+
((type === 'pointerdown' && event.pointerType === 'mouse') || type === 'mousedown') && (
|
|
651
|
+
// No primary button (Usually the left button)
|
|
652
|
+
(isNumber(buttons) && buttons !== 1) || (isNumber(button) && button !== 0)
|
|
653
|
+
// Open context menu
|
|
654
|
+
|| event.ctrlKey))) {
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
const { $pointers } = this;
|
|
658
|
+
let action = '';
|
|
659
|
+
if (event.changedTouches) {
|
|
660
|
+
Array.from(event.changedTouches).forEach(({ identifier, pageX, pageY, }) => {
|
|
661
|
+
$pointers.set(identifier, {
|
|
662
|
+
startX: pageX,
|
|
663
|
+
startY: pageY,
|
|
664
|
+
endX: pageX,
|
|
665
|
+
endY: pageY,
|
|
666
|
+
});
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
else {
|
|
670
|
+
const { pointerId = 0, pageX, pageY } = event;
|
|
671
|
+
$pointers.set(pointerId, {
|
|
672
|
+
startX: pageX,
|
|
673
|
+
startY: pageY,
|
|
674
|
+
endX: pageX,
|
|
675
|
+
endY: pageY,
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
if ($pointers.size > 1) {
|
|
679
|
+
action = ACTION_TRANSFORM;
|
|
680
|
+
}
|
|
681
|
+
else if (isElement(event.target)) {
|
|
682
|
+
action = event.target.action || event.target.getAttribute(ATTRIBUTE_ACTION) || '';
|
|
683
|
+
}
|
|
684
|
+
if (this.$emit(EVENT_ACTION_START, {
|
|
685
|
+
action,
|
|
686
|
+
relatedEvent: event,
|
|
687
|
+
}) === false) {
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
// Prevent page zooming in the browsers for iOS.
|
|
691
|
+
event.preventDefault();
|
|
692
|
+
this.$action = action;
|
|
693
|
+
this.style.willChange = 'transform';
|
|
694
|
+
}
|
|
695
|
+
$handlePointerMove(event) {
|
|
696
|
+
const { $action, $pointers } = this;
|
|
697
|
+
if (this.disabled || $action === ACTION_NONE || $pointers.size === 0) {
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
if (this.$emit(EVENT_ACTION_MOVE, {
|
|
701
|
+
action: $action,
|
|
702
|
+
relatedEvent: event,
|
|
703
|
+
}) === false) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
// Prevent page scrolling.
|
|
707
|
+
event.preventDefault();
|
|
708
|
+
if (event.changedTouches) {
|
|
709
|
+
Array.from(event.changedTouches).forEach(({ identifier, pageX, pageY, }) => {
|
|
710
|
+
const pointer = $pointers.get(identifier);
|
|
711
|
+
if (pointer) {
|
|
712
|
+
Object.assign(pointer, {
|
|
713
|
+
endX: pageX,
|
|
714
|
+
endY: pageY,
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
else {
|
|
720
|
+
const { pointerId = 0, pageX, pageY } = event;
|
|
721
|
+
const pointer = $pointers.get(pointerId);
|
|
722
|
+
if (pointer) {
|
|
723
|
+
Object.assign(pointer, {
|
|
724
|
+
endX: pageX,
|
|
725
|
+
endY: pageY,
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
const detail = {
|
|
730
|
+
action: $action,
|
|
731
|
+
relatedEvent: event,
|
|
732
|
+
};
|
|
733
|
+
if ($action === ACTION_TRANSFORM) {
|
|
734
|
+
const pointers2 = new Map($pointers);
|
|
735
|
+
let maxRotateRate = 0;
|
|
736
|
+
let maxScaleRate = 0;
|
|
737
|
+
let rotate = 0;
|
|
738
|
+
let scale = 0;
|
|
739
|
+
let centerX = event.pageX;
|
|
740
|
+
let centerY = event.pageY;
|
|
741
|
+
$pointers.forEach((pointer, pointerId) => {
|
|
742
|
+
pointers2.delete(pointerId);
|
|
743
|
+
pointers2.forEach((pointer2) => {
|
|
744
|
+
let x1 = pointer2.startX - pointer.startX;
|
|
745
|
+
let y1 = pointer2.startY - pointer.startY;
|
|
746
|
+
let x2 = pointer2.endX - pointer.endX;
|
|
747
|
+
let y2 = pointer2.endY - pointer.endY;
|
|
748
|
+
let z1 = 0;
|
|
749
|
+
let z2 = 0;
|
|
750
|
+
let a1 = 0;
|
|
751
|
+
let a2 = 0;
|
|
752
|
+
if (x1 === 0) {
|
|
753
|
+
if (y1 < 0) {
|
|
754
|
+
a1 = Math.PI * 2;
|
|
755
|
+
}
|
|
756
|
+
else if (y1 > 0) {
|
|
757
|
+
a1 = Math.PI;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
else if (x1 > 0) {
|
|
761
|
+
a1 = (Math.PI / 2) + Math.atan(y1 / x1);
|
|
762
|
+
}
|
|
763
|
+
else if (x1 < 0) {
|
|
764
|
+
a1 = (Math.PI * 1.5) + Math.atan(y1 / x1);
|
|
765
|
+
}
|
|
766
|
+
if (x2 === 0) {
|
|
767
|
+
if (y2 < 0) {
|
|
768
|
+
a2 = Math.PI * 2;
|
|
769
|
+
}
|
|
770
|
+
else if (y2 > 0) {
|
|
771
|
+
a2 = Math.PI;
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
else if (x2 > 0) {
|
|
775
|
+
a2 = (Math.PI / 2) + Math.atan(y2 / x2);
|
|
776
|
+
}
|
|
777
|
+
else if (x2 < 0) {
|
|
778
|
+
a2 = (Math.PI * 1.5) + Math.atan(y2 / x2);
|
|
779
|
+
}
|
|
780
|
+
if (a2 > 0 || a1 > 0) {
|
|
781
|
+
const rotateRate = a2 - a1;
|
|
782
|
+
const absRotateRate = Math.abs(rotateRate);
|
|
783
|
+
if (absRotateRate > maxRotateRate) {
|
|
784
|
+
maxRotateRate = absRotateRate;
|
|
785
|
+
rotate = rotateRate;
|
|
786
|
+
centerX = (pointer.startX + pointer2.startX) / 2;
|
|
787
|
+
centerY = (pointer.startY + pointer2.startY) / 2;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
x1 = Math.abs(x1);
|
|
791
|
+
y1 = Math.abs(y1);
|
|
792
|
+
x2 = Math.abs(x2);
|
|
793
|
+
y2 = Math.abs(y2);
|
|
794
|
+
if (x1 > 0 && y1 > 0) {
|
|
795
|
+
z1 = Math.sqrt((x1 * x1) + (y1 * y1));
|
|
796
|
+
}
|
|
797
|
+
else if (x1 > 0) {
|
|
798
|
+
z1 = x1;
|
|
799
|
+
}
|
|
800
|
+
else if (y1 > 0) {
|
|
801
|
+
z1 = y1;
|
|
802
|
+
}
|
|
803
|
+
if (x2 > 0 && y2 > 0) {
|
|
804
|
+
z2 = Math.sqrt((x2 * x2) + (y2 * y2));
|
|
805
|
+
}
|
|
806
|
+
else if (x2 > 0) {
|
|
807
|
+
z2 = x2;
|
|
808
|
+
}
|
|
809
|
+
else if (y2 > 0) {
|
|
810
|
+
z2 = y2;
|
|
811
|
+
}
|
|
812
|
+
if (z1 > 0 && z2 > 0) {
|
|
813
|
+
const scaleRate = (z2 - z1) / z1;
|
|
814
|
+
const absScaleRate = Math.abs(scaleRate);
|
|
815
|
+
if (absScaleRate > maxScaleRate) {
|
|
816
|
+
maxScaleRate = absScaleRate;
|
|
817
|
+
scale = scaleRate;
|
|
818
|
+
centerX = (pointer.startX + pointer2.startX) / 2;
|
|
819
|
+
centerY = (pointer.startY + pointer2.startY) / 2;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
const rotatable = maxRotateRate > 0;
|
|
825
|
+
const scalable = maxScaleRate > 0;
|
|
826
|
+
if (rotatable && scalable) {
|
|
827
|
+
detail.rotate = rotate;
|
|
828
|
+
detail.scale = scale;
|
|
829
|
+
detail.centerX = centerX;
|
|
830
|
+
detail.centerY = centerY;
|
|
831
|
+
}
|
|
832
|
+
else if (rotatable) {
|
|
833
|
+
detail.action = ACTION_ROTATE;
|
|
834
|
+
detail.rotate = rotate;
|
|
835
|
+
detail.centerX = centerX;
|
|
836
|
+
detail.centerY = centerY;
|
|
837
|
+
}
|
|
838
|
+
else if (scalable) {
|
|
839
|
+
detail.action = ACTION_SCALE;
|
|
840
|
+
detail.scale = scale;
|
|
841
|
+
detail.centerX = centerX;
|
|
842
|
+
detail.centerY = centerY;
|
|
843
|
+
}
|
|
844
|
+
else {
|
|
845
|
+
detail.action = ACTION_NONE;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
else {
|
|
849
|
+
const [pointer] = Array.from($pointers.values());
|
|
850
|
+
Object.assign(detail, pointer);
|
|
851
|
+
}
|
|
852
|
+
// Override the starting coordinate
|
|
853
|
+
$pointers.forEach((pointer) => {
|
|
854
|
+
pointer.startX = pointer.endX;
|
|
855
|
+
pointer.startY = pointer.endY;
|
|
856
|
+
});
|
|
857
|
+
if (detail.action !== ACTION_NONE) {
|
|
858
|
+
this.$emit(EVENT_ACTION, detail, {
|
|
859
|
+
cancelable: false,
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
$handlePointerUp(event) {
|
|
864
|
+
const { $action, $pointers } = this;
|
|
865
|
+
if (this.disabled || $action === ACTION_NONE) {
|
|
866
|
+
return;
|
|
867
|
+
}
|
|
868
|
+
if (this.$emit(EVENT_ACTION_END, {
|
|
869
|
+
action: $action,
|
|
870
|
+
relatedEvent: event,
|
|
871
|
+
}) === false) {
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
event.preventDefault();
|
|
875
|
+
if (event.changedTouches) {
|
|
876
|
+
Array.from(event.changedTouches).forEach(({ identifier, }) => {
|
|
877
|
+
$pointers.delete(identifier);
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
else {
|
|
881
|
+
const { pointerId = 0 } = event;
|
|
882
|
+
$pointers.delete(pointerId);
|
|
883
|
+
}
|
|
884
|
+
if ($pointers.size === 0) {
|
|
885
|
+
this.style.willChange = '';
|
|
886
|
+
this.$action = ACTION_NONE;
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
$handleWheel(event) {
|
|
890
|
+
if (this.disabled) {
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
event.preventDefault();
|
|
894
|
+
// Limit wheel speed to prevent zoom too fast (#21)
|
|
895
|
+
if (this.$wheeling) {
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
this.$wheeling = true;
|
|
899
|
+
// Debounce by 50ms
|
|
900
|
+
setTimeout(() => {
|
|
901
|
+
this.$wheeling = false;
|
|
902
|
+
}, 50);
|
|
903
|
+
const delta = event.deltaY > 0 ? -1 : 1;
|
|
904
|
+
const scale = delta * this.scaleStep;
|
|
905
|
+
this.$emit(EVENT_ACTION, {
|
|
906
|
+
action: ACTION_SCALE,
|
|
907
|
+
scale,
|
|
908
|
+
relatedEvent: event,
|
|
909
|
+
}, {
|
|
910
|
+
cancelable: false,
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Changes the current action to a new one.
|
|
915
|
+
* @param {string} action The new action.
|
|
916
|
+
* @returns {CropperCanvas} Returns `this` for chaining.
|
|
917
|
+
*/
|
|
918
|
+
$setAction(action) {
|
|
919
|
+
if (isString(action)) {
|
|
920
|
+
this.$action = action;
|
|
921
|
+
}
|
|
922
|
+
return this;
|
|
923
|
+
}
|
|
924
|
+
/**
|
|
925
|
+
* Generates a real canvas element, with the image draw into if there is one.
|
|
926
|
+
* @param {object} [options] The available options.
|
|
927
|
+
* @param {number} [options.width] The width of the canvas.
|
|
928
|
+
* @param {number} [options.height] The height of the canvas.
|
|
929
|
+
* @param {Function} [options.beforeDraw] The function called before drawing the image onto the canvas.
|
|
930
|
+
* @returns {Promise} Returns a promise that resolves to the generated canvas element.
|
|
931
|
+
*/
|
|
932
|
+
$toCanvas(options) {
|
|
933
|
+
return new Promise((resolve, reject) => {
|
|
934
|
+
if (!this.isConnected) {
|
|
935
|
+
reject(new Error('The current element is not connected to the DOM.'));
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
const canvas = document.createElement('canvas');
|
|
939
|
+
let width = this.offsetWidth;
|
|
940
|
+
let height = this.offsetHeight;
|
|
941
|
+
let scale = 1;
|
|
942
|
+
if (isPlainObject(options)
|
|
943
|
+
&& (isPositiveNumber(options.width) || isPositiveNumber(options.height))) {
|
|
944
|
+
({ width, height } = getAdjustedSizes({
|
|
945
|
+
aspectRatio: width / height,
|
|
946
|
+
width: options.width,
|
|
947
|
+
height: options.height,
|
|
948
|
+
}));
|
|
949
|
+
scale = width / this.offsetWidth;
|
|
950
|
+
}
|
|
951
|
+
canvas.width = width;
|
|
952
|
+
canvas.height = height;
|
|
953
|
+
const cropperImage = this.querySelector(this.$getTagNameOf(CROPPER_IMAGE));
|
|
954
|
+
if (!cropperImage) {
|
|
955
|
+
resolve(canvas);
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
cropperImage.$ready().then((image) => {
|
|
959
|
+
const context = canvas.getContext('2d');
|
|
960
|
+
if (context) {
|
|
961
|
+
const [a, b, c, d, e, f] = cropperImage.$getTransform();
|
|
962
|
+
let newE = e;
|
|
963
|
+
let newF = f;
|
|
964
|
+
let destWidth = image.naturalWidth;
|
|
965
|
+
let destHeight = image.naturalHeight;
|
|
966
|
+
if (scale !== 1) {
|
|
967
|
+
newE *= scale;
|
|
968
|
+
newF *= scale;
|
|
969
|
+
destWidth *= scale;
|
|
970
|
+
destHeight *= scale;
|
|
971
|
+
}
|
|
972
|
+
const centerX = destWidth / 2;
|
|
973
|
+
const centerY = destHeight / 2;
|
|
974
|
+
context.fillStyle = 'transparent';
|
|
975
|
+
context.fillRect(0, 0, width, height);
|
|
976
|
+
if (isPlainObject(options) && isFunction(options.beforeDraw)) {
|
|
977
|
+
options.beforeDraw.call(this, context, canvas);
|
|
978
|
+
}
|
|
979
|
+
context.save();
|
|
980
|
+
// Move the transform origin to the center of the image.
|
|
981
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin
|
|
982
|
+
context.translate(centerX, centerY);
|
|
983
|
+
context.transform(a, b, c, d, newE, newF);
|
|
984
|
+
// Reset the transform origin to the top-left of the image.
|
|
985
|
+
context.translate(-centerX, -centerY);
|
|
986
|
+
context.drawImage(image, 0, 0, destWidth, destHeight);
|
|
987
|
+
context.restore();
|
|
988
|
+
}
|
|
989
|
+
resolve(canvas);
|
|
990
|
+
}).catch(reject);
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
CropperCanvas.$name = CROPPER_CANVAS;
|
|
995
|
+
CropperCanvas.$version = '2.0.0-beta.4';
|
|
996
|
+
|
|
997
|
+
var style$6 = `:host{display:inline-block}img{display:block;height:100%;max-height:none!important;max-width:none!important;min-height:0!important;min-width:0!important;width:100%}`;
|
|
998
|
+
|
|
999
|
+
const canvasCache$2 = new WeakMap();
|
|
1000
|
+
const NATIVE_ATTRIBUTES = [
|
|
1001
|
+
'alt',
|
|
1002
|
+
'crossorigin',
|
|
1003
|
+
'decoding',
|
|
1004
|
+
'importance',
|
|
1005
|
+
'loading',
|
|
1006
|
+
'referrerpolicy',
|
|
1007
|
+
'sizes',
|
|
1008
|
+
'src',
|
|
1009
|
+
'srcset',
|
|
1010
|
+
];
|
|
1011
|
+
class CropperImage extends CropperElement {
|
|
1012
|
+
constructor() {
|
|
1013
|
+
super(...arguments);
|
|
1014
|
+
this.$matrix = [1, 0, 0, 1, 0, 0];
|
|
1015
|
+
this.$onLoad = null;
|
|
1016
|
+
this.$onCanvasAction = null;
|
|
1017
|
+
this.$onCanvasActionEnd = null;
|
|
1018
|
+
this.$onCanvasActionStart = null;
|
|
1019
|
+
this.$actionStartTarget = null;
|
|
1020
|
+
this.$style = style$6;
|
|
1021
|
+
this.$image = new Image();
|
|
1022
|
+
this.rotatable = true;
|
|
1023
|
+
this.scalable = true;
|
|
1024
|
+
this.skewable = true;
|
|
1025
|
+
this.slottable = false;
|
|
1026
|
+
this.translatable = true;
|
|
1027
|
+
}
|
|
1028
|
+
set $canvas(element) {
|
|
1029
|
+
canvasCache$2.set(this, element);
|
|
1030
|
+
}
|
|
1031
|
+
get $canvas() {
|
|
1032
|
+
return canvasCache$2.get(this);
|
|
1033
|
+
}
|
|
1034
|
+
static get observedAttributes() {
|
|
1035
|
+
return super.observedAttributes.concat(NATIVE_ATTRIBUTES, [
|
|
1036
|
+
'rotatable',
|
|
1037
|
+
'scalable',
|
|
1038
|
+
'skewable',
|
|
1039
|
+
'translatable',
|
|
1040
|
+
]);
|
|
1041
|
+
}
|
|
1042
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
1043
|
+
if (Object.is(newValue, oldValue)) {
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
super.attributeChangedCallback(name, oldValue, newValue);
|
|
1047
|
+
// Inherits the native attributes
|
|
1048
|
+
if (NATIVE_ATTRIBUTES.includes(name)) {
|
|
1049
|
+
this.$image.setAttribute(name, newValue);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
connectedCallback() {
|
|
1053
|
+
super.connectedCallback();
|
|
1054
|
+
const { $image } = this;
|
|
1055
|
+
const $canvas = this.closest(this.$getTagNameOf(CROPPER_CANVAS));
|
|
1056
|
+
if ($canvas) {
|
|
1057
|
+
this.$canvas = $canvas;
|
|
1058
|
+
this.$setStyles({
|
|
1059
|
+
// Make it a block element to avoid side effects (#1074).
|
|
1060
|
+
display: 'block',
|
|
1061
|
+
position: 'absolute',
|
|
1062
|
+
});
|
|
1063
|
+
this.$onCanvasActionStart = (event) => {
|
|
1064
|
+
var _a, _b;
|
|
1065
|
+
this.$actionStartTarget = (_b = (_a = event.detail) === null || _a === void 0 ? void 0 : _a.relatedEvent) === null || _b === void 0 ? void 0 : _b.target;
|
|
1066
|
+
};
|
|
1067
|
+
this.$onCanvasActionEnd = () => {
|
|
1068
|
+
this.$actionStartTarget = null;
|
|
1069
|
+
};
|
|
1070
|
+
this.$onCanvasAction = this.$handleAction.bind(this);
|
|
1071
|
+
on($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
|
|
1072
|
+
on($canvas, EVENT_ACTION_END, this.$onCanvasActionEnd);
|
|
1073
|
+
on($canvas, EVENT_ACTION, this.$onCanvasAction);
|
|
1074
|
+
}
|
|
1075
|
+
this.$onLoad = this.$handleLoad.bind(this);
|
|
1076
|
+
on($image, EVENT_LOAD, this.$onLoad);
|
|
1077
|
+
this.$getShadowRoot().appendChild($image);
|
|
1078
|
+
}
|
|
1079
|
+
disconnectedCallback() {
|
|
1080
|
+
const { $image, $canvas } = this;
|
|
1081
|
+
if ($canvas) {
|
|
1082
|
+
if (this.$onCanvasActionStart) {
|
|
1083
|
+
off($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
|
|
1084
|
+
this.$onCanvasActionStart = null;
|
|
1085
|
+
}
|
|
1086
|
+
if (this.$onCanvasActionEnd) {
|
|
1087
|
+
off($canvas, EVENT_ACTION_END, this.$onCanvasActionEnd);
|
|
1088
|
+
this.$onCanvasActionEnd = null;
|
|
1089
|
+
}
|
|
1090
|
+
if (this.$onCanvasAction) {
|
|
1091
|
+
off($canvas, EVENT_ACTION, this.$onCanvasAction);
|
|
1092
|
+
this.$onCanvasAction = null;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
if ($image && this.$onLoad) {
|
|
1096
|
+
off($image, EVENT_LOAD, this.$onLoad);
|
|
1097
|
+
this.$onLoad = null;
|
|
1098
|
+
}
|
|
1099
|
+
this.$getShadowRoot().removeChild($image);
|
|
1100
|
+
super.disconnectedCallback();
|
|
1101
|
+
}
|
|
1102
|
+
$handleLoad() {
|
|
1103
|
+
const { $image } = this;
|
|
1104
|
+
this.$setStyles({
|
|
1105
|
+
width: $image.naturalWidth,
|
|
1106
|
+
height: $image.naturalHeight,
|
|
1107
|
+
});
|
|
1108
|
+
if (this.$canvas) {
|
|
1109
|
+
this.$center('cover');
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
$handleAction(event) {
|
|
1113
|
+
if (this.hidden || !(this.rotatable || this.scalable || this.translatable)) {
|
|
1114
|
+
return;
|
|
1115
|
+
}
|
|
1116
|
+
const { $canvas } = this;
|
|
1117
|
+
const { detail } = event;
|
|
1118
|
+
if (detail) {
|
|
1119
|
+
const { relatedEvent } = detail;
|
|
1120
|
+
let { action } = detail;
|
|
1121
|
+
if (action === ACTION_TRANSFORM && (!this.rotatable || !this.scalable)) {
|
|
1122
|
+
if (this.rotatable) {
|
|
1123
|
+
action = ACTION_ROTATE;
|
|
1124
|
+
}
|
|
1125
|
+
else if (this.scalable) {
|
|
1126
|
+
action = ACTION_SCALE;
|
|
1127
|
+
}
|
|
1128
|
+
else {
|
|
1129
|
+
action = ACTION_NONE;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
switch (action) {
|
|
1133
|
+
case ACTION_MOVE:
|
|
1134
|
+
if (this.translatable) {
|
|
1135
|
+
const cropperSelectionTagName = this.$getTagNameOf(CROPPER_SELECTION);
|
|
1136
|
+
let selection = $canvas.querySelector(cropperSelectionTagName);
|
|
1137
|
+
if (selection && selection.multiple && !selection.active) {
|
|
1138
|
+
selection = $canvas.querySelector(`${cropperSelectionTagName}[active]`);
|
|
1139
|
+
}
|
|
1140
|
+
if (!selection || selection.hidden || !selection.movable
|
|
1141
|
+
|| !(this.$actionStartTarget && selection.contains(this.$actionStartTarget))) {
|
|
1142
|
+
this.$move(detail.endX - detail.startX, detail.endY - detail.startY);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
break;
|
|
1146
|
+
case ACTION_ROTATE:
|
|
1147
|
+
if (this.rotatable) {
|
|
1148
|
+
if (relatedEvent) {
|
|
1149
|
+
const { x, y } = this.getBoundingClientRect();
|
|
1150
|
+
this.$rotate(detail.rotate, relatedEvent.clientX - x, relatedEvent.clientY - y);
|
|
1151
|
+
}
|
|
1152
|
+
else {
|
|
1153
|
+
this.$rotate(detail.rotate);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
break;
|
|
1157
|
+
case ACTION_SCALE:
|
|
1158
|
+
if (this.scalable) {
|
|
1159
|
+
if (relatedEvent) {
|
|
1160
|
+
const { x, y } = this.getBoundingClientRect();
|
|
1161
|
+
this.$zoom(detail.scale, relatedEvent.clientX - x, relatedEvent.clientY - y);
|
|
1162
|
+
}
|
|
1163
|
+
else {
|
|
1164
|
+
this.$zoom(detail.scale);
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
break;
|
|
1168
|
+
case ACTION_TRANSFORM:
|
|
1169
|
+
if (this.rotatable && this.scalable) {
|
|
1170
|
+
const { rotate } = detail;
|
|
1171
|
+
let { scale } = detail;
|
|
1172
|
+
if (scale < 0) {
|
|
1173
|
+
scale = 1 / (1 - scale);
|
|
1174
|
+
}
|
|
1175
|
+
else {
|
|
1176
|
+
scale += 1;
|
|
1177
|
+
}
|
|
1178
|
+
const cos = Math.cos(rotate);
|
|
1179
|
+
const sin = Math.sin(rotate);
|
|
1180
|
+
const [scaleX, skewY, skewX, scaleY] = [
|
|
1181
|
+
cos * scale,
|
|
1182
|
+
sin * scale,
|
|
1183
|
+
-sin * scale,
|
|
1184
|
+
cos * scale,
|
|
1185
|
+
];
|
|
1186
|
+
if (relatedEvent) {
|
|
1187
|
+
const clientRect = this.getBoundingClientRect();
|
|
1188
|
+
const x = relatedEvent.clientX - clientRect.x;
|
|
1189
|
+
const y = relatedEvent.clientY - clientRect.y;
|
|
1190
|
+
const [a, b, c, d] = this.$matrix;
|
|
1191
|
+
const originX = clientRect.width / 2;
|
|
1192
|
+
const originY = clientRect.height / 2;
|
|
1193
|
+
const moveX = x - originX;
|
|
1194
|
+
const moveY = y - originY;
|
|
1195
|
+
const translateX = ((moveX * d) - (c * moveY)) / ((a * d) - (c * b));
|
|
1196
|
+
const translateY = (moveY - (b * translateX)) / d;
|
|
1197
|
+
/**
|
|
1198
|
+
* Equals to
|
|
1199
|
+
* this.$rotate(rotate, x, y);
|
|
1200
|
+
* this.$scale(scale, x, y);
|
|
1201
|
+
*/
|
|
1202
|
+
this.$transform(scaleX, skewY, skewX, scaleY, translateX * (1 - scaleX) + translateY * skewX, translateY * (1 - scaleY) + translateX * skewY);
|
|
1203
|
+
}
|
|
1204
|
+
else {
|
|
1205
|
+
/**
|
|
1206
|
+
* Equals to
|
|
1207
|
+
* this.$rotate(rotate);
|
|
1208
|
+
* this.$scale(scale);
|
|
1209
|
+
*/
|
|
1210
|
+
this.$transform(scaleX, skewY, skewX, scaleY, 0, 0);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
break;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Defers the callback to execute after successfully loading the image.
|
|
1219
|
+
* @param {Function} [callback] The callback to execute after successfully loading the image.
|
|
1220
|
+
* @returns {Promise} Returns a promise that resolves to the image element.
|
|
1221
|
+
*/
|
|
1222
|
+
$ready(callback) {
|
|
1223
|
+
const { $image } = this;
|
|
1224
|
+
const promise = new Promise((resolve, reject) => {
|
|
1225
|
+
const error = new Error('Failed to load the image source');
|
|
1226
|
+
if ($image.complete) {
|
|
1227
|
+
if ($image.naturalWidth > 0 && $image.naturalHeight > 0) {
|
|
1228
|
+
resolve($image);
|
|
1229
|
+
}
|
|
1230
|
+
else {
|
|
1231
|
+
reject(error);
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
else {
|
|
1235
|
+
const onLoad = () => {
|
|
1236
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
1237
|
+
off($image, EVENT_ERROR, onError);
|
|
1238
|
+
resolve($image);
|
|
1239
|
+
};
|
|
1240
|
+
const onError = () => {
|
|
1241
|
+
off($image, EVENT_LOAD, onLoad);
|
|
1242
|
+
reject(error);
|
|
1243
|
+
};
|
|
1244
|
+
once($image, EVENT_LOAD, onLoad);
|
|
1245
|
+
once($image, EVENT_ERROR, onError);
|
|
1246
|
+
}
|
|
1247
|
+
});
|
|
1248
|
+
if (isFunction(callback)) {
|
|
1249
|
+
promise.then((image) => {
|
|
1250
|
+
callback(image);
|
|
1251
|
+
return image;
|
|
1252
|
+
});
|
|
1253
|
+
}
|
|
1254
|
+
return promise;
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Aligns the image to the center of its parent element.
|
|
1258
|
+
* @param {string} [size] The size of the image.
|
|
1259
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1260
|
+
*/
|
|
1261
|
+
$center(size) {
|
|
1262
|
+
const { parentElement } = this;
|
|
1263
|
+
if (!parentElement) {
|
|
1264
|
+
return this;
|
|
1265
|
+
}
|
|
1266
|
+
const container = parentElement.getBoundingClientRect();
|
|
1267
|
+
const containerWidth = container.width;
|
|
1268
|
+
const containerHeight = container.height;
|
|
1269
|
+
const { x, y, width, height, } = this.getBoundingClientRect();
|
|
1270
|
+
const startX = x + (width / 2);
|
|
1271
|
+
const startY = y + (height / 2);
|
|
1272
|
+
const endX = container.x + (containerWidth / 2);
|
|
1273
|
+
const endY = container.y + (containerHeight / 2);
|
|
1274
|
+
this.$move(endX - startX, endY - startY);
|
|
1275
|
+
if (size && (width !== containerWidth || height !== containerHeight)) {
|
|
1276
|
+
const scaleX = containerWidth / width;
|
|
1277
|
+
const scaleY = containerHeight / height;
|
|
1278
|
+
switch (size) {
|
|
1279
|
+
case 'cover':
|
|
1280
|
+
this.$scale(Math.max(scaleX, scaleY));
|
|
1281
|
+
break;
|
|
1282
|
+
case 'contain':
|
|
1283
|
+
this.$scale(Math.min(scaleX, scaleY));
|
|
1284
|
+
break;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
return this;
|
|
1288
|
+
}
|
|
1289
|
+
/**
|
|
1290
|
+
* Moves the image.
|
|
1291
|
+
* @param {number} x The moving distance in the horizontal direction.
|
|
1292
|
+
* @param {number} [y] The moving distance in the vertical direction.
|
|
1293
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1294
|
+
*/
|
|
1295
|
+
$move(x, y = x) {
|
|
1296
|
+
if (this.translatable && isNumber(x) && isNumber(y)) {
|
|
1297
|
+
const [a, b, c, d] = this.$matrix;
|
|
1298
|
+
const e = ((x * d) - (c * y)) / ((a * d) - (c * b));
|
|
1299
|
+
const f = (y - (b * e)) / d;
|
|
1300
|
+
this.$translate(e, f);
|
|
1301
|
+
}
|
|
1302
|
+
return this;
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Moves the image to a specific position.
|
|
1306
|
+
* @param {number} x The new position in the horizontal direction.
|
|
1307
|
+
* @param {number} [y] The new position in the vertical direction.
|
|
1308
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1309
|
+
*/
|
|
1310
|
+
$moveTo(x, y = x) {
|
|
1311
|
+
if (this.translatable && isNumber(x) && isNumber(y)) {
|
|
1312
|
+
const [a, b, c, d] = this.$matrix;
|
|
1313
|
+
const e = ((x * d) - (c * y)) / ((a * d) - (c * b));
|
|
1314
|
+
const f = (y - (b * e)) / d;
|
|
1315
|
+
this.$setTransform(a, b, c, d, e, f);
|
|
1316
|
+
}
|
|
1317
|
+
return this;
|
|
1318
|
+
}
|
|
1319
|
+
/**
|
|
1320
|
+
* Rotates the image.
|
|
1321
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/rotate}
|
|
1322
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rotate}
|
|
1323
|
+
* @param {number|string} angle The rotation angle (in radians).
|
|
1324
|
+
* @param {number} [x] The rotation origin in the horizontal, defaults to the center of the image.
|
|
1325
|
+
* @param {number} [y] The rotation origin in the vertical, defaults to the center of the image.
|
|
1326
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1327
|
+
*/
|
|
1328
|
+
$rotate(angle, x, y) {
|
|
1329
|
+
if (this.rotatable) {
|
|
1330
|
+
const radian = toAngleInRadian(angle);
|
|
1331
|
+
const cos = Math.cos(radian);
|
|
1332
|
+
const sin = Math.sin(radian);
|
|
1333
|
+
const [scaleX, skewY, skewX, scaleY] = [cos, sin, -sin, cos];
|
|
1334
|
+
if (isNumber(x) && isNumber(y)) {
|
|
1335
|
+
const [a, b, c, d] = this.$matrix;
|
|
1336
|
+
const { width, height } = this.getBoundingClientRect();
|
|
1337
|
+
const originX = width / 2;
|
|
1338
|
+
const originY = height / 2;
|
|
1339
|
+
const moveX = x - originX;
|
|
1340
|
+
const moveY = y - originY;
|
|
1341
|
+
const translateX = ((moveX * d) - (c * moveY)) / ((a * d) - (c * b));
|
|
1342
|
+
const translateY = (moveY - (b * translateX)) / d;
|
|
1343
|
+
/**
|
|
1344
|
+
* Equals to
|
|
1345
|
+
* this.$translate(translateX, translateX);
|
|
1346
|
+
* this.$rotate(angle);
|
|
1347
|
+
* this.$translate(-translateX, -translateX);
|
|
1348
|
+
*/
|
|
1349
|
+
this.$transform(scaleX, skewY, skewX, scaleY, translateX * (1 - scaleX) - translateY * skewX, translateY * (1 - scaleY) - translateX * skewY);
|
|
1350
|
+
}
|
|
1351
|
+
else {
|
|
1352
|
+
this.$transform(scaleX, skewY, skewX, scaleY, 0, 0);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
return this;
|
|
1356
|
+
}
|
|
1357
|
+
/**
|
|
1358
|
+
* Zooms the image.
|
|
1359
|
+
* @param {number} scale The zoom factor. Positive numbers for zooming in, and negative numbers for zooming out.
|
|
1360
|
+
* @param {number} [x] The zoom origin in the horizontal, defaults to the center of the image.
|
|
1361
|
+
* @param {number} [y] The zoom origin in the vertical, defaults to the center of the image.
|
|
1362
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1363
|
+
*/
|
|
1364
|
+
$zoom(scale, x, y) {
|
|
1365
|
+
if (!this.scalable || scale === 0) {
|
|
1366
|
+
return this;
|
|
1367
|
+
}
|
|
1368
|
+
if (scale < 0) {
|
|
1369
|
+
scale = 1 / (1 - scale);
|
|
1370
|
+
}
|
|
1371
|
+
else {
|
|
1372
|
+
scale += 1;
|
|
1373
|
+
}
|
|
1374
|
+
if (isNumber(x) && isNumber(y)) {
|
|
1375
|
+
const [a, b, c, d] = this.$matrix;
|
|
1376
|
+
const { width, height } = this.getBoundingClientRect();
|
|
1377
|
+
const originX = width / 2;
|
|
1378
|
+
const originY = height / 2;
|
|
1379
|
+
const moveX = x - originX;
|
|
1380
|
+
const moveY = y - originY;
|
|
1381
|
+
const translateX = ((moveX * d) - (c * moveY)) / ((a * d) - (c * b));
|
|
1382
|
+
const translateY = (moveY - (b * translateX)) / d;
|
|
1383
|
+
/**
|
|
1384
|
+
* Equals to
|
|
1385
|
+
* this.$translate(translateX, translateX);
|
|
1386
|
+
* this.$scale(scale);
|
|
1387
|
+
* this.$translate(-translateX, -translateX);
|
|
1388
|
+
*/
|
|
1389
|
+
this.$transform(scale, 0, 0, scale, translateX * (1 - scale), translateY * (1 - scale));
|
|
1390
|
+
}
|
|
1391
|
+
else {
|
|
1392
|
+
this.$scale(scale);
|
|
1393
|
+
}
|
|
1394
|
+
return this;
|
|
1395
|
+
}
|
|
1396
|
+
/**
|
|
1397
|
+
* Scales the image.
|
|
1398
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/scale}
|
|
1399
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/scale}
|
|
1400
|
+
* @param {number} x The scaling factor in the horizontal direction.
|
|
1401
|
+
* @param {number} [y] The scaling factor in the vertical direction.
|
|
1402
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1403
|
+
*/
|
|
1404
|
+
$scale(x, y = x) {
|
|
1405
|
+
if (this.scalable) {
|
|
1406
|
+
this.$transform(x, 0, 0, y, 0, 0);
|
|
1407
|
+
}
|
|
1408
|
+
return this;
|
|
1409
|
+
}
|
|
1410
|
+
/**
|
|
1411
|
+
* Skews the image.
|
|
1412
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/skew}
|
|
1413
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform}
|
|
1414
|
+
* @param {number|string} x The skewing angle in the horizontal direction.
|
|
1415
|
+
* @param {number|string} [y] The skewing angle in the vertical direction.
|
|
1416
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1417
|
+
*/
|
|
1418
|
+
$skew(x, y = 0) {
|
|
1419
|
+
if (this.skewable) {
|
|
1420
|
+
const radianX = toAngleInRadian(x);
|
|
1421
|
+
const radianY = toAngleInRadian(y);
|
|
1422
|
+
this.$transform(1, Math.tan(radianY), Math.tan(radianX), 1, 0, 0);
|
|
1423
|
+
}
|
|
1424
|
+
return this;
|
|
1425
|
+
}
|
|
1426
|
+
/**
|
|
1427
|
+
* Translates the image.
|
|
1428
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate}
|
|
1429
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/translate}
|
|
1430
|
+
* @param {number} x The translating distance in the horizontal direction.
|
|
1431
|
+
* @param {number} [y] The translating distance in the vertical direction.
|
|
1432
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1433
|
+
*/
|
|
1434
|
+
$translate(x, y = x) {
|
|
1435
|
+
if (this.translatable && isNumber(x) && isNumber(y)) {
|
|
1436
|
+
this.$transform(1, 0, 0, 1, x, y);
|
|
1437
|
+
}
|
|
1438
|
+
return this;
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Transforms the image.
|
|
1442
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/matrix}
|
|
1443
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/transform}
|
|
1444
|
+
* @param {number} a The scaling factor in the horizontal direction.
|
|
1445
|
+
* @param {number} b The skewing angle in the vertical direction.
|
|
1446
|
+
* @param {number} c The skewing angle in the horizontal direction.
|
|
1447
|
+
* @param {number} d The scaling factor in the vertical direction.
|
|
1448
|
+
* @param {number} e The translating distance in the horizontal direction.
|
|
1449
|
+
* @param {number} f The translating distance in the vertical direction.
|
|
1450
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1451
|
+
*/
|
|
1452
|
+
$transform(a, b, c, d, e, f) {
|
|
1453
|
+
if (isNumber(a)
|
|
1454
|
+
&& isNumber(b)
|
|
1455
|
+
&& isNumber(c)
|
|
1456
|
+
&& isNumber(d)
|
|
1457
|
+
&& isNumber(e)
|
|
1458
|
+
&& isNumber(f)) {
|
|
1459
|
+
return this.$setTransform(multiplyMatrices(this.$matrix, [a, b, c, d, e, f]));
|
|
1460
|
+
}
|
|
1461
|
+
return this;
|
|
1462
|
+
}
|
|
1463
|
+
/**
|
|
1464
|
+
* Resets (overrides) the current transform to the specific identity matrix.
|
|
1465
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/setTransform}
|
|
1466
|
+
* @param {number|Array} a The scaling factor in the horizontal direction.
|
|
1467
|
+
* @param {number} b The skewing angle in the vertical direction.
|
|
1468
|
+
* @param {number} c The skewing angle in the horizontal direction.
|
|
1469
|
+
* @param {number} d The scaling factor in the vertical direction.
|
|
1470
|
+
* @param {number} e The translating distance in the horizontal direction.
|
|
1471
|
+
* @param {number} f The translating distance in the vertical direction.
|
|
1472
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1473
|
+
*/
|
|
1474
|
+
$setTransform(a, b, c, d, e, f) {
|
|
1475
|
+
if (this.rotatable || this.scalable || this.skewable || this.translatable) {
|
|
1476
|
+
if (Array.isArray(a)) {
|
|
1477
|
+
[a, b, c, d, e, f] = a;
|
|
1478
|
+
}
|
|
1479
|
+
if (isNumber(a)
|
|
1480
|
+
&& isNumber(b)
|
|
1481
|
+
&& isNumber(c)
|
|
1482
|
+
&& isNumber(d)
|
|
1483
|
+
&& isNumber(e)
|
|
1484
|
+
&& isNumber(f)) {
|
|
1485
|
+
const newMatrix = [a, b, c, d, e, f];
|
|
1486
|
+
if (this.$emit(EVENT_TRANSFORM, {
|
|
1487
|
+
matrix: newMatrix,
|
|
1488
|
+
oldMatrix: this.$matrix,
|
|
1489
|
+
}) === false) {
|
|
1490
|
+
return this;
|
|
1491
|
+
}
|
|
1492
|
+
this.$matrix = newMatrix;
|
|
1493
|
+
this.style.transform = `matrix(${newMatrix.join(', ')})`;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
return this;
|
|
1497
|
+
}
|
|
1498
|
+
/**
|
|
1499
|
+
* Retrieves the current transformation matrix being applied to the element.
|
|
1500
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/getTransform}
|
|
1501
|
+
* @returns {Array} Returns the readonly transformation matrix.
|
|
1502
|
+
*/
|
|
1503
|
+
$getTransform() {
|
|
1504
|
+
return this.$matrix.slice();
|
|
1505
|
+
}
|
|
1506
|
+
/**
|
|
1507
|
+
* Resets the current transform to the initial identity matrix.
|
|
1508
|
+
* {@link https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/resetTransform}
|
|
1509
|
+
* @returns {CropperImage} Returns `this` for chaining.
|
|
1510
|
+
*/
|
|
1511
|
+
$resetTransform() {
|
|
1512
|
+
return this.$setTransform([1, 0, 0, 1, 0, 0]);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
CropperImage.$name = CROPPER_IMAGE;
|
|
1516
|
+
CropperImage.$version = '2.0.0-beta.4';
|
|
1517
|
+
|
|
1518
|
+
var style$5 = `:host{display:block;height:0;left:0;outline:var(--theme-color) solid 1px;position:relative;top:0;width:0}:host([transparent]){outline-color:transparent}`;
|
|
1519
|
+
|
|
1520
|
+
const canvasCache$1 = new WeakMap();
|
|
1521
|
+
class CropperShade extends CropperElement {
|
|
1522
|
+
constructor() {
|
|
1523
|
+
super(...arguments);
|
|
1524
|
+
this.$onCanvasChange = null;
|
|
1525
|
+
this.$onCanvasActionEnd = null;
|
|
1526
|
+
this.$onCanvasActionStart = null;
|
|
1527
|
+
this.$style = style$5;
|
|
1528
|
+
this.x = 0;
|
|
1529
|
+
this.y = 0;
|
|
1530
|
+
this.width = 0;
|
|
1531
|
+
this.height = 0;
|
|
1532
|
+
this.slottable = false;
|
|
1533
|
+
this.themeColor = 'rgba(0, 0, 0, 0.65)';
|
|
1534
|
+
}
|
|
1535
|
+
set $canvas(element) {
|
|
1536
|
+
canvasCache$1.set(this, element);
|
|
1537
|
+
}
|
|
1538
|
+
get $canvas() {
|
|
1539
|
+
return canvasCache$1.get(this);
|
|
1540
|
+
}
|
|
1541
|
+
static get observedAttributes() {
|
|
1542
|
+
return super.observedAttributes.concat([
|
|
1543
|
+
'height',
|
|
1544
|
+
'width',
|
|
1545
|
+
'x',
|
|
1546
|
+
'y',
|
|
1547
|
+
]);
|
|
1548
|
+
}
|
|
1549
|
+
connectedCallback() {
|
|
1550
|
+
super.connectedCallback();
|
|
1551
|
+
const $canvas = this.closest(this.$getTagNameOf(CROPPER_CANVAS));
|
|
1552
|
+
if ($canvas) {
|
|
1553
|
+
this.$canvas = $canvas;
|
|
1554
|
+
this.style.position = 'absolute';
|
|
1555
|
+
const $selection = $canvas.querySelector(this.$getTagNameOf(CROPPER_SELECTION));
|
|
1556
|
+
if ($selection) {
|
|
1557
|
+
this.$onCanvasActionStart = (event) => {
|
|
1558
|
+
if ($selection.hidden && event.detail.action === ACTION_SELECT) {
|
|
1559
|
+
this.hidden = false;
|
|
1560
|
+
}
|
|
1561
|
+
};
|
|
1562
|
+
this.$onCanvasActionEnd = (event) => {
|
|
1563
|
+
if ($selection.hidden && event.detail.action === ACTION_SELECT) {
|
|
1564
|
+
this.hidden = true;
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
this.$onCanvasChange = (event) => {
|
|
1568
|
+
const { x, y, width, height, } = event.detail;
|
|
1569
|
+
this.$change(x, y, width, height);
|
|
1570
|
+
if ($selection.hidden || (x === 0 && y === 0 && width === 0 && height === 0)) {
|
|
1571
|
+
this.hidden = true;
|
|
1572
|
+
}
|
|
1573
|
+
};
|
|
1574
|
+
on($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
|
|
1575
|
+
on($canvas, EVENT_ACTION_END, this.$onCanvasActionEnd);
|
|
1576
|
+
on($canvas, EVENT_CHANGE, this.$onCanvasChange);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
this.$render();
|
|
1580
|
+
}
|
|
1581
|
+
disconnectedCallback() {
|
|
1582
|
+
const { $canvas } = this;
|
|
1583
|
+
if ($canvas) {
|
|
1584
|
+
if (this.$onCanvasActionStart) {
|
|
1585
|
+
off($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
|
|
1586
|
+
this.$onCanvasActionStart = null;
|
|
1587
|
+
}
|
|
1588
|
+
if (this.$onCanvasActionEnd) {
|
|
1589
|
+
off($canvas, EVENT_ACTION_END, this.$onCanvasActionEnd);
|
|
1590
|
+
this.$onCanvasActionEnd = null;
|
|
1591
|
+
}
|
|
1592
|
+
if (this.$onCanvasChange) {
|
|
1593
|
+
off($canvas, EVENT_CHANGE, this.$onCanvasChange);
|
|
1594
|
+
this.$onCanvasChange = null;
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
super.disconnectedCallback();
|
|
1598
|
+
}
|
|
1599
|
+
/**
|
|
1600
|
+
* Changes the position and/or size of the shade.
|
|
1601
|
+
* @param {number} x The new position in the horizontal direction.
|
|
1602
|
+
* @param {number} y The new position in the vertical direction.
|
|
1603
|
+
* @param {number} [width] The new width.
|
|
1604
|
+
* @param {number} [height] The new height.
|
|
1605
|
+
* @returns {CropperShade} Returns `this` for chaining.
|
|
1606
|
+
*/
|
|
1607
|
+
$change(x, y, width = this.width, height = this.height) {
|
|
1608
|
+
if (!isNumber(x)
|
|
1609
|
+
|| !isNumber(y)
|
|
1610
|
+
|| !isNumber(width)
|
|
1611
|
+
|| !isNumber(height)
|
|
1612
|
+
|| (x === this.x && y === this.y && width === this.width && height === this.height)) {
|
|
1613
|
+
return this;
|
|
1614
|
+
}
|
|
1615
|
+
if (this.hidden) {
|
|
1616
|
+
this.hidden = false;
|
|
1617
|
+
}
|
|
1618
|
+
this.x = x;
|
|
1619
|
+
this.y = y;
|
|
1620
|
+
this.width = width;
|
|
1621
|
+
this.height = height;
|
|
1622
|
+
return this.$render();
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Resets the shade to its initial position and size.
|
|
1626
|
+
* @returns {CropperShade} Returns `this` for chaining.
|
|
1627
|
+
*/
|
|
1628
|
+
$reset() {
|
|
1629
|
+
return this.$change(0, 0, 0, 0);
|
|
1630
|
+
}
|
|
1631
|
+
/**
|
|
1632
|
+
* Refreshes the position or size of the shade.
|
|
1633
|
+
* @returns {CropperShade} Returns `this` for chaining.
|
|
1634
|
+
*/
|
|
1635
|
+
$render() {
|
|
1636
|
+
return this.$setStyles({
|
|
1637
|
+
transform: `translate(${this.x}px, ${this.y}px)`,
|
|
1638
|
+
width: this.width,
|
|
1639
|
+
height: this.height,
|
|
1640
|
+
outlineWidth: WINDOW.innerWidth,
|
|
1641
|
+
});
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
CropperShade.$name = CROPPER_SHADE;
|
|
1645
|
+
CropperShade.$version = '2.0.0-beta.4';
|
|
1646
|
+
|
|
1647
|
+
var style$4 = `:host{background-color:var(--theme-color);display:block}:host([action=move]),:host([action=select]){height:100%;left:0;position:absolute;top:0;width:100%}:host([action=move]){cursor:move}:host([action=select]){cursor:crosshair}:host([action$=-resize]){background-color:transparent;height:15px;position:absolute;width:15px}:host([action$=-resize]):after{background-color:var(--theme-color);content:"";display:block;height:5px;left:50%;position:absolute;top:50%;transform:translate(-50%,-50%);width:5px}:host([action=n-resize]),:host([action=s-resize]){cursor:ns-resize;left:50%;transform:translateX(-50%);width:100%}:host([action=n-resize]){top:-8px}:host([action=s-resize]){bottom:-8px}:host([action=e-resize]),:host([action=w-resize]){cursor:ew-resize;height:100%;top:50%;transform:translateY(-50%)}:host([action=e-resize]){right:-8px}:host([action=w-resize]){left:-8px}:host([action=ne-resize]){cursor:nesw-resize;right:-8px;top:-8px}:host([action=nw-resize]){cursor:nwse-resize;left:-8px;top:-8px}:host([action=se-resize]){bottom:-8px;cursor:nwse-resize;right:-8px}:host([action=se-resize]):after{height:15px;width:15px}@media (pointer:coarse){:host([action=se-resize]):after{height:10px;width:10px}}@media (pointer:fine){:host([action=se-resize]):after{height:5px;width:5px}}:host([action=sw-resize]){bottom:-8px;cursor:nesw-resize;left:-8px}:host([plain]){background-color:transparent}`;
|
|
1648
|
+
|
|
1649
|
+
class CropperHandle extends CropperElement {
|
|
1650
|
+
constructor() {
|
|
1651
|
+
super(...arguments);
|
|
1652
|
+
this.$onCanvasCropEnd = null;
|
|
1653
|
+
this.$onCanvasCropStart = null;
|
|
1654
|
+
this.$style = style$4;
|
|
1655
|
+
this.action = ACTION_NONE;
|
|
1656
|
+
this.plain = false;
|
|
1657
|
+
this.slottable = false;
|
|
1658
|
+
this.themeColor = 'rgba(51, 153, 255, 0.5)';
|
|
1659
|
+
}
|
|
1660
|
+
static get observedAttributes() {
|
|
1661
|
+
return super.observedAttributes.concat([
|
|
1662
|
+
'action',
|
|
1663
|
+
'plain',
|
|
1664
|
+
]);
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
CropperHandle.$name = CROPPER_HANDLE;
|
|
1668
|
+
CropperHandle.$version = '2.0.0-beta.4';
|
|
1669
|
+
|
|
1670
|
+
var style$3 = `:host{display:block;left:0;position:relative;right:0}:host([outlined]){outline:1px solid var(--theme-color)}:host([multiple]){outline:1px dashed hsla(0,0%,100%,.5)}:host([multiple]):after{bottom:0;content:"";cursor:pointer;display:block;left:0;position:absolute;right:0;top:0}:host([multiple][active]){outline-color:var(--theme-color);z-index:1}:host([multiple])>*{visibility:hidden}:host([multiple][active])>*{visibility:visible}:host([multiple][active]):after{display:none}`;
|
|
1671
|
+
|
|
1672
|
+
const canvasCache = new WeakMap();
|
|
1673
|
+
class CropperSelection extends CropperElement {
|
|
1674
|
+
constructor() {
|
|
1675
|
+
super(...arguments);
|
|
1676
|
+
this.$onCanvasAction = null;
|
|
1677
|
+
this.$onCanvasActionStart = null;
|
|
1678
|
+
this.$onCanvasActionEnd = null;
|
|
1679
|
+
this.$onDocumentKeyDown = null;
|
|
1680
|
+
this.$action = '';
|
|
1681
|
+
this.$actionStartTarget = null;
|
|
1682
|
+
this.$style = style$3;
|
|
1683
|
+
this.x = 0;
|
|
1684
|
+
this.y = 0;
|
|
1685
|
+
this.width = 0;
|
|
1686
|
+
this.height = 0;
|
|
1687
|
+
this.aspectRatio = NaN;
|
|
1688
|
+
this.initialAspectRatio = NaN;
|
|
1689
|
+
this.initialCoverage = NaN;
|
|
1690
|
+
this.active = false;
|
|
1691
|
+
this.movable = false;
|
|
1692
|
+
this.resizable = false;
|
|
1693
|
+
this.zoomable = false;
|
|
1694
|
+
this.multiple = false;
|
|
1695
|
+
this.keyboard = false;
|
|
1696
|
+
this.outlined = false;
|
|
1697
|
+
this.precise = false;
|
|
1698
|
+
}
|
|
1699
|
+
set $canvas(element) {
|
|
1700
|
+
canvasCache.set(this, element);
|
|
1701
|
+
}
|
|
1702
|
+
get $canvas() {
|
|
1703
|
+
return canvasCache.get(this);
|
|
1704
|
+
}
|
|
1705
|
+
static get observedAttributes() {
|
|
1706
|
+
return super.observedAttributes.concat([
|
|
1707
|
+
'active',
|
|
1708
|
+
'aspect-ratio',
|
|
1709
|
+
'height',
|
|
1710
|
+
'initial-aspect-ratio',
|
|
1711
|
+
'initial-coverage',
|
|
1712
|
+
'keyboard',
|
|
1713
|
+
'movable',
|
|
1714
|
+
'multiple',
|
|
1715
|
+
'outlined',
|
|
1716
|
+
'precise',
|
|
1717
|
+
'resizable',
|
|
1718
|
+
'width',
|
|
1719
|
+
'x',
|
|
1720
|
+
'y',
|
|
1721
|
+
'zoomable',
|
|
1722
|
+
]);
|
|
1723
|
+
}
|
|
1724
|
+
$propertyChangedCallback(name, oldValue, newValue) {
|
|
1725
|
+
if (Object.is(newValue, oldValue)) {
|
|
1726
|
+
return;
|
|
1727
|
+
}
|
|
1728
|
+
super.$propertyChangedCallback(name, oldValue, newValue);
|
|
1729
|
+
switch (name) {
|
|
1730
|
+
case 'aspectRatio':
|
|
1731
|
+
if (!isPositiveNumber(newValue)) {
|
|
1732
|
+
this.aspectRatio = NaN;
|
|
1733
|
+
}
|
|
1734
|
+
break;
|
|
1735
|
+
case 'initialAspectRatio':
|
|
1736
|
+
if (!isPositiveNumber(newValue)) {
|
|
1737
|
+
this.initialAspectRatio = NaN;
|
|
1738
|
+
}
|
|
1739
|
+
break;
|
|
1740
|
+
case 'initialCoverage':
|
|
1741
|
+
if (!isPositiveNumber(newValue) || newValue > 1) {
|
|
1742
|
+
this.initialCoverage = NaN;
|
|
1743
|
+
}
|
|
1744
|
+
break;
|
|
1745
|
+
case 'keyboard':
|
|
1746
|
+
this.$nextTick(() => {
|
|
1747
|
+
if (this.$canvas) {
|
|
1748
|
+
if (newValue) {
|
|
1749
|
+
if (!this.$onDocumentKeyDown) {
|
|
1750
|
+
this.$onDocumentKeyDown = this.$handleKeyDown.bind(this);
|
|
1751
|
+
on(this.ownerDocument, EVENT_KEYDOWN, this.$onDocumentKeyDown);
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
else if (this.$onDocumentKeyDown) {
|
|
1755
|
+
off(this.ownerDocument, EVENT_KEYDOWN, this.$onDocumentKeyDown);
|
|
1756
|
+
this.$onDocumentKeyDown = null;
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
});
|
|
1760
|
+
break;
|
|
1761
|
+
case 'multiple':
|
|
1762
|
+
this.$nextTick(() => {
|
|
1763
|
+
if (this.$canvas) {
|
|
1764
|
+
const selections = this.$getSelections();
|
|
1765
|
+
if (newValue) {
|
|
1766
|
+
selections.forEach((selection) => {
|
|
1767
|
+
selection.active = false;
|
|
1768
|
+
});
|
|
1769
|
+
this.active = true;
|
|
1770
|
+
this.$emit(EVENT_CHANGE, {
|
|
1771
|
+
x: this.x,
|
|
1772
|
+
y: this.y,
|
|
1773
|
+
width: this.width,
|
|
1774
|
+
height: this.height,
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1777
|
+
else {
|
|
1778
|
+
this.active = false;
|
|
1779
|
+
selections.slice(1).forEach((selection) => {
|
|
1780
|
+
this.$removeSelection(selection);
|
|
1781
|
+
});
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
});
|
|
1785
|
+
break;
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
connectedCallback() {
|
|
1789
|
+
super.connectedCallback();
|
|
1790
|
+
const $canvas = this.closest(this.$getTagNameOf(CROPPER_CANVAS));
|
|
1791
|
+
if ($canvas) {
|
|
1792
|
+
this.$canvas = $canvas;
|
|
1793
|
+
this.$setStyles({
|
|
1794
|
+
position: 'absolute',
|
|
1795
|
+
transform: `translate(${this.x}px, ${this.y}px)`,
|
|
1796
|
+
});
|
|
1797
|
+
if (!this.hidden) {
|
|
1798
|
+
this.$render();
|
|
1799
|
+
}
|
|
1800
|
+
const { initialCoverage, parentElement } = this;
|
|
1801
|
+
if (isPositiveNumber(initialCoverage) && parentElement) {
|
|
1802
|
+
const aspectRatio = this.aspectRatio || this.initialAspectRatio;
|
|
1803
|
+
const { offsetWidth, offsetHeight } = parentElement;
|
|
1804
|
+
let width = offsetWidth * initialCoverage;
|
|
1805
|
+
let height = offsetHeight * initialCoverage;
|
|
1806
|
+
if (isPositiveNumber(aspectRatio)) {
|
|
1807
|
+
({ width, height } = getAdjustedSizes({ aspectRatio, width, height }));
|
|
1808
|
+
}
|
|
1809
|
+
this.$change(this.x, this.y, width, height);
|
|
1810
|
+
this.$center();
|
|
1811
|
+
}
|
|
1812
|
+
this.$onCanvasActionStart = this.$handleActionStart.bind(this);
|
|
1813
|
+
this.$onCanvasActionEnd = this.$handleActionEnd.bind(this);
|
|
1814
|
+
this.$onCanvasAction = this.$handleAction.bind(this);
|
|
1815
|
+
on($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
|
|
1816
|
+
on($canvas, EVENT_ACTION_END, this.$onCanvasActionStart);
|
|
1817
|
+
on($canvas, EVENT_ACTION, this.$onCanvasAction);
|
|
1818
|
+
}
|
|
1819
|
+
else {
|
|
1820
|
+
this.$render();
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
disconnectedCallback() {
|
|
1824
|
+
const { $canvas } = this;
|
|
1825
|
+
if ($canvas) {
|
|
1826
|
+
if (this.$onCanvasActionStart) {
|
|
1827
|
+
off($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
|
|
1828
|
+
this.$onCanvasActionStart = null;
|
|
1829
|
+
}
|
|
1830
|
+
if (this.$onCanvasActionEnd) {
|
|
1831
|
+
off($canvas, EVENT_ACTION_END, this.$onCanvasActionEnd);
|
|
1832
|
+
this.$onCanvasActionEnd = null;
|
|
1833
|
+
}
|
|
1834
|
+
if (this.$onCanvasAction) {
|
|
1835
|
+
off($canvas, EVENT_ACTION, this.$onCanvasAction);
|
|
1836
|
+
this.$onCanvasAction = null;
|
|
1837
|
+
}
|
|
1838
|
+
}
|
|
1839
|
+
super.disconnectedCallback();
|
|
1840
|
+
}
|
|
1841
|
+
$getSelections() {
|
|
1842
|
+
let selections = [];
|
|
1843
|
+
if (this.parentElement) {
|
|
1844
|
+
selections = Array.from(this.parentElement.querySelectorAll(this.$getTagNameOf(CROPPER_SELECTION)));
|
|
1845
|
+
}
|
|
1846
|
+
return selections;
|
|
1847
|
+
}
|
|
1848
|
+
$createSelection() {
|
|
1849
|
+
const newSelection = this.cloneNode(true);
|
|
1850
|
+
if (this.hasAttribute('id')) {
|
|
1851
|
+
newSelection.removeAttribute('id');
|
|
1852
|
+
}
|
|
1853
|
+
this.active = false;
|
|
1854
|
+
if (this.parentElement) {
|
|
1855
|
+
this.parentElement.insertBefore(newSelection, this.nextSibling);
|
|
1856
|
+
}
|
|
1857
|
+
return newSelection;
|
|
1858
|
+
}
|
|
1859
|
+
$removeSelection(selection = this) {
|
|
1860
|
+
if (this.parentElement) {
|
|
1861
|
+
const selections = this.$getSelections();
|
|
1862
|
+
if (selections.length > 1) {
|
|
1863
|
+
const index = selections.indexOf(selection);
|
|
1864
|
+
const activeSelection = selections[index + 1] || selections[index - 1];
|
|
1865
|
+
if (activeSelection) {
|
|
1866
|
+
selection.active = false;
|
|
1867
|
+
this.parentElement.removeChild(selection);
|
|
1868
|
+
activeSelection.active = true;
|
|
1869
|
+
activeSelection.$emit(EVENT_CHANGE, {
|
|
1870
|
+
x: activeSelection.x,
|
|
1871
|
+
y: activeSelection.y,
|
|
1872
|
+
width: activeSelection.width,
|
|
1873
|
+
height: activeSelection.height,
|
|
1874
|
+
});
|
|
1875
|
+
}
|
|
1876
|
+
}
|
|
1877
|
+
else {
|
|
1878
|
+
this.$reset();
|
|
1879
|
+
this.hidden = true;
|
|
1880
|
+
}
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
$handleActionStart(event) {
|
|
1884
|
+
var _a, _b;
|
|
1885
|
+
const relatedTarget = (_b = (_a = event.detail) === null || _a === void 0 ? void 0 : _a.relatedEvent) === null || _b === void 0 ? void 0 : _b.target;
|
|
1886
|
+
this.$action = '';
|
|
1887
|
+
this.$actionStartTarget = relatedTarget;
|
|
1888
|
+
if (!this.hidden
|
|
1889
|
+
&& this.multiple
|
|
1890
|
+
&& !this.active
|
|
1891
|
+
&& relatedTarget === this
|
|
1892
|
+
&& this.parentElement) {
|
|
1893
|
+
this.$getSelections().forEach((selection) => {
|
|
1894
|
+
selection.active = false;
|
|
1895
|
+
});
|
|
1896
|
+
this.active = true;
|
|
1897
|
+
this.$emit(EVENT_CHANGE, {
|
|
1898
|
+
x: this.x,
|
|
1899
|
+
y: this.y,
|
|
1900
|
+
width: this.width,
|
|
1901
|
+
height: this.height,
|
|
1902
|
+
});
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
$handleAction(event) {
|
|
1906
|
+
const { currentTarget, detail } = event;
|
|
1907
|
+
if (currentTarget && detail) {
|
|
1908
|
+
const { relatedEvent } = detail;
|
|
1909
|
+
let { action } = detail;
|
|
1910
|
+
// Switching to another selection
|
|
1911
|
+
if (!action && this.multiple) {
|
|
1912
|
+
// Get the `action` property from the focusing in selection
|
|
1913
|
+
action = this.$action || (relatedEvent === null || relatedEvent === void 0 ? void 0 : relatedEvent.target.action);
|
|
1914
|
+
this.$action = action;
|
|
1915
|
+
}
|
|
1916
|
+
if (!action
|
|
1917
|
+
|| (this.hidden && action !== ACTION_SELECT)
|
|
1918
|
+
|| (this.multiple && !this.active && action !== ACTION_SCALE)) {
|
|
1919
|
+
return;
|
|
1920
|
+
}
|
|
1921
|
+
const moveX = detail.endX - detail.startX;
|
|
1922
|
+
const moveY = detail.endY - detail.startY;
|
|
1923
|
+
const { width, height } = this;
|
|
1924
|
+
let { aspectRatio } = this;
|
|
1925
|
+
// Locking aspect ratio by holding shift key
|
|
1926
|
+
if (!isPositiveNumber(aspectRatio) && event.shiftKey) {
|
|
1927
|
+
aspectRatio = isPositiveNumber(width) && isPositiveNumber(height) ? width / height : 1;
|
|
1928
|
+
}
|
|
1929
|
+
switch (action) {
|
|
1930
|
+
case ACTION_SELECT: {
|
|
1931
|
+
const { $canvas } = this;
|
|
1932
|
+
const offset = getOffset(currentTarget);
|
|
1933
|
+
(this.multiple && !this.hidden ? this.$createSelection() : this).$change(detail.startX - offset.left, detail.startY - offset.top, moveX, moveY, aspectRatio);
|
|
1934
|
+
action = ACTION_RESIZE_SOUTHEAST;
|
|
1935
|
+
if (moveX < 0) {
|
|
1936
|
+
if (moveY > 0) {
|
|
1937
|
+
action = ACTION_RESIZE_SOUTHWEST;
|
|
1938
|
+
}
|
|
1939
|
+
else if (moveY < 0) {
|
|
1940
|
+
action = ACTION_RESIZE_NORTHWEST;
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
else if (moveX > 0) {
|
|
1944
|
+
if (moveY < 0) {
|
|
1945
|
+
action = ACTION_RESIZE_NORTHEAST;
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
if ($canvas) {
|
|
1949
|
+
$canvas.$action = action;
|
|
1950
|
+
}
|
|
1951
|
+
break;
|
|
1952
|
+
}
|
|
1953
|
+
case ACTION_MOVE:
|
|
1954
|
+
if (this.movable
|
|
1955
|
+
&& (this.$actionStartTarget && this.contains(this.$actionStartTarget))) {
|
|
1956
|
+
this.$move(moveX, moveY);
|
|
1957
|
+
}
|
|
1958
|
+
break;
|
|
1959
|
+
case ACTION_SCALE:
|
|
1960
|
+
if (relatedEvent && this.zoomable) {
|
|
1961
|
+
const offset = getOffset(currentTarget);
|
|
1962
|
+
this.$zoom(detail.scale, relatedEvent.pageX - offset.left, relatedEvent.pageY - offset.top);
|
|
1963
|
+
}
|
|
1964
|
+
break;
|
|
1965
|
+
default:
|
|
1966
|
+
this.$resize(action, moveX, moveY, aspectRatio);
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
$handleActionEnd() {
|
|
1971
|
+
this.$action = '';
|
|
1972
|
+
this.$actionStartTarget = null;
|
|
1973
|
+
}
|
|
1974
|
+
$handleKeyDown(event) {
|
|
1975
|
+
if (this.hidden
|
|
1976
|
+
|| !this.keyboard
|
|
1977
|
+
|| (this.multiple && !this.active)
|
|
1978
|
+
|| event.defaultPrevented) {
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
switch (event.key) {
|
|
1982
|
+
case 'Backspace':
|
|
1983
|
+
if (event.metaKey) {
|
|
1984
|
+
event.preventDefault();
|
|
1985
|
+
this.$removeSelection();
|
|
1986
|
+
}
|
|
1987
|
+
break;
|
|
1988
|
+
case 'Delete':
|
|
1989
|
+
event.preventDefault();
|
|
1990
|
+
this.$removeSelection();
|
|
1991
|
+
break;
|
|
1992
|
+
// Move to the left
|
|
1993
|
+
case 'ArrowLeft':
|
|
1994
|
+
event.preventDefault();
|
|
1995
|
+
this.$move(-1, 0);
|
|
1996
|
+
break;
|
|
1997
|
+
// Move to the right
|
|
1998
|
+
case 'ArrowRight':
|
|
1999
|
+
event.preventDefault();
|
|
2000
|
+
this.$move(1, 0);
|
|
2001
|
+
break;
|
|
2002
|
+
// Move to the top
|
|
2003
|
+
case 'ArrowUp':
|
|
2004
|
+
event.preventDefault();
|
|
2005
|
+
this.$move(0, -1);
|
|
2006
|
+
break;
|
|
2007
|
+
// Move to the bottom
|
|
2008
|
+
case 'ArrowDown':
|
|
2009
|
+
event.preventDefault();
|
|
2010
|
+
this.$move(0, 1);
|
|
2011
|
+
break;
|
|
2012
|
+
case '+':
|
|
2013
|
+
event.preventDefault();
|
|
2014
|
+
this.$zoom(0.1);
|
|
2015
|
+
break;
|
|
2016
|
+
case '-':
|
|
2017
|
+
event.preventDefault();
|
|
2018
|
+
this.$zoom(-0.1);
|
|
2019
|
+
break;
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
/**
|
|
2023
|
+
* Aligns the selection to the center of its parent element.
|
|
2024
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2025
|
+
*/
|
|
2026
|
+
$center() {
|
|
2027
|
+
const { parentElement } = this;
|
|
2028
|
+
if (!parentElement) {
|
|
2029
|
+
return this;
|
|
2030
|
+
}
|
|
2031
|
+
const x = (parentElement.offsetWidth - this.width) / 2;
|
|
2032
|
+
const y = (parentElement.offsetHeight - this.height) / 2;
|
|
2033
|
+
return this.$change(x, y);
|
|
2034
|
+
}
|
|
2035
|
+
/**
|
|
2036
|
+
* Moves the selection.
|
|
2037
|
+
* @param {number} x The moving distance in the horizontal direction.
|
|
2038
|
+
* @param {number} [y] The moving distance in the vertical direction.
|
|
2039
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2040
|
+
*/
|
|
2041
|
+
$move(x, y = x) {
|
|
2042
|
+
return this.$moveTo(this.x + x, this.y + y);
|
|
2043
|
+
}
|
|
2044
|
+
/**
|
|
2045
|
+
* Moves the selection to a specific position.
|
|
2046
|
+
* @param {number} x The new position in the horizontal direction.
|
|
2047
|
+
* @param {number} [y] The new position in the vertical direction.
|
|
2048
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2049
|
+
*/
|
|
2050
|
+
$moveTo(x, y = x) {
|
|
2051
|
+
if (!this.movable) {
|
|
2052
|
+
return this;
|
|
2053
|
+
}
|
|
2054
|
+
return this.$change(x, y);
|
|
2055
|
+
}
|
|
2056
|
+
/**
|
|
2057
|
+
* Adjusts the size the selection on a specific side or corner.
|
|
2058
|
+
* @param {string} action Indicates the side or corner to resize.
|
|
2059
|
+
* @param {number} [offsetX] The horizontal offset of the specific side or corner.
|
|
2060
|
+
* @param {number} [offsetY] The vertical offset of the specific side or corner.
|
|
2061
|
+
* @param {number} [aspectRatio] The aspect ratio for computing the new size if it is necessary.
|
|
2062
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2063
|
+
*/
|
|
2064
|
+
$resize(action, offsetX = 0, offsetY = 0, aspectRatio = this.aspectRatio) {
|
|
2065
|
+
if (!this.resizable) {
|
|
2066
|
+
return this;
|
|
2067
|
+
}
|
|
2068
|
+
const hasValidAspectRatio = isPositiveNumber(aspectRatio);
|
|
2069
|
+
const { $canvas } = this;
|
|
2070
|
+
let { x, y, width, height, } = this;
|
|
2071
|
+
switch (action) {
|
|
2072
|
+
case ACTION_RESIZE_NORTH:
|
|
2073
|
+
y += offsetY;
|
|
2074
|
+
height -= offsetY;
|
|
2075
|
+
if (height < 0) {
|
|
2076
|
+
action = ACTION_RESIZE_SOUTH;
|
|
2077
|
+
height = -height;
|
|
2078
|
+
y -= height;
|
|
2079
|
+
}
|
|
2080
|
+
if (hasValidAspectRatio) {
|
|
2081
|
+
offsetX = offsetY * aspectRatio;
|
|
2082
|
+
x += offsetX / 2;
|
|
2083
|
+
width -= offsetX;
|
|
2084
|
+
if (width < 0) {
|
|
2085
|
+
width = -width;
|
|
2086
|
+
x -= width;
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
break;
|
|
2090
|
+
case ACTION_RESIZE_EAST:
|
|
2091
|
+
width += offsetX;
|
|
2092
|
+
if (width < 0) {
|
|
2093
|
+
action = ACTION_RESIZE_WEST;
|
|
2094
|
+
width = -width;
|
|
2095
|
+
x -= width;
|
|
2096
|
+
}
|
|
2097
|
+
if (hasValidAspectRatio) {
|
|
2098
|
+
offsetY = offsetX / aspectRatio;
|
|
2099
|
+
y -= offsetY / 2;
|
|
2100
|
+
height += offsetY;
|
|
2101
|
+
if (height < 0) {
|
|
2102
|
+
height = -height;
|
|
2103
|
+
y -= height;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
break;
|
|
2107
|
+
case ACTION_RESIZE_SOUTH:
|
|
2108
|
+
height += offsetY;
|
|
2109
|
+
if (height < 0) {
|
|
2110
|
+
action = ACTION_RESIZE_NORTH;
|
|
2111
|
+
height = -height;
|
|
2112
|
+
y -= height;
|
|
2113
|
+
}
|
|
2114
|
+
if (hasValidAspectRatio) {
|
|
2115
|
+
offsetX = offsetY * aspectRatio;
|
|
2116
|
+
x -= offsetX / 2;
|
|
2117
|
+
width += offsetX;
|
|
2118
|
+
if (width < 0) {
|
|
2119
|
+
width = -width;
|
|
2120
|
+
x -= width;
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
break;
|
|
2124
|
+
case ACTION_RESIZE_WEST:
|
|
2125
|
+
x += offsetX;
|
|
2126
|
+
width -= offsetX;
|
|
2127
|
+
if (width < 0) {
|
|
2128
|
+
action = ACTION_RESIZE_EAST;
|
|
2129
|
+
width = -width;
|
|
2130
|
+
x -= width;
|
|
2131
|
+
}
|
|
2132
|
+
if (hasValidAspectRatio) {
|
|
2133
|
+
offsetY = offsetX / aspectRatio;
|
|
2134
|
+
y += offsetY / 2;
|
|
2135
|
+
height -= offsetY;
|
|
2136
|
+
if (height < 0) {
|
|
2137
|
+
height = -height;
|
|
2138
|
+
y -= height;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
break;
|
|
2142
|
+
case ACTION_RESIZE_NORTHEAST:
|
|
2143
|
+
if (hasValidAspectRatio) {
|
|
2144
|
+
offsetY = -offsetX / aspectRatio;
|
|
2145
|
+
}
|
|
2146
|
+
y += offsetY;
|
|
2147
|
+
height -= offsetY;
|
|
2148
|
+
width += offsetX;
|
|
2149
|
+
if (width < 0 && height < 0) {
|
|
2150
|
+
action = ACTION_RESIZE_SOUTHWEST;
|
|
2151
|
+
width = -width;
|
|
2152
|
+
height = -height;
|
|
2153
|
+
x -= width;
|
|
2154
|
+
y -= height;
|
|
2155
|
+
}
|
|
2156
|
+
else if (width < 0) {
|
|
2157
|
+
action = ACTION_RESIZE_NORTHWEST;
|
|
2158
|
+
width = -width;
|
|
2159
|
+
x -= width;
|
|
2160
|
+
}
|
|
2161
|
+
else if (height < 0) {
|
|
2162
|
+
action = ACTION_RESIZE_SOUTHEAST;
|
|
2163
|
+
height = -height;
|
|
2164
|
+
y -= height;
|
|
2165
|
+
}
|
|
2166
|
+
break;
|
|
2167
|
+
case ACTION_RESIZE_NORTHWEST:
|
|
2168
|
+
if (hasValidAspectRatio) {
|
|
2169
|
+
offsetY = offsetX / aspectRatio;
|
|
2170
|
+
}
|
|
2171
|
+
x += offsetX;
|
|
2172
|
+
y += offsetY;
|
|
2173
|
+
width -= offsetX;
|
|
2174
|
+
height -= offsetY;
|
|
2175
|
+
if (width < 0 && height < 0) {
|
|
2176
|
+
action = ACTION_RESIZE_SOUTHEAST;
|
|
2177
|
+
width = -width;
|
|
2178
|
+
height = -height;
|
|
2179
|
+
x -= width;
|
|
2180
|
+
y -= height;
|
|
2181
|
+
}
|
|
2182
|
+
else if (width < 0) {
|
|
2183
|
+
action = ACTION_RESIZE_NORTHEAST;
|
|
2184
|
+
width = -width;
|
|
2185
|
+
x -= width;
|
|
2186
|
+
}
|
|
2187
|
+
else if (height < 0) {
|
|
2188
|
+
action = ACTION_RESIZE_SOUTHWEST;
|
|
2189
|
+
height = -height;
|
|
2190
|
+
y -= height;
|
|
2191
|
+
}
|
|
2192
|
+
break;
|
|
2193
|
+
case ACTION_RESIZE_SOUTHEAST:
|
|
2194
|
+
if (hasValidAspectRatio) {
|
|
2195
|
+
offsetY = offsetX / aspectRatio;
|
|
2196
|
+
}
|
|
2197
|
+
width += offsetX;
|
|
2198
|
+
height += offsetY;
|
|
2199
|
+
if (width < 0 && height < 0) {
|
|
2200
|
+
action = ACTION_RESIZE_NORTHWEST;
|
|
2201
|
+
width = -width;
|
|
2202
|
+
height = -height;
|
|
2203
|
+
x -= width;
|
|
2204
|
+
y -= height;
|
|
2205
|
+
}
|
|
2206
|
+
else if (width < 0) {
|
|
2207
|
+
action = ACTION_RESIZE_SOUTHWEST;
|
|
2208
|
+
width = -width;
|
|
2209
|
+
x -= width;
|
|
2210
|
+
}
|
|
2211
|
+
else if (height < 0) {
|
|
2212
|
+
action = ACTION_RESIZE_NORTHEAST;
|
|
2213
|
+
height = -height;
|
|
2214
|
+
y -= height;
|
|
2215
|
+
}
|
|
2216
|
+
break;
|
|
2217
|
+
case ACTION_RESIZE_SOUTHWEST:
|
|
2218
|
+
if (hasValidAspectRatio) {
|
|
2219
|
+
offsetY = -offsetX / aspectRatio;
|
|
2220
|
+
}
|
|
2221
|
+
x += offsetX;
|
|
2222
|
+
width -= offsetX;
|
|
2223
|
+
height += offsetY;
|
|
2224
|
+
if (width < 0 && height < 0) {
|
|
2225
|
+
action = ACTION_RESIZE_NORTHEAST;
|
|
2226
|
+
width = -width;
|
|
2227
|
+
height = -height;
|
|
2228
|
+
x -= width;
|
|
2229
|
+
y -= height;
|
|
2230
|
+
}
|
|
2231
|
+
else if (width < 0) {
|
|
2232
|
+
action = ACTION_RESIZE_SOUTHEAST;
|
|
2233
|
+
width = -width;
|
|
2234
|
+
x -= width;
|
|
2235
|
+
}
|
|
2236
|
+
else if (height < 0) {
|
|
2237
|
+
action = ACTION_RESIZE_NORTHWEST;
|
|
2238
|
+
height = -height;
|
|
2239
|
+
y -= height;
|
|
2240
|
+
}
|
|
2241
|
+
break;
|
|
2242
|
+
}
|
|
2243
|
+
if ($canvas) {
|
|
2244
|
+
$canvas.$setAction(action);
|
|
2245
|
+
}
|
|
2246
|
+
return this.$change(x, y, width, height);
|
|
2247
|
+
}
|
|
2248
|
+
/**
|
|
2249
|
+
* Zooms the selection.
|
|
2250
|
+
* @param {number} scale The zoom factor. Positive numbers for zooming in, and negative numbers for zooming out.
|
|
2251
|
+
* @param {number} [x] The zoom origin in the horizontal, defaults to the center of the selection.
|
|
2252
|
+
* @param {number} [y] The zoom origin in the vertical, defaults to the center of the selection.
|
|
2253
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2254
|
+
*/
|
|
2255
|
+
$zoom(scale, x, y) {
|
|
2256
|
+
if (!this.zoomable || scale === 0) {
|
|
2257
|
+
return this;
|
|
2258
|
+
}
|
|
2259
|
+
if (scale < 0) {
|
|
2260
|
+
scale = 1 / (1 - scale);
|
|
2261
|
+
}
|
|
2262
|
+
else {
|
|
2263
|
+
scale += 1;
|
|
2264
|
+
}
|
|
2265
|
+
const { width, height } = this;
|
|
2266
|
+
const newWidth = width * scale;
|
|
2267
|
+
const newHeight = height * scale;
|
|
2268
|
+
let newX = this.x;
|
|
2269
|
+
let newY = this.y;
|
|
2270
|
+
if (isNumber(x) && isNumber(y)) {
|
|
2271
|
+
newX -= (newWidth - width) * ((x - this.x) / width);
|
|
2272
|
+
newY -= (newHeight - height) * ((y - this.y) / height);
|
|
2273
|
+
}
|
|
2274
|
+
else {
|
|
2275
|
+
// Zoom from the center of the selection
|
|
2276
|
+
newX -= (newWidth - width) / 2;
|
|
2277
|
+
newY -= (newHeight - height) / 2;
|
|
2278
|
+
}
|
|
2279
|
+
return this.$change(newX, newY, newWidth, newHeight);
|
|
2280
|
+
}
|
|
2281
|
+
/**
|
|
2282
|
+
* Changes the position and/or size of the selection.
|
|
2283
|
+
* @param {number} x The new position in the horizontal direction.
|
|
2284
|
+
* @param {number} y The new position in the vertical direction.
|
|
2285
|
+
* @param {number} [width] The new width.
|
|
2286
|
+
* @param {number} [height] The new height.
|
|
2287
|
+
* @param {number} [aspectRatio] The new aspect ratio for this change only.
|
|
2288
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2289
|
+
*/
|
|
2290
|
+
$change(x, y, width = this.width, height = this.height, aspectRatio = this.aspectRatio) {
|
|
2291
|
+
if (!isNumber(x) || !isNumber(y) || !isNumber(width) || !isNumber(height)) {
|
|
2292
|
+
return this;
|
|
2293
|
+
}
|
|
2294
|
+
if (!this.precise) {
|
|
2295
|
+
x = Math.round(x);
|
|
2296
|
+
y = Math.round(y);
|
|
2297
|
+
width = Math.round(width);
|
|
2298
|
+
height = Math.round(height);
|
|
2299
|
+
}
|
|
2300
|
+
if (x === this.x && y === this.y && width === this.width && height === this.height) {
|
|
2301
|
+
return this;
|
|
2302
|
+
}
|
|
2303
|
+
if (this.hidden) {
|
|
2304
|
+
this.hidden = false;
|
|
2305
|
+
}
|
|
2306
|
+
if (isPositiveNumber(aspectRatio)) {
|
|
2307
|
+
({ width, height } = getAdjustedSizes({ aspectRatio, width, height }, 'cover'));
|
|
2308
|
+
}
|
|
2309
|
+
if (this.$emit(EVENT_CHANGE, {
|
|
2310
|
+
x,
|
|
2311
|
+
y,
|
|
2312
|
+
width,
|
|
2313
|
+
height,
|
|
2314
|
+
}) === false) {
|
|
2315
|
+
return this;
|
|
2316
|
+
}
|
|
2317
|
+
this.x = x;
|
|
2318
|
+
this.y = y;
|
|
2319
|
+
this.width = width;
|
|
2320
|
+
this.height = height;
|
|
2321
|
+
return this.$render();
|
|
2322
|
+
}
|
|
2323
|
+
/**
|
|
2324
|
+
* Resets the selection to its initial position and size.
|
|
2325
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2326
|
+
*/
|
|
2327
|
+
$reset() {
|
|
2328
|
+
return this.$change(0, 0, 0, 0);
|
|
2329
|
+
}
|
|
2330
|
+
/**
|
|
2331
|
+
* Refreshes the position or size of the selection.
|
|
2332
|
+
* @returns {CropperSelection} Returns `this` for chaining.
|
|
2333
|
+
*/
|
|
2334
|
+
$render() {
|
|
2335
|
+
return this.$setStyles({
|
|
2336
|
+
transform: `translate(${this.x}px, ${this.y}px)`,
|
|
2337
|
+
width: this.width,
|
|
2338
|
+
height: this.height,
|
|
2339
|
+
});
|
|
2340
|
+
}
|
|
2341
|
+
/**
|
|
2342
|
+
* Generates a real canvas element, with the image (selected area only) draw into if there is one.
|
|
2343
|
+
* @param {object} [options] The available options.
|
|
2344
|
+
* @param {number} [options.width] The width of the canvas.
|
|
2345
|
+
* @param {number} [options.height] The height of the canvas.
|
|
2346
|
+
* @param {Function} [options.beforeDraw] The function called before drawing the image onto the canvas.
|
|
2347
|
+
* @returns {Promise} Returns a promise that resolves to the generated canvas element.
|
|
2348
|
+
*/
|
|
2349
|
+
$toCanvas(options) {
|
|
2350
|
+
return new Promise((resolve, reject) => {
|
|
2351
|
+
if (!this.isConnected) {
|
|
2352
|
+
reject(new Error('The current element is not connected to the DOM.'));
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
const canvas = document.createElement('canvas');
|
|
2356
|
+
let { width, height } = this;
|
|
2357
|
+
let scale = 1;
|
|
2358
|
+
if (isPlainObject(options)
|
|
2359
|
+
&& (isPositiveNumber(options.width) || isPositiveNumber(options.height))) {
|
|
2360
|
+
({ width, height } = getAdjustedSizes({
|
|
2361
|
+
aspectRatio: width / height,
|
|
2362
|
+
width: options.width,
|
|
2363
|
+
height: options.height,
|
|
2364
|
+
}));
|
|
2365
|
+
scale = width / this.width;
|
|
2366
|
+
}
|
|
2367
|
+
canvas.width = width;
|
|
2368
|
+
canvas.height = height;
|
|
2369
|
+
if (!this.$canvas) {
|
|
2370
|
+
resolve(canvas);
|
|
2371
|
+
return;
|
|
2372
|
+
}
|
|
2373
|
+
const cropperImage = this.$canvas.querySelector(this.$getTagNameOf(CROPPER_IMAGE));
|
|
2374
|
+
if (!cropperImage) {
|
|
2375
|
+
resolve(canvas);
|
|
2376
|
+
return;
|
|
2377
|
+
}
|
|
2378
|
+
cropperImage.$ready().then((image) => {
|
|
2379
|
+
const context = canvas.getContext('2d');
|
|
2380
|
+
if (context) {
|
|
2381
|
+
const [a, b, c, d, e, f] = cropperImage.$getTransform();
|
|
2382
|
+
const offsetX = -this.x;
|
|
2383
|
+
const offsetY = -this.y;
|
|
2384
|
+
const translateX = ((offsetX * d) - (c * offsetY)) / ((a * d) - (c * b));
|
|
2385
|
+
const translateY = (offsetY - (b * translateX)) / d;
|
|
2386
|
+
let newE = a * translateX + c * translateY + e;
|
|
2387
|
+
let newF = b * translateX + d * translateY + f;
|
|
2388
|
+
let destWidth = image.naturalWidth;
|
|
2389
|
+
let destHeight = image.naturalHeight;
|
|
2390
|
+
if (scale !== 1) {
|
|
2391
|
+
newE *= scale;
|
|
2392
|
+
newF *= scale;
|
|
2393
|
+
destWidth *= scale;
|
|
2394
|
+
destHeight *= scale;
|
|
2395
|
+
}
|
|
2396
|
+
const centerX = destWidth / 2;
|
|
2397
|
+
const centerY = destHeight / 2;
|
|
2398
|
+
context.fillStyle = 'transparent';
|
|
2399
|
+
context.fillRect(0, 0, width, height);
|
|
2400
|
+
if (isPlainObject(options) && isFunction(options.beforeDraw)) {
|
|
2401
|
+
options.beforeDraw.call(this, context, canvas);
|
|
2402
|
+
}
|
|
2403
|
+
context.save();
|
|
2404
|
+
// Move the transform origin to the center of the image.
|
|
2405
|
+
// https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin
|
|
2406
|
+
context.translate(centerX, centerY);
|
|
2407
|
+
context.transform(a, b, c, d, newE, newF);
|
|
2408
|
+
// Move the transform origin to the top-left of the image.
|
|
2409
|
+
context.translate(-centerX, -centerY);
|
|
2410
|
+
context.drawImage(image, 0, 0, destWidth, destHeight);
|
|
2411
|
+
context.restore();
|
|
2412
|
+
}
|
|
2413
|
+
resolve(canvas);
|
|
2414
|
+
}).catch(reject);
|
|
2415
|
+
});
|
|
2416
|
+
}
|
|
2417
|
+
}
|
|
2418
|
+
CropperSelection.$name = CROPPER_SELECTION;
|
|
2419
|
+
CropperSelection.$version = '2.0.0-beta.4';
|
|
2420
|
+
|
|
2421
|
+
var style$2 = `:host{display:flex;flex-direction:column;position:relative;touch-action:none;-webkit-user-select:none;-moz-user-select:none;user-select:none}:host([bordered]){border:1px dashed var(--theme-color)}:host([covered]){bottom:0;left:0;position:absolute;right:0;top:0}:host>span{display:flex;flex:1}:host>span+span{border-top:1px dashed var(--theme-color)}:host>span>span{flex:1}:host>span>span+span{border-left:1px dashed var(--theme-color)}`;
|
|
2422
|
+
|
|
2423
|
+
class CropperGrid extends CropperElement {
|
|
2424
|
+
constructor() {
|
|
2425
|
+
super(...arguments);
|
|
2426
|
+
this.$style = style$2;
|
|
2427
|
+
this.bordered = false;
|
|
2428
|
+
this.columns = 3;
|
|
2429
|
+
this.covered = false;
|
|
2430
|
+
this.rows = 3;
|
|
2431
|
+
this.slottable = false;
|
|
2432
|
+
this.themeColor = 'rgba(238, 238, 238, 0.5)';
|
|
2433
|
+
}
|
|
2434
|
+
static get observedAttributes() {
|
|
2435
|
+
return super.observedAttributes.concat([
|
|
2436
|
+
'bordered',
|
|
2437
|
+
'columns',
|
|
2438
|
+
'covered',
|
|
2439
|
+
'rows',
|
|
2440
|
+
]);
|
|
2441
|
+
}
|
|
2442
|
+
$propertyChangedCallback(name, oldValue, newValue) {
|
|
2443
|
+
if (Object.is(newValue, oldValue)) {
|
|
2444
|
+
return;
|
|
2445
|
+
}
|
|
2446
|
+
super.$propertyChangedCallback(name, oldValue, newValue);
|
|
2447
|
+
if (name === 'rows' || name === 'columns') {
|
|
2448
|
+
this.$render();
|
|
2449
|
+
}
|
|
2450
|
+
}
|
|
2451
|
+
connectedCallback() {
|
|
2452
|
+
super.connectedCallback();
|
|
2453
|
+
this.$render();
|
|
2454
|
+
}
|
|
2455
|
+
$render() {
|
|
2456
|
+
const shadow = this.$getShadowRoot();
|
|
2457
|
+
const fragment = document.createDocumentFragment();
|
|
2458
|
+
for (let i = 0; i < this.rows; i += 1) {
|
|
2459
|
+
const row = document.createElement('span');
|
|
2460
|
+
row.setAttribute('role', 'row');
|
|
2461
|
+
for (let j = 0; j < this.columns; j += 1) {
|
|
2462
|
+
const column = document.createElement('span');
|
|
2463
|
+
column.setAttribute('role', 'gridcell');
|
|
2464
|
+
row.appendChild(column);
|
|
2465
|
+
}
|
|
2466
|
+
fragment.appendChild(row);
|
|
2467
|
+
}
|
|
2468
|
+
if (shadow) {
|
|
2469
|
+
shadow.innerHTML = '';
|
|
2470
|
+
shadow.appendChild(fragment);
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
CropperGrid.$name = CROPPER_GIRD;
|
|
2475
|
+
CropperGrid.$version = '2.0.0-beta.4';
|
|
2476
|
+
|
|
2477
|
+
var style$1 = `:host{display:inline-block;height:1em;position:relative;touch-action:none;-webkit-user-select:none;-moz-user-select:none;user-select:none;vertical-align:middle;width:1em}:host:after,:host:before{background-color:var(--theme-color);content:"";display:block;position:absolute}:host:before{height:1px;left:0;top:50%;transform:translateY(-50%);width:100%}:host:after{height:100%;left:50%;top:0;transform:translateX(-50%);width:1px}:host([centered]){left:50%;position:absolute;top:50%;transform:translate(-50%,-50%)}`;
|
|
2478
|
+
|
|
2479
|
+
class CropperCrosshair extends CropperElement {
|
|
2480
|
+
constructor() {
|
|
2481
|
+
super(...arguments);
|
|
2482
|
+
this.$style = style$1;
|
|
2483
|
+
this.centered = false;
|
|
2484
|
+
this.slottable = false;
|
|
2485
|
+
this.themeColor = 'rgba(238, 238, 238, 0.5)';
|
|
2486
|
+
}
|
|
2487
|
+
static get observedAttributes() {
|
|
2488
|
+
return super.observedAttributes.concat([
|
|
2489
|
+
'centered',
|
|
2490
|
+
]);
|
|
2491
|
+
}
|
|
2492
|
+
}
|
|
2493
|
+
CropperCrosshair.$name = CROPPER_CROSSHAIR;
|
|
2494
|
+
CropperCrosshair.$version = '2.0.0-beta.4';
|
|
2495
|
+
|
|
2496
|
+
var style = `:host{display:block;height:100%;overflow:hidden;position:relative;width:100%}`;
|
|
2497
|
+
|
|
2498
|
+
const imageCache = new WeakMap();
|
|
2499
|
+
const selectionCache = new WeakMap();
|
|
2500
|
+
const sourceImageCache = new WeakMap();
|
|
2501
|
+
const RESIZE_BOTH = 'both';
|
|
2502
|
+
const RESIZE_HORIZONTAL = 'horizontal';
|
|
2503
|
+
const RESIZE_VERTICAL = 'vertical';
|
|
2504
|
+
const RESIZE_NONE = 'none';
|
|
2505
|
+
class CropperViewer extends CropperElement {
|
|
2506
|
+
constructor() {
|
|
2507
|
+
super(...arguments);
|
|
2508
|
+
this.$onSelectionChange = null;
|
|
2509
|
+
this.$onSourceImageLoad = null;
|
|
2510
|
+
this.$onSourceImageTransform = null;
|
|
2511
|
+
this.$scale = 1;
|
|
2512
|
+
this.$style = style;
|
|
2513
|
+
this.resize = RESIZE_VERTICAL;
|
|
2514
|
+
this.selection = '';
|
|
2515
|
+
this.slottable = false;
|
|
2516
|
+
}
|
|
2517
|
+
set $image(element) {
|
|
2518
|
+
imageCache.set(this, element);
|
|
2519
|
+
}
|
|
2520
|
+
get $image() {
|
|
2521
|
+
return imageCache.get(this);
|
|
2522
|
+
}
|
|
2523
|
+
set $sourceImage(element) {
|
|
2524
|
+
sourceImageCache.set(this, element);
|
|
2525
|
+
}
|
|
2526
|
+
get $sourceImage() {
|
|
2527
|
+
return sourceImageCache.get(this);
|
|
2528
|
+
}
|
|
2529
|
+
set $selection(element) {
|
|
2530
|
+
selectionCache.set(this, element);
|
|
2531
|
+
}
|
|
2532
|
+
get $selection() {
|
|
2533
|
+
return selectionCache.get(this);
|
|
2534
|
+
}
|
|
2535
|
+
static get observedAttributes() {
|
|
2536
|
+
return super.observedAttributes.concat([
|
|
2537
|
+
'resize',
|
|
2538
|
+
'selection',
|
|
2539
|
+
]);
|
|
2540
|
+
}
|
|
2541
|
+
connectedCallback() {
|
|
2542
|
+
super.connectedCallback();
|
|
2543
|
+
let $selection = null;
|
|
2544
|
+
if (this.selection) {
|
|
2545
|
+
$selection = this.ownerDocument.querySelector(this.selection);
|
|
2546
|
+
}
|
|
2547
|
+
else {
|
|
2548
|
+
$selection = this.closest(this.$getTagNameOf(CROPPER_SELECTION));
|
|
2549
|
+
}
|
|
2550
|
+
if (isElement($selection)) {
|
|
2551
|
+
this.$selection = $selection;
|
|
2552
|
+
this.$onSelectionChange = this.$handleSelectionChange.bind(this);
|
|
2553
|
+
on($selection, EVENT_CHANGE, this.$onSelectionChange);
|
|
2554
|
+
const $canvas = $selection.closest(this.$getTagNameOf(CROPPER_CANVAS));
|
|
2555
|
+
if ($canvas) {
|
|
2556
|
+
const $sourceImage = $canvas.querySelector(this.$getTagNameOf(CROPPER_IMAGE));
|
|
2557
|
+
if ($sourceImage) {
|
|
2558
|
+
this.$sourceImage = $sourceImage;
|
|
2559
|
+
this.$image = $sourceImage.cloneNode(true);
|
|
2560
|
+
this.$getShadowRoot().appendChild(this.$image);
|
|
2561
|
+
this.$onSourceImageLoad = this.$handleSourceImageLoad.bind(this);
|
|
2562
|
+
this.$onSourceImageTransform = this.$handleSourceImageTransform.bind(this);
|
|
2563
|
+
on($sourceImage.$image, EVENT_LOAD, this.$onSourceImageLoad);
|
|
2564
|
+
on($sourceImage, EVENT_TRANSFORM, this.$onSourceImageTransform);
|
|
2565
|
+
}
|
|
2566
|
+
}
|
|
2567
|
+
this.$render();
|
|
2568
|
+
}
|
|
2569
|
+
}
|
|
2570
|
+
disconnectedCallback() {
|
|
2571
|
+
const { $selection, $sourceImage } = this;
|
|
2572
|
+
if ($selection && this.$onSelectionChange) {
|
|
2573
|
+
off($selection, EVENT_CHANGE, this.$onSelectionChange);
|
|
2574
|
+
this.$onSelectionChange = null;
|
|
2575
|
+
}
|
|
2576
|
+
if ($sourceImage && this.$onSourceImageLoad) {
|
|
2577
|
+
off($sourceImage.$image, EVENT_LOAD, this.$onSourceImageLoad);
|
|
2578
|
+
this.$onSourceImageLoad = null;
|
|
2579
|
+
}
|
|
2580
|
+
if ($sourceImage && this.$onSourceImageTransform) {
|
|
2581
|
+
off($sourceImage, EVENT_TRANSFORM, this.$onSourceImageTransform);
|
|
2582
|
+
this.$onSourceImageTransform = null;
|
|
2583
|
+
}
|
|
2584
|
+
super.disconnectedCallback();
|
|
2585
|
+
}
|
|
2586
|
+
$handleSelectionChange(event) {
|
|
2587
|
+
this.$render(event.detail);
|
|
2588
|
+
}
|
|
2589
|
+
$handleSourceImageLoad() {
|
|
2590
|
+
const { $image, $sourceImage } = this;
|
|
2591
|
+
const oldSrc = $image.getAttribute('src');
|
|
2592
|
+
const newSrc = $sourceImage.getAttribute('src');
|
|
2593
|
+
if (newSrc && newSrc !== oldSrc) {
|
|
2594
|
+
$image.setAttribute('src', newSrc);
|
|
2595
|
+
$image.$ready(() => {
|
|
2596
|
+
setTimeout(() => {
|
|
2597
|
+
const { x, y } = this.$selection;
|
|
2598
|
+
this.$transformImageByOffset($sourceImage.$getTransform(), -x, -y);
|
|
2599
|
+
});
|
|
2600
|
+
});
|
|
2601
|
+
}
|
|
2602
|
+
}
|
|
2603
|
+
$handleSourceImageTransform(event) {
|
|
2604
|
+
const { x, y } = this.$selection;
|
|
2605
|
+
this.$transformImageByOffset(event.detail.matrix, -x, -y);
|
|
2606
|
+
}
|
|
2607
|
+
$render(selection) {
|
|
2608
|
+
const { x, y, width, height, } = selection || this.$selection;
|
|
2609
|
+
const styles = {};
|
|
2610
|
+
const { clientWidth, clientHeight } = this;
|
|
2611
|
+
let newWidth = clientWidth;
|
|
2612
|
+
let newHeight = clientHeight;
|
|
2613
|
+
let scale = NaN;
|
|
2614
|
+
switch (this.resize) {
|
|
2615
|
+
case RESIZE_BOTH:
|
|
2616
|
+
scale = 1;
|
|
2617
|
+
newWidth = width;
|
|
2618
|
+
newHeight = height;
|
|
2619
|
+
styles.width = width;
|
|
2620
|
+
styles.height = height;
|
|
2621
|
+
break;
|
|
2622
|
+
case RESIZE_HORIZONTAL:
|
|
2623
|
+
scale = clientHeight / height;
|
|
2624
|
+
newWidth = width * scale;
|
|
2625
|
+
styles.width = newWidth;
|
|
2626
|
+
break;
|
|
2627
|
+
case RESIZE_VERTICAL:
|
|
2628
|
+
scale = clientWidth / width;
|
|
2629
|
+
newHeight = height * scale;
|
|
2630
|
+
styles.height = newHeight;
|
|
2631
|
+
break;
|
|
2632
|
+
case RESIZE_NONE:
|
|
2633
|
+
default:
|
|
2634
|
+
if (clientWidth > 0) {
|
|
2635
|
+
scale = clientWidth / width;
|
|
2636
|
+
}
|
|
2637
|
+
else if (clientHeight > 0) {
|
|
2638
|
+
scale = clientHeight / height;
|
|
2639
|
+
}
|
|
2640
|
+
}
|
|
2641
|
+
this.$scale = scale;
|
|
2642
|
+
this.$setStyles(styles);
|
|
2643
|
+
if (this.$sourceImage) {
|
|
2644
|
+
this.$transformImageByOffset(this.$sourceImage.$getTransform(), -x, -y);
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
$transformImageByOffset(matrix, x, y) {
|
|
2648
|
+
const { $image, $scale, $sourceImage, } = this;
|
|
2649
|
+
if ($sourceImage && $image && $scale > 0) {
|
|
2650
|
+
const [a, b, c, d, e, f] = matrix;
|
|
2651
|
+
const translateX = ((x * d) - (c * y)) / ((a * d) - (c * b));
|
|
2652
|
+
const translateY = (y - (b * translateX)) / d;
|
|
2653
|
+
const newE = a * translateX + c * translateY + e;
|
|
2654
|
+
const newF = b * translateX + d * translateY + f;
|
|
2655
|
+
$image.$ready((image) => {
|
|
2656
|
+
this.$setStyles.call($image, {
|
|
2657
|
+
width: image.naturalWidth * $scale,
|
|
2658
|
+
height: image.naturalHeight * $scale,
|
|
2659
|
+
});
|
|
2660
|
+
});
|
|
2661
|
+
$image.$setTransform(a, b, c, d, newE * $scale, newF * $scale);
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
CropperViewer.$name = CROPPER_VIEWER;
|
|
2666
|
+
CropperViewer.$version = '2.0.0-beta.4';
|
|
2667
|
+
|
|
2668
|
+
/*! Cropper.js v2.0.0-beta.4 | (c) 2015-present Chen Fengyuan | MIT */
|
|
2669
|
+
CropperCanvas.$define();
|
|
2670
|
+
CropperCrosshair.$define();
|
|
2671
|
+
CropperGrid.$define();
|
|
2672
|
+
CropperHandle.$define();
|
|
2673
|
+
CropperImage.$define();
|
|
2674
|
+
CropperSelection.$define();
|
|
2675
|
+
CropperShade.$define();
|
|
2676
|
+
CropperViewer.$define();
|
|
2677
|
+
|
|
2678
|
+
const cropperComponentCss = ".relative{position:relative!important}.mb-6{margin-bottom:1.5rem!important}.flex{display:flex!important}.h-4{height:1rem!important}.h-\\[2px\\]{height:2px!important}.w-4{width:1rem!important}.w-full{width:100%!important}.appearance-none{-webkit-appearance:none!important;-moz-appearance:none!important;appearance:none!important}.flex-col{flex-direction:column!important}.items-center{align-items:center!important}.justify-between{justify-content:space-between!important}.rounded-round{border-radius:100%!important}.border-0{border-width:0!important}*{box-sizing:border-box}:host{align-items:center;display:flex;flex-direction:column;gap:1.5rem;justify-content:space-between;margin-bottom:1.5rem;width:100%}:host .p-input[type=range]{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:transparent;position:relative}:host .p-input[type=range]::-moz-range-track,:host .p-input[type=range]::-webkit-slider-runnable-track{--tw-bg-opacity:1;background-color:rgb(218 230 240/var(--tw-bg-opacity));height:2px}:host .p-input[type=range]::-moz-range-thumb,:host .p-input[type=range]::-webkit-slider-thumb{--tw-bg-opacity:1;background-color:rgb(82 138 250/var(--tw-bg-opacity));border-radius:100%;border-width:0;height:1rem;width:1rem}.static{position:static!important}.hidden{display:none!important}.h-\\[17\\.5rem\\]{height:17.5rem!important}.transform{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))!important}.resize{resize:both!important}.gap-2{gap:.5rem!important}.rounded{border-radius:.25rem!important}.border-b{border-bottom-width:1px!important}.border-solid{border-style:solid!important}.border-mystic-medium{--tw-border-opacity:1!important;border-color:rgb(227 236 243/var(--tw-border-opacity))!important}.bg-white{--tw-bg-opacity:1!important;background-color:rgb(255 255 255/var(--tw-bg-opacity))!important}.px-14{padding-left:3.5rem!important;padding-right:3.5rem!important}.text-storm-vague{--tw-text-opacity:1!important;color:rgb(152 154 183/var(--tw-text-opacity))!important}@media (min-width:40rem){.tablet\\:max-w-xs{max-width:20rem!important}.tablet\\:px-1{padding-left:.25rem!important;padding-right:.25rem!important}}";
|
|
2679
|
+
|
|
2680
|
+
const Cropper = class {
|
|
2681
|
+
constructor(hostRef) {
|
|
2682
|
+
index.registerInstance(this, hostRef);
|
|
2683
|
+
this.valueChange = index.createEvent(this, "valueChange", 3);
|
|
2684
|
+
this._maxScale = 0;
|
|
2685
|
+
this._minScale = 0;
|
|
2686
|
+
this.variant = 'user';
|
|
2687
|
+
this.value = undefined;
|
|
2688
|
+
this.returnType = 'base64';
|
|
2689
|
+
this._loaded = false;
|
|
2690
|
+
this._currentScale = 0;
|
|
2691
|
+
}
|
|
2692
|
+
onResize() {
|
|
2693
|
+
if (this._resizeDebounceTimeout) {
|
|
2694
|
+
clearTimeout(this._resizeDebounceTimeout);
|
|
2695
|
+
}
|
|
2696
|
+
this._resizeDebounceTimeout = setTimeout(() => {
|
|
2697
|
+
this._selectionRef.$center();
|
|
2698
|
+
this._imageRef.$center();
|
|
2699
|
+
}, 200);
|
|
2700
|
+
}
|
|
2701
|
+
componentDidLoad() {
|
|
2702
|
+
setTimeout(() => {
|
|
2703
|
+
this._loaded = true;
|
|
2704
|
+
}, 200);
|
|
2705
|
+
}
|
|
2706
|
+
render() {
|
|
2707
|
+
return (index.h(index.Host, { class: "p-cropper" }, this._loaded && (index.h("cropper-canvas", { class: "h-[17.5rem] w-full border-0 border-b border-solid border-mystic-medium bg-white", onAction: () => this._onAction() }, index.h("cropper-image", { src: this.value, alt: "Picture", ref: (ref) => this._setImageRef(ref), scalable: true, crossorigin: "anonymous" }), index.h("cropper-shade", { class: clsx({
|
|
2708
|
+
'rounded-round': this.variant === 'user',
|
|
2709
|
+
rounded: this.variant === 'company',
|
|
2710
|
+
}), "theme-color": "rgba(255, 255, 255, 0.5)", hidden: true }), index.h("cropper-selection", { "initial-coverage": "0.7", "aspect-ratio": "1", ref: (ref) => (this._selectionRef = ref) }), index.h("cropper-handle", { action: "move", plain: true }))), index.h("div", { class: "flex w-full items-center gap-2 px-14 text-storm-vague tablet:max-w-xs tablet:px-1" }, index.h("p-icon", { variant: "minus" }), index.h("input", { class: "p-input w-full", type: "range", min: "0", max: "100", value: this._currentScale, onInput: (ev) => this._onInput(ev.target.value), step: "0.5" }), index.h("p-icon", { variant: "plus" }))));
|
|
2711
|
+
}
|
|
2712
|
+
_setImageRef(ref) {
|
|
2713
|
+
if (this._imageRef) {
|
|
2714
|
+
return;
|
|
2715
|
+
}
|
|
2716
|
+
this._imageRef = ref;
|
|
2717
|
+
this._imageRef.$ready((image) => setTimeout(() => this._setInitialState(image), 200));
|
|
2718
|
+
}
|
|
2719
|
+
_setInitialState(image) {
|
|
2720
|
+
let scale;
|
|
2721
|
+
if (image.naturalWidth > image.naturalHeight) {
|
|
2722
|
+
// set scale by height
|
|
2723
|
+
scale = 200 / image.naturalHeight;
|
|
2724
|
+
}
|
|
2725
|
+
if (image.naturalHeight >= image.naturalWidth) {
|
|
2726
|
+
// set scale by width
|
|
2727
|
+
scale = 200 / image.naturalWidth;
|
|
2728
|
+
}
|
|
2729
|
+
const current = this._imageRef.$getTransform();
|
|
2730
|
+
this._minScale = scale;
|
|
2731
|
+
this._maxScale = current[0];
|
|
2732
|
+
this._selectionRef.$resize('nw-resize', 200, 200, 1);
|
|
2733
|
+
this._selectionRef.$center();
|
|
2734
|
+
this._setScale(scale);
|
|
2735
|
+
this._toCanvas();
|
|
2736
|
+
}
|
|
2737
|
+
_onAction() {
|
|
2738
|
+
this._setCurrentScale();
|
|
2739
|
+
this._toCanvas();
|
|
2740
|
+
}
|
|
2741
|
+
_setScale(scale, setCenter = true, setCurrentScale = true) {
|
|
2742
|
+
const transform = this._imageRef.$getTransform();
|
|
2743
|
+
this._imageRef.$setTransform([
|
|
2744
|
+
scale,
|
|
2745
|
+
0,
|
|
2746
|
+
0,
|
|
2747
|
+
scale,
|
|
2748
|
+
transform[4],
|
|
2749
|
+
transform[5],
|
|
2750
|
+
]);
|
|
2751
|
+
if (setCenter) {
|
|
2752
|
+
this._imageRef.$center();
|
|
2753
|
+
}
|
|
2754
|
+
if (setCurrentScale) {
|
|
2755
|
+
this._setCurrentScale();
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
_setCurrentScale() {
|
|
2759
|
+
const transform = this._imageRef.$getTransform();
|
|
2760
|
+
const current = transform[0];
|
|
2761
|
+
if (current < this._minScale) {
|
|
2762
|
+
this._setScale(this._minScale, true, false);
|
|
2763
|
+
this._currentScale = 0;
|
|
2764
|
+
return;
|
|
2765
|
+
}
|
|
2766
|
+
if (current > this._maxScale) {
|
|
2767
|
+
this._setScale(this._maxScale, true, false);
|
|
2768
|
+
this._currentScale = 100;
|
|
2769
|
+
return;
|
|
2770
|
+
}
|
|
2771
|
+
if (current === this._minScale) {
|
|
2772
|
+
this._currentScale = 0;
|
|
2773
|
+
return;
|
|
2774
|
+
}
|
|
2775
|
+
if (current === this._maxScale) {
|
|
2776
|
+
this._currentScale = 100;
|
|
2777
|
+
return;
|
|
2778
|
+
}
|
|
2779
|
+
const minMaxDiff = this._maxScale - this._minScale;
|
|
2780
|
+
const currentDiff = current - this._minScale;
|
|
2781
|
+
const percentage = (currentDiff / minMaxDiff) * 100;
|
|
2782
|
+
this._currentScale = percentage;
|
|
2783
|
+
}
|
|
2784
|
+
_onInput(value) {
|
|
2785
|
+
const transform = this._imageRef.$getTransform();
|
|
2786
|
+
const current = transform[0];
|
|
2787
|
+
const minMaxDiff = this._maxScale - this._minScale;
|
|
2788
|
+
const toSet = minMaxDiff * (value / 100);
|
|
2789
|
+
if (toSet === current) {
|
|
2790
|
+
return;
|
|
2791
|
+
}
|
|
2792
|
+
this._setScale(toSet + this._minScale, false, false);
|
|
2793
|
+
this._currentScale = value;
|
|
2794
|
+
this._toCanvas();
|
|
2795
|
+
}
|
|
2796
|
+
_toCanvas() {
|
|
2797
|
+
if (this._toCanvasDebounceTimeout) {
|
|
2798
|
+
clearTimeout(this._toCanvasDebounceTimeout);
|
|
2799
|
+
}
|
|
2800
|
+
this._toCanvasDebounceTimeout = setTimeout(async () => {
|
|
2801
|
+
const canvas = await this._selectionRef.$toCanvas();
|
|
2802
|
+
if (this.returnType === 'canvas') {
|
|
2803
|
+
this.valueChange.emit(canvas);
|
|
2804
|
+
return;
|
|
2805
|
+
}
|
|
2806
|
+
const data = canvas.toDataURL('image/png');
|
|
2807
|
+
this.valueChange.emit(data);
|
|
2808
|
+
}, 200);
|
|
2809
|
+
}
|
|
2810
|
+
};
|
|
2811
|
+
Cropper.style = cropperComponentCss;
|
|
2812
|
+
|
|
2813
|
+
exports.p_cropper = Cropper;
|
|
2814
|
+
|
|
2815
|
+
//# sourceMappingURL=p-cropper.cjs.entry.js.map
|