Move blocks to the right position

This commit is contained in:
Peter Stuifzand 2020-11-02 22:42:09 +01:00
parent 7ede2e8054
commit 56802dc106
2 changed files with 21 additions and 19 deletions

View File

@ -583,18 +583,16 @@ function editor(root, inputData, options) {
} }
function blockMoveBackward(event) { function blockMoveBackward(event) {
stopEditing(root, store, currentEditor); stopEditing(root, store, currentEditor)
let item = cursor.getCurrent(store); let before = _.clamp(cursor.get() - 1, 0, store.length())
let before = store.firstSameIndented(cursor.get(), item.indented) let beforeId = store.currentID(before)
let beforeID = store.currentID(before); let item = cursor.getCurrent(store)
let beforeItem = store.value(beforeID); let [index, n] = store.moveBefore(item.id, beforeId)
store.moveBefore(item.id, beforeID) let beforeItem = store.value(beforeId)
// NOTE: should moveBefore adjust the indents of the children let dir = beforeItem ? (beforeItem.indented - item.indented) : -item.indented
if (item.indented > beforeItem.indented) { store.indent(index, n, dir)
item.indented = beforeItem.indented cursor.set(index)
}
cursor.set(store.index(item.id))
trigger('change') trigger('change')
return false return false
@ -602,15 +600,16 @@ function editor(root, inputData, options) {
function blockMoveForward(event) { function blockMoveForward(event) {
stopEditing(root, store, currentEditor); stopEditing(root, store, currentEditor);
let blockLen = store.lastHigherIndented(cursor.get()) - cursor.get()
let before = _.clamp(cursor.get() + 1 + blockLen, 0, store.length())
let beforeId = store.currentID(before)
let item = cursor.getCurrent(store); let item = cursor.getCurrent(store);
let after = store.lastHigherIndented(cursor.get(), item.indented) let [index, n] = store.moveBefore(item.id, beforeId)
after = store.lastHigherIndented(after, item.indented) let beforeItem = store.value(beforeId)
store.moveBefore(item.id, store.currentID(after)) let dir = beforeItem ? (beforeItem.indented - item.indented) : -item.indented
store.indent(index, n, dir)
cursor.set(store.index(item.id)) cursor.set(index)
trigger('change') trigger('change')
return false return false
} }

View File

@ -1,5 +1,6 @@
import _ from 'lodash'; import _ from 'lodash';
import util from './util' import util from './util'
let rotate = util.rotate; let rotate = util.rotate;
/** /**
@ -250,6 +251,8 @@ function Store(inputData) {
* @param {int} dir * @param {int} dir
*/ */
function indent(first, len, dir) { function indent(first, len, dir) {
if (len === 0 || dir === 0) return
let selection = idList.slice(first, first + len) let selection = idList.slice(first, first + len)
let result = _.reduce(selection, function (list, itemId) { let result = _.reduce(selection, function (list, itemId) {
@ -298,7 +301,7 @@ function Store(inputData) {
rotate(idList, toIndex, fromIndex + n, fromIndex - toIndex) rotate(idList, toIndex, fromIndex + n, fromIndex - toIndex)
changed = true changed = true
} }
return index(from) return [index(from), n]
} }
/** /**