From 56802dc106a6275c41f4bfb457365311574f6fcc Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Mon, 2 Nov 2020 22:42:09 +0100 Subject: [PATCH] Move blocks to the right position --- list-editor/index.js | 35 +++++++++++++++++------------------ list-editor/store.js | 5 ++++- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/list-editor/index.js b/list-editor/index.js index 15d54d3..dc2e6ca 100644 --- a/list-editor/index.js +++ b/list-editor/index.js @@ -583,18 +583,16 @@ function editor(root, inputData, options) { } function blockMoveBackward(event) { - stopEditing(root, store, currentEditor); + stopEditing(root, store, currentEditor) - let item = cursor.getCurrent(store); - let before = store.firstSameIndented(cursor.get(), item.indented) - let beforeID = store.currentID(before); - let beforeItem = store.value(beforeID); - store.moveBefore(item.id, beforeID) - // NOTE: should moveBefore adjust the indents of the children - if (item.indented > beforeItem.indented) { - item.indented = beforeItem.indented - } - cursor.set(store.index(item.id)) + let before = _.clamp(cursor.get() - 1, 0, store.length()) + let beforeId = store.currentID(before) + let item = cursor.getCurrent(store) + let [index, n] = store.moveBefore(item.id, beforeId) + let beforeItem = store.value(beforeId) + let dir = beforeItem ? (beforeItem.indented - item.indented) : -item.indented + store.indent(index, n, dir) + cursor.set(index) trigger('change') return false @@ -602,15 +600,16 @@ function editor(root, inputData, options) { function blockMoveForward(event) { 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 after = store.lastHigherIndented(cursor.get(), item.indented) - after = store.lastHigherIndented(after, item.indented) - store.moveBefore(item.id, store.currentID(after)) - - cursor.set(store.index(item.id)) + let [index, n] = store.moveBefore(item.id, beforeId) + let beforeItem = store.value(beforeId) + let dir = beforeItem ? (beforeItem.indented - item.indented) : -item.indented + store.indent(index, n, dir) + cursor.set(index) trigger('change') - return false } diff --git a/list-editor/store.js b/list-editor/store.js index aa84788..980407a 100644 --- a/list-editor/store.js +++ b/list-editor/store.js @@ -1,5 +1,6 @@ import _ from 'lodash'; import util from './util' + let rotate = util.rotate; /** @@ -250,6 +251,8 @@ function Store(inputData) { * @param {int} dir */ function indent(first, len, dir) { + if (len === 0 || dir === 0) return + let selection = idList.slice(first, first + len) let result = _.reduce(selection, function (list, itemId) { @@ -298,7 +301,7 @@ function Store(inputData) { rotate(idList, toIndex, fromIndex + n, fromIndex - toIndex) changed = true } - return index(from) + return [index(from), n] } /**