Skip to content

Commit 1793c34

Browse files
committed
feature: aleman: nemo: submenu
1 parent 9d0136f commit 1793c34

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

nemo/state/parse-state.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ export const parseState = (source) => {
1515
items,
1616
};
1717

18-
for (const [index, line] of lines.entries()) {
18+
for (let index = 0; index < lines.length; index++) {
19+
const line = lines[index];
1920
const {
2021
mark,
2122
name,
2223
hasSubmenu,
24+
show,
2325
} = parseLine(line);
2426

2527
const selected = mark === '+';
@@ -33,13 +35,33 @@ export const parseState = (source) => {
3335
path: name,
3436
};
3537

36-
if (hasSubmenu)
38+
if (hasSubmenu) {
3739
assign(current, {
3840
submenu: {
39-
show: false,
41+
show,
4042
items: [],
4143
},
4244
});
45+
46+
for (;index < lines.length; index++) {
47+
const line = lines[index];
48+
const {
49+
mark,
50+
name: submenuName,
51+
} = parseLine(line);
52+
53+
const selected = mark === '+';
54+
55+
if (selected)
56+
state.index = index;
57+
58+
current.submenu.items.push({
59+
selected,
60+
name,
61+
path: `${name}.${submenuName}`,
62+
});
63+
}
64+
}
4365

4466
items.push(current);
4567
}
@@ -49,10 +71,15 @@ export const parseState = (source) => {
4971

5072
function parseLine(line) {
5173
let hasSubmenu = false;
74+
let show = false;
5275

5376
if (line.endsWith('*')) {
5477
line = line.slice(0, -1);
5578
hasSubmenu = true;
79+
} else if (line.endsWith('>')) {
80+
line = line.slice(0, -1);
81+
hasSubmenu = true;
82+
show = true;
5683
}
5784

5885
const [mark, ...nameChars] = line;
@@ -62,5 +89,7 @@ function parseLine(line) {
6289
mark,
6390
name,
6491
hasSubmenu,
92+
show,
6593
};
6694
}
95+

nemo/state/print-state.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export const printState = ({items}) => {
33

44
for (const {selected, name, submenu} of items) {
55
const selectionMark = selected ? '+' : '-';
6-
const submenuMark = submenu ? '*' : '';
6+
const submenuMark = submenu ? submenu.show ? '>' : '*' : '';
77

88
const current = `${selectionMark}${name}${submenuMark}`;
99

nemo/state/state.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,21 @@ export const updateState = (command, state, options = {}) => {
6565

6666
function down(state, {infiniteScroll}) {
6767
let {index, items} = state;
68+
const current = items[index];
6869

6970
if (index === -1) {
7071
const [first] = items;
7172
++index;
7273
first.selected = true;
7374
} else if (index < items.length - 1) {
74-
items[index].selected = false;
75+
current.selected = false;
7576
++index;
76-
items[index].selected = true;
77+
const next = items[index];
78+
79+
next.selected = true;
80+
81+
if (next.submenu)
82+
next.submenu.show = true;
7783
}
7884

7985
if (infiniteScroll && index === items.length - 1) {
@@ -89,9 +95,14 @@ function down(state, {infiniteScroll}) {
8995

9096
function up(state, {infiniteScroll}) {
9197
let {index, items} = state;
98+
const current = items[index];
9299

93100
if (index > 0) {
94-
items[index].selected = false;
101+
current.selected = false;
102+
103+
if (current.submenu)
104+
current.submenu.show = false;
105+
95106
--index;
96107
items[index].selected = true;
97108

nemo/state/state.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,39 @@ test('state: updateState: up: infiniteScroll', (t) => {
219219
});
220220
t.end();
221221
});
222+
223+
test.only('state: updateState: up: submenu', (t) => {
224+
const from = montag`
225+
-Hello
226+
-World
227+
+ABC>
228+
-A
229+
-B
230+
`;
231+
232+
const to = montag`
233+
-Hello
234+
+World
235+
-ABC*
236+
`;
237+
238+
t.updateState('up', from, to);
239+
t.end();
240+
});
241+
242+
test.only('state: updateState: down: submenu', (t) => {
243+
const from = montag`
244+
-Hello
245+
+World
246+
-ABC*
247+
`;
248+
249+
const to = montag`
250+
-Hello
251+
-World
252+
+ABC>
253+
`;
254+
255+
t.updateState('down', from, to);
256+
t.end();
257+
});

0 commit comments

Comments
 (0)