From 5a7067dc7d47be852123576582193579dec643f5 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Tue, 24 Aug 2021 22:27:16 +0200 Subject: [PATCH] fix(store): move before and fix indent of multiple items --- list-editor/spec/store.spec.js | 10 ++++++++++ list-editor/store.js | 12 +++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/list-editor/spec/store.spec.js b/list-editor/spec/store.spec.js index be2a3b9..5f737ec 100644 --- a/list-editor/spec/store.spec.js +++ b/list-editor/spec/store.spec.js @@ -214,5 +214,15 @@ describe("A store", function () { expect(debug.idList).toEqual(["_a", "_b", "_c", "_e", "_d"]) expect(debug.result[4]).toEqual({text: "d", id: "_d", indented: 0}) }) + it("moveBefore _b, at-end", function () { + store.moveBefore("_b", "at-end") + let debug = store.debug(); + expect(debug.idList).toEqual(["_a", "_e", "_b", "_c", "_d"]) + expect(debug.result[0]).toEqual({text: "a", id: "_a", indented: 0}) + expect(debug.result[1]).toEqual({text: "e", id: "_e", indented: 0}) + expect(debug.result[2]).toEqual({text: "b", id: "_b", indented: 0}) + expect(debug.result[3]).toEqual({text: "c", id: "_c", indented: 1}) + expect(debug.result[4]).toEqual({text: "d", id: "_d", indented: 2}) + }) }) }) diff --git a/list-editor/store.js b/list-editor/store.js index 83a321d..e6dfd89 100644 --- a/list-editor/store.js +++ b/list-editor/store.js @@ -304,10 +304,16 @@ function Store(inputData) { // Copy indent from the next item, or 0 when at the end const v = value(to) - if (v) value(from).indented = v.indented - else value(from).indented = 0 + const diff = (v ? v.indented : 0) - value(from).indented - return [index(from), n] + let first = index(from) + let i = 0 + while (i < n) { + value(idList[first+i]).indented += diff + i++ + } + + return [first, n] } /**