Skip to content

Commit c0d2ee2

Browse files
committed
refactor(layout): refactor layout to fit changes with vitepress@latest
Related Issue: DevCloudFE#1879
1 parent 494a4b0 commit c0d2ee2

File tree

4 files changed

+86
-109
lines changed

4 files changed

+86
-109
lines changed

packages/devui-vue/docs/.vitepress/devui-theme/Layout.vue

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ import { LANG_KEY, ZH_CN, EN_US } from './const';
1515
1616
const Home = defineAsyncComponent(() => import('./components/Home.vue'));
1717
18-
const NoopComponent = () => null;
19-
20-
const CarbonAds = __CARBON__ ? defineAsyncComponent(() => import('./components/CarbonAds.vue')) : NoopComponent;
21-
const BuySellAds = __BSA__ ? defineAsyncComponent(() => import('./components/BuySellAds.vue')) : NoopComponent;
22-
const AlgoliaSearchBox = __ALGOLIA__ ? defineAsyncComponent(() => import('./components/AlgoliaSearchBox.vue')) : NoopComponent;
18+
const CarbonAds = defineAsyncComponent(() => import('./components/CarbonAds.vue'));
19+
const BuySellAds = defineAsyncComponent(() => import('./components/BuySellAds.vue'));
20+
const AlgoliaSearchBox = defineAsyncComponent(() => import('./components/AlgoliaSearchBox.vue'));
2321
2422
// generic state
2523
const route = useRoute();
@@ -29,7 +27,8 @@ const router = useRouter();
2927
// custom layout
3028
const isCustomLayout = computed(() => !!frontmatter.value.customLayout);
3129
// home
32-
const enableHome = computed(() => !!frontmatter.value.home);
30+
console.table(frontmatter.value);
31+
const enableHome = computed(() => !!frontmatter.value.layout);
3332
3433
// automatic multilang check for AlgoliaSearchBox
3534
const isMultiLang = computed(() => Object.keys(theme.value.locales || {}).length > 0);
@@ -47,7 +46,7 @@ const showNavbar = computed(() => {
4746
const openSideBar = ref(false);
4847
4948
const showSidebar = computed(() => {
50-
if (frontmatter.value.home || frontmatter.value.sidebar === false) {
49+
if (frontmatter.value.layout || frontmatter.value.sidebar === false) {
5150
return false;
5251
}
5352
@@ -182,20 +181,17 @@ const contributors = computed(() => {
182181
<template #top>
183182
<slot name="page-top-ads">
184183
<div id="ads-container" v-if="theme.carbonAds && theme.carbonAds.carbon">
185-
<CarbonAds :key="'carbon' + page.relativePath" :code="theme.carbonAds.carbon" :placement="theme.carbonAds.placement" />
184+
<CarbonAds :key="'carbon' + page.relativePath" :code="theme.carbonAds.carbon"
185+
:placement="theme.carbonAds.placement" />
186186
</div>
187187
</slot>
188188
<slot name="page-top" />
189189
</template>
190190
<template #bottom>
191191
<slot name="page-bottom" />
192192
<slot name="page-bottom-ads">
193-
<BuySellAds
194-
v-if="theme.carbonAds && theme.carbonAds.custom"
195-
:key="'custom' + page.relativePath"
196-
:code="theme.carbonAds.custom"
197-
:placement="theme.carbonAds.placement"
198-
/>
193+
<BuySellAds v-if="theme.carbonAds && theme.carbonAds.custom" :key="'custom' + page.relativePath"
194+
:code="theme.carbonAds.custom" :placement="theme.carbonAds.placement" />
199195
</slot>
200196
</template>
201197
</Page>
@@ -204,14 +200,15 @@ const contributors = computed(() => {
204200
<div class="container-contributors" v-if="enableHome">
205201
<div class="contributors-inner">
206202
<h2>✨贡献者✨</h2>
207-
<PageContributor v-if="contributors && contributors.length > 0" :contributors="contributors" :spacing="20" :avatarSize="48" />
208-
<a href="/contributing/"><Button class="btn-become-contributor" variant="solid" color="primary">成为贡献者</Button></a>
203+
<PageContributor v-if="contributors && contributors.length > 0" :contributors="contributors" :spacing="20"
204+
:avatarSize="48" />
205+
<a href="/contributing/"><Button class="btn-become-contributor" variant="solid"
206+
color="primary">成为贡献者</Button></a>
209207
</div>
210208
</div>
211209
</div>
212210

213211
<DevuiFooter class="footer" v-if="enableHome" />
214-
<Debug v-if="false" />
215212
</div>
216213
</template>
217214

@@ -313,21 +310,21 @@ body[ui-theme='galaxy-theme'] {
313310
}
314311
315312
.page-contributor {
316-
& > a > span {
313+
&>a>span {
317314
margin: 0 12px 8px 0 !important;
318315
319-
& > img,
316+
&>img,
320317
& svg {
321318
width: 40px !important;
322319
height: 40px !important;
323320
}
324321
}
325322
326-
& > a:nth-child(8n) > span {
323+
&>a:nth-child(8n)>span {
327324
margin: 0 12px 8px 0 !important;
328325
}
329326
330-
& > a:nth-child(7n) > span {
327+
&>a:nth-child(7n)>span {
331328
margin: 0 !important;
332329
}
333330
}
@@ -338,11 +335,11 @@ body[ui-theme='galaxy-theme'] {
338335
@media (max-width: 385px) {
339336
.container-contributors .contributors-inner {
340337
.page-contributor {
341-
& > a:nth-child(7n) > span {
338+
&>a:nth-child(7n)>span {
342339
margin: 0 12px 8px 0 !important;
343340
}
344341
345-
& > a:nth-child(6n) > span {
342+
&>a:nth-child(6n)>span {
346343
margin: 0 !important;
347344
}
348345
}
@@ -353,11 +350,11 @@ body[ui-theme='galaxy-theme'] {
353350
@media (max-width: 330px) {
354351
.container-contributors .contributors-inner {
355352
.page-contributor {
356-
& > a:nth-child(6n) > span {
353+
&>a:nth-child(6n)>span {
357354
margin: 0 12px 8px 0 !important;
358355
}
359356
360-
& > a:nth-child(5n) > span {
357+
&>a:nth-child(5n)>span {
361358
margin: 0 !important;
362359
}
363360
}

packages/devui-vue/docs/.vitepress/devui-theme/components/SideBarLink.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const SideBarLink = (props) => {
3838
}
3939

4040
const link = resolveLink(site.value.base, props.item.link);
41-
const children = props.item.children;
41+
const children = props.item.items;
4242
const active = isActive(route, props.item.link);
4343
const childItems = depth < maxDepth ? createChildren(active, children, headers, depth + 1) : null;
4444
return h('li', { class: 'sidebar-link' }, [
Lines changed: 62 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,116 @@
1-
import { onMounted, onUnmounted, onUpdated } from 'vue'
1+
import { onMounted, onUnmounted, onUpdated } from 'vue';
22

3-
import type { Ref } from 'vue'
3+
import type { Ref } from 'vue';
44

55
// 防抖节流控制
66
export const throttleAndDebounce = (fn: () => any, delay: number) => {
7-
let timeout: ReturnType<typeof setTimeout>
8-
let called = false
7+
let timeout: ReturnType<typeof setTimeout>;
8+
let called = false;
99
return () => {
1010
if (timeout) {
11-
clearTimeout(timeout)
11+
clearTimeout(timeout);
1212
}
1313
if (!called) {
14-
fn()
15-
called = true
14+
fn();
15+
called = true;
1616
setTimeout(() => {
17-
called = false
18-
}, delay)
17+
called = false;
18+
}, delay);
1919
} else {
20-
timeout = setTimeout(fn, delay)
20+
timeout = setTimeout(fn, delay);
2121
}
22-
}
23-
}
22+
};
23+
};
2424

25-
export function useActiveSidebarLinks(
26-
container: Ref<HTMLElement>,
27-
marker: Ref<HTMLElement>
28-
) {
29-
const onScroll = throttleAndDebounce(setActiveLink, 150)
25+
export function useActiveSidebarLinks(container: Ref<HTMLElement>, marker: Ref<HTMLElement>) {
26+
const onScroll = throttleAndDebounce(setActiveLink, 150);
3027
function setActiveLink() {
31-
const sidebarLinks = getSidebarLinks()
32-
const anchors = getAnchors(sidebarLinks)
28+
const sidebarLinks = getSidebarLinks();
29+
const anchors = getAnchors(sidebarLinks);
3330

34-
if (
35-
anchors.length &&
36-
window.scrollY + window.innerHeight === document.body.offsetHeight
37-
) {
38-
activateLink(anchors[anchors.length - 1].hash)
39-
return
31+
if (anchors.length && window.scrollY + window.innerHeight === document.body.offsetHeight) {
32+
activateLink(anchors[anchors.length - 1].hash);
33+
return;
4034
}
4135
for (let i = 0; i < anchors.length; i++) {
42-
const anchor = anchors[i]
43-
const nextAnchor = anchors[i + 1]
44-
const [isActive, hash] = isAnchorActive(i, anchor, nextAnchor)
36+
const anchor = anchors[i];
37+
const nextAnchor = anchors[i + 1];
38+
const [isActive, hash] = isAnchorActive(i, anchor, nextAnchor);
4539
if (isActive) {
46-
history.replaceState(
47-
null,
48-
document.title,
49-
hash ? (hash as string) : ' '
50-
)
51-
activateLink(hash as string)
52-
return
40+
history.replaceState(null, document.title, hash ? (hash as string) : ' ');
41+
activateLink(hash as string);
42+
return;
5343
}
5444
}
5545
}
5646

57-
let prevActiveLink: HTMLAnchorElement | null = null
47+
let prevActiveLink: HTMLAnchorElement | null = null;
5848

5949
function activateLink(hash: string) {
60-
deactiveLink(prevActiveLink)
50+
deactiveLink(prevActiveLink);
6151

62-
const activeLink = (prevActiveLink =
63-
hash == null
64-
? null
65-
: (container.value.querySelector(
66-
`.devui-item a[href="${decodeURIComponent(hash)}"]`
67-
) as HTMLAnchorElement))
68-
if (activeLink) {
69-
activeLink.classList.add('active')
70-
marker.value.style.opacity = '1'
71-
marker.value.style.top = `${activeLink.offsetTop}px`
72-
} else {
73-
marker.value.style.opacity = '0'
74-
marker.value.style.top = '33px'
75-
}
52+
// const activeLink = (prevActiveLink =
53+
// hash == null
54+
// ? null
55+
// : (container.value.querySelector(
56+
// `.devui-item a[href="${decodeURIComponent(hash)}"]`
57+
// ) as HTMLAnchorElement))
58+
// if (activeLink) {
59+
// activeLink.classList.add('active')
60+
// marker.value.style.opacity = '1'
61+
// marker.value.style.top = `${activeLink.offsetTop}px`
62+
// } else {
63+
// marker.value.style.opacity = '0'
64+
// marker.value.style.top = '33px'
65+
// }
7666
}
7767

7868
function deactiveLink(link: HTMLElement) {
79-
link && link.classList.remove('active')
69+
link && link.classList.remove('active');
8070
}
8171

8272
onMounted(() => {
83-
window.requestAnimationFrame(setActiveLink)
84-
window.addEventListener('scroll', onScroll)
85-
})
73+
window.requestAnimationFrame(setActiveLink);
74+
window.addEventListener('scroll', onScroll);
75+
});
8676

8777
onUpdated(() => {
88-
activateLink(location.hash)
89-
})
78+
activateLink(location.hash);
79+
});
9080

9181
onUnmounted(() => {
92-
window.removeEventListener('scroll', onScroll)
93-
})
82+
window.removeEventListener('scroll', onScroll);
83+
});
9484
}
9585
function getSidebarLinks() {
96-
return Array.from(
97-
document.querySelectorAll('.devui-content-nav .devui-link')
98-
) as HTMLAnchorElement[]
86+
return Array.from(document.querySelectorAll('.devui-content-nav .devui-link')) as HTMLAnchorElement[];
9987
}
10088
function getAnchors(sidebarLinks: HTMLAnchorElement[]) {
101-
return (
102-
Array.from(
103-
document.querySelectorAll('.content .header-anchor')
104-
) as HTMLAnchorElement[]
105-
).filter((anchor) =>
89+
return (Array.from(document.querySelectorAll('.content .header-anchor')) as HTMLAnchorElement[]).filter((anchor) =>
10690
sidebarLinks.some((sidebarLink) => sidebarLink.hash === anchor.hash)
107-
)
91+
);
10892
}
10993
function getPageOffset() {
110-
return (document.querySelector('.nav-bar') as HTMLElement).offsetHeight
94+
return (document.querySelector('.nav-bar') as HTMLElement).offsetHeight;
11195
}
11296
function getAnchorTop(anchor: HTMLAnchorElement) {
113-
const pageOffset = getPageOffset()
97+
const pageOffset = getPageOffset();
11498
try {
115-
return anchor.parentElement.offsetTop - pageOffset - 15
99+
return anchor.parentElement.offsetTop - pageOffset - 15;
116100
} catch (e) {
117-
return 0
101+
return 0;
118102
}
119103
}
120-
function isAnchorActive(
121-
index: number,
122-
anchor: HTMLAnchorElement,
123-
nextAnchor: HTMLAnchorElement
124-
) {
125-
const scrollTop = window.scrollY
104+
function isAnchorActive(index: number, anchor: HTMLAnchorElement, nextAnchor: HTMLAnchorElement) {
105+
const scrollTop = window.scrollY;
126106
if (index === 0 && scrollTop === 0) {
127-
return [true, null]
107+
return [true, null];
128108
}
129109
if (scrollTop < getAnchorTop(anchor)) {
130-
return [false, null]
110+
return [false, null];
131111
}
132112
if (!nextAnchor || scrollTop < getAnchorTop(nextAnchor)) {
133-
return [true, decodeURIComponent(anchor.hash)]
113+
return [true, decodeURIComponent(anchor.hash)];
134114
}
135-
return [false, null]
115+
return [false, null];
136116
}

packages/devui-vue/docs/.vitepress/theme/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if (typeof window !== 'undefined') {
3939
export default {
4040
extends: DevUITheme,
4141
Layout: () => {
42-
return h(DefaultTheme.Layout, null, {
42+
return h(DevUITheme.Layout ?? DefaultTheme.Layout, null, {
4343
// https://vitepress.dev/guide/extending-default-theme#layout-slots
4444
});
4545
},

0 commit comments

Comments
 (0)