editor.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. $(document).ready(function() {
  2. var $editor = $('#editor');
  3. var $editorJs = $('#editor-javascript');
  4. var $editorHtml = $('#editor-html');
  5. var $htmlContent = $('#html-content');
  6. var $selectorNav = $('#selector');
  7. var $fileSelect = $('#file-select');
  8. var $addExampleBtn = $('#add-example-btn');
  9. var $exampleForm = $('#add-example-form');
  10. var $exampleSave = $('#add-example-save');
  11. var $exampleCancel = $('#add-example-cancel');
  12. var $saveChanges = $('#save-changes');
  13. var $notification = $('#notification');
  14. var $revertEditor = $('#revert-editor');
  15. var $panelLeft = $('.panel-left');
  16. var $panelRight = $('.panel-right');
  17. var $window = $(window);
  18. var activeMode = 'html';
  19. var currentHash;
  20. var editor;
  21. var editorStorage = new LocalStorageDraft();
  22. var saveTimeout1;
  23. var saveTimeout2;
  24. var exampleList;
  25. $panelLeft.resizable({
  26. handleSelector: ".splitter",
  27. resizeHeight: false,
  28. onDrag: function(e) {
  29. $editor.width($panelLeft.width());
  30. }
  31. });
  32. $window.resize(function() {
  33. $editor.width($panelLeft.width());
  34. $(".panel-container").height($(window).height());
  35. });
  36. $(".panel-container").height($(window).height());
  37. $editor.width($panelLeft.width());
  38. editor = ace.edit("editor");
  39. editor.setTheme("ace/theme/eclipse");
  40. editor.$blockScrolling = Infinity;
  41. editor.getSession().setUseWrapMode(true);
  42. function setCurrentHash(slug) {
  43. // console.log('setCurrentHash', slug)
  44. if(slug) {
  45. // console.log('save current hash', slug);
  46. window.location.hash = currentHash = slug;
  47. }
  48. else {
  49. currentHash = window.location.hash ?
  50. window.location.hash.substr(1) : undefined;
  51. // if(currentHash) console.log('restored current hash', currentHash);
  52. }
  53. }
  54. function setEditorMode(mode) {
  55. editor.getSession().setMode("ace/mode/" + mode);
  56. }
  57. function saveToLocalStorage() {
  58. var editorContent = editor.getSession().getValue();
  59. // console.log('saveToLocalStorage', activeMode, editorContent.substr(0, 10) + '[...]');
  60. editorStorage.saveSource(activeMode, editorContent);
  61. saveTimeout = undefined;
  62. }
  63. function editorContentChanged() {
  64. // console.log('editorContent changed')
  65. if(saveTimeout1 || saveTimeout2) {
  66. clearTimeout(saveTimeout1);
  67. clearTimeout(saveTimeout2);
  68. }
  69. saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  70. // saveTimeout2 = setTimeout(saveChanges, 1000);
  71. }
  72. editor.getSession().on('change', editorContentChanged);
  73. function setActiveTab(mode) {
  74. // console.log('setting mode', mode);
  75. var elementId = 'show-' + mode;
  76. $('#show-' + activeMode).removeClass('active');
  77. activeMode = mode;
  78. $('#' + elementId).addClass('active');
  79. var ed = $('#editor-' + mode);
  80. setEditorMode(mode);
  81. editor.getSession().off('change');
  82. editor.getSession().setValue(ed[0].innerHTML);
  83. editor.getSession().on('change', editorContentChanged);
  84. }
  85. $('#tabs button').click(function() {
  86. saveToLocalStorage();
  87. var mode = $(this).prop('id').substr(5);
  88. setActiveTab(mode);
  89. })
  90. function loadAsync(url, dataType) {
  91. return new Promise(function(resolve, reject) {
  92. $.ajax({
  93. type: 'GET',
  94. url: url,
  95. success: function(data) {
  96. resolve(data);
  97. },
  98. error: function(jqXHR) {
  99. reject(new Error(jqXHR.responseText));
  100. }
  101. }, dataType);
  102. });
  103. }
  104. function loadExample(exampleSlug) {
  105. // console.log('loadExample', exampleSlug);
  106. var serverPath = 'exemples/' + exampleSlug + '/';
  107. loadAsync(serverPath + 'script.js', 'text')
  108. .then(javascript => $editorJs.html(javascript))
  109. .then(() => loadAsync(serverPath + '/contenu.html', 'text'))
  110. .then(html => {
  111. $editorHtml.html(html);
  112. setHtmlContent(html);
  113. setActiveTab('html');
  114. setCurrentHash(exampleSlug);
  115. var sources = {
  116. html: $editorHtml.html(),
  117. javascript: $editorJs.html()
  118. };
  119. editorStorage.init(exampleSlug, sources);
  120. loadJS(serverPath + 'script.js');
  121. })
  122. .then(() => {
  123. var item = _.find(exampleList, { slug: exampleSlug });
  124. // console.log(item.test ? 'test' : 'no test');
  125. // loadJS('exemples/' + item.slug + '/test.js', function() {
  126. // $('#tests').show();
  127. // });
  128. });
  129. }
  130. function addFileSelectItem(item) {
  131. $fileSelect.append(
  132. '<option value="' + item.slug + '">' +
  133. item.title +
  134. '</option>'
  135. );
  136. }
  137. function loadExampleList() {
  138. $.get('exemples/liste.json', function(_exampleList) {
  139. exampleList = _exampleList;
  140. var restoredDraft;
  141. exampleList.forEach(addFileSelectItem);
  142. if(currentHash) {
  143. $fileSelect.val(currentHash);
  144. var item = _.find(exampleList, { slug: currentHash });
  145. if( ! item) {
  146. return;
  147. }
  148. restoredDraft = editorStorage.restore(item.slug);
  149. if(! restoredDraft) {
  150. loadExample(item.slug);
  151. }
  152. else {
  153. $editorHtml.html(restoredDraft.sources.html);
  154. setHtmlContent(restoredDraft.sources.html);
  155. $editorJs.html(restoredDraft.sources.javascript);
  156. loadJS('exemples/' + item.slug + '/script.js');
  157. // if(item.test) {
  158. // loadJS('exemples/' + item.slug + '/test.js', function() {
  159. // $('#tests').show();
  160. // });
  161. // }
  162. setActiveTab('html');
  163. }
  164. }
  165. }, 'json');
  166. }
  167. function setHtmlContent(html) {
  168. $htmlContent.empty();
  169. $htmlContent.html(html);
  170. }
  171. function notify(type, text) {
  172. $notification
  173. .addClass(type)
  174. .addClass('active');
  175. $notification.html(text);
  176. setTimeout(function() {
  177. $notification.removeClass('active');
  178. }, 2000);
  179. setTimeout(function() {
  180. $notification.removeClass(type);
  181. }, 3000);
  182. }
  183. function toggleEditor() {
  184. $addExampleBtn.toggle();
  185. $selectorNav.toggle();
  186. $exampleForm.toggle();
  187. }
  188. function saveExample(e) {
  189. e.preventDefault();
  190. var title = $(this).find('input[name="title"]').val();
  191. $.ajax({
  192. type: 'POST',
  193. url: '/examples',
  194. data: JSON.stringify({ title }),
  195. success: function(newExample) {
  196. clearAndCloseEditor();
  197. addFileSelectItem(newExample);
  198. $fileSelect.val(newExample.slug);
  199. $fileSelect.trigger('change');
  200. notify('success', "Exemple créé !");
  201. },
  202. error: function(jqXHR, textStatus, errorThrown ) {
  203. notify('error', 'Erreur: ' + jqXHR.responseText);
  204. },
  205. contentType: 'application/json',
  206. dataType: 'json'
  207. });
  208. }
  209. function clearAndCloseEditor() {
  210. $exampleForm.find('input').val('');
  211. toggleEditor();
  212. }
  213. function revertEditor() {
  214. editorStorage.reset();
  215. }
  216. function saveChanges() {
  217. var payload = editorStorage.getSources();
  218. $.ajax({
  219. type: 'PUT',
  220. url: '/examples/' + currentHash,
  221. data: JSON.stringify(payload),
  222. success: function(newExample) {
  223. notify('success', "Exemple sauvegardé !");
  224. loadExample(currentHash);
  225. },
  226. error: function(jqXHR, textStatus, errorThrown ) {
  227. notify('error', 'Erreur: ' + jqXHR.responseText);
  228. },
  229. contentType: 'application/json',
  230. dataType: 'json'
  231. });
  232. }
  233. setCurrentHash();
  234. $fileSelect.change(function() {
  235. loadExample($(this).val());
  236. });
  237. $addExampleBtn.click(toggleEditor);
  238. $exampleCancel.click(clearAndCloseEditor);
  239. $saveChanges.click(saveChanges);
  240. $exampleForm.submit(saveExample);
  241. $revertEditor.click(revertEditor);
  242. loadExampleList();
  243. });