@windwalker-io/unicorn-next 0.1.19 → 0.1.21

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.
Files changed (107) hide show
  1. package/dist/chunks/_arrayPush.js +325 -108
  2. package/dist/chunks/_arrayPush.js.map +1 -1
  3. package/dist/chunks/_baseRest.js +155 -60
  4. package/dist/chunks/_baseRest.js.map +1 -1
  5. package/dist/chunks/_baseUnary.js +463 -0
  6. package/dist/chunks/_baseUnary.js.map +1 -0
  7. package/dist/chunks/_getPrototype.js +292 -100
  8. package/dist/chunks/_getPrototype.js.map +1 -1
  9. package/dist/chunks/alert-adapter.js +29 -0
  10. package/dist/chunks/alert-adapter.js.map +1 -0
  11. package/dist/chunks/alert.js +21 -0
  12. package/dist/chunks/alert.js.map +1 -0
  13. package/dist/chunks/arr.js +24 -0
  14. package/dist/chunks/arr.js.map +1 -0
  15. package/dist/chunks/button-radio.js +127 -145
  16. package/dist/chunks/button-radio.js.map +1 -1
  17. package/dist/chunks/checkboxes-multi-select.js +44 -43
  18. package/dist/chunks/checkboxes-multi-select.js.map +1 -1
  19. package/dist/chunks/chunk.js +24 -0
  20. package/dist/chunks/cloneDeep.js +679 -212
  21. package/dist/chunks/cloneDeep.js.map +1 -1
  22. package/dist/chunks/cropper.min.js +6 -5
  23. package/dist/chunks/cropper.min.js.map +1 -1
  24. package/dist/chunks/crypto.js +26 -0
  25. package/dist/chunks/crypto.js.map +1 -0
  26. package/dist/chunks/data.js +49 -0
  27. package/dist/chunks/data.js.map +1 -0
  28. package/dist/chunks/dom.js +128 -0
  29. package/dist/chunks/dom.js.map +1 -0
  30. package/dist/chunks/events.js +270 -0
  31. package/dist/chunks/events.js.map +1 -0
  32. package/dist/chunks/field-cascade-select.js +207 -250
  33. package/dist/chunks/field-cascade-select.js.map +1 -1
  34. package/dist/chunks/field-file-drag.js +175 -209
  35. package/dist/chunks/field-file-drag.js.map +1 -1
  36. package/dist/chunks/field-flatpickr.js +94 -898
  37. package/dist/chunks/field-flatpickr.js.map +1 -1
  38. package/dist/chunks/field-modal-select.js +728 -467
  39. package/dist/chunks/field-modal-select.js.map +1 -1
  40. package/dist/chunks/field-modal-tree.js +771 -766
  41. package/dist/chunks/field-modal-tree.js.map +1 -1
  42. package/dist/chunks/field-multi-uploader.js +249 -256
  43. package/dist/chunks/field-multi-uploader.js.map +1 -1
  44. package/dist/chunks/field-repeatable.js +111 -127
  45. package/dist/chunks/field-repeatable.js.map +1 -1
  46. package/dist/chunks/field-single-image-drag.js +286 -338
  47. package/dist/chunks/field-single-image-drag.js.map +1 -1
  48. package/dist/chunks/form.js +146 -159
  49. package/dist/chunks/form.js.map +1 -1
  50. package/dist/chunks/grid.js +349 -418
  51. package/dist/chunks/grid.js.map +1 -1
  52. package/dist/chunks/helper.js +39 -0
  53. package/dist/chunks/helper.js.map +1 -0
  54. package/dist/chunks/http-client.js +221 -211
  55. package/dist/chunks/http-client.js.map +1 -1
  56. package/dist/chunks/iframe-modal.js +95 -115
  57. package/dist/chunks/iframe-modal.js.map +1 -1
  58. package/dist/chunks/keep-tab.js +92 -101
  59. package/dist/chunks/keep-tab.js.map +1 -1
  60. package/dist/chunks/lang.js +250 -0
  61. package/dist/chunks/lang.js.map +1 -0
  62. package/dist/chunks/legacy.js +197 -201
  63. package/dist/chunks/legacy.js.map +1 -1
  64. package/dist/chunks/list-dependent.js +195 -228
  65. package/dist/chunks/list-dependent.js.map +1 -1
  66. package/dist/chunks/loader.js +106 -0
  67. package/dist/chunks/loader.js.map +1 -0
  68. package/dist/chunks/monthSelect.js +251 -0
  69. package/dist/chunks/monthSelect.js.map +1 -0
  70. package/dist/chunks/router.js +111 -0
  71. package/dist/chunks/router.js.map +1 -0
  72. package/dist/chunks/s3-multipart-uploader.js +183 -210
  73. package/dist/chunks/s3-multipart-uploader.js.map +1 -1
  74. package/dist/chunks/s3-uploader.js +106 -128
  75. package/dist/chunks/s3-uploader.js.map +1 -1
  76. package/dist/chunks/show-on.js +358 -205
  77. package/dist/chunks/show-on.js.map +1 -1
  78. package/dist/chunks/timing.js +10 -0
  79. package/dist/chunks/timing.js.map +1 -0
  80. package/dist/chunks/tinymce.js +153 -203
  81. package/dist/chunks/tinymce.js.map +1 -1
  82. package/dist/chunks/ui-bootstrap5.js +58 -72
  83. package/dist/chunks/ui-bootstrap5.js.map +1 -1
  84. package/dist/chunks/ui.js +320 -0
  85. package/dist/chunks/ui.js.map +1 -0
  86. package/dist/chunks/unicorn.js.map +1 -1
  87. package/dist/chunks/useQueue.js +111 -0
  88. package/dist/chunks/useQueue.js.map +1 -0
  89. package/dist/chunks/useStack.js +76 -0
  90. package/dist/chunks/useStack.js.map +1 -0
  91. package/dist/chunks/validation.js +761 -853
  92. package/dist/chunks/validation.js.map +1 -1
  93. package/dist/editor.css +1 -1
  94. package/dist/index.d.ts +10 -8
  95. package/dist/multi-level-menu.css +1 -1
  96. package/dist/switcher.css +1 -1
  97. package/dist/unicorn.js +805 -130
  98. package/dist/unicorn.js.map +1 -1
  99. package/package.json +3 -3
  100. package/src/composable/useBsModalAlert.ts +29 -0
  101. package/src/composable/useHttp.ts +13 -1
  102. package/src/module/s3-uploader.ts +1 -1
  103. package/vite.config.ts +5 -1
  104. package/dist/chunks/_commonjsHelpers.js +0 -7
  105. package/dist/chunks/index.js +0 -314
  106. package/dist/chunks/isArguments.js +0 -146
  107. package/dist/chunks/unicorn.js +0 -2621
@@ -1,346 +1,294 @@
1
+ import { t as mergeDeep } from "./arr.js";
2
+ import { s as injectCssToDocument, u as selectAll } from "./dom.js";
3
+ import { r as simpleAlert } from "./alert.js";
4
+ import { t as __ } from "./lang.js";
5
+ import { useHttpClient } from "../unicorn.js";
1
6
  import { Modal } from "bootstrap";
2
- import { a6 as mergeDeep, w as selectAll, _ as __, G as simpleAlert, u as useHttpClient, ab as injectCssToDocument } from "./unicorn.js";
3
- const css = ".c-sid-default__left-col {\n width: 30%;\n margin-right: 15px;\n justify-content: center;\n}\n.c-sid-default__left-col img {\n max-height: 250px;\n}\n.c-sid-default__right-col {\n overflow: hidden;\n}\n.c-sid-default__dragarea {\n font-weight: bold;\n text-align: center;\n padding: 9% 0;\n color: #ccc;\n border: 2px dashed #ccc;\n border-radius: 7px;\n cursor: default;\n}\n.c-sid-default__dragarea.hover {\n color: #333;\n border-color: #333;\n background-color: #f9f9f9;\n}\n.c-sid-default__img-loader {\n display: flex;\n justify-content: center;\n align-items: center;\n width: 100%;\n height: 180px;\n}\n.c-sid-default__size-info {\n margin-top: 5px;\n font-size: 13px;\n}\n.c-sid-default__remove {\n margin-left: 5px;\n}\n.c-sid-default__modal .btn {\n position: relative;\n}\n\n.c-sid-modal .modal-body {\n position: relative;\n}\n.c-sid-modal__content {\n position: relative;\n z-index: 3;\n}\n.c-sid-modal__loading {\n position: absolute;\n left: 0;\n right: 0;\n top: 0;\n bottom: 0;\n display: flex;\n justify-content: center;\n align-items: center;\n z-index: 1;\n}\n.is-invalid[uni-field-validate] > uni-sid ~ [data-field-error] {\n display: block;\n}";
4
- const defaultOptions = {
5
- accept: [
6
- "image/jpeg",
7
- "image/png",
8
- "image/webp",
9
- "image/avif",
10
- "image/gif"
11
- ],
12
- crop: false,
13
- width: 800,
14
- height: 800
7
+ //#region scss/field/single-image-drag.scss?inline
8
+ var single_image_drag_default = ".c-sid-default__left-col {\n width: 30%;\n margin-right: 15px;\n justify-content: center;\n}\n.c-sid-default__left-col img {\n max-height: 250px;\n}\n.c-sid-default__right-col {\n overflow: hidden;\n}\n.c-sid-default__dragarea {\n font-weight: bold;\n text-align: center;\n padding: 9% 0;\n color: #ccc;\n border: 2px dashed #ccc;\n border-radius: 7px;\n cursor: default;\n}\n.c-sid-default__dragarea.hover {\n color: #333;\n border-color: #333;\n background-color: #f9f9f9;\n}\n.c-sid-default__img-loader {\n display: flex;\n justify-content: center;\n align-items: center;\n width: 100%;\n height: 180px;\n}\n.c-sid-default__size-info {\n margin-top: 5px;\n font-size: 13px;\n}\n.c-sid-default__remove {\n margin-left: 5px;\n}\n.c-sid-default__modal .btn {\n position: relative;\n}\n\n.c-sid-modal .modal-body {\n position: relative;\n}\n.c-sid-modal__content {\n position: relative;\n z-index: 3;\n}\n.c-sid-modal__loading {\n position: absolute;\n left: 0;\n right: 0;\n top: 0;\n bottom: 0;\n display: flex;\n justify-content: center;\n align-items: center;\n z-index: 1;\n}\n.is-invalid[uni-field-validate] > uni-sid ~ [data-field-error] {\n display: block;\n}";
9
+ //#endregion
10
+ //#region src/module/field-single-image-drag.ts
11
+ var defaultOptions = {
12
+ accept: [
13
+ "image/jpeg",
14
+ "image/png",
15
+ "image/webp",
16
+ "image/avif",
17
+ "image/gif"
18
+ ],
19
+ crop: false,
20
+ width: 800,
21
+ height: 800
22
+ };
23
+ var SingleImageDragElement = class extends HTMLElement {
24
+ static is = "uni-sid";
25
+ currentImage = "";
26
+ currentFile = void 0;
27
+ lastZoom = 0;
28
+ valueBackup = "";
29
+ options;
30
+ valueInput;
31
+ fileInput;
32
+ selectButton = null;
33
+ pasteButton = null;
34
+ dragarea = null;
35
+ previewImage;
36
+ removeCheckbox;
37
+ modalElement;
38
+ modal;
39
+ cropContainer;
40
+ savebutton;
41
+ modalToolbarButtons;
42
+ cropper;
43
+ constructor() {
44
+ super();
45
+ }
46
+ connectedCallback() {
47
+ this.options = mergeDeep({}, defaultOptions, JSON.parse(this.getAttribute("options") || "{}"));
48
+ this.valueInput = this.querySelector("[data-field-input]");
49
+ this.fileInput = this.querySelector("[data-sid=file]");
50
+ this.selectButton = this.querySelector("[data-sid=select]");
51
+ this.pasteButton = this.querySelector("[data-sid=paste]");
52
+ this.dragarea = this.querySelector("[data-sid=dragarea]");
53
+ this.previewImage = this.querySelector("[data-sid=preview]");
54
+ this.removeCheckbox = this.querySelector("[data-sid=remove]");
55
+ this.modalElement = document.querySelector(this.options.modalTarget);
56
+ this.modal = Modal.getOrCreateInstance(this.modalElement);
57
+ this.cropContainer = this.modalElement.querySelector("[data-sid=\"crop-container\"]");
58
+ this.savebutton = this.modalElement.querySelector("[data-sid=save-button]");
59
+ this.modalToolbarButtons = this.modalElement.querySelectorAll("[data-sid-toolbar]");
60
+ const modalShown = async () => {
61
+ (await this.getCropper()).replace(this.currentImage);
62
+ this.cropContainer.style.visibility = "";
63
+ this.currentImage = "";
64
+ };
65
+ this.modalElement.addEventListener("shown.bs.modal", modalShown.bind(this));
66
+ this.savebutton.addEventListener("click", () => {
67
+ this.saveCropped();
68
+ this.modal.hide();
69
+ });
70
+ this.bindEvents();
71
+ this.style.visibility = "";
72
+ }
73
+ bindEvents() {
74
+ if (this.dragarea) {
75
+ this.dragarea.addEventListener("dragover", (event) => {
76
+ event.stopPropagation();
77
+ event.preventDefault();
78
+ this.dragarea.classList.add("hover");
79
+ });
80
+ this.dragarea.addEventListener("dragleave", (event) => {
81
+ event.stopPropagation();
82
+ event.preventDefault();
83
+ this.dragarea.classList.remove("hover");
84
+ });
85
+ this.dragarea.addEventListener("drop", (event) => {
86
+ event.stopPropagation();
87
+ event.preventDefault();
88
+ this.dragarea.classList.remove("hover");
89
+ const files = event.target.files || event.dataTransfer?.files || [];
90
+ this.handleFileSelect(files[0]);
91
+ });
92
+ }
93
+ this.selectButton?.addEventListener("click", () => {
94
+ const input = document.createElement("input");
95
+ input.setAttribute("type", "file");
96
+ input.setAttribute("accept", this.getInputAccept());
97
+ input.style.display = "none";
98
+ input.addEventListener("change", (e) => {
99
+ this.handleFileSelect(input.files[0]);
100
+ input.remove();
101
+ });
102
+ document.body.appendChild(input);
103
+ input.click();
104
+ });
105
+ this.pasteButton?.addEventListener("click", () => {
106
+ navigator.clipboard.read().then((items) => {
107
+ let types = items[0].types;
108
+ if (types.length === 0) {
109
+ this.alert("This browser unable to get clipboard data.");
110
+ return;
111
+ }
112
+ types = types.slice().sort();
113
+ const type = types[0];
114
+ items[0].getType(type).then((blob) => {
115
+ this.handleFileSelect(new File([blob], "image.png", { type }));
116
+ });
117
+ });
118
+ });
119
+ this.removeCheckbox?.addEventListener("click", () => {
120
+ if (this.removeCheckbox.checked) {
121
+ this.valueBackup = this.valueInput.value;
122
+ this.valueInput.value = "";
123
+ } else {
124
+ this.valueInput.value = this.valueBackup;
125
+ this.valueBackup = "";
126
+ }
127
+ });
128
+ selectAll(this.modalToolbarButtons, (button) => {
129
+ button.addEventListener("click", (event) => {
130
+ this.toolbarClicked(button, event);
131
+ });
132
+ });
133
+ }
134
+ getInputAccept() {
135
+ let accept = this.options.accept;
136
+ if (Array.isArray(accept)) accept = accept.join(",");
137
+ return accept;
138
+ }
139
+ handleFileSelect(file) {
140
+ if (!this.checkFile(file)) return;
141
+ if (this.options.crop) {
142
+ const reader = new FileReader();
143
+ reader.addEventListener("load", (event) => {
144
+ this.cropContainer.style.visibility = "hidden";
145
+ this.currentImage = event.target.result;
146
+ this.currentFile = file;
147
+ this.modal.show();
148
+ });
149
+ reader.readAsDataURL(file);
150
+ return;
151
+ }
152
+ this.saveImage(file);
153
+ }
154
+ async saveCropped() {
155
+ (await this.getCropper()).getCroppedCanvas({
156
+ width: this.options.width,
157
+ height: this.options.height,
158
+ imageSmoothingEnabled: true
159
+ }).toBlob((blob) => {
160
+ const file = new File([blob], this.currentFile.name, { type: "image/png" });
161
+ this.saveImage(file);
162
+ }, "image/png");
163
+ }
164
+ async getCropper() {
165
+ if (this.cropper) return this.cropper;
166
+ return this.cropper = new (await (loadCropper()))(this.cropContainer.querySelector("img"), {
167
+ aspectRatio: this.options.width / this.options.height,
168
+ autoCropArea: 1,
169
+ viewMode: 1,
170
+ dragMode: "move",
171
+ cropBoxMovable: false,
172
+ cropBoxResizable: false,
173
+ ready: (e) => {}
174
+ });
175
+ }
176
+ async toolbarClicked(button, event) {
177
+ const cropper = await this.getCropper();
178
+ const data = cropper.getData();
179
+ switch (button.dataset.sidToolbar) {
180
+ case "zoom-in":
181
+ cropper.zoom(.1);
182
+ break;
183
+ case "zoom-out":
184
+ cropper.zoom(-.1);
185
+ break;
186
+ case "rotate-left":
187
+ cropper.rotate(-90);
188
+ break;
189
+ case "rotate-right":
190
+ cropper.rotate(90);
191
+ break;
192
+ case "scale-x":
193
+ cropper.scaleX(-data.scaleX);
194
+ break;
195
+ case "scale-y":
196
+ cropper.scaleY(-data.scaleY);
197
+ break;
198
+ }
199
+ }
200
+ checkFile(file) {
201
+ let accept = this.options.accept;
202
+ if (typeof accept === "string") accept = accept.split(",").map((v) => v.trim());
203
+ if (!accept.length) return true;
204
+ let allow = false;
205
+ for (const type of accept) if (type.indexOf("/") !== -1) allow = allow || this.compareMimeType(type, file.type);
206
+ else allow = allow || type.toLowerCase() === getFileExtension(file)?.toLowerCase();
207
+ if (allow) return true;
208
+ this.alert(__("unicorn.field.sid.message.invalid.image.title"), __("unicorn.field.sid.message.invalid.image.desc"), "error");
209
+ return false;
210
+ }
211
+ compareMimeType(accept, mime) {
212
+ const accept2 = accept.split("/");
213
+ const mime2 = mime.split("/");
214
+ if (accept2[1] === "*") return accept2[0] === mime2[0];
215
+ return accept === mime;
216
+ }
217
+ checkSize(image) {
218
+ try {
219
+ if (this.options.max_width && this.options.max_width < image.width) throw new Error(__("unicorn.field.sid.message.invalid.size.max.width", this.options.max_width));
220
+ if (this.options.min_width && this.options.min_width > image.width) throw new Error(__("unicorn.field.sid.message.invalid.size.min.width", this.options.min_width));
221
+ if (this.options.max_height && this.options.max_height < image.height) throw new Error(__("unicorn.field.sid.message.invalid.size.max.height", this.options.max_height));
222
+ if (this.options.min_height && this.options.min_height > image.height) throw new Error(__("unicorn.field.sid.message.invalid.size.min.height", this.options.min_height));
223
+ } catch (e) {
224
+ this.alert(__("unicorn.field.sid.message.invalid.size.title"), e.message, "error");
225
+ return false;
226
+ }
227
+ return true;
228
+ }
229
+ alert(title, text = "", type = "info") {
230
+ return simpleAlert(title, text, type);
231
+ }
232
+ async saveImage(file) {
233
+ if (this.options.ajax_url) {
234
+ const loading = this.querySelector("[data-sid=file-uploading]");
235
+ this.previewImage.src = "";
236
+ this.previewImage.style.display = "none";
237
+ loading.style.display = "flex";
238
+ try {
239
+ const res = await this.uploadImage(file);
240
+ this.storeValue(res.data.data.url, res.data.data.url);
241
+ } catch (e) {
242
+ console.error(e);
243
+ simpleAlert(e.message);
244
+ return;
245
+ } finally {
246
+ loading.style.display = "none";
247
+ }
248
+ return;
249
+ }
250
+ const dt = new DataTransfer();
251
+ dt.items.add(file);
252
+ this.valueInput.required = false;
253
+ this.fileInput.files = dt.files;
254
+ this.fileInput.dispatchEvent(new CustomEvent("change", { bubbles: true }));
255
+ this.fileInput.dispatchEvent(new CustomEvent("input", { bubbles: true }));
256
+ this.storeValue("", URL.createObjectURL(file));
257
+ }
258
+ async uploadImage(file) {
259
+ const formData = new FormData();
260
+ formData.append("file", file);
261
+ const { post } = await useHttpClient();
262
+ return post(this.options.ajax_url, formData, { headers: { "Content-Type": "multipart/form-data" } });
263
+ }
264
+ storeValue(url, preview) {
265
+ this.previewImage.src = preview;
266
+ this.previewImage.style.display = "inline-block";
267
+ if (this.removeCheckbox) this.removeCheckbox.checked = false;
268
+ if (url) this.valueInput.value = url;
269
+ this.previewImage.dispatchEvent(new CustomEvent("change", { bubbles: true }));
270
+ this.valueInput.dispatchEvent(new CustomEvent("change", { bubbles: true }));
271
+ this.valueInput.dispatchEvent(new CustomEvent("input", { bubbles: true }));
272
+ }
15
273
  };
16
- class SingleImageDragElement extends HTMLElement {
17
- static is = "uni-sid";
18
- currentImage = "";
19
- currentFile = void 0;
20
- lastZoom = 0;
21
- valueBackup = "";
22
- options;
23
- valueInput;
24
- fileInput;
25
- selectButton = null;
26
- pasteButton = null;
27
- dragarea = null;
28
- previewImage;
29
- removeCheckbox;
30
- modalElement;
31
- modal;
32
- cropContainer;
33
- savebutton;
34
- modalToolbarButtons;
35
- cropper;
36
- constructor() {
37
- super();
38
- }
39
- connectedCallback() {
40
- this.options = mergeDeep(
41
- {},
42
- defaultOptions,
43
- JSON.parse(this.getAttribute("options") || "{}")
44
- );
45
- this.valueInput = this.querySelector("[data-field-input]");
46
- this.fileInput = this.querySelector("[data-sid=file]");
47
- this.selectButton = this.querySelector("[data-sid=select]");
48
- this.pasteButton = this.querySelector("[data-sid=paste]");
49
- this.dragarea = this.querySelector("[data-sid=dragarea]");
50
- this.previewImage = this.querySelector("[data-sid=preview]");
51
- this.removeCheckbox = this.querySelector("[data-sid=remove]");
52
- this.modalElement = document.querySelector(this.options.modalTarget);
53
- this.modal = Modal.getOrCreateInstance(this.modalElement);
54
- this.cropContainer = this.modalElement.querySelector('[data-sid="crop-container"]');
55
- this.savebutton = this.modalElement.querySelector("[data-sid=save-button]");
56
- this.modalToolbarButtons = this.modalElement.querySelectorAll("[data-sid-toolbar]");
57
- const modalShown = async () => {
58
- const cropper = await this.getCropper();
59
- cropper.replace(this.currentImage);
60
- this.cropContainer.style.visibility = "";
61
- this.currentImage = "";
62
- };
63
- this.modalElement.addEventListener("shown.bs.modal", modalShown.bind(this));
64
- this.savebutton.addEventListener("click", () => {
65
- this.saveCropped();
66
- this.modal.hide();
67
- });
68
- this.bindEvents();
69
- this.style.visibility = "";
70
- }
71
- bindEvents() {
72
- if (this.dragarea) {
73
- this.dragarea.addEventListener("dragover", (event) => {
74
- event.stopPropagation();
75
- event.preventDefault();
76
- this.dragarea.classList.add("hover");
77
- });
78
- this.dragarea.addEventListener("dragleave", (event) => {
79
- event.stopPropagation();
80
- event.preventDefault();
81
- this.dragarea.classList.remove("hover");
82
- });
83
- this.dragarea.addEventListener("drop", (event) => {
84
- event.stopPropagation();
85
- event.preventDefault();
86
- this.dragarea.classList.remove("hover");
87
- const files = event.target.files || event.dataTransfer?.files || [];
88
- this.handleFileSelect(files[0]);
89
- });
90
- }
91
- this.selectButton?.addEventListener("click", () => {
92
- const input = document.createElement("input");
93
- input.setAttribute("type", "file");
94
- input.setAttribute("accept", this.getInputAccept());
95
- input.style.display = "none";
96
- input.addEventListener("change", (e) => {
97
- this.handleFileSelect(input.files[0]);
98
- input.remove();
99
- });
100
- document.body.appendChild(input);
101
- input.click();
102
- });
103
- this.pasteButton?.addEventListener("click", () => {
104
- navigator.clipboard.read().then((items) => {
105
- let types = items[0].types;
106
- if (types.length === 0) {
107
- this.alert("This browser unable to get clipboard data.");
108
- return;
109
- }
110
- types = types.slice().sort();
111
- const type = types[0];
112
- items[0].getType(type).then((blob) => {
113
- this.handleFileSelect(new File([blob], "image.png", { type }));
114
- });
115
- });
116
- });
117
- this.removeCheckbox?.addEventListener("click", () => {
118
- if (this.removeCheckbox.checked) {
119
- this.valueBackup = this.valueInput.value;
120
- this.valueInput.value = "";
121
- } else {
122
- this.valueInput.value = this.valueBackup;
123
- this.valueBackup = "";
124
- }
125
- });
126
- selectAll(this.modalToolbarButtons, (button) => {
127
- button.addEventListener("click", (event) => {
128
- this.toolbarClicked(button, event);
129
- });
130
- });
131
- }
132
- getInputAccept() {
133
- let accept = this.options.accept;
134
- if (Array.isArray(accept)) {
135
- accept = accept.join(",");
136
- }
137
- return accept;
138
- }
139
- handleFileSelect(file) {
140
- if (!this.checkFile(file)) {
141
- return;
142
- }
143
- if (this.options.crop) {
144
- const reader = new FileReader();
145
- reader.addEventListener("load", (event) => {
146
- this.cropContainer.style.visibility = "hidden";
147
- this.currentImage = event.target.result;
148
- this.currentFile = file;
149
- this.modal.show();
150
- });
151
- reader.readAsDataURL(file);
152
- return;
153
- }
154
- this.saveImage(file);
155
- }
156
- async saveCropped() {
157
- const Cropper = await this.getCropper();
158
- Cropper.getCroppedCanvas({
159
- width: this.options.width,
160
- height: this.options.height,
161
- imageSmoothingEnabled: true
162
- }).toBlob((blob) => {
163
- const file = new File([blob], this.currentFile.name, { type: "image/png" });
164
- this.saveImage(file);
165
- }, "image/png");
166
- }
167
- async getCropper() {
168
- if (this.cropper) {
169
- return this.cropper;
170
- }
171
- const Cropper = await loadCropper();
172
- return this.cropper = new Cropper(this.cropContainer.querySelector("img"), {
173
- aspectRatio: this.options.width / this.options.height,
174
- autoCropArea: 1,
175
- viewMode: 1,
176
- dragMode: "move",
177
- cropBoxMovable: false,
178
- cropBoxResizable: false,
179
- ready: (e) => {
180
- }
181
- });
182
- }
183
- async toolbarClicked(button, event) {
184
- const cropper = await this.getCropper();
185
- const data = cropper.getData();
186
- switch (button.dataset.sidToolbar) {
187
- case "zoom-in":
188
- cropper.zoom(0.1);
189
- break;
190
- case "zoom-out":
191
- cropper.zoom(-0.1);
192
- break;
193
- case "rotate-left":
194
- cropper.rotate(-90);
195
- break;
196
- case "rotate-right":
197
- cropper.rotate(90);
198
- break;
199
- case "scale-x":
200
- cropper.scaleX(-data.scaleX);
201
- break;
202
- case "scale-y":
203
- cropper.scaleY(-data.scaleY);
204
- break;
205
- }
206
- }
207
- checkFile(file) {
208
- let accept = this.options.accept;
209
- if (typeof accept === "string") {
210
- accept = accept.split(",").map((v) => v.trim());
211
- }
212
- if (!accept.length) {
213
- return true;
214
- }
215
- let allow = false;
216
- for (const type of accept) {
217
- if (type.indexOf("/") !== -1) {
218
- allow = allow || this.compareMimeType(type, file.type);
219
- } else {
220
- allow = allow || type.toLowerCase() === getFileExtension(file)?.toLowerCase();
221
- }
222
- }
223
- if (allow) {
224
- return true;
225
- }
226
- this.alert(
227
- __("unicorn.field.sid.message.invalid.image.title"),
228
- __("unicorn.field.sid.message.invalid.image.desc"),
229
- "error"
230
- );
231
- return false;
232
- }
233
- compareMimeType(accept, mime) {
234
- const accept2 = accept.split("/");
235
- const mime2 = mime.split("/");
236
- if (accept2[1] === "*") {
237
- return accept2[0] === mime2[0];
238
- }
239
- return accept === mime;
240
- }
241
- checkSize(image) {
242
- try {
243
- if (this.options.max_width && this.options.max_width < image.width) {
244
- throw new Error(__("unicorn.field.sid.message.invalid.size.max.width", this.options.max_width));
245
- }
246
- if (this.options.min_width && this.options.min_width > image.width) {
247
- throw new Error(__("unicorn.field.sid.message.invalid.size.min.width", this.options.min_width));
248
- }
249
- if (this.options.max_height && this.options.max_height < image.height) {
250
- throw new Error(__("unicorn.field.sid.message.invalid.size.max.height", this.options.max_height));
251
- }
252
- if (this.options.min_height && this.options.min_height > image.height) {
253
- throw new Error(__("unicorn.field.sid.message.invalid.size.min.height", this.options.min_height));
254
- }
255
- } catch (e) {
256
- this.alert(
257
- __("unicorn.field.sid.message.invalid.size.title"),
258
- e.message,
259
- "error"
260
- );
261
- return false;
262
- }
263
- return true;
264
- }
265
- alert(title, text = "", type = "info") {
266
- return simpleAlert(title, text, type);
267
- }
268
- async saveImage(file) {
269
- if (this.options.ajax_url) {
270
- const loading = this.querySelector("[data-sid=file-uploading]");
271
- this.previewImage.src = "";
272
- this.previewImage.style.display = "none";
273
- loading.style.display = "flex";
274
- try {
275
- const res = await this.uploadImage(file);
276
- this.storeValue(res.data.data.url, res.data.data.url);
277
- } catch (e) {
278
- console.error(e);
279
- simpleAlert(e.message);
280
- return;
281
- } finally {
282
- loading.style.display = "none";
283
- }
284
- return;
285
- }
286
- const dt = new DataTransfer();
287
- dt.items.add(file);
288
- this.valueInput.required = false;
289
- this.fileInput.files = dt.files;
290
- this.fileInput.dispatchEvent(new CustomEvent("change", { bubbles: true }));
291
- this.fileInput.dispatchEvent(new CustomEvent("input", { bubbles: true }));
292
- this.storeValue("", URL.createObjectURL(file));
293
- }
294
- async uploadImage(file) {
295
- const formData = new FormData();
296
- formData.append("file", file);
297
- const { post } = await useHttpClient();
298
- return post(this.options.ajax_url, formData, {
299
- headers: {
300
- "Content-Type": "multipart/form-data"
301
- }
302
- });
303
- }
304
- storeValue(url, preview) {
305
- this.previewImage.src = preview;
306
- this.previewImage.style.display = "inline-block";
307
- if (this.removeCheckbox) {
308
- this.removeCheckbox.checked = false;
309
- }
310
- if (url) {
311
- this.valueInput.value = url;
312
- }
313
- this.previewImage.dispatchEvent(new CustomEvent("change", { bubbles: true }));
314
- this.valueInput.dispatchEvent(new CustomEvent("change", { bubbles: true }));
315
- this.valueInput.dispatchEvent(new CustomEvent("input", { bubbles: true }));
316
- }
317
- }
318
274
  function getFileExtension(file) {
319
- const parts = file.name.split(".");
320
- if (parts.length > 1) {
321
- return parts.pop();
322
- }
323
- return void 0;
275
+ const parts = file.name.split(".");
276
+ if (parts.length > 1) return parts.pop();
324
277
  }
325
- let loadingCropper;
278
+ var loadingCropper;
326
279
  async function loadCropper() {
327
- loadingCropper ??= Promise.all([
328
- import("cropperjs"),
329
- import("./cropper.min.js").then(({ default: css2 }) => {
330
- injectCssToDocument(css2);
331
- })
332
- ]);
333
- const module = (await loadingCropper)[0];
334
- return module.default;
280
+ loadingCropper ??= Promise.all([import("cropperjs"), import("./cropper.min.js").then(({ default: css }) => {
281
+ injectCssToDocument(css);
282
+ })]);
283
+ return (await loadingCropper)[0].default;
335
284
  }
336
285
  async function init() {
337
- injectCssToDocument(css);
338
- customElements.define(SingleImageDragElement.is, SingleImageDragElement);
339
- await loadCropper();
286
+ injectCssToDocument(single_image_drag_default);
287
+ customElements.define(SingleImageDragElement.is, SingleImageDragElement);
288
+ await loadCropper();
340
289
  }
341
- const ready = /* @__PURE__ */ init();
342
- export {
343
- SingleImageDragElement,
344
- ready
345
- };
346
- //# sourceMappingURL=field-single-image-drag.js.map
290
+ var ready = /* @__PURE__ */ init();
291
+ //#endregion
292
+ export { SingleImageDragElement, ready };
293
+
294
+ //# sourceMappingURL=field-single-image-drag.js.map