Skip to content

Commit 157c205

Browse files
committed
saving and rendering notebook the way it is written
1 parent cd70ea9 commit 157c205

2 files changed

Lines changed: 24 additions & 16 deletions

File tree

notebookjs/src/public/javascripts/index.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let vars_in_scope = {
2626
"div-1": editor
2727
}
2828

29+
let cells_order = ["div-1"] // store the cells order
2930
var md_texts = {} //stores markdown text and corresponding div name
3031
var __code_cell_count = 1
3132

@@ -98,7 +99,6 @@ function add_new_code_cell(c_id, where) {
9899
__code_cell_count += 1
99100
let last_scope_id = parseInt(Object.keys(vars_in_scope).pop().split("-")[1])
100101
let id = c_id.split("-")[1]
101-
102102
if (where == "down") {
103103
where = "down"
104104
} else {
@@ -155,10 +155,14 @@ function add_new_code_cell(c_id, where) {
155155

156156
if (where == "up") {
157157
divReference.insertAdjacentHTML("beforebegin", html);
158+
let current_cell_id = cells_order.indexOf(`div-${id}`)
159+
cells_order.splice(current_cell_id,0,`div-${new_id}`)
158160
} else {
159161
divReference.insertAdjacentHTML("afterend", html);
162+
cells_order[new_id-1] = `div-${new_id}`
160163
}
161164

165+
console.log(cells_order)
162166
let editor = CodeMirror(document.getElementById(`div-${new_id}`), {
163167
lineNumbers: true,
164168
tabSize: 2,
@@ -195,7 +199,7 @@ function add_new_text_cell(c_id, where) {
195199
__code_cell_count += 1
196200
let last_scope_id = parseInt(Object.keys(vars_in_scope).pop().split("-")[1])
197201
let id = c_id.split("-")[1]
198-
202+
199203
if (where == "down") {
200204
where = "down"
201205
} else {
@@ -254,10 +258,14 @@ function add_new_text_cell(c_id, where) {
254258

255259
if (where == "up") {
256260
divReference.insertAdjacentHTML("beforebegin", html);
261+
let current_cell_id = cells_order.indexOf(`div_text-${id}`)
262+
cells_order.splice(current_cell_id,0,`div_text-${new_id}`)
257263
} else {
258264
divReference.insertAdjacentHTML("afterend", html);
265+
cells_order[new_id-1] = `div_text-${new_id}`
259266
}
260267

268+
console.log(cells_order)
261269
vars_in_scope[`div_text-${new_id}`] = ""
262270

263271
update_text_box_size()
@@ -364,7 +372,7 @@ function update_text_box_size() {
364372

365373

366374
$("#download").click(function () {
367-
let out = notebook_json(vars_in_scope, md_texts);
375+
let out = notebook_json(cells_order,vars_in_scope, md_texts);
368376

369377
var blob = new Blob([out], { "type": "application/json" });
370378
var url = (window.URL || window.webkitURL).createObjectURL(blob);
@@ -383,11 +391,11 @@ $("#download").click(function () {
383391

384392
$("#import-notebook-file").change(() => {
385393

386-
var files = $("#import-notebook-file")[0].files
394+
let files = $("#import-notebook-file")[0].files
387395
let json_content = null
388396
if (files.length > 0) {
389-
var content = files[0];
390-
var reader = new FileReader();
397+
let content = files[0];
398+
let reader = new FileReader();
391399
reader.onload = function (t) {
392400
json_content = t.target.result;
393401
let json = JSON.parse(json_content)

notebookjs/src/public/javascripts/utils.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,15 @@ function this_div() {
8787
}
8888

8989

90-
// function viz(name, callback) {
91-
// // out_div-1
92-
// let id = `#out_div${window.current_cell}`
93-
// // $(`${id}`).append(`<div id=${name}></div>`)
90+
function viz(name, callback) {
91+
// out_div-1
92+
let id = `#out_div${window.current_cell}`
93+
$(`${id}`).append(`<div id=${name}></div>`)
9494

95-
// let cb = callback(name);
96-
// // $("#ploty").remove(`${name}`)
95+
let cb = callback(name);
9796

98-
// return cb
99-
// }
97+
return cb
98+
}
10099

101100
function table(df) {
102101

@@ -152,12 +151,13 @@ function table(df) {
152151

153152
}
154153

155-
function notebook_json(scope, md_scope) {
154+
function notebook_json(cells_order,scope, md_scope) {
156155

157156
var store = {}
158157

159-
for (let key in scope) {
158+
for (let i=0; i < cells_order.length; i++) {
160159

160+
let key = cells_order[i];
161161
let key_split = key.split("-")
162162
let id = key_split[1]
163163

0 commit comments

Comments
 (0)