Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix demo anchor position not correct #2391

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions examples/sites/demos/pc/webdoc/import-components-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ For more information about automatic on-demand imports, please refer to [unplugi

### Multi-component introduction

<div class="tip custom-block">
<br />
Warm reminder: Exporting components with Tiny prefix is ​​supported since 3.17.0. If using previous versions, you need to use aliases.
<br />
<p>For example:<code>import { Button as TinyButton } from '@opentiny/vue'</code></p>
</div>

Multi-component reference is from the dependency package.`@opentiny/vue`Multiple components are referenced in. The following is an example (in the following figure)`Button`and`Alert`Component as an example):

```html
Expand All @@ -55,12 +62,12 @@ Multi-component reference is from the dependency package.`@opentiny/vue`Multiple
</template>

<script>
import { Button, Alert } from '@opentiny/vue'
import { TinyButton, TinyAlert } from '@opentiny/vue'

export default {
components: {
TinyButton: Button,
TinyAlert: Alert
TinyButton,
TinyAlert
}
}
</script>
Expand Down
15 changes: 11 additions & 4 deletions examples/sites/demos/pc/webdoc/import-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ module.exports = defineConfig({

### 多组件引入

多组件引用即从依赖包 `@opentiny/vue` 中引用多个组件,参考示例如下(以 `Button` 和 `Alert` 组件为例):
<div class="tip custom-block">
<br />
温馨提示:带有Tiny前缀的组件导出自3.17.0开始支持。若使用之前版本,需使用别名。
<br />
<p>例如:<code>import { Button as TinyButton } from '@opentiny/vue'</code></p>
</div>

多组件引用即从依赖包 `@opentiny/vue` 中引用多个组件,参考示例如下(以 `TinyButton` 和 `TinyAlert` 组件为例):

```html
<template>
Expand All @@ -95,12 +102,12 @@ module.exports = defineConfig({
</template>

<script>
import { Button, Alert } from '@opentiny/vue'
import { TinyButton, TinyAlert } from '@opentiny/vue'

export default {
components: {
TinyButton: Button,
TinyAlert: Alert
TinyButton,
TinyAlert
}
}
</script>
Expand Down
14 changes: 12 additions & 2 deletions examples/sites/src/menus.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cmpMenus } from '@menu/menus.js'
import { docMenus, cmpMenus } from '@menu/menus.js'
import { appData, $split } from './tools'

/**
Expand Down Expand Up @@ -26,6 +26,16 @@ function genMenus() {
}
]

const docOptions = docMenus.map((menu) => ({
...menu,
label: `${appData.lang === 'zhCN' ? menu.label : menu.labelEn}${getChildrenStr(menu)}`,
children: menu.children.map((page) => ({
...page,
id: page.key,
label: appData.lang === 'zhCN' ? page.title : page.titleEn,
type: 'docs'
}))
}))
const cmpOptions = cmpMenus.map((menu) => ({
Comment on lines +29 to +38
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add safety checks and consider type constants

While the logic is sound, consider these improvements for robustness:

  1. Add null checks for menu.children
  2. Consider extracting 'docs' type to a constant

Here's a suggested improvement:

+const MENU_TYPES = {
+  DOCS: 'docs',
+  COMPONENTS: 'components',
+  OVERVIEW: 'overview'
+}

 const docOptions = docMenus.map((menu) => ({
   ...menu,
   label: `${appData.lang === 'zhCN' ? menu.label : menu.labelEn}${getChildrenStr(menu)}`,
-  children: menu.children.map((page) => ({
+  children: (menu.children || []).map((page) => ({
     ...page,
     id: page.key,
     label: appData.lang === 'zhCN' ? page.title : page.titleEn,
-    type: 'docs'
+    type: MENU_TYPES.DOCS
   }))
 }))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const docOptions = docMenus.map((menu) => ({
...menu,
label: `${appData.lang === 'zhCN' ? menu.label : menu.labelEn}${getChildrenStr(menu)}`,
children: menu.children.map((page) => ({
...page,
id: page.key,
label: appData.lang === 'zhCN' ? page.title : page.titleEn,
type: 'docs'
}))
}))
const MENU_TYPES = {
DOCS: 'docs',
COMPONENTS: 'components',
OVERVIEW: 'overview'
}
const docOptions = docMenus.map((menu) => ({
...menu,
label: `${appData.lang === 'zhCN' ? menu.label : menu.labelEn}${getChildrenStr(menu)}`,
children: (menu.children || []).map((page) => ({
...page,
id: page.key,
label: appData.lang === 'zhCN' ? page.title : page.titleEn,
type: MENU_TYPES.DOCS
}))
}))

...menu,
label: `${appData.lang === 'zhCN' ? menu.label : menu.labelEn}${getChildrenStr(menu)}`,
Expand All @@ -36,7 +46,7 @@ function genMenus() {
type: 'components'
}))
}))
return [...standaloneOptions, ...cmpOptions]
return [...standaloneOptions, ...docOptions, ...cmpOptions]
}

// 获取菜单的类别图标
Expand Down
7 changes: 7 additions & 0 deletions examples/sites/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ let routes = [
name: 'overview',
children: [{ name: 'Overview', path: '', component: Overview, meta: { title: '组件总览 | TinyVue' } }]
},
// 文档
{
path: `${context}:all?/:lang/:theme/docs/:docId`,
component: Layout,
name: 'docs',
children: [{ name: 'Docs', path: '', component: Docs }]
},
// 组件
{
path: `${context}:all?/zh-CN/:theme/components/:cmpId`,
Expand Down
31 changes: 26 additions & 5 deletions examples/sites/src/views/components/components.vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
:demo="demo"
:curr-demo-id="currDemoId"
class="mb32"
@mounted="demoMounted"
/>
</div>
<div v-else>
Expand Down Expand Up @@ -224,6 +225,7 @@
<div class="cmp-page-anchor catalog" v-if="currAnchorLinks.length">
<tiny-anchor
id="anchor"
:offset-top="156"
:links="currAnchorLinks"
:key="anchorRefreshKey"
:is-affix="anchorAffix"
Expand Down Expand Up @@ -338,6 +340,26 @@ export default defineComponent({
const { apiModeState } = useApiMode()
const { templateModeState, staticPath, optionsList } = useTemplateMode()

let finishNum = 0
let isAllMounted = false
let demoMountedResolve
const demoMounted = () => {
finishNum++
if (finishNum === state.currJson.demos.length) {
isAllMounted = true
demoMountedResolve(true)
}
}

const allDemoMounted = async () => {
if (isAllMounted) {
return isAllMounted
}
return new Promise((resolve) => {
demoMountedResolve = resolve
})
}
Comment on lines +343 to +361
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure mounting state variables are reset appropriately

Variables finishNum, isAllMounted, and demoMountedResolve may retain their values across multiple invocations of loadPage, potentially causing incorrect behavior when navigating between components or reloading. Reset these variables when loadPage is called to maintain correct state.

Apply this diff to reset the variables at the beginning of the loadPage function:

 const loadPage = debounce(templateModeState.isSaas ? 100 : 0, false, () => {
+  finishNum = 0
+  isAllMounted = false
+  demoMountedResolve = null
   const lang = getWord('cn', 'en')
   state.cmpId = router.currentRoute.value.params.cmpId

Committable suggestion was skipped due to low confidence.


const getApiAnchorLinks = () => {
if (!state.currJson.apis?.length) {
return []
Expand Down Expand Up @@ -569,11 +591,9 @@ export default defineComponent({

// F5刷新加载时,跳到当前示例
// 应当在所有demo渲染完毕后在滚动,否则滚动完位置后,demo渲染会使滚动位置错位
setTimeout(() => {
nextTick(() => {
scrollByHash(hash)
})
}, 0)
return allDemoMounted().then(() => {
scrollByHash(hash)
})
})
.finally(() => {
// 获取组件贡献者
Expand Down Expand Up @@ -616,6 +636,7 @@ export default defineComponent({
}

const fn = {
demoMounted,
copyText: (text) => {
navigator.clipboard.writeText(text)
},
Expand Down
6 changes: 5 additions & 1 deletion examples/sites/src/views/components/demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ const { apiModeState, apiModeFn } = useApiMode()
export default defineComponent({
name: 'Demo',
props: ['demo', 'currDemoId'],
emits: ['mounted'],
components: {
TinyTooltip,
TinyTabs,
TinyTabItem,
TinyButton,
AsyncHighlight
},
setup(props) {
setup(props, { emit }) {
const { templateModeState } = useTemplateMode()
const { currentThemeKey } = useTheme()
const isMobileFirst = computed(() => {
Expand Down Expand Up @@ -213,6 +214,9 @@ export default defineComponent({
const log = `${demoName}示例资源不存在,请检查文件名是否正确?`
cmp.value = <div>{log}</div>
}
nextTick(() => {
emit('mounted')
})
})

const demos = ref(props.demo)
Expand Down
Loading