Skip to content

Commit e69121a

Browse files
committed
enhance(cm): add sort-child-cards action
1 parent 894a0a6 commit e69121a

11 files changed

Lines changed: 605 additions & 3 deletions

File tree

src/lang/lang.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ export const lang = {
286286
modals_rules_rule_cm_move_to_document: 'Move to document rules',
287287
modals_rules_rule_cm_move_to_global: 'Move to global rules',
288288

289+
// sort
290+
cmd_sort_child_cards_asc: 'Sort child cards: ascending order',
291+
cmd_sort_child_cards_desc: 'Sort child cards: descending order',
292+
cm_sort_child: 'Sort child cards',
293+
cm_sort_child_cards_asc: 'Ascending order',
294+
cm_sort_child_cards_desc: 'Descending order',
295+
modals_snapshots_sorted_child_cards: 'Sorted child cards of card ',
296+
289297
// settings
290298
controls_settings: 'Settings',
291299
controls_toggle_bar: 'Toggle controls bar',

src/lang/snapshot-action-lang.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import {
99
Heading1,
1010
Merge,
1111
Scissors,
12+
SortAsc,
13+
SortDesc,
1214
Split,
1315
} from 'lucide-svelte';
1416
import { UndoableAction } from 'src/stores/document/document-store-actions';
1517
import { Snapshot } from 'src/stores/document/document-state-type';
1618
import { customIcons } from 'src/helpers/load-custom-icons';
1719
import { lang } from './lang';
20+
import { SortChildNodesAction } from 'src/stores/document/reducers/sort/sort-direct-child-nodes';
1821

1922
type Key = UndoableAction['type'];
2023

@@ -90,4 +93,13 @@ export const snapshotActionLang: Partial<
9093
lang.modals_snapshots_split_card + snapshot.context.affectedSection,
9194
icon: Split,
9295
}),
96+
'document/sort-direct-child-nodes': (snapshot) => {
97+
const action = snapshot.context.action as SortChildNodesAction;
98+
return {
99+
label:
100+
lang.modals_snapshots_sorted_child_cards +
101+
snapshot.context.affectedSection,
102+
icon: action.payload.order === 'descending' ? SortDesc : SortAsc,
103+
};
104+
},
93105
};

src/obsidian/commands/add-commands.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { extractBranch } from 'src/obsidian/commands/helpers/extract-branch/extr
1414
import { exportColumn } from 'src/view/actions/context-menu/card-context-menu/helpers/export-column';
1515
import { exportDocument } from 'src/obsidian/commands/helpers/export-document/export-document';
1616
import { onPluginError } from 'src/lib/store/on-plugin-error';
17+
import invariant from 'tiny-invariant';
18+
import { sortChildNodes } from 'src/view/actions/context-menu/card-context-menu/helpers/sort-child-nodes';
1719

1820
const createCommands = (plugin: Lineage) => {
1921
const commands: (Omit<Command, 'id' | 'callback'> & {
@@ -80,6 +82,40 @@ const createCommands = (plugin: Lineage) => {
8082
},
8183
});
8284

85+
commands.push({
86+
name: lang.cmd_sort_child_cards_asc,
87+
icon: 'sort-asc',
88+
checkCallback: (checking) => {
89+
const view = getActiveLineageView(plugin);
90+
if (checking) {
91+
return Boolean(view);
92+
}
93+
invariant(view);
94+
sortChildNodes(
95+
view,
96+
view.viewStore.getValue().document.activeNode,
97+
'ascending',
98+
);
99+
},
100+
});
101+
102+
commands.push({
103+
name: lang.cmd_sort_child_cards_desc,
104+
icon: 'sort-desc',
105+
checkCallback: (checking) => {
106+
const view = getActiveLineageView(plugin);
107+
if (checking) {
108+
return Boolean(view);
109+
}
110+
invariant(view);
111+
sortChildNodes(
112+
view,
113+
view.viewStore.getValue().document.activeNode,
114+
'descending',
115+
);
116+
},
117+
});
118+
83119
commands.push({
84120
name: lang.cm_copy_link_to_block,
85121
icon: 'links-coming-in',

src/stores/document/document-reducer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { loadPinnedNodes } from 'src/stores/document/reducers/pinned-nodes/load-
3535
import { refreshGroupParentIds } from 'src/stores/document/reducers/meta/refresh-group-parent-ids';
3636
import { loadDocumentFromJSON } from 'src/stores/document/reducers/load-document-from-file/load-document-from-json';
3737
import { NO_UPDATE } from 'src/lib/store/store';
38+
import { sortDirectChildNodes } from 'src/stores/document/reducers/sort/sort-direct-child-nodes';
3839

3940
const updateDocumentState = (
4041
state: DocumentState,
@@ -91,6 +92,10 @@ const updateDocumentState = (
9192
state.document.content[action.payload.activeNodeId];
9293
newActiveNodeId = mergeNode(state.document, action);
9394
affectedNodeId = action.payload.activeNodeId;
95+
} else if (action.type === 'document/sort-direct-child-nodes') {
96+
sortDirectChildNodes(state.document, action.payload);
97+
newActiveNodeId = action.payload.id;
98+
affectedNodeId = newActiveNodeId;
9499
} else if (action.type === 'DOCUMENT/LOAD_FILE') {
95100
if (action.payload.__test_document__) {
96101
newActiveNodeId = loadDocumentFromJSON(

src/stores/document/document-store-actions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { UnpinNodeAction } from 'src/stores/document/reducers/pinned-nodes/unpin
1717
import { RemoveStalePinnedNodesAction } from 'src/stores/document/reducers/pinned-nodes/remove-stale-pinned-nodes';
1818
import { LoadPinnedNodesAction } from 'src/stores/document/reducers/pinned-nodes/load-pinned-nodes';
1919
import { RefreshGroupParentIdsAction } from 'src/stores/document/reducers/meta/refresh-group-parent-ids';
20+
import { SortChildNodesAction } from 'src/stores/document/reducers/sort/sort-direct-child-nodes';
2021

2122
export type VerticalDirection = 'up' | 'down';
2223
export type Direction = VerticalDirection | 'right';
@@ -57,7 +58,8 @@ export type DocumentAction =
5758
};
5859
}
5960
| PinnedNodesActions
60-
| MetaActions;
61+
| MetaActions
62+
| SortChildNodesAction;
6163

6264
export type HistoryAction = UndoRedoAction | SelectSnapshotAction;
6365
export type UndoableAction =
@@ -72,7 +74,8 @@ export type UndoableAction =
7274
| PasteNodeAction
7375
| CutNodeAction
7476
| ExtractNodeAction
75-
| SplitNodeAction;
77+
| SplitNodeAction
78+
| SortChildNodesAction;
7679

7780
export type CopyNodeAction = {
7881
type: 'DOCUMENT/COPY_NODE';

0 commit comments

Comments
 (0)