Ajax Loader
×
HTML
<link rel="stylesheet" href="http://codemirror.net/lib/codemirror.css">
1
<link rel="stylesheet" href="http://codemirror.net/lib/codemirror.css">
2
 
3
<div id="wrap">
4
 
5
  <!-- Code Editors -->
6
  <section id="code_editors">
7
    <div id="html" class="code_box">
8
      <h3>HTML</h3>
9
      <textarea name="html"></textarea>
10
    </div>
11
    <div id="css" class="code_box">
12
      <h3>CSS</h3>
13
      <textarea name="css"></textarea>
14
    </div>
15
    <div id="js" class="code_box">
16
      <h3>JavaScript</h3>
17
      <textarea name="js"></textarea>
18
    </div>
19
  </section>
20
  
21
  <!-- Sandboxing -->
22
  <section id="output">
23
    <iframe></iframe>
24
  </section>
25
  
26
</div>
27
 
28
<script src="http://codemirror.net/lib/codemirror.js"></script>
29
 
30
<!-- For HTML/XML -->
31
<script src="http://codemirror.net/mode/xml/xml.js"></script>
32
<script src="http://codemirror.net/mode/htmlmixed/htmlmixed.js"></script>
33
 
34
<!-- For CSS -->
35
<script src="http://codemirror.net/mode/css/css.js"></script>
36
 
37
<!-- For JS -->
38
<script src="http://codemirror.net/mode/javascript/javascript.js"></script>
 
CSS
* {
1
* {
2
  -webkit-box-sizing: border-box;
3
  -moz-box-sizing: border-box;
4
  box-sizing: border-box;
5
}
6
 
7
html, body {
8
  width: 100%; height: 100%;
9
}
10
 
11
#wrap {
12
  width: 100%;
13
  height: 100%;
14
}
15
 
16
/* Code Editors */
17
 
18
#code_editors {
19
  position: absolute;
20
  top: 0; left: 0; bottom: 0; right: 60%;
21
}
22
 
23
#code_editors .code_box {
24
  height: 33%; width: 100%;
25
  position: relative;
26
}
27
.code_box h3 {
28
  font-size: 13px;
29
  height: 30px;
30
  padding: 5px 10px; margin: 0;
31
  background: linear-gradient(#707070, #555);
32
  color: white;
33
  border-top: 1px solid #8F8F8F;
34
  border-bottom: 1px solid #202020;
35
}
36
.code_box textarea {
37
  position: absolute;
38
  left: 0; right: 0; top: 30px; bottom: 0;
39
  resize: none; border: 0;
40
  padding: 10px;
41
  font-family: monospace;
42
}
43
.code_box textarea:focus {
44
  outline: none;
45
  background: #EFEFEF;
46
}
47
 
48
/* Output Area */
49
#output {
50
  position: absolute;
51
  left: 40%; top: 0; right: 0; bottom: 0;
52
  border: 5px solid #444;
53
  border-left-width: 10px;
54
  overflow: hidden;
55
}
56
 
57
#output iframe {
58
  width: 100%; height: 100%;
59
  border: 0;
60
}
 
JavaScript
(function() {
1
(function() {
2
  
3
  // Base template
4
  var base_tpl =
5
      "<!doctype html>\n" +
6
      "<html>\n\t" +
7
      "<head>\n\t\t" +
8
      "<meta charset=\"utf-8\">\n\t\t" +
9
      "<title>Test</title>\n\n\t\t\n\t" +
10
      "</head>\n\t" +
11
      "<body>\n\t\n\t" +
12
      "</body>\n" +
13
      "</html>";
14
  
15
  var prepareSource = function() {
16
    var html = html_editor.getValue(),
17
        css = css_editor.getValue(),
18
        js = js_editor.getValue(),
19
        src = '';
20
    
21
    // HTML
22
    src = base_tpl.replace('</body>', html + '</body>');
23
    
24
    // CSS
25
    css = '<style>' + css + '</style>';
26
    src = src.replace('</head>', css + '</head>');
27
    
28
    // Javascript
29
    js = '<script>' + js + '<\/script>';
30
    src = src.replace('</body>', js + '</body>');
31
    
32
    return src;
33
  };
34
  
35
  var render = function() {
36
    var source = prepareSource();
37
    
38
    var iframe = document.querySelector('#output iframe'),
39
        iframe_doc = iframe.contentDocument;
40
    
41
    iframe_doc.open();
42
    iframe_doc.write(source);
43
    iframe_doc.close();
44
  };
45
  
46
  
47
  // EDITORS
48
  
49
  // CM OPTIONS
50
  var cm_opt = {
51
    mode: 'text/html',
52
    gutter: true,
53
    lineNumbers: true,
54
    
55
    onChange: function (inst, changes) {
56
      render();
57
    }
58
  };
59
  
60
  // HTML EDITOR
61
  var html_box = document.querySelector('#html textarea');
62
  var html_editor = CodeMirror.fromTextArea(html_box, cm_opt);
63
  
64
  // CSS EDITOR
65
  cm_opt.mode = 'css';
66
  var css_box = document.querySelector('#css textarea');
67
  var css_editor = CodeMirror.fromTextArea(css_box, cm_opt);
68
  
69
  // JAVASCRIPT EDITOR
70
  cm_opt.mode = 'javascript';
71
  var js_box = document.querySelector('#js textarea');
72
  var js_editor = CodeMirror.fromTextArea(js_box, cm_opt);
73
  
74
  // SETTING CODE EDITORS INITIAL CONTENT
75
  html_editor.setValue('<p>Hello World</p>');
76
  css_editor.setValue('body { color: red; }');
77
  
78
  
79
  // RENDER CALL ON PAGE LOAD
80
  // NOT NEEDED ANYMORE, SINCE WE RELY
81
  // ON CODEMIRROR'S onChange OPTION THAT GETS
82
  // TRIGGERED ON setValue
83
  // render();
84
  
85
  
86
  // NOT SO IMPORTANT - IF YOU NEED TO DO THIS
87
  // THEN THIS SHOULD GO TO CSS
88
  
89
  /*
90
    Fixing the Height of CodeMirror.
91
    You might want to do this in CSS instead
92
    of JS and override the styles from the main
93
    codemirror.css
94
  */
95
  var cms = document.querySelectorAll('.CodeMirror');
96
  for (var i = 0; i < cms.length; i++) {
97
    
98
    cms[i].style.position = 'absolute';
99
    cms[i].style.top = '30px';
100
    cms[i].style.bottom = '0';
101
    cms[i].style.left = '0';
102
    cms[i].style.right = '0';
103
  }
104
  cms = document.querySelectorAll('.CodeMirror-scroll');
105
  for (i = 0; i < cms.length; i++) {
106
    cms[i].style.height = '100%';
107
  }
108
      
109
}());
 

Untitled

CSSDeck G+