Skip to content

Commit c1d05d0

Browse files
committed
select previous: should be from last selected not pivot #14
1 parent 24b80e5 commit c1d05d0

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "macos-multi-select",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "Given a list of ids, and an action, return a list of selected items with the same behaviour of macOS finder list view selection.",
55
"main": "dist/index.js",
66
"repository": "[email protected]:codingedgar/macos-multi-select.git",

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export function multiselect(context: Context, command: Command): Context {
8282
return {
8383
...context,
8484
selected: take(n, context.list),
85+
adjacentPivot: head(context.list),
8586
}
8687
} else if (
8788
command.type === "SELECT ADJACENT" &&
@@ -160,9 +161,9 @@ export function multiselect(context: Context, command: Command): Context {
160161
} else if (
161162
command.type === "SELECT PREVIOUS" &&
162163
context.list.length &&
163-
context.adjacentPivot !== undefined
164+
context.selected.length
164165
) {
165-
const pivotIndex = context.list.indexOf(context.adjacentPivot)
166+
const pivotIndex = context.list.indexOf(last(context.selected)!)
166167

167168
if (pivotIndex > 0) {
168169
const prevItem = context.list[pivotIndex - 1];

src/spec/selectPrevious.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,63 @@ describe('Select Previous Item', () => {
110110
)
111111
})
112112

113+
test('Should select previous from the last selected (not the pivot) given the last command were select adjacent', () => {
114+
fc.assert(
115+
fc.property(
116+
fc.tuple(
117+
fc.boolean(),
118+
fc.set(
119+
fc.string(), { minLength: 1 }
120+
)
121+
)
122+
.chain(([undefOrTop, list]) =>
123+
fc.nat(list.length - 1)
124+
.map(id => ({
125+
list,
126+
adjacentPivot: undefOrTop ? undefined : head(list),
127+
id: list[id],
128+
}))
129+
)
130+
,
131+
({
132+
adjacentPivot,
133+
list,
134+
id,
135+
}) => {
136+
137+
const prevContext = multiselect(
138+
{
139+
list,
140+
adjacentPivot: adjacentPivot,
141+
selected: []
142+
},
143+
{
144+
type: "SELECT ADJACENT",
145+
id,
146+
}
147+
)
148+
149+
const currentSelectionPivot = list.indexOf(id)
150+
const nextSelection = currentSelectionPivot > 0 ? currentSelectionPivot - 1 : 0
151+
const nextPivot = list[nextSelection];
152+
153+
expect(
154+
multiselect(
155+
prevContext,
156+
{
157+
type: "SELECT PREVIOUS",
158+
}
159+
)
160+
)
161+
.toEqual({
162+
list,
163+
selected: [nextPivot],
164+
adjacentPivot: nextPivot,
165+
})
166+
167+
}
168+
)
169+
)
170+
})
171+
113172
});

0 commit comments

Comments
 (0)