|
| 1 | +<template> |
| 2 | + <div ref="containerRef" class="flex overflow-hidden h-full"> |
| 3 | + <!-- 左侧面板 --> |
| 4 | + <div :style="{ width: `${leftWidth}px` }" class="overflow-hidden"> |
| 5 | + <slot name="left"></slot> |
| 6 | + </div> |
| 7 | + |
| 8 | + <!-- 拖拽分隔条 --> |
| 9 | + <div |
| 10 | + class="relative w-1 bg-gray-200 hover:bg-blue-500 cursor-col-resize transition-colors flex-shrink-0 group" |
| 11 | + @mousedown="startResize" |
| 12 | + @touchstart="startResize"> |
| 13 | + <div class="absolute inset-y-0 -left-1 -right-1"></div> |
| 14 | + <div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 opacity-0 group-hover:opacity-100 transition-opacity"> |
| 15 | + <div class="w-1 h-8 bg-blue-500 rounded-full"></div> |
| 16 | + </div> |
| 17 | + </div> |
| 18 | + |
| 19 | + <!-- 右侧面板 --> |
| 20 | + <div class="flex-1 overflow-hidden"> |
| 21 | + <slot name="right"></slot> |
| 22 | + </div> |
| 23 | + </div> |
| 24 | +</template> |
| 25 | + |
| 26 | +<script setup lang="ts"> |
| 27 | +import { onMounted, onUnmounted, ref } from 'vue' |
| 28 | +
|
| 29 | +interface Props { |
| 30 | + minLeftWidth?: number |
| 31 | + minRightWidth?: number |
| 32 | + defaultLeftWidth?: number |
| 33 | +} |
| 34 | +
|
| 35 | +const props = withDefaults(defineProps<Props>(), { |
| 36 | + minLeftWidth: 300, |
| 37 | + minRightWidth: 300, |
| 38 | + defaultLeftWidth: 0 |
| 39 | +}) |
| 40 | +
|
| 41 | +const containerRef = ref<HTMLElement | null>(null) |
| 42 | +const leftWidth = ref(0) |
| 43 | +const isResizing = ref(false) |
| 44 | +
|
| 45 | +// 初始化左侧宽度 |
| 46 | +onMounted(() => { |
| 47 | + if (containerRef.value) { |
| 48 | + const containerWidth = containerRef.value.clientWidth |
| 49 | + if (props.defaultLeftWidth > 0) { |
| 50 | + leftWidth.value = props.defaultLeftWidth |
| 51 | + } else { |
| 52 | + // 默认左侧占 60% |
| 53 | + leftWidth.value = Math.floor(containerWidth * 0.6) |
| 54 | + } |
| 55 | +
|
| 56 | + // 从 localStorage 读取保存的宽度 |
| 57 | + const savedWidth = localStorage.getItem('resizable-panels-left-width') |
| 58 | + if (savedWidth) { |
| 59 | + const width = parseInt(savedWidth, 10) |
| 60 | + if (width >= props.minLeftWidth && width <= containerWidth - props.minRightWidth) { |
| 61 | + leftWidth.value = width |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + window.addEventListener('resize', handleWindowResize) |
| 67 | +}) |
| 68 | +
|
| 69 | +onUnmounted(() => { |
| 70 | + window.removeEventListener('resize', handleWindowResize) |
| 71 | +}) |
| 72 | +
|
| 73 | +// 窗口大小改变时调整面板宽度 |
| 74 | +const handleWindowResize = () => { |
| 75 | + if (containerRef.value) { |
| 76 | + const containerWidth = containerRef.value.clientWidth |
| 77 | + const maxLeftWidth = containerWidth - props.minRightWidth |
| 78 | +
|
| 79 | + if (leftWidth.value > maxLeftWidth) { |
| 80 | + leftWidth.value = maxLeftWidth |
| 81 | + } else if (leftWidth.value < props.minLeftWidth) { |
| 82 | + leftWidth.value = props.minLeftWidth |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | +
|
| 87 | +// 开始调整大小 |
| 88 | +const startResize = (e: MouseEvent | TouchEvent) => { |
| 89 | + e.preventDefault() |
| 90 | + isResizing.value = true |
| 91 | +
|
| 92 | + // 添加全局事件监听 |
| 93 | + document.addEventListener('mousemove', handleResize) |
| 94 | + document.addEventListener('mouseup', stopResize) |
| 95 | + document.addEventListener('touchmove', handleResize) |
| 96 | + document.addEventListener('touchend', stopResize) |
| 97 | +
|
| 98 | + // 添加选择禁用样式 |
| 99 | + document.body.style.userSelect = 'none' |
| 100 | + document.body.style.cursor = 'col-resize' |
| 101 | +} |
| 102 | +
|
| 103 | +// 调整大小 |
| 104 | +const handleResize = (e: MouseEvent | TouchEvent) => { |
| 105 | + if (!isResizing.value || !containerRef.value) return |
| 106 | +
|
| 107 | + const containerRect = containerRef.value.getBoundingClientRect() |
| 108 | + const clientX = 'touches' in e ? e.touches[0].clientX : e.clientX |
| 109 | +
|
| 110 | + let newLeftWidth = clientX - containerRect.left |
| 111 | +
|
| 112 | + // 限制最小/最大宽度 |
| 113 | + const maxLeftWidth = containerRect.width - props.minRightWidth |
| 114 | + newLeftWidth = Math.max(props.minLeftWidth, Math.min(newLeftWidth, maxLeftWidth)) |
| 115 | +
|
| 116 | + leftWidth.value = newLeftWidth |
| 117 | +} |
| 118 | +
|
| 119 | +// 停止调整大小 |
| 120 | +const stopResize = () => { |
| 121 | + if (isResizing.value) { |
| 122 | + isResizing.value = false |
| 123 | +
|
| 124 | + // 移除全局事件监听 |
| 125 | + document.removeEventListener('mousemove', handleResize) |
| 126 | + document.removeEventListener('mouseup', stopResize) |
| 127 | + document.removeEventListener('touchmove', handleResize) |
| 128 | + document.removeEventListener('touchend', stopResize) |
| 129 | +
|
| 130 | + // 移除选择禁用样式 |
| 131 | + document.body.style.userSelect = '' |
| 132 | + document.body.style.cursor = '' |
| 133 | +
|
| 134 | + // 保存宽度到 localStorage |
| 135 | + localStorage.setItem('resizable-panels-left-width', leftWidth.value.toString()) |
| 136 | + } |
| 137 | +} |
| 138 | +</script> |
0 commit comments