ws-editor.js 9.0 KB

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