editor.js 6.7 KB

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