import { convertFileSrc, isTauri } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { writeImage, writeText } from "@tauri-apps/plugin-clipboard-manager"; import { save } from "@tauri-apps/plugin-dialog"; import { Check, CircleAlert, Clipboard, Copy, ExternalLink, FileText, FolderOpen, LoaderCircle, Pin, ScanText, X, } from "lucide-react"; import { useEffect, useRef, useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; import { recognizeImage } from "../lib/ocr"; import { FEEDBACK } from "../lib/feedback"; import { basename, dirname, extensionOf, IS_MAC, messageFrom } from "../lib/utils"; import { history } from "../lib/history"; import type { EditorInfo } from "../types"; import { ImageAnnotator } from "./ImageAnnotator"; export function Editor() { const { t } = useI18n(); const [session, setSession] = useState(null); const [ocrOpen, setOcrOpen] = useState(false); const [ocrText, setOcrText] = useState(""); const [ocrProgress, setOcrProgress] = useState(0); const [ocrStatus, setOcrStatus] = useState(""); const [ocrRunning, setOcrRunning] = useState(false); const [ocrFailed, setOcrFailed] = useState(false); const [pinBusy, setPinBusy] = useState(false); const [copied, setCopied] = useState(null); const [copiedIsError, setCopiedIsError] = useState(false); const [textContent, setTextContent] = useState(""); const [textError, setTextError] = useState(null); const copyTimer = useRef(null); const sessionRef = useRef(null); function applySession(next: EditorInfo | null) { if (!next) return; sessionRef.current = next; setSession(next); setOcrOpen(false); setOcrText(""); setOcrProgress(0); setOcrStatus(""); setTextContent(""); setTextError(null); // Anything opened for editing belongs in the library. Rust assigns the kind // and timestamp, so the editor does not classify the file a second time. void history.record([next.path]).catch(() => undefined); } useEffect(() => { let disposed = false; const unlisteners: Array<() => void> = []; if (!isTauri()) { applySession({ path: "/demo-preview.svg", kind: "image" }); return; } void api.getEditorSession().then((next) => { if (!disposed) applySession(next); }); void listen("editor://open", (event) => applySession(event.payload)).then( (unlisten) => unlisteners.push(unlisten), ); void listen("editor://run-ocr", () => void runOcr()).then((unlisten) => unlisteners.push(unlisten), ); return () => { disposed = true; unlisteners.forEach((unlisten) => unlisten()); if (copyTimer.current) window.clearTimeout(copyTimer.current); }; }, []); useEffect(() => { if (session?.kind !== "text") return; let cancelled = false; void api .readTextFile(session.path) .then((text) => { if (!cancelled) setTextContent(text); }) .catch((error) => { if (!cancelled) setTextError(messageFrom(error)); }); return () => { cancelled = true; }; }, [session?.kind, session?.path]); /** The window's one transient notice. A failure must not arrive wearing a tick. */ function flashNotice(label: string, failed = false) { if (copyTimer.current) window.clearTimeout(copyTimer.current); setCopied(label); setCopiedIsError(failed); copyTimer.current = window.setTimeout(() => setCopied(null), FEEDBACK.done); } async function copy() { if (!session) return; // A clipboard the OS refuses is a result the user has to be told about; it // used to fail in silence here while every other window reported it. try { if (session.kind === "image") await writeImage(session.path); else if (session.kind === "text") await writeText(textContent); else await writeText(session.path); flashNotice(t("copy")); } catch (error) { flashNotice(t(messageFrom(error)), true); } } async function saveAs() { if (!session) return; const ext = extensionOf(session.path); const target = await save({ defaultPath: `${dirname(session.path)}/${basename(session.path)}`, filters: ext ? [{ name: ext.toUpperCase(), extensions: [ext] }] : undefined, }); if (!target) return; try { await api.copyFileTo(session.path, target); flashNotice(t("saved")); } catch (error) { flashNotice(t(messageFrom(error)), true); } } async function runOcr() { const current = sessionRef.current; if (!current || ocrRunning) return; setOcrOpen(true); setOcrRunning(true); setOcrText(""); setOcrProgress(0); setOcrFailed(false); setOcrStatus(t("ocrRunning")); try { const source = isTauri() ? convertFileSrc(current.path) : current.path; const result = await recognizeImage(source, (progress, status) => { setOcrProgress(progress); setOcrStatus(status || t("ocrRunning")); }); setOcrText(result.text || t("ocrEmpty")); if (result.text) { await writeText(result.text); flashNotice(t("copyText")); } setOcrProgress(1); setOcrStatus(""); } catch (error) { // Nothing was read, so the panel says only that and stays out of the way; // the reason belongs on the notice, which is where this window reports. setOcrFailed(true); flashNotice(t(messageFrom(error)), true); setOcrStatus(""); } finally { setOcrRunning(false); } } async function pinToScreen() { const current = sessionRef.current; if (!current || pinBusy) return; setPinBusy(true); try { await api.pinImage(current.path); } catch (error) { flashNotice(t(messageFrom(error)), true); } finally { setPinBusy(false); } } if (!session) { return
; } const url = isTauri() ? convertFileSrc(session.path) : session.path; const name = basename(session.path); return (
Shift Your Files
{name}
{session.kind === "image" ? ( ) : null} {session.kind === "image" ? ( ) : null}
{dirname(session.path)}
{session.kind === "image" ? ( { applySession({ path: nextPath, kind: "image" }); flashNotice(t("saved")); }} /> ) : session.kind === "video" ? (
{ocrOpen ? (