| 1 | export function readLocal(key, fallback) { |
| 2 | try { |
| 3 | return JSON.parse(localStorage.getItem(key)) ?? fallback; |
| 4 | } catch { |
| 5 | return fallback; |
| 6 | } |
| 7 | } |
| 8 | |
| 9 | export function orderedTabs(tabs, saved) { |
| 10 | const order = saved.order || []; |
| 11 | return [...tabs].sort((a, b) => { |
| 12 | const pinned = |
| 13 | Number(Boolean(saved[b.id]?.pinned)) - |
| 14 | Number(Boolean(saved[a.id]?.pinned)); |
| 15 | if (pinned) return pinned; |
| 16 | const index = (id) => |
| 17 | order.includes(id) ? order.indexOf(id) : order.length; |
| 18 | return index(a.id) - index(b.id); |
| 19 | }); |
| 20 | } |
| 21 | |
| 22 | const clientMount = /^\/run\/cage-client-projects\/[0-9a-f]{32}$/; |
| 23 | const folderName = (path) => path?.split(/[\\/]/).filter(Boolean).at(-1); |
| 24 | |
| 25 | export function rememberClientProjectName(share) { |
| 26 | if (!clientMount.test(share.guest_path)) return; |
| 27 | try { sessionStorage.setItem(share.guest_path, folderName(share.path)); } |
| 28 | catch { } |
| 29 | } |
| 30 | |
| 31 | export function projectName(path, defaultWorkspace) { |
| 32 | if (clientMount.test(path)) { |
| 33 | try { return sessionStorage.getItem(path) || "本机共享项目"; } |
| 34 | catch { return "本机共享项目"; } |
| 35 | } |
| 36 | if (path === defaultWorkspace && path?.endsWith("-workspace")) return "默认工作区"; |
| 37 | return folderName(path) || "新项目"; |
| 38 | } |
| 39 | |
| 40 | export function tabName(tab, saved, project) { |
| 41 | return (tab.incognito ? "无痕 · " : "") + ( |
| 42 | saved[tab.id]?.title || |
| 43 | (tab.kind === "editor" |
| 44 | ? `文件 · ${tab.workspace ? projectName(tab.workspace) : project}` |
| 45 | : tab.title) || |
| 46 | "新标签页" |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | export function focusTab(layout, id) { |
| 52 | if (layout.active === id) return layout; |
| 53 | if (!id) return { active: "" }; |
| 54 | if (layout.other === id) |
| 55 | return { active: id, other: layout.active, side: layout.side === "left" ? "right" : "left" }; |
| 56 | if (layout.other && id.startsWith("terminal:") !== layout.active.startsWith("terminal:") && |
| 57 | id.startsWith("terminal:") === layout.other.startsWith("terminal:")) |
| 58 | return { ...focusTab(layout, layout.other), active: id }; |
| 59 | return { ...layout, active: id }; |
| 60 | } |
| 61 | |
| 62 | export function splitTab(layout, tabs, id, side) { |
| 63 | if (!tabs.some(tab => tab.id === id)) return layout; |
| 64 | const opposite = layout.side === side ? layout.other : layout.active; |
| 65 | const other = [opposite, layout.other, layout.active, ...tabs.map(tab => tab.id)] |
| 66 | .find(candidate => candidate !== id && tabs.some(tab => tab.id === candidate)); |
| 67 | return other ? { active: id, other, side } : layout; |
| 68 | } |