2020-10-14 21:43:52 +00:00
|
|
|
function createCursor(start) {
|
|
|
|
let cursor = start;
|
2020-10-31 12:36:49 +00:00
|
|
|
let lastMovement = 0;
|
2020-10-14 21:43:52 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
get() {
|
|
|
|
return cursor;
|
|
|
|
},
|
|
|
|
set(newPosition) {
|
|
|
|
cursor = newPosition;
|
|
|
|
},
|
|
|
|
getId(store) {
|
|
|
|
return store.currentID(cursor)
|
|
|
|
},
|
|
|
|
atFirst() {
|
|
|
|
return cursor === 0;
|
|
|
|
},
|
|
|
|
atPosition(other) {
|
|
|
|
return cursor === other;
|
|
|
|
},
|
|
|
|
atEnd(store) {
|
|
|
|
return cursor === store.length()
|
|
|
|
},
|
|
|
|
hasMoved(saved) {
|
|
|
|
return cursor !== saved.get()
|
|
|
|
},
|
|
|
|
getCurrent(store) {
|
|
|
|
let id = store.currentID(cursor)
|
|
|
|
return store.value(id)
|
|
|
|
},
|
|
|
|
getCurrentElement(elements) {
|
|
|
|
return elements.slice(cursor, cursor + 1);
|
|
|
|
},
|
|
|
|
save() {
|
|
|
|
return createCursor(cursor);
|
|
|
|
},
|
|
|
|
moveUp(store) {
|
|
|
|
cursor = store.prevCursorPosition(cursor)
|
2020-10-31 12:36:49 +00:00
|
|
|
lastMovement = -1
|
2020-10-14 21:43:52 +00:00
|
|
|
},
|
|
|
|
moveDown(store) {
|
|
|
|
cursor = store.nextCursorPosition(cursor, true)
|
2020-10-31 12:36:49 +00:00
|
|
|
lastMovement = 1
|
2020-10-14 21:43:52 +00:00
|
|
|
},
|
|
|
|
remove(store) {
|
2020-10-28 19:12:04 +00:00
|
|
|
let id = store.currentID(cursor)
|
|
|
|
let item = store.value(id)
|
|
|
|
store.remove(cursor)
|
|
|
|
if (cursor >= store.length()) {
|
|
|
|
cursor = store.length() - 1
|
|
|
|
}
|
2020-10-14 21:43:52 +00:00
|
|
|
},
|
|
|
|
insertAbove(store, item) {
|
|
|
|
store.insertBefore(store.currentID(cursor), item)
|
|
|
|
},
|
|
|
|
insertBelow(store, item) {
|
2020-10-28 19:56:40 +00:00
|
|
|
let currentID = store.currentID(cursor)
|
|
|
|
let currentItem = store.value(currentID)
|
|
|
|
if (currentItem.fold !== 'open') {
|
|
|
|
cursor = store.lastHigherIndented(cursor)
|
|
|
|
item.indented = currentItem.indented
|
|
|
|
cursor--
|
|
|
|
}
|
2020-10-14 21:43:52 +00:00
|
|
|
let id = store.insertAfter(store.currentID(cursor), item)
|
|
|
|
cursor = store.index(id)
|
|
|
|
},
|
|
|
|
forwardToNextVisible(store) {
|
|
|
|
cursor = store.nextCursorPosition(cursor, false)
|
|
|
|
},
|
2020-10-31 12:36:49 +00:00
|
|
|
lastDir() {
|
|
|
|
return lastMovement
|
|
|
|
},
|
|
|
|
resetLastMove() {
|
|
|
|
lastMovement = 0
|
|
|
|
}
|
2020-10-14 21:43:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default createCursor;
|