editor.js 8.9 KB

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