11<template >
2- <div ref =" demoBlock" :class =" ['demo-block', blockClass, customClass ? customClass : '', { hover }]"
3- @mouseenter =" hover = true" @mouseleave =" hover = false" >
2+ <div
3+ ref =" demoBlock"
4+ :class =" ['demo-block', blockClass, customClass ? customClass : '', { hover }]"
5+ @mouseenter =" hover = true"
6+ @mouseleave =" hover = false"
7+ >
48 <template v-if =" isUseVueFile " >
59 <div class =" source" >
610 <component :is =" DemoComponent" />
3034
3135 <div ref =" control" :class =" ['demo-block-control', { 'is-fixed': fixedControl }]" @click =" onClickControl" >
3236 <transition name =" arrow-slide" >
33- <i :class =" [
34- 'control-icon',
35- { 'icon-caret-down': !isExpanded, 'icon-caret-up': isExpanded, hovering: hover }
36- ]" ></i >
37+ <i :class =" ['control-icon', { 'icon-caret-down': !isExpanded, 'icon-caret-up': isExpanded, hovering: hover }]" ></i >
3738 </transition >
3839 <transition name =" text-slide" >
3940 <span v-show =" hover" class =" control-text" >{{ controlText }}</span >
5354import { useRoute , useData } from ' vitepress' ;
5455import { throttle } from ' lodash' ;
5556import copy from ' clipboard-copy' ;
56- import {
57- ref ,
58- computed ,
59- watch ,
60- onMounted ,
61- onBeforeUnmount ,
62- nextTick ,
63- defineAsyncComponent
64- } from ' vue' ;
57+ import { ref , computed , watch , onMounted , onBeforeUnmount , nextTick , defineAsyncComponent } from ' vue' ;
6558export default {
6659 name: ' Demo' ,
6760 props: {
@@ -70,122 +63,123 @@ export default {
7063 lightCode: String ,
7164 desc: String ,
7265 targetFilePath: String ,
73- demoList: Array | undefined
66+ demoList: Array | undefined ,
7467 },
7568 setup (props ) {
76- const hover = ref (false )
77- const fixedControl = ref (false )
78- const isExpanded = ref (false )
79- const isShowTip = ref (false )
69+ const hover = ref (false );
70+ const fixedControl = ref (false );
71+ const isExpanded = ref (false );
72+ const isShowTip = ref (false );
8073 const isUseVueFile = computed (() => !! props .targetFilePath );
8174
82- const data = useData ()
83- const route = useRoute ()
75+ const data = useData ();
76+ const route = useRoute ();
8477
85- const pathArr = ref (route .path .split (' /' ))
86- const component = computed (() => pathArr .value [pathArr .value .length - 1 ].split (' .' )[0 ])
87- const DemoComponent = props .demoList ? .[props .targetFilePath ]? .default ?? defineAsyncComponent (() => import (/* vite-ignore */ props .targetFilePath ));
78+ const pathArr = ref (route .path .split (' /' ));
79+ const component = computed (() => pathArr .value [pathArr .value .length - 1 ].split (' .' )[0 ]);
80+ const DemoComponent =
81+ props .demoList ? .[props .targetFilePath ]? .default ?? defineAsyncComponent (() => import (/* vite-ignore */ props .targetFilePath ));
8882 watch (
8983 () => route .path ,
90- path => {
91- pathArr .value = path .split (' /' )
84+ ( path ) => {
85+ pathArr .value = path .split (' /' );
9286 }
93- )
87+ );
9488 const onClickControl = () => {
95- isExpanded .value = ! isExpanded .value
96- hover .value = isExpanded .value
97- }
89+ isExpanded .value = ! isExpanded .value ;
90+ hover .value = isExpanded .value ;
91+ };
9892 const blockClass = computed (() => {
99- return ` demo-${ component .value } `
100- })
93+ return ` demo-${ component .value } ` ;
94+ });
10195 const locale = computed (() => {
10296 return (
10397 data .theme .value .demoblock ? .[data .localePath .value ] ?? {
10498 ' hide-text' : ' 隐藏代码' ,
10599 ' show-text' : ' 显示代码' ,
106100 ' copy-button-text' : ' 复制代码片段' ,
107- ' copy-success-text' : ' 复制成功'
101+ ' copy-success-text' : ' 复制成功' ,
108102 }
109- )
110- })
103+ );
104+ });
111105 const decoded = computed (() => {
112- return decodeURIComponent (props .lightCode )
113- })
106+ return decodeURIComponent (props .lightCode );
107+ });
114108 const desc = computed (() => {
115- return props? .desc ? decodeURIComponent (props .desc ) : null
116- })
109+ return props? .desc ? decodeURIComponent (props .desc ) : null ;
110+ });
117111
118112 const copyText = computed (() => {
119- return isShowTip .value ? locale .value [' copy-success-text' ] : locale .value [' copy-button-text' ]
120- })
113+ return isShowTip .value ? locale .value [' copy-success-text' ] : locale .value [' copy-button-text' ];
114+ });
121115
122116 const controlText = computed (() => {
123- return isExpanded .value ? locale .value [' hide-text' ] : locale .value [' show-text' ]
124- })
117+ return isExpanded .value ? locale .value [' hide-text' ] : locale .value [' show-text' ];
118+ });
125119
126120 // template refs
127- const highlight = ref (null )
128- const description = ref (null )
129- const meta = ref (null )
130- const control = ref (null )
131- const demoBlock = ref (null )
121+ const highlight = ref (null );
122+ const description = ref (null );
123+ const meta = ref (null );
124+ const control = ref (null );
125+ const demoBlock = ref (null );
132126
133127 const codeAreaHeight = computed (() => {
134128 if (description .value ) {
135- return description .value .clientHeight + highlight .value .clientHeight + 20
129+ return description .value .clientHeight + highlight .value .clientHeight + 20 ;
136130 }
137- return highlight .value .clientHeight
138- })
131+ return highlight .value .clientHeight ;
132+ });
139133
140134 const _scrollHandler = () => {
141- const { top , bottom , left } = meta .value .getBoundingClientRect ()
142- const innerHeight = window .innerHeight || document .body .clientHeight
143- fixedControl .value = bottom > innerHeight && top + 44 <= innerHeight
144- control .value .style .left = fixedControl .value ? ` ${ left} px` : ' 0'
145- const dv = fixedControl .value ? 1 : 2
146- control .value .style .width = ` ${ demoBlock .value .offsetWidth - dv} px`
147- }
148- const scrollHandler = throttle (_scrollHandler, 200 )
135+ const { top , bottom , left } = meta .value .getBoundingClientRect ();
136+ const innerHeight = window .innerHeight || document .body .clientHeight ;
137+ fixedControl .value = bottom > innerHeight && top + 44 <= innerHeight;
138+ control .value .style .left = fixedControl .value ? ` ${ left} px` : ' 0' ;
139+ const dv = fixedControl .value ? 1 : 2 ;
140+ control .value .style .width = ` ${ demoBlock .value .offsetWidth - dv} px` ;
141+ };
142+ const scrollHandler = throttle (_scrollHandler, 200 );
149143 const removeScrollHandler = () => {
150- window .removeEventListener (' scroll' , scrollHandler)
151- window .removeEventListener (' resize' , scrollHandler)
152- }
144+ window .removeEventListener (' scroll' , scrollHandler);
145+ window .removeEventListener (' resize' , scrollHandler);
146+ };
153147
154148 const onCopy = () => {
155- copy (props .sourceCode )
156- isShowTip .value = true
149+ copy (props .sourceCode );
150+ isShowTip .value = true ;
157151 setTimeout (() => {
158- isShowTip .value = false
159- }, 1300 )
160- }
152+ isShowTip .value = false ;
153+ }, 1300 );
154+ };
161155
162- watch (isExpanded, val => {
163- meta .value .style .height = val ? ` ${ codeAreaHeight .value + 1 } px` : ' 0'
156+ watch (isExpanded, ( val ) => {
157+ meta .value .style .height = val ? ` ${ codeAreaHeight .value + 1 } px` : ' 0' ;
164158 if (! val) {
165- fixedControl .value = false
166- control .value .style .left = ' 0'
167- control .value .style .width = ` ${ demoBlock .value .offsetWidth - 2 } px`
168- removeScrollHandler ()
169- return
159+ fixedControl .value = false ;
160+ control .value .style .left = ' 0' ;
161+ control .value .style .width = ` ${ demoBlock .value .offsetWidth - 2 } px` ;
162+ removeScrollHandler ();
163+ return ;
170164 }
171165 setTimeout (() => {
172- window .addEventListener (' scroll' , scrollHandler)
173- window .addEventListener (' resize' , scrollHandler)
174- _scrollHandler ()
175- }, 300 )
176- })
166+ window .addEventListener (' scroll' , scrollHandler);
167+ window .addEventListener (' resize' , scrollHandler);
168+ _scrollHandler ();
169+ }, 300 );
170+ });
177171
178172 onMounted (() => {
179173 nextTick (() => {
180174 if (! description .value ) {
181- highlight .value .style .width = ' 100%'
175+ highlight .value .style .width = ' 100%' ;
182176 }
183- })
184- })
177+ });
178+ });
185179
186180 onBeforeUnmount (() => {
187- removeScrollHandler ()
188- })
181+ removeScrollHandler ();
182+ });
189183
190184 return {
191185 blockClass,
@@ -205,10 +199,10 @@ export default {
205199 onCopy,
206200 demoBlock,
207201 decoded,
208- DemoComponent
209- }
210- }
211- }
202+ DemoComponent,
203+ };
204+ },
205+ };
212206< / script>
213207
214208< style scoped>
0 commit comments