Skip to content

Commit b862b15

Browse files
committed
chore: prettier code
1 parent 08bdc7e commit b862b15

File tree

10 files changed

+153
-125
lines changed

10 files changed

+153
-125
lines changed

lib/src/components/ColorPicker/components/GradientBar.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
<script lang="ts" setup>
3030
import { getHandleValue } from '@/utils/utils'
3131
32-
const { colorState, handleGradient, addPoint, setSelectColorIdx } = inject('colorProvider') as any
32+
const { colorState, handleGradient, addPoint, setSelectColorIdx } = inject(
33+
'colorProvider',
34+
) as any
3335
const leftMultiplyer = (colorState.width - 18) / 100
3436
3537
const backgroundImage = computed(() => {

lib/src/components/ColorPicker/components/Inputs/components/CMYKInputs.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { round } from '@/utils/format'
3131
import { rgb2cmyk, cmykToRgb } from '@/utils/convert'
3232
import { InputType } from '@/enums'
3333
34-
const { colorState, changeColor } = inject('colorProvider') as any
34+
const { colorState, changeColor } = inject('colorProvider') as any
3535
3636
const handleCmyk = (value: any) => {
3737
const { r, g, b } = cmykToRgb(value)

lib/src/components/ColorPicker/components/Inputs/components/HSLInputs.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { InputType } from '@/enums'
2727
2828
const sVal = ref(0)
2929
const lVal = ref(0)
30-
const { colorState, changeColor, tinycolor } = inject('colorProvider') as any
30+
const { colorState, changeColor, tinycolor } = inject('colorProvider') as any
3131
3232
const handleSl = (value: any) => {
3333
const { r, g, b } = tc(value).toRgb()

lib/src/components/ColorPicker/components/Inputs/components/HSVInputs.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { InputType } from '@/enums'
3434
3535
const sVal = ref(0)
3636
const vVal = ref(0)
37-
const { colorState, changeColor } = inject('colorProvider') as any
37+
const { colorState, changeColor } = inject('colorProvider') as any
3838
3939
const handleSV = (value: any) => {
4040
const { r, g, b } = tc(value).toRgb()

lib/src/components/ColorPicker/components/Operation.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ import { config } from '@/constants'
8686
import type { IMode } from '@/interfaces'
8787
8888
const { defaultColor, defaultGradient } = config
89-
const { colorState, isGradient, setInputType, setMode, setValue } = inject('colorProvider') as any
89+
const { colorState, isGradient, setInputType, setMode, setValue } = inject(
90+
'colorProvider',
91+
) as any
9092
9193
// input type
9294
const showInputTypes = ref(false)

lib/src/components/ColorPicker/components/OperationGradient.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ const handleChangeType = debounce(function (type: GradientType) {
7171
type === GradientType.linear && setLinear()
7272
type === GradientType.radial && setRadial()
7373
colorState.gradientColors &&
74-
setValue(createGradientStr(colorState.gradientColors, unref(gradientType), colorState))
74+
setValue(
75+
createGradientStr(
76+
colorState.gradientColors,
77+
unref(gradientType),
78+
colorState,
79+
),
80+
)
7581
}, 250)
7682
7783
// 角度设置

lib/src/components/ColorPicker/components/PickerArea.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ import { computePickerPosition, computeSquareXY } from '@/utils/utils'
3737
import { config } from '@/constants'
3838
import tc from 'tinycolor2'
3939
40-
const { colorState, isGradient, setValue, updateSelectColor } = inject('colorProvider') as any
40+
const { colorState, isGradient, setValue, updateSelectColor } = inject(
41+
'colorProvider',
42+
) as any
4143
const { crossSize } = config
4244
4345
const canvasRef = ref<HTMLCanvasElement | null>(null)

lib/src/components/ColorPicker/components/Preview.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
<script lang="ts" setup>
3636
import { debounce } from 'lodash-es'
3737
38-
const { colorState, isGradient, changeColor, setValue } = inject('colorProvider') as any
38+
const { colorState, isGradient, changeColor, setValue } = inject(
39+
'colorProvider',
40+
) as any
3941
4042
const colorValue = computed(() => {
4143
if (unref(isGradient)) {

lib/src/components/ColorPicker/index.vue

Lines changed: 117 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ import tc from 'tinycolor2'
5555
import { InputType, GradientType, Modes } from '@/enums'
5656
import { cloneDeep } from 'lodash-es'
5757
import { createGradientStr } from '@/utils/color'
58-
import type { IColor, ColorPickerProps, GradientProps, IMode } from '@/interfaces'
58+
import type {
59+
IColor,
60+
ColorPickerProps,
61+
GradientProps,
62+
IMode,
63+
} from '@/interfaces'
5964
6065
const emits = defineEmits(['update:value', 'change'])
6166
// const { init, colorState, isGradient } = useColor()
@@ -106,7 +111,6 @@ const colorState = reactive<ColorPickerProps>({
106111
const gradientType = ref<GradientType>(GradientType.linear)
107112
const tinycolor = ref<typeof tc | null>(null)
108113
109-
110114
const onChange = (val: IColor) => {
111115
emits('update:value', val.color)
112116
emits('change', { ...val })
@@ -129,7 +133,11 @@ const setValue = (color?: string) => {
129133
if (unref(isGradient)) {
130134
colorState.degreesStr = degreeStr
131135
colorState.gradientColors = colors
132-
colorState.gradientColor = createGradientStr(colors, unref(gradientType), colorState)
136+
colorState.gradientColor = createGradientStr(
137+
colors,
138+
unref(gradientType),
139+
colorState,
140+
)
133141
tinycolor.value = tc(
134142
colorState.gradientColors[colorState.gradientColorsIdx!].value,
135143
)
@@ -165,126 +173,126 @@ const setValue = (color?: string) => {
165173
}
166174
167175
const updateSelectColor = (value: string) => {
168-
const colors = colorState.gradientColors || []
169-
const colorValue = colors[colorState.gradientColorsIdx!]
170-
if (!colorValue) return
171-
colorValue.value = value
172-
const newGradStr = createGradientStr(colors, unref(gradientType), colorState)
173-
setValue(newGradStr)
174-
}
175-
176-
const handleGradient = (newColor: string, left?: number) => {
177-
const colors = colorState.gradientColors || []
178-
const colorValue = colors[colorState.gradientColorsIdx!]
179-
if (!colorValue) return
180-
colorValue.left = left ?? colorValue.left
181-
colorValue.value = newColor
182-
const newGradStr = createGradientStr(colors, unref(gradientType), colorState)
183-
setValue(newGradStr)
184-
}
176+
const colors = colorState.gradientColors || []
177+
const colorValue = colors[colorState.gradientColorsIdx!]
178+
if (!colorValue) return
179+
colorValue.value = value
180+
const newGradStr = createGradientStr(colors, unref(gradientType), colorState)
181+
setValue(newGradStr)
182+
}
185183
184+
const handleGradient = (newColor: string, left?: number) => {
185+
const colors = colorState.gradientColors || []
186+
const colorValue = colors[colorState.gradientColorsIdx!]
187+
if (!colorValue) return
188+
colorValue.left = left ?? colorValue.left
189+
colorValue.value = newColor
190+
const newGradStr = createGradientStr(colors, unref(gradientType), colorState)
191+
setValue(newGradStr)
192+
}
186193
187-
const changeColor = (newColor: string) => {
188-
if (unref(isGradient)) {
189-
handleGradient(newColor)
190-
} else {
191-
setValue(newColor)
192-
}
194+
const changeColor = (newColor: string) => {
195+
if (unref(isGradient)) {
196+
handleGradient(newColor)
197+
} else {
198+
setValue(newColor)
193199
}
200+
}
194201
195-
const setHcH = (h: number) => {
196-
if (colorState.hc?.h) {
197-
colorState.hc.h = h
198-
}
202+
const setHcH = (h: number) => {
203+
if (colorState.hc?.h) {
204+
colorState.hc.h = h
199205
}
206+
}
200207
201-
const setSelectColorIdx = (idx: number) => {
202-
colorState.gradientColorsIdx = idx
203-
// warning: Here is update hc, but not need to handle onChange
204-
tinycolor.value = tc(
205-
colorState.gradientColors![colorState.gradientColorsIdx!].value,
206-
)
207-
const rgba = tinycolor.value.toRgb()
208-
const hsv = tinycolor.value.toHsv()
209-
colorState.hc = { ...rgba, ...hsv }
210-
}
208+
const setSelectColorIdx = (idx: number) => {
209+
colorState.gradientColorsIdx = idx
210+
// warning: Here is update hc, but not need to handle onChange
211+
tinycolor.value = tc(
212+
colorState.gradientColors![colorState.gradientColorsIdx!].value,
213+
)
214+
const rgba = tinycolor.value.toRgb()
215+
const hsv = tinycolor.value.toHsv()
216+
colorState.hc = { ...rgba, ...hsv }
217+
}
211218
212-
// 设置inputs的类型
213-
const setInputType = (type: InputType) => {
214-
colorState.inputType = type
215-
}
219+
// 设置inputs的类型
220+
const setInputType = (type: InputType) => {
221+
colorState.inputType = type
222+
}
216223
217-
// 设置线性渐变
218-
const setLinear = () => {
219-
const value = colorState.gradientColor?.split(/,(.+)/)[1]
220-
value && setValue(`linear-gradient(90deg, ${value}`)
221-
gradientType.value = GradientType.linear
222-
}
224+
// 设置线性渐变
225+
const setLinear = () => {
226+
const value = colorState.gradientColor?.split(/,(.+)/)[1]
227+
value && setValue(`linear-gradient(90deg, ${value}`)
228+
gradientType.value = GradientType.linear
229+
}
223230
224-
// 设置径向渐变
225-
const setRadial = () => {
226-
const value = colorState.gradientColor?.split(/,(.+)/)[1]
227-
value && setValue(`radial-gradient(circle, ${value}`)
228-
gradientType.value = GradientType.radial
229-
}
231+
// 设置径向渐变
232+
const setRadial = () => {
233+
const value = colorState.gradientColor?.split(/,(.+)/)[1]
234+
value && setValue(`radial-gradient(circle, ${value}`)
235+
gradientType.value = GradientType.radial
236+
}
230237
231-
// 设置角度
232-
const setDegrees = (val: number) => {
233-
if (gradientType.value !== GradientType.linear)
234-
return console.log(
235-
'Warning: you are updating degrees when the gradient type is not linear. This will change the gradients type which may be undesired',
236-
)
237-
238-
const remaining = colorState.gradientColor?.split(/,(.+)/)[1]
239-
if (!remaining) return
240-
colorState.degrees = val
241-
setValue(
242-
`linear-gradient(${formatInputValues(val, 0, 360)}deg, ${remaining}`,
238+
// 设置角度
239+
const setDegrees = (val: number) => {
240+
if (gradientType.value !== GradientType.linear)
241+
return console.log(
242+
'Warning: you are updating degrees when the gradient type is not linear. This will change the gradients type which may be undesired',
243243
)
244-
}
245244
246-
// 添加渐变点
247-
const addPoint = (left: number) => {
248-
if (!left && left !== 0) {
249-
console.log(
250-
'You did not pass a stop value (left amount) for the new color point so it defaulted to 50',
251-
)
252-
}
253-
const colors = cloneDeep(colorState.gradientColors!)
254-
const curColorValue = colors[colorState.gradientColorsIdx!]
255-
256-
const newColors = [
257-
...colors?.map((c: GradientProps) => ({
258-
...c,
259-
value: low(c),
260-
})),
261-
{ value: curColorValue.value, left: left },
262-
]?.sort((a: any, b: any) => a.left - b.left)
263-
264-
colorState.gradientColorsIdx = newColors.findIndex((i) => i.left === left)
265-
const color = createGradientStr(newColors, unref(gradientType), colorState)
266-
setValue(color)
267-
}
245+
const remaining = colorState.gradientColor?.split(/,(.+)/)[1]
246+
if (!remaining) return
247+
colorState.degrees = val
248+
setValue(`linear-gradient(${formatInputValues(val, 0, 360)}deg, ${remaining}`)
249+
}
268250
269-
// 删除渐变点
270-
const deletePoint = (index?: number) => {
271-
const colors = colorState.gradientColors
272-
if (colors && colors?.length > 2) {
273-
const pointToDelete = index ?? colorState.gradientColorsIdx
274-
const remaining = colors?.filter(
275-
(rc: GradientProps, i: number) => i !== pointToDelete,
276-
)
277-
colorState.gradientColors = cloneDeep(remaining)
278-
279-
const newGradientColor = createGradientStr(remaining, unref(gradientType), colorState)
280-
setValue(newGradientColor)
281-
} else {
282-
console.log(
283-
'A gradient must have atleast two colors, disable your delete button when necessary',
284-
)
285-
}
251+
// 添加渐变点
252+
const addPoint = (left: number) => {
253+
if (!left && left !== 0) {
254+
console.log(
255+
'You did not pass a stop value (left amount) for the new color point so it defaulted to 50',
256+
)
286257
}
258+
const colors = cloneDeep(colorState.gradientColors!)
259+
const curColorValue = colors[colorState.gradientColorsIdx!]
260+
261+
const newColors = [
262+
...colors?.map((c: GradientProps) => ({
263+
...c,
264+
value: low(c),
265+
})),
266+
{ value: curColorValue.value, left: left },
267+
]?.sort((a: any, b: any) => a.left - b.left)
268+
269+
colorState.gradientColorsIdx = newColors.findIndex((i) => i.left === left)
270+
const color = createGradientStr(newColors, unref(gradientType), colorState)
271+
setValue(color)
272+
}
273+
274+
// 删除渐变点
275+
const deletePoint = (index?: number) => {
276+
const colors = colorState.gradientColors
277+
if (colors && colors?.length > 2) {
278+
const pointToDelete = index ?? colorState.gradientColorsIdx
279+
const remaining = colors?.filter(
280+
(rc: GradientProps, i: number) => i !== pointToDelete,
281+
)
282+
colorState.gradientColors = cloneDeep(remaining)
287283
284+
const newGradientColor = createGradientStr(
285+
remaining,
286+
unref(gradientType),
287+
colorState,
288+
)
289+
setValue(newGradientColor)
290+
} else {
291+
console.log(
292+
'A gradient must have atleast two colors, disable your delete button when necessary',
293+
)
294+
}
295+
}
288296
289297
const init = () => {
290298
Object.assign(colorState, props)
@@ -319,7 +327,7 @@ const colorProvider = {
319327
setDegrees,
320328
setSelectColorIdx,
321329
addPoint,
322-
deletePoint
330+
deletePoint,
323331
}
324332
325333
init()

lib/src/utils/color.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ import { GradientProps, ColorPickerProps } from '@/interfaces'
22
import { GradientType } from '@/enums'
33

44
// 生成渐变色string
5-
export function createGradientStr(newColors: GradientProps[], gradientType: GradientType, colorState: ColorPickerProps) {
6-
const sorted = newColors.sort(
7-
(a: GradientProps, b: GradientProps) => a.left! - b.left!,
8-
)
9-
const colorString = sorted?.map((cc: any) => `${cc?.value} ${cc.left}%`)
10-
const newGrade = `${gradientType}-gradient(${`${colorState.degreesStr}`}, ${colorString.join(', ')})`
11-
return newGrade
12-
}
5+
export function createGradientStr(
6+
newColors: GradientProps[],
7+
gradientType: GradientType,
8+
colorState: ColorPickerProps,
9+
) {
10+
const sorted = newColors.sort(
11+
(a: GradientProps, b: GradientProps) => a.left! - b.left!,
12+
)
13+
const colorString = sorted?.map((cc: any) => `${cc?.value} ${cc.left}%`)
14+
const newGrade = `${gradientType}-gradient(${`${colorState.degreesStr}`}, ${colorString.join(
15+
', ',
16+
)})`
17+
return newGrade
18+
}

0 commit comments

Comments
 (0)