stringTools.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { NodeMaterialBlockConnectionPointTypes } from 'babylonjs/Materials/Node/nodeMaterialBlockConnectionPointTypes';
  2. export class StringTools {
  3. /**
  4. * Gets the base math type of node material block connection point.
  5. * @param type Type to parse.
  6. */
  7. public static GetBaseType(type: NodeMaterialBlockConnectionPointTypes): string {
  8. switch (type) {
  9. case NodeMaterialBlockConnectionPointTypes.Vector3OrColor3:
  10. case NodeMaterialBlockConnectionPointTypes.Vector4OrColor4:
  11. case NodeMaterialBlockConnectionPointTypes.Vector3OrVector4:
  12. case NodeMaterialBlockConnectionPointTypes.Vector3OrColor3OrVector4OrColor4:
  13. return "Vector";
  14. case NodeMaterialBlockConnectionPointTypes.Color3:
  15. case NodeMaterialBlockConnectionPointTypes.Color3OrColor4:
  16. case NodeMaterialBlockConnectionPointTypes.Color4: {
  17. return "Color";
  18. }
  19. default: {
  20. return NodeMaterialBlockConnectionPointTypes[type];
  21. }
  22. }
  23. }
  24. /**
  25. * Download a string into a file that will be saved locally by the browser
  26. * @param content defines the string to download locally as a file
  27. */
  28. public static DownloadAsFile(content: string, filename: string) {
  29. let blob = new Blob([content],
  30. {
  31. type: "application/octet-stream"
  32. });
  33. if (window.navigator.msSaveOrOpenBlob) {
  34. window.navigator.msSaveOrOpenBlob(blob, filename);
  35. return;
  36. }
  37. var file = new Blob([content], { type: "application/octet-stream" });
  38. var reader = new FileReader();
  39. reader.onload = function(e) {
  40. var bdata = btoa(reader.result as string);
  41. var datauri = 'data:text/plain;base64,' + bdata;
  42. setTimeout(() => {
  43. let link = document.createElement("a");
  44. link.setAttribute("href", datauri);
  45. link.setAttribute("download", filename);
  46. link.target = "_self";
  47. link.style.visibility = 'hidden';
  48. document.body.appendChild(link);
  49. link.click();
  50. setTimeout(function() {
  51. document.body.removeChild(link);
  52. }, 0);
  53. }, 10);
  54. };
  55. reader.readAsBinaryString(file);
  56. }
  57. }