checklists: Do not add unnecessary \n

This commit is contained in:
Vishesh Handa
2020-02-17 01:05:35 +01:00
parent 27875eba0c
commit 4f5c44812f
2 changed files with 35 additions and 3 deletions

View File

@ -105,10 +105,9 @@ class Checklist {
} }
void addItem(ChecklistItem item) { void addItem(ChecklistItem item) {
_insertNewLineIfRequired(nodes.length - 1);
items.add(item); items.add(item);
if (nodes.isNotEmpty && !nodes.last.textContent.endsWith('\n')) {
nodes.add(md.Text("\n"));
}
nodes.add(item.element); nodes.add(item.element);
} }
@ -126,9 +125,22 @@ class Checklist {
var prevItem = items[index]; var prevItem = items[index];
var nodeIndex = nodes.indexOf(prevItem.element); var nodeIndex = nodes.indexOf(prevItem.element);
_insertNewLineIfRequired(nodeIndex);
nodes.insert(nodeIndex, item.element); nodes.insert(nodeIndex, item.element);
items.insert(index, item); items.insert(index, item);
} }
void _insertNewLineIfRequired(int pos) {
if (nodes.isEmpty) return;
var node = nodes[pos];
if (node is md.Text) {
if (!node.text.endsWith('\n')) {
nodes.add(md.Text("\n"));
}
}
}
} }
/// Copied from flutter-markdown - cannot be merged as we added xUpperCase and changed the regexp /// Copied from flutter-markdown - cannot be merged as we added xUpperCase and changed the regexp

View File

@ -150,6 +150,26 @@ Booga Wooga
expect(note.body, "Hi.\n[ ] item\n"); expect(note.body, "Hi.\n[ ] item\n");
}); });
test('Should not add \\n when adding after item', () async {
var content = "[ ] one";
var notePath = p.join(tempDir.path, "note13.md");
await File(notePath).writeAsString(content);
var parentFolder = NotesFolder(null, tempDir.path);
var note = Note(parentFolder, notePath);
await note.load();
var checklist = Checklist(note);
var items = checklist.items;
expect(items.length, equals(1));
checklist.addItem(checklist.buildItem(false, "item"));
note = checklist.note;
expect(note.body, "[ ] one\n[ ] item\n");
});
test('insertItem works', () async { test('insertItem works', () async {
var content = "Hi.\n[ ] One\nTwo\n[ ] Three"; var content = "Hi.\n[ ] One\nTwo\n[ ] Three";