1+ const editor = CodeMirror ( document . getElementById ( 'div-1' ) , {
2+ lineNumbers : true ,
3+ tabSize : 1 ,
4+ mode : 'javascript' ,
5+ theme : 'monokai' ,
6+ value : ''
7+ } ) ;
8+
9+ /////PROTO///////////
10+
11+ let vars_in_scope = {
12+ "div-1" : editor
13+ }
14+
15+ function exec_cell ( id , count ) {
16+ $ ( `#out_${ id } ` ) . html ( ( "global" , eval ) ( vars_in_scope [ id ] . getValue ( ) ) || "" ) ;
17+
18+ count = parseInt ( count ) + 1
19+ let div_count = `div-${ count } `
20+
21+ if ( ! ( div_count in vars_in_scope ) ) {
22+ $ ( "#container" ) . append (
23+ `<div id=${ div_count } class=""></div><br />
24+ <button style="color: black;" id=run_${ div_count } class="run">Run</button>
25+ <div id=out_${ div_count } ></div><br />`
26+ )
27+
28+ let editor = CodeMirror ( document . getElementById ( `${ div_count } ` ) , {
29+ lineNumbers : true ,
30+ tabSize : 2 ,
31+ mode : 'javascript' ,
32+ theme : 'monokai' ,
33+ value : ''
34+ } ) ;
35+
36+
37+ vars_in_scope [ div_count ] = editor ;
38+
39+ }
40+
41+ }
42+
43+ function add_new_code_cell ( id , last_scope_id , where ) {
44+ let new_id = parseInt ( last_scope_id ) + 1
45+ let parent_cell_id = `cell-${ id } `
46+ let html = `
47+ <div class="row" style="margin-top: 50px;" id="cell-${ new_id } ">
48+ <div class="col-md-1">
49+ <p id="cell-num" class="code_symbol">[${ new_id } ]</p>
50+ </div>
51+ <div id="div-${ new_id } " class="col-md-11">
52+ <div class="btn-group-horizontal">
53+ <button type="button" id="run_div-${ new_id } " class="btn btn-sm btn-success run"><i
54+ class="fas fa-play"></i>Run</button>
55+ <div class="btn-group" role="group" aria-label="Basic example">
56+
57+ <button type="button" id="add_code_down_btn-${ new_id } " class="btn btn-info add-code">
58+ <i class="fas fa-sort-down" style="margin-top: -10px;"></i> Code
59+ </button>
60+ <button type="button" id="add_code_up_btn-${ new_id } " class="btn btn-sm btn-info add-code">
61+ <i class="fas fa-sort-up"></i> Code
62+ </button>
63+
64+ </div>
65+
66+ <div class="btn-group" role="group" aria-label="Basic example">
67+
68+ <button type="button" id="add_text_down_btn-${ new_id } " class="btn btn-sm btn-info add-text">
69+ <i class="fas fa-sort-down" style="margin-top: -10px;"></i> Text
70+ </button>
71+ <button type="button" id="add_text_up_btn-${ new_id } " class="btn btn-sm btn-info add-text">
72+ <i class="fas fa-sort-up"></i> Text
73+ </button>
74+ </div>
75+
76+ <button type="button" id="del_btn-${ new_id } " class="btn btn-sm btn-danger del"><i
77+ class="fas fa-trash-alt"></i>
78+ Delete</button>
79+ </div>
80+
81+ </div>
82+ <div class="col-md-1"></div>
83+ <div id="out_div-${ new_id } " class="col-md-10 out-divs">
84+
85+ </div>
86+ </div>
87+ `
88+ let divReference = document . getElementById ( parent_cell_id ) ;
89+
90+ if ( where == "up" ) {
91+ divReference . insertAdjacentHTML ( "beforebegin" , html ) ;
92+ } else {
93+ divReference . insertAdjacentHTML ( "afterend" , html ) ;
94+ }
95+
96+
97+
98+
99+ // $(`#${parent_cell_id}`).parentNode.append(html)
100+
101+ let editor = CodeMirror ( document . getElementById ( `div-${ new_id } ` ) , {
102+ lineNumbers : true ,
103+ tabSize : 2 ,
104+ mode : 'javascript' ,
105+ theme : 'monokai' ,
106+ value : ''
107+ } ) ;
108+
109+
110+ vars_in_scope [ `div-${ new_id } ` ] = editor
111+
112+ }
113+
114+ function add_new_text_cell ( id , count ) {
115+
116+
117+ }
118+
119+ function delete_cell ( id ) {
120+ row_id = `cell-${ Number ( id ) } `
121+ var div_ele = document . getElementById ( row_id ) ;
122+ div_ele . parentNode . removeChild ( div_ele ) ;
123+
124+ }
125+
126+ $ ( document ) . on ( "click" , "button.run" , function ( ) {
127+ let id = this . id . split ( "_" ) [ 1 ]
128+ let count = this . id . split ( "-" ) [ 1 ]
129+ // console.log(id);
130+ // console.log(count);
131+ exec_cell ( id , count ) ;
132+ } )
133+
134+ $ ( document ) . on ( "click" , "button.del" , function ( ) {
135+ // let id = this.id.split("_")[1]
136+ let id = this . id . split ( "-" ) [ 1 ]
137+ delete_cell ( id )
138+ } )
139+
140+ $ ( document ) . on ( "click" , "button.add-code" , function ( ) {
141+ let last_cell_in_scope = parseInt ( Object . keys ( vars_in_scope ) . pop ( ) . split ( "-" ) [ 1 ] )
142+ let id = this . id . split ( "-" ) [ 1 ]
143+
144+ let where ;
145+ if ( this . id . split ( "_" ) . includes ( "down" ) ) {
146+ where = "down"
147+ } else {
148+ where = "up"
149+ }
150+ add_new_code_cell ( id , last_cell_in_scope , where )
151+ } )
0 commit comments