From b4b277adbfd31e6e29aaf942e6f6dff7b2050567 Mon Sep 17 00:00:00 2001 From: brian Date: Wed, 10 Mar 2021 15:37:05 -0500 Subject: [PATCH] note text is now editable --- Note.js | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/Note.js b/Note.js index 5c41ff8..28ac22f 100644 --- a/Note.js +++ b/Note.js @@ -1,11 +1,44 @@ -function Note(){ + + +function Note(vnode_init){ +const {note} = vnode_init.attrs; +let is_editing = false; +let temp_text = note.text; +const edit_handler = function(){ + is_editing = true; + }; +const cancel_handler = function(){ + is_editing = false; + }; +const text_change_handler = function(e){ + temp_text = e.target.value; + // prevent redraw, because the temp text is anyway contained in the textarea: + // also, the "Save" button is going to cause its own redraw; otherwise clicking it would cause two redraws (and it is noticeable, that's how I knew two redraws were happening) + e.redraw = false; + }; +const save_handler = function(){ + note.text = temp_text; + is_editing = false; + }; return { -view: function(vnode){ -const {note} = vnode.attrs; -return m('.note', [ - m('.text', {key: 'text'}, note.text), - m('button', {key: 'edit-button'}, 'Edit') - ]); +view: function(){ +if(is_editing){ + return m('.note', [ + m('textarea', {key: 'textarea', onchange: text_change_handler}, note.text), + m('.buttons', {key: 'editing-buttons'}, [ + m('button', {key: 'cancel-button', onclick: cancel_handler}, 'Cancel'), + m('button', {key: 'save-button', onclick: save_handler}, 'Save') + ]) + ]); + } +else{ + return m('.note', [ + m('.text', {key: 'text'}, note.text), + m('.buttons', {key: 'viewing-buttons'}, [ + m('button', {key: 'edit-button', onclick: edit_handler}, 'Edit') + ]) + ]); + } } }; }