editor.js 9.7 KB

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