editor.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. "use strict";
  2. $(document).ready(function() {
  3. var $editor = $('#editor');
  4. var $editorJs = $('#editor-javascript');
  5. var $editorHtml = $('#editor-html');
  6. var $editorCss = $('#editor-css');
  7. var $htmlContent = $('#html-content');
  8. var $selectorNav = $('#selector');
  9. var $fileSelect = $('#file-select');
  10. var $addExampleBtn = $('#add-example-btn');
  11. var $exampleForm = $('#add-example-form');
  12. var $exampleFormIn = $exampleForm.find('input')
  13. var $exampleSave = $('#add-example-save');
  14. var $exampleCancel = $('#add-example-cancel');
  15. var $saveChanges = $('#save-changes');
  16. var $notification = $('#notification');
  17. var $revertEditor = $('#revert-editor');
  18. var $panelLeft = $('.panel-left');
  19. var $panelRight = $('.panel-right');
  20. var $panelWrap = $('.panel-container');
  21. var $tabItems = $('#tabs li');
  22. var $window = $(window);
  23. var activeMode = 'html';
  24. var currentHash;
  25. var editor;
  26. var editorSession;
  27. var editorStorage = new LocalStorageDraft();
  28. var saveTimeout1;
  29. var saveTimeout2;
  30. var exampleList;
  31. var mapTypes = {
  32. html: 'html', js: 'javascript', css: 'css'
  33. }
  34. if(_webSandboxFiles.length === 0) {
  35. return;
  36. }
  37. // /**
  38. // * Make the left panel resizable
  39. // */
  40. // $panelLeft.resizable({
  41. // handleSelector: ".splitter",
  42. // resizeHeight: false,
  43. // onDrag: function(e) {
  44. // $editor.width($panelLeft.width());
  45. // }
  46. // });
  47. // /**
  48. // * Handle window resize
  49. // */
  50. // function onResize() {
  51. // $editor.width($panelLeft.width());
  52. // $panelWrap.height($window.height());
  53. // }
  54. // $window.resize(onResize);
  55. // onResize();
  56. function initEditor() {
  57. editor = ace.edit("editor");
  58. editorSession = editor.getSession();
  59. editor.setTheme("ace/theme/eclipse");
  60. editor.$blockScrolling = Infinity;
  61. editorSession.setUseWrapMode(true);
  62. if(_webSandboxFiles) {
  63. editorSession.setMode("ace/mode/html");
  64. var firstHtml = _webSandboxFiles.find(f => (f.type === 'html'));
  65. editorSession.setValue(firstHtml.content);
  66. }
  67. }
  68. /**
  69. * File list tabs click handler
  70. */
  71. $tabItems.click(function() {
  72. var clickedItem = $(this);
  73. var type = clickedItem.data('type');
  74. editorSession.setMode("ace/mode/" + mapTypes[type]);
  75. var name = clickedItem.html();
  76. var file = _webSandboxFiles.find(f => (f.name === name));
  77. editorSession.setValue(file.content);
  78. // saveToLocalStorage();
  79. // var mode = $(this).prop('id').substr(5);
  80. // console.log( .html() );
  81. // setActiveTab(mode);
  82. })
  83. // /**
  84. // * Set the current hash. If none given, take it from lococation.hash
  85. // */
  86. // function setCurrentHash(slug) {
  87. // if(slug) {
  88. // window.location.hash = currentHash = slug;
  89. // }
  90. // else {
  91. // currentHash = window.location.hash ?
  92. // window.location.hash.substr(1) : undefined;
  93. // }
  94. // }
  95. // /**
  96. // * Set editor mode (html, javascript, css)
  97. // */
  98. // function setEditorMode(mode) {
  99. // editor.getSession().setMode("ace/mode/" + mode);
  100. // }
  101. // /**
  102. // * Save a copy in localStorage
  103. // */
  104. // function saveToLocalStorage() {
  105. // var editorContent = editor.getSession().getValue();
  106. // editorStorage.saveSource(activeMode, editorContent);
  107. // saveTimeout1 = undefined;
  108. // }
  109. // /**
  110. // * React to changes in editor by saving a copy
  111. // */
  112. // function editorContentChanged() {
  113. // // console.log('editorContent changed')
  114. // if(saveTimeout1 || saveTimeout2) {
  115. // clearTimeout(saveTimeout1);
  116. // clearTimeout(saveTimeout2);
  117. // }
  118. // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
  119. // // saveTimeout2 = setTimeout(saveChanges, 1000);
  120. // }
  121. // editor.getSession().on('change', editorContentChanged);
  122. // function setActiveTab(mode) {
  123. // console.log('setting mode', mode);
  124. // var elementId = 'show-' + mode;
  125. // $('#show-' + activeMode).removeClass('active');
  126. // activeMode = mode;
  127. // $('#' + elementId).addClass('active');
  128. // var ed = $('#editor-' + mode);
  129. // setEditorMode(mode);
  130. // editor.getSession().off('change');
  131. // editor.getSession().setValue(ed[0].innerHTML);
  132. // editor.getSession().on('change', editorContentChanged);
  133. // }
  134. // function loadExample(exampleSlug) {
  135. // // console.log('loadExample', exampleSlug);
  136. // var serverPath = 'exemples/jquery/' + exampleSlug + '/';
  137. // rp.get(serverPath + 'script.js', 'text')
  138. // .then(javascript => $editorJs.html(javascript))
  139. // .then(() => rp.get(serverPath + 'example.html', 'text'))
  140. // .then(() => rp.get(serverPath + 'styles.css', 'text')
  141. // .then(css => $editorCss.html(css))
  142. // .catch(err => {
  143. // return '';
  144. // })
  145. // )
  146. // .then(html => {
  147. // $editorHtml.html(html);
  148. // $('iframe.panel-right')
  149. // .prop('src', '/examples/' + exampleSlug)
  150. // setActiveTab('html');
  151. // setCurrentHash(exampleSlug);
  152. // var sources = {
  153. // html: $editorHtml.html(),
  154. // javascript: $editorJs.html()
  155. // };
  156. // editorStorage.init(exampleSlug, sources);
  157. // })
  158. // .then(() => {
  159. // var item = _.find(exampleList, { slug: exampleSlug });
  160. // // console.log(item.test ? 'test' : 'no test');
  161. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  162. // // $('#tests').show();
  163. // // });
  164. // });
  165. // }
  166. // /**
  167. // * Build select option string
  168. // */
  169. // function makeFileSelectOption(item) {
  170. // return '<option value="' + item.slug + '">' +
  171. // item.title +
  172. // '</option>'
  173. // }
  174. // /**
  175. // * Populate file selector from JSON list content
  176. // */
  177. // function resetFileSelect(exampleList) {
  178. // $fileSelect.html(
  179. // '<option value="-">&mdash;</option>' +
  180. // exampleList.map(makeFileSelectOption)
  181. // );
  182. // }
  183. // function loadExampleList() {
  184. // rp.get('/list/jquery', 'json')
  185. // .then(function(_exampleList) {
  186. // exampleList = _exampleList;
  187. // var restoredDraft;
  188. // resetFileSelect(exampleList);
  189. // if(currentHash) {
  190. // $fileSelect.val(currentHash);
  191. // var item = _.find(exampleList, { slug: currentHash });
  192. // if( ! item) {
  193. // return;
  194. // }
  195. // restoredDraft = editorStorage.restore(item.slug);
  196. // if(! restoredDraft) {
  197. // loadExample(item.slug);
  198. // }
  199. // else {
  200. // console.log('restore', item.slug, restoredDraft.sources);
  201. // $editorHtml.html(restoredDraft.sources.html);
  202. // $editorCss.html(restoredDraft.sources.css);
  203. // $editorJs.html(restoredDraft.sources.javascript);
  204. // $('iframe.panel-right')
  205. // .prop('src', '/examples/' + item.slug)
  206. // // loadJS('exemples/' + item.slug + '/script.js');
  207. // // if(item.test) {
  208. // // loadJS('exemples/' + item.slug + '/test.js', function() {
  209. // // $('#tests').show();
  210. // // });
  211. // // }
  212. // setActiveTab('html');
  213. // }
  214. // }
  215. // });
  216. // }
  217. // function notify(type, text) {
  218. // $notification
  219. // .addClass(type)
  220. // .addClass('active');
  221. // $notification.html(text);
  222. // setTimeout(function() {
  223. // $notification.removeClass('active');
  224. // }, 2000);
  225. // setTimeout(function() {
  226. // $notification.removeClass(type);
  227. // }, 3000);
  228. // }
  229. function toggleEditor() {
  230. $addExampleBtn.toggle();
  231. $selectorNav.toggle();
  232. $exampleForm.toggle();
  233. }
  234. /**
  235. * Add new example
  236. */
  237. function addExample(e) {
  238. e.preventDefault();
  239. var title = $(this).find('input[name="title"]').val();
  240. rp.post('/examples', { title: title })
  241. .then(function(newExample) {
  242. clearAndCloseEditor();
  243. $fileSelect.append(makeFileSelectOption(newExample));
  244. $fileSelect.val(newExample.slug);
  245. notify('success', "Exemple créé !");
  246. })
  247. .catch(errText => notify('error', 'Erreur: ' + errText));
  248. }
  249. function clearAndCloseEditor() {
  250. $exampleFormIn.val('');
  251. toggleEditor();
  252. }
  253. // function revertEditor() {
  254. // editorStorage.reset();
  255. // location.reload();
  256. // }
  257. // function saveChanges() {
  258. // var payload = editorStorage.getSources();
  259. // rp.put('/examples/' + currentHash, payload)
  260. // .then(function(newExample) {
  261. // notify('success', "Exemple sauvegardé !");
  262. // loadExample(currentHash);
  263. // })
  264. // .catch(function(err) {
  265. // notify('error', 'Erreur: ' + jqXHR.responseText);
  266. // });
  267. // }
  268. // setCurrentHash();
  269. // $fileSelect.change(function() {
  270. // loadExample($(this).val());
  271. // });
  272. $addExampleBtn.click(toggleEditor);
  273. $exampleCancel.click(clearAndCloseEditor);
  274. // $saveChanges.click(saveChanges);
  275. $exampleForm.submit(addExample);
  276. // $revertEditor.click(revertEditor);
  277. // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
  278. // loadExampleList();
  279. initEditor();
  280. });