ws-editor.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. console.log('log events from editor', _ws);
  27. _ws.events.on('navToRoot', function() {
  28. $editorWrapper.hide();
  29. $detailsRepo.hide();
  30. $detailsExmp.hide();
  31. });
  32. _ws.events.on('navToRepo', function(repoSlug) {
  33. console.log('got navToRepo', repoSlug);
  34. $editorWrapper.hide();
  35. $detailsRepo.show();
  36. $detailsExmp.hide();
  37. });
  38. // /**
  39. // * Make the left panel resizable
  40. // */
  41. $panelLeft.resizable({
  42. handleSelector: ".splitter",
  43. resizeHeight: false,
  44. onDrag: function(e) {
  45. $editor.width($panelLeft.width());
  46. }
  47. });
  48. function setDefaultEditorContent() {
  49. if(_ws.files.length > 0) {
  50. editorSession.setMode("ace/mode/html");
  51. var firstHtml = _ws.files.find(f => (f.type === 'html'));
  52. _ws.ui.tabs.setActiveTab(firstHtml);
  53. editorSession.setValue(firstHtml.content);
  54. }
  55. }
  56. function initEditor() {
  57. editor = ace.edit("editor");
  58. editorSession = editor.getSession();
  59. editor.setTheme("ace/theme/eclipse");
  60. editor.$blockScrolling = Infinity;
  61. editorSession.setUseWrapMode(true);
  62. setDefaultEditorContent();
  63. }
  64. function onTabClicked(e) {
  65. var activeTab = _ws.ui.tabs.getActiveTab();
  66. if(activeTab.hasClass('dirty')) {
  67. var proceed = window.confirm("Vos changements sur " + activeTab.data('name') +
  68. " seront perdus si vous changez de fichier. Continuer tout de même ?");
  69. if(! proceed) {
  70. return;
  71. }
  72. activeTab.removeClass('dirty');
  73. }
  74. var clickedItem = $(e.target);
  75. var type = clickedItem.data('type');
  76. editorSession.setMode("ace/mode/" + mapTypes[type]);
  77. var name = clickedItem.data('name');
  78. var file = _ws.files.find(f => (f.name === name));
  79. console.log('activate', name, file);
  80. _ws.ui.tabs.setActiveTab(file);
  81. console.log('remove change handler', _ws.ui.editor.contentChanged)
  82. editor.getSession().off('change', _ws.ui.editor.contentChanged);
  83. editorSession.setValue(file.content);
  84. editor.getSession().on('change', _ws.ui.editor.contentChanged);
  85. // saveToLocalStorage();
  86. // var mode = $(this).prop('id').substr(5);
  87. // console.log( .html() );
  88. // setActiveTab(mode);
  89. }
  90. _ws.makeView('tabs', {
  91. setActiveTab: function(file) {
  92. var file = _ws.files.find(f => (f.name === file.name));
  93. if(this.activeTab) {
  94. this.activeTab.removeClass('bold');
  95. }
  96. console.log('set active tab', idx, file);
  97. var idx = this.fileIdx = _ws.files.indexOf(file);
  98. this.activeTab = this.$elem.find('li:eq(' + idx + ')');
  99. this.activeTab.addClass('bold');
  100. },
  101. getActiveTab: function() {
  102. return this.activeTab;
  103. },
  104. events: {
  105. 'click li[data-type]': onTabClicked
  106. }
  107. })
  108. initEditor();
  109. _ws.makeView('editor', {
  110. aceEditor: editor,
  111. aceSession: editor.getSession(),
  112. init: function() {
  113. this.aceSession.on('change', this.contentChanged);
  114. },
  115. /**
  116. * React to changes in editor by saving a copy
  117. */
  118. contentChanged: function() {
  119. console.log('content changed handler');
  120. _ws.ui.tabs.getActiveTab()
  121. .addClass('dirty');
  122. // if(saveTimeout1 || saveTimeout2) {
  123. // clearTimeout(saveTimeout1);
  124. // clearTimeout(saveTimeout2);
  125. // }
  126. // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  127. // // saveTimeout2 = setTimeout(saveChanges, 1000);
  128. },
  129. render: function() {
  130. console.log('render editor', this);
  131. setDefaultEditorContent();
  132. $editorWrapper.show();
  133. this.$elem.removeClass('hidden');
  134. }
  135. });
  136. _ws.makeView('sandbox-iframe', {
  137. render: function(src) {
  138. this.$elem.prop('src', src);
  139. }
  140. });
  141. // /**
  142. // * Set editor mode (html, javascript, css)
  143. // */
  144. // function setEditorMode(mode) {
  145. // editor.getSession().setMode("ace/mode/" + mode);
  146. // }
  147. // /**
  148. // * Save a copy in localStorage
  149. // */
  150. // function saveToLocalStorage() {
  151. // var editorContent = editor.getSession().getValue();
  152. // editorStorage.saveSource(activeMode, editorContent);
  153. // saveTimeout1 = undefined;
  154. // }
  155. // function revertEditor() {
  156. // editorStorage.reset();
  157. // location.reload();
  158. // }
  159. // function saveChanges() {
  160. // var payload = editorStorage.getSources();
  161. // rp.put('/examples/' + currentHash, payload)
  162. // .then(function(newExample) {
  163. // notify('success', "Exemple sauvegardé !");
  164. // loadExample(currentHash);
  165. // })
  166. // .catch(function(err) {
  167. // notify('error', 'Erreur: ' + jqXHR.responseText);
  168. // });
  169. // }
  170. // $fileSelect.change(function() {
  171. // loadExample($(this).val());
  172. // });
  173. // $saveChanges.click(saveChanges);
  174. // $revertEditor.click(revertEditor);
  175. // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
  176. // loadExampleList();
  177. });