index.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>Markdown Editor</title>
  5. <script
  6. src="https://code.jquery.com/jquery-3.7.0.min.js"
  7. integrity="sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g="
  8. crossorigin="anonymous"></script>
  9. <link rel="stylesheet" href="script/mde.css">
  10. <script src="script/mde.js"></script>
  11. <script src="../upld.js"></script>
  12. <style>
  13. html, body{
  14. background-color:white;
  15. height: calc(100% - 80px);
  16. }
  17. #maintext{
  18. height:calc(100% - 1em) !important;
  19. }
  20. #statusText{
  21. position: fixed;
  22. bottom: 0.4em;
  23. left: 0.4em;
  24. color: rgb(179, 179, 179);
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <textarea id="maintext"></textarea>
  30. <p id="statusText">Starting Up</p>
  31. <script>
  32. var simplemde; //SimpleMDE editor object
  33. var filepath = ""; //Current editing filepath
  34. var file = "";
  35. var originalTitle = "MDEditor";
  36. var lastSaveContent = ""; //Content for last saved content
  37. if (window.location.hash.length > 1){
  38. file = window.location.hash.substr(1);
  39. file = JSON.parse(decodeURIComponent(file));
  40. console.log("file Loaded", file);
  41. }
  42. if (typeof(file) == "object"){
  43. //Set the window name
  44. document.title = ("MDEditor - " + file.filename);
  45. originalTitle = "MDEditor - " + file.filename;
  46. //Check if there are more than 1 text files to be opened. If yes, open new windows.
  47. filepath = file.filepath;
  48. //Check if this is json. If yes, parse it to string before displaying to prevent the [object Object] bug
  49. var ext = filepath.split(".").pop();
  50. var isJson = false;
  51. if (ext == "json"){
  52. isJson = true;
  53. }
  54. //Load the file into the textarea
  55. $.get("/api/fs/download?file=" + file.filepath,function(data){
  56. if (isJson){
  57. data = JSON.stringify(data);
  58. }
  59. $("#statusText").text("File loaded: " + file.filepath);
  60. $("#maintext").text(data);
  61. //Load Markdown Editor
  62. simplemde = new SimpleMDE({
  63. autofocus: true,
  64. element: document.getElementById("maintext"),
  65. forceSync: true,
  66. insertTexts: {
  67. horizontalRule: ["", "\n\n-----\n\n"],
  68. image: ["![](http://", ")"],
  69. link: ["[", "](http://)"],
  70. table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n"],
  71. },
  72. placeholder: "(Empty file)",
  73. //promptURLs: true,
  74. renderingConfig: {
  75. singleLineBreaks: true,
  76. codeSyntaxHighlighting: true,
  77. },
  78. toolbar: ["bold","italic","heading","|","code","quote","unordered-list","ordered-list","|","link","image","table","|","preview","side-by-side","fullscreen","|",
  79. {
  80. name: "savebtn",
  81. action: function(){
  82. saveText();
  83. },
  84. className: "fa fa-floppy-o",
  85. title: "Save",
  86. },"guide","|"],
  87. spellChecker: true,
  88. status: ["autosave", "lines", "words", "cursor"], // Optional usage
  89. });
  90. lastSaveContent = data;
  91. lastSaveContent = lastSaveContent.split("\r").join("\n");
  92. });
  93. }else{
  94. simplemde = new SimpleMDE({ element: document.getElementById("maintext") });
  95. }
  96. function handleNewFileSave(filedata){
  97. for (var i=0; i < filedata.length; i++){
  98. var thisFilename = filedata[i].filename;
  99. var thisFilepath = filedata[i].filepath;
  100. //Update the current editing filepath
  101. filepath = thisFilepath;
  102. document.title = ("MDEditor - " + thisFilename);
  103. originalTitle = "MDEditor - " + thisFilename;
  104. }
  105. saveText();
  106. }
  107. function saveText(callback=undefined){
  108. if (filepath == ""){
  109. //This is a new file. Ask for save directory.
  110. ao_module_openFileSelector(handleNewFileSave, "user:/Desktop", "new",false, {
  111. defaultName: "Untitled.md"
  112. });
  113. return;
  114. }
  115. var newcontent = simplemde.value();
  116. //Create the file to upload
  117. const blob = new Blob([newcontent], { type: 'text/plain' });
  118. const preuploadFile = new File([blob], file.filename);
  119. let uploaddir = file.filepath.split("/");
  120. uploaddir.pop();
  121. uploaddir = uploaddir.join("/");
  122. $("#statusText").text("Saving file...");
  123. handleFile(preuploadFile, uploaddir, function(){
  124. lastSaveContent = newcontent;
  125. //Update the title as well
  126. document.title = (originalTitle);
  127. const currentDate = new Date();
  128. let saveDate = (`${currentDate.getFullYear()}/${currentDate.getMonth() + 1}/${currentDate.getDate()} ${currentDate.getHours()}:${currentDate.getMinutes()}:${currentDate.getSeconds()}`);
  129. $("#statusText").text("File saved on " + saveDate);
  130. if (callback != undefined){
  131. callback();
  132. }
  133. });
  134. /*
  135. ao_module_agirun("./MDEditor/filesaver.js", {
  136. filepath: filepath,
  137. content: newcontent
  138. }, function(data){
  139. console.log(data);
  140. if (data.error !== undefined){
  141. alert(data.error);
  142. }else{
  143. //Save succeed. Update last saved content
  144. lastSaveContent = newcontent;
  145. //Update the title as well
  146. document.title = (originalTitle);
  147. if (callback != undefined){
  148. callback();
  149. }
  150. }
  151. }, function(){
  152. alert("Save File Failed!")
  153. });
  154. */
  155. }
  156. setInterval(function(){
  157. //Check if the content from last save matched the current value on simplemde
  158. if (!isSaved()){
  159. document.title = ("*" + originalTitle);
  160. }else{
  161. //No new changes
  162. document.title = (originalTitle);
  163. }
  164. }, 1000);
  165. function isSaved(){
  166. if (typeof(simplemde) == "undefined"){
  167. //Still loading
  168. return;
  169. }
  170. var currentContent = simplemde.value();
  171. return (lastSaveContent == currentContent);
  172. }
  173. document.addEventListener('keydown', function(event) {
  174. if (event.ctrlKey && event.keyCode === 83) {
  175. event.preventDefault(); // Prevent the default browser behavior (e.g., saving the page)
  176. saveText();
  177. }
  178. });
  179. </script>
  180. </body>
  181. </html>