Rotate items to moveBefore
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
9f51bff58c
commit
39dd45b65d
|
@ -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)
|
||||
|
|
|
@ -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"])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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;
|
Loading…
Reference in New Issue
Block a user