@windwalker-io/unicorn-next 0.1.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.
Files changed (175) hide show
  1. package/.editorconfig +18 -0
  2. package/.gulp.json +7 -0
  3. package/bin/release.mjs +47 -0
  4. package/dist/chunks/_arrayPush.js +168 -0
  5. package/dist/chunks/_arrayPush.js.map +1 -0
  6. package/dist/chunks/_baseRest.js +73 -0
  7. package/dist/chunks/_baseRest.js.map +1 -0
  8. package/dist/chunks/_commonjsHelpers.js +7 -0
  9. package/dist/chunks/_commonjsHelpers.js.map +1 -0
  10. package/dist/chunks/_getPrototype.js +130 -0
  11. package/dist/chunks/_getPrototype.js.map +1 -0
  12. package/dist/chunks/button-radio.js +147 -0
  13. package/dist/chunks/button-radio.js.map +1 -0
  14. package/dist/chunks/checkboxes-multi-select.js +44 -0
  15. package/dist/chunks/checkboxes-multi-select.js.map +1 -0
  16. package/dist/chunks/cloneDeep.js +287 -0
  17. package/dist/chunks/cloneDeep.js.map +1 -0
  18. package/dist/chunks/cropper.min.js +5 -0
  19. package/dist/chunks/cropper.min.js.map +1 -0
  20. package/dist/chunks/field-cascade-select.js +256 -0
  21. package/dist/chunks/field-cascade-select.js.map +1 -0
  22. package/dist/chunks/field-file-drag.js +218 -0
  23. package/dist/chunks/field-file-drag.js.map +1 -0
  24. package/dist/chunks/field-flatpickr.js +893 -0
  25. package/dist/chunks/field-flatpickr.js.map +1 -0
  26. package/dist/chunks/field-modal-select.js +403 -0
  27. package/dist/chunks/field-modal-select.js.map +1 -0
  28. package/dist/chunks/field-modal-tree.js +790 -0
  29. package/dist/chunks/field-modal-tree.js.map +1 -0
  30. package/dist/chunks/field-multi-uploader.js +256 -0
  31. package/dist/chunks/field-multi-uploader.js.map +1 -0
  32. package/dist/chunks/field-repeatable.js +132 -0
  33. package/dist/chunks/field-repeatable.js.map +1 -0
  34. package/dist/chunks/field-single-image-drag.js +338 -0
  35. package/dist/chunks/field-single-image-drag.js.map +1 -0
  36. package/dist/chunks/form.js +154 -0
  37. package/dist/chunks/form.js.map +1 -0
  38. package/dist/chunks/grid.js +345 -0
  39. package/dist/chunks/grid.js.map +1 -0
  40. package/dist/chunks/http-client.js +229 -0
  41. package/dist/chunks/http-client.js.map +1 -0
  42. package/dist/chunks/iframe-modal.js +124 -0
  43. package/dist/chunks/iframe-modal.js.map +1 -0
  44. package/dist/chunks/index.js +309 -0
  45. package/dist/chunks/index.js.map +1 -0
  46. package/dist/chunks/isArguments.js +146 -0
  47. package/dist/chunks/isArguments.js.map +1 -0
  48. package/dist/chunks/keep-tab.js +101 -0
  49. package/dist/chunks/keep-tab.js.map +1 -0
  50. package/dist/chunks/legacy.js +210 -0
  51. package/dist/chunks/legacy.js.map +1 -0
  52. package/dist/chunks/list-dependent.js +231 -0
  53. package/dist/chunks/list-dependent.js.map +1 -0
  54. package/dist/chunks/s3-multipart-uploader.js +172 -0
  55. package/dist/chunks/s3-multipart-uploader.js.map +1 -0
  56. package/dist/chunks/s3-uploader.js +136 -0
  57. package/dist/chunks/s3-uploader.js.map +1 -0
  58. package/dist/chunks/show-on.js +237 -0
  59. package/dist/chunks/show-on.js.map +1 -0
  60. package/dist/chunks/tinymce.js +196 -0
  61. package/dist/chunks/tinymce.js.map +1 -0
  62. package/dist/chunks/ui-bootstrap5.js +71 -0
  63. package/dist/chunks/ui-bootstrap5.js.map +1 -0
  64. package/dist/chunks/unicorn.js +2202 -0
  65. package/dist/chunks/unicorn.js.map +1 -0
  66. package/dist/chunks/validation.js +854 -0
  67. package/dist/chunks/validation.js.map +1 -0
  68. package/dist/editor.css +1 -0
  69. package/dist/index.d.ts +1427 -0
  70. package/dist/multi-level-menu.css +1 -0
  71. package/dist/switcher.css +1 -0
  72. package/dist/unicorn-next.css +12 -0
  73. package/dist/unicorn.js +125 -0
  74. package/dist/unicorn.js.map +1 -0
  75. package/fusionfile.mjs +155 -0
  76. package/images/ajax-loader.gif +0 -0
  77. package/images/placeholder/avatar.png +0 -0
  78. package/images/placeholder/image-16x10.png +0 -0
  79. package/images/placeholder/image-16x9.png +0 -0
  80. package/images/placeholder/image-1x1.png +0 -0
  81. package/images/placeholder/image-4x3.png +0 -0
  82. package/package.json +102 -0
  83. package/scss/bootstrap/multi-level-menu.scss +121 -0
  84. package/scss/editor.scss +116 -0
  85. package/scss/field/file-drag.scss +102 -0
  86. package/scss/field/single-image-drag.scss +88 -0
  87. package/scss/field/vue-drag-uploader.scss +160 -0
  88. package/scss/switcher.scss +156 -0
  89. package/src/app.ts +128 -0
  90. package/src/bootstrap/button-radio.ts +208 -0
  91. package/src/bootstrap/keep-tab.ts +155 -0
  92. package/src/composable/index.ts +21 -0
  93. package/src/composable/useCheckboxesMultiSelect.ts +22 -0
  94. package/src/composable/useFieldCascadeSelect.ts +9 -0
  95. package/src/composable/useFieldFileDrag.ts +9 -0
  96. package/src/composable/useFieldFlatpickr.ts +3 -0
  97. package/src/composable/useFieldModalSelect.ts +6 -0
  98. package/src/composable/useFieldModalTree.ts +3 -0
  99. package/src/composable/useFieldMultiUploader.ts +3 -0
  100. package/src/composable/useFieldRepeatable.ts +9 -0
  101. package/src/composable/useFieldSingleImageDrag.ts +5 -0
  102. package/src/composable/useForm.ts +43 -0
  103. package/src/composable/useGrid.ts +57 -0
  104. package/src/composable/useHttp.ts +8 -0
  105. package/src/composable/useIframeModal.ts +9 -0
  106. package/src/composable/useListDependent.ts +26 -0
  107. package/src/composable/useQueue.ts +13 -0
  108. package/src/composable/useS3Uploader.ts +32 -0
  109. package/src/composable/useShowOn.ts +9 -0
  110. package/src/composable/useStack.ts +13 -0
  111. package/src/composable/useTinymce.ts +29 -0
  112. package/src/composable/useTomSelect.ts +72 -0
  113. package/src/composable/useUIBootstrap5.ts +48 -0
  114. package/src/composable/useUniDirective.ts +32 -0
  115. package/src/composable/useValidation.ts +39 -0
  116. package/src/data.ts +36 -0
  117. package/src/events.ts +73 -0
  118. package/src/legacy/legacy.ts +186 -0
  119. package/src/legacy/loader.ts +125 -0
  120. package/src/module/checkboxes-multi-select.ts +54 -0
  121. package/src/module/field-cascade-select.ts +292 -0
  122. package/src/module/field-file-drag.ts +292 -0
  123. package/src/module/field-flatpickr.ts +127 -0
  124. package/src/module/field-modal-select.ts +174 -0
  125. package/src/module/field-modal-tree.ts +27 -0
  126. package/src/module/field-multi-uploader.ts +361 -0
  127. package/src/module/field-repeatable.ts +202 -0
  128. package/src/module/field-single-image-drag.ts +468 -0
  129. package/src/module/form.ts +223 -0
  130. package/src/module/grid.ts +465 -0
  131. package/src/module/http-client.ts +243 -0
  132. package/src/module/iframe-modal.ts +167 -0
  133. package/src/module/list-dependent.ts +321 -0
  134. package/src/module/s3-multipart-uploader.ts +300 -0
  135. package/src/module/s3-uploader.ts +234 -0
  136. package/src/module/show-on.ts +173 -0
  137. package/src/module/tinymce.ts +263 -0
  138. package/src/module/ui-bootstrap5.ts +107 -0
  139. package/src/module/validation.ts +1019 -0
  140. package/src/plugin/index.ts +1 -0
  141. package/src/plugin/php-adapter.ts +65 -0
  142. package/src/polyfill/form-request-submit.ts +31 -0
  143. package/src/polyfill/index.ts +9 -0
  144. package/src/service/animate.ts +58 -0
  145. package/src/service/crypto.ts +27 -0
  146. package/src/service/dom-watcher.ts +62 -0
  147. package/src/service/dom.ts +265 -0
  148. package/src/service/helper.ts +48 -0
  149. package/src/service/index.ts +10 -0
  150. package/src/service/lang.ts +122 -0
  151. package/src/service/loader.ts +152 -0
  152. package/src/service/router.ts +118 -0
  153. package/src/service/ui.ts +497 -0
  154. package/src/service/uri.ts +106 -0
  155. package/src/types/base.ts +9 -0
  156. package/src/types/index.ts +4 -0
  157. package/src/types/modal-tree.ts +12 -0
  158. package/src/types/plugin.ts +6 -0
  159. package/src/types/shims.d.ts +18 -0
  160. package/src/types/ui.ts +6 -0
  161. package/src/unicorn.ts +63 -0
  162. package/src/utilities/arr.ts +25 -0
  163. package/src/utilities/base.ts +9 -0
  164. package/src/utilities/data.ts +48 -0
  165. package/src/utilities/index.ts +5 -0
  166. package/src/utilities/tree.ts +20 -0
  167. package/src/vue/components/ModalTree/ModalTreeApp.vue +175 -0
  168. package/src/vue/components/ModalTree/TreeItem.vue +262 -0
  169. package/src/vue/components/ModalTree/TreeModal.vue +225 -0
  170. package/tests/test.js +4 -0
  171. package/tsconfig.js.json +25 -0
  172. package/tsconfig.json +17 -0
  173. package/vite.assets.config.ts +61 -0
  174. package/vite.config.test.ts +36 -0
  175. package/vite.config.ts +112 -0
@@ -0,0 +1,321 @@
1
+
2
+ import type { CancelTokenSource } from 'axios';
3
+ import { useHttpClient, useUniDirective } from '../composable';
4
+ import { getBoundedInstance, html, selectOne } from '../service';
5
+ import { mergeDeep } from '../utilities';
6
+
7
+ const nope = () => {};
8
+
9
+ export interface ListDependentOptions {
10
+ ajax: {
11
+ url: string | null | ((self: ListDependent) => string);
12
+ value_field: string;
13
+ data: Record<string, any> | ((data: Record<string, any>, self: ListDependent) => Record<string, any>);
14
+ };
15
+ source?: Record<string, any>;
16
+ text_field: string;
17
+ value_field: string;
18
+ first_option?: Record<string, any>;
19
+ default_value: any;
20
+ initial_load: boolean;
21
+ empty_mark: string;
22
+ hooks: {
23
+ before_request: (value: any, element: HTMLSelectElement, dependent: HTMLSelectElement) => any;
24
+ after_request: (value: any, element: HTMLSelectElement, dependent: HTMLSelectElement) => any;
25
+ };
26
+ }
27
+
28
+ const defaultOptions: ListDependentOptions = {
29
+ ajax: {
30
+ url: null,
31
+ value_field: 'value',
32
+ data: {},
33
+ },
34
+ source: undefined,
35
+ text_field: 'title',
36
+ value_field: 'id',
37
+ first_option: undefined,
38
+ default_value: null,
39
+ initial_load: true,
40
+ empty_mark: '__EMPTY__',
41
+ hooks: {
42
+ before_request: nope,
43
+ after_request: nope
44
+ }
45
+ };
46
+
47
+ type ListItems = Record<string, any>[];
48
+ type MaybeGroupedListItems = Record<string, ListItems> | ListItems;
49
+
50
+ export class ListDependent {
51
+ element: HTMLSelectElement;
52
+ dependent: HTMLSelectElement;
53
+ options: ListDependentOptions;
54
+ abortController: AbortController | null = null;
55
+
56
+ static handle(el: string | HTMLSelectElement, dependent?: string | HTMLSelectElement, options: Partial<ListDependentOptions> = {}): ListDependent {
57
+ return getBoundedInstance(el, 'list-dependent', () => {
58
+ return new this(el, dependent, options);
59
+ });
60
+ }
61
+
62
+ constructor(element: string | HTMLSelectElement, dependent?: string | HTMLSelectElement, options: Partial<ListDependentOptions> = {}) {
63
+ this.options = this.mergeOptions(options);
64
+
65
+ this.element = selectOne<HTMLSelectElement>(element)!;
66
+
67
+ if (!dependent) {
68
+ dependent = this.element.dataset.dependent || '';
69
+ }
70
+
71
+ this.dependent = selectOne<HTMLSelectElement>(dependent)!;
72
+
73
+ this.bindEvents();
74
+
75
+ if (this.options.initial_load) {
76
+ this.changeList(this.dependent.value, true);
77
+ }
78
+ }
79
+
80
+ /**
81
+ * Bind events.
82
+ */
83
+ bindEvents() {
84
+ this.dependent.addEventListener('change', (event) => {
85
+ this.changeList((event.currentTarget as HTMLSelectElement)?.value);
86
+ });
87
+ }
88
+
89
+ /**
90
+ * Update the list elements.
91
+ *
92
+ * @param {*} value
93
+ * @param {bool} initial
94
+ */
95
+ changeList(value: string, initial = false) {
96
+ value = value || this.dependent.value;
97
+
98
+ // Empty mark
99
+ if (value === '') {
100
+ value = this.options.empty_mark;
101
+ }
102
+
103
+ if (this.options.ajax.url) {
104
+ this.ajaxUpdate(value);
105
+ } else if (this.options.source) {
106
+ this.sourceUpdate(value, initial);
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Update list by source.
112
+ */
113
+ sourceUpdate(value: string, initial = false) {
114
+ const source = this.options.source;
115
+
116
+ if (!source) {
117
+ return;
118
+ }
119
+
120
+ this.beforeHook(value, this.element, this.dependent);
121
+
122
+ if (source[value]) {
123
+ this.updateListElements(source[value]);
124
+ } else {
125
+ this.updateListElements([]);
126
+
127
+ if (!initial && value !== '' && parseInt(value) !== 0) {
128
+ console.log('List for value: ' + value + ' not found.');
129
+ }
130
+ }
131
+
132
+ this.afterHook(value, this.element, this.dependent);
133
+ }
134
+
135
+ /**
136
+ * Do ajax.
137
+ *
138
+ * @param {string} value
139
+ */
140
+ async ajaxUpdate(value: string) {
141
+ let data: Record<string, any> = {};
142
+
143
+ data[this.options.ajax.value_field] = value;
144
+
145
+ if (typeof this.options.ajax.data === 'object') {
146
+ data = { ...data, ...this.options.ajax.data };
147
+ } else if (typeof this.options.ajax.data === 'function') {
148
+ data = this.options.ajax.data(data, this) || data;
149
+ }
150
+
151
+ this.beforeHook(value, this.element, this.dependent);
152
+
153
+ this.abort();
154
+
155
+ let url = this.options.ajax.url;
156
+
157
+ if (typeof url === 'function') {
158
+ url = url(this);
159
+ }
160
+
161
+ if (!url) {
162
+ throw new Error('Ajax URL is not set.');
163
+ }
164
+
165
+ const http = await useHttpClient();
166
+
167
+ this.abortController = new AbortController();
168
+
169
+ try {
170
+ const res = await http.get<{
171
+ success: boolean;
172
+ data: any;
173
+ }>(url, {
174
+ params: data,
175
+ signal: this.abortController.signal
176
+ });
177
+
178
+ const { success, data: returnData } = res.data;
179
+
180
+ if (success) {
181
+ this.updateListElements(returnData);
182
+ } else {
183
+ console.error(returnData);
184
+ }
185
+
186
+ } catch (e) {
187
+ console.error(e);
188
+ } finally {
189
+ this.afterHook(value, this.element, this.dependent);
190
+ this.abortController = null;
191
+ }
192
+ }
193
+
194
+ abort() {
195
+ if (this.abortController) {
196
+ this.abortController.abort();
197
+ this.abortController = null;
198
+ }
199
+ }
200
+
201
+ updateListElements(items: MaybeGroupedListItems) {
202
+ const textField = this.options.text_field;
203
+ const valueField = this.options.value_field;
204
+ this.element.innerHTML = '';
205
+
206
+ if (this.options.first_option && Array.isArray(items)) {
207
+ items.unshift({});
208
+ items[0][textField] = this.options.first_option[textField];
209
+ items[0][valueField] = this.options.first_option[valueField];
210
+ }
211
+
212
+ for (const i in items) {
213
+ const item = items[i as keyof typeof items] as Record<string, any> | ListItems;
214
+
215
+ if (Array.isArray(item)) {
216
+ const group = html(`<optgroup label="${i}"></optgroup>`);
217
+
218
+ for (const k in item) {
219
+ const child = item[k];
220
+ this.appendOptionTo({
221
+ value: child[valueField],
222
+ text: child[textField],
223
+ attributes: child.attributes,
224
+ }, group);
225
+ }
226
+
227
+ this.element.appendChild(group);
228
+ continue;
229
+ }
230
+
231
+ this.appendOptionTo({
232
+ value: item[valueField],
233
+ text: item[textField],
234
+ attributes: item.attributes,
235
+ }, this.element);
236
+ }
237
+
238
+ this.element.dispatchEvent(new CustomEvent('change'));
239
+ this.element.dispatchEvent(new CustomEvent('list:updated'));
240
+ }
241
+
242
+ appendOptionTo(item: any, parent: any) {
243
+ const value = item.value;
244
+ const option = html('<option>' + item.text + '</option>');
245
+ option.setAttribute('value', value);
246
+
247
+ if (item.attributes) {
248
+ for (const index in item.attributes) {
249
+ const val = item.attributes[index];
250
+ option.setAttribute(index, val);
251
+ }
252
+ }
253
+
254
+ if (this.isSelected(value)) {
255
+ option.setAttribute('selected', 'selected');
256
+ }
257
+
258
+ parent.appendChild(option);
259
+ }
260
+
261
+ isSelected(value: string) {
262
+ let defaultValues: any[] = [];
263
+
264
+ // Convert all types to array
265
+ let defValue = this.element.dataset.selected ?? this.options.default_value;
266
+
267
+ if (typeof defValue === 'function') {
268
+ defValue = defValue(value, this);
269
+ }
270
+
271
+ if (Array.isArray(defValue)) {
272
+ defaultValues = defValue;
273
+ } else if (defValue && typeof defValue === 'object') {
274
+ defaultValues = Object.keys(defValue);
275
+ } else {
276
+ defaultValues = [defValue];
277
+ }
278
+
279
+ return defaultValues.indexOf(value) !== -1;
280
+ }
281
+
282
+ /**
283
+ * Before hook.
284
+ */
285
+ beforeHook(value: string, element: HTMLSelectElement, dependent: HTMLSelectElement) {
286
+ const before = this.options.hooks.before_request;
287
+
288
+ return before.call(this, value, element, dependent);
289
+ }
290
+
291
+ /**
292
+ * After hook.
293
+ */
294
+ afterHook(value: string, element: HTMLSelectElement, dependent: HTMLSelectElement) {
295
+ const after = this.options.hooks.after_request;
296
+
297
+ return after.call(this, value, element, dependent);
298
+ }
299
+
300
+ mergeOptions(options: Partial<ListDependentOptions>): ListDependentOptions {
301
+ return mergeDeep<ListDependentOptions>({}, defaultOptions, options);
302
+ }
303
+ }
304
+
305
+ export const ready = useUniDirective<HTMLSelectElement>('list-dependent', {
306
+ mounted(el, binding) {
307
+ const options = JSON.parse(binding.value);
308
+
309
+ ListDependent.handle(el, options.dependent, options);
310
+ },
311
+ updated(el, binding) {
312
+ const options = JSON.parse(binding.value);
313
+
314
+ ListDependent.handle(el).mergeOptions(options);
315
+ }
316
+ });
317
+
318
+ export type ListDependentModule = {
319
+ ListDependent: typeof ListDependent;
320
+ ready: typeof ready;
321
+ };
@@ -0,0 +1,300 @@
1
+ import { AxiosProgressEvent, AxiosResponseHeaders } from 'axios';
2
+ import { Mixin } from 'ts-mixer';
3
+ import { createQueue, useHttpClient } from '../composable';
4
+ import { EventHandler, EventMixin } from '../events';
5
+ import type { MaybePromise } from '../types';
6
+ import { mergeDeep } from '../utilities';
7
+ import { ApiReturn } from './http-client';
8
+
9
+ declare type RoutingOptions = {
10
+ init: string;
11
+ sign: string;
12
+ complete: string;
13
+ abort: string;
14
+ } | ((action: RouteActions) => MaybePromise<string>);
15
+
16
+ declare type RouteActions = 'init' | 'sign' | 'complete' | 'abort';
17
+ declare type RequestHandler = <T = Record<string, any>>(action: RouteActions, data: Record<string, any>) => Promise<T>;
18
+
19
+ export interface S3MultipartUploaderOptions {
20
+ profile?: string;
21
+ chunkSize: number;
22
+ concurrency: number;
23
+ routes: RoutingOptions;
24
+ requestHandler?: RequestHandler;
25
+ onProgress?: ProgressEventHandler;
26
+ ACL?: string;
27
+ extra?: Record<string, any>;
28
+
29
+ // maxRetries?: number;
30
+ // endpoint: string;
31
+ // subfolder?: string;
32
+ }
33
+
34
+ const defaultOptions: Partial<S3MultipartUploaderOptions> = {
35
+ chunkSize: 5 * 1024 * 1024, // 5MB
36
+ concurrency: 2,
37
+ };
38
+
39
+ export interface S3MultipartUploaderRequestOptions {
40
+ onProgress?: ProgressEventHandler;
41
+ filename?: string;
42
+ ContentType?: string;
43
+ ContentDisposition?: string;
44
+ ACL?: string;
45
+ extra?: Record<string, any>;
46
+ }
47
+
48
+ export class S3MultipartUploader extends Mixin(EventMixin) {
49
+ options: S3MultipartUploaderOptions;
50
+
51
+ constructor(options: Partial<S3MultipartUploaderOptions>) {
52
+ super();
53
+ this.options = mergeDeep({}, defaultOptions, options);
54
+ }
55
+
56
+ async upload(
57
+ file: string | File | Blob,
58
+ path: string,
59
+ options: S3MultipartUploaderRequestOptions = {}
60
+ ): Promise<{ url: string; }> {
61
+ const extra: Record<string, any> = { ...(this.options.extra ?? {}), ...(options.extra ?? {}) };
62
+
63
+ if (typeof file === 'string') {
64
+ file = new Blob([file], { type: options['ContentType'] || 'text/plain' });
65
+ }
66
+
67
+ if (file instanceof Blob && !(file instanceof File)) {
68
+ if (path.endsWith('.{ext}')) {
69
+ throw new Error('If using Blob or file data string, you must provide a valid file extension in the path.');
70
+ }
71
+
72
+ file = new File([file], 'blob', { type: file.type });
73
+ }
74
+
75
+ if (file instanceof File) {
76
+ extra['ContentType'] = options['ContentType'] || file.type;
77
+ }
78
+
79
+ if (options.ACL || this.options.ACL) {
80
+ extra.ACL = options.ACL || this.options.ACL;
81
+ }
82
+
83
+ path = this.replaceExt(path, file);
84
+
85
+ const initData: Record<string, any> = { extra, path, profile: this.options.profile };
86
+
87
+ if (options['filename']) {
88
+ initData['filename'] = options['filename'];
89
+ }
90
+
91
+ this.trigger('start', file, initData);
92
+
93
+ // @Request sign
94
+ const { id } = await this.request<{ id: string; }>(
95
+ 'init',
96
+ initData
97
+ );
98
+
99
+ try {
100
+ const chunkSize = this.options.chunkSize;
101
+ const chunks = Math.ceil(file.size / chunkSize);
102
+
103
+ let uploadedBytes = 0;
104
+ let parts: { ETag: string, PartNumber: number }[] = [];
105
+ let currentPart = 1;
106
+ const queue = createQueue(this.options.concurrency);
107
+ const promises = [];
108
+ const partsUploaded: Record<number, number> = {};
109
+
110
+ // Loop from 1 to chunks
111
+ while (currentPart <= chunks) {
112
+ const partNumber = currentPart;
113
+
114
+ // Push to queue
115
+ const p = queue.push(async () => {
116
+ const { blob, etag } = await this.uploadPart(
117
+ file as File,
118
+ {
119
+ id,
120
+ path,
121
+ partNumber,
122
+ chunkSize,
123
+ onUploadProgress: (e) => {
124
+ partsUploaded[partNumber] = e.loaded;
125
+
126
+ const uploaded = Object.values(partsUploaded).reduce((sum, a) => sum + a, 0);
127
+
128
+ this.updateProgress(uploaded, file.size, options);
129
+ }
130
+ }
131
+ );
132
+
133
+ uploadedBytes += blob.size;
134
+
135
+ this.updateProgress(uploadedBytes, file.size, options);
136
+
137
+ parts.push({ ETag: etag, PartNumber: partNumber });
138
+ });
139
+
140
+ promises.push(p);
141
+
142
+ currentPart++;
143
+ }
144
+
145
+ await Promise.all(promises);
146
+
147
+ // @Request sign
148
+ const { url } = await this.request<{ url: string }>(
149
+ 'complete',
150
+ {
151
+ id,
152
+ path,
153
+ parts: parts.sort((a, b) => a.PartNumber - b.PartNumber),
154
+ profile: this.options.profile,
155
+ },
156
+ );
157
+
158
+ this.trigger('success', url);
159
+
160
+ return { url };
161
+ } catch (e) {
162
+ await this.abort(id, path);
163
+
164
+ throw e;
165
+ }
166
+ }
167
+
168
+ protected async uploadPart(
169
+ file: File,
170
+ payload: {
171
+ id: string;
172
+ path: string;
173
+ partNumber: number;
174
+ chunkSize: number;
175
+ onUploadProgress: (e: AxiosProgressEvent) => void;
176
+ }
177
+ ) {
178
+ const http = await useHttpClient();
179
+ const { id, path, partNumber, chunkSize, onUploadProgress } = payload;
180
+
181
+ const start = (partNumber - 1) * chunkSize;
182
+ const end = Math.min(partNumber * chunkSize, file.size);
183
+
184
+ const blob = file.slice(start, end);
185
+
186
+ // @Request sign
187
+ const { url } = await this.request<{ url: string; }>(
188
+ 'sign',
189
+ {
190
+ id,
191
+ path,
192
+ partNumber,
193
+ profile: this.options.profile,
194
+ }
195
+ );
196
+
197
+ // PUT to S3
198
+ const res = await http.put(
199
+ url,
200
+ blob,
201
+ {
202
+ onUploadProgress,
203
+ }
204
+ );
205
+
206
+ const etag = String((res.headers as AxiosResponseHeaders).get('ETag') || '');
207
+
208
+ return { blob, etag };
209
+ }
210
+
211
+ protected async request<T = Record<string, any>>(action: RouteActions, body: Record<string, any>): Promise<T> {
212
+ if (this.options.requestHandler) {
213
+ return this.options.requestHandler<T>(action, body);
214
+ }
215
+
216
+ const http = await useHttpClient();
217
+
218
+ const res = await http.post<ApiReturn<T>>(await this.resolveRoute(action), body);
219
+
220
+ return res.data.data;
221
+ }
222
+
223
+ async abort(id: string, path: string) {
224
+ await this.request(
225
+ 'abort',
226
+ {
227
+ id,
228
+ path,
229
+ profile: this.options.profile,
230
+ }
231
+ );
232
+ }
233
+
234
+ updateProgress(loaded: number, total: number, options: S3MultipartUploaderRequestOptions) {
235
+ const percentage = (loaded / total) * 100;
236
+
237
+ const event: ProgressEvent = { percentage, loaded, total };
238
+
239
+ this.trigger('progress', event);
240
+
241
+ this.options.onProgress?.(event);
242
+
243
+ if (options.onProgress) {
244
+ options.onProgress(event);
245
+ }
246
+ }
247
+
248
+ async resolveRoute(action: RouteActions): Promise<string> {
249
+ if (typeof this.options.routes === 'function') {
250
+ return this.options.routes(action);
251
+ }
252
+
253
+ return this.options.routes[action];
254
+ }
255
+
256
+ setChunkSize(size: number): this {
257
+ this.options.chunkSize = size;
258
+
259
+ return this;
260
+ }
261
+
262
+ setChunkSizeInMiB(size: number): this {
263
+ this.options.chunkSize = size * 1024 * 1024;
264
+
265
+ return this;
266
+ }
267
+
268
+ replaceExt(path: string, file: File | Blob): string {
269
+ if (file instanceof File) {
270
+ const fileExt = file.name.split('.').pop();
271
+
272
+ if (path.endsWith('.{ext}')) {
273
+ return path.replace(/\.{ext}$/, fileExt ? '.' + fileExt : '');
274
+ }
275
+ }
276
+
277
+ return path;
278
+ }
279
+
280
+ on(
281
+ event: 'start',
282
+ handler: (file: File, data: { path: string; extra: Record<string, any>; [name: string]: any; }) => void
283
+ ): this;
284
+ on(event: 'success', handler: (url: string) => void): this;
285
+ on(event: 'progress', handler: (event: ProgressEvent) => void): this;
286
+ on(event: string | string[], handler: EventHandler): this {
287
+ return super.on(event, handler);
288
+ }
289
+ }
290
+
291
+ type ProgressEvent = {
292
+ percentage: number;
293
+ loaded: number;
294
+ total: number;
295
+ };
296
+ type ProgressEventHandler = (e: ProgressEvent) => void;
297
+
298
+ export interface S3MultipartUploaderModule {
299
+ S3MultipartUploader: typeof S3MultipartUploader;
300
+ }