You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.6 KiB

7 months ago
5 months ago
7 months ago
5 months ago
7 months ago
5 months ago
7 months ago
  1. document.addEventListener("DOMContentLoaded", function () {
  2. const svgFile = document.getElementById("fileInput");
  3. // Event listener for file selection
  4. svgFile.addEventListener("change", function (event) {
  5. const file = event.target.files[0];
  6. if (file) {
  7. handleDotFileUpload(file);
  8. }
  9. });
  10. const textFile = document.getElementById("prismModelInput");
  11. textFile.addEventListener("change", function (event) {
  12. const file = event.target.files[0];
  13. if (file) {
  14. handleCodeFileUpload(file);
  15. }
  16. });
  17. const specs = document.getElementById("specificationsInput");
  18. specs.addEventListener("change", function (event) {
  19. const file = event.target.files[0];
  20. if (file) {
  21. handleSpecsFileUpload(file);
  22. }
  23. });
  24. // Function to handle file upload for editor
  25. function handleCodeFileUpload(file) {
  26. const reader = new FileReader();
  27. reader.onload = function (event) {
  28. const fileContents = event.target.result;
  29. editor.setValue(fileContents);
  30. };
  31. reader.onerror = function (event) {
  32. console.error("Error reading file:", event);
  33. };
  34. // Read the file as text
  35. reader.readAsText(file);
  36. }
  37. // Function to handle file upload for specs
  38. function handleSpecsFileUpload(file) {
  39. const reader = new FileReader();
  40. reader.onload = function (event) {
  41. const fileContents = event.target.result;
  42. editor2.setValue(fileContents);
  43. };
  44. reader.onerror = function (event) {
  45. console.error("Error reading file:", event);
  46. };
  47. // Read the file as text
  48. reader.readAsText(file);
  49. }
  50. // Function to handle file upload for SVG
  51. function handleDotFileUpload(file) {
  52. const reader = new FileReader();
  53. reader.onload = function (event) {
  54. const fileContents = event.target.result;
  55. Viz.instance().then(function (viz) {
  56. var svg = viz.renderSVGElement(fileContents);
  57. graphFunction(svg);
  58. updateTextarea(getActiveTabContent());
  59. });
  60. };
  61. reader.onerror = function (event) {
  62. console.error("Error reading file:", event);
  63. };
  64. // Read the file as text
  65. reader.readAsText(file);
  66. }
  67. // Function for converting input to text format and automatically downloading the content
  68. (function () {
  69. // Declare a variable for the text file URL
  70. var textFile = null;
  71. // Function to create the text file and return its URL
  72. var makeTextFile = function (text) {
  73. // Create a Blob from the text
  74. var data = new Blob([text], { type: "text/plain" });
  75. // Revoke any previous object URL to avoid memory leaks
  76. if (textFile !== null) {
  77. window.URL.revokeObjectURL(textFile);
  78. }
  79. // Create a new object URL for the Blob and return it
  80. textFile = window.URL.createObjectURL(data);
  81. return textFile;
  82. };
  83. var downloadEditorText = document.getElementById("downloadFirstEditor");
  84. var downloadSpecifications = document.getElementById("downloadSpecifications");
  85. var input1 = editor;
  86. var input2 = editor2;
  87. // Add an event listener to the button for the click event
  88. downloadEditorText.addEventListener("click", function () {
  89. // Check if the editor is empty
  90. if (!editor.getValue()) {
  91. Toastify({
  92. text: "Editor field empty",
  93. duration: 3000,
  94. close: true,
  95. gravity: "top",
  96. }).showToast();
  97. } else {
  98. // Create the text file URL from the input editor value
  99. var fileURL = makeTextFile(input1.getValue());
  100. // Create a temporary link element
  101. var tempLink = document.createElement("a");
  102. tempLink.setAttribute("download", "prism.txt");
  103. // Set the link's href attribute to the file URL
  104. tempLink.href = fileURL;
  105. // Append the link to the document body (required for the click event to work in some browsers)
  106. document.body.appendChild(tempLink);
  107. tempLink.click();
  108. // Remove the link from the document body after the download
  109. document.body.removeChild(tempLink);
  110. }
  111. });
  112. downloadSpecifications.addEventListener("click", function () {
  113. if (!editor2.getValue()) {
  114. Toastify({
  115. text: "Specifications field empty",
  116. duration: 3000,
  117. close: true,
  118. gravity: "top",
  119. }).showToast();
  120. } else {
  121. var fileURL = makeTextFile(input2.getValue());
  122. var tempLink = document.createElement("a");
  123. tempLink.setAttribute("download", "specifications.txt");
  124. tempLink.href = fileURL;
  125. document.body.appendChild(tempLink);
  126. tempLink.click();
  127. document.body.removeChild(tempLink);
  128. }
  129. });
  130. })();
  131. });