Skip to content

Commit df4024d

Browse files
committed
enhance(card-cm): replace export column with export selection
1 parent ffc10ae commit df4024d

10 files changed

Lines changed: 118 additions & 82 deletions

File tree

src/lang/lang.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ export const lang = {
140140

141141
// export
142142
cm_export_document: 'Export document',
143-
cm_export_column: 'Export column',
143+
cm_export_selection: 'Export selection',
144+
cm_export_with_subitems: 'With sub-items',
145+
cm_export_wo_subitems: 'Without sub-items',
146+
cmd_export_selection_with_subitems: 'Export selection with sub-items',
147+
cmd_export_selection_wo_subitems: 'Export selection without sub-items',
144148

145149
// document format
146150
settings_general_default_format: 'Default format',

src/obsidian/commands/add-commands.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { openSplitNodeModal } from 'src/view/modals/split-node-modal/open-split-
1111
import { isEditing } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/is-editing';
1212
import { copyLinkToBlock } from 'src/view/actions/context-menu/card-context-menu/helpers/copy-link-to-block';
1313
import { extractBranch } from 'src/obsidian/commands/helpers/extract-branch/extract-branch';
14-
import { exportColumn } from 'src/view/actions/context-menu/card-context-menu/helpers/export-column';
14+
import { exportSelection } from 'src/view/actions/context-menu/card-context-menu/helpers/export-selection';
1515
import { exportDocument } from 'src/obsidian/commands/helpers/export-document/export-document';
1616
import { onPluginError } from 'src/lib/store/on-plugin-error';
1717
import invariant from 'tiny-invariant';
@@ -165,14 +165,26 @@ const createCommands = (plugin: Lineage) => {
165165
});
166166

167167
commands.push({
168-
name: lang.cm_export_column,
168+
name: lang.cmd_export_selection_with_subitems,
169169
icon: 'file-text',
170170
checkCallback: (checking) => {
171171
const view = getActiveLineageView(plugin);
172172
if (checking) {
173173
return Boolean(view);
174174
}
175-
exportColumn(view!);
175+
exportSelection(view!, true);
176+
},
177+
});
178+
179+
commands.push({
180+
name: lang.cmd_export_selection_wo_subitems,
181+
icon: 'file-text',
182+
checkCallback: (checking) => {
183+
const view = getActiveLineageView(plugin);
184+
if (checking) {
185+
return Boolean(view);
186+
}
187+
exportSelection(view!, false);
176188
},
177189
});
178190

src/view/actions/context-menu/card-context-menu/helpers/export-column.ts

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { LineageView } from 'src/view/view';
2+
import { createNewFile } from 'src/obsidian/events/workspace/effects/create-new-file';
3+
import { openFile } from 'src/obsidian/events/workspace/effects/open-file';
4+
import { saveNodeContent } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/save-node-content';
5+
import { getTextOfFlatNodes } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/get-text-of-flat-nodes';
6+
import { getActiveNodes } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/get-active-nodes';
7+
import { mapBranchesToText } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/map-branches-to-text';
8+
9+
export const exportSelection = async (
10+
view: LineageView,
11+
includeSubItems: boolean,
12+
) => {
13+
const viewState = view.viewStore.getValue();
14+
15+
const isEditing = Boolean(viewState.document.editing.activeNodeId);
16+
if (isEditing) {
17+
saveNodeContent(view);
18+
setTimeout(() => {
19+
exportSelection(view, includeSubItems);
20+
}, 100);
21+
return;
22+
}
23+
24+
let text = '';
25+
26+
const nodes = getActiveNodes(view, false);
27+
if (includeSubItems) {
28+
text = mapBranchesToText(
29+
view.documentStore.getValue().document,
30+
nodes,
31+
'unformatted-text',
32+
);
33+
} else {
34+
text = getTextOfFlatNodes(view, nodes, false);
35+
}
36+
37+
const file = view.file!;
38+
const newFile = await createNewFile(
39+
view.plugin,
40+
file.parent!,
41+
text,
42+
`${file.basename} - exported selection`,
43+
);
44+
await openFile(view.plugin, newFile, 'split');
45+
};

src/view/actions/context-menu/card-context-menu/show-card-context-menu.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { pasteNode } from 'src/view/actions/keyboard-shortcuts/helpers/commands/
88
import { openSplitNodeModal } from 'src/view/modals/split-node-modal/open-split-node-modal';
99
import { customIcons } from 'src/helpers/load-custom-icons';
1010
import { copyLinkToBlock } from 'src/view/actions/context-menu/card-context-menu/helpers/copy-link-to-block';
11-
import { exportColumn } from 'src/view/actions/context-menu/card-context-menu/helpers/export-column';
11+
import { exportSelection } from 'src/view/actions/context-menu/card-context-menu/helpers/export-selection';
1212
import {
1313
MenuItemObject,
1414
renderContextMenu,
@@ -181,10 +181,21 @@ export const showCardContextMenu = (event: MouseEvent, view: LineageView) => {
181181
disabled: multipleNodesAreSelected || isInSidebar,
182182
},
183183
{
184-
title: lang.cm_export_column,
184+
title: lang.cm_export_selection,
185185
icon: 'file-text',
186-
action: () => exportColumn(view),
187-
disabled: multipleNodesAreSelected || isInSidebar,
186+
disabled: isInSidebar,
187+
submenu: [
188+
{
189+
title: lang.cm_export_with_subitems,
190+
icon: 'file-text',
191+
action: () => exportSelection(view, true),
192+
},
193+
{
194+
title: lang.cm_export_wo_subitems,
195+
icon: 'file-text',
196+
action: () => exportSelection(view, false),
197+
},
198+
],
188199
},
189200
];
190201

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { LineageView } from 'src/view/view';
22
import { getActiveNodes } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/get-active-nodes';
3-
import { copyNodesToClipboard } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/copy-nodes-to-clipboard';
3+
import { copyFlatNodesToClipboard } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/copy-flat-nodes-to-clipboard';
44

55
export const copyActiveNodesToClipboard = async (
66
view: LineageView,
77
isInSidebar: boolean,
88
) => {
99
const nodes = getActiveNodes(view, isInSidebar);
10-
await copyNodesToClipboard(view, nodes);
10+
await copyFlatNodesToClipboard(view, nodes);
1111
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { LineageView } from 'src/view/view';
2+
import { getTextOfFlatNodes } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/get-text-of-flat-nodes';
3+
4+
export const copyFlatNodesToClipboard = async (
5+
view: LineageView,
6+
nodes: string[],
7+
copyAsOutline = false,
8+
) => {
9+
const text = getTextOfFlatNodes(view, nodes, copyAsOutline);
10+
await navigator.clipboard.writeText(text);
11+
};
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { LineageView } from 'src/view/view';
2-
import { sortNodeIdsBySectionNumber } from 'src/lib/tree-utils/sort/sort-node-ids-by-section-number';
3-
import { copyNodesToClipboard } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/copy-nodes-to-clipboard';
2+
import { copyFlatNodesToClipboard } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/copy-flat-nodes-to-clipboard';
43

54
export const copyFlatSearchResultsToClipboard = async (view: LineageView) => {
65
const results = Array.from(view.viewStore.getValue().search.results.keys());
7-
const sortedResults = sortNodeIdsBySectionNumber(
8-
view.documentStore.getValue().sections,
9-
results,
10-
);
11-
await copyNodesToClipboard(view, sortedResults, true);
6+
7+
await copyFlatNodesToClipboard(view, results, true);
128
};

src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/copy-nodes-to-clipboard.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { LineageView } from 'src/view/view';
2+
import { sortNodeIdsBySectionNumber } from 'src/lib/tree-utils/sort/sort-node-ids-by-section-number';
3+
4+
export const getTextOfFlatNodes = (
5+
view: LineageView,
6+
nodes: string[],
7+
copyAsOutline = false,
8+
) => {
9+
if (nodes.length === 1) copyAsOutline = false;
10+
const documentState = view.documentStore.getValue();
11+
const documentContent = documentState.document.content;
12+
const sortedNodes = sortNodeIdsBySectionNumber(
13+
documentState.sections,
14+
nodes,
15+
);
16+
return sortedNodes
17+
.map((id) => {
18+
const content = documentContent[id].content;
19+
return (copyAsOutline ? '- ' : '') + content;
20+
})
21+
.join(copyAsOutline ? '\n' : '\n\n');
22+
};

0 commit comments

Comments
 (0)