editor.js 6.5 KB

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