editor.js 7.1 KB

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