@agile-plus/headless 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (231) hide show
  1. package/README.md +24 -17
  2. package/dist/index.cjs +4163 -223
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +33 -0
  5. package/dist/index.d.ts +33 -0
  6. package/dist/index.js +4258 -216
  7. package/dist/index.js.map +1 -1
  8. package/dist/types-BxsQI7P5.d.cts +93 -0
  9. package/dist/types-BxsQI7P5.d.ts +93 -0
  10. package/dist/types-zpeY0yvD.d.cts +73 -0
  11. package/dist/types-zpeY0yvD.d.ts +73 -0
  12. package/dist/useAccordion/index.cjs +149 -0
  13. package/dist/useAccordion/index.cjs.map +1 -0
  14. package/dist/useAccordion/index.d.cts +119 -0
  15. package/dist/useAccordion/index.d.ts +119 -0
  16. package/dist/useAccordion/index.js +128 -0
  17. package/dist/useAccordion/index.js.map +1 -0
  18. package/dist/useAvatar/index.cjs +135 -0
  19. package/dist/useAvatar/index.cjs.map +1 -0
  20. package/dist/useAvatar/index.d.cts +120 -0
  21. package/dist/useAvatar/index.d.ts +120 -0
  22. package/dist/useAvatar/index.js +106 -0
  23. package/dist/useAvatar/index.js.map +1 -0
  24. package/dist/useCheckbox/index.cjs +3 -1
  25. package/dist/useCheckbox/index.cjs.map +1 -1
  26. package/dist/useCheckbox/index.js +3 -1
  27. package/dist/useCheckbox/index.js.map +1 -1
  28. package/dist/useCollapsible/index.cjs +10 -0
  29. package/dist/useCollapsible/index.cjs.map +1 -1
  30. package/dist/useCollapsible/index.js +11 -1
  31. package/dist/useCollapsible/index.js.map +1 -1
  32. package/dist/useDatePicker/index.cjs +684 -0
  33. package/dist/useDatePicker/index.cjs.map +1 -0
  34. package/dist/useDatePicker/index.d.cts +191 -0
  35. package/dist/useDatePicker/index.d.ts +191 -0
  36. package/dist/useDatePicker/index.js +671 -0
  37. package/dist/useDatePicker/index.js.map +1 -0
  38. package/dist/useDateRangePicker/index.cjs +614 -0
  39. package/dist/useDateRangePicker/index.cjs.map +1 -0
  40. package/dist/useDateRangePicker/index.d.cts +159 -0
  41. package/dist/useDateRangePicker/index.d.ts +159 -0
  42. package/dist/useDateRangePicker/index.js +601 -0
  43. package/dist/useDateRangePicker/index.js.map +1 -0
  44. package/dist/useDialog/index.cjs.map +1 -1
  45. package/dist/useDialog/index.js.map +1 -1
  46. package/dist/useDropdownMenu/index.cjs +468 -0
  47. package/dist/useDropdownMenu/index.cjs.map +1 -0
  48. package/dist/useDropdownMenu/index.d.cts +73 -0
  49. package/dist/useDropdownMenu/index.d.ts +73 -0
  50. package/dist/useDropdownMenu/index.js +441 -0
  51. package/dist/useDropdownMenu/index.js.map +1 -0
  52. package/dist/useEllipsis/index.cjs +181 -0
  53. package/dist/useEllipsis/index.cjs.map +1 -0
  54. package/dist/useEllipsis/index.d.cts +67 -0
  55. package/dist/useEllipsis/index.d.ts +67 -0
  56. package/dist/useEllipsis/index.js +162 -0
  57. package/dist/useEllipsis/index.js.map +1 -0
  58. package/dist/useFloating/index.cjs +177 -0
  59. package/dist/useFloating/index.cjs.map +1 -0
  60. package/dist/useFloating/index.d.cts +21 -0
  61. package/dist/useFloating/index.d.ts +21 -0
  62. package/dist/useFloating/index.js +150 -0
  63. package/dist/useFloating/index.js.map +1 -0
  64. package/dist/useFormControl/index.cjs +5 -0
  65. package/dist/useFormControl/index.cjs.map +1 -1
  66. package/dist/useFormControl/index.js +5 -0
  67. package/dist/useFormControl/index.js.map +1 -1
  68. package/dist/useInput/index.cjs +192 -0
  69. package/dist/useInput/index.cjs.map +1 -0
  70. package/dist/useInput/index.d.cts +41 -0
  71. package/dist/useInput/index.d.ts +41 -0
  72. package/dist/useInput/index.js +165 -0
  73. package/dist/useInput/index.js.map +1 -0
  74. package/dist/useKeepMounted/index.cjs +44 -0
  75. package/dist/useKeepMounted/index.cjs.map +1 -0
  76. package/dist/useKeepMounted/index.d.cts +45 -0
  77. package/dist/useKeepMounted/index.d.ts +45 -0
  78. package/dist/useKeepMounted/index.js +17 -0
  79. package/dist/useKeepMounted/index.js.map +1 -0
  80. package/dist/useMenu/index.cjs +438 -0
  81. package/dist/useMenu/index.cjs.map +1 -0
  82. package/dist/useMenu/index.d.cts +134 -0
  83. package/dist/useMenu/index.d.ts +134 -0
  84. package/dist/useMenu/index.js +419 -0
  85. package/dist/useMenu/index.js.map +1 -0
  86. package/dist/usePagination/index.cjs +113 -0
  87. package/dist/usePagination/index.cjs.map +1 -0
  88. package/dist/usePagination/index.d.cts +139 -0
  89. package/dist/usePagination/index.d.ts +139 -0
  90. package/dist/usePagination/index.js +95 -0
  91. package/dist/usePagination/index.js.map +1 -0
  92. package/dist/usePopover/index.cjs +312 -0
  93. package/dist/usePopover/index.cjs.map +1 -0
  94. package/dist/usePopover/index.d.cts +71 -0
  95. package/dist/usePopover/index.d.ts +71 -0
  96. package/dist/usePopover/index.js +285 -0
  97. package/dist/usePopover/index.js.map +1 -0
  98. package/dist/useSelect/index.cjs +729 -0
  99. package/dist/useSelect/index.cjs.map +1 -0
  100. package/dist/useSelect/index.d.cts +193 -0
  101. package/dist/useSelect/index.d.ts +193 -0
  102. package/dist/useSelect/index.js +710 -0
  103. package/dist/useSelect/index.js.map +1 -0
  104. package/dist/useSplitter/index.cjs +558 -0
  105. package/dist/useSplitter/index.cjs.map +1 -0
  106. package/dist/useSplitter/index.d.cts +307 -0
  107. package/dist/useSplitter/index.d.ts +307 -0
  108. package/dist/useSplitter/index.js +541 -0
  109. package/dist/useSplitter/index.js.map +1 -0
  110. package/dist/useSwitch/index.cjs +4 -1
  111. package/dist/useSwitch/index.cjs.map +1 -1
  112. package/dist/useSwitch/index.js +4 -1
  113. package/dist/useSwitch/index.js.map +1 -1
  114. package/dist/useTable/index.cjs +308 -0
  115. package/dist/useTable/index.cjs.map +1 -0
  116. package/dist/useTable/index.d.cts +242 -0
  117. package/dist/useTable/index.d.ts +242 -0
  118. package/dist/useTable/index.js +301 -0
  119. package/dist/useTable/index.js.map +1 -0
  120. package/dist/useTabs/index.cjs +13 -4
  121. package/dist/useTabs/index.cjs.map +1 -1
  122. package/dist/useTabs/index.d.cts +5 -1
  123. package/dist/useTabs/index.d.ts +5 -1
  124. package/dist/useTabs/index.js +13 -4
  125. package/dist/useTabs/index.js.map +1 -1
  126. package/dist/useTextControl-Dcq0GFmg.d.cts +63 -0
  127. package/dist/useTextControl-Dcq0GFmg.d.ts +63 -0
  128. package/dist/useTextarea/index.cjs +186 -0
  129. package/dist/useTextarea/index.cjs.map +1 -0
  130. package/dist/useTextarea/index.d.cts +30 -0
  131. package/dist/useTextarea/index.d.ts +30 -0
  132. package/dist/useTextarea/index.js +159 -0
  133. package/dist/useTextarea/index.js.map +1 -0
  134. package/dist/useToast/index.cjs +27 -20
  135. package/dist/useToast/index.cjs.map +1 -1
  136. package/dist/useToast/index.js +27 -20
  137. package/dist/useToast/index.js.map +1 -1
  138. package/dist/useToastViewport/index.cjs +1 -3
  139. package/dist/useToastViewport/index.cjs.map +1 -1
  140. package/dist/useToastViewport/index.js +1 -3
  141. package/dist/useToastViewport/index.js.map +1 -1
  142. package/dist/useTooltip/index.cjs +357 -0
  143. package/dist/useTooltip/index.cjs.map +1 -0
  144. package/dist/useTooltip/index.d.cts +81 -0
  145. package/dist/useTooltip/index.d.ts +81 -0
  146. package/dist/useTooltip/index.js +338 -0
  147. package/dist/useTooltip/index.js.map +1 -0
  148. package/dist/useVirtualList/index.cjs +308 -0
  149. package/dist/useVirtualList/index.cjs.map +1 -0
  150. package/dist/useVirtualList/index.d.cts +31 -0
  151. package/dist/useVirtualList/index.d.ts +31 -0
  152. package/dist/useVirtualList/index.js +288 -0
  153. package/dist/useVirtualList/index.js.map +1 -0
  154. package/dist/useVirtualTree/index.cjs +520 -0
  155. package/dist/useVirtualTree/index.cjs.map +1 -0
  156. package/dist/useVirtualTree/index.d.cts +146 -0
  157. package/dist/useVirtualTree/index.d.ts +146 -0
  158. package/dist/useVirtualTree/index.js +511 -0
  159. package/dist/useVirtualTree/index.js.map +1 -0
  160. package/package.json +121 -2
  161. package/src/index.ts +38 -0
  162. package/src/useAccordion/index.ts +7 -0
  163. package/src/useAccordion/types.ts +118 -0
  164. package/src/useAccordion/useAccordion.ts +130 -0
  165. package/src/useAvatar/index.ts +14 -0
  166. package/src/useAvatar/types.ts +90 -0
  167. package/src/useAvatar/useAvatar.ts +39 -0
  168. package/src/useAvatar/useAvatarFallback.ts +62 -0
  169. package/src/useAvatar/useAvatarImage.ts +78 -0
  170. package/src/useCheckbox/useCheckbox.ts +4 -1
  171. package/src/useCollapsible/useCollapsible.ts +22 -1
  172. package/src/useDatePicker/index.ts +9 -0
  173. package/src/useDatePicker/types.ts +177 -0
  174. package/src/useDatePicker/useDatePicker.ts +688 -0
  175. package/src/useDateRangePicker/index.ts +8 -0
  176. package/src/useDateRangePicker/types.ts +146 -0
  177. package/src/useDateRangePicker/useDateRangePicker.ts +618 -0
  178. package/src/useDropdownMenu/index.ts +3 -0
  179. package/src/useDropdownMenu/types.ts +61 -0
  180. package/src/useDropdownMenu/useDropdownMenu.ts +322 -0
  181. package/src/useEllipsis/index.ts +3 -0
  182. package/src/useEllipsis/types.ts +51 -0
  183. package/src/useEllipsis/useEllipsis.ts +169 -0
  184. package/src/useFloating/index.ts +3 -0
  185. package/src/useFloating/types.ts +79 -0
  186. package/src/useFloating/useFloating.ts +199 -0
  187. package/src/useFormControl/useFormControl.ts +13 -0
  188. package/src/useInput/index.ts +3 -0
  189. package/src/useInput/types.ts +29 -0
  190. package/src/useInput/useInput.ts +40 -0
  191. package/src/useKeepMounted/index.ts +3 -0
  192. package/src/useKeepMounted/types.ts +29 -0
  193. package/src/useKeepMounted/useKeepMounted.ts +34 -0
  194. package/src/useMenu/index.ts +9 -0
  195. package/src/useMenu/types.ts +126 -0
  196. package/src/useMenu/useMenu.ts +504 -0
  197. package/src/usePagination/index.ts +3 -0
  198. package/src/usePagination/types.ts +137 -0
  199. package/src/usePagination/usePagination.ts +109 -0
  200. package/src/usePopover/index.ts +3 -0
  201. package/src/usePopover/types.ts +65 -0
  202. package/src/usePopover/usePopover.ts +157 -0
  203. package/src/useSelect/index.ts +9 -0
  204. package/src/useSelect/types.ts +184 -0
  205. package/src/useSelect/useSelect.ts +713 -0
  206. package/src/useSplitter/index.ts +10 -0
  207. package/src/useSplitter/types.ts +336 -0
  208. package/src/useSplitter/useSplitter.ts +648 -0
  209. package/src/useSplitter/useSplitterPanel.ts +106 -0
  210. package/src/useSwitch/useSwitch.ts +6 -1
  211. package/src/useTable/index.ts +3 -0
  212. package/src/useTable/types.ts +282 -0
  213. package/src/useTable/useTable.ts +407 -0
  214. package/src/useTabs/types.ts +5 -1
  215. package/src/useTabs/useTabs.ts +30 -6
  216. package/src/useTextarea/index.ts +3 -0
  217. package/src/useTextarea/types.ts +17 -0
  218. package/src/useTextarea/useTextarea.ts +34 -0
  219. package/src/useToast/useToast.ts +43 -26
  220. package/src/useToastViewport/useToastViewport.ts +10 -9
  221. package/src/useTooltip/index.ts +3 -0
  222. package/src/useTooltip/types.ts +72 -0
  223. package/src/useTooltip/useTooltip.ts +240 -0
  224. package/src/useVirtualList/index.ts +8 -0
  225. package/src/useVirtualList/types.ts +95 -0
  226. package/src/useVirtualList/useVirtualList.ts +478 -0
  227. package/src/useVirtualTree/index.ts +3 -0
  228. package/src/useVirtualTree/types.ts +179 -0
  229. package/src/useVirtualTree/useVirtualTree.ts +342 -0
  230. package/src/utils/useFocusTrap.ts +2 -0
  231. package/src/utils/useTextControl.ts +191 -0
@@ -0,0 +1,648 @@
1
+ import { computed, ref, shallowRef, toValue } from 'vue';
2
+
3
+ import {
4
+ getSplitterAvailableSize,
5
+ getSplitterBarControls,
6
+ getSplitterBarStyles,
7
+ getSplitterKeyboardIntent,
8
+ getSplitterPanelStyles,
9
+ getSplitterPointerDelta,
10
+ moveSplitterSpace,
11
+ parseSplitterUnit,
12
+ resizeSplitterPair,
13
+ resolveSplitterSizes,
14
+ } from '@agile-plus/primitives/splitter';
15
+
16
+ import { useCollection } from '../utils/useCollection';
17
+ import { usePointerDrag } from '../utils/usePointerDrag';
18
+ import { useResizeObserver } from '../utils/useResizeObserver';
19
+
20
+ import type { CSSProperties } from 'vue';
21
+
22
+ import type {
23
+ SplitterBarControl,
24
+ SplitterBarStyles,
25
+ SplitterEdge,
26
+ SplitterPanelCollapseState,
27
+ SplitterPanelStyles,
28
+ } from '@agile-plus/primitives/splitter';
29
+
30
+ import type { SplitterPanelEntry, UseSplitterOptions, UseSplitterReturn } from './types';
31
+
32
+ /** 分隔条默认占位:既要留出可点的命中区,又要放得下两个 16px 的折叠箭头。 */
33
+ const DEFAULT_BAR_SIZE = 8;
34
+
35
+ /** 方向键一次移动的像素。 */
36
+ const DEFAULT_RESIZE_STEP = 10;
37
+
38
+ /** 未声明 max 的面板视为不限。 */
39
+ const UNLIMITED = Number.POSITIVE_INFINITY;
40
+
41
+ /** 两次像素比较视为相等的容差:浮点解析与取整会留下亚像素噪声。 */
42
+ const EPSILON = 0.01;
43
+
44
+ /** 一次折叠的还原凭据:让出了多少像素、分别被哪些面板吸收。 */
45
+ interface CollapseMemory {
46
+ /** 折叠时让出的像素 */
47
+ amount: number;
48
+
49
+ /** 吸收空间的面板 id,按接收顺序 */
50
+ donors: string[];
51
+ }
52
+
53
+ /** 一次进行中的拖拽的起点快照。 */
54
+ interface DragSession {
55
+ /** 分隔条键 */
56
+ barId: string;
57
+
58
+ /** 分隔条下标 */
59
+ index: number;
60
+
61
+ /** 按下时的指针坐标 */
62
+ startPointer: number;
63
+
64
+ /** 按下时的全部尺寸,整次拖拽都以它为基准算绝对位移 */
65
+ startSizes: number[];
66
+
67
+ /** lazy 模式下尚未落地的预览尺寸 */
68
+ pending: number[] | null;
69
+ }
70
+
71
+ /**
72
+ * 取指针在排列方向上的坐标。
73
+ *
74
+ * @param event 原生指针事件。
75
+ * @param horizontal 是否横向排列。
76
+ * @returns 该轴上的视口坐标。
77
+ */
78
+ function readPointer(event: PointerEvent, horizontal: boolean): number {
79
+ return horizontal ? event.clientX : event.clientY;
80
+ }
81
+
82
+ /**
83
+ * Splitter 的 Vue Composable 适配。
84
+ *
85
+ * 全部几何、约束与折叠状态都在这一份状态里:面板挂载时把自己的响应式条目登记进来,
86
+ * 根按登记顺序(即 DOM 顺序)算出像素数组,再把结果发回各部件。之所以不拆成第二个
87
+ * composable:一次分隔条操作同时牵动左右两个面板,尺寸真源必须唯一,两处各持一半就会
88
+ * 出现「拖完再解析对不上」的漂移。
89
+ *
90
+ * **作者单位是意图,像素是工作值**:拖拽、折叠、键盘都把自己的单位改写成像素(与 antd
91
+ * 拖动后 `onResize` 给出像素数组的行为一致),容器尺寸变化时再由这些单位重新解析——
92
+ * 定宽面板保持不动、`%` 面板按比例跟随、自适应面板吸收余量。
93
+ *
94
+ * 拖拽用 window 监听而不是 `setPointerCapture`,与 Slider、ScrollArea 的选择一致:
95
+ * 指针移出控件仍能继续拖,也避开合成事件下 `setPointerCapture` 抛错。
96
+ *
97
+ * @param options 排列方向、分隔条尺寸、lazy 与事件回调。
98
+ * @returns 像素尺寸、各部件属性与样式、折叠与尺寸操作入口。
99
+ */
100
+ export function useSplitter(options: UseSplitterOptions = {}): UseSplitterReturn {
101
+ const layout = computed(() => toValue(options.layout) ?? 'horizontal');
102
+
103
+ const direction = computed(() => toValue(options.dir) ?? 'ltr');
104
+
105
+ const disabled = computed(() => toValue(options.disabled) === true);
106
+
107
+ const lazy = computed(() => toValue(options.lazy) === true);
108
+
109
+ const barSize = computed(() => Math.max(0, toValue(options.barSize) ?? DEFAULT_BAR_SIZE));
110
+
111
+ const resizeStep = computed(() =>
112
+ Math.max(1, toValue(options.resizeStep) ?? DEFAULT_RESIZE_STEP)
113
+ );
114
+
115
+ const panels = useCollection<SplitterPanelEntry>({ key: (entry) => entry.id });
116
+
117
+ const rootElement = ref<HTMLElement | null>(null);
118
+
119
+ const containerSize = ref(0);
120
+
121
+ /** 折叠还原凭据,键是被折叠面板的 id。 */
122
+ const memories = shallowRef<Record<string, CollapseMemory>>({});
123
+
124
+ /** lazy 拖拽期间各分隔条的预览位移,键是分隔条 id。 */
125
+ const previews = shallowRef<Record<string, number>>({});
126
+
127
+ /** 当前正在拖拽的分隔条键,用于 `data-resizing`。 */
128
+ const activeBarId = shallowRef<string | null>(null);
129
+
130
+ let session: DragSession | null = null;
131
+
132
+ const entries = computed(() => panels.items.value);
133
+
134
+ const panelCount = computed(() => entries.value.length);
135
+
136
+ const measured = computed(() => containerSize.value > 0);
137
+
138
+ const available = computed(() =>
139
+ getSplitterAvailableSize(containerSize.value, barSize.value, panelCount.value)
140
+ );
141
+
142
+ /**
143
+ * 是否处于折叠态只看作者单位,不看解析结果。
144
+ *
145
+ * 若反过来用像素判定,`min` 就得为折叠面板放宽到 0,而像素又依赖 `min`——判定会自引用。
146
+ * 写成「单位解析为 0」之后,用户把已折叠的面板拖回非 0 时折叠态自动解除,不必额外记账。
147
+ */
148
+ function isCollapsed(entry: SplitterPanelEntry): boolean {
149
+ return parseSplitterUnit(entry.unit.value, { available: 1 }) === 0;
150
+ }
151
+
152
+ const collapsedFlags = computed(() => entries.value.map((entry) => isCollapsed(entry)));
153
+
154
+ const sizes = computed(() =>
155
+ resolveSplitterSizes({
156
+ available: available.value,
157
+ sizes: entries.value.map((entry) => entry.unit.value),
158
+ // 折叠优先于最小尺寸承诺:否则一个 min 为 200 的面板永远收不起来。
159
+ mins: entries.value.map((entry, index) =>
160
+ collapsedFlags.value[index] ? 0 : entry.min.value
161
+ ),
162
+ maxs: entries.value.map((entry) => entry.max.value),
163
+ })
164
+ );
165
+
166
+ /** 交互原语要的是像素约束,与 `available` 同源解析,百分比才不会各算各的。 */
167
+ function pixelConstraints(): { mins: number[]; maxs: number[] } {
168
+ const context = { available: available.value };
169
+
170
+ return {
171
+ mins: entries.value.map((entry, index) =>
172
+ collapsedFlags.value[index]
173
+ ? 0
174
+ : Math.max(0, parseSplitterUnit(entry.min.value, context) ?? 0)
175
+ ),
176
+ maxs: entries.value.map((entry) =>
177
+ Math.max(0, parseSplitterUnit(entry.max.value, context) ?? UNLIMITED)
178
+ ),
179
+ };
180
+ }
181
+
182
+ /**
183
+ * 读取容器在排列方向上的实际尺寸。
184
+ *
185
+ * 取 client 而不是 offset:可用空间是面板真正能分到的那一块,边框宽度算进去会让
186
+ * 面板总和超出内容盒,末段被根裁掉一条。client 含内距,因此根不该在排列方向上加内距。
187
+ *
188
+ * 只在数值真的变了才写入:面板尺寸反过来影响容器尺寸时(例如宽度 `fit-content`),
189
+ * 无条件写入会让 ResizeObserver 与响应式更新互相触发。
190
+ */
191
+ function measure(): void {
192
+ const element = rootElement.value;
193
+
194
+ if (!element) return;
195
+
196
+ const next = Math.round(
197
+ layout.value === 'horizontal' ? element.clientWidth : element.clientHeight
198
+ );
199
+
200
+ if (next === containerSize.value) return;
201
+
202
+ containerSize.value = next;
203
+ }
204
+
205
+ const resize = useResizeObserver(measure);
206
+
207
+ const drag = usePointerDrag();
208
+
209
+ function getPanelIndex(id: string): number {
210
+ return entries.value.findIndex((entry) => entry.id === id);
211
+ }
212
+
213
+ /**
214
+ * 把像素数组写回作者单位。
215
+ *
216
+ * 只写真正变化的那几项:一次拖拽只牵动相邻两个面板,其余面板不该收到 `update:size`。
217
+ *
218
+ * @param next 目标像素数组。
219
+ * @param indexes 可能变化的面板下标。
220
+ * @returns 是否有面板被写入。
221
+ */
222
+ function commit(next: number[], indexes: number[]): boolean {
223
+ const before = sizes.value;
224
+ let changed = false;
225
+
226
+ for (const index of indexes) {
227
+ const entry = entries.value[index];
228
+ const value = next[index];
229
+
230
+ if (!entry || value === undefined) continue;
231
+ if (Math.abs((before[index] ?? 0) - value) < EPSILON) continue;
232
+
233
+ entry.setUnit(value);
234
+ changed = true;
235
+ }
236
+
237
+ return changed;
238
+ }
239
+
240
+ /** 尺寸写回可能顺带翻转折叠态(把已折叠面板拖回非 0),事件负载因此按前后对比派发。 */
241
+ function emitChanges(previousCollapsed: boolean[], resized: boolean): void {
242
+ if (resized) options.onResize?.([...sizes.value]);
243
+
244
+ const next = collapsedFlags.value;
245
+ const collapsedChanged =
246
+ next.length !== previousCollapsed.length ||
247
+ next.some((flag, index) => flag !== previousCollapsed[index]);
248
+
249
+ if (collapsedChanged) options.onCollapse?.([...next], [...sizes.value]);
250
+ }
251
+
252
+ function isBarUsable(index: number): boolean {
253
+ if (index < 0 || disabled.value) return false;
254
+
255
+ const left = entries.value[index];
256
+ const right = entries.value[index + 1];
257
+
258
+ if (!left || !right) return false;
259
+
260
+ return (
261
+ left.resizable.value && right.resizable.value && !left.disabled.value && !right.disabled.value
262
+ );
263
+ }
264
+
265
+ /**
266
+ * 以基准尺寸算出某条分隔条位移后的尺寸数组。
267
+ *
268
+ * 基准是拖拽起点的快照而不是当前值:逐帧累加会把被钳制掉的位移丢掉,指针回撤时分隔条
269
+ * 跟不上手;按绝对位移算才天然是「回到起点就恢复原状」的语义。
270
+ */
271
+ function resizeBar(index: number, delta: number, base: number[]): number[] {
272
+ const { mins, maxs } = pixelConstraints();
273
+
274
+ return resizeSplitterPair({ sizes: base, index, delta, mins, maxs });
275
+ }
276
+
277
+ /**
278
+ * 按下分隔条,开始一次拖拽。
279
+ *
280
+ * 启动必须在登记会话之前:`drag.start` 会先把上一次拖拽以 `cancel` 收尾,而那一步清掉的
281
+ * 必须是旧会话。顺序反过来时,第二根手指按下会把刚写下的新会话一起抹掉,之后怎么拖都不动。
282
+ */
283
+ function onBarPointerDown(event: PointerEvent, barId: string): void {
284
+ const index = getPanelIndex(barId);
285
+
286
+ if (!isBarUsable(index) || event.button !== 0) return;
287
+
288
+ event.preventDefault();
289
+
290
+ const startPointer = readPointer(event, layout.value === 'horizontal');
291
+ const startSizes = [...sizes.value];
292
+
293
+ drag.start({
294
+ onMove: (moveEvent: PointerEvent): void => {
295
+ const current = session;
296
+
297
+ if (!current) return;
298
+
299
+ const pointer = readPointer(moveEvent, layout.value === 'horizontal');
300
+ const delta = getSplitterPointerDelta(
301
+ pointer,
302
+ current.startPointer,
303
+ layout.value,
304
+ direction.value
305
+ );
306
+ const next = resizeBar(current.index, delta, current.startSizes);
307
+
308
+ if (lazy.value) {
309
+ // 只在 lazy 分支留快照:抬起要提交的是「尚未落地的那一份」。非 lazy 已逐帧写完,
310
+ // 抬起再提交一次会给受控面板重复发一遍 update:size。
311
+ current.pending = next;
312
+
313
+ previews.value = {
314
+ ...previews.value,
315
+ [current.barId]: (next[current.index] ?? 0) - (current.startSizes[current.index] ?? 0),
316
+ };
317
+
318
+ return;
319
+ }
320
+
321
+ current.pending = null;
322
+
323
+ const previous = collapsedFlags.value;
324
+
325
+ emitChanges(previous, commit(next, [current.index, current.index + 1]));
326
+ },
327
+ onEnd: (reason): void => endDrag(reason),
328
+ });
329
+
330
+ session = { barId, index, startPointer, startSizes, pending: null };
331
+
332
+ activeBarId.value = barId;
333
+ options.onResizeStart?.(startSizes);
334
+ }
335
+
336
+ /**
337
+ * 收尾一次拖拽。
338
+ *
339
+ * lazy 的预览只是位移,抬起才写单位;被系统中断(`cancel`)时不写,尺寸回到按下前,
340
+ * 与 Slider 把非 `up` 结束当作取消的判断一致。`onResizeEnd` 只在正常抬起时报——
341
+ * 被中断的那次用户并没有完成调整。
342
+ */
343
+ function endDrag(reason: 'up' | 'cancel'): void {
344
+ const current = session;
345
+
346
+ session = null;
347
+ activeBarId.value = null;
348
+
349
+ if (!current) return;
350
+
351
+ const nextPreviews = { ...previews.value };
352
+
353
+ delete nextPreviews[current.barId];
354
+ previews.value = nextPreviews;
355
+
356
+ if (reason !== 'up') return;
357
+
358
+ const previous = collapsedFlags.value;
359
+ const pending = current.pending;
360
+ const resized = pending !== null && commit(pending, [current.index, current.index + 1]);
361
+
362
+ emitChanges(previous, resized);
363
+ options.onResizeEnd?.([...sizes.value]);
364
+ }
365
+
366
+ function onBarKeydown(event: KeyboardEvent, barId: string): void {
367
+ const index = getPanelIndex(barId);
368
+
369
+ if (!isBarUsable(index)) return;
370
+
371
+ const intent = getSplitterKeyboardIntent({
372
+ key: event.key,
373
+ shiftKey: event.shiftKey,
374
+ layout: layout.value,
375
+ direction: direction.value,
376
+ });
377
+
378
+ if (!intent) return;
379
+
380
+ event.preventDefault();
381
+
382
+ // Home / End 没有「一步」可言:用无穷位移把分隔条直接推到该侧的约束边界。
383
+ const delta =
384
+ intent.type === 'resize'
385
+ ? intent.sign * resizeStep.value * intent.multiplier
386
+ : intent.type === 'max'
387
+ ? Number.POSITIVE_INFINITY
388
+ : Number.NEGATIVE_INFINITY;
389
+
390
+ const previous = collapsedFlags.value;
391
+ const next = resizeBar(index, delta, [...sizes.value]);
392
+
393
+ emitChanges(previous, commit(next, [index, index + 1]));
394
+ }
395
+
396
+ /**
397
+ * 折叠:把该面板的全部尺寸让给这条边外侧的邻居。
398
+ *
399
+ * 外侧邻居吸不完时剩余量交给另一侧(`alt`),两边都吸不完就留成空白——把余量硬塞给
400
+ * 更远的面板会让一次点击牵动整组尺寸。让出的像素与被谁吸收记进凭据,还原时原路要回。
401
+ */
402
+ function collapse(panelIndex: number, edge: SplitterEdge, base: number[]): void {
403
+ const list = entries.value;
404
+ const entry = list[panelIndex];
405
+
406
+ if (!entry) return;
407
+
408
+ const receiver = edge === 'end' ? panelIndex + 1 : panelIndex - 1;
409
+ const alt = edge === 'end' ? panelIndex - 1 : panelIndex + 1;
410
+ const amount = base[panelIndex] ?? 0;
411
+ const { mins, maxs } = pixelConstraints();
412
+ const next = moveSplitterSpace({
413
+ sizes: base,
414
+ from: panelIndex,
415
+ to: receiver,
416
+ alt: list[alt] ? alt : undefined,
417
+ amount,
418
+ mins: mins.map((min, index) => (index === panelIndex ? 0 : min)),
419
+ maxs,
420
+ });
421
+
422
+ commit(next, [panelIndex, receiver, alt]);
423
+
424
+ const donors = [receiver, alt].filter(
425
+ (index) => list[index] && (next[index] ?? 0) > (base[index] ?? 0) + EPSILON
426
+ );
427
+
428
+ memories.value = {
429
+ ...memories.value,
430
+ [entry.id]: {
431
+ amount,
432
+ donors: donors.map((index) => String(list[index]?.id)),
433
+ },
434
+ };
435
+ }
436
+
437
+ /**
438
+ * 还原:从当初吸收空间的面板手里把像素要回来。
439
+ *
440
+ * 没有凭据时(作者直接把尺寸写成 0)退回自适应单位,让面板按剩余空间重新参与分配,
441
+ * 而不是凭空造一个「默认宽度」——那会把使用方写下的 min / max 语义搅浑。
442
+ */
443
+ function expand(panelIndex: number, base: number[]): void {
444
+ const list = entries.value;
445
+ const entry = list[panelIndex];
446
+
447
+ if (!entry) return;
448
+
449
+ const memory = memories.value[entry.id];
450
+ const nextMemories = { ...memories.value };
451
+
452
+ delete nextMemories[entry.id];
453
+ memories.value = nextMemories;
454
+
455
+ if (!memory || memory.amount <= 0) {
456
+ entry.setUnit(undefined);
457
+
458
+ return;
459
+ }
460
+
461
+ const donors = memory.donors
462
+ .map((id) => list.findIndex((item) => item.id === id))
463
+ .filter((index) => index >= 0);
464
+
465
+ if (donors.length === 0) {
466
+ entry.setUnit(memory.amount);
467
+
468
+ return;
469
+ }
470
+
471
+ const { mins, maxs } = pixelConstraints();
472
+ const moved = moveSplitterSpace({
473
+ sizes: base,
474
+ from: donors[0] ?? 0,
475
+ to: panelIndex,
476
+ alt: donors[1],
477
+ amount: memory.amount,
478
+ mins,
479
+ maxs,
480
+ });
481
+
482
+ commit(moved, [panelIndex, ...donors]);
483
+ }
484
+
485
+ function toggleCollapse(panelIndex: number, edge: SplitterEdge): void {
486
+ const entry = entries.value[panelIndex];
487
+
488
+ if (!entry || disabled.value || entry.disabled.value) return;
489
+
490
+ const collapsible = entry.collapsible.value;
491
+
492
+ if (!collapsible) return;
493
+ if (edge === 'start' && !collapsible.start) return;
494
+ if (edge === 'end' && !collapsible.end) return;
495
+
496
+ const previous = collapsedFlags.value;
497
+ const base = [...sizes.value];
498
+
499
+ if (isCollapsed(entry)) expand(panelIndex, base);
500
+ else collapse(panelIndex, edge, base);
501
+
502
+ emitChanges(previous, false);
503
+ }
504
+
505
+ /**
506
+ * 双击复位:把该分隔条两侧的面板单位写回各自初值。
507
+ *
508
+ * 只复位相邻两个而不是整组——一条分隔条的语义范围就是它两侧的面板,顺手改动无关面板
509
+ * 会让这个手势不可预期。复位同时清掉还原凭据,否则残留的像素会把折叠态钉在旧值上。
510
+ */
511
+ function onBarDoubleClick(barId: string): void {
512
+ const index = getPanelIndex(barId);
513
+ const list = entries.value;
514
+ const left = list[index];
515
+ const right = list[index + 1];
516
+
517
+ options.onDraggerDoubleClick?.(index);
518
+
519
+ if (!left || !right || !isBarUsable(index)) return;
520
+
521
+ const previous = collapsedFlags.value;
522
+ const before = [...sizes.value];
523
+
524
+ for (const entry of [left, right]) {
525
+ const nextMemories = { ...memories.value };
526
+
527
+ delete nextMemories[entry.id];
528
+ memories.value = nextMemories;
529
+ entry.setUnit(entry.initialUnit);
530
+ }
531
+
532
+ const resized = sizes.value.some(
533
+ (value, position) => Math.abs(value - (before[position] ?? 0)) >= EPSILON
534
+ );
535
+
536
+ emitChanges(previous, resized);
537
+ }
538
+
539
+ function getBarControls(barId: string): SplitterBarControl[] {
540
+ const states: SplitterPanelCollapseState[] = entries.value.map((entry, position) => ({
541
+ collapsible: entry.collapsible.value,
542
+ collapsed: collapsedFlags.value[position] === true,
543
+ disabled: disabled.value || entry.disabled.value,
544
+ }));
545
+
546
+ return getSplitterBarControls(states, getPanelIndex(barId));
547
+ }
548
+
549
+ function getRootProps(): Record<string, unknown> {
550
+ return {
551
+ 'data-part': 'root',
552
+ 'data-layout': layout.value,
553
+ dir: direction.value,
554
+ 'data-disabled': disabled.value ? '' : undefined,
555
+ 'aria-disabled': disabled.value || undefined,
556
+ 'data-resizing': activeBarId.value ? '' : undefined,
557
+ };
558
+ }
559
+
560
+ function getBarProps(barId: string): Record<string, unknown> {
561
+ const index = getPanelIndex(barId);
562
+ const usable = isBarUsable(index);
563
+ const { mins, maxs } = pixelConstraints();
564
+ const rawMax = maxs[index] ?? UNLIMITED;
565
+
566
+ return {
567
+ role: 'separator',
568
+ // 可聚焦的分隔条必须有名字,否则读屏只念得出 role 而不知道动的是什么。
569
+ 'aria-label': `第 ${index + 1} 条分隔条`,
570
+ // separator 的朝向说的是这条缝本身,不是面板的排列轴:左右分栏的缝是竖着的,
571
+ // 因此 layout='horizontal' 要报 vertical。APG 的按键表用的也是这个口径
572
+ // (「Left Arrow: Moves a vertical splitter to the left」)。
573
+ 'aria-orientation': layout.value === 'horizontal' ? 'vertical' : 'horizontal',
574
+ // 取值语义是「前一个面板的尺寸」:它与 min / max 同源,键盘用户听到的就是可调整的量。
575
+ 'aria-valuemin': Math.round(mins[index] ?? 0),
576
+ 'aria-valuenow': Math.round(sizes.value[index] ?? 0),
577
+ 'aria-valuemax': Math.round(
578
+ Number.isFinite(rawMax) ? Math.min(rawMax, available.value) : available.value
579
+ ),
580
+ 'aria-disabled': usable ? undefined : true,
581
+ tabindex: usable ? 0 : undefined,
582
+ 'data-part': 'bar',
583
+ 'data-index': index,
584
+ 'data-layout': layout.value,
585
+ 'data-disabled': usable ? undefined : '',
586
+ 'data-resizing': activeBarId.value === barId ? '' : undefined,
587
+ };
588
+ }
589
+
590
+ function getBarStyle(barId: string): CSSProperties {
591
+ const preview: SplitterBarStyles = getSplitterBarStyles(
592
+ previews.value[barId] ?? null,
593
+ layout.value
594
+ );
595
+
596
+ return {
597
+ flexGrow: '0',
598
+ flexShrink: '0',
599
+ // 占位宽度由这里写死,几何计算才和实际布局对得上;样式层只负责在里面画线。
600
+ flexBasis: `${barSize.value}px`,
601
+ // 触摸端必须关掉默认手势,否则指针跟手之前浏览器先把页面滚走了。
602
+ touchAction: 'none',
603
+ ...preview,
604
+ };
605
+ }
606
+
607
+ function getPanelStyle(index: number): SplitterPanelStyles {
608
+ return getSplitterPanelStyles(measured.value ? (sizes.value[index] ?? null) : null);
609
+ }
610
+
611
+ function setRootElement(node: unknown): void {
612
+ const element = node instanceof HTMLElement ? node : null;
613
+ const previous = rootElement.value;
614
+
615
+ if (previous && previous !== element) resize.unobserve(previous);
616
+
617
+ rootElement.value = element;
618
+
619
+ if (!element) return;
620
+
621
+ resize.observe(element);
622
+ measure();
623
+ }
624
+
625
+ return {
626
+ layout,
627
+ direction,
628
+ disabled,
629
+ lazy,
630
+ barSize,
631
+ measured,
632
+ panelCount,
633
+ sizes,
634
+ collapsedFlags,
635
+ registerPanel: panels.add,
636
+ getPanelIndex,
637
+ getPanelStyle,
638
+ getBarProps,
639
+ getBarStyle,
640
+ getBarControls,
641
+ getRootProps,
642
+ toggleCollapse,
643
+ setRootElement,
644
+ onBarPointerDown,
645
+ onBarKeydown,
646
+ onBarDoubleClick,
647
+ };
648
+ }