Skip to content

Commit

Permalink
fix: number drag initial
Browse files Browse the repository at this point in the history
  • Loading branch information
YunYouJun committed Nov 24, 2023
1 parent ad308de commit c1648c7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
5 changes: 1 addition & 4 deletions components/BNumberField/BNumberField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ function onUp() {
}
const vNumberDrag = numberDrag({
value: props.modelValue,
step: props.step,
min: props.min,
max: props.max,
props,
onChange,
onClick,
onDown,
Expand Down
3 changes: 1 addition & 2 deletions components/BSelectMenu/BSelectMenu.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { computed, nextTick, onMounted, ref, watch } from 'vue'
import { computed, nextTick, ref } from 'vue'
import Option from './BSelectOption.vue'
Expand Down Expand Up @@ -28,7 +28,6 @@ let timer: number | undefined
// 选择选项的逻辑
function select(next: OptionType) {
console.log('select', next)
const newValue = typeof next === 'string' ? next : next.value
emit('change', newValue)
emit('update:modelValue', newValue)
Expand Down
5 changes: 5 additions & 0 deletions components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Components

## Todo

- [ ] Vector
35 changes: 18 additions & 17 deletions components/actions/numberDrag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type { Directive } from 'vue'
let pointerLockSupported = true

export interface Config {
value?: number
step?: number
min?: number
max?: number
props: {
modelValue: number
step?: number
min?: number
max?: number
}
onChange: (value: number) => void
onClick?: (e: MouseEvent) => void
onDown?: (e: MouseEvent) => void
Expand All @@ -23,15 +25,17 @@ export default function numberDrag(config: Config): Directive {
}
| undefined

const { props } = config

function onMousedown(e: MouseEvent) {
if (!config.step)
if (!props.step)
return

config.onDown?.(e)
if (typeof config.value === 'number') {
if (typeof props.modelValue === 'number') {
started = {
moved: 0,
value: config.value,
value: props.modelValue,
ts: Date.now(),
}
document.addEventListener('mousemove', onMousemove)
Expand All @@ -56,10 +60,7 @@ export default function numberDrag(config: Config): Directive {
}

function onMousemove(e: MouseEvent) {
console.log('onMousemove')
console.log(config)
console.log(config.value)
if (!started || !config.step)
if (!started || !props.step)
return

if (started.moved === 0) {
Expand All @@ -70,19 +71,19 @@ export default function numberDrag(config: Config): Directive {
started.moved += e.movementX
}

const mouseStep = e.shiftKey ? config.step / 20 : config.step / 2
const mouseStep = e.shiftKey ? props.step / 20 : props.step / 2
const offset = started.moved * mouseStep
let value = started.value + offset
if (e.ctrlKey) {
const rest = value % (config.step * (e.shiftKey ? 1 : 10))
const rest = value % (props.step * (e.shiftKey ? 1 : 10))
value -= rest
}

if (typeof config.min === 'number' && value < config.min)
value = config.min
if (typeof props.min === 'number' && value < props.min)
value = props.min

if (typeof config.max === 'number' && value > config.max)
value = config.max
if (typeof props.max === 'number' && value > props.max)
value = props.max

config.onChange(value)
}
Expand Down

0 comments on commit c1648c7

Please sign in to comment.