forked from r.muellner/velum-raycast
Preserve rich formatting when restoring selected text
This commit is contained in:
@@ -11,11 +11,18 @@ import {
|
||||
} from "@raycast/api";
|
||||
import { useEffect, useState } from "react";
|
||||
import { getSelectedTextSafely } from "./selection";
|
||||
import { canonicalProjectPath, listAllRuns, listRuns, restoreTextWithCount } from "./lib/velum-project";
|
||||
import {
|
||||
canonicalProjectPath,
|
||||
listAllRuns,
|
||||
listRuns,
|
||||
restoreTextWithCount,
|
||||
} from "./lib/velum-project";
|
||||
import { depseudonymize } from "./velum";
|
||||
import { getActiveSessionId, getSession, listSessions } from "./sessions";
|
||||
import type { DepseudonymizeResponse, VelumSession } from "./types";
|
||||
import { markdownCodeBlock, sessionSubtitle } from "./ui";
|
||||
import { restoreRichHtml } from "./lib/rich-text";
|
||||
import { copyRichText } from "./rich-clipboard";
|
||||
|
||||
type FormValues = {
|
||||
text: string;
|
||||
@@ -23,26 +30,56 @@ type FormValues = {
|
||||
};
|
||||
|
||||
const CHOOSE_TARGET = "choose-target";
|
||||
type RichSource = { text: string; html: string };
|
||||
|
||||
export default function Command(props: { initialSource?: "selection" | "clipboard" } = {}) {
|
||||
export default function Command(
|
||||
props: { initialSource?: "selection" | "clipboard" } = {},
|
||||
) {
|
||||
const [text, setText] = useState("");
|
||||
const [richSource, setRichSource] = useState<RichSource>();
|
||||
const [sessions, setSessions] = useState<VelumSession[]>([]);
|
||||
const [activeSessionId, setActiveSessionId] = useState<string>();
|
||||
const [projects, setProjects] = useState<Array<{ path: string; name: string }>>([]);
|
||||
const [projects, setProjects] = useState<
|
||||
Array<{ path: string; name: string }>
|
||||
>([]);
|
||||
const [targetId, setTargetId] = useState(CHOOSE_TARGET);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const { push } = useNavigation();
|
||||
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
const initialText = props.initialSource === "selection" ? getSelectedTextSafely()
|
||||
: props.initialSource === "clipboard" ? Clipboard.readText() : Promise.resolve(undefined);
|
||||
const [loadedSessions, activeId, savedRuns, selected] = await Promise.all([
|
||||
listSessions(),
|
||||
getActiveSessionId(),
|
||||
listAllRuns(),
|
||||
initialText,
|
||||
]);
|
||||
const initialText = (async (): Promise<{
|
||||
text?: string | null;
|
||||
rich?: RichSource;
|
||||
}> => {
|
||||
if (props.initialSource === "selection") {
|
||||
const selected = await getSelectedTextSafely();
|
||||
const clipboard = selected
|
||||
? await Clipboard.read().catch(() => undefined)
|
||||
: undefined;
|
||||
return {
|
||||
text: selected,
|
||||
rich:
|
||||
clipboard?.html && clipboard.text === selected
|
||||
? { text: selected, html: clipboard.html }
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
if (props.initialSource === "clipboard") {
|
||||
const clipboard = await Clipboard.read();
|
||||
return {
|
||||
text: clipboard.text,
|
||||
rich:
|
||||
clipboard.html && clipboard.text
|
||||
? { text: clipboard.text, html: clipboard.html }
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
return {};
|
||||
})();
|
||||
const [loadedSessions, activeId, savedRuns, selected] = await Promise.all(
|
||||
[listSessions(), getActiveSessionId(), listAllRuns(), initialText],
|
||||
);
|
||||
setSessions(loadedSessions);
|
||||
setActiveSessionId(activeId);
|
||||
const unique = new Map<string, string>();
|
||||
@@ -51,19 +88,32 @@ export default function Command(props: { initialSource?: "selection" | "clipboar
|
||||
if (!unique.has(path)) unique.set(path, run.projectName);
|
||||
}
|
||||
setProjects([...unique].map(([path, name]) => ({ path, name })));
|
||||
if (selected) setText(selected);
|
||||
else if (props.initialSource) await showToast({ style: Toast.Style.Failure,
|
||||
title: props.initialSource === "selection" ? "Kein markierter Text gefunden" : "Zwischenablage enthält keinen Text" });
|
||||
if (selected.text) {
|
||||
setText(selected.text);
|
||||
setRichSource(selected.rich);
|
||||
} else if (props.initialSource)
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title:
|
||||
props.initialSource === "selection"
|
||||
? "Kein markierter Text gefunden"
|
||||
: "Zwischenablage enthält keinen Text",
|
||||
});
|
||||
setIsLoading(false);
|
||||
}
|
||||
load().catch(async (error: Error) => {
|
||||
setIsLoading(false);
|
||||
await showToast({ style: Toast.Style.Failure, title: "Zuordnungen nicht lesbar", message: error.message });
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Zuordnungen nicht lesbar",
|
||||
message: error.message,
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
|
||||
async function loadClipboardText() {
|
||||
const clipboardText = await Clipboard.readText();
|
||||
const clipboard = await Clipboard.read();
|
||||
const clipboardText = clipboard.text;
|
||||
if (!clipboardText) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
@@ -72,11 +122,20 @@ export default function Command(props: { initialSource?: "selection" | "clipboar
|
||||
return;
|
||||
}
|
||||
setText(clipboardText);
|
||||
setRichSource(
|
||||
clipboard.html
|
||||
? { text: clipboardText, html: clipboard.html }
|
||||
: undefined,
|
||||
);
|
||||
}
|
||||
|
||||
async function handleSubmit(values: FormValues) {
|
||||
const input = values.text.trim();
|
||||
if (!input || !values.sessionId || values.sessionId === CHOOSE_TARGET) {
|
||||
const input = values.text;
|
||||
if (
|
||||
!input.trim() ||
|
||||
!values.sessionId ||
|
||||
values.sessionId === CHOOSE_TARGET
|
||||
) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Projekt oder Sitzung wählen und Text eingeben",
|
||||
@@ -90,27 +149,62 @@ export default function Command(props: { initialSource?: "selection" | "clipboar
|
||||
});
|
||||
try {
|
||||
let result: DepseudonymizeResponse;
|
||||
let richHtml: string | undefined;
|
||||
let targetName: string;
|
||||
let targetSubtitle: string;
|
||||
if (values.sessionId.startsWith("project:")) {
|
||||
const projectPath = values.sessionId.slice("project:".length);
|
||||
const saved = (await listRuns(projectPath))[0];
|
||||
if (!saved) throw new Error("Projektstand nicht gefunden.");
|
||||
const restored = restoreTextWithCount(input, saved.run.mapping, saved.run.matchModes);
|
||||
result = { original_text: restored.text, replacements_made: restored.replacements };
|
||||
const restored = restoreTextWithCount(
|
||||
input,
|
||||
saved.run.mapping,
|
||||
saved.run.matchModes,
|
||||
);
|
||||
result = {
|
||||
original_text: restored.text,
|
||||
replacements_made: restored.replacements,
|
||||
};
|
||||
if (richSource?.text === input) {
|
||||
richHtml = restoreRichHtml(
|
||||
richSource.html,
|
||||
input,
|
||||
result.original_text,
|
||||
saved.run.mapping,
|
||||
saved.run.matchModes,
|
||||
);
|
||||
}
|
||||
targetName = `Projekt ${saved.run.projectName}`;
|
||||
targetSubtitle = saved.run.projectPath;
|
||||
} else {
|
||||
const session = await getSession(values.sessionId);
|
||||
if (!session) throw new Error("Sitzung nicht gefunden.");
|
||||
result = await depseudonymize(input, session.mapping);
|
||||
if (richSource?.text === input) {
|
||||
richHtml = restoreRichHtml(
|
||||
richSource.html,
|
||||
input,
|
||||
result.original_text,
|
||||
session.mapping,
|
||||
undefined,
|
||||
true,
|
||||
);
|
||||
}
|
||||
targetName = session.name;
|
||||
targetSubtitle = sessionSubtitle(session);
|
||||
}
|
||||
toast.style = Toast.Style.Success;
|
||||
toast.title = "Wiederhergestellt";
|
||||
toast.message = `${result.replacements_made} Ersetzungen`;
|
||||
push(<DepseudonymizeResult result={result} targetName={targetName} targetSubtitle={targetSubtitle} />);
|
||||
push(
|
||||
<DepseudonymizeResult
|
||||
result={result}
|
||||
targetName={targetName}
|
||||
targetSubtitle={targetSubtitle}
|
||||
richHtml={richHtml}
|
||||
showFormatStatus={Boolean(props.initialSource || richSource)}
|
||||
/>,
|
||||
);
|
||||
} catch (error) {
|
||||
toast.style = Toast.Style.Failure;
|
||||
toast.title = "Wiederherstellung fehlgeschlagen";
|
||||
@@ -149,10 +243,18 @@ export default function Command(props: { initialSource?: "selection" | "clipboar
|
||||
value={targetId}
|
||||
onChange={setTargetId}
|
||||
>
|
||||
<Form.Dropdown.Item value={CHOOSE_TARGET} title="Bitte Projekt oder Sitzung wählen" icon={Icon.Circle} />
|
||||
<Form.Dropdown.Item
|
||||
value={CHOOSE_TARGET}
|
||||
title="Bitte Projekt oder Sitzung wählen"
|
||||
icon={Icon.Circle}
|
||||
/>
|
||||
{projects.map((project) => (
|
||||
<Form.Dropdown.Item key={project.path} value={`project:${project.path}`}
|
||||
title={`Projekt: ${project.name} · ${project.path}`} icon={Icon.Folder} />
|
||||
<Form.Dropdown.Item
|
||||
key={project.path}
|
||||
value={`project:${project.path}`}
|
||||
title={`Projekt: ${project.name} · ${project.path}`}
|
||||
icon={Icon.Folder}
|
||||
/>
|
||||
))}
|
||||
{sessions.map((session) => (
|
||||
<Form.Dropdown.Item
|
||||
@@ -173,10 +275,14 @@ function DepseudonymizeResult({
|
||||
result,
|
||||
targetName,
|
||||
targetSubtitle,
|
||||
richHtml,
|
||||
showFormatStatus,
|
||||
}: {
|
||||
result: DepseudonymizeResponse;
|
||||
targetName: string;
|
||||
targetSubtitle: string;
|
||||
richHtml?: string;
|
||||
showFormatStatus: boolean;
|
||||
}) {
|
||||
const markdown = [
|
||||
`# ${targetName}`,
|
||||
@@ -186,6 +292,11 @@ function DepseudonymizeResult({
|
||||
markdownCodeBlock(result.original_text),
|
||||
"",
|
||||
`Ersetzungen: ${result.replacements_made}`,
|
||||
richHtml
|
||||
? "Formatierung kann beim Kopieren oder Einfügen übernommen werden."
|
||||
: showFormatStatus
|
||||
? "Formatierung war nicht verfügbar oder konnte nicht sicher zugeordnet werden; Klartext kann weiterhin kopiert werden."
|
||||
: "",
|
||||
].join("\n");
|
||||
|
||||
return (
|
||||
@@ -193,12 +304,46 @@ function DepseudonymizeResult({
|
||||
markdown={markdown}
|
||||
actions={
|
||||
<ActionPanel>
|
||||
{richHtml && (
|
||||
<>
|
||||
<Action
|
||||
title="Mit Formatierung kopieren"
|
||||
icon={Icon.Clipboard}
|
||||
onAction={async () => {
|
||||
try {
|
||||
await copyRichText(richHtml, result.original_text);
|
||||
await showToast({
|
||||
style: Toast.Style.Success,
|
||||
title: "Mit Formatierung kopiert",
|
||||
});
|
||||
} catch (error) {
|
||||
await showToast({
|
||||
style: Toast.Style.Failure,
|
||||
title: "Formatierter Text konnte nicht kopiert werden",
|
||||
message:
|
||||
error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Action
|
||||
title="Mit Formatierung einfügen"
|
||||
icon={Icon.TextCursor}
|
||||
onAction={() =>
|
||||
Clipboard.paste({
|
||||
html: richHtml,
|
||||
text: result.original_text,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<Action.CopyToClipboard
|
||||
title="Wiederhergestellten Text kopieren"
|
||||
title="Als Klartext kopieren"
|
||||
content={result.original_text}
|
||||
/>
|
||||
<Action
|
||||
title="Wiederhergestellten Text einfügen"
|
||||
title="Als Klartext einfügen"
|
||||
icon={Icon.TextCursor}
|
||||
onAction={() => Clipboard.paste(result.original_text)}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user