@rails/actiontext 8.0.300 → 8.1.0-rc1

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.
@@ -672,7 +672,7 @@ class DirectUploadController {
672
672
  }));
673
673
  }
674
674
  uploadRequestDidProgress(event) {
675
- const progress = event.loaded / event.total * 100;
675
+ const progress = event.loaded / event.total * 90;
676
676
  if (progress) {
677
677
  this.dispatch("progress", {
678
678
  progress: progress
@@ -707,6 +707,42 @@ class DirectUploadController {
707
707
  xhr: xhr
708
708
  });
709
709
  xhr.upload.addEventListener("progress", (event => this.uploadRequestDidProgress(event)));
710
+ xhr.upload.addEventListener("loadend", (() => {
711
+ this.simulateResponseProgress(xhr);
712
+ }));
713
+ }
714
+ simulateResponseProgress(xhr) {
715
+ let progress = 90;
716
+ const startTime = Date.now();
717
+ const updateProgress = () => {
718
+ const elapsed = Date.now() - startTime;
719
+ const estimatedResponseTime = this.estimateResponseTime();
720
+ const responseProgress = Math.min(elapsed / estimatedResponseTime, 1);
721
+ progress = 90 + responseProgress * 9;
722
+ this.dispatch("progress", {
723
+ progress: progress
724
+ });
725
+ if (xhr.readyState !== XMLHttpRequest.DONE && progress < 99) {
726
+ requestAnimationFrame(updateProgress);
727
+ }
728
+ };
729
+ xhr.addEventListener("loadend", (() => {
730
+ this.dispatch("progress", {
731
+ progress: 100
732
+ });
733
+ }));
734
+ requestAnimationFrame(updateProgress);
735
+ }
736
+ estimateResponseTime() {
737
+ const fileSize = this.file.size;
738
+ const MB = 1024 * 1024;
739
+ if (fileSize < MB) {
740
+ return 1e3;
741
+ } else if (fileSize < 10 * MB) {
742
+ return 2e3;
743
+ } else {
744
+ return 3e3 + fileSize / MB * 50;
745
+ }
710
746
  }
711
747
  }
712
748
 
@@ -846,31 +882,69 @@ function autostart() {
846
882
  setTimeout(autostart, 1);
847
883
 
848
884
  class AttachmentUpload {
849
- constructor(attachment, element) {
885
+ constructor(attachment, element, file = attachment.file) {
850
886
  this.attachment = attachment;
851
887
  this.element = element;
852
- this.directUpload = new DirectUpload(attachment.file, this.directUploadUrl, this);
888
+ this.directUpload = new DirectUpload(file, this.directUploadUrl, this);
889
+ this.file = file;
853
890
  }
854
891
  start() {
855
- this.directUpload.create(this.directUploadDidComplete.bind(this));
856
- this.dispatch("start");
892
+ return new Promise(((resolve, reject) => {
893
+ this.directUpload.create(((error, attributes) => this.directUploadDidComplete(error, attributes, resolve, reject)));
894
+ this.dispatch("start");
895
+ }));
857
896
  }
858
897
  directUploadWillStoreFileWithXHR(xhr) {
859
898
  xhr.upload.addEventListener("progress", (event => {
860
- const progress = event.loaded / event.total * 100;
861
- this.attachment.setUploadProgress(progress);
899
+ const progress = event.loaded / event.total * 90;
862
900
  if (progress) {
863
901
  this.dispatch("progress", {
864
902
  progress: progress
865
903
  });
866
904
  }
867
905
  }));
906
+ xhr.upload.addEventListener("loadend", (() => {
907
+ this.simulateResponseProgress(xhr);
908
+ }));
868
909
  }
869
- directUploadDidComplete(error, attributes) {
910
+ simulateResponseProgress(xhr) {
911
+ let progress = 90;
912
+ const startTime = Date.now();
913
+ const updateProgress = () => {
914
+ const elapsed = Date.now() - startTime;
915
+ const estimatedResponseTime = this.estimateResponseTime();
916
+ const responseProgress = Math.min(elapsed / estimatedResponseTime, 1);
917
+ progress = 90 + responseProgress * 9;
918
+ this.dispatch("progress", {
919
+ progress: progress
920
+ });
921
+ if (xhr.readyState !== XMLHttpRequest.DONE && progress < 99) {
922
+ requestAnimationFrame(updateProgress);
923
+ }
924
+ };
925
+ xhr.addEventListener("loadend", (() => {
926
+ this.dispatch("progress", {
927
+ progress: 100
928
+ });
929
+ }));
930
+ requestAnimationFrame(updateProgress);
931
+ }
932
+ estimateResponseTime() {
933
+ const fileSize = this.file.size;
934
+ const MB = 1024 * 1024;
935
+ if (fileSize < MB) {
936
+ return 1e3;
937
+ } else if (fileSize < 10 * MB) {
938
+ return 2e3;
939
+ } else {
940
+ return 3e3 + fileSize / MB * 50;
941
+ }
942
+ }
943
+ directUploadDidComplete(error, attributes, resolve, reject) {
870
944
  if (error) {
871
- this.dispatchError(error);
945
+ this.dispatchError(error, reject);
872
946
  } else {
873
- this.attachment.setAttributes({
947
+ resolve({
874
948
  sgid: attributes.attachable_sgid,
875
949
  url: this.createBlobUrl(attributes.signed_id, attributes.filename)
876
950
  });
@@ -886,12 +960,12 @@ class AttachmentUpload {
886
960
  detail: detail
887
961
  });
888
962
  }
889
- dispatchError(error) {
963
+ dispatchError(error, reject) {
890
964
  const event = this.dispatch("error", {
891
965
  error: error
892
966
  });
893
967
  if (!event.defaultPrevented) {
894
- alert(error);
968
+ reject(error);
895
969
  }
896
970
  }
897
971
  get directUploadUrl() {
@@ -905,7 +979,11 @@ class AttachmentUpload {
905
979
  addEventListener("trix-attachment-add", (event => {
906
980
  const {attachment: attachment, target: target} = event;
907
981
  if (attachment.file) {
908
- const upload = new AttachmentUpload(attachment, target);
909
- upload.start();
982
+ const upload = new AttachmentUpload(attachment, target, attachment.file);
983
+ const onProgress = event => attachment.setUploadProgress(event.detail.progress);
984
+ target.addEventListener("direct-upload:progress", onProgress);
985
+ upload.start().then((attributes => attachment.setAttributes(attributes))).catch((error => alert(error))).finally((() => target.removeEventListener("direct-upload:progress", onProgress)));
910
986
  }
911
987
  }));
988
+
989
+ export { AttachmentUpload };
@@ -1,6 +1,7 @@
1
- (function(factory) {
2
- typeof define === "function" && define.amd ? define(factory) : factory();
3
- })((function() {
1
+ (function(global, factory) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
3
+ factory(global.ActionText = {}));
4
+ })(this, (function(exports) {
4
5
  "use strict";
5
6
  var sparkMd5 = {
6
7
  exports: {}
@@ -661,7 +662,7 @@
661
662
  }));
662
663
  }
663
664
  uploadRequestDidProgress(event) {
664
- const progress = event.loaded / event.total * 100;
665
+ const progress = event.loaded / event.total * 90;
665
666
  if (progress) {
666
667
  this.dispatch("progress", {
667
668
  progress: progress
@@ -696,6 +697,42 @@
696
697
  xhr: xhr
697
698
  });
698
699
  xhr.upload.addEventListener("progress", (event => this.uploadRequestDidProgress(event)));
700
+ xhr.upload.addEventListener("loadend", (() => {
701
+ this.simulateResponseProgress(xhr);
702
+ }));
703
+ }
704
+ simulateResponseProgress(xhr) {
705
+ let progress = 90;
706
+ const startTime = Date.now();
707
+ const updateProgress = () => {
708
+ const elapsed = Date.now() - startTime;
709
+ const estimatedResponseTime = this.estimateResponseTime();
710
+ const responseProgress = Math.min(elapsed / estimatedResponseTime, 1);
711
+ progress = 90 + responseProgress * 9;
712
+ this.dispatch("progress", {
713
+ progress: progress
714
+ });
715
+ if (xhr.readyState !== XMLHttpRequest.DONE && progress < 99) {
716
+ requestAnimationFrame(updateProgress);
717
+ }
718
+ };
719
+ xhr.addEventListener("loadend", (() => {
720
+ this.dispatch("progress", {
721
+ progress: 100
722
+ });
723
+ }));
724
+ requestAnimationFrame(updateProgress);
725
+ }
726
+ estimateResponseTime() {
727
+ const fileSize = this.file.size;
728
+ const MB = 1024 * 1024;
729
+ if (fileSize < MB) {
730
+ return 1e3;
731
+ } else if (fileSize < 10 * MB) {
732
+ return 2e3;
733
+ } else {
734
+ return 3e3 + fileSize / MB * 50;
735
+ }
699
736
  }
700
737
  }
701
738
  const inputSelector = "input[type=file][data-direct-upload-url]:not([disabled])";
@@ -819,31 +856,69 @@
819
856
  }
820
857
  setTimeout(autostart, 1);
821
858
  class AttachmentUpload {
822
- constructor(attachment, element) {
859
+ constructor(attachment, element, file = attachment.file) {
823
860
  this.attachment = attachment;
824
861
  this.element = element;
825
- this.directUpload = new DirectUpload(attachment.file, this.directUploadUrl, this);
862
+ this.directUpload = new DirectUpload(file, this.directUploadUrl, this);
863
+ this.file = file;
826
864
  }
827
865
  start() {
828
- this.directUpload.create(this.directUploadDidComplete.bind(this));
829
- this.dispatch("start");
866
+ return new Promise(((resolve, reject) => {
867
+ this.directUpload.create(((error, attributes) => this.directUploadDidComplete(error, attributes, resolve, reject)));
868
+ this.dispatch("start");
869
+ }));
830
870
  }
831
871
  directUploadWillStoreFileWithXHR(xhr) {
832
872
  xhr.upload.addEventListener("progress", (event => {
833
- const progress = event.loaded / event.total * 100;
834
- this.attachment.setUploadProgress(progress);
873
+ const progress = event.loaded / event.total * 90;
835
874
  if (progress) {
836
875
  this.dispatch("progress", {
837
876
  progress: progress
838
877
  });
839
878
  }
840
879
  }));
880
+ xhr.upload.addEventListener("loadend", (() => {
881
+ this.simulateResponseProgress(xhr);
882
+ }));
883
+ }
884
+ simulateResponseProgress(xhr) {
885
+ let progress = 90;
886
+ const startTime = Date.now();
887
+ const updateProgress = () => {
888
+ const elapsed = Date.now() - startTime;
889
+ const estimatedResponseTime = this.estimateResponseTime();
890
+ const responseProgress = Math.min(elapsed / estimatedResponseTime, 1);
891
+ progress = 90 + responseProgress * 9;
892
+ this.dispatch("progress", {
893
+ progress: progress
894
+ });
895
+ if (xhr.readyState !== XMLHttpRequest.DONE && progress < 99) {
896
+ requestAnimationFrame(updateProgress);
897
+ }
898
+ };
899
+ xhr.addEventListener("loadend", (() => {
900
+ this.dispatch("progress", {
901
+ progress: 100
902
+ });
903
+ }));
904
+ requestAnimationFrame(updateProgress);
905
+ }
906
+ estimateResponseTime() {
907
+ const fileSize = this.file.size;
908
+ const MB = 1024 * 1024;
909
+ if (fileSize < MB) {
910
+ return 1e3;
911
+ } else if (fileSize < 10 * MB) {
912
+ return 2e3;
913
+ } else {
914
+ return 3e3 + fileSize / MB * 50;
915
+ }
841
916
  }
842
- directUploadDidComplete(error, attributes) {
917
+ directUploadDidComplete(error, attributes, resolve, reject) {
843
918
  if (error) {
844
- this.dispatchError(error);
919
+ this.dispatchError(error, reject);
845
920
  } else {
846
- this.attachment.setAttributes({
921
+ resolve({
847
922
  sgid: attributes.attachable_sgid,
848
923
  url: this.createBlobUrl(attributes.signed_id, attributes.filename)
849
924
  });
@@ -859,12 +934,12 @@
859
934
  detail: detail
860
935
  });
861
936
  }
862
- dispatchError(error) {
937
+ dispatchError(error, reject) {
863
938
  const event = this.dispatch("error", {
864
939
  error: error
865
940
  });
866
941
  if (!event.defaultPrevented) {
867
- alert(error);
942
+ reject(error);
868
943
  }
869
944
  }
870
945
  get directUploadUrl() {
@@ -877,8 +952,14 @@
877
952
  addEventListener("trix-attachment-add", (event => {
878
953
  const {attachment: attachment, target: target} = event;
879
954
  if (attachment.file) {
880
- const upload = new AttachmentUpload(attachment, target);
881
- upload.start();
955
+ const upload = new AttachmentUpload(attachment, target, attachment.file);
956
+ const onProgress = event => attachment.setUploadProgress(event.detail.progress);
957
+ target.addEventListener("direct-upload:progress", onProgress);
958
+ upload.start().then((attributes => attachment.setAttributes(attributes))).catch((error => alert(error))).finally((() => target.removeEventListener("direct-upload:progress", onProgress)));
882
959
  }
883
960
  }));
961
+ exports.AttachmentUpload = AttachmentUpload;
962
+ Object.defineProperty(exports, "__esModule", {
963
+ value: true
964
+ });
884
965
  }));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rails/actiontext",
3
- "version": "8.0.300",
3
+ "version": "8.1.0-rc1",
4
4
  "description": "Edit and display rich text in Rails applications",
5
5
  "module": "app/assets/javascripts/actiontext.esm.js",
6
6
  "main": "app/assets/javascripts/actiontext.js",
@@ -22,7 +22,7 @@
22
22
  ],
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
- "@rails/activestorage": ">= 8.0.0-alpha"
25
+ "@rails/activestorage": ">= 8.1.0-alpha"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "trix": "^2.0.0"