Checklists: Allow items to be removed

This commit is contained in:
Vishesh Handa
2020-02-10 16:55:21 +01:00
parent 8bebe91bc0
commit dbbe8589c6
3 changed files with 25 additions and 6 deletions

View File

@ -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');
}
}
}
}

View File

@ -121,8 +121,7 @@ class ChecklistEditorState extends State<ChecklistEditor>
},
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);
});
},
);

View File

@ -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 = """---