helpers.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { sortInstalledEditorsForOpenWith } from "@/lib/openWithEditors.js";
  2. import { toFileUrl } from "@/lib/path.js";
  3. import type { WorkspaceFileTreeRow } from "@/workspace-file-tree/model.js";
  4. export function toError(value: unknown): Error {
  5. return value instanceof Error ? value : new Error(String(value));
  6. }
  7. export function replaceSetValue(set: Set<string>, value: string, present: boolean): Set<string> {
  8. const next = new Set(set);
  9. if (present) {
  10. next.add(value);
  11. } else {
  12. next.delete(value);
  13. }
  14. return next;
  15. }
  16. function isMacLike(): boolean {
  17. if (typeof navigator === "undefined") {
  18. return false;
  19. }
  20. return /mac/i.test(navigator.userAgent);
  21. }
  22. function isWindowsLike(): boolean {
  23. if (typeof navigator === "undefined") {
  24. return false;
  25. }
  26. return /windows/i.test(navigator.userAgent);
  27. }
  28. export function getFileManagerLabel(intl: { formatMessage: (desc: { id: string }) => string }) {
  29. if (isMacLike()) {
  30. return intl.formatMessage({ id: "appHeader.openInFinder" });
  31. }
  32. if (isWindowsLike()) {
  33. return intl.formatMessage({ id: "appHeader.openInFileExplorer" });
  34. }
  35. return intl.formatMessage({ id: "appHeader.openInFileManager" });
  36. }
  37. export function isWorkspaceFileTreeHtmlFile(
  38. row: Pick<WorkspaceFileTreeRow, "path" | "type">,
  39. ): boolean {
  40. if (row.type !== "file") {
  41. return false;
  42. }
  43. return /\.(?:html|htm)$/i.test(row.path);
  44. }
  45. export function createWorkspaceFileTreeHtmlBrowserUrl(
  46. row: Pick<WorkspaceFileTreeRow, "path" | "type">,
  47. ): string | null {
  48. if (!isWorkspaceFileTreeHtmlFile(row)) {
  49. return null;
  50. }
  51. return toFileUrl(row.path);
  52. }
  53. export const sortInstalledEditorsForFileTree = sortInstalledEditorsForOpenWith;