editor.js 7.5 KB

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