Fix TTS might stop at the end of the section (#126)

This commit is contained in:
Huang Xin
2025-01-08 17:50:36 +01:00
committed by GitHub
parent ddee347eb3
commit c6defb97ee
3 changed files with 8 additions and 3 deletions
@@ -23,6 +23,7 @@ import { useFoliateEvents } from '../../hooks/useFoliateEvents';
import { useNotesSync } from '../../hooks/useNotesSync';
import { getPopupPosition, getPosition, Position, TextSelection } from '@/utils/sel';
import { eventDispatcher } from '@/utils/event';
import { getBookLangCode } from '@/utils/book';
import AnnotationPopup from './AnnotationPopup';
import WiktionaryPopup from './WiktionaryPopup';
import WikipediaPopup from './WikipediaPopup';
@@ -339,7 +340,9 @@ const Annotator: React.FC<{ bookKey: string }> = ({ bookKey }) => {
if (!selection || !selection.text) return;
setShowAnnotPopup(false);
await view!.initTTS();
const lang = getBookLangCode(bookData.bookDoc!.metadata.language);
const granularity = ['zh', 'ja', 'ko'].includes(lang) ? 'sentence' : 'word';
await view!.initTTS(granularity);
const ssml = view!.tts.from(selection.range)!;
eventDispatcher.dispatch('speak', { bookKey, ssml });
};
+3 -1
View File
@@ -2,6 +2,8 @@ import { BookDoc } from '@/libs/document';
import { BookNote, BookSearchConfig, BookSearchResult } from '@/types/book';
import { TTS } from 'foliate-js/tts.js';
type TTSGranularity = 'sentence' | 'word';
export interface FoliateView extends HTMLElement {
open: (book: BookDoc) => Promise<void>;
close: () => void;
@@ -18,7 +20,7 @@ export interface FoliateView extends HTMLElement {
clearSearch: () => void;
select: (target: string | number | { fraction: number }) => void;
deselect: () => void;
initTTS: () => Promise<void>;
initTTS: (granularity?: TTSGranularity) => Promise<void>;
tts: TTS;
history: {
canGoBack: boolean;