editor.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. // var value = editorStorage.hasSource(mode) ?
  60. // editorStorage.getSource(mode) : ed[0].innerHTML;
  61. // editor.getSession().setValue(value);
  62. }
  63. $('#tabs button').click(function() {
  64. saveToLocalStorage();
  65. var mode = $(this).prop('id').substr(5);
  66. setActiveTab(mode);
  67. })
  68. function loadAsync(url, dataType) {
  69. return new Promise(function(resolve, reject) {
  70. $.ajax({
  71. type: 'GET',
  72. url: url,
  73. success: function(data) {
  74. resolve(data);
  75. },
  76. error: function(jqXHR) {
  77. reject(new Error(jqXHR.responseText));
  78. }
  79. }, dataType);
  80. });
  81. }
  82. function loadExample(exampleSlug) {
  83. var serverPath = 'exemples/' + exampleSlug + '/';
  84. loadAsync(serverPath + 'script.js', 'text')
  85. .then(javascript => $editorJs.html(javascript))
  86. .then(() => loadAsync(serverPath + '/contenu.html', 'text'))
  87. .then(html => {
  88. $editorHtml.html(html);
  89. setHtmlContent(html);
  90. setActiveTab('html');
  91. setCurrentHash(exampleSlug);
  92. var sources = {
  93. html: $editorHtml.html(),
  94. javascript: $editorJs.html()
  95. };
  96. editorStorage.init(exampleSlug, sources);
  97. loadJS(serverPath + 'script.js');
  98. });
  99. }
  100. function addFileSelectItem(item) {
  101. $fileSelect.append(
  102. '<option value="' + item.slug + '">' +
  103. item.title +
  104. '</option>'
  105. );
  106. }
  107. function loadExampleList() {
  108. $.get('exemples/liste.json', function(liste) {
  109. var didRestore;
  110. liste.forEach(addFileSelectItem);
  111. if(currentHash) {
  112. $fileSelect.val(currentHash);
  113. var item = _.find(liste, { slug: currentHash });
  114. if( ! item) {
  115. return;
  116. }
  117. restoredDraft = editorStorage.restore(item.slug);
  118. if(! restoredDraft) {
  119. loadExample(item.slug);
  120. }
  121. else {
  122. $editorHtml.html(restoredDraft.sources.html);
  123. setHtmlContent(restoredDraft.sources.html);
  124. $editorJs.html(restoredDraft.sources.javascript);
  125. loadJS('exemples/' + item.slug + '/script.js');
  126. setActiveTab('html');
  127. }
  128. }
  129. }, 'json');
  130. }
  131. function setHtmlContent(html) {
  132. $htmlContent.empty();
  133. $htmlContent.html(html);
  134. }
  135. function notify(type, text) {
  136. $notification
  137. .addClass(type)
  138. .addClass('active');
  139. $notification.html(text);
  140. setTimeout(function() {
  141. $notification.removeClass('active');
  142. }, 2000);
  143. setTimeout(function() {
  144. $notification.removeClass(type);
  145. }, 3000);
  146. }
  147. function toggleEditor() {
  148. $addExampleBtn.toggle();
  149. $selectorNav.toggle();
  150. $exampleForm.toggle();
  151. }
  152. function saveExample(e) {
  153. e.preventDefault();
  154. var title = $(this).find('input[name="title"]').val();
  155. $.ajax({
  156. type: 'POST',
  157. url: '/examples',
  158. data: JSON.stringify({ title }),
  159. success: function(newExample) {
  160. clearAndCloseEditor();
  161. addFileSelectItem(newExample);
  162. $fileSelect.val(newExample.slug);
  163. $fileSelect.trigger('change');
  164. notify('success', "Exemple créé !");
  165. },
  166. error: function(jqXHR, textStatus, errorThrown ) {
  167. notify('error', 'Erreur: ' + jqXHR.responseText);
  168. },
  169. contentType: 'application/json',
  170. dataType: 'json'
  171. });
  172. }
  173. function clearAndCloseEditor() {
  174. $exampleForm.find('input').val('');
  175. toggleEditor();
  176. }
  177. function saveChanges() {
  178. var payload = editorStorage.getSources();
  179. $.ajax({
  180. type: 'PUT',
  181. url: '/examples/' + currentHash,
  182. data: JSON.stringify(payload),
  183. success: function(newExample) {
  184. notify('success', "Exemple sauvegardé !");
  185. loadExample(currentHash);
  186. },
  187. error: function(jqXHR, textStatus, errorThrown ) {
  188. notify('error', 'Erreur: ' + jqXHR.responseText);
  189. },
  190. contentType: 'application/json',
  191. dataType: 'json'
  192. });
  193. }
  194. setCurrentHash();
  195. $fileSelect.change(function() {
  196. loadExample($(this).val());
  197. });
  198. $addExampleBtn.click(toggleEditor);
  199. $exampleCancel.click(clearAndCloseEditor);
  200. $saveChanges.click(saveChanges);
  201. $exampleForm.submit(saveExample);
  202. loadExampleList();
  203. });