editor.js 9.0 KB

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