notebook.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <html>
  2. <head>
  3. <title>Notebook</title>
  4. <script src="../script/jquery.min.js"></script>
  5. <script src="../script/ao_module.js"></script>
  6. <link rel="stylesheet" href="script/SimpleMDE/simplemde.min.css">
  7. <script src="script/SimpleMDE/simplemde.min.js"></script>
  8. <style>
  9. html, body{
  10. background-color:white;
  11. height: calc(100% - 80px);
  12. }
  13. #maintext{
  14. height:100% !important;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <textarea id="maintext"></textarea>
  20. <script>
  21. var simplemde; //SimpleMDE editor object
  22. var filepath = ""; //Current editing filepath
  23. var files = ao_module_loadInputFiles();
  24. var originalTitle = "Notebook";
  25. var lastSaveContent = ""; //Content for last saved content
  26. $(window).on("keydown",function(event) {
  27. if (event.which == 83 && event.ctrlKey){
  28. event.preventDefault();
  29. saveText();
  30. }else if (event.target.tagName == "TEXTAREA"){
  31. //Some wierd hack to make SimpleMDE to work with ArozOS IME API
  32. if (parent.window.ime){
  33. parent.window.ime.focus = event.target;
  34. if (parent.window.ime.handler != null){
  35. parent.window.ime.handler(event);
  36. }
  37. }
  38. }
  39. });
  40. if (files !== null && files.length > 0){
  41. //Set the window name
  42. ao_module_setWindowTitle("Notebook - " + files[0].filename);
  43. originalTitle = "Notebook - " + files[0].filename;
  44. //Check if there are more than 1 text files to be opened. If yes, open new windows.
  45. if (files.length > 1){
  46. for (var i = 1; i < files.length; i++){
  47. var thisFilelist = [{
  48. filename: files[i].filename,
  49. filepath: files[i].filepath
  50. }];
  51. thisFilelist = encodeURIComponent(JSON.stringify(thisFilelist));
  52. ao_module_newfw({
  53. url: "SystemAO/utilities/notebook.html#" + thisFilelist,
  54. width: 1080,
  55. height: 580,
  56. appicon: "SystemAO/utilities/img/notebook.png",
  57. title: "Notebook",
  58. });
  59. }
  60. }
  61. filepath = files[0].filepath;
  62. //Load the file into the textarea
  63. $.get("../../media?file=" + files[0].filepath,function(data){
  64. $("#maintext").text(data);
  65. lastSaveContent = data;
  66. //Load Markdown Editor
  67. simplemde = new SimpleMDE({
  68. autofocus: true,
  69. element: document.getElementById("maintext"),
  70. forceSync: true,
  71. insertTexts: {
  72. horizontalRule: ["", "\n\n-----\n\n"],
  73. image: ["![](http://", ")"],
  74. link: ["[", "](http://)"],
  75. table: ["", "\n\n| Column 1 | Column 2 | Column 3 |\n| -------- | -------- | -------- |\n| Text | Text | Text |\n\n"],
  76. },
  77. placeholder: "(Empty file)",
  78. //promptURLs: true,
  79. renderingConfig: {
  80. singleLineBreaks: true,
  81. codeSyntaxHighlighting: true,
  82. },
  83. toolbar: ["bold","italic","heading","|","code","quote","unordered-list","ordered-list","|","link","image","table","|","preview","side-by-side","fullscreen","|",
  84. {
  85. name: "savebtn",
  86. action: function(){
  87. saveText();
  88. },
  89. className: "fa fa-floppy-o",
  90. title: "Save",
  91. },"guide","|"],
  92. spellChecker: false,
  93. status: ["autosave", "lines", "words", "cursor"], // Optional usage
  94. });
  95. });
  96. }else{
  97. simplemde = new SimpleMDE({ element: document.getElementById("maintext") });
  98. }
  99. function saveText(callback=undefined){
  100. var newcontent = simplemde.value();
  101. ao_module_agirun("Notebook/filesaver.js", {
  102. filepath: filepath,
  103. content: newcontent
  104. }, function(data){
  105. console.log(data);
  106. if (data.error !== undefined){
  107. alert(data.error);
  108. }else{
  109. //Save succeed. Update last saved content
  110. lastSaveContent = newcontent;
  111. //Update the title as well
  112. ao_module_setWindowTitle(originalTitle);
  113. if (callback != undefined){
  114. callback();
  115. }
  116. }
  117. }, function(){
  118. alert("Save File Failed!")
  119. });
  120. /*
  121. $.ajax({
  122. url: "../../system/utils/notebook/save",
  123. data: {filepath: filepath, content: newcontent},
  124. success: function(data){
  125. console.log(data);
  126. if (data.error !== undefined){
  127. alert(data.error);
  128. }
  129. }
  130. });
  131. */
  132. }
  133. setInterval(function(){
  134. //Check if the content from last save matched the current value on simplemde
  135. if (!isSaved()){
  136. ao_module_setWindowTitle(originalTitle + " *");
  137. }else{
  138. //No new changes
  139. ao_module_setWindowTitle(originalTitle);
  140. }
  141. }, 1000);
  142. function isSaved(){
  143. var currentContent = simplemde.value();
  144. return (lastSaveContent == currentContent);
  145. }
  146. //Overwrite the ao_module_close function to allow
  147. if (!ao_module_virtualDesktop){
  148. window.onbeforeunload = function(){
  149. if (!isSaved()){
  150. return "Content is not saved. Confirm Exit?";
  151. }
  152. }
  153. }
  154. //Overwrite the close sequence
  155. function ao_module_close(){
  156. if (!isSaved()){
  157. //Not saved
  158. if (confirm("Some changes are not saved. Save before exit?")){
  159. saveText(function(){
  160. //Exit after save
  161. closeThisWindow();
  162. });
  163. }else{
  164. //User request not to save
  165. closeThisWindow();
  166. }
  167. }else{
  168. //Saved. Exit
  169. closeThisWindow();
  170. }
  171. }
  172. function closeThisWindow(){
  173. if (!ao_module_virtualDesktop){
  174. window.close('','_parent','');
  175. window.location.href = ao_root + "SystemAO/closeTabInsturction.html";
  176. return;
  177. }
  178. parent.closeFwProcess(ao_module_windowID);
  179. }
  180. </script>
  181. </body>
  182. </html>