ws-editor.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. }
  86. });
  87. _ws.makeView('sandbox-iframe', {
  88. render: function(navState) {
  89. console.log('change iframe src', '/examples/' + navState.repo + '/' + navState.example);
  90. this.$elem.prop('src',
  91. '/examples/' + navState.repo + '/' + navState.example
  92. );
  93. }
  94. });
  95. // /**
  96. // * Set the current hash. If none given, take it from lococation.hash
  97. // */
  98. // function setCurrentHash(slug) {
  99. // if(slug) {
  100. // window.location.hash = currentHash = slug;
  101. // }
  102. // else {
  103. // currentHash = window.location.hash ?
  104. // window.location.hash.substr(1) : undefined;
  105. // }
  106. // }
  107. // /**
  108. // * Set editor mode (html, javascript, css)
  109. // */
  110. // function setEditorMode(mode) {
  111. // editor.getSession().setMode("ace/mode/" + mode);
  112. // }
  113. // /**
  114. // * Save a copy in localStorage
  115. // */
  116. // function saveToLocalStorage() {
  117. // var editorContent = editor.getSession().getValue();
  118. // editorStorage.saveSource(activeMode, editorContent);
  119. // saveTimeout1 = undefined;
  120. // }
  121. // /**
  122. // * React to changes in editor by saving a copy
  123. // */
  124. // function editorContentChanged() {
  125. // // console.log('editorContent changed')
  126. // if(saveTimeout1 || saveTimeout2) {
  127. // clearTimeout(saveTimeout1);
  128. // clearTimeout(saveTimeout2);
  129. // }
  130. // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  131. // // saveTimeout2 = setTimeout(saveChanges, 1000);
  132. // }
  133. // editor.getSession().on('change', editorContentChanged);
  134. // function setActiveTab(mode) {
  135. // console.log('setting mode', mode);
  136. // var elementId = 'show-' + mode;
  137. // $('#show-' + activeMode).removeClass('active');
  138. // activeMode = mode;
  139. // $('#' + elementId).addClass('active');
  140. // var ed = $('#editor-' + mode);
  141. // setEditorMode(mode);
  142. // editor.getSession().off('change');
  143. // editor.getSession().setValue(ed[0].innerHTML);
  144. // editor.getSession().on('change', editorContentChanged);
  145. // }
  146. // function loadExample(exampleSlug) {
  147. // // console.log('loadExample', exampleSlug);
  148. // var serverPath = 'exemples/jquery/' + exampleSlug + '/';
  149. // rp.get(serverPath + 'script.js', 'text')
  150. // .then(javascript => $editorJs.html(javascript))
  151. // .then(() => rp.get(serverPath + 'example.html', 'text'))
  152. // .then(() => rp.get(serverPath + 'styles.css', 'text')
  153. // .then(css => $editorCss.html(css))
  154. // .catch(err => {
  155. // return '';
  156. // })
  157. // )
  158. // .then(html => {
  159. // $editorHtml.html(html);
  160. // $('iframe.panel-right')
  161. // .prop('src', '/examples/' + exampleSlug)
  162. // setActiveTab('html');
  163. // setCurrentHash(exampleSlug);
  164. // var sources = {
  165. // html: $editorHtml.html(),
  166. // javascript: $editorJs.html()
  167. // };
  168. // editorStorage.init(exampleSlug, sources);
  169. // })
  170. // .then(() => {
  171. // var item = _.find(exampleList, { slug: exampleSlug });
  172. // // console.log(item.test ? 'test' : 'no test');
  173. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  174. // // $('#tests').show();
  175. // // });
  176. // });
  177. // }
  178. // function loadExampleList() {
  179. // rp.get('/list/jquery', 'json')
  180. // .then(function(_exampleList) {
  181. // exampleList = _exampleList;
  182. // var restoredDraft;
  183. // resetFileSelect(exampleList);
  184. // if(currentHash) {
  185. // $fileSelect.val(currentHash);
  186. // var item = _.find(exampleList, { slug: currentHash });
  187. // if( ! item) {
  188. // return;
  189. // }
  190. // restoredDraft = editorStorage.restore(item.slug);
  191. // if(! restoredDraft) {
  192. // loadExample(item.slug);
  193. // }
  194. // else {
  195. // console.log('restore', item.slug, restoredDraft.sources);
  196. // $editorHtml.html(restoredDraft.sources.html);
  197. // $editorCss.html(restoredDraft.sources.css);
  198. // $editorJs.html(restoredDraft.sources.javascript);
  199. // $('iframe.panel-right')
  200. // .prop('src', '/examples/' + item.slug)
  201. // // loadJS('exemples/' + item.slug + '/script.js');
  202. // // if(item.test) {
  203. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  204. // // $('#tests').show();
  205. // // });
  206. // // }
  207. // setActiveTab('html');
  208. // }
  209. // }
  210. // });
  211. // }
  212. // function revertEditor() {
  213. // editorStorage.reset();
  214. // location.reload();
  215. // }
  216. // function saveChanges() {
  217. // var payload = editorStorage.getSources();
  218. // rp.put('/examples/' + currentHash, payload)
  219. // .then(function(newExample) {
  220. // notify('success', "Exemple sauvegardé !");
  221. // loadExample(currentHash);
  222. // })
  223. // .catch(function(err) {
  224. // notify('error', 'Erreur: ' + jqXHR.responseText);
  225. // });
  226. // }
  227. // $fileSelect.change(function() {
  228. // loadExample($(this).val());
  229. // });
  230. // $saveChanges.click(saveChanges);
  231. // $revertEditor.click(revertEditor);
  232. // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
  233. // loadExampleList();
  234. });
  235. // var $editorJs = $('#editor-javascript');
  236. // var $editorHtml = $('#editor-html');
  237. // var $editorCss = $('#editor-css');
  238. // var $htmlContent = $('#html-content'); // replaced by iframe
  239. // var $selectorNav = $('#selector');
  240. // /**
  241. // * Handle window resize
  242. // */
  243. // function onResize() {
  244. // $editor.width($panelLeft.width());
  245. // $panelWrap.height($window.height());
  246. // }
  247. // $window.resize(onResize);
  248. // onResize();