editor.html 11 KB

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