feat: add keyboard shortcuts to go prev/next sections, closes #2287 (#2291)

This commit is contained in:
Huang Xin
2025-10-22 13:41:33 +08:00
committed by GitHub
parent 34fbea6a44
commit 34a5e58872
3 changed files with 50 additions and 2 deletions
@@ -53,6 +53,26 @@ const useBookShortcuts = ({ sideBarBookKey, bookKeys }: UseBookShortcutsProps) =
viewPagination(getView(sideBarBookKey), viewSettings, 'right');
};
const goPrevSection = () => {
const viewSettings = getViewSettings(sideBarBookKey ?? '');
viewPagination(getView(sideBarBookKey), viewSettings, 'up', 'section');
};
const goNextSection = () => {
const viewSettings = getViewSettings(sideBarBookKey ?? '');
viewPagination(getView(sideBarBookKey), viewSettings, 'down', 'section');
};
const goLeftSection = () => {
const viewSettings = getViewSettings(sideBarBookKey ?? '');
viewPagination(getView(sideBarBookKey), viewSettings, 'left', 'section');
};
const goRightSection = () => {
const viewSettings = getViewSettings(sideBarBookKey ?? '');
viewPagination(getView(sideBarBookKey), viewSettings, 'right', 'section');
};
const goPrev = () => {
getView(sideBarBookKey)?.prev(distance);
};
@@ -176,6 +196,10 @@ const useBookShortcuts = ({ sideBarBookKey, bookKeys }: UseBookShortcutsProps) =
onGoNext: goNext,
onGoHalfPageDown: goHalfPageDown,
onGoHalfPageUp: goHalfPageUp,
onGoPrevSection: goPrevSection,
onGoNextSection: goNextSection,
onGoLeftSection: goLeftSection,
onGoRightSection: goRightSection,
onGoBack: goBack,
onGoForward: goForward,
onZoomIn: zoomIn,
@@ -12,6 +12,7 @@ import { tauriGetWindowLogicalPosition } from '@/utils/window';
export type ScrollSource = 'touch' | 'mouse';
type PaginationSide = 'left' | 'right' | 'up' | 'down';
type PaginationMode = 'page' | 'section';
const swapLeftRight = (side: PaginationSide) => {
if (side === 'left') return 'right';
@@ -23,6 +24,7 @@ export const viewPagination = (
view: FoliateView | null,
viewSettings: ViewSettings | null | undefined,
side: PaginationSide,
mode: PaginationMode = 'page',
) => {
if (!view || !viewSettings) return;
const renderer = view.renderer;
@@ -35,9 +37,27 @@ export const viewPagination = (
const showFooter = viewSettings.showFooter && viewSettings.showBarsOnScroll;
const scrollingOverlap = viewSettings.scrollingOverlap;
const distance = size - scrollingOverlap - (showHeader ? 44 : 0) - (showFooter ? 44 : 0);
return side === 'left' || side === 'up' ? view.prev(distance) : view.next(distance);
switch (mode) {
case 'page':
return side === 'left' || side === 'up' ? view.prev(distance) : view.next(distance);
case 'section':
if (side === 'left' || side === 'up') {
return view.renderer.prevSection?.();
} else {
return view.renderer.nextSection?.();
}
}
} else {
return side === 'left' || side === 'up' ? view.prev() : view.next();
switch (mode) {
case 'page':
return side === 'left' || side === 'up' ? view.prev() : view.next();
case 'section':
if (side === 'left' || side === 'up') {
return view.renderer.prevSection?.();
} else {
return view.renderer.nextSection?.();
}
}
}
};
@@ -17,6 +17,10 @@ const DEFAULT_SHORTCUTS = {
onGoRight: ['ArrowRight', 'PageDown', 'l', ' '],
onGoNext: ['ArrowDown', 'j'],
onGoPrev: ['ArrowUp', 'k'],
onGoLeftSection: ['opt+ArrowLeft', 'alt+ArrowLeft'],
onGoRightSection: ['opt+ArrowRight', 'alt+ArrowRight'],
onGoPrevSection: ['opt+ArrowUp', 'alt+ArrowUp'],
onGoNextSection: ['opt+ArrowDown', 'alt+ArrowDown'],
onGoHalfPageDown: ['shift+ArrowDown', 'd'],
onGoHalfPageUp: ['shift+ArrowUp', 'u'],
onGoBack: ['shift+ArrowLeft', 'shift+h', 'alt+ArrowLeft'],