diff --git a/lib/core/checklist.dart b/lib/core/checklist.dart index b8a5927d..3f41d6b5 100644 --- a/lib/core/checklist.dart +++ b/lib/core/checklist.dart @@ -62,22 +62,33 @@ class Checklist { } void addItem(bool value, String text) { + if (!text.endsWith('\n')) { + text += '\n'; + } var elem = md.Element.withTag("input"); elem.attributes["type"] = "checkbox"; elem.attributes["checked"] = value.toString(); elem.attributes["xUpperCase"] = "false"; - elem.attributes["text"] = text + "\n"; + elem.attributes["text"] = text; items.add(ChecklistItem.fromMarkdownElement(elem)); nodes.add(elem); } + + void removeItem(ChecklistItem item) { + assert(nodes.contains(item.element)); + assert(items.contains(item)); + + nodes.remove(item.element); + items.remove(item); + } } /// Copied from flutter-markdown - cannot be merged as we added xUpperCase and changed the regexp /// Parse [task list items](https://github.github.com/gfm/#task-list-items-extension-). class TaskListSyntax extends md.InlineSyntax { // FIXME: Waiting for dart-lang/markdown#269 to land - static final String _pattern = r'^ *\[([ xX])\] +(.*)$'; + static final String _pattern = r'^ *\[([ xX])\] +(.*)\n'; TaskListSyntax() : super(_pattern); @@ -158,7 +169,11 @@ class CustomRenderer implements md.NodeVisitor { } else { buffer.write('[ ] '); } - buffer.write(el.attributes['text']); + var text = el.attributes['text']; + buffer.write(text); + if (!text.endsWith('\n')) { + buffer.write('\n'); + } } } } diff --git a/lib/editors/checklist_editor.dart b/lib/editors/checklist_editor.dart index 5ad5943e..b84b91ba 100644 --- a/lib/editors/checklist_editor.dart +++ b/lib/editors/checklist_editor.dart @@ -121,8 +121,7 @@ class ChecklistEditorState extends State }, itemRemoved: () { setState(() { - // FIXME: The body isn't a good indicator, there could be multiple with the same body! - // todos.removeWhere((t) => t.body == todo.body); + checklist.removeItem(item); }); }, ); diff --git a/test/checklist_test.dart b/test/checklist_test.dart index 9b12bf2e..97d9fadd 100644 --- a/test/checklist_test.dart +++ b/test/checklist_test.dart @@ -32,6 +32,7 @@ How are you doing? [x] item 2 [x] item 3 [ ] item 4 +[ ] item 5 Booga Wooga """; @@ -44,17 +45,19 @@ Booga Wooga await note.load(); var checklist = Checklist(note); - expect(checklist.items.length, equals(4)); + expect(checklist.items.length, equals(5)); expect(checklist.items[0].checked, false); expect(checklist.items[1].checked, true); expect(checklist.items[2].checked, true); expect(checklist.items[3].checked, false); + expect(checklist.items[4].checked, false); expect(checklist.items[0].text, "item 1"); expect(checklist.items[1].text, "item 2"); expect(checklist.items[2].text, "item 3"); expect(checklist.items[3].text, "item 4"); + expect(checklist.items[4].text, "item 5"); // // Serialization @@ -65,6 +68,8 @@ Booga Wooga checklist.items[1].text = "Foo"; checklist.addItem(false, "Howdy"); + checklist.removeItem(checklist.items[4]); + await checklist.note.save(); var expectedContent = """---