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

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,6 +1,8 @@
1
1
  export class Thumb extends UploaderBlock {
2
2
  /** @private */
3
3
  private _entrySubs;
4
+ /** @private */
5
+ private _once;
4
6
  /**
5
7
  * @private
6
8
  * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
@@ -44,7 +46,13 @@ export class Thumb extends UploaderBlock {
44
46
  '*historyBack': null;
45
47
  '*closeModal': () => void;
46
48
  };
47
- _calculateThumbSize(): number;
49
+ /**
50
+ * Loads an image and sets it as the thumbnail.
51
+ *
52
+ * @param {string} imageUrl - The URL of the image to load.
53
+ */
54
+ loadImage(imageUrl: string): Promise<any>;
55
+ _calculateThumbSize(force?: boolean): number;
48
56
  /** @private */
49
57
  private _generateThumbnail;
50
58
  _debouncedGenerateThumb: ((...args: any[]) => Promise<void>) & {
@@ -1 +1 @@
1
- {"version":3,"file":"Thumb.d.ts","sourceRoot":"","sources":["Thumb.js"],"names":[],"mappings":"AAQA;IACE,eAAe;IACf,mBAAuB;IAEvB;;;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;;;;;;;+CAnDI,gBAAU;mCAOf,gBAAQ;6BAAmF,2BACvF;;wCAI0C,wCAC3C;;;;;;MA2CD;IAGH,8BAYC;IAED,eAAe;IACf,2BAuCG;IAEH;;MAA4E;IAE5E;;;OAGG;IACH,0BAaC;IAqCC,4CAAuG;CAY1G;;;;8BA3M6B,iCAAiC"}
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"}
@@ -10,6 +10,9 @@ export class Thumb extends UploaderBlock {
10
10
  /** @private */
11
11
  _entrySubs = new Set();
12
12
 
13
+ /** @private */
14
+ _once = false;
15
+
13
16
  /**
14
17
  * @private
15
18
  * @type {import('../../abstract/uploadEntrySchema.js').UploadEntryTypedData | null}
@@ -79,7 +82,31 @@ export class Thumb extends UploaderBlock {
79
82
  };
80
83
  }
81
84
 
82
- _calculateThumbSize() {
85
+ /**
86
+ * Loads an image and sets it as the thumbnail.
87
+ *
88
+ * @param {string} imageUrl - The URL of the image to load.
89
+ */
90
+ loadImage(imageUrl) {
91
+ return new Promise((resolve, reject) => {
92
+ const img = new Image();
93
+ img.onload = () => {
94
+ this.set$({ thumbUrl: `url(${imageUrl})` });
95
+ resolve(true);
96
+ };
97
+ img.onerror = () => {
98
+ console.error('Failed to load image:', imageUrl);
99
+ reject(new Error(`Failed to load image: ${imageUrl}`));
100
+ };
101
+ img.src = imageUrl;
102
+ });
103
+ }
104
+
105
+ _calculateThumbSize(force = false) {
106
+ if (force) {
107
+ this._thumbRect = this.getBoundingClientRect();
108
+ }
109
+
83
110
  let size = Math.max(
84
111
  parseInt(String(this?._thumbRect?.height || 0)),
85
112
  parseInt(String(this?._thumbRect?.width || 0)),
@@ -94,11 +121,11 @@ export class Thumb extends UploaderBlock {
94
121
  }
95
122
 
96
123
  /** @private */
97
- _generateThumbnail = this._withEntry(async (entry) => {
124
+ _generateThumbnail = this._withEntry(async (entry, force = false) => {
98
125
  const fileInfo = entry.getValue('fileInfo');
99
126
  const isImage = entry.getValue('isImage');
100
127
  const uuid = entry.getValue('uuid');
101
- let size = this._calculateThumbSize();
128
+ let size = this._calculateThumbSize(force);
102
129
 
103
130
  if (fileInfo && isImage && uuid) {
104
131
  let thumbUrl = await this.proxyUrl(
@@ -107,11 +134,15 @@ export class Thumb extends UploaderBlock {
107
134
  createCdnUrlModifiers(entry.getValue('cdnUrlModifiers'), `scale_crop/${size}x${size}/center`),
108
135
  ),
109
136
  );
110
- let currentThumbUrl = entry.getValue('thumbUrl');
111
- if (currentThumbUrl !== thumbUrl) {
112
- entry.setValue('thumbUrl', thumbUrl);
113
- currentThumbUrl?.startsWith('blob:') && URL.revokeObjectURL(currentThumbUrl);
114
- }
137
+
138
+ this.loadImage(thumbUrl).then(() => {
139
+ let currentThumbUrl = entry.getValue('thumbUrl');
140
+ if (currentThumbUrl !== thumbUrl) {
141
+ entry.setValue('thumbUrl', thumbUrl);
142
+ currentThumbUrl?.startsWith('blob:') && URL.revokeObjectURL(currentThumbUrl);
143
+ }
144
+ });
145
+
115
146
  return;
116
147
  }
117
148
 
@@ -185,6 +216,13 @@ export class Thumb extends UploaderBlock {
185
216
  }
186
217
  });
187
218
 
219
+ this.subConfigValue('filesViewMode', (viewMode) => {
220
+ if (!this._once && viewMode === 'grid') {
221
+ this._debouncedGenerateThumb(true);
222
+ this._once = true;
223
+ }
224
+ });
225
+
188
226
  this.setAttribute('role', 'img');
189
227
  }
190
228
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uploadcare/file-uploader",
3
- "version": "1.15.0-alpha.13",
3
+ "version": "1.15.0-alpha.14",
4
4
  "description": "Building blocks for Uploadcare products integration",
5
5
  "keywords": [
6
6
  "web components",