Skip to content

Commit d15f8a5

Browse files
committed
chroe: add call api of components and optimization of code
1 parent f41a16e commit d15f8a5

File tree

27 files changed

+734
-287
lines changed

27 files changed

+734
-287
lines changed

README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ interface ClickConfig {
5959
showTheme?: boolean;
6060
title?: string;
6161
buttonText?: string;
62+
iconSize?: number;
63+
dotSize?: number;
6264
}
6365

6466
// data = {}
@@ -72,7 +74,15 @@ interface ClickEvents {
7274
click?: (x: number, y: number) => void;
7375
refresh?: () => void;
7476
close?: () => void;
75-
confirm?: (dots: Array<ClickDot>) => boolean;
77+
confirm?: (dots: Array<ClickDot>, reset:() => void) => void;
78+
}
79+
80+
// component call method
81+
interface ClickExpose {
82+
reset: Function,
83+
clear: Function,
84+
refresh: Function,
85+
close: Function,
7686
}
7787
```
7888

@@ -102,6 +112,7 @@ interface SlideConfig {
102112
horizontalPadding?: number;
103113
showTheme?: boolean;
104114
title?: string;
115+
scope ?: boolean;
105116
}
106117

107118
// data = {}
@@ -119,7 +130,15 @@ interface SlideEvents {
119130
move?: (x: number, y: number) => void;
120131
refresh?: () => void;
121132
close?: () => void;
122-
confirm?: (point: SlidePoint) => boolean;
133+
confirm?: (point: SlidePoint, reset:() => void) => void;
134+
}
135+
136+
// component call method
137+
interface SlideExpose {
138+
reset: Function,
139+
clear: Function,
140+
refresh: Function,
141+
close: Function,
123142
}
124143
```
125144
```ts
@@ -133,6 +152,7 @@ interface SlideRegionConfig {
133152
horizontalPadding?: number;
134153
showTheme?: boolean;
135154
title?: string;
155+
scope ?: boolean;
136156
}
137157

138158
// data = {}
@@ -150,7 +170,15 @@ interface SlideRegionEvents {
150170
move?: (x: number, y: number) => void;
151171
refresh?: () => void;
152172
close?: () => void;
153-
confirm?: (point: SlideRegionPoint) => boolean;
173+
confirm?: (point: SlideRegionPoint, reset:() => void) => void;
174+
}
175+
176+
// component call method
177+
interface SlideRegionExpose {
178+
reset: Function,
179+
clear: Function,
180+
refresh: Function,
181+
close: Function,
154182
}
155183
```
156184

@@ -175,6 +203,7 @@ interface RotateConfig {
175203
horizontalPadding?: number;
176204
showTheme?: boolean;
177205
title?: string;
206+
scope ?: boolean;
178207
}
179208

180209
// data = {}
@@ -189,7 +218,15 @@ interface RotateEvents {
189218
rotate?: (angle: number) => void;
190219
refresh?: () => void;
191220
close?: () => void;
192-
confirm?: (angle: number) => boolean;
221+
confirm?: (angle: number, reset:() => void) => void;
222+
}
223+
224+
// component call method
225+
interface RotateExpose {
226+
reset: Function,
227+
clear: Function,
228+
refresh: Function,
229+
close: Function,
193230
}
194231
```
195232

example/App.vue

Lines changed: 126 additions & 25 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "go-captcha-vue",
3-
"version": "2.0.2",
3+
"version": "2.0.3",
44
"private": false,
55
"type": "module",
66
"license": "MIT",

packages/components/button/index.vue

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@
1515
</template>
1616

1717
<script lang="ts" setup>
18-
import {computed, defineEmits, ref, toRefs, watch} from "vue"
18+
import {computed, defineEmits, reactive, toRaw, toRefs, watch} from "vue"
1919
import BtnDefaultIcon from "../../assets/icons/btn-default-icon.vue";
2020
import BtnWarnIcon from "../../assets/icons/btn-warn-icon.vue";
2121
import BtnErrorIcon from "../../assets/icons/btn-error-icon.vue";
2222
import BtnSuccessIcon from "../../assets/icons/btn-success-icon.vue";
2323
2424
import {ButtonConfig, defaultConfig} from "./meta/config";
2525
import {ButtonType} from "@/components/button/meta/types";
26+
import {ClickConfig} from "@/components/click/meta/config";
2627
2728
// @ts-ignore
2829
const props = withDefaults(
@@ -41,18 +42,12 @@ const props = withDefaults(
4142
},
4243
)
4344
44-
const { type, title, disabled } = toRefs(props);
45+
const { type, title, disabled, config } = toRefs(props);
46+
const localConfig = reactive<ClickConfig>({...defaultConfig(), ...toRaw(config)})
4547
46-
const conf = ref({
47-
...defaultConfig(),
48-
...props.config,
49-
})
50-
watch(() => props.config, () => {
51-
conf.value = {
52-
...conf.value,
53-
...props.config
54-
}
55-
})
48+
watch(() => props.config, (newData, _) => {
49+
Object.assign(localConfig, newData)
50+
},{ deep: true })
5651
5752
const btnClass = computed(() => {
5853
const tc = `gc-${type.value}`
@@ -61,12 +56,12 @@ const btnClass = computed(() => {
6156
6257
const btnStyle = computed(() => {
6358
return {
64-
width: conf.value.width + "px",
65-
height: conf.value.height + "px",
66-
paddingLeft: conf.value.horizontalPadding + "px",
67-
paddingRight: conf.value.horizontalPadding + "px",
68-
paddingTop: conf.value.verticalPadding + "px",
69-
paddingBottom: conf.value.verticalPadding + "px",
59+
width: localConfig.width + "px",
60+
height: localConfig.height + "px",
61+
paddingLeft: localConfig.horizontalPadding + "px",
62+
paddingRight: localConfig.horizontalPadding + "px",
63+
paddingTop: localConfig.verticalPadding + "px",
64+
paddingBottom: localConfig.verticalPadding + "px",
7065
}
7166
})
7267

packages/components/click/hooks/handler.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {reactive, toRaw} from "vue";
44
import {getDomXY} from "@/helper/helper";
55

66
export function useHandler(
7-
_data: ClickData,
7+
data: ClickData,
88
event: ClickEvent,
99
) {
1010
const dots = reactive<{list: Array<ClickDot>}>({list: []})
@@ -26,9 +26,7 @@ export function useHandler(
2626
const yy = parseInt(yPos.toString())
2727
const date = new Date()
2828
const index = dots.list.length
29-
const list = dots.list
30-
list.push({key: date.getTime(), index: index + 1, x: xx, y: yy})
31-
dots.list = list
29+
dots.list.push({key: date.getTime(), index: index + 1, x: xx, y: yy})
3230

3331
event.click && event.click(xx, yy)
3432
e.cancelBubble = true
@@ -38,7 +36,7 @@ export function useHandler(
3836

3937
const confirmEvent = (e: Event|any) => {
4038
event.confirm && event.confirm(toRaw(dots.list), () => {
41-
dots.list = []
39+
resetData()
4240
})
4341
e.cancelBubble = true
4442
e.preventDefault()
@@ -47,25 +45,37 @@ export function useHandler(
4745

4846
const closeEvent = (e: Event|any) => {
4947
event.close && event.close()
50-
dots.list = []
48+
resetData()
5149
e.cancelBubble = true
5250
e.preventDefault()
5351
return false
5452
}
5553

5654
const refreshEvent = (e: Event|any) => {
5755
event.refresh && event.refresh()
58-
dots.list = []
56+
resetData()
5957
e.cancelBubble = true
6058
e.preventDefault()
6159
return false
6260
}
6361

62+
const resetData = () => {
63+
dots.list = []
64+
}
65+
66+
const clearData = () => {
67+
resetData()
68+
data.thumb = ''
69+
data.image = ''
70+
}
71+
6472
return {
6573
dots,
6674
clickEvent,
6775
confirmEvent,
6876
closeEvent,
6977
refreshEvent,
78+
resetData,
79+
clearData,
7080
}
7181
}

packages/components/click/index.vue

Lines changed: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
<template>
22
<div
3-
:class="`go-captcha gc-wrapper ${config.showTheme ? 'gc-theme' : ''}`"
3+
:class="`go-captcha gc-wrapper ${localConfig.showTheme && 'gc-theme'}`"
44
:style="wrapperStyles"
5+
v-show="hasDisplayWrapperState"
56
>
67
<div class="gc-header">
7-
<span>{{ config.title }}</span>
8-
<img v-show="data.thumb !== ''" :style="thumbStyles" :src="data.thumb" alt="..." />
8+
<span>{{ localConfig.title }}</span>
9+
<img v-show="hasDisplayImageState" :style="thumbStyles" :src="localData.thumb" alt="" />
910
</div>
1011
<div class="gc-body" :style="imageStyles">
1112
<div class="gc-loading">
1213
<loading-icon />
1314
</div>
14-
<img :style="imageStyles" v-show="data.image !== ''" class="gc-picture" :src="data.image" alt="..." @click="handler.clickEvent"/>
15+
<img :style="imageStyles" v-show="hasDisplayImageState" class="gc-picture" :src="localData.image" alt="" @click="handler.clickEvent"/>
1516
<div class="gc-dots">
16-
<div class="gc-dot" v-for="dot in dots.list" :key="`${dot.key + '-' + dot.index}`" :style="{
17-
top: (dot.y - 11) + 'px',
18-
left: (dot.x - 11) + 'px',
17+
<div class="gc-dot" v-for="dot in handler.dots.list" :key="`${dot.key + '-' + dot.index}`" :style="{
18+
top: (dot.y - ((localConfig.dotSize || 1)/2)-1) + 'px',
19+
left: (dot.x - ((localConfig.dotSize || 1)/2)-1) + 'px',
1920
}">{{dot.index}}</div>
2021
</div>
2122
</div>
2223
<div class="gc-footer">
2324
<div class="gc-icon-block gc-icon-block2">
24-
<close-icon :width="22" :height="22" @click="handler.closeEvent"/>
25-
<refresh-icon :width="22" :height="22" @click="handler.refreshEvent"/>
25+
<close-icon :width="localConfig.iconSize" :height="localConfig.iconSize" @click="handler.closeEvent"/>
26+
<refresh-icon :width="localConfig.iconSize" :height="localConfig.iconSize" @click="handler.refreshEvent"/>
2627
</div>
2728
<div class="gc-button-block">
28-
<button @click="handler.confirmEvent">{{ config.buttonText }}</button>
29+
<button :class="!hasDisplayImageState && 'disabled'" @click="handler.confirmEvent">{{ localConfig.buttonText }}</button>
2930
</div>
3031
</div>
3132
</div>
3233
</template>
3334

3435
<script lang="ts" setup>
35-
import {computed, ref, watch} from "vue"
36-
import {ClickConfig, defaultConfig} from "./meta/config";
37-
36+
import {computed, reactive, toRaw, watch} from "vue"
3837
import CloseIcon from "../../assets/icons/close-icon.vue";
38+
3939
import RefreshIcon from "../../assets/icons/refresh-icon.vue";
4040
import LoadingIcon from "../../assets/icons/loading-icon.vue";
4141
42+
import {ClickConfig, defaultConfig} from "./meta/config";
4243
import {ClickData} from "./meta/data";
4344
import {ClickEvent} from "./meta/event";
45+
import {ClickExpose} from "./meta/expose";
4446
import {useHandler} from "./hooks/handler";
4547
4648
// @ts-ignore
@@ -57,26 +59,30 @@ const props = withDefaults(
5759
},
5860
)
5961
60-
const { data, events } = props;
61-
const config = ref({
62-
...defaultConfig(),
63-
...props.config,
64-
})
65-
watch(() => props.config, () => {
66-
config.value = {
67-
...config.value,
68-
...props.config
69-
}
70-
})
62+
const { data, events, config } = props;
63+
const localData = reactive<ClickData>({...toRaw(data)})
64+
const localEvent = reactive<ClickEvent>({...toRaw(events)})
65+
const localConfig = reactive<ClickConfig>({...defaultConfig(), ...toRaw(config)})
66+
67+
watch(() => props.data, (newData, _) => {
68+
Object.assign(localData, newData)
69+
},{ deep: true });
7170
72-
const handler = useHandler(data, events);
71+
watch(() => props.events, (newData, _) => {
72+
Object.assign(localEvent, newData)
73+
},{ deep: true })
7374
74-
const hPadding = config.value.horizontalPadding || 0
75-
const vPadding = config.value.verticalPadding || 0
76-
const width = (config.value.width || 0) + ( hPadding * 2) + (config.value.showTheme ? 2 : 0)
77-
const dots = handler.dots
75+
watch(() => props.config, (newData, _) => {
76+
Object.assign(localConfig, newData)
77+
},{ deep: true })
78+
79+
const handler = useHandler(localData, localEvent);
7880
7981
const wrapperStyles = computed(() => {
82+
const hPadding = localConfig.horizontalPadding || 0
83+
const vPadding = localConfig.verticalPadding || 0
84+
const width = (localConfig.width || 0) + ( hPadding * 2) + (localConfig.showTheme ? 2 : 0)
85+
8086
return {
8187
width: width+ "px",
8288
paddingLeft: hPadding + "px",
@@ -88,17 +94,32 @@ const wrapperStyles = computed(() => {
8894
8995
const thumbStyles = computed(() => {
9096
return {
91-
width: config.value.thumbWidth + "px",
92-
height: config.value.thumbHeight + "px",
97+
width: localConfig.thumbWidth + "px",
98+
height: localConfig.thumbHeight + "px",
9399
}
94100
})
95101
96102
const imageStyles = computed(() => {
97103
return {
98-
width: config.value.width + "px",
99-
height: config.value.height + "px"
104+
width: localConfig.width + "px",
105+
height: localConfig.height + "px"
100106
}
101107
})
108+
109+
const hasDisplayImageState = computed(() => {
110+
return localData.image != '' && localData.thumb != ''
111+
})
112+
113+
const hasDisplayWrapperState = computed(() => {
114+
return (localConfig.width || 0) > 0 || (localConfig.height || 0) > 0
115+
})
116+
117+
defineExpose<ClickExpose>({
118+
reset: handler.resetData,
119+
clear: handler.clearData,
120+
refresh: handler.refreshEvent,
121+
close: handler.closeEvent,
122+
});
102123
</script>
103124

104125
<style lang="less">
@@ -116,11 +137,11 @@ const imageStyles = computed(() => {
116137
.gc-dot {
117138
position: absolute;
118139
z-index: 2;
119-
width: 20px;
120-
height: 20px;
140+
width: 22px;
141+
height: 22px;
121142
color: #cedffe;
122143
background: #3e7cff;
123-
border: 2px solid #f7f9fb;
144+
border: 3px solid #f7f9fb;
124145
display:-webkit-box;
125146
display:-webkit-flex;
126147
display:-ms-flexbox;

0 commit comments

Comments
 (0)