|
| 1 | +import type { Workspace } from "@coder-studio/core"; |
| 2 | +import type { Store } from "jotai/vanilla/store"; |
| 3 | +import { activeFilePathAtomFamily, openEditorPathsAtomFamily } from "../atoms"; |
| 4 | + |
| 5 | +function hasOwnProperty<T extends object>(value: T, key: PropertyKey): boolean { |
| 6 | + return Object.prototype.hasOwnProperty.call(value, key); |
| 7 | +} |
| 8 | + |
| 9 | +export function normalizeOpenEditorPaths(value: unknown): string[] { |
| 10 | + if (!Array.isArray(value)) { |
| 11 | + return []; |
| 12 | + } |
| 13 | + |
| 14 | + const seen = new Set<string>(); |
| 15 | + const next: string[] = []; |
| 16 | + |
| 17 | + for (const entry of value) { |
| 18 | + if (typeof entry !== "string" || entry.trim().length === 0 || seen.has(entry)) { |
| 19 | + continue; |
| 20 | + } |
| 21 | + |
| 22 | + seen.add(entry); |
| 23 | + next.push(entry); |
| 24 | + } |
| 25 | + |
| 26 | + return next; |
| 27 | +} |
| 28 | + |
| 29 | +export function normalizeActiveEditorPath(value: unknown): string | null { |
| 30 | + if (typeof value !== "string" || value.trim().length === 0) { |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + return value; |
| 35 | +} |
| 36 | + |
| 37 | +export function mergeOpenEditorPaths(...pathLists: Array<Iterable<string> | undefined>): string[] { |
| 38 | + return normalizeOpenEditorPaths(pathLists.flatMap((paths) => (paths ? Array.from(paths) : []))); |
| 39 | +} |
| 40 | + |
| 41 | +export function appendOpenEditorPath(paths: Iterable<string>, path: string): string[] { |
| 42 | + return mergeOpenEditorPaths(paths, [path]); |
| 43 | +} |
| 44 | + |
| 45 | +export function removeOpenEditorPaths( |
| 46 | + paths: Iterable<string>, |
| 47 | + removedPaths: Iterable<string> |
| 48 | +): string[] { |
| 49 | + const removed = new Set(removedPaths); |
| 50 | + return normalizeOpenEditorPaths(Array.from(paths)).filter((path) => !removed.has(path)); |
| 51 | +} |
| 52 | + |
| 53 | +export function rewriteDescendantEditorPath( |
| 54 | + path: string, |
| 55 | + fromPath: string, |
| 56 | + toPath: string |
| 57 | +): string { |
| 58 | + if (path === fromPath) { |
| 59 | + return toPath; |
| 60 | + } |
| 61 | + |
| 62 | + if (path.startsWith(`${fromPath}/`)) { |
| 63 | + return `${toPath}${path.slice(fromPath.length)}`; |
| 64 | + } |
| 65 | + |
| 66 | + return path; |
| 67 | +} |
| 68 | + |
| 69 | +export function rewriteOpenEditorPaths( |
| 70 | + paths: Iterable<string>, |
| 71 | + fromPath: string, |
| 72 | + toPath: string |
| 73 | +): string[] { |
| 74 | + return normalizeOpenEditorPaths( |
| 75 | + Array.from(paths).map((path) => rewriteDescendantEditorPath(path, fromPath, toPath)) |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export function normalizeWorkspaceEditorUiStatePatch( |
| 80 | + uiState: Partial<Pick<Workspace["uiState"], "openEditorPaths" | "activeEditorPath">> |
| 81 | +): { openEditorPaths?: string[]; activeEditorPath?: string | null } | null { |
| 82 | + const hasOpenEditorPaths = hasOwnProperty(uiState, "openEditorPaths"); |
| 83 | + const hasActiveEditorPath = hasOwnProperty(uiState, "activeEditorPath"); |
| 84 | + |
| 85 | + if (!hasOpenEditorPaths && !hasActiveEditorPath) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + const next: { openEditorPaths?: string[]; activeEditorPath?: string | null } = {}; |
| 90 | + |
| 91 | + if (hasOpenEditorPaths) { |
| 92 | + const openEditorPaths = normalizeOpenEditorPaths(uiState.openEditorPaths); |
| 93 | + next.openEditorPaths = openEditorPaths; |
| 94 | + |
| 95 | + if (hasActiveEditorPath) { |
| 96 | + const activeEditorPath = normalizeActiveEditorPath(uiState.activeEditorPath); |
| 97 | + next.activeEditorPath = activeEditorPath; |
| 98 | + |
| 99 | + if (activeEditorPath && !openEditorPaths.includes(activeEditorPath)) { |
| 100 | + next.openEditorPaths = [...openEditorPaths, activeEditorPath]; |
| 101 | + } |
| 102 | + } |
| 103 | + } else if (hasActiveEditorPath) { |
| 104 | + next.activeEditorPath = normalizeActiveEditorPath(uiState.activeEditorPath); |
| 105 | + } |
| 106 | + |
| 107 | + return next; |
| 108 | +} |
| 109 | + |
| 110 | +export function normalizeWorkspaceEditorUiState( |
| 111 | + uiState: Workspace["uiState"] |
| 112 | +): Workspace["uiState"] { |
| 113 | + const normalizedPatch = normalizeWorkspaceEditorUiStatePatch(uiState); |
| 114 | + if (!normalizedPatch) { |
| 115 | + return uiState; |
| 116 | + } |
| 117 | + |
| 118 | + return { |
| 119 | + ...uiState, |
| 120 | + ...(hasOwnProperty(normalizedPatch, "openEditorPaths") |
| 121 | + ? { openEditorPaths: normalizedPatch.openEditorPaths } |
| 122 | + : {}), |
| 123 | + ...(hasOwnProperty(normalizedPatch, "activeEditorPath") |
| 124 | + ? { activeEditorPath: normalizedPatch.activeEditorPath } |
| 125 | + : {}), |
| 126 | + }; |
| 127 | +} |
| 128 | + |
| 129 | +export function hydrateWorkspaceEditorState( |
| 130 | + store: Store, |
| 131 | + workspaceId: string, |
| 132 | + uiState?: Partial<Workspace["uiState"]> | null |
| 133 | +): void { |
| 134 | + if (!uiState) { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const hasOpenEditorPaths = hasOwnProperty(uiState, "openEditorPaths"); |
| 139 | + const hasActiveEditorPath = hasOwnProperty(uiState, "activeEditorPath"); |
| 140 | + if (!hasOpenEditorPaths && !hasActiveEditorPath) { |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + const normalizedPatch = normalizeWorkspaceEditorUiStatePatch(uiState); |
| 145 | + if (!normalizedPatch) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + if (hasOpenEditorPaths) { |
| 150 | + store.set(openEditorPathsAtomFamily(workspaceId), normalizedPatch.openEditorPaths ?? []); |
| 151 | + } |
| 152 | + |
| 153 | + if (hasActiveEditorPath) { |
| 154 | + store.set(activeFilePathAtomFamily(workspaceId), normalizedPatch.activeEditorPath ?? null); |
| 155 | + } |
| 156 | +} |
0 commit comments