@uploadcare/file-uploader 1.15.0-alpha.14 → 1.15.0-alpha.16

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.
@@ -1,21 +1,6 @@
1
- export class FileItem extends UploaderBlock {
2
- /** @private */
3
- private _entrySubs;
4
- /**
5
- * @private
6
- * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
7
- */
8
- private _entry;
1
+ export class FileItem extends FileItemConfig {
9
2
  /** @private */
10
3
  private _renderedOnce;
11
- /**
12
- * @private
13
- * @template {any[]} A
14
- * @template {(entry: import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData, ...args: A) => any} T
15
- * @param {T} fn
16
- * @returns {(...args: A) => ReturnType<T>}
17
- */
18
- private _withEntry;
19
4
  init$: {
20
5
  uid: string;
21
6
  itemName: string;
@@ -62,12 +47,6 @@ export class FileItem extends UploaderBlock {
62
47
  private _calculateState;
63
48
  /** @private */
64
49
  private _debouncedCalculateState;
65
- /**
66
- * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
67
- * @param {K} prop_
68
- * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler_
69
- */
70
- _subEntry: (prop_: K, handler_: (value: import("../../abstract/uploadEntrySchema.js").UploadEntryData[K]) => void) => void;
71
50
  _updateHint: (...args: any[]) => void;
72
51
  /**
73
52
  * @private
@@ -92,5 +71,5 @@ export namespace FileItem {
92
71
  let template: string;
93
72
  let activeInstances: Set<any>;
94
73
  }
95
- import { UploaderBlock } from '../../abstract/UploaderBlock.js';
74
+ import { FileItemConfig } from './FileItemConfig.js';
96
75
  //# sourceMappingURL=FileItem.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"FileItem.d.ts","sourceRoot":"","sources":["FileItem.js"],"names":[],"mappings":"AAiBA;IAIE,eAAe;IACf,mBAAuB;IACvB;;;OAGG;IACH,eAAc;IAEd,eAAe;IACf,sBAAsB;IAEtB;;;;;;OAMG;IACH,mBAYC;IAKC;;;;;;;;;;;;;;;;;;;;;;;+CAvCS,gBAAU;mCAIjB,gBAAU;6BAImB,2BAA2B;;wCAS5C,wCACE;;;;;;MAoDf;IAGH,eAQC;IAED;;;OAGG;IACH,0BAUC;IAPC,qCAA2C;IAC3C,wCAA0C;IAQ5C,eAAe;IACf,wBAYG;IAEH,eAAe;IACf,iCAA0E;IAE1E;;;;OAIG;IACH,YAAa,OAHF,CAGO,EAAE,UAFT,CAAC,KAAK,EAAE,OAAO,qCAAqC,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAE5D,UAgBP;IAErB,sCAeE;IAEF;;;OAGG;IACH,uBAiCC;IAyDD,uCA+BE;IAaA,eAAe;IACf,kBAEE;IAUJ;;;MAEC;IAED;;;OAGG;IACH,uBAEC;IAED,0CAqGG;CACJ;;;;;8BAnb6B,iCAAiC"}
1
+ {"version":3,"file":"FileItem.d.ts","sourceRoot":"","sources":["FileItem.js"],"names":[],"mappings":"AAiBA;IAIE,eAAe;IACf,sBAAsB;IAKpB;;;;;;;;;;;;;;;;;;;;;;;+CAVoB,gBAAU;mCAIlB,gBACR;6BAMM,2BAET;;wCAMC,wCACK;;;;;;MAqBN;IAGH,eAQC;IAED;;;OAGG;IACH,0BAUC;IAPC,qCAA2C;IAC3C,wCAA0C;IAQ5C,eAAe;IACf,wBAYG;IAEH,eAAe;IACf,iCAA0E;IAE1E,sCAeE;IAEF;;;OAGG;IACH,uBAiCC;IAyDD,uCA+BE;IAaA,eAAe;IACf,kBAEE;IAUJ;;;MAEC;IAED;;;OAGG;IACH,uBAEC;IAED,0CAqGG;CACJ;;;;;+BA3X8B,qBAAqB"}
@@ -2,11 +2,11 @@
2
2
  import { shrinkFile } from '@uploadcare/image-shrink';
3
3
  import { CancelError, UploadcareError, uploadFile } from '@uploadcare/upload-client';
4
4
  import { ActivityBlock } from '../../abstract/ActivityBlock.js';
5
- import { UploaderBlock } from '../../abstract/UploaderBlock.js';
6
5
  import { debounce } from '../utils/debounce.js';
7
6
  import { parseShrink } from '../../utils/parseShrink.js';
8
7
  import { UploadSource } from '../utils/UploadSource.js';
9
8
  import { throttle } from '../utils/throttle.js';
9
+ import { FileItemConfig } from './FileItemConfig.js';
10
10
 
11
11
  const FileItemState = Object.freeze({
12
12
  FINISHED: Symbol('FINISHED'),
@@ -15,42 +15,13 @@ const FileItemState = Object.freeze({
15
15
  IDLE: Symbol('IDLE'),
16
16
  });
17
17
 
18
- export class FileItem extends UploaderBlock {
18
+ export class FileItem extends FileItemConfig {
19
19
  couldBeCtxOwner = true;
20
20
  pauseRender = true;
21
21
 
22
- /** @private */
23
- _entrySubs = new Set();
24
- /**
25
- * @private
26
- * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
27
- */
28
- _entry = null;
29
-
30
22
  /** @private */
31
23
  _renderedOnce = false;
32
24
 
33
- /**
34
- * @private
35
- * @template {any[]} A
36
- * @template {(entry: import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData, ...args: A) => any} T
37
- * @param {T} fn
38
- * @returns {(...args: A) => ReturnType<T>}
39
- */
40
- _withEntry(fn) {
41
- const wrapperFn = /** @type {(...args: A) => ReturnType<T>} */ (
42
- (...args) => {
43
- const entry = this._entry;
44
- if (!entry) {
45
- console.warn('No entry found');
46
- return;
47
- }
48
- return fn(entry, ...args);
49
- }
50
- );
51
- return wrapperFn;
52
- }
53
-
54
25
  constructor() {
55
26
  super();
56
27
 
@@ -132,29 +103,6 @@ export class FileItem extends UploaderBlock {
132
103
  /** @private */
133
104
  _debouncedCalculateState = debounce(this._calculateState.bind(this), 100);
134
105
 
135
- /**
136
- * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
137
- * @param {K} prop_
138
- * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler_
139
- */
140
- _subEntry = (prop_, handler_) =>
141
- this._withEntry(
142
- /**
143
- * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
144
- * @param {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData} entry
145
- * @param {K} prop
146
- * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler
147
- */
148
- (entry, prop, handler) => {
149
- let sub = entry.subscribe(prop, (value) => {
150
- if (this.isConnected) {
151
- handler(value);
152
- }
153
- });
154
- this._entrySubs.add(sub);
155
- },
156
- )(prop_, handler_);
157
-
158
106
  _updateHint = this._withEntry(
159
107
  throttle((entry) => {
160
108
  const source = entry.getValue('source');
@@ -0,0 +1,26 @@
1
+ export class FileItemConfig extends UploaderBlock {
2
+ /** @protected */
3
+ protected _entrySubs: Set<any>;
4
+ /**
5
+ * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
6
+ * @protected
7
+ */
8
+ protected _entry: import("../../abstract/uploadEntrySchema.js").UploadEntryTypedData | null;
9
+ /**
10
+ * @template {any[]} A
11
+ * @template {(entry: import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData, ...args: A) => any} T
12
+ * @param {T} fn
13
+ * @returns {(...args: A) => ReturnType<T>}
14
+ * @protected
15
+ */
16
+ protected _withEntry<A extends any[], T extends (entry: import("../../abstract/uploadEntrySchema.js").UploadEntryTypedData, ...args: A) => any>(fn: T): (...args: A) => ReturnType<T>;
17
+ /**
18
+ * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
19
+ * @param {K} prop_
20
+ * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler_
21
+ * @protected
22
+ */
23
+ protected _subEntry: (prop_: K, handler_: (value: import("../../abstract/uploadEntrySchema.js").UploadEntryData[K]) => void) => void;
24
+ }
25
+ import { UploaderBlock } from '../../abstract/UploaderBlock.js';
26
+ //# sourceMappingURL=FileItemConfig.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FileItemConfig.d.ts","sourceRoot":"","sources":["FileItemConfig.js"],"names":[],"mappings":"AAIA;IACE,iBAAiB;IACjB,+BAAuB;IAEvB;;;OAGG;IACH,kBAHU,OAAO,qCAAqC,EAAE,oBAAoB,GAAG,IAAI,CAGrE;IAEd;;;;;;OAMG;IACH,qBANqB,CAAC,SAAR,GAAG,EAAG,EAC0F,CAAC,SAAlG,CAAE,KAAK,EAAE,OAAO,qCAAqC,EAAE,oBAAoB,EAAE,GAAG,IAAI,EAAE,CAAC,KAAK,GAAI,MAClG,CAAC,GACC,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,CAezC;IAED;;;;;OAKG;IACH,sBAAa,OAJF,CAIO,EAAE,UAHT,CAAC,KAAK,EAAE,OAAO,qCAAqC,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAG5D,UAgBP;CAMtB;8BA7D6B,iCAAiC"}
@@ -0,0 +1,64 @@
1
+ //@ts-check
2
+
3
+ import { UploaderBlock } from '../../abstract/UploaderBlock.js';
4
+
5
+ export class FileItemConfig extends UploaderBlock {
6
+ /** @protected */
7
+ _entrySubs = new Set();
8
+
9
+ /**
10
+ * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
11
+ * @protected
12
+ */
13
+ _entry = null;
14
+
15
+ /**
16
+ * @template {any[]} A
17
+ * @template {(entry: import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData, ...args: A) => any} T
18
+ * @param {T} fn
19
+ * @returns {(...args: A) => ReturnType<T>}
20
+ * @protected
21
+ */
22
+ _withEntry(fn) {
23
+ const wrapperFn = /** @type {(...args: A) => ReturnType<T>} */ (
24
+ (...args) => {
25
+ const entry = this._entry;
26
+ if (!entry) {
27
+ console.warn('No entry found');
28
+ return;
29
+ }
30
+ return fn(entry, ...args);
31
+ }
32
+ );
33
+ return wrapperFn;
34
+ }
35
+
36
+ /**
37
+ * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
38
+ * @param {K} prop_
39
+ * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler_
40
+ * @protected
41
+ */
42
+ _subEntry = (prop_, handler_) =>
43
+ this._withEntry(
44
+ /**
45
+ * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
46
+ * @param {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData} entry
47
+ * @param {K} prop
48
+ * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler
49
+ */
50
+ (entry, prop, handler) => {
51
+ let sub = entry.subscribe(prop, (value) => {
52
+ if (this.isConnected) {
53
+ handler(value);
54
+ }
55
+ });
56
+ this._entrySubs.add(sub);
57
+ },
58
+ )(prop_, handler_);
59
+
60
+ disconnectedCallback() {
61
+ super.disconnectedCallback();
62
+ this._entrySubs = new Set();
63
+ }
64
+ }
@@ -1,33 +1,13 @@
1
- export class Thumb extends UploaderBlock {
1
+ export class Thumb extends FileItemConfig {
2
2
  /** @private */
3
- private _entrySubs;
4
- /** @private */
5
- private _once;
6
- /**
7
- * @private
8
- * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
9
- */
10
- private _entry;
3
+ private _renderedGridOnce;
11
4
  /**
12
5
  * @private
13
6
  * @type {IntersectionObserverEntry['boundingClientRect'] | null}
14
7
  */
15
8
  private _thumbRect;
16
9
  _isIntersecting: boolean;
17
- /**
18
- * @private
19
- * @template {any[]} A
20
- * @template {(entry: import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData, ...args: A) => any} T
21
- * @param {T} fn
22
- * @returns {(...args: A) => ReturnType<T>}
23
- */
24
- private _withEntry;
25
- /**
26
- * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
27
- * @param {K} prop_
28
- * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler_
29
- */
30
- _subEntry: (prop_: K, handler_: (value: import("../../abstract/uploadEntrySchema.js").UploadEntryData[K]) => void) => void;
10
+ _firstViewMode: string;
31
11
  init$: {
32
12
  thumbUrl: string;
33
13
  badgeIcon: string;
@@ -46,11 +26,7 @@ export class Thumb extends UploaderBlock {
46
26
  '*historyBack': null;
47
27
  '*closeModal': () => void;
48
28
  };
49
- /**
50
- * Loads an image and sets it as the thumbnail.
51
- *
52
- * @param {string} imageUrl - The URL of the image to load.
53
- */
29
+ /** @param {string} imageUrl */
54
30
  loadImage(imageUrl: string): Promise<any>;
55
31
  _calculateThumbSize(force?: boolean): number;
56
32
  /** @private */
@@ -63,10 +39,15 @@ export class Thumb extends UploaderBlock {
63
39
  * @param {IntersectionObserverEntry[]} entries
64
40
  */
65
41
  private _observerCallback;
42
+ /**
43
+ * @private
44
+ * @param {String} id
45
+ */
46
+ private _handleEntryId;
66
47
  _observer: IntersectionObserver | undefined;
67
48
  }
68
49
  export namespace Thumb {
69
50
  let template: string;
70
51
  }
71
- import { UploaderBlock } from '../../abstract/UploaderBlock.js';
52
+ import { FileItemConfig } from '../FileItem/FileItemConfig.js';
72
53
  //# sourceMappingURL=Thumb.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Thumb.d.ts","sourceRoot":"","sources":["Thumb.js"],"names":[],"mappings":"AAQA;IACE,eAAe;IACf,mBAAuB;IAEvB,eAAe;IACf,cAAc;IAEd;;;OAGG;IACH,eAAc;IAEd;;;OAGG;IACH,mBAAkB;IAElB,yBAAwB;IAExB;;;;;;OAMG;IACH,mBAYC;IAED;;;;OAIG;IACH,YAAa,OAHF,CAGO,EAAE,UAFT,CAAC,KAAK,EAAE,OAAO,qCAAqC,EAAE,eAAe,CAAC,CAAC,CAAC,KAAK,IAE5D,UAgBP;IAKnB;;;;;;;+CArD0C,gBAAU;mCAOnD,gBACL;6BAC0D,2BAA2B;;wCAKzE,wCAA2B;;;;;;MA4ClC;IAGH;;;;OAIG;IACH,oBAFW,MAAM,gBAehB;IAED,6CAgBC;IAED,eAAe;IACf,2BA2CG;IAEH;;MAA4E;IAE5E;;;OAGG;IACH,0BAaC;IA4CC,4CAAuG;CAY1G;;;;8BAjP6B,iCAAiC"}
1
+ {"version":3,"file":"Thumb.d.ts","sourceRoot":"","sources":["Thumb.js"],"names":[],"mappings":"AAQA;IACE,eAAe;IACf,0BAA0B;IAE1B;;;OAGG;IACH,mBAAkB;IAElB,yBAAwB;IAExB,uBAAoB;IAKlB;;;;;;;+CACO,gBACX;mCAMU,gBAAU;6BAGd,2BAAoB;;wCAKpB,wCACa;;;;;;MAZd;IAGH,+BAA+B;IAC/B,oBADY,MAAM,gBAgBjB;IAED,6CAgBC;IAED,eAAe;IACf,2BA2CG;IAEH;;MAA4E;IAE5E;;;OAGG;IACH,0BAaC;IAED;;;OAGG;IACH,uBA0BC;IAgCC,4CAAuG;CAY1G;;;;+BA/M8B,+BAA+B"}
@@ -1,23 +1,14 @@
1
1
  //@ts-check
2
2
 
3
- import { UploaderBlock } from '../../abstract/UploaderBlock.js';
4
3
  import { createCdnUrl, createCdnUrlModifiers, createOriginalUrl } from '../../utils/cdn-utils.js';
4
+ import { FileItemConfig } from '../FileItem/FileItemConfig.js';
5
5
  import { fileCssBg } from '../svg-backgrounds/svg-backgrounds.js';
6
6
  import { debounce } from '../utils/debounce.js';
7
7
  import { generateThumb } from '../utils/resizeImage.js';
8
8
 
9
- export class Thumb extends UploaderBlock {
9
+ export class Thumb extends FileItemConfig {
10
10
  /** @private */
11
- _entrySubs = new Set();
12
-
13
- /** @private */
14
- _once = false;
15
-
16
- /**
17
- * @private
18
- * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
19
- */
20
- _entry = null;
11
+ _renderedGridOnce = false;
21
12
 
22
13
  /**
23
14
  * @private
@@ -27,49 +18,7 @@ export class Thumb extends UploaderBlock {
27
18
 
28
19
  _isIntersecting = false;
29
20
 
30
- /**
31
- * @private
32
- * @template {any[]} A
33
- * @template {(entry: import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData, ...args: A) => any} T
34
- * @param {T} fn
35
- * @returns {(...args: A) => ReturnType<T>}
36
- */
37
- _withEntry(fn) {
38
- const wrapperFn = /** @type {(...args: A) => ReturnType<T>} */ (
39
- (...args) => {
40
- const entry = this._entry;
41
- if (!entry) {
42
- console.warn('No entry found');
43
- return;
44
- }
45
- return fn(entry, ...args);
46
- }
47
- );
48
- return wrapperFn;
49
- }
50
-
51
- /**
52
- * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
53
- * @param {K} prop_
54
- * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler_
55
- */
56
- _subEntry = (prop_, handler_) =>
57
- this._withEntry(
58
- /**
59
- * @template {import('../../abstract/uploadEntrySchema.js').UploadEntryKeys} K
60
- * @param {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData} entry
61
- * @param {K} prop
62
- * @param {(value: import('../../abstract/uploadEntrySchema.js').UploadEntryData[K]) => void} handler
63
- */
64
- (entry, prop, handler) => {
65
- let sub = entry.subscribe(prop, (value) => {
66
- if (this.isConnected) {
67
- handler(value);
68
- }
69
- });
70
- this._entrySubs.add(sub);
71
- },
72
- )(prop_, handler_);
21
+ _firstViewMode = '';
73
22
 
74
23
  constructor() {
75
24
  super();
@@ -82,21 +31,19 @@ export class Thumb extends UploaderBlock {
82
31
  };
83
32
  }
84
33
 
85
- /**
86
- * Loads an image and sets it as the thumbnail.
87
- *
88
- * @param {string} imageUrl - The URL of the image to load.
89
- */
34
+ /** @param {string} imageUrl */
90
35
  loadImage(imageUrl) {
91
36
  return new Promise((resolve, reject) => {
92
37
  const img = new Image();
93
38
  img.onload = () => {
94
39
  this.set$({ thumbUrl: `url(${imageUrl})` });
95
40
  resolve(true);
41
+ img.remove();
96
42
  };
97
43
  img.onerror = () => {
98
44
  console.error('Failed to load image:', imageUrl);
99
45
  reject(new Error(`Failed to load image: ${imageUrl}`));
46
+ img.remove();
100
47
  };
101
48
  img.src = imageUrl;
102
49
  });
@@ -187,18 +134,16 @@ export class Thumb extends UploaderBlock {
187
134
  }
188
135
  }
189
136
 
190
- initCallback() {
191
- super.initCallback();
192
-
193
- this.defineAccessor('badgeIcon', (/** @type {string} */ val) => (this.$.badgeIcon = val));
194
-
195
- this.defineAccessor('uid', (/** @type {string} */ value) => {
196
- this.set$({ uid: value });
197
- });
198
-
199
- this.sub('uid', (value) => {
200
- this._entry = this.uploadCollection.read(value);
201
- });
137
+ /**
138
+ * @private
139
+ * @param {String} id
140
+ */
141
+ _handleEntryId(id) {
142
+ let entry = this.uploadCollection?.read(id);
143
+ this._entry = entry;
144
+ if (!entry) {
145
+ return;
146
+ }
202
147
 
203
148
  this._subEntry('fileInfo', (fileInfo) => {
204
149
  if (fileInfo?.isImage && this._isIntersecting) {
@@ -216,10 +161,32 @@ export class Thumb extends UploaderBlock {
216
161
  }
217
162
  });
218
163
 
164
+ if (this._isIntersecting) {
165
+ this._debouncedGenerateThumb();
166
+ }
167
+ }
168
+
169
+ initCallback() {
170
+ super.initCallback();
171
+
172
+ this._firstViewMode = this.cfg.filesViewMode;
173
+
174
+ this.defineAccessor('badgeIcon', (/** @type {string} */ val) => (this.$.badgeIcon = val));
175
+
176
+ this.defineAccessor('uid', (/** @type {string} */ value) => {
177
+ this.set$({ uid: value });
178
+ });
179
+
180
+ this.sub('uid', (uid) => {
181
+ this._handleEntryId(uid);
182
+ });
183
+
219
184
  this.subConfigValue('filesViewMode', (viewMode) => {
220
- if (!this._once && viewMode === 'grid') {
221
- this._debouncedGenerateThumb(true);
222
- this._once = true;
185
+ if (viewMode === 'grid' && !this._renderedGridOnce) {
186
+ if (this._firstViewMode === 'list') {
187
+ this._debouncedGenerateThumb(true);
188
+ }
189
+ this._renderedGridOnce = true;
223
190
  }
224
191
  });
225
192
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uploadcare/file-uploader",
3
- "version": "1.15.0-alpha.14",
3
+ "version": "1.15.0-alpha.16",
4
4
  "description": "Building blocks for Uploadcare products integration",
5
5
  "keywords": [
6
6
  "web components",