@windwalker-io/unicorn-next 0.1.2 → 0.1.4
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/.editorconfig +18 -18
- package/.gulp.json +7 -7
- package/bin/release.mjs +47 -47
- package/dist/chunks/button-radio.js.map +1 -1
- package/dist/chunks/checkboxes-multi-select.js.map +1 -1
- package/dist/chunks/field-cascade-select.js.map +1 -1
- package/dist/chunks/field-file-drag.js +34 -31
- package/dist/chunks/field-file-drag.js.map +1 -1
- package/dist/chunks/field-flatpickr.js +10 -3
- package/dist/chunks/field-flatpickr.js.map +1 -1
- package/dist/chunks/field-modal-select.js +6 -2
- package/dist/chunks/field-modal-select.js.map +1 -1
- package/dist/chunks/field-modal-tree.js +10 -4
- package/dist/chunks/field-modal-tree.js.map +1 -1
- package/dist/chunks/field-multi-uploader.js +10 -3
- package/dist/chunks/field-multi-uploader.js.map +1 -1
- package/dist/chunks/field-repeatable.js.map +1 -1
- package/dist/chunks/field-single-image-drag.js +12 -7
- package/dist/chunks/field-single-image-drag.js.map +1 -1
- package/dist/chunks/form.js.map +1 -1
- package/dist/chunks/grid.js.map +1 -1
- package/dist/chunks/http-client.js.map +1 -1
- package/dist/chunks/iframe-modal.js +27 -24
- package/dist/chunks/iframe-modal.js.map +1 -1
- package/dist/chunks/keep-tab.js.map +1 -1
- package/dist/chunks/legacy.js.map +1 -1
- package/dist/chunks/list-dependent.js.map +1 -1
- package/dist/chunks/s3-multipart-uploader.js.map +1 -1
- package/dist/chunks/s3-uploader.js.map +1 -1
- package/dist/chunks/show-on.js.map +1 -1
- package/dist/chunks/tinymce.js.map +1 -1
- package/dist/chunks/ui-bootstrap5.js.map +1 -1
- package/dist/chunks/unicorn.js +10 -6
- package/dist/chunks/unicorn.js.map +1 -1
- package/dist/chunks/validation.js.map +1 -1
- package/dist/index.d.ts +47 -26
- package/dist/unicorn.js +3 -3
- package/fusionfile.mjs +155 -155
- package/package.json +103 -103
- package/scss/bootstrap/multi-level-menu.scss +121 -121
- package/scss/editor.scss +116 -116
- package/scss/field/file-drag.scss +102 -102
- package/scss/field/single-image-drag.scss +88 -88
- package/scss/field/vue-drag-uploader.scss +160 -160
- package/scss/switcher.scss +156 -156
- package/src/app.ts +128 -128
- package/src/bootstrap/button-radio.ts +208 -208
- package/src/bootstrap/keep-tab.ts +155 -155
- package/src/composable/index.ts +21 -21
- package/src/composable/useCheckboxesMultiSelect.ts +22 -22
- package/src/composable/useFieldCascadeSelect.ts +9 -9
- package/src/composable/useFieldFileDrag.ts +9 -9
- package/src/composable/useFieldFlatpickr.ts +3 -3
- package/src/composable/useFieldModalSelect.ts +6 -6
- package/src/composable/useFieldModalTree.ts +3 -3
- package/src/composable/useFieldMultiUploader.ts +9 -3
- package/src/composable/useFieldRepeatable.ts +9 -9
- package/src/composable/useFieldSingleImageDrag.ts +9 -5
- package/src/composable/useForm.ts +43 -43
- package/src/composable/useGrid.ts +57 -57
- package/src/composable/useHttp.ts +9 -9
- package/src/composable/useIframeModal.ts +10 -10
- package/src/composable/useListDependent.ts +26 -26
- package/src/composable/useQueue.ts +13 -13
- package/src/composable/useS3Uploader.ts +32 -32
- package/src/composable/useShowOn.ts +9 -9
- package/src/composable/useStack.ts +13 -13
- package/src/composable/useTinymce.ts +29 -29
- package/src/composable/useTomSelect.ts +72 -72
- package/src/composable/useUIBootstrap5.ts +48 -48
- package/src/composable/useUniDirective.ts +32 -32
- package/src/composable/useValidation.ts +39 -39
- package/src/data.ts +34 -34
- package/src/events.ts +82 -82
- package/src/legacy/legacy.ts +190 -190
- package/src/legacy/loader.ts +125 -125
- package/src/module/checkboxes-multi-select.ts +54 -54
- package/src/module/field-cascade-select.ts +292 -292
- package/src/module/field-file-drag.ts +295 -292
- package/src/module/field-flatpickr.ts +130 -127
- package/src/module/field-modal-select.ts +179 -174
- package/src/module/field-modal-tree.ts +31 -27
- package/src/module/field-multi-uploader.ts +368 -361
- package/src/module/field-repeatable.ts +202 -202
- package/src/module/field-single-image-drag.ts +475 -468
- package/src/module/form.ts +223 -223
- package/src/module/grid.ts +465 -465
- package/src/module/http-client.ts +248 -248
- package/src/module/iframe-modal.ts +170 -167
- package/src/module/list-dependent.ts +321 -321
- package/src/module/s3-multipart-uploader.ts +300 -300
- package/src/module/s3-uploader.ts +234 -234
- package/src/module/show-on.ts +175 -175
- package/src/module/tinymce.ts +276 -276
- package/src/module/ui-bootstrap5.ts +116 -116
- package/src/module/validation.ts +1026 -1026
- package/src/plugin/index.ts +1 -1
- package/src/plugin/php-adapter.ts +72 -72
- package/src/polyfill/form-request-submit.ts +31 -31
- package/src/polyfill/index.ts +9 -9
- package/src/service/animate.ts +58 -58
- package/src/service/crypto.ts +27 -27
- package/src/service/dom-watcher.ts +62 -62
- package/src/service/dom.ts +265 -265
- package/src/service/helper.ts +48 -48
- package/src/service/index.ts +10 -10
- package/src/service/lang.ts +122 -122
- package/src/service/loader.ts +152 -152
- package/src/service/router.ts +118 -118
- package/src/service/ui.ts +525 -525
- package/src/service/uri.ts +106 -106
- package/src/types/base.ts +9 -9
- package/src/types/index.ts +4 -4
- package/src/types/modal-tree.ts +12 -12
- package/src/types/plugin.ts +6 -6
- package/src/types/shims.d.ts +18 -18
- package/src/types/ui.ts +6 -6
- package/src/unicorn.ts +79 -79
- package/src/utilities/arr.ts +25 -25
- package/src/utilities/base.ts +9 -9
- package/src/utilities/data.ts +48 -48
- package/src/utilities/index.ts +5 -5
- package/src/utilities/tree.ts +20 -20
- package/src/vue/components/ModalTree/ModalTreeApp.vue +175 -175
- package/src/vue/components/ModalTree/TreeItem.vue +262 -262
- package/src/vue/components/ModalTree/TreeModal.vue +225 -225
- package/tests/test.js +4 -4
- package/tsconfig.js.json +25 -25
- package/tsconfig.json +17 -17
- package/vite.assets.config.ts +61 -61
- package/vite.config.test.ts +36 -36
- package/vite.config.ts +112 -112
|
@@ -1,468 +1,475 @@
|
|
|
1
|
-
|
|
2
|
-
import { useHttpClient } from '../composable';
|
|
3
|
-
import { __, injectCssToDocument, selectAll, simpleAlert } from '../service';
|
|
4
|
-
import { mergeDeep } from '../utilities';
|
|
5
|
-
import css from '../../scss/field/single-image-drag.scss?inline';
|
|
6
|
-
import { Modal } from 'bootstrap';
|
|
7
|
-
import type Cropper from 'cropperjs';
|
|
8
|
-
import { ApiReturn } from './http-client';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
'image/
|
|
28
|
-
'image/
|
|
29
|
-
'image/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
private
|
|
47
|
-
private
|
|
48
|
-
private
|
|
49
|
-
private
|
|
50
|
-
private
|
|
51
|
-
private
|
|
52
|
-
private
|
|
53
|
-
private
|
|
54
|
-
private
|
|
55
|
-
private
|
|
56
|
-
private
|
|
57
|
-
private
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
{}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
this.
|
|
73
|
-
this.
|
|
74
|
-
this.
|
|
75
|
-
this.
|
|
76
|
-
this.
|
|
77
|
-
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
this.
|
|
81
|
-
this.
|
|
82
|
-
this.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
this.
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
input.
|
|
134
|
-
input.
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
this.valueInput.value =
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
this.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
allow = allow ||
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
this.
|
|
393
|
-
|
|
394
|
-
this.fileInput.
|
|
395
|
-
|
|
396
|
-
this.
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
this.
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
//
|
|
435
|
-
//
|
|
436
|
-
//
|
|
437
|
-
// ]
|
|
438
|
-
//
|
|
439
|
-
//
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
]
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
}
|
|
1
|
+
|
|
2
|
+
import { useHttpClient } from '../composable';
|
|
3
|
+
import { __, injectCssToDocument, selectAll, simpleAlert } from '../service';
|
|
4
|
+
import { mergeDeep } from '../utilities';
|
|
5
|
+
import css from '../../scss/field/single-image-drag.scss?inline';
|
|
6
|
+
import { Modal } from 'bootstrap';
|
|
7
|
+
import type Cropper from 'cropperjs';
|
|
8
|
+
import { ApiReturn } from './http-client';
|
|
9
|
+
|
|
10
|
+
export interface SingleImageDragOptions {
|
|
11
|
+
accept: string | string[];
|
|
12
|
+
ajax_url?: string;
|
|
13
|
+
crop: boolean;
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
max_width?: number;
|
|
17
|
+
min_width?: number;
|
|
18
|
+
max_height?: number;
|
|
19
|
+
min_height?: number;
|
|
20
|
+
modalTarget: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const defaultOptions: Partial<SingleImageDragOptions> = {
|
|
24
|
+
accept: [
|
|
25
|
+
'image/jpeg',
|
|
26
|
+
'image/png',
|
|
27
|
+
'image/webp',
|
|
28
|
+
'image/avif',
|
|
29
|
+
'image/gif',
|
|
30
|
+
],
|
|
31
|
+
crop: false,
|
|
32
|
+
width: 800,
|
|
33
|
+
height: 800,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export class SingleImageDragElement extends HTMLElement {
|
|
37
|
+
static is = 'uni-sid';
|
|
38
|
+
|
|
39
|
+
currentImage = '';
|
|
40
|
+
currentFile: File | undefined = undefined;
|
|
41
|
+
lastZoom = 0;
|
|
42
|
+
valueBackup = '';
|
|
43
|
+
|
|
44
|
+
private options!: SingleImageDragOptions;
|
|
45
|
+
private valueInput!: HTMLInputElement;
|
|
46
|
+
private fileInput!: HTMLInputElement;
|
|
47
|
+
private selectButton!: HTMLButtonElement;
|
|
48
|
+
private pasteButton!: HTMLButtonElement;
|
|
49
|
+
private dragarea!: HTMLDivElement;
|
|
50
|
+
private previewImage!: HTMLImageElement;
|
|
51
|
+
private removeCheckbox!: HTMLInputElement;
|
|
52
|
+
private modalElement!: HTMLDivElement;
|
|
53
|
+
private modal!: Modal;
|
|
54
|
+
private cropContainer!: HTMLDivElement;
|
|
55
|
+
private savebutton!: HTMLButtonElement;
|
|
56
|
+
private modalToolbarButtons!: NodeListOf<HTMLButtonElement>;
|
|
57
|
+
private cropper!: Cropper;
|
|
58
|
+
|
|
59
|
+
constructor() {
|
|
60
|
+
super();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
connectedCallback() {
|
|
64
|
+
this.options = mergeDeep(
|
|
65
|
+
{},
|
|
66
|
+
defaultOptions,
|
|
67
|
+
JSON.parse(this.getAttribute('options') || '{}')
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
this.valueInput = this.querySelector<HTMLInputElement>('[data-field-input]')!;
|
|
71
|
+
this.fileInput = this.querySelector<HTMLInputElement>('[data-sid=file]')!;
|
|
72
|
+
this.selectButton = this.querySelector<HTMLButtonElement>('[data-sid=select]')!;
|
|
73
|
+
this.pasteButton = this.querySelector<HTMLButtonElement>('[data-sid=paste]')!;
|
|
74
|
+
this.dragarea = this.querySelector<HTMLDivElement>('[data-sid=dragarea]')!;
|
|
75
|
+
this.previewImage = this.querySelector<HTMLImageElement>('[data-sid=preview]')!;
|
|
76
|
+
this.removeCheckbox = this.querySelector<HTMLInputElement>('[data-sid=remove]')!;
|
|
77
|
+
|
|
78
|
+
this.modalElement = document.querySelector<HTMLDivElement>(this.options.modalTarget)!;
|
|
79
|
+
this.modal = Modal.getOrCreateInstance(this.modalElement);
|
|
80
|
+
this.cropContainer = this.modalElement.querySelector<HTMLDivElement>('[data-sid="crop-container"]')!;
|
|
81
|
+
this.savebutton = this.modalElement.querySelector<HTMLButtonElement>('[data-sid=save-button]')!;
|
|
82
|
+
this.modalToolbarButtons = this.modalElement.querySelectorAll<HTMLButtonElement>('[data-sid-toolbar]');
|
|
83
|
+
|
|
84
|
+
const modalShown = async () => {
|
|
85
|
+
const cropper = await this.getCropper();
|
|
86
|
+
cropper.replace(this.currentImage);
|
|
87
|
+
this.cropContainer.style.visibility = '';
|
|
88
|
+
this.currentImage = '';
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
this.modalElement.addEventListener('shown.bs.modal', modalShown.bind(this));
|
|
92
|
+
|
|
93
|
+
this.savebutton.addEventListener('click', () => {
|
|
94
|
+
this.saveCropped();
|
|
95
|
+
this.modal.hide();
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
this.bindEvents();
|
|
99
|
+
|
|
100
|
+
this.style.visibility = '';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
bindEvents() {
|
|
104
|
+
this.dragarea.addEventListener('dragover', (event) => {
|
|
105
|
+
event.stopPropagation();
|
|
106
|
+
event.preventDefault();
|
|
107
|
+
|
|
108
|
+
this.dragarea.classList.add('hover');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
this.dragarea.addEventListener('dragleave', (event) => {
|
|
112
|
+
event.stopPropagation();
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
|
|
115
|
+
this.dragarea.classList.remove('hover');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
this.dragarea.addEventListener('drop', (event) => {
|
|
119
|
+
event.stopPropagation();
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
|
|
122
|
+
this.dragarea.classList.remove('hover');
|
|
123
|
+
|
|
124
|
+
const files = (event.target as HTMLInputElement).files || event.dataTransfer?.files || [];
|
|
125
|
+
this.handleFileSelect(files[0]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// Select button
|
|
129
|
+
this.selectButton.addEventListener('click', () => {
|
|
130
|
+
const input = document.createElement('input');
|
|
131
|
+
input.setAttribute('type', 'file');
|
|
132
|
+
input.setAttribute('accept', this.getInputAccept());
|
|
133
|
+
input.style.display = 'none';
|
|
134
|
+
input.addEventListener('change', (e) => {
|
|
135
|
+
this.handleFileSelect(input.files![0]!);
|
|
136
|
+
|
|
137
|
+
input.remove();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
document.body.appendChild(input);
|
|
141
|
+
input.click();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
this.pasteButton.addEventListener('click', () => {
|
|
145
|
+
navigator.clipboard.read().then((items) => {
|
|
146
|
+
let types = items[0].types;
|
|
147
|
+
|
|
148
|
+
if (types.length === 0) {
|
|
149
|
+
this.alert('This browser unable to get clipboard data.');
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
types = types.slice().sort();
|
|
154
|
+
|
|
155
|
+
const type = types[0];
|
|
156
|
+
|
|
157
|
+
items[0].getType(type).then((blob) => {
|
|
158
|
+
this.handleFileSelect(new File([ blob ], 'image.png', { type }));
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// Delete
|
|
164
|
+
this.removeCheckbox?.addEventListener('click', () => {
|
|
165
|
+
if (this.removeCheckbox.checked) {
|
|
166
|
+
this.valueBackup = this.valueInput.value;
|
|
167
|
+
this.valueInput.value = '';
|
|
168
|
+
} else {
|
|
169
|
+
this.valueInput.value = this.valueBackup;
|
|
170
|
+
this.valueBackup = '';
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Zoom slider
|
|
175
|
+
selectAll(this.modalToolbarButtons, (button) => {
|
|
176
|
+
button.addEventListener('click', (event) => {
|
|
177
|
+
this.toolbarClicked(button, event);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
getInputAccept() {
|
|
183
|
+
let accept = this.options.accept;
|
|
184
|
+
|
|
185
|
+
if (Array.isArray(accept)) {
|
|
186
|
+
accept = accept.join(',');
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return accept;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
handleFileSelect(file: File) {
|
|
193
|
+
if (!this.checkFile(file)) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (this.options.crop) {
|
|
198
|
+
const reader = new FileReader();
|
|
199
|
+
|
|
200
|
+
reader.addEventListener('load', (event) => {
|
|
201
|
+
this.cropContainer.style.visibility = 'hidden';
|
|
202
|
+
this.currentImage = event.target!.result as string;
|
|
203
|
+
this.currentFile = file;
|
|
204
|
+
|
|
205
|
+
// After modal shown, cropper will auto load.
|
|
206
|
+
this.modal.show();
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
reader.readAsDataURL(file);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this.saveImage(file);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async saveCropped() {
|
|
217
|
+
const Cropper = await this.getCropper();
|
|
218
|
+
|
|
219
|
+
Cropper.getCroppedCanvas({
|
|
220
|
+
width: this.options.width,
|
|
221
|
+
height: this.options.height,
|
|
222
|
+
imageSmoothingEnabled: true
|
|
223
|
+
})
|
|
224
|
+
.toBlob((blob) => {
|
|
225
|
+
const file = new File([ blob! ], this.currentFile!.name, { type: 'image/png' });
|
|
226
|
+
this.saveImage(file);
|
|
227
|
+
}, 'image/png');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async getCropper() {
|
|
231
|
+
if (this.cropper) {
|
|
232
|
+
return this.cropper;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const Cropper = await loadCropper();
|
|
236
|
+
|
|
237
|
+
return this.cropper = new Cropper(this.cropContainer.querySelector('img')!, {
|
|
238
|
+
aspectRatio: this.options.width / this.options.height,
|
|
239
|
+
autoCropArea: 1,
|
|
240
|
+
viewMode: 1,
|
|
241
|
+
dragMode: 'move',
|
|
242
|
+
cropBoxMovable: false,
|
|
243
|
+
cropBoxResizable: false,
|
|
244
|
+
ready: (e) => {
|
|
245
|
+
//
|
|
246
|
+
},
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async toolbarClicked(button: HTMLButtonElement, event: MouseEvent) {
|
|
251
|
+
const cropper = await this.getCropper();
|
|
252
|
+
|
|
253
|
+
const data = cropper.getData();
|
|
254
|
+
|
|
255
|
+
switch (button.dataset.sidToolbar) {
|
|
256
|
+
case 'zoom-in':
|
|
257
|
+
cropper.zoom(0.1);
|
|
258
|
+
break;
|
|
259
|
+
|
|
260
|
+
case 'zoom-out':
|
|
261
|
+
cropper.zoom(-0.1);
|
|
262
|
+
break;
|
|
263
|
+
|
|
264
|
+
case 'rotate-left':
|
|
265
|
+
cropper.rotate(-90);
|
|
266
|
+
break;
|
|
267
|
+
|
|
268
|
+
case 'rotate-right':
|
|
269
|
+
cropper.rotate(90);
|
|
270
|
+
break;
|
|
271
|
+
|
|
272
|
+
case 'scale-x':
|
|
273
|
+
cropper.scaleX(-data.scaleX);
|
|
274
|
+
break;
|
|
275
|
+
|
|
276
|
+
case 'scale-y':
|
|
277
|
+
cropper.scaleY(-data.scaleY);
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
checkFile(file: File): boolean {
|
|
283
|
+
let accept = this.options.accept;
|
|
284
|
+
|
|
285
|
+
if (typeof accept === 'string') {
|
|
286
|
+
accept = accept.split(',').map(v => v.trim());
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
if (!accept.length) {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
let allow = false;
|
|
294
|
+
|
|
295
|
+
for (const type of accept) {
|
|
296
|
+
if (type.indexOf('/') !== -1) {
|
|
297
|
+
allow = allow || this.compareMimeType(type, file.type);
|
|
298
|
+
} else {
|
|
299
|
+
allow = allow || type.toLowerCase() === getFileExtension(file)?.toLowerCase();
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (allow) {
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
this.alert(
|
|
308
|
+
__('unicorn.field.sid.message.invalid.image.title'),
|
|
309
|
+
__('unicorn.field.sid.message.invalid.image.desc'),
|
|
310
|
+
'error'
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
return false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
compareMimeType(accept: string, mime: string) {
|
|
317
|
+
const accept2 = accept.split('/');
|
|
318
|
+
const mime2 = mime.split('/');
|
|
319
|
+
|
|
320
|
+
if (accept2[1] === '*') {
|
|
321
|
+
return accept2[0] === mime2[0];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return accept === mime;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
checkSize(image: HTMLImageElement): boolean {
|
|
328
|
+
try {
|
|
329
|
+
if (this.options.max_width && this.options.max_width < image.width) {
|
|
330
|
+
throw new Error(__('unicorn.field.sid.message.invalid.size.max.width', this.options.max_width));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
if (this.options.min_width && this.options.min_width > image.width) {
|
|
334
|
+
throw new Error(__('unicorn.field.sid.message.invalid.size.min.width', this.options.min_width));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
if (this.options.max_height && this.options.max_height < image.height) {
|
|
338
|
+
throw new Error(__('unicorn.field.sid.message.invalid.size.max.height', this.options.max_height));
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (this.options.min_height && this.options.min_height > image.height) {
|
|
342
|
+
throw new Error(__('unicorn.field.sid.message.invalid.size.min.height', this.options.min_height));
|
|
343
|
+
}
|
|
344
|
+
} catch (e) {
|
|
345
|
+
this.alert(
|
|
346
|
+
__('unicorn.field.sid.message.invalid.size.title'),
|
|
347
|
+
(e as Error).message,
|
|
348
|
+
'error'
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return true;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
alert(title: string, text = '', type = 'info') {
|
|
358
|
+
return simpleAlert(title, text, type);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async saveImage(file: File) {
|
|
362
|
+
if (this.options.ajax_url) {
|
|
363
|
+
const loading = this.querySelector<HTMLImageElement>('[data-sid=file-uploading]')!;
|
|
364
|
+
|
|
365
|
+
this.previewImage.src = '';
|
|
366
|
+
this.previewImage.style.display = 'none';
|
|
367
|
+
loading.style.display = 'flex';
|
|
368
|
+
|
|
369
|
+
try {
|
|
370
|
+
const res = await this.uploadImage(file);
|
|
371
|
+
|
|
372
|
+
this.storeValue(res.data.data.url, res.data.data.url);
|
|
373
|
+
} catch (e) {
|
|
374
|
+
console.error(e);
|
|
375
|
+
simpleAlert((e as Error).message);
|
|
376
|
+
return;
|
|
377
|
+
} finally {
|
|
378
|
+
loading.style.display = 'none';
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// @see https://stackoverflow.com/a/47172409
|
|
385
|
+
// @see https://stackoverflow.com/a/47522812
|
|
386
|
+
const dt = new DataTransfer();
|
|
387
|
+
dt.items.add(file);
|
|
388
|
+
|
|
389
|
+
// No required for value input to remove validation message
|
|
390
|
+
this.valueInput.required = false;
|
|
391
|
+
|
|
392
|
+
this.fileInput.files = dt.files;
|
|
393
|
+
this.fileInput.dispatchEvent(new CustomEvent('change', { bubbles: true }));
|
|
394
|
+
this.fileInput.dispatchEvent(new CustomEvent('input', { bubbles: true }));
|
|
395
|
+
|
|
396
|
+
this.storeValue('', URL.createObjectURL(file));
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async uploadImage(file: File) {
|
|
400
|
+
const formData = new FormData();
|
|
401
|
+
formData.append('file', file);
|
|
402
|
+
|
|
403
|
+
const { post } = await useHttpClient();
|
|
404
|
+
|
|
405
|
+
return post<ApiReturn<{ url: string; }>>(this.options.ajax_url!, formData, {
|
|
406
|
+
headers: {
|
|
407
|
+
'Content-Type': 'multipart/form-data'
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
storeValue(url: string, preview: string) {
|
|
413
|
+
this.previewImage.src = preview;
|
|
414
|
+
this.previewImage.style.display = 'inline-block';
|
|
415
|
+
|
|
416
|
+
// Make delete box unchecked
|
|
417
|
+
if (this.removeCheckbox) {
|
|
418
|
+
this.removeCheckbox.checked = false;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (url) {
|
|
422
|
+
this.valueInput.value = url;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Trigger change
|
|
426
|
+
this.previewImage.dispatchEvent(new CustomEvent('change', { bubbles: true }));
|
|
427
|
+
this.valueInput.dispatchEvent(new CustomEvent('change', { bubbles: true }));
|
|
428
|
+
this.valueInput.dispatchEvent(new CustomEvent('input', { bubbles: true }));
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Promise.all([
|
|
433
|
+
// import('@cropperjs/cropper.min.js'),
|
|
434
|
+
// import('@cropperjs/cropper.css'),
|
|
435
|
+
// ])
|
|
436
|
+
// .then((service) => {
|
|
437
|
+
// const styleSheet = service[1].default;
|
|
438
|
+
// document.adoptedStyleSheets = [...document.adoptedStyleSheets, styleSheet];
|
|
439
|
+
// });
|
|
440
|
+
|
|
441
|
+
function getFileExtension(file: File): string | undefined {
|
|
442
|
+
const parts = file.name.split('.');
|
|
443
|
+
if (parts.length > 1) {
|
|
444
|
+
return parts.pop();
|
|
445
|
+
}
|
|
446
|
+
return undefined;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
let loadingCropper: Promise<any>;
|
|
450
|
+
|
|
451
|
+
async function loadCropper(): Promise<typeof Cropper> {
|
|
452
|
+
loadingCropper ??= Promise.all([
|
|
453
|
+
import('cropperjs'),
|
|
454
|
+
import('cropperjs/dist/cropper.min.css?inline').then(({ default: css }) => {
|
|
455
|
+
injectCssToDocument(css);
|
|
456
|
+
})
|
|
457
|
+
]);
|
|
458
|
+
|
|
459
|
+
return (await loadingCropper)[0];
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async function init() {
|
|
463
|
+
injectCssToDocument(css);
|
|
464
|
+
|
|
465
|
+
customElements.define(SingleImageDragElement.is, SingleImageDragElement);
|
|
466
|
+
|
|
467
|
+
await loadCropper();
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
export const ready = init();
|
|
471
|
+
|
|
472
|
+
export interface SingleImageDragModule {
|
|
473
|
+
SingleImageDragElement: typeof SingleImageDragElement;
|
|
474
|
+
ready: typeof ready;
|
|
475
|
+
}
|