Open History to see the last commit
1import { formatFilePathForClipboard } from "@t3tools/client-runtime/media-actions";
2import { fileBasename } from "@t3tools/shared/path";
3import type { ThreadId } from "@t3tools/contracts";
4import { resolveMarkdownLinkPresentation } from "../../lib/markdownLinks";
5import type { MarkdownFileContextMenu } from "../../native/SelectableMarkdownText.types";
6import { hostPreviewMimeTypeFromExtension } from "@t3tools/shared/filePreview";
7
8import {
9 isAbsolutePath,
10 resolveWorkspaceFilePath,
11 resolveWorkspaceRelativeFilePath,
12} from "../files/filePath";
13
14export interface FileChipTarget {
15 readonly fullPath?: string;
16 readonly relativePath?: string;
17}
18
19export function fileChipClipboardPath(
20 target: FileChipTarget,
21 action: "copy-full-path" | "copy-relative-path",
22): string | undefined {
23 const path = action === "copy-full-path" ? target.fullPath : target.relativePath;
24 return path === undefined ? undefined : formatFilePathForClipboard(path, target.fullPath);
25}
26
27export function resolveFileChipTarget(
28 href: string,
29 workspaceRoot: string | null | undefined,
30): FileChipTarget | null {
31 const presentation = resolveMarkdownLinkPresentation(href);
32 if (presentation.kind !== "file") return null;
33 const relativePath = resolveWorkspaceRelativeFilePath(workspaceRoot, presentation.path);
34 const fullPath = isAbsolutePath(presentation.path)
35 ? presentation.path
36 : workspaceRoot && relativePath
37 ? resolveWorkspaceFilePath(workspaceRoot, relativePath)
38 : undefined;
39 if (!fullPath && !relativePath) return null;
40 return {
41 ...(fullPath ? { fullPath } : {}),
42 ...(relativePath ? { relativePath } : {}),
43 };
44}
45
46function fileChipMetadata(target: FileChipTarget) {
47 const path = target.fullPath ?? target.relativePath;
48 if (!path) return null;
49 const name = fileBasename(path);
50 const dot = name.lastIndexOf(".");
51 const mimeType = dot < 0 ? null : hostPreviewMimeTypeFromExtension(name.slice(dot));
52 return mimeType ? { path, name, mimeType } : null;
53}
54
55/** Use literal resolved paths so encoded filename characters are not decoded twice. */
56export function fileChipShareSource(target: FileChipTarget, threadId: ThreadId) {
57 const metadata = fileChipMetadata(target);
58 return metadata
59 ? {
60 name: metadata.name,
61 mimeType: metadata.mimeType,
62 resource: { _tag: "media-file" as const, threadId, path: metadata.path },
63 }
64 : null;
65}
66
67export function fileChipMenu(target: FileChipTarget): MarkdownFileContextMenu {
68 return {
69 title: target.fullPath ?? target.relativePath ?? "",
70 actions: [
71 ...(target.fullPath ? [{ id: "copy-full-path", title: "Copy full path" }] : []),
72 ...(target.relativePath ? [{ id: "copy-relative-path", title: "Copy relative path" }] : []),
73 { id: "open-file", title: "Open in file viewer" },
74 ...(fileChipMetadata(target)
75 ? [
76 {
77 id: "save",
78 title: "Save or share",
79 },
80 ]
81 : []),
82 ],
83 };
84}