ws-editor.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* global window,$,ace,setTimeout,rp,_ws */
  2. "use strict";
  3. $(document).ready(function() {
  4. // console.log('ws: init editor');
  5. var $editorWrapper = $('#editor-wrapper');
  6. var $editor = $('#editor');
  7. var $saveChanges = $('#save-changes');
  8. var $revertEditor = $('#revert-editor');
  9. var $panelLeft = $('.panel-left');
  10. // var $panelRight = $('.panel-right');
  11. // var $panelWrap = $('.panel-container');
  12. var $tabItems = $('#tabs li');
  13. var $detailsRepo = $('#details-repo');
  14. var $detailsExmp = $('#details-example');
  15. var $window = $(window);
  16. var activeMode = 'html';
  17. var currentFileIdx;
  18. var editor;
  19. var editorSession;
  20. var editorStorage = new LocalStorageDraft();
  21. // var saveTimeout1;
  22. // var saveTimeout2;
  23. var mapTypes = {
  24. html: 'html', js: 'javascript', css: 'css'
  25. }
  26. _ws.events.on('navToRoot', function() {
  27. $editorWrapper.hide();
  28. $detailsRepo.hide();
  29. $detailsExmp.hide();
  30. });
  31. _ws.events.on('navToRepo', function(repoSlug) {
  32. $editorWrapper.hide();
  33. $detailsRepo.show();
  34. $detailsExmp.hide();
  35. });
  36. // https://stackoverflow.com/questions/93695/best-cross-browser-method-to-capture-ctrls-with-jquery#answer-14180949
  37. $(window).bind('keydown', function(event) {
  38. if (event.ctrlKey || event.metaKey) {
  39. switch (String.fromCharCode(event.which).toLowerCase()) {
  40. case 's':
  41. event.preventDefault();
  42. (onSaveChanges.bind(_ws.ui.tabs))();
  43. break;
  44. }
  45. }
  46. });
  47. // /**
  48. // * Make the left panel resizable
  49. // */
  50. $panelLeft.resizable({
  51. handleSelector: ".splitter",
  52. resizeHeight: false,
  53. onDrag: function(e) {
  54. $editor.width($panelLeft.width());
  55. }
  56. });
  57. function setDefaultEditorContent() {
  58. if(_ws.files.length > 0) {
  59. editorSession.setMode("ace/mode/html");
  60. var firstHtml = _ws.files.find(f => (f.type === 'html'));
  61. _ws.ui.tabs.setActiveTab(firstHtml);
  62. editorSession.setValue(firstHtml.content);
  63. }
  64. }
  65. function initEditor() {
  66. editor = ace.edit("editor");
  67. editorSession = editor.getSession();
  68. editor.setTheme("ace/theme/eclipse");
  69. editor.$blockScrolling = Infinity;
  70. editorSession.setUseWrapMode(true);
  71. setDefaultEditorContent();
  72. }
  73. function onTabClicked(e) {
  74. var clickedItem = $(e.target);
  75. var activeTab = _ws.ui.tabs.getActiveTab();
  76. if(activeTab[0] === clickedItem[0] || clickedItem[0].tagName === 'SPAN') {
  77. return true;
  78. }
  79. if(activeTab.hasClass('dirty')) {
  80. var proceed = window.confirm("Vos changements sur " + activeTab.data('name') +
  81. " seront perdus si vous changez de fichier. Continuer tout de même ?");
  82. if(! proceed) {
  83. return;
  84. }
  85. activeTab.removeClass('dirty');
  86. }
  87. var type = clickedItem.data('type');
  88. editorSession.setMode("ace/mode/" + mapTypes[type]);
  89. var name = clickedItem.data('name');
  90. var file = _ws.files.find(f => (f.name === name));
  91. _ws.ui.tabs.setActiveTab(file);
  92. editor.getSession().off('change', _ws.ui.editor.contentChanged);
  93. editorSession.setValue(file.content);
  94. editor.getSession().on('change', _ws.ui.editor.contentChanged);
  95. // saveToLocalStorage();
  96. // var mode = $(this).prop('id').substr(5);
  97. // console.log( .html() );
  98. // setActiveTab(mode);
  99. }
  100. function setFirstActiveTab() {
  101. var firstHtml = _ws.files.find(f => (f.type === 'html'));
  102. this.setActiveTab(firstHtml);
  103. }
  104. _ws.makeView('tabs', {
  105. setActiveTab: function(file) {
  106. this.file = _ws.files.find(f => (f.name === file.name));
  107. if(this.activeTab) {
  108. this.activeTab.removeClass('bold');
  109. }
  110. var idx = this.fileIdx = _ws.files.indexOf(this.file);
  111. this.activeTab = this.$elem.find('li:eq(' + idx + ')');
  112. this.activeTab.addClass('bold');
  113. },
  114. setFirstActiveTab: setFirstActiveTab,
  115. afterRender: setFirstActiveTab,
  116. getActiveTab: function() {
  117. return this.activeTab;
  118. },
  119. events: {
  120. 'click li[data-type]': onTabClicked,
  121. 'click li[data-type] span': onSaveChanges
  122. }
  123. })
  124. initEditor();
  125. _ws.makeView('editor', {
  126. aceEditor: editor,
  127. aceSession: editor.getSession(),
  128. init: function() {
  129. this.aceSession.on('change', this.contentChanged);
  130. },
  131. /**
  132. * React to changes in editor by saving a copy
  133. */
  134. contentChanged: function() {
  135. // console.log('content changed handler', editorSession.getValue());
  136. _ws.ui.tabs.getActiveTab()
  137. .addClass('dirty');
  138. // if(saveTimeout1 || saveTimeout2) {
  139. // clearTimeout(saveTimeout1);
  140. // clearTimeout(saveTimeout2);
  141. // }
  142. // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  143. // // saveTimeout2 = setTimeout(saveChanges, 1000);
  144. },
  145. render: function() {
  146. // console.log('render editor', this);
  147. setDefaultEditorContent();
  148. $editorWrapper.show();
  149. this.$elem.removeClass('hidden');
  150. }
  151. });
  152. _ws.makeView('sandbox-iframe', {
  153. reload: function() {
  154. var src = this.$elem.prop('src');
  155. this.$elem.prop('src', '/empty.html');
  156. this.$elem.prop('src', src);
  157. },
  158. render: function(src) {
  159. this.$elem.prop('src', src);
  160. }
  161. });
  162. // /**
  163. // * Set editor mode (html, javascript, css)
  164. // */
  165. // function setEditorMode(mode) {
  166. // editor.getSession().setMode("ace/mode/" + mode);
  167. // }
  168. // /**
  169. // * Save a copy in localStorage
  170. // */
  171. // function saveToLocalStorage() {
  172. // var editorContent = editor.getSession().getValue();
  173. // editorStorage.saveSource(activeMode, editorContent);
  174. // saveTimeout1 = undefined;
  175. // }
  176. // function revertEditor() {
  177. // editorStorage.reset();
  178. // location.reload();
  179. // }
  180. function onSaveChanges() {
  181. // console.log('Saving', this.file, editorSession.getValue())
  182. var filename = this.file.name;
  183. // var payload = editorStorage.getSources();
  184. var payload = {
  185. content: editorSession.getValue()
  186. };
  187. // Submit the new file content to server
  188. rp.put('/' + _ws.repo.path + '/examples/' + _ws.example.slug + '/files/' + filename, payload)
  189. .then(function(updatedFile) {
  190. // notify
  191. _ws.notify('success', "Fichier " + filename + " sauvegardé !");
  192. // remove the dirty state on current editor tab
  193. _ws.ui.tabs.getActiveTab().removeClass('dirty');
  194. // replace content with the updated one in _ws.files
  195. var originalFile = _ws.files.find(f => (f.name === updatedFile.name));
  196. var index = _ws.files.indexOf(originalFile);
  197. _ws.files[index].content = updatedFile.content;
  198. // reload sandbox iframe
  199. _ws.ui.sandboxIframe.reload();
  200. })
  201. .catch(function(err) {
  202. // console.error(err);
  203. _ws.notify('error', 'Erreur: ' + err.message);
  204. });
  205. }
  206. // $fileSelect.change(function() {
  207. // loadExample($(this).val());
  208. // });
  209. // $saveChanges.click(saveChanges);
  210. // $revertEditor.click(revertEditor);
  211. // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
  212. // loadExampleList();
  213. });