@@ -6,6 +6,21 @@ const editor = CodeMirror(document.getElementById('div-1'), {
66 value : ''
77} ) ;
88
9+
10+ var md = new Remarkable ( )
11+ // console.log(md.render('# Remarkable rulezz!'));
12+
13+ //Global Params
14+ let vars_in_scope = {
15+ "div-1" : editor
16+ }
17+
18+ let md_texts = { } //stores markdown text and corresponding div name
19+
20+ let __code_cell_count = 1
21+ // let __all_cells_count =
22+
23+
924$ ( "#div-1" )
1025 . mouseover ( function ( ) {
1126 $ ( "#btn-actions-1" ) . show ( )
@@ -15,12 +30,6 @@ $("#div-1")
1530 } ) ;
1631
1732
18- //Global Params
19- let vars_in_scope = {
20- "div-1" : editor
21- }
22-
23- let __cells_count = 1
2433
2534function exec_cell ( c_id ) {
2635 let id = c_id . split ( "_" ) [ 1 ]
@@ -45,7 +54,7 @@ function exec_cell(c_id) {
4554
4655
4756function add_new_code_cell ( c_id , where ) {
48- __cells_count += 1
57+ __code_cell_count += 1
4958 let last_scope_id = parseInt ( Object . keys ( vars_in_scope ) . pop ( ) . split ( "-" ) [ 1 ] )
5059 let id = c_id . split ( "-" ) [ 1 ]
5160
@@ -127,25 +136,113 @@ function add_new_code_cell(c_id, where) {
127136
128137}
129138
130- function add_new_text_cell ( id , count ) {
139+ function add_new_text_cell ( c_id , where ) {
140+ __code_cell_count += 1
141+ let last_scope_id = parseInt ( Object . keys ( vars_in_scope ) . pop ( ) . split ( "-" ) [ 1 ] )
142+ let id = c_id . split ( "-" ) [ 1 ]
143+
144+ if ( where == "down" ) {
145+ where = "down"
146+ } else {
147+ where = "up"
148+ }
149+
150+ let new_id = parseInt ( last_scope_id ) + 1
151+ let parent_cell_id = `cell-${ id } `
152+
153+ let html = `
154+ <div class="row" style="margin-top: 10px;" id="cell-${ new_id } ">
155+ <div class="col-md-1">
156+ <p id="cell-num" class="code_symbol">[${ new_id } ]</p>
157+ </div>
158+
159+ <div id="text-div_${ new_id } " class="col-md-11">
160+ <div id="btn-actions-${ new_id } " class="btn-group-horizontal" style="margin-bottom: 2px;">
161+ <button type="button" id="run_div-1" class="btn btn-sm btn-success run"><i class="fas fa-play"></i>
162+ Run</button>
163+ <div class="btn-group" role="group" aria-label="Basic example">
164+ <button type="button" id="add_code_down_btn-${ new_id } " class="btn btn-sm btn-info add-code">
165+ <i class="fas fa-sort-down" style="margin-top: -10px;"></i> Code
166+ </button>
167+ <button type="button" id="add_code_up_btn-${ new_id } " class="btn btn-sm btn-info add-code">
168+ <i class="fas fa-sort-up"></i> Code
169+ </button>
170+ </div>
171+
172+ <div class="btn-group" role="group" aria-label="Basic example">
173+ <button type="button" id="add_text_down_btn-${ new_id } " class="btn btn-sm btn-info add-text">
174+ <i class="fas fa-sort-down" style="margin-top: -10px;"></i> Text
175+ </button>
176+ <button type="button" id="add_text_up_btn-${ new_id } " class="btn btn-sm btn-info add-text">
177+ <i class="fas fa-sort-up"></i> Text
178+ </button>
179+ </div>
180+
181+ <button type="button" id="del-text_${ new_id } " class="btn btn-sm btn-danger del"><i
182+ class="fas fa-trash-alt"></i>
183+ </button>
184+ </div>
185+
186+ <textarea id="text-box_${ new_id } " class="text-box"></textarea>
187+ </div>
188+
189+ <div class="col-md-1"></div>
190+ <div id="out-text-div_${ new_id } " style="display:block;" class="col-md-11 text-out-box">
191+ </div>
192+ </div>
193+
194+ `
195+
196+
197+ let divReference = document . getElementById ( parent_cell_id ) ;
198+
199+ if ( where == "up" ) {
200+ divReference . insertAdjacentHTML ( "beforebegin" , html ) ;
201+ } else {
202+ divReference . insertAdjacentHTML ( "afterend" , html ) ;
203+ }
204+
205+ vars_in_scope [ `div-${ new_id } ` ] = ""
206+
207+ update_text_box_size ( )
208+
209+ $ ( `#text-div_${ new_id } ` )
210+ . mouseover ( function ( ) {
211+ document . getElementById ( `btn-actions-${ new_id } ` ) . style . display = "block"
212+ } )
213+ . mouseout ( function ( ) {
214+ document . getElementById ( `btn-actions-${ new_id } ` ) . style . display = "none"
215+ } ) ;
131216
132217
133218}
134219
135220function delete_cell ( id ) {
136- if ( __cells_count == 1 ) {
221+ if ( __code_cell_count == 1 ) {
137222 document . getElementsByClassName ( "del" ) . disable = true
138223 } else {
139224 row_id = `cell-${ Number ( id ) } `
140225 var div_ele = document . getElementById ( row_id ) ;
141226 div_ele . parentNode . removeChild ( div_ele ) ;
142- __cells_count -= 1
227+ __code_cell_count -= 1
143228 }
144229
230+ }
145231
232+ function delete_text_cell ( id ) {
233+ // __all_cell_count
234+ md_textarea_id = `text-div_${ Number ( id ) } `
235+ md_out_id = `out-text-div_${ Number ( id ) } `
146236
147- }
237+ var md_div_ele = document . getElementById ( md_textarea_id ) ;
238+ var out_div_ele = document . getElementById ( md_out_id ) ;
239+
240+ md_div_ele . parentNode . removeChild ( md_div_ele ) ;
241+ out_div_ele . parentNode . removeChild ( out_div_ele ) ;
148242
243+ delete md_texts [ md_textarea_id ]
244+
245+ }
149246
150247
151248$ ( document ) . on ( "click" , "button.run" , function ( ) {
@@ -157,6 +254,11 @@ $(document).on("click", "button.del", function () {
157254 delete_cell ( id )
158255} )
159256
257+ $ ( document ) . on ( "click" , "button.del-text" , function ( ) {
258+ let id = this . id . split ( "-" ) [ 1 ]
259+ delete_text_cell ( id )
260+ } )
261+
160262$ ( document ) . on ( "click" , "button.add-code" , function ( ) {
161263 let where ;
162264 if ( this . id . split ( "_" ) . includes ( "down" ) ) {
@@ -165,4 +267,51 @@ $(document).on("click", "button.add-code", function () {
165267 where = "up"
166268 }
167269 add_new_code_cell ( this . id , where )
168- } )
270+ } )
271+
272+ $ ( document ) . on ( "click" , "button.add-text" , function ( ) {
273+ let where ;
274+ if ( this . id . split ( "_" ) . includes ( "down" ) ) {
275+ where = "down"
276+ } else {
277+ where = "up"
278+ }
279+ console . log ( this . id ) ;
280+ add_new_text_cell ( this . id , where )
281+ } )
282+
283+ $ ( document ) . on ( "dblclick" , "textarea.text-box" , function ( ) {
284+ let id = this . id . split ( "_" ) [ 1 ]
285+ div_id = `text-div_${ id } `
286+ md_texts [ div_id ] = this . value //stores the markdown text for the corresponding div
287+ render_md = md . render ( this . value )
288+ $ ( `#out-text-div_${ id } ` ) . html ( render_md ) . show ( )
289+ document . getElementById ( div_id ) . style . display = "none"
290+
291+ } )
292+
293+ $ ( document ) . on ( "dblclick" , "div.text-out-box" , function ( ) {
294+ let id = this . id . split ( "_" ) [ 1 ]
295+ md_id = `text-div_${ id } `
296+ out_id = `out-text-div_${ id } `
297+ md_txt = md_texts [ md_id ]
298+
299+ document . getElementById ( md_id ) . style . display = "block"
300+ document . getElementById ( md_id ) . value = md_txt
301+ document . getElementById ( out_id ) . style . display = "none"
302+
303+ } )
304+
305+
306+ function update_text_box_size ( ) {
307+ $ ( 'textarea' ) . each ( function ( ) {
308+ this . setAttribute ( 'style' , 'height:' + ( this . scrollHeight ) + 'px;overflow-y:hidden;' ) ;
309+ } ) . on ( 'input' , function ( ) {
310+ this . style . height = 'auto' ;
311+ this . style . height = ( this . scrollHeight ) + 'px' ;
312+ } ) ;
313+ }
314+
315+
316+
317+
0 commit comments