function hasCheckbox(id) { let found = false let todo = false this.update(id, function (item, prev, next) { let res = item.text.match(/^#\[\[(TODO|DONE)\]\]/) if (res) { found = true todo = res[1] === 'TODO' } return item }); return [found, todo] } function addCheckbox(id) { this.update(id, function (item, prev, next) { if (item.text.match(/^#\[\[TODO\]\]/)) { return item } else if (item.text.match(/^#\[\[|DONE\]\]/)) { item.text = item.text.replace(/^#\[\[DONE\]\]/, '#[[TODO]]') } else { item.text = '#[[TODO]] ' + item.text } return item }); } function removeCheckbox(id) { this.update(id, function (item, prev, next) { if (item.text.match(/^#\[\[(TODO|DONE)\]\]/)) { item.text = item.text.replace(/#\[\[(TODO|DONE)\]\]\s*/, '') } return item }); } function markDone(id) { this.update(id, function (item, prev, next) { if (item.text.match(/^#\[\[(TODO)\]\]/)) { item.text = item.text.replace(/#\[\[(TODO)\]\]\s*/, '#[[DONE]]') } return item }); } function markTodo(id) { this.update(id, function (item, prev, next) { if (item.text.match(/^#\[\[(DONE)\]\]/)) { item.text = item.text.replace(/#\[\[(DONE)\]\]\s*/, '#[[TODO]]') } return item }); } function sort(id, property, direction) { let children = this.getChildren(id) if (property === 'todo') { children = _.orderBy(children, item => { const res = item.text.match(/^#\[\[(TODO|DONE)/) if (!res) return "C"; if (res[1] === 'TODO') return "A"; if (res[1] === 'DONE') return "B"; return "D"; }, direction) } else if (property === 'text') { children = _.orderBy(children, item => { return item.text.replace(/^#\[\[(TODO|DONE)]]\s*/, '') }, direction) } else { children = _.orderBy(children, property, direction) } this.replaceChildren(id, children) } function removeCompleted(id) { let children = this.getChildren(id) let newChildren = _.filter(children, item => { let res = item.text.match(/^#\[\[(TODO|DONE)\]\]/) let found = false, todo = false if (res) { found = true todo = res[1] === 'TODO' } return !found || todo }) this.replaceChildren(id, newChildren) } export default { hasCheckbox, addCheckbox, removeCheckbox, markTodo, markDone, sort, removeCompleted }