editor.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. $(document).ready(function() {
  2. var $editor = $('#editor');
  3. var $editorJs = $('#editor-javascript');
  4. var $editorHtml = $('#editor-html');
  5. var $htmlContent = $('#html-content');
  6. var activeTab = 'show-html';
  7. var editor;
  8. function initEditor(mode) {
  9. editor = ace.edit("editor");
  10. editor.setTheme("ace/theme/eclipse");
  11. console.log(mode);
  12. editor.getSession().setMode("ace/mode/" + mode);
  13. editor.getSession().setUseWrapMode(true);
  14. }
  15. // editor.getSession().on('change', function() {
  16. // console.log(arguments)
  17. // });
  18. function setActiveTab(which) {
  19. var elementId = 'show-' + which;
  20. $('#' + activeTab).removeClass('active');
  21. activeTab = elementId;
  22. $('#' + elementId).addClass('active');
  23. var ed = $('#editor-' + which);
  24. // editor.getSession().setValue('');
  25. console.log(which, "ace/mode/" + which);
  26. // editor.getSession().setMode("ace/mode/" + which);
  27. // console.log('set editor content to', ed.html());
  28. // editor.getSession().setValue(ed.html());
  29. // newSession = ace.createEditSession(ed.html(), "ace/mode/" + which)
  30. // var oldSession = editor.session
  31. // editor.setSession(newSession)
  32. initEditor(which);
  33. console.log(ed[0].innerHTML)
  34. editor.getSession().setValue('<button id="pouet">Click me</button>'); //ed[0].innerHTML);
  35. }
  36. $('#tabs button').click(function() {
  37. var which = $(this).prop('id').substr(5);
  38. setActiveTab(which);
  39. })
  40. function loadExample() {
  41. var exampleName = $(this).val();
  42. var serverPath = 'exemples/' + exampleName + '/';
  43. $.get(serverPath + 'script.js', function(javascript) {
  44. $editorJs.html(javascript);
  45. }, 'text');
  46. $.get(serverPath + '/contenu.html', function(html) {
  47. $editorHtml.html(html);
  48. $htmlContent.html(html);
  49. setActiveTab('html');
  50. loadJS(serverPath + 'script.js');
  51. }, 'text');
  52. }
  53. var fileSelect = $('#fileSelect');
  54. fileSelect.change(loadExample);
  55. setActiveTab('html');
  56. });