diff --git a/0006-on.mouse_press b/0006-on.mouse_press index b751dee..fe48806 100644 --- a/0006-on.mouse_press +++ b/0006-on.mouse_press @@ -29,11 +29,17 @@ on.mouse_press = function(x,y, mouse_button) end local button = on_button(x,y) if button then + local n, id = new_comment(button.name, button.depth) + local parent = find_node(Global_state.thread, button.name) + assert(parent) + table.insert(parent.metadata.replies, id) + save_metadata(parent) + local item_index = find_comment_index(button.name) table.insert(Global_state.thread.data, - button.item_index+1, - new_comment(button.name, button.depth)) + item_index+1, + n) A() end -- pan surface Pan = {x=Viewport.x+x/Viewport.zoom,y=Viewport.y+y/Viewport.zoom} -end +end \ No newline at end of file diff --git a/0118-load_subtree b/0118-load_subtree index 62fab08..ab21caa 100644 --- a/0118-load_subtree +++ b/0118-load_subtree @@ -1,7 +1,7 @@ -load_subtree = function(filename, out, depth) +load_subtree = function(id, out, depth) -- load a file and recursively all replies to it --- print('load_subtree', filename) - local item = initialize_item(filename, depth) +-- print('load_subtree', id) + local item = initialize_item(id, depth) -- every item renders to a row consisting of two -- columns: padding and 'item stuff' local row = cols(Inter_comment_spacing) @@ -12,9 +12,9 @@ load_subtree = function(filename, out, depth) table.insert(row.data, item_stuff) table.insert(item_stuff.data, item) table.insert(item_stuff.data, - reply_button(filename, #out, depth)) + reply_button(id, depth)) for i,reply_id in ipairs(item.metadata.replies) do local reply = load_subtree(reply_id, out, depth+1) end return item -end \ No newline at end of file +end diff --git a/0120-initialize_item b/0120-initialize_item index 2c7aae4..0c4e140 100644 --- a/0120-initialize_item +++ b/0120-initialize_item @@ -1,14 +1,14 @@ -initialize_item = function(filename, depth) - local result = {filename=full_path(filename)} +initialize_item = function(id, depth) + local result = { + type='text', + id=id, filename=full_path(id), + width=Width, depth=depth, + metadata=load_metadata(id), + border=Border_color, + } load_from_disk(result) -- massage schema slightly; this isn't an editor state result.data = result.lines result.lines = nil - -- more fields - result.type = 'text' - result.width = Width - result.depth = depth - result.metadata = load_metadata(filename) - result.border = Border_color return result end \ No newline at end of file diff --git a/0139-new_comment b/0139-new_comment index 6d3b503..76b69d1 100644 --- a/0139-new_comment +++ b/0139-new_comment @@ -1,18 +1,23 @@ new_comment = function(parent_id, depth) + local id = next_comment_id(parent_id) + print('creating', id) local comment = { type='text', - filename=full_path(next_comment(parent_id)), + id=id, + filename=full_path(id), data={{data=''}}, width=Width, depth=depth+1, metadata={replies={}}, border=Border_color, } + save_metadata(comment) local result = cols(20) table.insert(result.data, indent(depth+1)) local item_stuff = rows() table.insert(result.data, item_stuff) table.insert(item_stuff.data, comment) - table.insert(item_stuff.data, reply_button(comment.filename, 0, depth+1)) - return result -end \ No newline at end of file + table.insert(item_stuff.data, + reply_button(comment.id, depth+1)) + return result, id +end diff --git a/0142-reply_button b/0142-reply_button index 4609681..c578c9d 100644 --- a/0142-reply_button +++ b/0142-reply_button @@ -1,10 +1,10 @@ -reply_button = function(filename, index, depth) +reply_button = function(filename, depth) return { type='text', data={{data='reply'}}, button=true, margin=20, - name=filename, item_index=index, + name=filename, depth=depth, bg=Reply_button_color, border=Reply_button_border_color, } -end \ No newline at end of file +end diff --git a/0143-find_node b/0143-find_node new file mode 100644 index 0000000..6c9d298 --- /dev/null +++ b/0143-find_node @@ -0,0 +1,15 @@ +find_node = function(node, id) + if node.type == 'text' and not node.button then + if node.id == id then + return node + else + return nil + end + end + if node.type == 'rows' or node.type == 'cols' then + for _,child in ipairs(node.data) do + local t = find_node(child, id) + if t then return t end + end + end +end diff --git a/0140-next_comment b/0144-next_comment_id similarity index 80% rename from 0140-next_comment rename to 0144-next_comment_id index 6c33a02..e52f29c 100644 --- a/0140-next_comment +++ b/0144-next_comment_id @@ -1,4 +1,4 @@ -next_comment = function(id) +next_comment_id = function(id) local num_replies = #load_metadata(id).replies local corename = id:gsub('%.md$', '') return ('%s-%d.md'):format(corename, num_replies) diff --git a/0145-save_metadata b/0145-save_metadata new file mode 100644 index 0000000..68c6671 --- /dev/null +++ b/0145-save_metadata @@ -0,0 +1,6 @@ +save_metadata = function(node) + --print('saving metadata for', node.id) + local mfile = metadata_file(node.id) + local mpath = save_dir_path(mfile) + love.filesystem.write(mpath, json.encode(node.metadata)) +end diff --git a/0146-find_comment_index b/0146-find_comment_index new file mode 100644 index 0000000..15aa19c --- /dev/null +++ b/0146-find_comment_index @@ -0,0 +1,11 @@ +find_comment_index = function(id) + assert(Global_state.thread.type == 'rows') + for i,row in ipairs(Global_state.thread.data) do + assert(row.type == 'cols') + assert(#row.data == 2) + assert(row.data[2].type == 'rows') + if row.data[2].data[1].id == id then + return i + end + end +end \ No newline at end of file