//右侧阅读选项面板 import React from "react"; import "./settingPanel.css"; import ThemeList from "../../components/themeList"; import FontSizeList from "../../components/fontSizeList"; import DropdownList from "../../components/dropdownList"; import ModeControl from "../../components/modeControl"; import { SettingPanelProps, SettingPanelState } from "./interface"; import { Trans } from "react-i18next"; class SettingPanel extends React.Component< SettingPanelProps, SettingPanelState > { constructor(props: SettingPanelProps) { super(props); this.state = { isSupported: false, isAudioOn: false, }; } componentDidMount() { if ("speechSynthesis" in window) { this.setState({ isSupported: true }); } if (this.state.isAudioOn) { window.speechSynthesis && window.speechSynthesis.cancel(); this.setState({ isAudioOn: false }); } } handleChangeAudio = () => { if (this.state.isAudioOn) { window.speechSynthesis.cancel(); this.setState({ isAudioOn: false }); } else { this.setState({ isAudioOn: true }, () => { this.handleAudio(); }); } }; handleAudio = () => { const currentLocation = this.props.currentEpub.rendition.currentLocation(); const cfibase = currentLocation.start.cfi .replace(/!.*/, "") .replace("epubcfi(", ""); const cfistart = currentLocation.start.cfi .replace(/.*!/, "") .replace(/\)/, ""); const cfiend = currentLocation.end.cfi.replace(/.*!/, "").replace(/\)/, ""); const cfiRange = `epubcfi(${cfibase}!,${cfistart},${cfiend})`; this.props.currentEpub.getRange(cfiRange).then((range: any) => { let text = range.toString(); text = text.replace(/\s\s/g, ""); text = text.replace(/\r/g, ""); text = text.replace(/\n/g, ""); text = text.replace(/\t/g, ""); text = text.replace(/\f/g, ""); var msg = new SpeechSynthesisUtterance(); msg.text = text; window.speechSynthesis.speak(msg); msg.onend = (event) => { console.log( "Utterance has finished being spoken after " + event.elapsedTime + " milliseconds." ); if (!this.state.isAudioOn || this.props.isReading) { return; } this.props.currentEpub.rendition.next(); this.handleAudio(); }; }); }; render() { return (
Reading Option
{this.state.isSupported && this.props.locations ? (
{this.state.isAudioOn ? ( Turn off audio ) : ( Turn on audio )} { this.handleChangeAudio(); }} >
) : null}
); } } export default SettingPanel;