editor.js 10 KB

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