From 39dd45b65d4b292372ee0441a9846c5f93d471ba Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Fri, 30 Oct 2020 23:43:00 +0100 Subject: [PATCH] Rotate items to moveBefore --- list-editor/index.js | 4 ++++ list-editor/spec/store.spec.js | 29 +++++++++++++++++++++++++++++ list-editor/store.js | 19 +++++++++++++------ list-editor/util.js | 20 ++++++++++++++++++++ 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/list-editor/index.js b/list-editor/index.js index 4b8b866..88cfcd1 100644 --- a/list-editor/index.js +++ b/list-editor/index.js @@ -280,12 +280,16 @@ function editor(root, inputData, options) { }) drake.on('drop', function (el, target, source, sibling) { + stopEditing(root, store, currentEditor) let stopID = $(sibling).attr('data-id') if (startID === stopID) { return } let id = store.moveBefore(startID, stopID) + if (id === startID) { + return + } let position = store.index(id); cursor.set(position) diff --git a/list-editor/spec/store.spec.js b/list-editor/spec/store.spec.js index 5d9509f..9b95aa1 100644 --- a/list-editor/spec/store.spec.js +++ b/list-editor/spec/store.spec.js @@ -160,4 +160,33 @@ describe("A store", function () { }, ]) }) + + describe("contains a moveBefore method 2", function () { + let store + + beforeEach(function () { + store = createStore([ + {text: "a", id: "_a", indented: 0}, + {text: "b", id: "_b", indented: 0}, + {text: "c", id: "_c", indented: 0}, + {text: "d", id: "_d", indented: 0}, + ]) + }) + it("moveBefore _d, _a", function () { + store.moveBefore("_d", "_a") + expect(store.debug().idList).toEqual(["_d", "_a", "_b", "_c"]) + }) + it("moveBefore _d, _b", function () { + store.moveBefore("_d", "_b") + expect(store.debug().idList).toEqual(["_a", "_d", "_b", "_c"]) + }) + it("moveBefore _a, _d", function() { + store.moveBefore("_a", "_d") + expect(store.debug().idList).toEqual(["_b", "_c", "_a", "_d"]) + }) + it("moveBefore _b, _d", function() { + store.moveBefore("_b", "_d") + expect(store.debug().idList).toEqual(["_a", "_c", "_b", "_d"]) + }) + }) }) diff --git a/list-editor/store.js b/list-editor/store.js index 1c1ecd2..532e085 100644 --- a/list-editor/store.js +++ b/list-editor/store.js @@ -1,4 +1,5 @@ import _ from 'lodash'; +import rotate from './util' /** * NOTE: Store should contain all methods that work with items. At the moment @@ -285,12 +286,18 @@ function Store(inputData) { * @returns {string} */ function moveBefore(from, to) { - let fromIndex = _.findIndex(idList, (id) => id === from) - let item = values[from] - internalRemove(fromIndex, 1) - let result = insertBefore(to, item) - changed = true - return result + let fromIndex = index(from) + let toIndex = to ? index(to) : idList.length + let n = lastHigherIndented(fromIndex) - fromIndex + if (toIndex >= fromIndex && toIndex < fromIndex + n) return index(from) + if (fromIndex < toIndex) { + rotate(idList, fromIndex, toIndex, n) + changed = true + } else if (toIndex < fromIndex) { + rotate(idList, toIndex, fromIndex + n, fromIndex - toIndex) + changed = true + } + return index(from) } /** diff --git a/list-editor/util.js b/list-editor/util.js index e69de29..fecd9b5 100644 --- a/list-editor/util.js +++ b/list-editor/util.js @@ -0,0 +1,20 @@ +function reverse(a, from, to) { + --from; + while (++from < --to) { + var tmp = a[from]; + a[from] = a[to]; + a[to] = tmp; + } +} + +function rotate(a, from, to, k) { + var n = to - from; + k = (k % n + n) % n; + if (k > 0) { + reverse(a, from, from + k); + reverse(a, from + k, to); + reverse(a, from, to); + } +} + +export default rotate;