|
| 1 | +import { head, take } from "ramda"; |
| 2 | + |
1 | 3 | export type Context = { |
2 | 4 | list: string[], |
3 | 5 | selected: string[], |
| 6 | + adjacentPivot: undefined, |
| 7 | + lastSelected: undefined, |
| 8 | +} | { |
| 9 | + list: string[], |
| 10 | + selected: string[], |
| 11 | + adjacentPivot: string, |
| 12 | + lastSelected: string, |
4 | 13 | } |
5 | 14 |
|
6 | 15 | type Command = |
7 | 16 | | { type: "SELECT ONE", id: string } |
8 | 17 | | { type: "TOGGLE SELECTION", id: string } |
9 | 18 | | { type: "DESELECT ALL" } |
| 19 | + | { type: "SELECT ADJACENT", id: string } |
| 20 | + |
| 21 | +function listIncludesAndIsNotEmpty(list: string[], item: string) { |
| 22 | + return list.length > 0 && list.includes(item) |
| 23 | +} |
10 | 24 |
|
11 | 25 | export function multiselect(context: Context, command: Command): Context { |
12 | | - if (command.type === "SELECT ONE" && context.list.includes(command.id)) { |
| 26 | + if ( |
| 27 | + command.type === "SELECT ONE" && |
| 28 | + listIncludesAndIsNotEmpty(context.list, command.id) |
| 29 | + ) { |
| 30 | + return { |
| 31 | + ...context, |
| 32 | + selected: [command.id], |
| 33 | + adjacentPivot: command.id, |
| 34 | + lastSelected: command.id, |
| 35 | + }; |
| 36 | + } else if ( |
| 37 | + command.type === 'TOGGLE SELECTION' && |
| 38 | + listIncludesAndIsNotEmpty(context.list, command.id) && |
| 39 | + context.selected.includes(command.id) && |
| 40 | + context.selected.length === 1 |
| 41 | + ) { |
| 42 | + const index = context.selected.indexOf(command.id) |
13 | 43 | return { |
14 | 44 | ...context, |
15 | | - selected: [command.id] |
| 45 | + selected: [], |
| 46 | + adjacentPivot: undefined, |
| 47 | + lastSelected: undefined |
16 | 48 | }; |
17 | 49 | } else if ( |
18 | 50 | command.type === 'TOGGLE SELECTION' && |
19 | | - context.list.includes(command.id) && |
| 51 | + listIncludesAndIsNotEmpty(context.list, command.id) && |
20 | 52 | context.selected.includes(command.id) |
21 | | - ) { |
| 53 | + ) { |
| 54 | + const index = context.selected.indexOf(command.id); |
| 55 | + const adjacentPivot= (index < (context.selected.length - 1)) |
| 56 | + ? context.selected[index + 1] |
| 57 | + : context.selected[index - 1]; |
22 | 58 | return { |
23 | 59 | ...context, |
24 | 60 | selected: context.selected.filter(x => x !== command.id), |
| 61 | + adjacentPivot, |
| 62 | + lastSelected: adjacentPivot |
25 | 63 | }; |
26 | 64 | } else if ( |
27 | | - command.type === 'TOGGLE SELECTION' && |
28 | | - context.list.includes(command.id) |
29 | | - ) { |
| 65 | + command.type === 'TOGGLE SELECTION' && |
| 66 | + context.list.includes(command.id) |
| 67 | + ) { |
30 | 68 | return { |
31 | 69 | ...context, |
32 | | - selected: context.selected.concat([command.id]) |
| 70 | + selected: context.selected.concat([command.id]), |
| 71 | + adjacentPivot: command.id, |
| 72 | + lastSelected: command.id |
33 | 73 | }; |
34 | 74 | } else if (command.type === "DESELECT ALL") { |
35 | 75 | return { |
36 | 76 | ...context, |
37 | | - selected: [] |
| 77 | + selected: [], |
| 78 | + adjacentPivot: undefined, |
| 79 | + lastSelected: undefined |
| 80 | + } |
| 81 | + } else if ( |
| 82 | + command.type === "SELECT ADJACENT" && |
| 83 | + listIncludesAndIsNotEmpty(context.list, command.id) && |
| 84 | + context.selected.length === 0 |
| 85 | + ) { |
| 86 | + const n = context.list.indexOf(command.id) + 1; |
| 87 | + return { |
| 88 | + ...context, |
| 89 | + selected: take(n, context.list), |
| 90 | + adjacentPivot: head(context.list)!, |
| 91 | + lastSelected: command.id, |
| 92 | + } |
| 93 | + } else if ( |
| 94 | + command.type === "SELECT ADJACENT" && |
| 95 | + listIncludesAndIsNotEmpty(context.list, command.id) && |
| 96 | + context.adjacentPivot |
| 97 | + ) { |
| 98 | + const start = context.list.indexOf(context.adjacentPivot); |
| 99 | + const n = context.list.indexOf(command.id); |
| 100 | + return { |
| 101 | + ...context, |
| 102 | + selected: context.list.slice(Math.min(start, n), Math.max(start, n) + 1), |
| 103 | + lastSelected: command.id |
38 | 104 | } |
39 | 105 | } else { |
40 | 106 | return context; |
|
0 commit comments