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

feat(docs): the official website adds API anchor function #2366

Merged
merged 2 commits into from
Oct 24, 2024
Merged
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
12 changes: 9 additions & 3 deletions examples/sites/src/views/components/components.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
</template>
</div>
</tiny-tab-item>
<tiny-tab-item v-if="showApiTab && !isRunningTest" title="API" name="api">
<tiny-tab-item v-if="showApiTab && !isRunningTest && currJson.apis?.length" title="API" name="api">
<!-- api文档 -->
<div id="API" class="all-api-container">
<div class="ti-f-c ti-f-wrap api-list">
Expand Down Expand Up @@ -248,6 +248,7 @@

<script lang="jsx">
import { defineComponent, reactive, computed, toRefs, watch, onMounted, ref, onUnmounted, nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { marked } from 'marked'
import hljs from 'highlight.js'
import { Anchor, ButtonGroup, Grid, GridColumn, Tabs, TabItem, Tooltip } from '@opentiny/vue'
Expand Down Expand Up @@ -279,6 +280,8 @@ export default defineComponent({
const isRunningTest = localStorage.getItem('tiny-e2e-test') === 'true'
const anchorRefreshKey = ref(0)
const apiTableRef = ref()
const route = useRoute()

const state = reactive({
webDocPath: computed(() => ''),
langKey: getWord('zh-CN', 'en-US'),
Expand Down Expand Up @@ -310,7 +313,7 @@ export default defineComponent({
currAnchorLinks: computed(() => (state.activeTab === 'demos' ? state.demoAnchorLinks : state.apiAnchorLinks)),
// 单demo显示时
singleDemo: null,
activeTab: 'demos',
activeTab: route.hash === '#api' ? 'api' : 'demos',
tableData: {},
currApiTypes: [],
showApiTab: computed(() => state.currApiTypes.length),
Expand Down Expand Up @@ -616,7 +619,8 @@ export default defineComponent({
copyText: (text) => {
navigator.clipboard.writeText(text)
},
onTabsClick: () => {
onTabsClick: (data) => {
router.push(`#${data.name}`)
scrollToLayoutTop()
Comment on lines +622 to 624
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider conditional scroll behavior for tab switching.

The current implementation always scrolls to the top when switching tabs. This might not be the desired behavior when switching to the API tab, as users might want to maintain their scroll position.

Consider updating the implementation to only scroll when necessary:

 onTabsClick: (data) => {
   router.push(`#${data.name}`)
-  scrollToLayoutTop()
+  if (data.name !== 'api') {
+    scrollToLayoutTop()
+  }
 }
📝 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
onTabsClick: (data) => {
router.push(`#${data.name}`)
scrollToLayoutTop()
onTabsClick: (data) => {
router.push(`#${data.name}`)
if (data.name !== 'api') {
scrollToLayoutTop()
}

},
// 点击 api区域的 name列时
Expand Down Expand Up @@ -676,6 +680,8 @@ export default defineComponent({
state.currJson = {}
} else {
loadPage()
// 切换组件时tabs激活页变成demos
state.activeTab = 'demos'
Comment on lines +683 to +684
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider preserving the active tab based on URL hash.

When switching components, the code always resets to the 'demos' tab. This might not be the desired behavior when users navigate via API anchor links or switch components while viewing API documentation.

Consider this implementation:

-state.activeTab = 'demos'
+state.activeTab = route.hash === '#api' ? 'api' : 'demos'

Committable suggestion was skipped due to low confidence.

// 每次切换组件都需要让锚点组件重新刷新
anchorRefreshKey.value++
}
Expand Down
Loading