Skip to content

Commit 94d27ee

Browse files
committed
fix: selected nodes should reset when active node changes
1 parent f2958a6 commit 94d27ee

10 files changed

Lines changed: 69 additions & 61 deletions

File tree

src/stores/view/reducers/document/helpers/update-selection-state.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
import { DocumentViewState } from 'src/stores/view/view-state-type';
2-
import { ChangeActiveNodeAction } from 'src/stores/view/reducers/document/navigate-using-keyboard';
32
import { findNodeColumn } from 'src/lib/tree-utils/find/find-node-column';
43
import invariant from 'tiny-invariant';
54
import { updateSelectedNodes } from 'src/stores/view/reducers/document/helpers/update-selected-nodes';
6-
import { JumpToNodeAction } from 'src/stores/view/reducers/document/jump-to-node';
75
import { resetSelectionState } from 'src/stores/view/reducers/document/helpers/reset-selection-state';
6+
import { Column } from 'src/stores/document/document-state-type';
87

98
export const updateSelectionState = (
9+
columns: Column[],
1010
documentState: DocumentViewState,
1111
nextNode: string,
12-
action: ChangeActiveNodeAction | JumpToNodeAction,
12+
isVertical: boolean,
13+
shiftKey: boolean,
1314
) => {
14-
const isJump = action.type === 'DOCUMENT/JUMP_TO_NODE';
15-
const isVerticalStep =
16-
action.type === 'DOCUMENT/NAVIGATE_USING_KEYBOARD' &&
17-
(action.payload.direction === 'up' ||
18-
action.payload.direction === 'down');
19-
20-
if (action.context?.shiftKey && (isJump || isVerticalStep)) {
21-
const columnIndex = findNodeColumn(action.payload.columns, nextNode);
22-
const column = action.payload.columns[columnIndex];
15+
if (shiftKey && isVertical) {
16+
const columnIndex = findNodeColumn(columns, nextNode);
17+
const column = columns[columnIndex];
2318
invariant(column);
2419
updateSelectedNodes(
2520
column,

src/stores/view/reducers/document/jump-to-node.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export type JumpToNodeAction = {
1313
type: 'DOCUMENT/JUMP_TO_NODE';
1414
payload: {
1515
target: JumpTarget;
16-
columns: Column[];
1716
};
1817
context?: {
1918
shiftKey: boolean;
@@ -24,14 +23,21 @@ export const jumpToNode = (
2423
documentViewState: DocumentViewState,
2524
state: Pick<ViewState, 'navigationHistory'>,
2625
action: JumpToNodeAction,
26+
columns: Column[],
2727
) => {
2828
const nextNode = findNextActiveNode(
29-
action.payload.columns,
29+
columns,
3030
documentViewState.activeNode,
3131
action,
3232
);
3333
if (nextNode) {
34-
updateSelectionState(documentViewState, nextNode, action);
34+
updateSelectionState(
35+
columns,
36+
documentViewState,
37+
nextNode,
38+
true,
39+
Boolean(action.context?.shiftKey),
40+
);
3541
updateActiveNode(documentViewState, nextNode, state);
3642
}
3743
};

src/stores/view/reducers/document/navigate-using-keyboard.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type ChangeActiveNodeAction = {
99
type: 'DOCUMENT/NAVIGATE_USING_KEYBOARD';
1010
payload: {
1111
direction: AllDirections;
12-
columns: Column[];
1312
};
1413
context: {
1514
shiftKey?: boolean;
@@ -21,17 +20,25 @@ export const navigateUsingKeyboard = (
2120
documentState: DocumentViewState,
2221
state: Pick<ViewState, 'navigationHistory' | 'outline'>,
2322
action: ChangeActiveNodeAction,
23+
columns: Column[],
2424
) => {
2525
const nextNode = findNextActiveNodeOnKeyboardNavigation(
26-
action.payload.columns,
26+
columns,
2727
documentState.activeNode,
2828
action.payload.direction,
2929
documentState.activeNodesOfColumn,
3030
action.context.outlineMode ? state.outline.collapsedParents : null,
3131
action.context.shiftKey,
3232
);
3333
if (nextNode) {
34-
updateSelectionState(documentState, nextNode, action);
34+
updateSelectionState(
35+
columns,
36+
documentState,
37+
nextNode,
38+
action.payload.direction === 'up' ||
39+
action.payload.direction === 'down',
40+
Boolean(action.context.shiftKey),
41+
);
3542
updateActiveNode(documentState, nextNode, state);
3643
}
3744
};

src/stores/view/reducers/ui/navigate-active-node-history.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { updateNavigationState } from 'src/stores/document/reducers/history/help
33
import { DocumentViewState, ViewState } from 'src/stores/view/view-state-type';
44

55
import { RemoveObsoleteNavigationItemsAction } from 'src/stores/view/reducers/ui/helpers/remove-deleted-navigation-items';
6+
import { resetSelectionState } from 'src/stores/view/reducers/document/helpers/reset-selection-state';
67

78
export type NodeHistoryNavigationAction =
89
| {
@@ -24,5 +25,6 @@ export const navigateActiveNodeHistory = (
2425
state.navigationHistory = { ...state.navigationHistory };
2526
updateActiveNode(documentState, newItem, null);
2627
state.recentNodes.activeNode = newItem;
28+
resetSelectionState(documentState);
2729
}
2830
};

src/stores/view/reducers/ui/navigate-active-node.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { updateActiveNode } from 'src/stores/view/reducers/document/helpers/upda
22
import { DocumentViewState, ViewState } from 'src/stores/view/view-state-type';
33
import { Sections } from 'src/stores/document/document-state-type';
44
import { findNextNode } from 'src/lib/tree-utils/find/find-next-node';
5+
import { resetSelectionState } from 'src/stores/view/reducers/document/helpers/reset-selection-state';
56

67
export type NodeNavigationAction = {
78
type: 'NAVIGATION/SELECT_NEXT_NODE';
@@ -25,6 +26,8 @@ export const navigateActiveNode = (
2526
action.payload.direction,
2627
action.context.outlineMode ? state.outline.hiddenNodes : null,
2728
);
28-
if (nextNode && nextNode !== documentState.activeNode)
29+
if (nextNode && nextNode !== documentState.activeNode) {
2930
updateActiveNode(documentState, nextNode, state);
31+
resetSelectionState(documentState);
32+
}
3033
};

src/stores/view/subscriptions/actions/view/set-initial-active-node.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ export const setInitialActiveNode = (view: LineageView) => {
88
const settings = view.plugin.settings.getValue();
99
const path = view.file!.path;
1010
const persistedSection = settings.documents[path]?.activeSection;
11-
const sections = documentState.sections;
1211
if (persistedSection) {
13-
id = maybeGetIdOfSection(sections, persistedSection);
12+
id = maybeGetIdOfSection(documentState.sections, persistedSection);
1413
}
1514
const mostRecentActiveSection = documentState.history.context.activeSection;
1615
if (!id && mostRecentActiveSection) {
17-
id = maybeGetIdOfSection(sections, mostRecentActiveSection);
16+
id = maybeGetIdOfSection(
17+
documentState.sections,
18+
mostRecentActiveSection,
19+
);
1820
}
1921
if (!id) return;
22+
viewStore.setContext(documentState.document);
2023
viewStore.dispatch({
2124
type: 'view/set-active-node/document',
2225
payload: {

src/stores/view/subscriptions/effects/align-branch/create-align-branch-actions/lazy-vertical-scrolling-mode.spec.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ import { PluginAction } from 'src/stores/view/subscriptions/effects/align-branch
44

55
describe('lazy vertical scrolling', () => {
66
test('should not align parent when moving left', () => {
7-
const n2 = 'nBBzDYZaD';
8-
const n3 = 'nx3_ci_kX';
9-
const n0 = 'rk1LyHnoM';
10-
const c1 = 'c5SOvC488';
7+
// const n2 = 'nBBzDYZaD';
8+
// const n3 = 'nx3_ci_kX';
9+
// const n0 = 'rk1LyHnoM';
10+
// const c1 = 'c5SOvC488';
1111
const c2 = 'c2hxRIVJi';
1212
const c3 = 'cxym9-Tob';
13-
const c4 = 'cdXjrMGq1';
13+
// const c4 = 'cdXjrMGq1';
1414
const n_1 = 'nBefxOMaM';
1515
const n1_1 = 'nixEcLX-z';
1616
const n1_1_1 = 'ncJjgXCQy';
17-
const n1_1_1_1 = 'nPjS2fJCU';
17+
// const n1_1_1_1 = 'nPjS2fJCU';
1818
const context = {
1919
previousActiveBranch: {
2020
childGroups: new Set([n1_1_1]),
@@ -32,33 +32,33 @@ describe('lazy vertical scrolling', () => {
3232
},
3333
};
3434

35+
/* const columns = [
36+
{
37+
id: c1,
38+
groups: [
39+
{
40+
nodes: [n_1, n2, n3],
41+
parentId: n0,
42+
},
43+
],
44+
},
45+
{
46+
id: c2,
47+
groups: [{ nodes: [n1_1], parentId: n_1 }],
48+
},
49+
{
50+
id: c3,
51+
groups: [{ nodes: [n1_1_1], parentId: n1_1 }],
52+
},
53+
{
54+
id: c4,
55+
groups: [{ nodes: [n1_1_1_1], parentId: n1_1_1 }],
56+
},
57+
];*/
3558
const action = {
3659
type: 'DOCUMENT/NAVIGATE_USING_KEYBOARD',
3760
payload: {
3861
direction: 'left',
39-
columns: [
40-
{
41-
id: c1,
42-
groups: [
43-
{
44-
nodes: [n_1, n2, n3],
45-
parentId: n0,
46-
},
47-
],
48-
},
49-
{
50-
id: c2,
51-
groups: [{ nodes: [n1_1], parentId: n_1 }],
52-
},
53-
{
54-
id: c3,
55-
groups: [{ nodes: [n1_1_1], parentId: n1_1 }],
56-
},
57-
{
58-
id: c4,
59-
groups: [{ nodes: [n1_1_1_1], parentId: n1_1_1 }],
60-
},
61-
],
6262
},
6363
context: { outlineMode: false },
6464
} satisfies PluginAction;

src/stores/view/view-reducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const updateDocumentState = (
4242
) {
4343
updateActiveNode(state.document, action.payload.id, state);
4444
} else if (action.type === 'DOCUMENT/NAVIGATE_USING_KEYBOARD') {
45-
navigateUsingKeyboard(state.document, state, action);
45+
navigateUsingKeyboard(state.document, state, action, context.columns);
4646
} else if (action.type === 'SEARCH/SET_QUERY') {
4747
setSearchQuery(state, action.payload.query);
4848
} else if (action.type === 'SEARCH/SET_RESULTS') {
@@ -140,7 +140,7 @@ const updateDocumentState = (
140140
} else if (action.type === 'NAVIGATION/NAVIGATE_BACK') {
141141
navigateActiveNodeHistory(state.document, state);
142142
} else if (action.type === 'DOCUMENT/JUMP_TO_NODE') {
143-
jumpToNode(state.document, state, action);
143+
jumpToNode(state.document, state, action, context.columns);
144144
} else if (action.type === 'NAVIGATION/REMOVE_OBSOLETE') {
145145
removeDeletedNavigationItems(state, action.payload.content);
146146
} else if (action.type === 'SEARCH/TOGGLE_FUZZY_MODE') {

src/view/actions/keyboard-shortcuts/helpers/commands/commands/navigate-commands.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ const spatialNavigation = (view: LineageView, direction: AllDirections) => {
3030
type: 'DOCUMENT/NAVIGATE_USING_KEYBOARD',
3131
payload: {
3232
direction: direction,
33-
columns: view.documentStore.getValue().document.columns,
3433
},
3534
context: {
3635
outlineMode: outlineModeSelector(view),
@@ -61,7 +60,6 @@ const jump = (view: LineageView, target: JumpTarget) => {
6160
type: 'DOCUMENT/JUMP_TO_NODE',
6261
payload: {
6362
target,
64-
columns: view.documentStore.getValue().document.columns,
6563
},
6664
});
6765
};

src/view/actions/keyboard-shortcuts/helpers/commands/commands/selection-commands.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const selectionCommands = () => {
1111
type: 'DOCUMENT/NAVIGATE_USING_KEYBOARD',
1212
payload: {
1313
direction: 'up',
14-
columns: view.documentStore.getValue().document.columns,
1514
},
1615
context: {
1716
shiftKey: true,
@@ -37,7 +36,6 @@ export const selectionCommands = () => {
3736
type: 'DOCUMENT/NAVIGATE_USING_KEYBOARD',
3837
payload: {
3938
direction: 'down',
40-
columns: view.documentStore.getValue().document.columns,
4139
},
4240
context: {
4341
shiftKey: true,
@@ -64,7 +62,6 @@ export const selectionCommands = () => {
6462
type: 'DOCUMENT/JUMP_TO_NODE',
6563
payload: {
6664
target: 'end-of-column',
67-
columns: view.documentStore.getValue().document.columns,
6865
},
6966
context: {
7067
shiftKey: true,
@@ -84,7 +81,6 @@ export const selectionCommands = () => {
8481
type: 'DOCUMENT/JUMP_TO_NODE',
8582
payload: {
8683
target: 'start-of-column',
87-
columns: view.documentStore.getValue().document.columns,
8884
},
8985
context: {
9086
shiftKey: true,
@@ -108,7 +104,6 @@ export const selectionCommands = () => {
108104
type: 'DOCUMENT/JUMP_TO_NODE',
109105
payload: {
110106
target: 'end-of-group',
111-
columns: view.documentStore.getValue().document.columns,
112107
},
113108
context: {
114109
shiftKey: true,
@@ -132,7 +127,6 @@ export const selectionCommands = () => {
132127
type: 'DOCUMENT/JUMP_TO_NODE',
133128
payload: {
134129
target: 'start-of-group',
135-
columns: view.documentStore.getValue().document.columns,
136130
},
137131
context: {
138132
shiftKey: true,

0 commit comments

Comments
 (0)