workbenchDragDrop.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import type { PaneSplitSide } from "@/v4/paneLayoutTree.js";
  2. export const WORKBENCH_SESSION_DRAG_MIME = "application/x-zcode-session";
  3. export interface WorkbenchSessionDragPayload {
  4. readonly kind: "zcode/session";
  5. readonly workspacePath: string;
  6. readonly workspaceIdentity?: string;
  7. readonly remoteSessionId?: string;
  8. readonly sessionId: string;
  9. }
  10. let activeWorkbenchSessionDragPayload: WorkbenchSessionDragPayload | null = null;
  11. interface DataTransferLike {
  12. readonly types?: Iterable<string> | ArrayLike<string>;
  13. getData(type: string): string;
  14. }
  15. interface RectLike {
  16. readonly left: number;
  17. readonly top: number;
  18. readonly width: number;
  19. readonly height: number;
  20. }
  21. export function serializeWorkbenchSessionDragPayload(payload: WorkbenchSessionDragPayload): string {
  22. return JSON.stringify(payload);
  23. }
  24. export function setActiveWorkbenchSessionDragPayload(payload: WorkbenchSessionDragPayload): void {
  25. activeWorkbenchSessionDragPayload = payload;
  26. }
  27. export function clearActiveWorkbenchSessionDragPayload(): void {
  28. activeWorkbenchSessionDragPayload = null;
  29. }
  30. function dataTransferHasType(dataTransfer: DataTransferLike): boolean {
  31. return Array.from(dataTransfer.types ?? []).includes(WORKBENCH_SESSION_DRAG_MIME);
  32. }
  33. function isRecord(value: unknown): value is Record<string, unknown> {
  34. return typeof value === "object" && value !== null && !Array.isArray(value);
  35. }
  36. export function parseWorkbenchSessionDragPayload(
  37. dataTransfer: DataTransferLike,
  38. ): WorkbenchSessionDragPayload | null {
  39. if (!dataTransferHasType(dataTransfer)) {
  40. return null;
  41. }
  42. try {
  43. const raw = dataTransfer.getData(WORKBENCH_SESSION_DRAG_MIME);
  44. if (!raw) {
  45. return activeWorkbenchSessionDragPayload;
  46. }
  47. const parsed = JSON.parse(raw);
  48. if (
  49. !isRecord(parsed) ||
  50. parsed.kind !== "zcode/session" ||
  51. typeof parsed.workspacePath !== "string" ||
  52. typeof parsed.sessionId !== "string"
  53. ) {
  54. return null;
  55. }
  56. return {
  57. kind: "zcode/session",
  58. workspacePath: parsed.workspacePath,
  59. workspaceIdentity:
  60. typeof parsed.workspaceIdentity === "string" ? parsed.workspaceIdentity : undefined,
  61. remoteSessionId:
  62. typeof parsed.remoteSessionId === "string" ? parsed.remoteSessionId : undefined,
  63. sessionId: parsed.sessionId,
  64. };
  65. } catch {
  66. return null;
  67. }
  68. }
  69. export function resolveWorkbenchDropSide(
  70. rect: RectLike,
  71. clientX: number,
  72. clientY: number,
  73. ): PaneSplitSide | null {
  74. if (rect.width <= 0 || rect.height <= 0) {
  75. return null;
  76. }
  77. const x = (clientX - rect.left) / rect.width;
  78. const y = (clientY - rect.top) / rect.height;
  79. if (x < 0 || x > 1 || y < 0 || y > 1) {
  80. return null;
  81. }
  82. const distances: Array<readonly [PaneSplitSide, number]> = [
  83. ["left", x],
  84. ["right", 1 - x],
  85. ["up", y],
  86. ["down", 1 - y],
  87. ];
  88. const [side, distance] = distances.reduce((best, candidate) =>
  89. candidate[1] < best[1] ? candidate : best,
  90. );
  91. return distance <= 0.32 ? side : null;
  92. }