Skip to content

Commit e2c23e0

Browse files
author
moninla
committed
refactor: simplify resizing logic in Panel.vue and remove unused resize composable
- Implement direct resizing logic within Panel.vue, eliminating the need for the external resize composable. - Introduce new functions for handling resize events and state management. - Update event listeners to use useEventListener for better cleanup and performance. - Remove the resize composable and its exports from the index file.
1 parent aedd6d8 commit e2c23e0

File tree

3 files changed

+85
-190
lines changed

3 files changed

+85
-190
lines changed

components/Panel.vue

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<script setup lang="ts">
2-
import { onBeforeUnmount } from 'vue'
3-
import { useResizable } from '@/composables'
42
import { useScrollbar } from '@/composables/scrollbar'
53
import { ui } from '@/ui/figma'
64
import { options } from '@/ui/state'
7-
import { useDraggable, useWindowSize, watchDebounced } from '@vueuse/core'
5+
import { useDraggable, useEventListener, useWindowSize, watchDebounced } from '@vueuse/core'
86
97
const panel = useTemplateRef('panel')
108
const header = useTemplateRef('header')
@@ -32,33 +30,89 @@ const { x, y, isDragging } = useDraggable(panel, {
3230
3331
const { width: windowWidth, height: windowHeight } = useWindowSize()
3432
35-
const {
36-
width: panelWidth,
37-
isResizing,
38-
handleRightEdgeResize,
39-
handleLeftEdgeResize,
40-
resetWidth,
41-
isAtMinWidth,
42-
isAtMaxWidth,
43-
cleanup
44-
} = useResizable({
45-
min: ui.tempadPanelWidth,
46-
max: ui.tempadPanelMaxWidth,
47-
defaultWidth: ui.tempadPanelWidth,
48-
initialWidth: position?.width,
49-
onPositionChange: (positionDelta) => {
33+
const panelWidth = ref(position?.width ?? ui.tempadPanelWidth)
34+
const isResizing = ref(false)
35+
36+
// no need reactive state, directly using variables
37+
let resizeState: {
38+
direction: 'left' | 'right'
39+
startX: number
40+
startWidth: number
41+
target: HTMLElement
42+
pointerId: number
43+
} | null = null
44+
45+
function clampWidth(value: number): number {
46+
return Math.max(ui.tempadPanelWidth, Math.min(ui.tempadPanelMaxWidth, value))
47+
}
48+
49+
function startResize(e: PointerEvent, direction: 'left' | 'right') {
50+
e.preventDefault()
51+
e.stopPropagation()
52+
53+
const target = e.currentTarget as HTMLElement
54+
if (!target) return
55+
56+
target.setPointerCapture(e.pointerId)
57+
isResizing.value = true
58+
59+
resizeState = {
60+
direction,
61+
startX: e.clientX,
62+
startWidth: panelWidth.value,
63+
target,
64+
pointerId: e.pointerId
65+
}
66+
}
67+
68+
function onPointerMove(e: PointerEvent) {
69+
if (!isResizing.value || !resizeState) return
70+
71+
if (e.buttons === 0) {
72+
endResize(e)
73+
return
74+
}
75+
76+
const deltaX = e.clientX - resizeState.startX
77+
const newWidth =
78+
resizeState.direction === 'right'
79+
? clampWidth(resizeState.startWidth + deltaX)
80+
: clampWidth(resizeState.startWidth - deltaX)
81+
82+
if (resizeState.direction === 'left') {
83+
const positionDelta = panelWidth.value - newWidth
5084
x.value += positionDelta
51-
},
52-
onResizeEnd: (width) => {
53-
if (position) {
54-
position.width = width
55-
}
5685
}
57-
})
5886
59-
onBeforeUnmount(() => {
60-
cleanup()
61-
})
87+
panelWidth.value = newWidth
88+
}
89+
90+
function endResize(e: PointerEvent) {
91+
if (!isResizing.value || !resizeState) return
92+
93+
isResizing.value = false
94+
resizeState.target.releasePointerCapture(resizeState.pointerId)
95+
96+
if (position) {
97+
position.width = panelWidth.value
98+
}
99+
100+
resizeState = null
101+
}
102+
103+
function resetWidth() {
104+
panelWidth.value = ui.tempadPanelWidth
105+
if (position) {
106+
delete position.width
107+
}
108+
}
109+
110+
useEventListener('pointermove', onPointerMove)
111+
useEventListener('pointerup', endResize)
112+
useEventListener('pointercancel', endResize)
113+
114+
const isAtMinWidth = computed(() => panelWidth.value <= ui.tempadPanelWidth)
115+
const isAtMaxWidth = computed(() => panelWidth.value >= ui.tempadPanelMaxWidth)
62116
63117
const restrictedPosition = computed(() => {
64118
if (!panel.value || !header.value) {
@@ -105,13 +159,6 @@ function toggleMinimized() {
105159
options.value.minimized = !options.value.minimized
106160
}
107161
108-
function onResizeHandleDoubleClick() {
109-
if (position) {
110-
delete position.width
111-
}
112-
resetWidth()
113-
}
114-
115162
const leftHandleCursor = computed(() => {
116163
if (isAtMaxWidth.value) return 'e-resize'
117164
if (isAtMinWidth.value) return 'w-resize'
@@ -140,13 +187,13 @@ const resizingCursor = 'ew-resize'
140187
>
141188
<div
142189
class="tp-panel-resize-handle tp-panel-resize-handle-left"
143-
@pointerdown="handleLeftEdgeResize"
144-
@dblclick="onResizeHandleDoubleClick"
190+
@pointerdown="startResize($event, 'left')"
191+
@dblclick="resetWidth"
145192
/>
146193
<div
147194
class="tp-panel-resize-handle tp-panel-resize-handle-right"
148-
@pointerdown="handleRightEdgeResize"
149-
@dblclick="onResizeHandleDoubleClick"
195+
@pointerdown="startResize($event, 'right')"
196+
@dblclick="resetWidth"
150197
/>
151198
<header ref="header" class="tp-row tp-row-justify tp-panel-header" @dblclick="toggleMinimized">
152199
<slot name="header" />

composables/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './copy'
22
export * from './input'
33
export * from './key-lock'
4-
export * from './resize'
54
export * from './selection'
65
export * from './toast'

composables/resize.ts

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)