Handle OS file association: open .md files passed as CLI args

This commit is contained in:
Your Name
2026-02-14 19:45:58 +02:00
parent a3b241b4a1
commit f462ca8d03
5 changed files with 51 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ import { open } from "@tauri-apps/plugin-dialog";
import { readTextFile } from "@tauri-apps/plugin-fs";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { getCurrentWebview } from "@tauri-apps/api/webview";
import { listen } from "@tauri-apps/api/event";
import MarkdownIt from "markdown-it";
import hljs from "highlight.js";
import { motion, AnimatePresence } from "framer-motion";
@@ -512,6 +513,31 @@ function App() {
return () => { unlisten?.(); };
}, [parseHeadings]);
// Handle file opened via OS file association (e.g. double-clicking a .md file)
useEffect(() => {
let unlisten: (() => void) | undefined;
listen<string>('open-file', async (event) => {
const filePath = event.payload;
try {
const existing = tabsRef.current.find(t => t.path === filePath);
if (existing) {
setActiveTabId(existing.id);
parseHeadings(existing.content);
} else {
const content = await readTextFile(filePath);
const title = filePath.split(/[/\\]/).pop() || 'Untitled';
const newTab: Tab = { id: Date.now().toString(), title, content, path: filePath };
setTabs(prev => [...prev, newTab]);
setActiveTabId(newTab.id);
parseHeadings(content);
}
} catch (err) {
console.error('Failed to open file from OS:', err);
}
}).then(fn => { unlisten = fn; });
return () => { unlisten?.(); };
}, [parseHeadings]);
const handleOpenDialog = useCallback(async () => {
try {
const selected = await open({ multiple: false, filters: [{ name: 'Markdown', extensions: ['md', 'markdown', 'txt'] }] });