Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

selectionCopyComposerContext.ts

34 lines1.4 KB

1import type { ComposerContextClipboardFragment } from "@t3tools/contracts";
2import {
3 formatComposerContextReference,
4 replaceComposerContextReferences,
5} from "@t3tools/shared/composerContextReferences";
6import type { SelectionCopyEvent } from "react-native-enriched-markdown";
7​
8/**
9 * Attaches T3 context metadata when a copied selection includes a context reference backed by
10 * `fragment`, canonicalizing matched link labels to their record labels. Selections without
11 * matching records leave the native clipboard untouched.
12 */
13export function writeCopiedSelectionComposerContext(
14 event: Pick<SelectionCopyEvent, "format" | "text">,
15 fragment: ComposerContextClipboardFragment,
16 attach: (
17 text: string,
18 fragment: ComposerContextClipboardFragment,
19 expectedText: string,
20 ) => Promise<void>,
21): boolean {
22 if (event.format === "imageUrls") return false;
23 const recordById = new Map(fragment.records.map((record) => [record.contextId, record]));
24 let matched = false;
25 const canonical = replaceComposerContextReferences(event.text, (reference) => {
26 const record = recordById.get(reference.contextId);
27 if (!record) return reference.source;
28 matched = true;
29 return formatComposerContextReference({ ...reference, label: record.label });
30 });
31 if (!matched) return false;
32 void attach(canonical, fragment, event.text).catch(() => {});
33 return true;