ws-editor.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 editor;
  18. var editorSession;
  19. var editorStorage = new LocalStorageDraft();
  20. // var saveTimeout1;
  21. // var saveTimeout2;
  22. var mapTypes = {
  23. html: 'html', js: 'javascript', css: 'css'
  24. }
  25. console.log('log events from editor', _ws);
  26. _ws.events.on('navToRoot', function() {
  27. $editorWrapper.hide();
  28. $detailsRepo.hide();
  29. $detailsExmp.hide();
  30. });
  31. _ws.events.on('navToRepo', function(repoSlug) {
  32. console.log('got navToRepo', repoSlug);
  33. $editorWrapper.hide();
  34. $detailsRepo.show();
  35. $detailsExmp.hide();
  36. });
  37. // /**
  38. // * Make the left panel resizable
  39. // */
  40. $panelLeft.resizable({
  41. handleSelector: ".splitter",
  42. resizeHeight: false,
  43. onDrag: function(e) {
  44. $editor.width($panelLeft.width());
  45. }
  46. });
  47. function setDefaultEditorContent() {
  48. if(_ws.files.length > 0) {
  49. editorSession.setMode("ace/mode/html");
  50. var firstHtml = _ws.files.find(f => (f.type === 'html'));
  51. editorSession.setValue(firstHtml.content);
  52. }
  53. }
  54. function initEditor() {
  55. editor = ace.edit("editor");
  56. editorSession = editor.getSession();
  57. editor.setTheme("ace/theme/eclipse");
  58. editor.$blockScrolling = Infinity;
  59. editorSession.setUseWrapMode(true);
  60. setDefaultEditorContent();
  61. }
  62. function onTabClicked(e) {
  63. var clickedItem = $(e.target);
  64. var type = clickedItem.data('type');
  65. editorSession.setMode("ace/mode/" + mapTypes[type]);
  66. var name = clickedItem.html();
  67. var file = _ws.files.find(f => (f.name === name));
  68. editorSession.setValue(file.content);
  69. // saveToLocalStorage();
  70. // var mode = $(this).prop('id').substr(5);
  71. // console.log( .html() );
  72. // setActiveTab(mode);
  73. }
  74. initEditor();
  75. _ws.makeView('tabs', {
  76. events: {
  77. 'click li[data-type]': onTabClicked
  78. }
  79. })
  80. _ws.makeView('editor', {
  81. render: function() {
  82. console.log('render editor', this);
  83. setDefaultEditorContent();
  84. $editorWrapper.show();
  85. this.$elem.removeClass('hidden');
  86. }
  87. });
  88. _ws.makeView('sandbox-iframe', {
  89. render: function(src) {
  90. // console.log('change iframe src', '/examples/' + navState.repo + '/' + navState.example);
  91. this.$elem.prop('src', src);
  92. }
  93. });
  94. // /**
  95. // * Set the current hash. If none given, take it from lococation.hash
  96. // */
  97. // function setCurrentHash(slug) {
  98. // if(slug) {
  99. // window.location.hash = currentHash = slug;
  100. // }
  101. // else {
  102. // currentHash = window.location.hash ?
  103. // window.location.hash.substr(1) : undefined;
  104. // }
  105. // }
  106. // /**
  107. // * Set editor mode (html, javascript, css)
  108. // */
  109. // function setEditorMode(mode) {
  110. // editor.getSession().setMode("ace/mode/" + mode);
  111. // }
  112. // /**
  113. // * Save a copy in localStorage
  114. // */
  115. // function saveToLocalStorage() {
  116. // var editorContent = editor.getSession().getValue();
  117. // editorStorage.saveSource(activeMode, editorContent);
  118. // saveTimeout1 = undefined;
  119. // }
  120. // /**
  121. // * React to changes in editor by saving a copy
  122. // */
  123. // function editorContentChanged() {
  124. // // console.log('editorContent changed')
  125. // if(saveTimeout1 || saveTimeout2) {
  126. // clearTimeout(saveTimeout1);
  127. // clearTimeout(saveTimeout2);
  128. // }
  129. // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  130. // // saveTimeout2 = setTimeout(saveChanges, 1000);
  131. // }
  132. // editor.getSession().on('change', editorContentChanged);
  133. // function setActiveTab(mode) {
  134. // console.log('setting mode', mode);
  135. // var elementId = 'show-' + mode;
  136. // $('#show-' + activeMode).removeClass('active');
  137. // activeMode = mode;
  138. // $('#' + elementId).addClass('active');
  139. // var ed = $('#editor-' + mode);
  140. // setEditorMode(mode);
  141. // editor.getSession().off('change');
  142. // editor.getSession().setValue(ed[0].innerHTML);
  143. // editor.getSession().on('change', editorContentChanged);
  144. // }
  145. // function loadExample(exampleSlug) {
  146. // // console.log('loadExample', exampleSlug);
  147. // var serverPath = 'exemples/jquery/' + exampleSlug + '/';
  148. // rp.get(serverPath + 'script.js', 'text')
  149. // .then(javascript => $editorJs.html(javascript))
  150. // .then(() => rp.get(serverPath + 'example.html', 'text'))
  151. // .then(() => rp.get(serverPath + 'styles.css', 'text')
  152. // .then(css => $editorCss.html(css))
  153. // .catch(err => {
  154. // return '';
  155. // })
  156. // )
  157. // .then(html => {
  158. // $editorHtml.html(html);
  159. // $('iframe.panel-right')
  160. // .prop('src', '/examples/' + exampleSlug)
  161. // setActiveTab('html');
  162. // setCurrentHash(exampleSlug);
  163. // var sources = {
  164. // html: $editorHtml.html(),
  165. // javascript: $editorJs.html()
  166. // };
  167. // editorStorage.init(exampleSlug, sources);
  168. // })
  169. // .then(() => {
  170. // var item = _.find(exampleList, { slug: exampleSlug });
  171. // // console.log(item.test ? 'test' : 'no test');
  172. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  173. // // $('#tests').show();
  174. // // });
  175. // });
  176. // }
  177. // function loadExampleList() {
  178. // rp.get('/list/jquery', 'json')
  179. // .then(function(_exampleList) {
  180. // exampleList = _exampleList;
  181. // var restoredDraft;
  182. // resetFileSelect(exampleList);
  183. // if(currentHash) {
  184. // $fileSelect.val(currentHash);
  185. // var item = _.find(exampleList, { slug: currentHash });
  186. // if( ! item) {
  187. // return;
  188. // }
  189. // restoredDraft = editorStorage.restore(item.slug);
  190. // if(! restoredDraft) {
  191. // loadExample(item.slug);
  192. // }
  193. // else {
  194. // console.log('restore', item.slug, restoredDraft.sources);
  195. // $editorHtml.html(restoredDraft.sources.html);
  196. // $editorCss.html(restoredDraft.sources.css);
  197. // $editorJs.html(restoredDraft.sources.javascript);
  198. // $('iframe.panel-right')
  199. // .prop('src', '/examples/' + item.slug)
  200. // // loadJS('exemples/' + item.slug + '/script.js');
  201. // // if(item.test) {
  202. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  203. // // $('#tests').show();
  204. // // });
  205. // // }
  206. // setActiveTab('html');
  207. // }
  208. // }
  209. // });
  210. // }
  211. // function revertEditor() {
  212. // editorStorage.reset();
  213. // location.reload();
  214. // }
  215. // function saveChanges() {
  216. // var payload = editorStorage.getSources();
  217. // rp.put('/examples/' + currentHash, payload)
  218. // .then(function(newExample) {
  219. // notify('success', "Exemple sauvegardé !");
  220. // loadExample(currentHash);
  221. // })
  222. // .catch(function(err) {
  223. // notify('error', 'Erreur: ' + jqXHR.responseText);
  224. // });
  225. // }
  226. // $fileSelect.change(function() {
  227. // loadExample($(this).val());
  228. // });
  229. // $saveChanges.click(saveChanges);
  230. // $revertEditor.click(revertEditor);
  231. // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
  232. // loadExampleList();
  233. });
  234. // var $editorJs = $('#editor-javascript');
  235. // var $editorHtml = $('#editor-html');
  236. // var $editorCss = $('#editor-css');
  237. // var $htmlContent = $('#html-content'); // replaced by iframe
  238. // var $selectorNav = $('#selector');
  239. // /**
  240. // * Handle window resize
  241. // */
  242. // function onResize() {
  243. // $editor.width($panelLeft.width());
  244. // $panelWrap.height($window.height());
  245. // }
  246. // $window.resize(onResize);
  247. // onResize();