stringTools.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  2. export class StringTools {
  3. /*
  4. * Based on FileSaver.js
  5. * A saveAs() FileSaver implementation.
  6. *
  7. * By Eli Grey, http://eligrey.com
  8. *
  9. * License : https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md (MIT)
  10. * source : http://purl.eligrey.com/github/FileSaver.js
  11. */
  12. private static _SaveAs(blob: Blob, name: string, document: HTMLDocument) {
  13. if ('download' in HTMLAnchorElement.prototype) {
  14. var URL = window.URL || window.webkitURL;
  15. var a = document.createElement('a');
  16. a.download = name;
  17. a.rel = 'noopener'; // tabnabbing
  18. a.href = URL.createObjectURL(blob)
  19. setTimeout(() => { URL.revokeObjectURL(a.href) }, 4E4) // 40s
  20. setTimeout(() => { this._Click(a, document) }, 0);
  21. return;
  22. }
  23. // Open a popup immediately do go around popup blocker
  24. // Mostly only available on user interaction and the fileReader is async so...
  25. var popup = open('', '_blank');
  26. if (popup) {
  27. popup.document.title = popup.document.body.innerText = 'downloading...';
  28. }
  29. var force = blob.type === 'application/octet-stream';
  30. var isSafari = /constructor/i.test((window as any).HTMLElement) || (window as any).safari;
  31. var isChromeIOS = /CriOS\/[\d]+/.test(navigator.userAgent);
  32. if ((isChromeIOS || (force && isSafari)) && typeof FileReader !== 'undefined') {
  33. // Safari doesn't allow downloading of blob URLs
  34. var reader = new FileReader();
  35. reader.onloadend = () => {
  36. var url:any = reader.result;
  37. url = isChromeIOS ? url : url.replace(/^data:[^;]*;/, 'data:attachment/file;');
  38. if (popup) {
  39. popup.location.href = url;
  40. }
  41. else {
  42. location = url;
  43. }
  44. popup = null;
  45. }
  46. reader.readAsDataURL(blob);
  47. } else {
  48. var URL = window.URL || window.webkitURL
  49. var url = URL.createObjectURL(blob)
  50. if (popup) {
  51. popup.location.href = url;
  52. }
  53. else {
  54. location.href = url;
  55. }
  56. popup = null;
  57. setTimeout(function () { URL.revokeObjectURL(url) }, 4E4);
  58. }
  59. }
  60. private static _Click(node: HTMLElement, document: HTMLDocument) {
  61. try {
  62. node.dispatchEvent(new MouseEvent('click'))
  63. } catch (e) {
  64. var evt = document.createEvent('MouseEvents');
  65. evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80, 20, false, false, false, false, 0, null);
  66. node.dispatchEvent(evt);
  67. }
  68. }
  69. /**
  70. * Gets the base math type of node material block connection point.
  71. * @param type Type to parse.
  72. */
  73. public static GetBaseType(type: NodeMaterialBlockConnectionPointTypes): string {
  74. return NodeMaterialBlockConnectionPointTypes[type];
  75. }
  76. /**
  77. * Download a string into a file that will be saved locally by the browser
  78. * @param content defines the string to download locally as a file
  79. */
  80. public static DownloadAsFile(document: HTMLDocument, content: string, filename: string) {
  81. let blob = new Blob([content],
  82. {
  83. type: "application/octet-stream"
  84. });
  85. this._SaveAs(blob, filename, document);
  86. }
  87. }