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

fileChipMenu.ts

49 lines1.9 KB

1import { resolveMarkdownLinkPresentation } from "@t3tools/mobile-markdown-text/links";
2import type { MarkdownFileContextMenu } from "@t3tools/mobile-markdown-text/types";
3​
4import {
5 isAbsolutePath,
6 resolveWorkspaceFilePath,
7 resolveWorkspaceRelativeFilePath,
8} from "../files/filePath";
9​
10export type FileChipAction = "copy-full-path" | "copy-relative-path" | "open-file";
11​
12export interface FileChipTarget {
13 /** The host path, when the link is absolute or the workspace root is known. */
14 readonly fullPath?: string;
15 /** The path inside the workspace, when the link resolves there. */
16 readonly relativePath?: string;
18​
19/** Null when the link is not a file or resolves nowhere the feed can open, such as `~/x` or `../x`. */
20export function resolveFileChipTarget(
21 href: string,
22 workspaceRoot: string | null | undefined,
23): FileChipTarget | null {
24 const presentation = resolveMarkdownLinkPresentation(href);
25 if (presentation.kind !== "file") return null;
26 const relativePath = resolveWorkspaceRelativeFilePath(workspaceRoot, presentation.path);
27 const fullPath = isAbsolutePath(presentation.path)
28 ? presentation.path
29 : workspaceRoot && relativePath
30 ? resolveWorkspaceFilePath(workspaceRoot, relativePath)
31 : undefined;
32 if (!fullPath && !relativePath) return null;
33 return {
34 ...(fullPath ? { fullPath } : {}),
35 ...(relativePath ? { relativePath } : {}),
36 };
38​
39/** The same actions the web file chip offers on right-click. Opening is what a tap does. */
40export function fileChipMenu(target: FileChipTarget): MarkdownFileContextMenu {
41 return {
42 title: target.fullPath ?? target.relativePath ?? "",
43 actions: [
44 ...(target.fullPath ? [{ id: "copy-full-path", title: "Copy full path" }] : []),
45 ...(target.relativePath ? [{ id: "copy-relative-path", title: "Copy relative path" }] : []),
46 { id: "open-file", title: "Open in file viewer" },
47 ],
48 };