fix: retain rich formatting when restoring selected Teams text

This commit is contained in:
muena
2026-10-21 12:02:45 +02:00
parent 41b935a145
commit 5200fcde70
2 changed files with 78 additions and 22 deletions

View File

@@ -22,7 +22,7 @@ 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";
import { copyRichText, pasteRichText, readRichClipboardHtml } from "./rich-clipboard";
import { mappingForRestoration } from "./lib/mapping-groups";
type FormValues = {
@@ -55,25 +55,25 @@ export default function Command(
}> => {
if (props.initialSource === "selection") {
const selected = await getSelectedTextSafely();
const clipboard = selected
? await Clipboard.read().catch(() => undefined)
: undefined;
let html: string | undefined;
if (selected) {
for (const delay of [0, 150, 300]) {
if (delay) await new Promise((resolve) => setTimeout(resolve, delay));
html = await readRichClipboardHtml();
if (html) break;
}
}
return {
text: selected,
rich:
clipboard?.html && clipboard.text === selected
? { text: selected, html: clipboard.html }
: undefined,
rich: selected && html ? { text: selected, html } : undefined,
};
}
if (props.initialSource === "clipboard") {
const clipboard = await Clipboard.read();
const html = clipboard.html ?? await readRichClipboardHtml();
return {
text: clipboard.text,
rich:
clipboard.html && clipboard.text
? { text: clipboard.text, html: clipboard.html }
: undefined,
rich: html && clipboard.text ? { text: clipboard.text, html } : undefined,
};
}
return {};
@@ -123,9 +123,10 @@ export default function Command(
return;
}
setText(clipboardText);
const html = clipboard.html ?? await readRichClipboardHtml();
setRichSource(
clipboard.html
? { text: clipboardText, html: clipboard.html }
html
? { text: clipboardText, html }
: undefined,
);
}
@@ -206,6 +207,9 @@ export default function Command(
targetSubtitle={targetSubtitle}
richHtml={richHtml}
showFormatStatus={Boolean(props.initialSource || richSource)}
formatUnavailableReason={!richHtml && richSource?.text === input
? "Die Formatierung der Auswahl konnte nicht eindeutig mit dem wiederhergestellten Text abgeglichen werden."
: "Die Quellanwendung hat für diese Auswahl keine HTML- oder RTF-Formatierung bereitgestellt."}
/>,
);
} catch (error) {
@@ -280,12 +284,14 @@ function DepseudonymizeResult({
targetSubtitle,
richHtml,
showFormatStatus,
formatUnavailableReason,
}: {
result: DepseudonymizeResponse;
targetName: string;
targetSubtitle: string;
richHtml?: string;
showFormatStatus: boolean;
formatUnavailableReason: string;
}) {
const markdown = [
`# ${targetName}`,
@@ -298,7 +304,7 @@ function DepseudonymizeResult({
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."
? `${formatUnavailableReason} Klartext kann weiterhin kopiert werden.`
: "",
].join("\n");
@@ -332,12 +338,17 @@ function DepseudonymizeResult({
<Action
title="Mit Formatierung einfügen"
icon={Icon.TextCursor}
onAction={() =>
Clipboard.paste({
html: richHtml,
text: result.original_text,
})
}
onAction={async () => {
try {
await pasteRichText(richHtml, result.original_text);
} catch (error) {
await showToast({
style: Toast.Style.Failure,
title: "Formatierter Text konnte nicht eingefügt werden",
message: `${error instanceof Error ? error.message : String(error)} · Der Text liegt formatiert in der Zwischenablage.`,
});
}
}}
/>
</>
)}