Skip to content

Commit 1adf39c

Browse files
committed
feat(clipboard): add copy as AI context feature
- add menu item in Obsidian context menu to copy content as AI context - add VS Code command `mindctx.copyAIContext` for editor title and explorer - implement logic to parse file content and generate AI context text - write generated text to system clipboard and show success notice
1 parent b453ac3 commit 1adf39c

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

packages/obsidian/src/main.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ export default class MindCtxPlugin extends Plugin {
210210
}
211211

212212
if (file instanceof TFile && this.isMindCtxFile(file)) {
213+
menu.addItem((item) => {
214+
item.setTitle('复制为 AI 上下文')
215+
.setIcon('sparkles')
216+
.onClick(() => {
217+
void this.app.vault.read(file).then((content) => {
218+
const tree = parse(content, { filePath: file.path });
219+
const text = copyAsAIContext(tree);
220+
void navigator.clipboard.writeText(text);
221+
new Notice('已复制到剪贴板');
222+
});
223+
});
224+
});
213225
menu.addItem((item) => {
214226
item.setTitle('导出为 opml')
215227
.setIcon('download')

packages/vscode/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,23 @@
112112
"group": "navigation"
113113
}
114114
],
115+
"editor/title/context": [
116+
{
117+
"command": "mindctx.copyAIContext",
118+
"when": "activeCustomEditorId == 'mindctx.editor'",
119+
"group": "navigation"
120+
}
121+
],
115122
"explorer/context": [
116123
{
117124
"command": "mindctx.openAs",
118125
"when": "resourceExtname == .md",
119126
"group": "navigation"
127+
},
128+
{
129+
"command": "mindctx.copyAIContext",
130+
"when": "resourceExtname == .md",
131+
"group": "2_workspace"
120132
}
121133
]
122134
},

packages/vscode/src/extension.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import { MindCtxEditorProvider } from './MindCtxEditorProvider.js';
3-
import { exportOPML, exportJSON, copyAsAIContext } from '@mindctx/core';
3+
import { parse, exportOPML, exportJSON, copyAsAIContext } from '@mindctx/core';
44

55
let provider: MindCtxEditorProvider;
66

@@ -116,7 +116,17 @@ async function exportPngFromEditor(): Promise<void> {
116116
provider.sendCommandToActivePanel(doc, 'export.png');
117117
}
118118

119-
async function copyAIContextFromEditor(): Promise<void> {
119+
async function copyAIContextFromEditor(uri?: vscode.Uri): Promise<void> {
120+
if (uri) {
121+
const data = await vscode.workspace.fs.readFile(uri);
122+
const content = new TextDecoder().decode(data);
123+
const tree = parse(content, { filePath: uri.fsPath });
124+
const text = copyAsAIContext(tree);
125+
await vscode.env.clipboard.writeText(text);
126+
vscode.window.showInformationMessage('AI context copied to clipboard.');
127+
return;
128+
}
129+
120130
const doc = provider.getActiveDocument();
121131
if (!doc) {
122132
vscode.window.showWarningMessage('No active MindCtx editor.');

0 commit comments

Comments
 (0)