ws-editor.js 11 KB

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