editor.js 10.0 KB

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