Open History to see the last commit
1import { ComposerContextId } from "@t3tools/contracts";
2import type {
3 ComposerContextClipboardFragment,
4 ComposerContextRecord,
5 ScopedThreadRef,
6} from "@t3tools/contracts";
7import {
8 COMPOSER_CONTEXT_CLIPBOARD_MIME,
9 decodeComposerContextFragment,
10 decodeComposerContextClipboardHtml,
11} from "@t3tools/shared/composerContextClipboard";
12import {
13 collectComposerContextReferences,
14 formatComposerContextReference,
15 replaceComposerContextReferences,
16 threadLinkContextReferences,
17} from "@t3tools/shared/composerContextReferences";
18
19import { restorePastedMarkdownLinks } from "~/markdown-clipboard";
20import { readThreadShell } from "~/state/entities";
21
22const threadTitle = (ref: ScopedThreadRef) => readThreadShell(ref)?.title;
23
24// Matches ProseMirror's default clipboard parser, which a custom handlePaste bypasses. A leading
25// BOM is not content, and kept it would stop a first-line fence from opening a code block.
26export function readPastedPlainText(clipboardData: Pick<DataTransfer, "getData">): string {
27 return clipboardData
28 .getData("text/plain")
29 .replace(/^\uFEFF/, "")
30 .replace(/\r\n?/g, "\n");
31}
32
33export function readPastedComposerClipboard(
34 clipboardData: Pick<DataTransfer, "getData">,
35 plainTextOnly = false,
36) {
37 const plain = readPastedPlainText(clipboardData);
38 if (plainTextOnly || !plain) return { text: plain, context: null };
39 const html = clipboardData.getData("text/html");
40 const text = restorePastedMarkdownLinks(plain, html);
41 const copied =
42 decodeComposerContextFragment(clipboardData.getData(COMPOSER_CONTEXT_CLIPBOARD_MIME)) ??
43 decodeComposerContextClipboardHtml(html);
44 const threads = threadLinkContextReferences(text, copied?.records ?? [], threadTitle);
45 const source =
46 copied?.source ??
47 (threads.records[0] ? { environmentId: threads.records[0].environmentId } : null);
48 if (source === null) return { text, context: null };
49 const pastedIds = new Set<string>(
50 collectComposerContextReferences(threads.text).map((occurrence) => occurrence.contextId),
51 );
52 const records = new Map<string, ComposerContextRecord>();
53 for (const record of [...(copied?.records ?? []), ...threads.records]) {
54 if (
55 record.kind === "preview-annotation" &&
56 !("payload" in record) &&
57 pastedIds.has(record.contextId) &&
58 record.screenshotContextId
59 ) {
60 pastedIds.add(record.screenshotContextId);
61 }
62 records.set(record.contextId, record);
63 }
64 const context: ComposerContextClipboardFragment = {
65 version: 1,
66 source,
67 records: [...records.values()].filter((record) => pastedIds.has(record.contextId)),
68 };
69 return { text, context };
70}
71
72export type PastedComposerClipboard = ReturnType<typeof readPastedComposerClipboard>;
73
74export function importPastedComposerText(
75 { text: pastedText, context: fragment }: PastedComposerClipboard,
76 importContextFragment?: (
77 fragment: ComposerContextClipboardFragment,
78 ) => ReadonlyMap<string, string>,
79): string {
80 const rewrittenIds =
81 fragment && fragment.records.length > 0 ? importContextFragment?.(fragment) : null;
82 if (!rewrittenIds || rewrittenIds.size === 0) return pastedText;
83 const nextId = (contextId: string) => {
84 const id = rewrittenIds.get(contextId);
85 return id ? ComposerContextId.make(id) : undefined;
86 };
87 const text = replaceComposerContextReferences(pastedText, (occurrence) => {
88 const contextId = nextId(occurrence.contextId);
89 return contextId
90 ? formatComposerContextReference({
91 ...occurrence,
92 contextId,
93 kind: occurrence.kind === "element" ? "preview-annotation" : occurrence.kind,
94 })
95 : occurrence.source;
96 });
97 // A thread the import refused, on a machine this agent cannot read, stays the link it was.
98 return threadLinkContextReferences(text, fragment?.records ?? [], threadTitle, (record) =>
99 nextId(record.contextId),
100 ).text;
101}