Skip to content

Commit d448546

Browse files
GaoNeng-wWwkagol
authored andcommitted
refactor: extract api,state,event init to index.ts
1 parent be334c5 commit d448546

File tree

2 files changed

+69
-129
lines changed

2 files changed

+69
-129
lines changed
Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,76 @@
1-
import type { IColorSelectPanelRef } from '@/types'
1+
import type {
2+
IColorSelectPanelHueProps,
3+
IColorSelectPanelRef,
4+
ISharedRenderlessParamHooks,
5+
ISharedRenderlessParamUtils
6+
} from '@/types'
27
import type { Color } from '../utils/color'
8+
import { getClientXY } from '../utils/getClientXY'
39

4-
export const setPosition = (el: HTMLElement, x: number, y: number) => {
5-
el.style.top = `${y}px`
6-
el.style.left = `${x}px`
10+
interface DomInit {
11+
thumb: IColorSelectPanelRef<HTMLElement | undefined>
12+
bar: IColorSelectPanelRef<HTMLElement | undefined>
13+
wrapper: IColorSelectPanelRef<HTMLElement | undefined>
714
}
8-
export const getXBySaturation = (s: number, width: number) => (s * width) / 100
9-
export const getYByLight = (l: number, height: number) => ((100 - l) * height) / 100
10-
export const updatePosition = (
11-
event: MouseEvent | TouchEvent,
12-
rect: DOMRect,
13-
cursor: IColorSelectPanelRef<HTMLElement>
14-
) => {
15-
let x = (event as MouseEvent).clientX - rect.left
16-
let y = (event as MouseEvent).clientY - rect.top
17-
x = Math.max(0, x)
18-
x = Math.min(x, rect.width)
19-
y = Math.max(0, y)
20-
y = Math.min(y, rect.height)
2115

22-
setPosition(cursor.value, x - (1 / 2) * cursor.value.offsetWidth, y - (1 / 2) * cursor.value.offsetWidth)
23-
return { x, y }
24-
}
25-
export const calcSaturation = (x: number, width: number) => x / width
26-
export const calcBrightness = (y: number, height: number) => 100 - (y / height) * 100
27-
export const getThumbTop = (wrapper: HTMLElement, thumb: HTMLElement, hue: number) => {
28-
return Math.round((hue * (wrapper.offsetHeight - thumb.offsetHeight / 2)) / 360)
29-
}
30-
31-
export const resetCursor = (
32-
s: number,
33-
v: number,
34-
wrapper: IColorSelectPanelRef<HTMLElement>,
35-
cursor: IColorSelectPanelRef<HTMLElement>,
36-
thumb: IColorSelectPanelRef<HTMLElement>,
37-
color: Color,
38-
h: IColorSelectPanelRef<number>
16+
export const initState = (
17+
props: IColorSelectPanelHueProps<Color>,
18+
{ reactive, ref, computed }: ISharedRenderlessParamHooks
3919
) => {
40-
const { width, height } = wrapper.value.getBoundingClientRect()
41-
const x = getXBySaturation(s, width) - (1 / 2) * cursor.value.offsetWidth
42-
const y = getYByLight(v, height) - (1 / 2) * cursor.value.offsetWidth
43-
setPosition(cursor.value, x < 0 ? 0 : x, y < 0 ? 0 : y)
44-
const thummbTop = getThumbTop(wrapper.value, thumb.value, color.get('h'))
45-
thumb.value.style.top = `${thummbTop}px`
46-
h.value = color.get('h')
20+
const hueValue = computed(() => props.color.get('hue'))
21+
const thumbTop = ref(0)
22+
const state = reactive({ hueValue, thumbTop })
23+
return state
4724
}
4825

49-
export const updateCursor = (
50-
wrapper: IColorSelectPanelRef<HTMLElement>,
51-
cursor: IColorSelectPanelRef<HTMLElement>,
52-
emit
53-
) => {
54-
return (color: Color, event: MouseEvent) => {
55-
const rect = wrapper.value.getBoundingClientRect()
56-
const { x, y } = updatePosition(event, rect, cursor)
57-
color.set({
58-
s: calcSaturation(x, rect.width) * 100,
59-
v: calcBrightness(y, rect.height),
60-
h: color.get('h')
61-
})
62-
emit('sv-update', {
63-
s: color.get('s'),
64-
v: color.get('v'),
65-
h: color.get('h')
66-
})
26+
export const initDom = ({ ref }: ISharedRenderlessParamHooks): DomInit => {
27+
return {
28+
thumb: ref<HTMLElement>(),
29+
bar: ref<HTMLElement>(),
30+
wrapper: ref<HTMLElement>()
6731
}
6832
}
6933

70-
export const updateThumb = (
71-
bar: IColorSelectPanelRef<HTMLElement>,
72-
thumb: IColorSelectPanelRef<HTMLElement>,
73-
h: IColorSelectPanelRef<Number>,
74-
emit
34+
export const useEvent = (
35+
{ thumb, bar }: DomInit,
36+
state: ReturnType<typeof initState>,
37+
props: IColorSelectPanelHueProps<Color>,
38+
{ emit }: ISharedRenderlessParamUtils
7539
) => {
76-
return (event: MouseEvent) => {
77-
const e = event as MouseEvent
78-
const rect = bar.value.getBoundingClientRect()
79-
let top = e.clientY - rect.top
40+
const onSvReady = (update) => {
41+
emit('svReady', update)
42+
}
43+
const getThumbTop = () => {
44+
if (!thumb.value) {
45+
return 0
46+
}
47+
const hue = props.color.get('hue')
48+
if (!bar.value) {
49+
return 0
50+
}
51+
return (hue * (bar.value.offsetHeight - thumb.value.offsetHeight / 2)) / 360
52+
}
53+
const update = () => {
54+
state.thumbTop = getThumbTop()
55+
}
56+
const onDrag = (event: MouseEvent | TouchEvent) => {
57+
if (!bar.value || !thumb.value) {
58+
return
59+
}
60+
const el = bar.value
61+
if (!el) {
62+
return
63+
}
64+
const rect = el?.getBoundingClientRect()
65+
const { clientY } = getClientXY(event)
66+
let top = clientY - rect.top
67+
8068
top = Math.min(top, rect.height - thumb.value.offsetHeight / 2)
8169
top = Math.max(thumb.value.offsetHeight / 2, top)
82-
thumb.value.style.top = `${top}px`
83-
h.value = Math.round(((top - thumb.value.offsetHeight / 2) / (rect.height - thumb.value.offsetHeight)) * 360)
84-
emit('hue-update', h.value)
70+
const hue = Math.round(((top - thumb.value.offsetHeight / 2) / (rect.height - thumb.value.offsetHeight)) * 360)
71+
state.thumbTop = top
72+
emit('hueUpdate', hue)
73+
props.color.set('hue', hue)
8574
}
75+
return { update, onDrag, onSvReady }
8676
}

packages/renderless/src/color-select-panel/hue-select/vue.ts

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,27 @@
11
import type { IColorSelectPanelHueProps, ISharedRenderlessParamHooks, ISharedRenderlessParamUtils } from '@/types'
22
import type { Color } from '../utils/color'
3-
import { getClientXY } from '../utils/getClientXY'
43
import { draggable } from '../utils/use-drag'
4+
import { initDom, initState, useEvent } from '.'
55

66
export const api = ['state', 'onSvReady', 'bar', 'thumb', 'wrapper']
77

88
export const renderless = (
99
props: IColorSelectPanelHueProps<Color>,
10-
{ onMounted, reactive, ref, computed }: ISharedRenderlessParamHooks,
11-
{ emit }: ISharedRenderlessParamUtils
10+
hooks: ISharedRenderlessParamHooks,
11+
utils: ISharedRenderlessParamUtils
1212
) => {
13-
const thumb = ref<HTMLElement>()
14-
const bar = ref<HTMLElement>()
15-
const wrapper = ref<HTMLElement>()
16-
const cursor = ref<HTMLElement>()
17-
18-
const thumbLeft = ref(0)
19-
const thumbTop = ref(0)
20-
const hueValue = computed(() => props.color.get('hue'))
21-
const state = reactive({ hueValue, thumb, bar, wrapper, thumbTop })
22-
const onSvReady = (update) => {
23-
emit('svReady', update)
24-
}
13+
const { onMounted } = hooks
14+
const { emit } = utils
15+
const { thumb, bar, wrapper } = initDom(hooks)
16+
const state = initState(props, hooks)
17+
const { onSvReady, onDrag, update } = useEvent({ thumb, bar, wrapper }, state, props, utils)
2518
const api = {
2619
state,
2720
onSvReady,
2821
bar,
2922
thumb,
3023
wrapper
3124
}
32-
const getThumbLeft = () => {
33-
if (!thumb.value) {
34-
return 0
35-
}
36-
const hue = props.color.get('hue')
37-
if (!wrapper.value) {
38-
return 0
39-
}
40-
return Math.round((hue * (wrapper.value.offsetWidth - thumb.value.offsetWidth / 2)) / 360)
41-
}
42-
const getThumbTop = () => {
43-
if (!thumb.value) {
44-
return 0
45-
}
46-
const hue = props.color.get('hue')
47-
if (!bar.value) {
48-
return 0
49-
}
50-
return (hue * (bar.value.offsetHeight - thumb.value.offsetHeight / 2)) / 360
51-
}
52-
const update = () => {
53-
thumbLeft.value = getThumbLeft()
54-
thumbTop.value = getThumbTop()
55-
}
56-
const onDrag = (event: MouseEvent | TouchEvent) => {
57-
if (!bar.value || !thumb.value) {
58-
return
59-
}
60-
const el = bar.value
61-
if (!el) {
62-
return
63-
}
64-
const rect = el?.getBoundingClientRect()
65-
const { clientY } = getClientXY(event)
66-
let top = clientY - rect.top
67-
68-
top = Math.min(top, rect.height - thumb.value.offsetHeight / 2)
69-
top = Math.max(thumb.value.offsetHeight / 2, top)
70-
const hue = Math.round(((top - thumb.value.offsetHeight / 2) / (rect.height - thumb.value.offsetHeight)) * 360)
71-
thumbTop.value = top
72-
emit('hueUpdate', hue)
73-
props.color.set('hue', hue)
74-
}
7525
onMounted(() => {
7626
if (!bar.value || !thumb.value) {
7727
return

0 commit comments

Comments
 (0)