File size: 5,168 Bytes
4d70170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
<script lang="ts">
import { computed, defineComponent, onUnmounted, ref } from 'vue'
export default defineComponent({
props: {
start: {
type: Number,
required: true,
},
end: {
type: Number,
required: true,
},
min: {
type: Number,
required: true,
},
max: {
type: Number,
required: true,
},
},
emits: ['update:start', 'update:end'],
setup(props, { emit }) {
const startRatio = computed(() => (props.start - props.min) / (props.max - props.min))
const endRatio = computed(() => (props.max - props.end) / (props.max - props.min))
const el = ref(null)
let mouseStartX: number, initialValue: number
// Main bar
const moving = ref(false)
function onMainBarMouseDown(event: MouseEvent) {
mouseStartX = event.clientX
initialValue = props.start
moving.value = true
window.addEventListener('mousemove', onMainBarMouseMove)
window.addEventListener('mouseup', onMainBarMouseUp)
}
function onMainBarMouseMove(event: MouseEvent) {
let start = props.start
const size = props.end - props.start
start = initialValue + (event.clientX - mouseStartX) / el.value.offsetWidth * (props.max - props.min)
if (start < props.min) {
start = props.min
}
else if (start > props.max - size) {
start = props.max - size
}
emit('update:start', Math.round(start))
emit('update:end', Math.round(start + size))
}
function onMainBarMouseUp() {
moving.value = false
removeMainBarEvents()
}
function removeMainBarEvents() {
window.removeEventListener('mousemove', onMainBarMouseMove)
window.removeEventListener('mouseup', onMainBarMouseUp)
}
onUnmounted(() => {
removeMainBarEvents()
})
// Start resize handle
function onStartHandleMouseDown(event: MouseEvent) {
mouseStartX = event.clientX
initialValue = props.start
window.addEventListener('mousemove', onStartHandleMouseMove)
window.addEventListener('mouseup', onStartHandleMouseUp)
}
function onStartHandleMouseMove(event: MouseEvent) {
let start = props.start
start = initialValue + (event.clientX - mouseStartX) / el.value.offsetWidth * (props.max - props.min)
if (start < props.min) {
start = props.min
}
else if (start > props.end - 1) {
start = props.end - 1
}
emit('update:start', Math.round(start))
}
function onStartHandleMouseUp() {
removeStartHandleEvents()
}
function removeStartHandleEvents() {
window.removeEventListener('mousemove', onStartHandleMouseMove)
window.removeEventListener('mouseup', onStartHandleMouseUp)
}
onUnmounted(() => {
removeStartHandleEvents()
})
// End resize handle
function onEndHandleMouseDown(event: MouseEvent) {
mouseStartX = event.clientX
initialValue = props.end
window.addEventListener('mousemove', onEndHandleMouseMove)
window.addEventListener('mouseup', onEndHandleMouseUp)
}
function onEndHandleMouseMove(event: MouseEvent) {
let end = props.end
end = initialValue + (event.clientX - mouseStartX) / el.value.offsetWidth * (props.max - props.min)
if (end < props.start + 1) {
end = props.start + 1
}
else if (end > props.max) {
end = props.max
}
emit('update:end', Math.round(end))
}
function onEndHandleMouseUp() {
removeEndHandleEvents()
}
function removeEndHandleEvents() {
window.removeEventListener('mousemove', onEndHandleMouseMove)
window.removeEventListener('mouseup', onEndHandleMouseUp)
}
onUnmounted(() => {
removeStartHandleEvents()
})
return {
el,
startRatio,
endRatio,
onMainBarMouseDown,
moving,
onStartHandleMouseDown,
onEndHandleMouseDown,
}
},
})
</script>
<template>
<div
ref="el"
class="h-6 bg-gray-200 dark:bg-gray-900 rounded relative select-none"
>
<!-- Main Bar -->
<div
class="absolute h-full top-0 bg-green-200 dark:bg-green-900 hover:bg-green-100 dark:hover:bg-green-800 cursor-move"
:class="{
'bg-green-100 dark:bg-green-800': moving,
}"
:style="{
left: `calc(${(start - min) / (max - min) * 100}% - 1px)`,
width: `calc(${(end - start) / (max - min) * 100}% + 1px)`,
}"
@mousedown="onMainBarMouseDown"
/>
<!-- Start resize handle -->
<div
class="absolute h-full rounded top-0 bg-green-300 dark:bg-green-700 cursor-ew-resize"
:style="{
left: `calc(${startRatio * 100}% - ${startRatio < 0.05 ? 0 : 4}px)`,
width: '4px',
}"
@mousedown="onStartHandleMouseDown"
/>
<!-- End resize handle -->
<div
class="absolute h-full rounded top-0 bg-green-300 dark:bg-green-700 cursor-ew-resize"
:style="{
right: `calc(${endRatio * 100}% - ${endRatio < 0.05 ? 0 : 4}px)`,
width: '4px',
}"
@mousedown="onEndHandleMouseDown"
/>
</div>
</template>
|