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

filePreviewMode.ts

76 lines2.9 KB

1import type { AssetResource, ProjectReadFileError, ScopedThreadRef } from "@t3tools/contracts";
2import { isAbsolutePath, workspaceRelativeFilePath } from "@t3tools/shared/path";
3​
4/** Resolve workspace links before choosing between the explorer and a file preview. */
5export function resolveFilePreviewPath(path: string | null, cwd: string): string | null {
6 if (path === null) return null;
7 return path === "." || workspaceRelativeFilePath(path, cwd) === "." ? null : path;
8}
9​
10export const isMarkdownPreviewFile = (path: string): boolean => /\.(?:md|mdx)$/i.test(path);
11​
12/** Describe existing failure codes without exposing the underlying platform cause. */
13export function filePreviewReadErrorMessage(error: ProjectReadFileError): string {
14 switch (error.failure) {
15 case "path_not_file":
16 return "The path is a directory or special file, not a regular file.";
17 case "binary_file":
18 return "The file is binary and cannot be displayed as text.";
19 case "workspace_path_outside_root":
20 return "The requested path is outside the workspace.";
21 case "resolved_path_outside_root":
22 return "The path resolves to a location outside the workspace.";
23 case "operation_failed":
24 // A realpath failure can mean a missing path, permissions, or another I/O error.
25 return error.operation === "realpath-workspace-root"
26 ? "The workspace folder could not be accessed."
27 : "The file could not be accessed or read. It may be missing or inaccessible.";
28 default:
29 return error.message;
30 }
32​
33export function shouldShowFileExplorer(input: {
34 readonly relativePath: string | null;
35 readonly explorerOpen: boolean;
36 readonly attachmentOpen: boolean;
37}): boolean {
38 if (input.attachmentOpen || (input.relativePath && isAbsolutePath(input.relativePath))) {
39 return false;
40 }
41 return input.explorerOpen || input.relativePath === null;
43​
44export function setMarkdownTaskChecked(
45 markdown: string,
46 markerOffset: number,
47 checked: boolean,
48): string {
49 if (
50 markerOffset < 0 ||
51 markdown[markerOffset] !== "[" ||
52 !/[ xX]/.test(markdown[markerOffset + 1] ?? "") ||
53 markdown[markerOffset + 2] !== "]"
54 ) {
55 return markdown;
56 }
57​
58 return `${markdown.slice(0, markerOffset + 1)}${checked ? "x" : " "}${markdown.slice(markerOffset + 2)}`;
60​
61/**
62 * The asset for a file the panel shows. A draft has no thread on the server
63 * yet, so it names its workspace root instead of a thread to resolve one from.
64 */
65export function workspaceAssetResource(input: {
66 readonly kind: "workspace-file" | "media-file";
67 readonly threadRef: ScopedThreadRef;
68 readonly draft: boolean;
69 readonly workspaceRoot: string;
70 readonly absolutePath: string;
71}): AssetResource {
72 if (input.draft) {
73 return { _tag: "draft-workspace-file", cwd: input.workspaceRoot, path: input.absolutePath };
74 }
75 return { _tag: input.kind, threadId: input.threadRef.threadId, path: input.absolutePath };