Skip to content

Commit

Permalink
fix: 修复滚动位置
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Jan 18, 2024
1 parent 69a2d06 commit 7dba76b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store-front-end",
"version": "2.14.77",
"version": "2.14.78",
"private": true,
"scripts": {
"dev": "vite --host",
Expand Down
5 changes: 4 additions & 1 deletion src/components/FileListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
import { computed, createVNode, ref, toRaw } from 'vue';
import useV3Clipboard from 'vue-clipboard3';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useRouter, useRoute } from 'vue-router';
import { useHostAPI } from '@/hooks/useHostAPI';
const { copy, isSupported } = useClipboard();
Expand All @@ -209,6 +209,7 @@
const swipeIsOpen = ref(false);
const compareData = ref();
const router = useRouter();
const route = useRoute();
const globalStore = useGlobalStore();
const subsStore = useSubsStore();
const subsApi = useSubsApi();
Expand Down Expand Up @@ -276,6 +277,8 @@
scrollTop = document.documentElement.scrollTop || document.body.scrollTop
globalStore.setSavedPositions(route.path, { left: 0, top: scrollTop })
document.querySelector('html').style['overflow-y'] = 'hidden';
document.querySelector('html').style.height = '100%';
document.body.style.height = '100%';
Expand Down
5 changes: 4 additions & 1 deletion src/components/SubListItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
import { computed, createVNode, ref, toRaw } from 'vue';
import useV3Clipboard from 'vue-clipboard3';
import { useI18n } from 'vue-i18n';
import { useRouter } from 'vue-router';
import { useRouter, useRoute } from 'vue-router';
import { useHostAPI } from '@/hooks/useHostAPI';
const { copy, isSupported } = useClipboard();
Expand All @@ -234,6 +234,7 @@
const swipeIsOpen = ref(false);
const compareData = ref();
const router = useRouter();
const route = useRoute();
const globalStore = useGlobalStore();
const subsStore = useSubsStore();
const subsApi = useSubsApi();
Expand Down Expand Up @@ -356,6 +357,8 @@
scrollTop = document.documentElement.scrollTop || document.body.scrollTop
globalStore.setSavedPositions(route.path, { left: 0, top: scrollTop })
document.querySelector('html').style['overflow-y'] = 'hidden';
document.querySelector('html').style.height = '100%';
document.body.style.height = '100%';
Expand Down
26 changes: 18 additions & 8 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,31 @@ declare module 'vue-router' {
const history = createWebHistory();
const router = createRouter({
scrollBehavior(to, from, savedPosition) {
// console.log(`scrollBehavior ${from.path} => ${to.path}`)
document.querySelector('html').style['overflow-y'] = '';
document.querySelector('html').style.height = '';
document.body.style.height = '';
document.body.style['overflow-y'] = '';
(document.querySelector('#app') as HTMLElement).style['overflow-y'] = '';
(document.querySelector('#app') as HTMLElement).style.height = '';

if (to.hash) {
return {
el: to.hash,
behavior: 'smooth',
}
}
if (globalStore !== null) {
const savedPositions = toRaw(globalStore.savedPositions);
if (savedPositions[to.path]) {
// console.log(`读取到 ${to.path} 保存的滚动位置:${savedPositions[to.path]?.top}`)
return savedPositions[to.path]
}
}
if (savedPosition) {
// console.log(`接受到 ${to.path} savedPosition 滚动位置:${savedPosition?.top}`)
return savedPosition
} else {
if (globalStore !== null && to?.meta?.needTabBar) {
const savedPositions = toRaw(globalStore.savedPositions);
if (savedPositions[to.path]) {
// console.log(`读取到 ${to.path} 保存的滚动位置:${savedPositions[to.path]?.top}`)
return savedPositions[to.path]
}
}
return { top: 0, left: 0 }
}
},
Expand Down Expand Up @@ -189,7 +197,9 @@ const router = createRouter({
// return true;
// });
router.beforeEach((to, from) => {
if (from?.meta?.needTabBar) {
// console.log(`beforeEach ${from.path} => ${to.path}`)
if (from?.meta?.needTabBar && from?.path !== to?.path) {
// if (from?.meta?.needTabBar) {
const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
// console.log(`保存 ${from.path} 滚动位置:${scrollTop}`)
globalStore.setSavedPositions(from.path, { left: 0, top: scrollTop })
Expand Down
2 changes: 2 additions & 0 deletions src/views/FileEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@
scrollTop = document.documentElement.scrollTop || document.body.scrollTop
globalStore.setSavedPositions(route.path, { left: 0, top: scrollTop })
document.querySelector('html').style['overflow-y'] = 'hidden';
document.querySelector('html').style.height = '100%';
document.body.style.height = '100%';
Expand Down
2 changes: 2 additions & 0 deletions src/views/SubEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@
scrollTop = document.documentElement.scrollTop || document.body.scrollTop
globalStore.setSavedPositions(route.path, { left: 0, top: scrollTop })
document.querySelector('html').style['overflow-y'] = 'hidden';
document.querySelector('html').style.height = '100%';
document.body.style.height = '100%';
Expand Down

0 comments on commit 7dba76b

Please sign in to comment.