| 1 | import { escapeRefPath, unescapeRefPath } from "./refToken"; |
| 2 | |
| 3 | export const WORKSPACE_REF_DRAG_TYPE = "application/x-reasonix-workspace-ref"; |
| 4 | |
| 5 | export interface WorkspaceRefDragPayload { |
| 6 | path: string; |
| 7 | isDir?: boolean; |
| 8 | } |
| 9 | |
| 10 | export function formatWorkspaceReference(path: string, isDir?: boolean): string { |
| 11 | const clean = isDir && !path.endsWith("/") ? path + "/" : path; |
| 12 | return `@${escapeRefPath(clean)}`; |
| 13 | } |
| 14 | |
| 15 | export function parseWorkspaceReference(text: string): WorkspaceRefDragPayload | null { |
| 16 | const trimmed = text.trim(); |
| 17 | const match = /^@((?:\\[ \t]|[^\s])+)$/.exec(trimmed); |
| 18 | if (!match) return null; |
| 19 | const path = unescapeRefPath(match[1]); |
| 20 | if (!path) return null; |
| 21 | return { path, isDir: path.endsWith("/") }; |
| 22 | } |
| 23 | |
| 24 | export function readWorkspaceReferenceDrag(dataTransfer: DataTransfer): WorkspaceRefDragPayload | null { |
| 25 | if (!Array.from(dataTransfer.types).includes(WORKSPACE_REF_DRAG_TYPE)) return null; |
| 26 | try { |
| 27 | const payload = JSON.parse(dataTransfer.getData(WORKSPACE_REF_DRAG_TYPE)) as WorkspaceRefDragPayload; |
| 28 | if (!payload.path) return null; |
| 29 | return { path: payload.path, isDir: payload.isDir }; |
| 30 | } catch { |
| 31 | return null; |
| 32 | } |
| 33 | } |
| 34 |