editor.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $htmlContent.html(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');
  132. $editorHtml.html(restoredDraft.sources.html);
  133. $editorJs.html(restoredDraft.sources.javascript);
  134. setActiveTab('html');
  135. }
  136. }
  137. }, 'json');
  138. }
  139. function notify(type, text) {
  140. $notification
  141. .addClass(type)
  142. .addClass('active');
  143. $notification.html(text);
  144. setTimeout(function() {
  145. $notification.removeClass('active');
  146. }, 2000);
  147. setTimeout(function() {
  148. $notification.removeClass(type);
  149. }, 3000);
  150. }
  151. function toggleEditor() {
  152. $addExampleBtn.toggle();
  153. $selectorNav.toggle();
  154. $exampleForm.toggle();
  155. }
  156. // function ajaxPost(url, data, success, error) {}
  157. function saveExample(e) {
  158. e.preventDefault();
  159. var title = $(this).find('input[name="title"]').val();
  160. $.ajax({
  161. type: 'POST',
  162. url: '/examples',
  163. data: JSON.stringify({ title }),
  164. success: function(newExample) {
  165. clearAndCloseEditor();
  166. addFileSelectItem(newExample);
  167. notify('success', "Exemple créé !");
  168. },
  169. error: function(jqXHR, textStatus, errorThrown ) {
  170. console.log(jqXHR, textStatus, errorThrown);
  171. notify('error', 'Erreur: ' + jqXHR.responseText);
  172. },
  173. contentType: 'application/json',
  174. dataType: 'json'
  175. });
  176. }
  177. function clearAndCloseEditor() {
  178. $exampleForm.find('input').val('');
  179. toggleEditor();
  180. }
  181. function saveChanges() {
  182. var payload = editorStorage.getSources();
  183. $.ajax({
  184. type: 'PUT',
  185. url: '/examples/' + currentHash,
  186. data: JSON.stringify(payload),
  187. success: function(newExample) {
  188. notify('success', "Exemple sauvegardé !");
  189. },
  190. error: function(jqXHR, textStatus, errorThrown ) {
  191. console.log(jqXHR, textStatus, errorThrown);
  192. notify('error', 'Erreur: ' + jqXHR.responseText);
  193. },
  194. contentType: 'application/json',
  195. dataType: 'json'
  196. });
  197. }
  198. setCurrentHash();
  199. $fileSelect.change(function() {
  200. loadExample($(this).val());
  201. });
  202. $addExampleBtn.click(toggleEditor);
  203. $exampleCancel.click(clearAndCloseEditor);
  204. $saveChanges.click(saveChanges);
  205. $exampleForm.submit(saveExample);
  206. loadExampleList();
  207. // setActiveTab('html');
  208. });