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

fileChipMenu.ts

87 lines3.0 KB

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 resolveFileLinkPath,
11 resolveWorkspaceFilePath,
12 resolveWorkspaceRelativeFilePath,
13} from "../files/filePath";
14​
15export interface FileChipTarget {
16 readonly fullPath?: string;
17 readonly relativePath?: string;
19​
20export function fileChipClipboardPath(
21 target: FileChipTarget,
22 action: "copy-full-path" | "copy-relative-path",
23): string | undefined {
24 const path = action === "copy-full-path" ? target.fullPath : target.relativePath;
25 return path === undefined ? undefined : formatFilePathForClipboard(path, target.fullPath);
27​
28export function resolveFileChipTarget(
29 href: string,
30 workspaceRoot: string | null | undefined,
31): FileChipTarget | null {
32 const presentation = resolveMarkdownLinkPresentation(href);
33 if (presentation.kind !== "file") return null;
34 const path = resolveFileLinkPath(workspaceRoot, presentation.path);
35 if (path === null) return null;
36 const relativePath = resolveWorkspaceRelativeFilePath(workspaceRoot, path);
37 const fullPath = isAbsolutePath(path)
38 ? path
39 : workspaceRoot && relativePath
40 ? resolveWorkspaceFilePath(workspaceRoot, relativePath)
41 : undefined;
42 if (!fullPath && !relativePath) return null;
43 return {
44 ...(fullPath ? { fullPath } : {}),
45 ...(relativePath ? { relativePath } : {}),
46 };
48​
49function fileChipMetadata(target: FileChipTarget) {
50 const path = target.fullPath ?? target.relativePath;
51 if (!path) return null;
52 const name = fileBasename(path);
53 const dot = name.lastIndexOf(".");
54 const mimeType = dot < 0 ? null : hostPreviewMimeTypeFromExtension(name.slice(dot));
55 return mimeType ? { path, name, mimeType } : null;
57​
58/** Use literal resolved paths so encoded filename characters are not decoded twice. */
59export function fileChipShareSource(target: FileChipTarget, threadId: ThreadId) {
60 const metadata = fileChipMetadata(target);
61 return metadata
62 ? {
63 name: metadata.name,
64 mimeType: metadata.mimeType,
65 resource: { _tag: "media-file" as const, threadId, path: metadata.path },
66 }
67 : null;
69​
70export function fileChipMenu(target: FileChipTarget): MarkdownFileContextMenu {
71 return {
72 title: target.fullPath ?? target.relativePath ?? "",
73 actions: [
74 ...(target.fullPath ? [{ id: "copy-full-path", title: "Copy full path" }] : []),
75 ...(target.relativePath ? [{ id: "copy-relative-path", title: "Copy relative path" }] : []),
76 { id: "open-file", title: "Open in file viewer" },
77 ...(fileChipMetadata(target)
78 ? [
79 {
80 id: "save",
81 title: "Save or share",
82 },
83 ]
84 : []),
85 ],
86 };