editor.js 7.2 KB

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