Line data Source code
1 : import { StorageSerializers, useStorage } from '@vueuse/core'
2 : import type { SplitterPanel } from 'reka-ui'
3 : import { computed, nextTick, shallowRef, toValue, watch } from 'vue'
4 : import type { MaybeRefOrGetter } from 'vue'
5 :
6 : interface SidePanelSizingConfig {
7 : id: string
8 : storageKey: MaybeRefOrGetter<string>
9 : visible: MaybeRefOrGetter<boolean>
10 : minWidth: MaybeRefOrGetter<number>
11 : defaultWidth: () => number
12 : }
13 :
14 2444 : export function usePanelSizing(
15 : panelConfigs: readonly [SidePanelSizingConfig, SidePanelSizingConfig],
16 : containerWidth: MaybeRefOrGetter<number>,
17 : reservedWidth: MaybeRefOrGetter<number>
18 : ) {
19 4888 : function storePanel(config: SidePanelSizingConfig) {
20 4888 : const width = useStorage<number | null>(
21 : config.storageKey,
22 : null,
23 : undefined,
24 : {
25 : serializer: StorageSerializers.number
26 : }
27 : )
28 4888 : watch(
29 : [width, () => toValue(config.visible)],
30 : () => {
31 5992 : if (!toValue(config.visible)) return
32 5992 : if (
33 : width.value === null ||
34 : !Number.isFinite(width.value) ||
35 : width.value <= 0
36 : ) {
37 551 : width.value = config.defaultWidth()
38 : }
39 : },
40 : { immediate: true }
41 : )
42 4888 : return { ...config, width }
43 : }
44 2444 : const storedPanels: readonly [
45 : ReturnType<typeof storePanel>,
46 : ReturnType<typeof storePanel>
47 : ] = [storePanel(panelConfigs[0]), storePanel(panelConfigs[1])]
48 :
49 2444 : const panelPercentages = computed<readonly [number, number, number]>(() => {
50 5664 : const total = toValue(containerWidth)
51 5664 : if (total <= 0) return [20, 60, 20]
52 5664 : const minimums: readonly [number, number] = [
53 : toValue(storedPanels[0].visible) ? toValue(storedPanels[0].minWidth) : 0,
54 : toValue(storedPanels[1].visible) ? toValue(storedPanels[1].minWidth) : 0
55 : ]
56 5664 : const excesses: readonly [number, number] = [
57 : toValue(storedPanels[0].visible)
58 : ? Math.max(
59 : 0,
60 : (storedPanels[0].width.value ?? storedPanels[0].defaultWidth()) -
61 : minimums[0]
62 : )
63 : : 0,
64 : toValue(storedPanels[1].visible)
65 : ? Math.max(
66 : 0,
67 : (storedPanels[1].width.value ?? storedPanels[1].defaultWidth()) -
68 : minimums[1]
69 : )
70 : : 0
71 : ]
72 5664 : const excess = excesses[0] + excesses[1]
73 5664 : const available = Math.max(
74 : 0,
75 : total - toValue(reservedWidth) - minimums[0] - minimums[1]
76 : )
77 5664 : const scale = excess > 0 ? Math.min(1, available / excess) : 0
78 5664 : const first = ((minimums[0] + excesses[0] * scale) / total) * 100
79 5664 : const last = ((minimums[1] + excesses[1] * scale) / total) * 100
80 5664 : return [first, 100 - first - last, last]
81 : })
82 :
83 2444 : const layoutKey = computed(() =>
84 : storedPanels
85 : .map((panel) => `${toValue(panel.storageKey)}:${toValue(panel.visible)}`)
86 : .join(':')
87 : )
88 2444 : const panelRefs = {
89 : first: shallowRef<InstanceType<typeof SplitterPanel>>(),
90 : last: shallowRef<InstanceType<typeof SplitterPanel>>()
91 : }
92 2444 : watch(
93 : [() => toValue(containerWidth), layoutKey, panelRefs.first, panelRefs.last],
94 : async () => {
95 3422 : await nextTick()
96 3422 : const updates = [panelRefs.first.value, panelRefs.last.value].flatMap(
97 : (splitterPanel, index) => {
98 6844 : if (!splitterPanel || !toValue(storedPanels[index].visible)) return []
99 690 : const size = panelPercentages.value[index * 2]
100 690 : return [
101 : { splitterPanel, size, delta: size - splitterPanel.getSize() }
102 : ]
103 : }
104 : )
105 3422 : for (const { splitterPanel, size } of updates.sort(
106 : (a, b) => a.delta - b.delta
107 : )) {
108 690 : splitterPanel.resize(size)
109 : }
110 : },
111 : { flush: 'post' }
112 : )
113 2444 : let gesture:
114 : | Array<{
115 : index: number
116 : element: HTMLElement
117 : width: number
118 : key: string
119 : }>
120 : | undefined
121 :
122 17 : function capturePanel(panelId: string) {
123 17 : if (gesture?.some(({ element }) => !element.isConnected))
124 0 : gesture = undefined
125 17 : if (gesture) return
126 17 : if (!storedPanels.some((panel) => panel.id === panelId)) return
127 17 : gesture = storedPanels.flatMap((panel, index) => {
128 34 : if (!toValue(panel.visible)) return []
129 16 : const element = document.querySelector(`[data-panel-id="${panel.id}"]`)
130 30 : if (!(element instanceof HTMLElement)) return []
131 16 : return [
132 : {
133 : index,
134 : element,
135 : width: element.getBoundingClientRect().width,
136 : key: toValue(panel.storageKey)
137 : }
138 : ]
139 : })
140 : }
141 :
142 77 : function onResizeStart(event: Event) {
143 77 : const handle =
144 : event.target instanceof Element
145 : ? event.target.closest(
146 : '[data-panel-resize-handle-id][data-orientation="horizontal"]'
147 : )
148 : : null
149 77 : if (!handle) return
150 0 : const adjacent = [handle.previousElementSibling, handle.nextElementSibling]
151 0 : const panel = storedPanels.find(({ id }) =>
152 : adjacent.some((element) => element?.getAttribute('data-panel-id') === id)
153 : )
154 0 : if (panel) capturePanel(panel.id)
155 : }
156 :
157 34 : function onResizeDragging(dragging: boolean, panelId: string) {
158 34 : if (dragging) capturePanel(panelId)
159 17 : else onResizeEnd()
160 : }
161 :
162 916 : function onResizeEnd() {
163 916 : if (!gesture) return
164 20 : for (const captured of gesture) {
165 16 : const panel = storedPanels[captured.index]
166 16 : const width = captured.element.getBoundingClientRect().width
167 16 : if (
168 : captured.element.isConnected &&
169 : width > 0 &&
170 : width !== captured.width &&
171 : toValue(panel.storageKey) === captured.key
172 : ) {
173 16 : panel.width.value = width
174 : }
175 : }
176 17 : gesture = undefined
177 : }
178 :
179 2444 : return {
180 : panelPercentages,
181 : panelRefs,
182 : onResizeStart,
183 : onResizeDragging,
184 : onResizeEnd
185 : }
186 : }
|