@tft/crispr-forms 13.0.3 → 13.0.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.
@@ -1127,7 +1127,6 @@ function convertBytesToMb(bytes) {
1127
1127
 
1128
1128
  const ImageUploadFieldMixin = crisprControlMixin(CrisprFieldComponent);
1129
1129
  class ImageUploadFieldComponent extends ImageUploadFieldMixin {
1130
- // ngControl: NgControl = null;
1131
1130
  constructor(ngControl, domSanitizer, cdr) {
1132
1131
  super();
1133
1132
  this.ngControl = ngControl;
@@ -1148,24 +1147,28 @@ class ImageUploadFieldComponent extends ImageUploadFieldMixin {
1148
1147
  minCompressionThresholdMb: .6,
1149
1148
  useWebWorker: true,
1150
1149
  };
1150
+ /**
1151
+ * The url mapped from initial input value, consumers can us mapInputValueToUrl in config to transform their input value into a src url for the image element
1152
+ */
1153
+ // inputValueUrl: string | undefined;
1151
1154
  this.inputImageFileSubject = new Subject();
1152
1155
  this.inputImageFile$ = this.inputImageFileSubject.asObservable().pipe(shareReplay(1));
1153
1156
  this.compressedFile$ = this.inputImageFile$.pipe(switchMap(async (file) => {
1154
- if (!file)
1157
+ if (!file || !(file instanceof File))
1155
1158
  return null;
1156
- if (typeof file === 'string')
1157
- return file;
1158
1159
  const { compressImage, targetCompressedImageFileSizeMb, minCompressionThresholdMb, maxWidthOrHeight, useWebWorker } = this.config;
1159
1160
  const fileSizeMb = convertBytesToMb(file.size);
1160
- const shouldCompress = compressImage && targetCompressedImageFileSizeMb < fileSizeMb && minCompressionThresholdMb < fileSizeMb;
1161
+ const shouldCompress = compressImage
1162
+ && targetCompressedImageFileSizeMb < fileSizeMb
1163
+ && minCompressionThresholdMb < fileSizeMb;
1161
1164
  if (shouldCompress) {
1162
1165
  const options = {
1163
1166
  // Use maxWidthOrHeight if it was provided, else use target compressed size
1164
1167
  ...(maxWidthOrHeight ? { maxWidthOrHeight } : { maxSizeMB: targetCompressedImageFileSizeMb }),
1165
1168
  useWebWorker,
1166
1169
  maxIterations: 30,
1167
- onProgress: (p) => {
1168
- this.progress = p;
1170
+ onProgress: (progress) => {
1171
+ this.progress = progress;
1169
1172
  this.cdr.detectChanges();
1170
1173
  }
1171
1174
  };
@@ -1173,23 +1176,27 @@ class ImageUploadFieldComponent extends ImageUploadFieldMixin {
1173
1176
  }
1174
1177
  else
1175
1178
  return file;
1176
- }), map((compressedFileOrString) => {
1177
- if (!compressedFileOrString || typeof compressedFileOrString === 'string')
1178
- return compressedFileOrString;
1179
- const { type, name, lastModified } = compressedFileOrString;
1180
- return new File([compressedFileOrString], this.config.fileName || name, {
1179
+ }), map((compressedFile) => {
1180
+ if (!compressedFile)
1181
+ return compressedFile;
1182
+ const { type, name, lastModified } = compressedFile;
1183
+ return new File([compressedFile], this.config.fileName || name, {
1181
1184
  type,
1182
1185
  lastModified
1183
1186
  });
1184
1187
  }), tap(file => {
1185
- this.control.setValue(file);
1188
+ // only set the value if there is a file because resetToInitialValue sets the control value
1189
+ // to initial value and clears the compressed image, so setting it to null here would undo that
1190
+ if (file) {
1191
+ this.control.setValue(file);
1192
+ }
1186
1193
  if (file || this.value) {
1187
1194
  this.control.markAsTouched();
1188
1195
  setTimeout(() => {
1189
1196
  this.cdr.detectChanges();
1190
1197
  }, 100);
1191
1198
  }
1192
- }), map(file => typeof file === 'string' ? null : file), shareReplay(1));
1199
+ }), shareReplay(1));
1193
1200
  this.compressedImageUrlString$ = this.compressedFile$.pipe(switchMap(async (file) => {
1194
1201
  if (!file)
1195
1202
  return null;
@@ -1205,11 +1212,18 @@ class ImageUploadFieldComponent extends ImageUploadFieldMixin {
1205
1212
  }
1206
1213
  ngOnInit() {
1207
1214
  super.ngOnInit();
1215
+ // this.inputValueUrl = this.value && this.config.mapInputValueToUrl?.(this.value) || this.value;
1208
1216
  this.imageSrcUrl$ = combineLatest([
1209
1217
  this.compressedImageUrlString$.pipe(startWith(null)),
1210
- this.control.valueChanges.pipe(startWith(this.value))
1218
+ this.control.valueChanges.pipe(startWith(this.value), map((controlValue) => {
1219
+ if (!controlValue)
1220
+ return null;
1221
+ if (controlValue instanceof File)
1222
+ return null;
1223
+ return this.config.mapInputValueToUrl?.(controlValue) || controlValue;
1224
+ }))
1211
1225
  ]).pipe(map(([compressedImageUrlString, controlUrl]) => {
1212
- return compressedImageUrlString || typeof controlUrl === 'string' && controlUrl || null;
1226
+ return compressedImageUrlString || controlUrl || null;
1213
1227
  }));
1214
1228
  this.disabled$ = this.config.disabledCallback
1215
1229
  ? this.config.disabledCallback(this.group, this.config)
@@ -1218,6 +1232,10 @@ class ImageUploadFieldComponent extends ImageUploadFieldMixin {
1218
1232
  filesChanged(files) {
1219
1233
  if (files && files.length > 0) {
1220
1234
  const imageFile = files[0];
1235
+ // We clear the control here so that it is invalid while compressing
1236
+ // the input pipeline will take care of resetting it
1237
+ this.control.patchValue(null);
1238
+ // this.control.setErrors({compressing: {loadingMessage: 'Please wait while file compresses'}});
1221
1239
  this.control.markAsTouched();
1222
1240
  this.inputImageFileSubject.next(imageFile);
1223
1241
  }
@@ -1230,8 +1248,8 @@ class ImageUploadFieldComponent extends ImageUploadFieldMixin {
1230
1248
  this.isUploaded = false;
1231
1249
  }
1232
1250
  resetToInitialValue(initialValue) {
1233
- this.inputImageFileSubject.next(initialValue);
1234
- // this.fileInputRef.nativeElement.value = initialValue
1251
+ this.inputImageFileSubject.next(null);
1252
+ this.control.patchValue(initialValue);
1235
1253
  this.isUploaded = false;
1236
1254
  }
1237
1255
  writeValue(value) { }
@@ -1310,7 +1328,6 @@ function maxFileSizeValidator(maxFileSize) {
1310
1328
  const actualFileSize = +convertBytesToMb(controlValue.size).toPrecision(3);
1311
1329
  if (maxFileSize > actualFileSize)
1312
1330
  return null;
1313
- // control.setErrors({maxFileSize: { maxFileSize, actualFileSize }} )
1314
1331
  return { maxFileSize: { maxFileSize, actualFileSize } };
1315
1332
  };
1316
1333
  }