editor.js 7.4 KB

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