From 79aba57dcbd65e8fc0d0bfbb4de71f5301cfadd7 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Fri, 29 Apr 2022 21:27:57 +0200 Subject: [PATCH] Problem: moving up from closed item, moved too far Solution: add tests for this problem, and remove indent code when moving up --- list-editor/spec/cursor.spec.js | 54 +++++++++++++++++++++++++++++++++ list-editor/store.js | 8 +---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/list-editor/spec/cursor.spec.js b/list-editor/spec/cursor.spec.js index eee473d..cb5d31c 100644 --- a/list-editor/spec/cursor.spec.js +++ b/list-editor/spec/cursor.spec.js @@ -47,4 +47,58 @@ describe("A cursor", function() { expect(this.cursor.get()).toBe(3) }) }) + + describe("with a store with current closed", function () { + beforeEach(function () { + this.store = createStore([ + {indented:0, fold: 'open'}, + {indented:1, fold: 'open'}, + {indented:2, fold: 'open'}, + {indented:3, fold: 'open'}, + {indented:1, fold: 'closed', hidden: true}, + ]) + }) + + it("moveUp moves up by one", function() { + this.cursor.set(4) + this.cursor.moveUp(this.store) + expect(this.cursor.get()).toBe(3) + }) + }) + + describe("with a store with above closed", function () { + beforeEach(function () { + this.store = createStore([ + {indented:0, fold: 'open'}, + {indented:1, fold: 'open'}, + {indented:2, fold: 'open'}, + {indented:3, fold: 'closed', hidden: true}, + {indented:1, fold: 'open'}, + ]) + }) + + it("moveUp moves up by one", function() { + this.cursor.set(4) + this.cursor.moveUp(this.store) + expect(this.cursor.get()).toBe(2) + }) + }) + + describe("with a store with multiple above closed", function () { + beforeEach(function () { + this.store = createStore([ + {indented:0, fold: 'open'}, + {indented:1, fold: 'open'}, + {indented:2, fold: 'closed', hidden: true}, + {indented:3, fold: 'closed', hidden: true}, + {indented:1, fold: 'open'}, + ]) + }) + + it("moveUp moves up by one", function() { + this.cursor.set(4) + this.cursor.moveUp(this.store) + expect(this.cursor.get()).toBe(1) + }) + }) }) diff --git a/list-editor/store.js b/list-editor/store.js index ab61ef1..0e4bed1 100644 --- a/list-editor/store.js +++ b/list-editor/store.js @@ -55,21 +55,15 @@ function Store(inputData) { } function prevCursorPosition(cursor) { - let curIndent = values[idList[cursor]].indented - let curClosed = values[idList[cursor]].fold !== 'open'; - if (!curClosed) { - curIndent = 10000000; - } let moving = true while (moving) { cursor-- if (cursor < 0) { cursor = idList.length - 1 - curIndent = values[idList[cursor]].indented } let next = values[idList[cursor]]; - if (curIndent >= next.indented && !next.hidden) { + if (!next.hidden) { moving = false } }