notebook.html 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 handleNewFileSave(filedata){
  100. for (var i=0; i < filedata.length; i++){
  101. var thisFilename = filedata[i].filename;
  102. var thisFilepath = filedata[i].filepath;
  103. //Update the current editing filepath
  104. filepath = thisFilepath;
  105. }
  106. saveText();
  107. }
  108. function saveText(callback=undefined){
  109. if (filepath == ""){
  110. //This is a new file. Ask for save directory.
  111. ao_module_openFileSelector(handleNewFileSave, "user:/Desktop", "new",false, {
  112. defaultName: "Untitled.txt"
  113. });
  114. return;
  115. }
  116. var newcontent = simplemde.value();
  117. ao_module_agirun("Notebook/filesaver.js", {
  118. filepath: filepath,
  119. content: newcontent
  120. }, function(data){
  121. console.log(data);
  122. if (data.error !== undefined){
  123. alert(data.error);
  124. }else{
  125. //Save succeed. Update last saved content
  126. lastSaveContent = newcontent;
  127. //Update the title as well
  128. ao_module_setWindowTitle(originalTitle);
  129. if (callback != undefined){
  130. callback();
  131. }
  132. }
  133. }, function(){
  134. alert("Save File Failed!")
  135. });
  136. /*
  137. $.ajax({
  138. url: "../../system/utils/notebook/save",
  139. data: {filepath: filepath, content: newcontent},
  140. success: function(data){
  141. console.log(data);
  142. if (data.error !== undefined){
  143. alert(data.error);
  144. }
  145. }
  146. });
  147. */
  148. }
  149. setInterval(function(){
  150. //Check if the content from last save matched the current value on simplemde
  151. if (!isSaved()){
  152. ao_module_setWindowTitle(originalTitle + " *");
  153. }else{
  154. //No new changes
  155. ao_module_setWindowTitle(originalTitle);
  156. }
  157. }, 1000);
  158. function isSaved(){
  159. var currentContent = simplemde.value();
  160. return (lastSaveContent == currentContent);
  161. }
  162. //Overwrite the ao_module_close function to allow
  163. if (!ao_module_virtualDesktop){
  164. window.onbeforeunload = function(){
  165. if (!isSaved()){
  166. return "Content is not saved. Confirm Exit?";
  167. }
  168. }
  169. }
  170. //Overwrite the close sequence
  171. function ao_module_close(){
  172. if (!isSaved()){
  173. //Not saved
  174. if (confirm("Some changes are not saved. Save before exit?")){
  175. saveText(function(){
  176. //Exit after save
  177. closeThisWindow();
  178. });
  179. }else{
  180. //User request not to save
  181. closeThisWindow();
  182. }
  183. }else{
  184. //Saved. Exit
  185. closeThisWindow();
  186. }
  187. }
  188. function closeThisWindow(){
  189. if (!ao_module_virtualDesktop){
  190. window.close('','_parent','');
  191. window.location.href = ao_root + "SystemAO/closeTabInsturction.html";
  192. return;
  193. }
  194. parent.closeFwProcess(ao_module_windowID);
  195. }
  196. </script>
  197. </body>
  198. </html>