index.html 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="apple-mobile-web-app-capable" content="yes" />
  5. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  6. <meta charset="UTF-8">
  7. <title>NotepadA</title>
  8. <meta name="theme-color" content="#4b75ff">
  9. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  10. <link rel="stylesheet" href="notepadA.css">
  11. <script src="../script/jquery.min.js"></script>
  12. <script src="../script/semantic/semantic.min.js"></script>
  13. <script src="../script/ao_module.js"></script>
  14. <link data-name="vs/editor/editor.main" rel="stylesheet" href="script/monaco/vs/editor/editor.main.css">
  15. <style>
  16. html{
  17. height:100%;
  18. }
  19. body{
  20. height:100%;
  21. overflow-y:hidden;
  22. padding:1px;
  23. background-color:#292929;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <!-- Main Menu -->
  29. <div class="npa menu">
  30. <div class="item"><img class="ui image" src="img/module_icon.png" style="height:20px;"></img></div>
  31. <div class="item selectable" onclick="initContextMenu('file',this);">File</div>
  32. <div class="item selectable" onclick="initContextMenu('edit',this);">Edit</div>
  33. <div class="item selectable" onclick="initContextMenu('view',this);">View</div>
  34. <div class="item selectable" onclick="initContextMenu('fontsize',this);">Font Size</div>
  35. <div class="item selectable" onclick="initContextMenu('help',this);">Help</div>
  36. </div>
  37. <div id="directoryList" onclick="hideContextMenu();">
  38. <div class="banner">Directory Listing</div>
  39. <div class="title item" onclick="event.preventDefault(); $('#openeditors').stop().finish().slideToggle('fast');"><i class="caret down icon"></i> OPEN EDITORS</div>
  40. <div class="subgroup" id="openeditors">
  41. </div>
  42. <div class="title item" onclick="toggleDirectoryExplorer(this);"><i class="caret down icon"></i><span id="openFolderTitle">NO FOLDER</span></div>
  43. <div class="subgroup" id="directoryExplorer">
  44. </div>
  45. </div>
  46. <div id="codeArea" onclick="hideContextMenu();">
  47. <div id="ca1" class="codeBoard">
  48. <div class="tabs">
  49. </div>
  50. <div class="editor" editorUID="mainEditor" onclick="focusThisEditor(this);"></div>
  51. <div class="editorcover" editorUID="mainEditor" onclick="focusThisEditor(this);"></div>
  52. </div>
  53. <div id="ca2" class="codeBoard splitMode right" style="display:none;">
  54. <div class="tabs">
  55. </div>
  56. <div class="editor" editorUID="subEditor" onclick="focusThisEditor(this);"></div>
  57. <div class="editorcover" editorUID="subEditor" onclick="focusThisEditor(this);"></div>
  58. </div>
  59. <div id="terminals" style="display:none;">
  60. <div class="menubar">
  61. <p>TERMINAL</p>
  62. <div style="position: absolute; right: 12px;">
  63. <i style="cursor: pointer;" onclick="restartTerminal();" class="refresh icon"></i>
  64. <i style="cursor: pointer;" onclick="closeTerminal();" class="remove icon"></i>
  65. </div>
  66. </div>
  67. <div class="terminalWindow focused">
  68. <iframe id="initTerminal" src="../SystemAO/wstty/console.html">
  69. </iframe>
  70. </div>
  71. </div>
  72. </div>
  73. <div class="bottommenu">
  74. <div class="item"><i class="exchange icon"></i><span id="pingStatus"></span></div>
  75. <div class="item extrapadding" id="messageField" style="display:none;"><i class="checkmark icon"></i>Connected</div>
  76. <div class="rightpadded">
  77. <div class="item" id="filepath"><i class="code file icon"></i> user:/</div>
  78. <div class="item" id="fileExt">N/A</div>
  79. <div class="item">NotepadA</div>
  80. </div>
  81. </div>
  82. <!-- Context Menu -->
  83. <div id="contextMenu" class="contextMenu">
  84. </div>
  85. <!-- Licnese information -->
  86. <div id="licenseInfo" class="ui modal">
  87. <i class="close icon"></i>
  88. <div class="header">
  89. NotepadA License
  90. </div>
  91. <div class="content">
  92. <div class="description">
  93. <div class="ui header">Copyright 2020-2021 Toby Chui</div>
  94. <p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  95. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  96. <br>
  97. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  98. </p>
  99. </div>
  100. </div>
  101. <div class="actions">
  102. <div class="ui positive right labeled icon button">
  103. OK
  104. <i class="checkmark icon"></i>
  105. </div>
  106. </div>
  107. </div>
  108. <!-- NotepadA Info-->
  109. <div id="aboutnpa" class="ui basic modal">
  110. <div class="ui icon header">
  111. <i class="code icon"></i>
  112. NotepadA Code Editor
  113. </div>
  114. <div class="content" align="center">
  115. <p>The default code editor for ArOZ Online 1.0<br>Developed by Toby Chui</p>
  116. </div>
  117. <div class="actions">
  118. <div class="ui green ok inverted button">
  119. <i class="checkmark icon"></i>
  120. OK
  121. </div>
  122. </div>
  123. </div>
  124. <!-- Copy and Paste menu reminder-->
  125. <div id="clipboardReminder" class="ui basic modal">
  126. <div class="ui icon header">
  127. <i id="clipboardReminderIcon" class="archive icon"></i>
  128. Keyboard Only
  129. </div>
  130. <div class="content" style="text-align:center;">
  131. <p id="clipboardReminderText"></p>
  132. </div>
  133. <div class="actions">
  134. <div class="ui green ok inverted button">
  135. <i class="checkmark icon"></i>
  136. OK
  137. </div>
  138. </div>
  139. </div>
  140. <script src="script/monaco/vs/loader.js"></script>
  141. <script>
  142. //Environment Paramters
  143. let editors = []; //List of opened editor
  144. let splitMode = false; //Split the screen into two half
  145. let focusedEditor = "mainEditor"; //The editor currently is focused
  146. let loadedModels = []; //Loaded models. Prevent duplicate loading problem
  147. let focusedFileInfo = [];
  148. let files = ao_module_loadInputFiles(); //Files imported from file system
  149. //Terminal realted
  150. let terminalStartingEndpoint = "../SystemAO/wstty/console.html";
  151. //Initiation functions
  152. initEditor($("#ca1").find(".editor")[0], "mainEditor", files, restoreEditorState);
  153. initEditor($("#ca2").find(".editor")[0],"subEditor");
  154. //Load ready page into the editorCover
  155. $(".editorcover").load("home.html");
  156. //Get system info to determind the terminal endpoint
  157. $.get("../system/info/getArOZInfo", function(data){
  158. if (data.HostOS == "windows"){
  159. terminalStartingEndpoint = "../SystemAO/wstty/console.html";
  160. }else if (data.HostOS == "linux"){
  161. terminalStartingEndpoint = "../wstty/";
  162. $("#initTerminal").attr('src', terminalStartingEndpoint);
  163. }
  164. });
  165. //Restore the previous editor state
  166. function restoreEditorState(editor){
  167. //Load previous state if exists
  168. if (window.location.hash.length > 1){
  169. var stateString = window.location.hash.substr(1);
  170. var oldState = JSON.parse(decodeURIComponent(stateString));
  171. //Load the previous statue opened folder
  172. if (oldState.folder !== undefined){
  173. //Load the previous opened folder
  174. var predictedName = oldState.folder.split("/").pop();
  175. if (oldState.folder.substr(oldState.folder.length -1) == "/"){
  176. var tmp = oldState.folder.split("/");
  177. tmp.pop();
  178. predictedName = tmp.pop();
  179. }
  180. openProjectFolder([{
  181. filename: predictedName,
  182. filepath: oldState.folder
  183. }], false);
  184. }
  185. //Open previous opened files
  186. if (oldState.files !== undefined){
  187. oldState.files.forEach(filepath => {
  188. openFile(filepath, false);
  189. })
  190. }
  191. //Open terminal if it is enabled previously
  192. if (oldState.terminal !== undefined && oldState.terminal == true){
  193. openTerminal();
  194. }
  195. }
  196. }
  197. //Restore previous editor states
  198. function restoreUserPreference(){
  199. getStorage("fontsize",function(fontsize){
  200. setFontSize(fontsize);
  201. });
  202. getStorage("splitmode",function(splitSetting){
  203. if (splitSetting.error !== undefined){
  204. return;
  205. }
  206. splitSetting = splitSetting.trim();
  207. if (splitSetting == "true"){
  208. splitMode = true;
  209. $("#ca1").attr("class","codeBoard splitMode left")
  210. $("#ca2").show();
  211. }else{
  212. splitMode = false;
  213. $("#ca1").attr("class","codeBoard")
  214. $("#ca2").hide();
  215. }
  216. //Update all editor's size
  217. for (var i =0; i < editors.length; i++){
  218. editors[i].editor.layout();
  219. }
  220. });
  221. }
  222. //Check if it is run under vdi mode. If not, establish checkauth ping to keep session alive
  223. if (!ao_module_virtualDesktop){
  224. setInterval(function(){
  225. $.get("../system/auth/checkLogin",function(data){
  226. if (data.error != undefined){
  227. $("#pingStatus").text(data.error);
  228. }else{
  229. var x = Date()
  230. $("#pingStatus").text(x);
  231. }
  232. });
  233. },30000);
  234. }else{
  235. $("#pingStatus").text("Web Desktop Mode");
  236. }
  237. $.get("../system/auth/checkLogin",function(data){
  238. var x = Date()
  239. $("#pingStatus").text(x);
  240. });
  241. //Bind the events of the tab objects
  242. function bindTabItemEvents(){
  243. $(".tabs .item").off("click").on('click',function(evt){
  244. if ($(this).hasClass("selected")){
  245. //This tab is already in focus.
  246. return;
  247. }
  248. //Add focused class to the tab
  249. $(this).parent().find(".item.selected").removeClass("selected");
  250. $(this).addClass("selected");
  251. //Check which editor this tab belongs to
  252. var editorUUID = $(this).parent().parent().find(".editor").attr("editoruid");
  253. var tabUUID = $(this).attr("uuid");
  254. var targetEditor;
  255. for (var i =0; i < editors.length; i++){
  256. if (editors[i].uuid == editorUUID){
  257. targetEditor = editors[i];
  258. }
  259. }
  260. if (typeof targetEditor === undefined){
  261. console.log("Error when loading tab");
  262. return;
  263. }
  264. changeTab(targetEditor,tabUUID);
  265. //Update the selected item in opened editor as well
  266. $("#openeditors .item.selected").removeClass("selected");
  267. $("#openeditors .item").each(function(){
  268. if ($(this).attr("uuid") == tabUUID){
  269. $(this).addClass("selected");
  270. }
  271. });
  272. });
  273. $("#openeditors .item").off("click").on("click",function(evt){
  274. //Update UI to match selected tab
  275. $("#openeditors .item.selected").removeClass('selected');
  276. $(this).addClass("selected");
  277. //Get the target editor UUID
  278. var tabID = $(this).attr("uuid");
  279. var editorUUID = $(this).attr("editorUUID");
  280. var targetEditorObject;
  281. for (var i =0; i < editors.length; i++){
  282. if (editors[i].uuid == editorUUID){
  283. targetEditorObject = editors[i];
  284. }
  285. }
  286. if (typeof targetEditorObject === undefined){
  287. console.log("Error when loading tab");
  288. return;
  289. }
  290. changeTab(targetEditorObject,tabID);
  291. //Also highlight the tab menu as well
  292. $(targetEditorObject.tabsMenu).find(".item.selected").removeClass('selected');
  293. $(targetEditorObject.tabsMenu).find(".item").each(function(){
  294. if ($(this).attr("uuid") == tabID){
  295. $(this).addClass("selected");
  296. }
  297. });
  298. });
  299. $(".tabs .item .closebtn").off("click").on('click',function(evt){
  300. evt.preventDefault();
  301. evt.stopImmediatePropagation();
  302. var tabUUID = $(this).parent().attr("uuid");
  303. var editorUUID = $(this).parent().attr("editorUUID");
  304. closeTabWithUUIDAndEditorID(tabUUID, editorUUID);
  305. });
  306. $("#openeditors .closebtn").off("click").on("click",function(evt){
  307. evt.stopImmediatePropagation();
  308. evt.preventDefault();
  309. var tabUUID = $(this).parent().attr("uuid");
  310. var editorUUID = $(this).parent().attr("editorUUID");
  311. closeTabWithUUIDAndEditorID(tabUUID, editorUUID);
  312. });
  313. }
  314. //Close a tab given the editorUUID and the tabUUID.
  315. function closeTabWithUUIDAndEditorID(tabUUID, editorUUID){
  316. //Find the editor object related to this tab
  317. let targetEditorObject;
  318. for (var i =0; i < editors.length; i++){
  319. if (editors[i].uuid == editorUUID){
  320. targetEditorObject = editors[i];
  321. }
  322. }
  323. if (targetEditorObject === undefined){
  324. console.log("Error when closing tab");
  325. return;
  326. }
  327. //Remove state and models from the editor object
  328. delete targetEditorObject.state[tabUUID];
  329. delete targetEditorObject.model[tabUUID];
  330. //Remove this tab from the list
  331. tabs = JSON.parse(JSON.stringify(targetEditorObject.tabs));
  332. for (var i =0; i < tabs.length; i++){
  333. if (tabUUID == tabs[i].tabUUID){
  334. targetEditorObject.tabs.splice(i,1);
  335. }
  336. }
  337. //Remove the tab objects from DOM
  338. removeTabFromDOMWithUUID(tabUUID);
  339. if (targetEditorObject.tabs.length > 0){
  340. //Switch to the first tab of the editor
  341. var newFocusedTabUUID = targetEditorObject.tabs[0].tabUUID;
  342. changeTab(targetEditorObject, newFocusedTabUUID);
  343. focusTabWithUUID(newFocusedTabUUID);
  344. }else{
  345. //No more tab lefts. Show the editor cover
  346. $(targetEditorObject.tabsMenu).parent().find(".editorcover").show();
  347. $(targetEditorObject.tabsMenu).parent().find(".editorcover").load("home.html");
  348. }
  349. }
  350. function getFocusedTabInfo(){
  351. var focusedEditorObject = getFocusedEditorObject();
  352. var focusedTabUUID = focusedEditorObject.currentTabUUID;
  353. for (var i =0; i < focusedEditorObject.tabs.length; i++){
  354. var thistab = focusedEditorObject.tabs[i];
  355. if (thistab.tabUUID == focusedTabUUID){
  356. //Update the global focused tab info as the current working-on file
  357. focusedFileInfo = thistab;
  358. return thistab;
  359. }
  360. }
  361. }
  362. function getCodeAreaFromEditorUUID(editorUUID){
  363. for (var i =0; i < editors.length; i++){
  364. if (editors[i].uuid == editorUUID){
  365. var tabsMenu = editors[i].tabsMenu;
  366. return $(tabsMenu).parent();
  367. }
  368. }
  369. }
  370. function updateFileStatusDisplay(filepath){
  371. $("#filepath").html(`<i class="code file icon"></i> ${filepath}`);
  372. var ext = filepath.split(".").pop();
  373. $("#fileExt").text(ext.toUpperCase());
  374. }
  375. function getFocusedEditorObject(){
  376. for (var i =0; i < editors.length; i++){
  377. if (editors[i].uuid == focusedEditor){
  378. return editors[i];
  379. }
  380. }
  381. }
  382. function focusThisEditor(object){
  383. var editorUUID = $(object).attr("editorUID");
  384. focusedEditor = editorUUID;
  385. //Update the target file info
  386. var tabInfo = getFocusedTabInfo();
  387. if (tabInfo !== undefined){
  388. updateFileStatusDisplay(tabInfo.filepath);
  389. }
  390. }
  391. function focusTabWithUUID(tabUUID){
  392. $(".tabs .item.selected").removeClass("selected");
  393. $(".tabs .item").each(function(){
  394. if ($(this).attr("uuid") == tabUUID){
  395. $(this).addClass("selected");
  396. }
  397. });
  398. $("#openeditors .item.selected").removeClass("selected");
  399. $("#openeditors .item").each(function(){
  400. if ($(this).attr("uuid") == tabUUID){
  401. $(this).addClass("selected");
  402. }
  403. });
  404. }
  405. function removeTabFromDOMWithUUID(tabUUID){
  406. $(".tabs .item").each(function(){
  407. if ($(this).attr("uuid") == tabUUID){
  408. $(this).remove();
  409. }
  410. });
  411. //Remove the tab from OPEN EDITORS
  412. $("#openeditors .item").each(function(){
  413. if ($(this).attr("uuid") == tabUUID){
  414. $(this).remove();
  415. }
  416. });
  417. }
  418. function focusTab(uuid){
  419. $(".tabs .item").each(function(){
  420. if ($(this).attr("uuid") == uuid){
  421. $(this).addClass("selected");
  422. }
  423. });
  424. $("#openeditors .item.selected").removeClass("selected");
  425. $("#openeditors .item").each(function(){
  426. if ($(this).attr("uuid") == uuid){
  427. $(this).addClass("selected");
  428. }
  429. });
  430. }
  431. //Main script section for UI handling
  432. //Load context menu
  433. function initContextMenu(target, object){
  434. if (target == "file"){
  435. //List items in files
  436. $("#contextMenu").html(`
  437. <div class="item">New File
  438. <div class="tips">Ctrl + N</div>
  439. </div>
  440. <div class="divider"></div>
  441. <div class="item" onclick="openFileWithSelector();">Open File
  442. <div class="tips">Ctrl + O</div>
  443. </div>
  444. <div class="item" onclick="openFolderWithSelector();">Open Folder
  445. <div class="tips">Ctrl + Shift + O</div>
  446. </div>
  447. <div class="divider"></div>
  448. <div class="item" onclick="saveCurrentFile();">Save</div>
  449. <div class="item">Save As</div>
  450. <div class="item" onclick="saveAllFiles();">Save All</div>
  451. <!--<div class="divider"></div>
  452. <div class="item">Preference</div>-->
  453. <div class="divider"></div>
  454. <div class="item" onclick="closeFileCurrentlyFocused();">Close File</div>
  455. <div class="item" onclick="closeAllFiles();">Close All Files</div>
  456. `);
  457. if (ao_module_virtualDesktop){
  458. $("#contextMenu").append(`<div class="item" onclick="hanleUserExit();">Exit</div>`);
  459. }
  460. }else if (target == "edit"){
  461. $("#contextMenu").html(`
  462. <div class="item" onclick="undo();">Undo
  463. <div class="tips">Ctrl + Z</div>
  464. </div>
  465. <div class="item" onclick="redo();">Redo
  466. <div class="tips">Ctrl + Y</div>
  467. </div>
  468. <div class="divider"></div>
  469. <div class="item" onclick="showClipboardReminders(1);">Cut
  470. <div class="tips">Ctrl + X</div>
  471. </div>
  472. <div class="item" onclick="showClipboardReminders(2);">Copy
  473. <div class="tips">Ctrl + C</div>
  474. </div>
  475. <div class="item" onclick="showClipboardReminders(3);">Paste
  476. <div class="tips">Ctrl + V</div>
  477. </div>
  478. <div class="item" onclick="showClipboardReminders(4);">Find
  479. <div class="tips">Ctrl + F</div>
  480. </div>
  481. <div class="item" onclick="showClipboardReminders(5);">Replace
  482. <div class="tips">Ctrl + H</div>
  483. </div>
  484. <div class="divider"></div>
  485. <div class="item" onclick="toggleSplit(true);">Split WorkSpace</div>
  486. <div class="item" onclick="toggleSplit(false);">Merge WorkSpace</div>
  487. `);
  488. }else if (target == "view"){
  489. $("#contextMenu").html(`
  490. <div class="item">New Editor
  491. <div class="tips"><i class="add icon"></i></div>
  492. </div>
  493. <div class="item">Open in new Tab
  494. <div class="tips"><i class="external icon"></i></div>
  495. </div>
  496. <div class="item">Open in floatWindow
  497. <div class="tips"><i class="window restore outline icon"></i></div>
  498. </div>
  499. <div class="item" onclick="openFileInFileManager();">Reveal in File Manager
  500. <div class="tips"><i class="folder outline icon"></i></div>
  501. </div>
  502. <div class="item" onclick="openTerminal();">Open Terminal
  503. <div class="tips"><i class="code icon"></i></div>
  504. </div>
  505. <div class="divider"></div>
  506. <div class="item">Download
  507. <div class="tips"><i class="download icon"></i></div>
  508. </div>
  509. `);
  510. }else if (target == "fontsize"){
  511. $("#contextMenu").html('');
  512. for (var i = 8; i < 26; i++ ){
  513. $("#contextMenu").append(`<div class="item" style="font-size:${i}px;" onclick="setFontSize(${i});">${i} px</div>`);
  514. }
  515. }else if (target == "help"){
  516. $("#contextMenu").html(`
  517. <div class="item" onclick="window.open('https://microsoft.github.io/monaco-editor/'); hideContextMenu();">About Monaco Editor</div>
  518. <div class="divider"></div>
  519. <div class="item" onclick="showLicense();">License</div>
  520. <div class="item" onclick="showAboutNotepadA();">About NotepadA</div>
  521. `);
  522. }
  523. $("#contextMenu").show();
  524. $("#contextMenu").css({
  525. left: $(object).offset().left,
  526. top: "28px"
  527. })
  528. }
  529. function showLicense(){
  530. $("#licenseInfo").modal("show");
  531. hideContextMenu();
  532. }
  533. function showAboutNotepadA(){
  534. $("#aboutnpa").modal("show");
  535. hideContextMenu();
  536. }
  537. function closeFileCurrentlyFocused(){
  538. var focusedEditor = getFocusedEditorObject();
  539. var editorUUID = focusedEditor.uuid;
  540. var tabsMenu = focusedEditor.tabsMenu;
  541. var focusedTabID = $(tabsMenu).find(".item.selected").attr("uuid");
  542. closeTabWithUUIDAndEditorID(focusedTabID,editorUUID);
  543. hideContextMenu();
  544. }
  545. function saveAllFiles(){
  546. //Store the editor object before saveall
  547. let beforeTabInfo = getFocusedTabInfo();
  548. let beforeEditor = getFocusedEditorObject();
  549. //Iterate through all editors and all tabs to save
  550. for (var i = 0; i < editors.length; i++){
  551. var thisEditor = editors[i];
  552. for (var j =0; j < thisEditor.tabs.length; j++){
  553. var tabUUID = thisEditor.tabs[j].tabUUID;
  554. changeTab(thisEditor, tabUUID);
  555. saveCurrentFile();
  556. }
  557. }
  558. //Restore the editor view
  559. changeTab(beforeEditor, beforeTabInfo.tabUUID);
  560. hideContextMenu();
  561. }
  562. function setFontSize(fontsize){
  563. for (var i =0; i < editors.length; i++){
  564. var thisEditor = editors[i].editor;
  565. thisEditor.updateOptions({
  566. fontSize: fontsize
  567. });
  568. }
  569. setStorage("fontsize",fontsize);
  570. hideContextMenu();
  571. }
  572. function toggleSplit(useSplitMode){
  573. if (useSplitMode){
  574. $("#ca1").attr("class","codeBoard splitMode left")
  575. $("#ca2").show();
  576. setStorage("splitmode","true");
  577. }else{
  578. $("#ca1").attr("class","codeBoard")
  579. $("#ca2").hide();
  580. setStorage("splitmode","false");
  581. }
  582. //Update all editor's size
  583. for (var i =0; i < editors.length; i++){
  584. editors[i].editor.layout();
  585. }
  586. hideContextMenu();
  587. }
  588. //Hide all the context menus
  589. function hideContextMenu(){
  590. $("#contextMenu").hide();
  591. }
  592. //open a file with a new editor by the given filepath
  593. function openFile(filepath, updateHashStatus=true){
  594. //Get the focused editor
  595. var targetEditorGroup = undefined;
  596. for (var i =0; i < editors.length; i++){
  597. if (editors[i].uuid == focusedEditor){
  598. targetEditorGroup = editors[i];
  599. }
  600. }
  601. if (targetEditorGroup === undefined){
  602. alert("Unable to load editor.");
  603. return;
  604. }
  605. //Check if the file is already loaded in this editor
  606. for (var i = 0; i < targetEditorGroup.tabs.length; i++){
  607. var thisTab = targetEditorGroup.tabs[i];
  608. if (thisTab.filepath == filepath){
  609. //Focus this tab
  610. changeTab(targetEditorGroup, thisTab.tabUUID);
  611. return;
  612. }
  613. }
  614. //Get the file
  615. ao_module_agirun("NotepadA/backend/read.agi",{
  616. file: filepath
  617. }, function(filecontent){
  618. //Get the target Opening Editor
  619. let targetOpeningEditor;
  620. for (var i = 0; i < editors.length; i++){
  621. if (editors[i].uuid == focusedEditor){
  622. targetOpeningEditor = editors[i];
  623. }
  624. }
  625. if (typeof targetOpeningEditor === undefined){
  626. console.log("Error when trying to get editor info")
  627. return;
  628. }
  629. //Load the editor instances
  630. var editor = targetOpeningEditor.editor;
  631. var editorUUID = targetOpeningEditor.uuid;
  632. var models = targetOpeningEditor.model;
  633. var states = targetOpeningEditor.state;
  634. var currentTabUUID = targetOpeningEditor.currentTabUUID;
  635. var targetTabMenu = targetOpeningEditor.tabsMenu;
  636. //Save the current editor status into the data
  637. var currentState = editor.saveViewState();
  638. var currentModel = editor.getModel();
  639. states[currentTabUUID] = currentState;
  640. models[currentTabUUID] = currentModel;
  641. //Create a tab for this file
  642. var tabUUID = new Date().getTime();
  643. var filename = filepath.split("/").pop();
  644. $(targetTabMenu).find(".item.selected").removeClass("selected");
  645. $(targetTabMenu).append(`<div class="item selected" uuid="${tabUUID}" editorUUID="${editorUUID}"><i class="code file icon"></i> ${filename} <div class="closebtn"><i class="remove icon"></i></i></div></div>`);
  646. //Add this tab into the open editor list
  647. $("#openeditors .item").removeClass("selected");
  648. $("#openeditors").append(`<div class="item selected" uuid="${tabUUID}" editorUUID="${editorUUID}"><div class="closebtn" style="display:inline-block;"><i class="remove icon"></i></div> <i class="code file icon"></i> ${filename} </div>`);
  649. //Update the current tag information
  650. targetOpeningEditor.currentTabUUID = tabUUID
  651. //Push the current tab into the array of tabs
  652. targetOpeningEditor.tabs.push({
  653. filename: filename,
  654. filepath: filepath,
  655. tabUUID: tabUUID,
  656. saveHash: filecontent.hashCode()
  657. });
  658. //Update title if nonVDI or update floatWindow title if under fdi
  659. if (ao_module_virtualDesktop){
  660. ao_module_setWindowTitle("NotepadA - " + filename);
  661. }else{
  662. document.title = "NotepadA - " + filename;
  663. }
  664. //Check if model is loaded before
  665. var model;
  666. if (loadedModels[filepath] === undefined){
  667. //Set the editor value and do model auto lanuage detection
  668. model = monaco.editor.createModel(
  669. filecontent,
  670. undefined,
  671. monaco.Uri.file("../media?file=" + encodeURIComponent(filepath))
  672. )
  673. loadedModels[filepath] = model;
  674. }else{
  675. //Load the already loaded model
  676. model = loadedModels[filepath];
  677. }
  678. editor.setModel(model)
  679. //Bind tab events
  680. bindTabItemEvents();
  681. //Update file info tab
  682. var tabInfo = getFocusedTabInfo();
  683. updateFileStatusDisplay(tabInfo.filepath);
  684. //Hide the editorCover
  685. var ca = getCodeAreaFromEditorUUID(editorUUID);
  686. $(ca).find(".editorcover").hide();
  687. //Write the opened filelist to window hash
  688. if (updateHashStatus){
  689. var currentState = getHashObject();
  690. currentState["files"] = getOpenedFiles();
  691. console.log(currentState);
  692. writeHashObject(currentState);
  693. }
  694. });
  695. }
  696. function hash(s){
  697. return s.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0);
  698. }
  699. //Open fileSelector
  700. function openFileWithSelector(){
  701. ao_module_openFileSelector(externalFileLoader, "user:/Document", "file",true);
  702. hideContextMenu();
  703. }
  704. //Open folderSelector
  705. function openFolderWithSelector(){
  706. ao_module_openFileSelector(openProjectFolder, "user:/Document", "folder");
  707. hideContextMenu();
  708. }
  709. //Handle fileSelector return value
  710. function externalFileLoader(filedata){
  711. if (filedata.length > 0){
  712. for (var i=0; i < filedata.length; i++){
  713. filepath = filedata[i].filepath;
  714. openFile(filepath);
  715. }
  716. }
  717. }
  718. function openProjectFolder(filedata, rewriteHash=true){
  719. for (var i=0; i < filedata.length; i++){
  720. folderpath = filedata[i].filepath;
  721. foldername = filedata[i].filename;
  722. //Load this as the folder
  723. $("#openFolderTitle").text(foldername.toUpperCase());
  724. $("#directoryExplorer").html("");
  725. $.get("../system/file_system/listDir?dir=" + encodeURIComponent(folderpath),function(data){
  726. var folders = [];
  727. var files = [];
  728. if (data === null){
  729. data = [];
  730. }
  731. for (var i =0; i < data.length; i++){
  732. var filename = data[i].Filename;
  733. var ext = filename.split(".").pop();
  734. var icon = ao_module_utils.getIconFromExt(ext);
  735. var filepath = data[i].Filepath;
  736. if (data[i].IsDir){
  737. icon = "folder";
  738. folders.push(`
  739. <div class="folderObjectWrapper">
  740. <div class="item" title="${filepath}" style="overflow:hidden;" onclick="listfolder(this);">
  741. <div class="showmore" style="display:inline-block;"><i class="caret right icon"></i></div>
  742. <i class="${icon} icon"></i> ${filename}
  743. </div>
  744. </div>
  745. `);
  746. }else{
  747. files.push(`
  748. <div class="item" title="${filepath}" style="overflow:hidden;" onclick="openFileViaDirectoryExplorer(this, event);">
  749. <i class="${icon} icon"></i> ${filename}
  750. </div>
  751. `);
  752. }
  753. }
  754. $("#directoryExplorer").append(folders.join(""));
  755. $("#directoryExplorer").append(files.join(""));
  756. });
  757. if (rewriteHash){
  758. var currentState = getHashObject();
  759. currentState["folder"] = foldername;
  760. writeHashObject(currentState);
  761. }
  762. }
  763. }
  764. function listfolder(folder){
  765. var folderPath = $(folder).attr("title");
  766. //Check if the folder is already list. If yes, toggle the list
  767. if ($(folder).parent().find(".dirlist").length == 0){
  768. //Folder is not listed. Load from file system
  769. $(folder).parent().append(`<div class="dirlist"></div>`);
  770. $(folder).find(".showmore i").attr("class","caret down icon");
  771. var targetListObject = $(folder).parent().find(".dirlist");
  772. $.get("../system/file_system/listDir?dir=" + encodeURIComponent(folderPath),function(data){
  773. var folders = [];
  774. var files = [];
  775. if (data === null){
  776. data = [];
  777. }
  778. for (var i =0; i < data.length; i++){
  779. var filename = data[i].Filename;
  780. var ext = filename.split(".").pop();
  781. var icon = ao_module_utils.getIconFromExt(ext);
  782. var filepath = data[i].Filepath;
  783. if (data[i].IsDir){
  784. icon = "folder";
  785. folders.push(`
  786. <div class="folderObjectWrapper">
  787. <div class="subitem" title="${filepath}" style="overflow:hidden;" onclick="listfolder(this);">
  788. <div class="showmore" style="display:inline-block;"><i class="caret right icon"></i></div>
  789. <i class="${icon} icon"></i> ${filename}
  790. </div>
  791. </div>
  792. `);
  793. }else{
  794. files.push(`
  795. <div class="subitem" title="${filepath}" style="overflow:hidden;" onclick="openFileViaDirectoryExplorer(this,event);">
  796. <i class="${icon} icon"></i> ${filename}
  797. </div>
  798. `);
  799. }
  800. }
  801. //Push the items into the list
  802. $(targetListObject).append(folders.join(""));
  803. $(targetListObject).append(files.join(""));
  804. });
  805. }else{
  806. //Folder is already listed. Toggle show of filelist
  807. if ($(folder).find(".caret.down.icon").length > 0){
  808. //Folder list visiable. Hide it
  809. $(folder).parent().find(".dirlist").slideUp('fast');
  810. $(folder).find(".showmore i").attr("class","caret right icon");
  811. }else{
  812. //Folder list hidden. Show it
  813. $(folder).parent().find(".dirlist").slideDown('fast');
  814. $(folder).find(".showmore i").attr("class","caret down icon");
  815. }
  816. }
  817. }
  818. function openFileViaDirectoryExplorer(object, event){
  819. var filepath = $(object).attr('title');
  820. openFile(filepath);
  821. }
  822. function toggleDirectoryExplorer(btn){
  823. if ($(btn).find("i").hasClass("right")){
  824. $(btn).find("i").attr("class","caret down icon");
  825. $("#directoryExplorer").slideDown('fast');
  826. }else{
  827. $(btn).find("i").attr("class","caret right icon");
  828. $("#directoryExplorer").slideUp('fast');
  829. }
  830. }
  831. //Initiate an editor with the given uuid and targetDOM
  832. function initEditor(targetDOM, editorUUID, loadPendingFiles = [], callback=undefined){
  833. require.config({ paths: { 'vs': 'script/monaco/vs' }});
  834. window.GlobalEnvironment = { getWorkerUrl: () => proxy };
  835. let proxy = URL.createObjectURL(new Blob([`
  836. self.GlobalEnvironment = {
  837. baseUrl: 'script/monaco/'
  838. };
  839. importScripts('script/monaco/vs/base/worker/workerMain.js');
  840. `], { type: 'text/javascript' }));
  841. require(["vs/editor/editor.main"], function () {
  842. let editor = monaco.editor.create(targetDOM, {
  843. value: "",
  844. language: 'javascript',
  845. automaticLayout: true,
  846. theme: 'vs-dark'
  847. });
  848. //Bind save event for this editor
  849. editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KEY_S, function() {
  850. let thisEditor = editor;
  851. //Save this editor text to the current editor focused tab folder
  852. var tabInfo = getFocusedTabInfo();
  853. var filepath = tabInfo.filepath;
  854. var content = thisEditor.getValue();
  855. //console.log(filepath, content);
  856. saveContentToFile(tabInfo.tabUUID, filepath, content, function(data){
  857. if (data.error !== undefined){
  858. alert(data.error);
  859. }else{
  860. //OK
  861. console.log("File saved");
  862. msgbox("save","File Saved")
  863. }
  864. });
  865. });
  866. editors.push({
  867. uuid: editorUUID,
  868. tabsMenu: $(targetDOM).parent().find(".tabs"),
  869. editor: editor,
  870. currentTabUUID: "default",
  871. model: [],
  872. state: [],
  873. tabs: []
  874. });
  875. //Check if there are load pending files. If yes, load them into this editor
  876. if (loadPendingFiles !== null && loadPendingFiles.length > 0){
  877. for (var i =0; i < loadPendingFiles.length; i++){
  878. openFile(loadPendingFiles[i].filepath);
  879. }
  880. }
  881. //Initialize user settings
  882. restoreUserPreference();
  883. //Perform callback with editor object if required
  884. if (callback !== undefined){
  885. callback(editor);
  886. }
  887. });
  888. }
  889. /*
  890. Terminal Function Related
  891. */
  892. function terminalInit(){
  893. $(".ui.dropdown").dropdown();
  894. }
  895. var codeAreaBottomOffset = 0;
  896. function openTerminal(){
  897. //Open WsTTY on the editor
  898. codeAreaBottomOffset = 300;
  899. $("#terminals").show();
  900. updateCodeAreaSize();
  901. hideContextMenu();
  902. updateStatusHash("terminal",true);
  903. }
  904. function closeTerminal(){
  905. codeAreaBottomOffset = 0;
  906. $("#terminals").hide();
  907. updateCodeAreaSize();
  908. updateStatusHash("terminal",false);
  909. }
  910. function restartTerminal(){
  911. $("#terminals").find(".terminalWindow.focused").find("iframe").attr("src","");
  912. setTimeout(function(){
  913. $("#terminals").find(".terminalWindow.focused").find("iframe").attr("src",terminalStartingEndpoint);
  914. }, 1000);
  915. }
  916. $(window).on("resize", function(data){
  917. updateCodeAreaSize();
  918. });
  919. updateCodeAreaSize();
  920. function updateCodeAreaSize(){
  921. $("#codeArea").css({
  922. "height":window.innerHeight - 52 - codeAreaBottomOffset
  923. });
  924. editors.forEach(editor => {
  925. editor.editor.layout();
  926. })
  927. }
  928. function openFileInFileManager(){
  929. var tabInfo = getFocusedTabInfo();
  930. var filepath = tabInfo.filepath;
  931. //Extract the parent dir
  932. var folderPath = "";
  933. if (filepath.substr(filepath.length -1) == "/"){
  934. var tmp = filepath.split("/");
  935. tmp.pop();
  936. folderPath = tmp.pop();
  937. }else{
  938. folderPath = filepath.split("/").pop();
  939. }
  940. ao_module_openPath(folderPath);
  941. }
  942. function getHashObject(){
  943. if (window.location.hash.length > 1){
  944. var stateString = window.location.hash.substr(1);
  945. var currentState = JSON.parse(decodeURIComponent(stateString));
  946. return currentState
  947. }else{
  948. return {};
  949. }
  950. }
  951. function writeHashObject(newStateObject){
  952. window.location.hash = JSON.stringify(newStateObject);
  953. }
  954. function updateStatusHash(key, value){
  955. var currentState = getHashObject();
  956. currentState[key] = value;
  957. writeHashObject(currentState);
  958. }
  959. function getOpenedFiles(){
  960. var openedFilepaths = [];
  961. editors.forEach(editor => {
  962. editor.tabs.forEach(tab => {
  963. openedFilepaths.push(tab.filepath);
  964. })
  965. });
  966. return openedFilepaths;
  967. }
  968. function changeTab(editorObject, targettabUUID) {
  969. //Move focus to the selected editor
  970. focusedEditor = editorObject.uuid;
  971. //Get the basic information for the editor object
  972. var editor = editorObject.editor;
  973. var models = editorObject.model;
  974. var states = editorObject.state;
  975. var currentTabUUID = editorObject.currentTabUUID;
  976. //save the current editor states
  977. var currentState = editor.saveViewState();
  978. var currentModel = editor.getModel();
  979. states[currentTabUUID] = currentState;
  980. models[currentTabUUID] = currentModel;
  981. //Update the current Tab UUID
  982. editorObject.currentTabUUID = targettabUUID;
  983. //Load the content from the given uuid tab
  984. editor.setModel(models[targettabUUID]);
  985. editor.restoreViewState(states[targettabUUID]);
  986. editor.focus();
  987. //Update the target file info
  988. var tabInfo = getFocusedTabInfo();
  989. updateFileStatusDisplay(tabInfo.filepath);
  990. //Update title if nonVDI or update floatWindow title if under fdi
  991. if (ao_module_virtualDesktop){
  992. ao_module_setWindowTitle("NotepadA - " + tabInfo.filename);
  993. }else{
  994. document.title = "NotepadA - " + tabInfo.filename
  995. }
  996. //Focus this tab
  997. focusTabWithUUID(targettabUUID);
  998. }
  999. //Show clipboard reminders
  1000. function showClipboardReminders(rt){
  1001. var reminderText = "This function is keyboard shortcut only.";
  1002. var iconClass = "keyboard icon";
  1003. if (rt == 1){
  1004. reminderText = "Use the keyboard shortcut Ctrl + X to cut text from the editor.";
  1005. iconClass = "cut icon";
  1006. }else if (rt == 2){
  1007. reminderText = "Use the keyboard shortcut Ctrl + C for copying text from the editor";
  1008. iconClass = "copy icon";
  1009. }else if (rt == 3){
  1010. reminderText = "Use the keyboard shortcut Ctrl + V for pasting to editor.";
  1011. iconClass = "paste icon";
  1012. }else if (rt == 4){
  1013. reminderText = "Press Ctrl + F on the editor to start searching.";
  1014. iconClass = "search icon";
  1015. }else if (rt == 5){
  1016. reminderText = "Press Ctrl + H on the editor to start replacing text.";
  1017. iconClass = "sync alternate icon";
  1018. }
  1019. $("#clipboardReminderText").text(reminderText);
  1020. $("#clipboardReminderIcon").attr("class",iconClass)
  1021. $('#clipboardReminder').modal('show');
  1022. hideContextMenu();
  1023. }
  1024. //Editor function binding
  1025. function undo(){
  1026. editorObject = getFocusedEditorObject();
  1027. editorObject.editor.trigger('aaa','undo','aaa');
  1028. editorObject.editor.focus();
  1029. }
  1030. function redo(){
  1031. editorObject = getFocusedEditorObject();
  1032. editorObject.editor.trigger('aaa','redo','aaa');
  1033. editorObject.editor.focus();
  1034. }
  1035. function saveCurrentFile(){
  1036. var tabInfo = getFocusedTabInfo();
  1037. var focusedEditor = getFocusedEditorObject();
  1038. var filepath = tabInfo.filepath;
  1039. var content = focusedEditor.editor.getValue();
  1040. saveContentToFile(tabInfo.tabUUID, filepath, content, function(data){
  1041. if (data.error !== undefined){
  1042. alert(data.error);
  1043. }else{
  1044. //OK
  1045. console.log("File saved");
  1046. msgbox("save","File Saved")
  1047. }
  1048. });
  1049. hideContextMenu();
  1050. }
  1051. function saveContentToFile(tabUUID, filepath, content, callback=undefined){
  1052. syscall("writeFile",{"filepath":filepath, "content":content}, function(data){
  1053. if (callback !== undefined){
  1054. callback(data);
  1055. }
  1056. var contextHash = content.hashCode();
  1057. //Update all the tabs that contains this filepath
  1058. for (var i =0; i < editors.length; i++){
  1059. var thisEditor = editors[i];
  1060. for (var j =0; j < thisEditor.tabs.length; j++){
  1061. var thisTab = thisEditor.tabs[j];
  1062. if (thisTab.tabUUID == tabUUID){
  1063. thisTab.saveHash = contextHash;
  1064. }
  1065. }
  1066. }
  1067. });
  1068. }
  1069. function msgbox(icon, message){
  1070. $("#messageField").html(`<i class="${icon} icon"></i> ${message}`);
  1071. $("#messageField").stop().finish().hide().fadeIn('fast').delay(5000).fadeOut('fast');
  1072. }
  1073. function setStorage(key, value){
  1074. syscall("store",{opr:"set",key:key,value:value});
  1075. /*
  1076. $.post("./store",{opr: "set", key: key, value: value}).done(function(data){
  1077. if (data.error !== undefined){
  1078. console.log(data);
  1079. }
  1080. });
  1081. */
  1082. }
  1083. function getStorage(key, callback){
  1084. syscall("store",{opr:"get",key:key}, callback);
  1085. //$.post("./store",{opr: "get", key: key}).done(callback);
  1086. }
  1087. function hanleUserExit(){
  1088. //Check for unsaved file, wip
  1089. if (confirm("Some files might not be saved. Confirm exit?")){
  1090. ao_module_close();
  1091. }
  1092. }
  1093. //Hashcode function for comparing save stated
  1094. String.prototype.hashCode = function() {
  1095. var hash = 0;
  1096. if (this.length == 0) {
  1097. return hash;
  1098. }
  1099. for (var i = 0; i < this.length; i++) {
  1100. var char = this.charCodeAt(i);
  1101. hash = ((hash<<5)-hash)+char;
  1102. hash = hash & hash; // Convert to 32bit integer
  1103. }
  1104. return hash;
  1105. }
  1106. //Call to the system core with AJGI script
  1107. function syscall(scriptName, data, callback=undefined){
  1108. $.ajax({
  1109. url: "../system/ajgi/interface?script=NotepadA/backend/" + scriptName + ".agi",
  1110. method: "POST",
  1111. data: data,
  1112. success: function(data){
  1113. if (callback !== undefined){
  1114. callback(data);
  1115. }
  1116. }
  1117. })
  1118. }
  1119. </script>
  1120. </body>
  1121. </html>