wiki/editor/src/actions.js
Peter Stuifzand 75209250c3
All checks were successful
continuous-integration/drone/push Build is passing
Add new actions for todo's and sorting to contextmenu
2020-11-11 22:16:07 +01:00

94 lines
2.5 KiB
JavaScript

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 => {
let res = item.text.match(/^#\[\[(TODO|DONE)/)
if (!res) return "C";
if (res[1] === 'TODO') return direction === 'asc' ? "A" : "B";
if (res[1] === 'DONE') return direction === 'asc' ? "B" : "A";
return "D";
}, 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
}