editor.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. $('#menu-btn').click(() => {
  34. $('#nav-menus').toggleClass('in');
  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. $tabItems.click(function() {
  68. var clickedItem = $(this);
  69. var type = clickedItem.data('type');
  70. editorSession.setMode("ace/mode/" + mapTypes[type]);
  71. var name = clickedItem.html();
  72. var file = _webSandboxFiles.find(f => (f.name === name));
  73. console.log(type, mapTypes[type], name, file);
  74. editorSession.setValue(file.content);
  75. // saveToLocalStorage();
  76. // var mode = $(this).prop('id').substr(5);
  77. // console.log( .html() );
  78. // setActiveTab(mode);
  79. })
  80. // /**
  81. // * Set the current hash. If none given, take it from lococation.hash
  82. // */
  83. // function setCurrentHash(slug) {
  84. // if(slug) {
  85. // window.location.hash = currentHash = slug;
  86. // }
  87. // else {
  88. // currentHash = window.location.hash ?
  89. // window.location.hash.substr(1) : undefined;
  90. // }
  91. // }
  92. // /**
  93. // * Set editor mode (html, javascript, css)
  94. // */
  95. // function setEditorMode(mode) {
  96. // editor.getSession().setMode("ace/mode/" + mode);
  97. // }
  98. // /**
  99. // * Save a copy in localStorage
  100. // */
  101. // function saveToLocalStorage() {
  102. // var editorContent = editor.getSession().getValue();
  103. // editorStorage.saveSource(activeMode, editorContent);
  104. // saveTimeout1 = undefined;
  105. // }
  106. // /**
  107. // * React to changes in editor by saving a copy
  108. // */
  109. // function editorContentChanged() {
  110. // // console.log('editorContent changed')
  111. // if(saveTimeout1 || saveTimeout2) {
  112. // clearTimeout(saveTimeout1);
  113. // clearTimeout(saveTimeout2);
  114. // }
  115. // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  116. // // saveTimeout2 = setTimeout(saveChanges, 1000);
  117. // }
  118. // editor.getSession().on('change', editorContentChanged);
  119. // function setActiveTab(mode) {
  120. // console.log('setting mode', mode);
  121. // var elementId = 'show-' + mode;
  122. // $('#show-' + activeMode).removeClass('active');
  123. // activeMode = mode;
  124. // $('#' + elementId).addClass('active');
  125. // var ed = $('#editor-' + mode);
  126. // setEditorMode(mode);
  127. // editor.getSession().off('change');
  128. // editor.getSession().setValue(ed[0].innerHTML);
  129. // editor.getSession().on('change', editorContentChanged);
  130. // }
  131. // function loadExample(exampleSlug) {
  132. // // console.log('loadExample', exampleSlug);
  133. // var serverPath = 'exemples/jquery/' + exampleSlug + '/';
  134. // rp.get(serverPath + 'script.js', 'text')
  135. // .then(javascript => $editorJs.html(javascript))
  136. // .then(() => rp.get(serverPath + 'example.html', 'text'))
  137. // .then(() => rp.get(serverPath + 'styles.css', 'text')
  138. // .then(css => $editorCss.html(css))
  139. // .catch(err => {
  140. // return '';
  141. // })
  142. // )
  143. // .then(html => {
  144. // $editorHtml.html(html);
  145. // $('iframe.panel-right')
  146. // .prop('src', '/examples/' + exampleSlug)
  147. // setActiveTab('html');
  148. // setCurrentHash(exampleSlug);
  149. // var sources = {
  150. // html: $editorHtml.html(),
  151. // javascript: $editorJs.html()
  152. // };
  153. // editorStorage.init(exampleSlug, sources);
  154. // })
  155. // .then(() => {
  156. // var item = _.find(exampleList, { slug: exampleSlug });
  157. // // console.log(item.test ? 'test' : 'no test');
  158. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  159. // // $('#tests').show();
  160. // // });
  161. // });
  162. // }
  163. // /**
  164. // * Build select option string
  165. // */
  166. // function makeFileSelectOption(item) {
  167. // return '<option value="' + item.slug + '">' +
  168. // item.title +
  169. // '</option>'
  170. // }
  171. // /**
  172. // * Populate file selector from JSON list content
  173. // */
  174. // function resetFileSelect(exampleList) {
  175. // $fileSelect.html(
  176. // '<option value="-">&mdash;</option>' +
  177. // exampleList.map(makeFileSelectOption)
  178. // );
  179. // }
  180. // function loadExampleList() {
  181. // rp.get('/list/jquery', 'json')
  182. // .then(function(_exampleList) {
  183. // exampleList = _exampleList;
  184. // var restoredDraft;
  185. // resetFileSelect(exampleList);
  186. // if(currentHash) {
  187. // $fileSelect.val(currentHash);
  188. // var item = _.find(exampleList, { slug: currentHash });
  189. // if( ! item) {
  190. // return;
  191. // }
  192. // restoredDraft = editorStorage.restore(item.slug);
  193. // if(! restoredDraft) {
  194. // loadExample(item.slug);
  195. // }
  196. // else {
  197. // console.log('restore', item.slug, restoredDraft.sources);
  198. // $editorHtml.html(restoredDraft.sources.html);
  199. // $editorCss.html(restoredDraft.sources.css);
  200. // $editorJs.html(restoredDraft.sources.javascript);
  201. // $('iframe.panel-right')
  202. // .prop('src', '/examples/' + item.slug)
  203. // // loadJS('exemples/' + item.slug + '/script.js');
  204. // // if(item.test) {
  205. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  206. // // $('#tests').show();
  207. // // });
  208. // // }
  209. // setActiveTab('html');
  210. // }
  211. // }
  212. // });
  213. // }
  214. // function notify(type, text) {
  215. // $notification
  216. // .addClass(type)
  217. // .addClass('active');
  218. // $notification.html(text);
  219. // setTimeout(function() {
  220. // $notification.removeClass('active');
  221. // }, 2000);
  222. // setTimeout(function() {
  223. // $notification.removeClass(type);
  224. // }, 3000);
  225. // }
  226. // function toggleEditor() {
  227. // $addExampleBtn.toggle();
  228. // $selectorNav.toggle();
  229. // $exampleForm.toggle();
  230. // }
  231. // /**
  232. // * Save new example
  233. // */
  234. // function saveExample(e) {
  235. // e.preventDefault();
  236. // var title = $(this).find('input[name="title"]').val();
  237. // rp.post('/examples', { title: title })
  238. // .then(function(newExample) {
  239. // clearAndCloseEditor();
  240. // $fileSelect.append(makeFileSelectOption(newExample));
  241. // $fileSelect.val(newExample.slug);
  242. // notify('success', "Exemple créé !");
  243. // })
  244. // .catch(errText => notify('error', 'Erreur: ' + errText));
  245. // }
  246. // function clearAndCloseEditor() {
  247. // $exampleForm.find('input').val('');
  248. // toggleEditor();
  249. // }
  250. // function revertEditor() {
  251. // editorStorage.reset();
  252. // location.reload();
  253. // }
  254. // function saveChanges() {
  255. // var payload = editorStorage.getSources();
  256. // rp.put('/examples/' + currentHash, payload)
  257. // .then(function(newExample) {
  258. // notify('success', "Exemple sauvegardé !");
  259. // loadExample(currentHash);
  260. // })
  261. // .catch(function(err) {
  262. // notify('error', 'Erreur: ' + jqXHR.responseText);
  263. // });
  264. // }
  265. // setCurrentHash();
  266. // $fileSelect.change(function() {
  267. // loadExample($(this).val());
  268. // });
  269. // $addExampleBtn.click(toggleEditor);
  270. // $exampleCancel.click(clearAndCloseEditor);
  271. // $saveChanges.click(saveChanges);
  272. // $exampleForm.submit(saveExample);
  273. // $revertEditor.click(revertEditor);
  274. // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
  275. // loadExampleList();
  276. initEditor();
  277. });