editor.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <!-- Special modification for NotepadA system, ArOZ Online Beta-->
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  7. <title id='pageTitle'>NotepadA - Loading</title>
  8. <script src='../../script/jquery.min.js'></script>
  9. <script>
  10. $('body').css('background-color','white');
  11. function checkIsSaved(){
  12. return true;
  13. }
  14. </script>
  15. <script src="../../script/jquery.min.js"></script>
  16. <style type="text/css" media="screen">
  17. body {
  18. overflow: hidden;
  19. padding-bottom:5px;
  20. background-color:#2b2b2b;
  21. }
  22. #editor {
  23. margin: 0;
  24. position: absolute;
  25. top: 0;
  26. bottom: 15px;
  27. left: 0;
  28. right: 0;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <pre id="editor"></pre>
  34. <script src="src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
  35. <script>
  36. //Get the filename, path and other information from the hash
  37. var filename = "";
  38. var filepath = "";
  39. var lastSave = "";
  40. var fontsize = 12;
  41. var theme = "github";
  42. var tabid = "";
  43. var editor = undefined;
  44. window.onhashchange = function(e){
  45. initEditor();
  46. }
  47. initEditor();
  48. function initEditor(){
  49. if (window.location.hash.length > 0){
  50. var payload = window.location.hash.substr(1);
  51. payload = JSON.parse(decodeURIComponent(payload));
  52. filepath = payload.filename;
  53. let basename = payload.filename.split("/").pop();
  54. filename = basename;
  55. fontsize = payload.fontsize;
  56. theme = payload.theme;
  57. tabid = payload.tabid;
  58. console.log("NotepadA editor loaded: ", payload);
  59. //Load the content of the file
  60. $.get("../../media?file=" + filepath, function(fileContent){
  61. if (fileContent.error !== undefined){
  62. //Failed to get file
  63. setTimeout(function(){
  64. parent.removeTab(tabid);
  65. },300);
  66. return
  67. }
  68. if (typeof fileContent == "object"){
  69. //Loading a json file
  70. fileContent = JSON.stringify(fileContent);
  71. }
  72. $("#editor").text(fileContent);
  73. //Start the editor if not started
  74. if (editor == undefined){
  75. StartEditor();
  76. }
  77. });
  78. }else{
  79. alert("Invalid use of editor!")
  80. }
  81. }
  82. document.addEventListener("mousedown", function(event) {
  83. //When click on this document, focus this
  84. if (parent.ao_module_virtualDesktop){
  85. parent.ao_module_focus();
  86. }
  87. }, true);
  88. function StartEditor(){
  89. //Init editor
  90. editor = ace.edit("editor");
  91. editor.setTheme("ace/theme/" + theme);
  92. var detectMode = getMode(filepath);
  93. //console.log("[NotepadA] Chaning mode to " + detectMode.toLowerCase());
  94. if ( detectMode != undefined){
  95. editor.session.setMode("ace/mode/" + detectMode.toLowerCase());
  96. }else{
  97. //Default mode: php
  98. editor.session.setMode("ace/mode/php");
  99. }
  100. editor.setOptions({
  101. //fontFamily: "tahoma",
  102. fontSize: fontsize + "pt"
  103. });
  104. lastSave = editor.getValue();
  105. }
  106. //Listener for Ctrl+S
  107. $(window).keypress(function(event) {
  108. if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
  109. //console.log(filepath);
  110. Save();
  111. event.preventDefault();
  112. return false;
  113. });
  114. $(document).on("click","#editor",function() {
  115. //Hide the parent window in NotepadA condition
  116. window.parent.hideToggleMenu();
  117. });
  118. function Print() {
  119. try {
  120. var printWindow = window.open("", "", "height=400,width=800");
  121. printWindow.document.write(`<html><head><title>${filename} ${new Date().toLocaleString()}</title>`);
  122. printWindow.document.write("</head><xmp>");
  123. printWindow.document.write(editor.getSession().getDocument().getValue());
  124. printWindow.document.write("</xmp></html>");
  125. printWindow.document.close();
  126. printWindow.print();
  127. }catch (ex) {
  128. console.error("Error: " + ex.message);
  129. }
  130. }
  131. //Absolute path is used for saving. So no need to worry about the relative path over php issues
  132. function Save(){
  133. code = editor.getValue();
  134. parent.ao_module_agirun(parent.moduleRootPath + "backend/filesaver.js", {
  135. filepath: filepath,
  136. content: code
  137. }, function(data){
  138. if (data.error == undefined){
  139. //Finish writting to the file
  140. console.log("%c[NotepadA] " + filename + " -> Saved!","color:#1265ea");
  141. lastSave = code;
  142. }else{
  143. alert(data.error);
  144. console.log(data);
  145. }
  146. })
  147. /*
  148. $.post("../writeCode.php", { filename: filepath, content: code })
  149. .done(function( data ) {
  150. if (data.includes("ERROR") == false){
  151. console.log("%c[NotepadA] " + filename + " -> Saved!","color:#1265ea");
  152. lastSave = code;
  153. }else{
  154. console.log(data);
  155. }
  156. });
  157. */
  158. }
  159. function getDocWidth(){
  160. return $(document).width();
  161. }
  162. function checkIsSaved(){
  163. if (typeof(editor) == "undefined"){
  164. //Editor not started
  165. return
  166. }
  167. if (lastSave == editor.getValue()){
  168. return true;
  169. }else{
  170. return false;
  171. }
  172. }
  173. function insertChar(text){
  174. editor.session.insert(editor.getCursorPosition(), text);
  175. }
  176. function getEditorContenet(){
  177. return editor.getValue();
  178. }
  179. function getFilepath(){
  180. return (filepath);
  181. }
  182. function openInNewTab(){
  183. window.open("../../media?file=" + filepath);
  184. }
  185. function getSelectedText(){
  186. return editor.getSession().doc.getTextRange(editor.selection.getRange());
  187. }
  188. function insertGivenText(text){
  189. editor.session.insert(editor.getCursorPosition(), text);
  190. }
  191. function callUndo(){
  192. editor.getSession().getUndoManager().undo();
  193. }
  194. function callRedo(){
  195. editor.getSession().getUndoManager().redo();
  196. }
  197. var nameOverrides = {
  198. ObjectiveC: "Objective-C",
  199. CSharp: "C#",
  200. golang: "Go",
  201. C_Cpp: "C and C++",
  202. Csound_Document: "Csound Document",
  203. Csound_Orchestra: "Csound",
  204. Csound_Score: "Csound Score",
  205. coffee: "CoffeeScript",
  206. HTML_Ruby: "HTML (Ruby)",
  207. HTML_Elixir: "HTML (Elixir)",
  208. FTL: "FreeMarker"
  209. };
  210. function startSearchBox(){
  211. editor.execCommand("find");
  212. }
  213. function getMode(filePath){
  214. var ext = filePath.split(".").pop();
  215. var supportedModes = {
  216. ABAP: ["abap"],
  217. ABC: ["abc"],
  218. ActionScript:["as"],
  219. ADA: ["ada|adb"],
  220. Apache_Conf: ["^htaccess|^htgroups|^htpasswd|^conf|htaccess|htgroups|htpasswd"],
  221. AsciiDoc: ["asciidoc|adoc"],
  222. ASL: ["dsl|asl"],
  223. Assembly_x86:["asm|a"],
  224. AutoHotKey: ["ahk"],
  225. BatchFile: ["bat|cmd"],
  226. Bro: ["bro"],
  227. C_Cpp: ["cpp|c|cc|cxx|h|hh|hpp|ino"],
  228. C9Search: ["c9search_results"],
  229. Cirru: ["cirru|cr"],
  230. Clojure: ["clj|cljs"],
  231. Cobol: ["CBL|COB"],
  232. coffee: ["coffee|cf|cson|^Cakefile"],
  233. ColdFusion: ["cfm"],
  234. CSharp: ["cs"],
  235. Csound_Document: ["csd"],
  236. Csound_Orchestra: ["orc"],
  237. Csound_Score: ["sco"],
  238. CSS: ["css"],
  239. Curly: ["curly"],
  240. D: ["d|di"],
  241. Dart: ["dart"],
  242. Diff: ["diff|patch"],
  243. Dockerfile: ["^Dockerfile"],
  244. Dot: ["dot"],
  245. Drools: ["drl"],
  246. Edifact: ["edi"],
  247. Eiffel: ["e|ge"],
  248. EJS: ["ejs"],
  249. Elixir: ["ex|exs"],
  250. Elm: ["elm"],
  251. Erlang: ["erl|hrl"],
  252. Forth: ["frt|fs|ldr|fth|4th"],
  253. Fortran: ["f|f90"],
  254. FTL: ["ftl"],
  255. Gcode: ["gcode"],
  256. Gherkin: ["feature"],
  257. Gitignore: ["^.gitignore"],
  258. Glsl: ["glsl|frag|vert"],
  259. Gobstones: ["gbs"],
  260. golang: ["go"],
  261. GraphQLSchema: ["gql"],
  262. Groovy: ["groovy"],
  263. HAML: ["haml"],
  264. Handlebars: ["hbs|handlebars|tpl|mustache"],
  265. Haskell: ["hs"],
  266. Haskell_Cabal: ["cabal"],
  267. haXe: ["hx"],
  268. Hjson: ["hjson"],
  269. HTML: ["html|htm|xhtml|vue|we|wpy"],
  270. HTML_Elixir: ["eex|html.eex"],
  271. HTML_Ruby: ["erb|rhtml|html.erb"],
  272. INI: ["ini|conf|cfg|prefs"],
  273. Io: ["io"],
  274. Jack: ["jack"],
  275. Jade: ["jade|pug"],
  276. Java: ["java"],
  277. JavaScript: ["js|jsm|jsx"],
  278. JSON: ["json"],
  279. JSONiq: ["jq"],
  280. JSP: ["jsp"],
  281. JSSM: ["jssm|jssm_state"],
  282. JSX: ["jsx"],
  283. Julia: ["jl"],
  284. Kotlin: ["kt|kts"],
  285. LaTeX: ["tex|latex|ltx|bib"],
  286. LESS: ["less"],
  287. Liquid: ["liquid"],
  288. Lisp: ["lisp"],
  289. LiveScript: ["ls"],
  290. LogiQL: ["logic|lql"],
  291. LSL: ["lsl"],
  292. Lua: ["lua"],
  293. LuaPage: ["lp"],
  294. Lucene: ["lucene"],
  295. Makefile: ["^Makefile|^GNUmakefile|^makefile|^OCamlMakefile|make"],
  296. Markdown: ["md|markdown"],
  297. Mask: ["mask"],
  298. MATLAB: ["matlab"],
  299. Maze: ["mz"],
  300. MEL: ["mel"],
  301. MIXAL: ["mixal"],
  302. MUSHCode: ["mc|mush"],
  303. MySQL: ["mysql"],
  304. Nix: ["nix"],
  305. NSIS: ["nsi|nsh"],
  306. ObjectiveC: ["m|mm"],
  307. OCaml: ["ml|mli"],
  308. Pascal: ["pas|p"],
  309. Perl: ["pl|pm"],
  310. pgSQL: ["pgsql"],
  311. PHP: ["php|phtml|shtml|php3|php4|php5|phps|phpt|aw|ctp|module"],
  312. Pig: ["pig"],
  313. Powershell: ["ps1"],
  314. Praat: ["praat|praatscript|psc|proc"],
  315. Prolog: ["plg|prolog"],
  316. Properties: ["properties"],
  317. Protobuf: ["proto"],
  318. Python: ["py"],
  319. R: ["r"],
  320. Razor: ["cshtml|asp"],
  321. RDoc: ["Rd"],
  322. Red: ["red|reds"],
  323. RHTML: ["Rhtml"],
  324. RST: ["rst"],
  325. Ruby: ["rb|ru|gemspec|rake|^Guardfile|^Rakefile|^Gemfile"],
  326. Rust: ["rs"],
  327. SASS: ["sass"],
  328. SCAD: ["scad"],
  329. Scala: ["scala"],
  330. Scheme: ["scm|sm|rkt|oak|scheme"],
  331. SCSS: ["scss"],
  332. SH: ["sh|bash|^.bashrc"],
  333. SJS: ["sjs"],
  334. Smarty: ["smarty|tpl"],
  335. snippets: ["snippets"],
  336. Soy_Template:["soy"],
  337. Space: ["space"],
  338. SQL: ["sql"],
  339. SQLServer: ["sqlserver"],
  340. Stylus: ["styl|stylus"],
  341. SVG: ["svg"],
  342. Swift: ["swift"],
  343. Tcl: ["tcl"],
  344. Tex: ["tex"],
  345. Text: ["txt"],
  346. Textile: ["textile"],
  347. Toml: ["toml"],
  348. TSX: ["tsx"],
  349. Twig: ["twig|swig"],
  350. Typescript: ["ts|typescript|str"],
  351. Vala: ["vala"],
  352. VBScript: ["vbs|vb"],
  353. Velocity: ["vm"],
  354. Verilog: ["v|vh|sv|svh"],
  355. VHDL: ["vhd|vhdl"],
  356. Wollok: ["wlk|wpgm|wtest"],
  357. XML: ["xml|rdf|rss|wsdl|xslt|atom|mathml|mml|xul|xbl|xaml"],
  358. XQuery: ["xq"],
  359. YAML: ["yaml|yml"],
  360. Django: ["html"]
  361. };
  362. for (var key in supportedModes) {
  363. if (supportedModes.hasOwnProperty(key)) {
  364. var thisExtension = supportedModes[key][0].split("|");
  365. if (thisExtension.length == 1){
  366. if (ext == thisExtension[0]){
  367. return key;
  368. }
  369. }else{
  370. for(var i =0; i < thisExtension.length;i++){
  371. if (ext == thisExtension[i]){
  372. return key;
  373. }
  374. }
  375. }
  376. }
  377. }
  378. }
  379. $(window).bind('keydown', function(event) {
  380. if (event.ctrlKey || event.metaKey) {
  381. switch (String.fromCharCode(event.which).toLowerCase()) {
  382. case 's':
  383. event.preventDefault();
  384. Save();
  385. break;
  386. }
  387. }
  388. });
  389. </script>
  390. </body>
  391. </html>