workbenchLayout.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // 分屏布局计算(纯函数):分割树 → 每个叶子/分隔条的绝对定位表达式。
  2. // 叶子扁平渲染为容器直接子元素(key = paneId 稳定)——拆分/关闭只是换 rect,
  3. // React 不重挂任何存活 pane(virtualizer 测高、滚动位置、composer 草稿全保留);
  4. // 若按树递归嵌套渲染,叶子被替换成分割容器时会整棵重挂。
  5. // rect 表达式引用分割占比 CSS 变量:拖动中改一个变量,受影响的 pane/分隔条全部
  6. // 由 CSS 重排,零 React 渲染(「容器 CSS 变量」方案按分割节点泛化)。
  7. import type { CSSProperties } from "react";
  8. import type { PaneLayoutNode, SplitDirection } from "@/v4/paneLayoutTree.js";
  9. /** 分割占比 CSS 变量前缀(每个分割节点一个 `--v4-split-<nodeId>`)。 */
  10. export const SPLIT_VAR_PREFIX = "--v4-split-";
  11. /** 绝对定位表达式(不含 calc() 包裹;style 侧统一 `calc(${expr})`)。 */
  12. export interface RectExpr {
  13. left: string;
  14. top: string;
  15. width: string;
  16. height: string;
  17. }
  18. export interface LeafLayout {
  19. paneId: string;
  20. rect: RectExpr;
  21. }
  22. export interface DividerLayout {
  23. splitId: string;
  24. direction: SplitDirection;
  25. /** store 当前占比(拖拽起点 + 容器 CSS 变量初值)。 */
  26. ratio: number;
  27. /** 该分割节点区域占容器主轴的数值比例(拖拽像素→占比换算;祖先占比取 store 数值)。 */
  28. regionFraction: number;
  29. /** 分割线(主轴位置)表达式。 */
  30. boundary: string;
  31. /** 交叉轴起点/长度表达式。 */
  32. crossStart: string;
  33. crossLength: string;
  34. }
  35. const ROOT_RECT: RectExpr = {
  36. left: "0%",
  37. top: "0%",
  38. width: "100%",
  39. height: "100%",
  40. };
  41. function collectNode(
  42. node: PaneLayoutNode,
  43. rect: RectExpr,
  44. widthFraction: number,
  45. heightFraction: number,
  46. leaves: LeafLayout[],
  47. dividers: DividerLayout[],
  48. ): void {
  49. if (node.type === "leaf") {
  50. leaves.push({ paneId: node.paneId, rect });
  51. return;
  52. }
  53. const ratioVar = `var(${SPLIT_VAR_PREFIX}${node.id}, ${node.ratio})`;
  54. if (node.direction === "row") {
  55. const boundary = `(${rect.left}) + (${rect.width}) * ${ratioVar}`;
  56. dividers.push({
  57. splitId: node.id,
  58. direction: "row",
  59. ratio: node.ratio,
  60. regionFraction: widthFraction,
  61. boundary,
  62. crossStart: rect.top,
  63. crossLength: rect.height,
  64. });
  65. collectNode(
  66. node.first,
  67. { ...rect, width: `(${rect.width}) * ${ratioVar}` },
  68. widthFraction * node.ratio,
  69. heightFraction,
  70. leaves,
  71. dividers,
  72. );
  73. collectNode(
  74. node.second,
  75. { ...rect, left: boundary, width: `(${rect.width}) * (1 - ${ratioVar})` },
  76. widthFraction * (1 - node.ratio),
  77. heightFraction,
  78. leaves,
  79. dividers,
  80. );
  81. return;
  82. }
  83. const boundary = `(${rect.top}) + (${rect.height}) * ${ratioVar}`;
  84. dividers.push({
  85. splitId: node.id,
  86. direction: "column",
  87. ratio: node.ratio,
  88. regionFraction: heightFraction,
  89. boundary,
  90. crossStart: rect.left,
  91. crossLength: rect.width,
  92. });
  93. collectNode(
  94. node.first,
  95. { ...rect, height: `(${rect.height}) * ${ratioVar}` },
  96. widthFraction,
  97. heightFraction * node.ratio,
  98. leaves,
  99. dividers,
  100. );
  101. collectNode(
  102. node.second,
  103. { ...rect, top: boundary, height: `(${rect.height}) * (1 - ${ratioVar})` },
  104. widthFraction,
  105. heightFraction * (1 - node.ratio),
  106. leaves,
  107. dividers,
  108. );
  109. }
  110. export interface WorkbenchLayout {
  111. leaves: LeafLayout[];
  112. dividers: DividerLayout[];
  113. }
  114. export function collectWorkbenchLayout(root: PaneLayoutNode): WorkbenchLayout {
  115. const leaves: LeafLayout[] = [];
  116. const dividers: DividerLayout[] = [];
  117. collectNode(root, ROOT_RECT, 1, 1, leaves, dividers);
  118. return { leaves, dividers };
  119. }
  120. export function rectStyle(rect: RectExpr): CSSProperties {
  121. return {
  122. left: `calc(${rect.left})`,
  123. top: `calc(${rect.top})`,
  124. width: `calc(${rect.width})`,
  125. height: `calc(${rect.height})`,
  126. };
  127. }
  128. export function dividerStyle(divider: DividerLayout): CSSProperties {
  129. if (divider.direction === "row") {
  130. return {
  131. left: `calc(${divider.boundary})`,
  132. top: `calc(${divider.crossStart})`,
  133. height: `calc(${divider.crossLength})`,
  134. width: "9px",
  135. transform: "translateX(-50%)",
  136. };
  137. }
  138. return {
  139. top: `calc(${divider.boundary})`,
  140. left: `calc(${divider.crossStart})`,
  141. width: `calc(${divider.crossLength})`,
  142. height: "9px",
  143. transform: "translateY(-50%)",
  144. };
  145. }