|
@@ -180,6 +180,15 @@ export const useSpeak = () => {
|
|
|
musicContent?.scrollTop
|
|
|
).toFixed(2) + 'px';
|
|
|
}
|
|
|
+ console.log({
|
|
|
+ parentRectWidth: parentRect?.width,
|
|
|
+ firstRectLeft: x,
|
|
|
+ parentRectLeft: parentRect?.left,
|
|
|
+ parentRectStatus:
|
|
|
+ parentRect?.width - (x - parentRect?.left) > showDomRect.width,
|
|
|
+ diff: parentRect?.width - (x - parentRect?.left),
|
|
|
+ showDomRect: showDomRect.width
|
|
|
+ });
|
|
|
if (parentRect?.width - (x - parentRect?.left) > showDomRect.width) {
|
|
|
// 判断是否选择到最右边 超出边界
|
|
|
showDom.style.left = (x - parentRect?.left).toFixed(2) + 'px';
|
|
@@ -257,8 +266,11 @@ export const useSpeak = () => {
|
|
|
const textContainer: any = document.querySelector('#musicContent');
|
|
|
const sentences: any = textContainer?.querySelectorAll('label.speak-label');
|
|
|
|
|
|
+ // console.log(options, '--endIndex');
|
|
|
let currentSentenceIndex = options.startIndex || 0;
|
|
|
- const end = options.endIndex || sentences.length - 1;
|
|
|
+ const end =
|
|
|
+ options.endIndex === undefined ? sentences.length - 1 : options.endIndex;
|
|
|
+
|
|
|
// 高亮显示
|
|
|
const highlightSentence = (index: number) => {
|
|
|
sentences.forEach((sentence: any, i: number) => {
|
|
@@ -277,6 +289,12 @@ export const useSpeak = () => {
|
|
|
const speaker = () => {
|
|
|
try {
|
|
|
state.synth = window.speechSynthesis;
|
|
|
+
|
|
|
+ // 获取可用的 voice 列表
|
|
|
+ // const voices = speechSynthesis.getVoices();
|
|
|
+ // 选择一个特定的 voice
|
|
|
+ // const voice = voices.find(voice => voice.lang === 'zh-CN');
|
|
|
+ // console.log(voice, 'voice');
|
|
|
// 如果当前正在播放,先暂停
|
|
|
if (state.synth.speaking) {
|
|
|
state.synth.cancel(); // 取消当前播放
|
|
@@ -288,23 +306,36 @@ export const useSpeak = () => {
|
|
|
return;
|
|
|
}
|
|
|
// 判断是否为选中的内容播放
|
|
|
- if (options.startIndex === options.endIndex) {
|
|
|
+ if (
|
|
|
+ options.startIndex === options.endIndex &&
|
|
|
+ options.endIndex !== undefined
|
|
|
+ ) {
|
|
|
sentence = sentence.substr(
|
|
|
options.anchorOffset,
|
|
|
(options.focusOffset || 0) - (options.anchorOffset || 0)
|
|
|
);
|
|
|
} else {
|
|
|
if (options.startIndex === currentSentenceIndex) {
|
|
|
- sentence = sentence.substr(
|
|
|
- options.anchorOffset,
|
|
|
- sentence.length - 1
|
|
|
- );
|
|
|
+ sentence = sentence.substr(options.anchorOffset, sentence.length);
|
|
|
}
|
|
|
if (options.endIndex === currentSentenceIndex) {
|
|
|
sentence = sentence.substr(0, options.focusOffset);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ const replaceText = ['长笛', '作曲', '曲目', '琴曲'];
|
|
|
+ const afterReplaceText = ['尝笛', '作取', '取目', '琴取'];
|
|
|
+
|
|
|
+ if (sentence) {
|
|
|
+ replaceText.forEach((item: string, index: number) => {
|
|
|
+ if (sentence.includes(item)) {
|
|
|
+ const regex = new RegExp(item, 'g');
|
|
|
+ sentence = sentence.replace(regex, afterReplaceText[index]);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log(sentence, currentSentenceIndex, end, '---------');
|
|
|
const utterance = new SpeechSynthesisUtterance();
|
|
|
utterance.lang = 'zh-CN';
|
|
|
utterance.volume = 1;
|
|
@@ -345,7 +376,7 @@ export const useSpeak = () => {
|
|
|
};
|
|
|
setTimeout(() => {
|
|
|
state.synth.speak(utterance);
|
|
|
- }, 100);
|
|
|
+ }, 80);
|
|
|
} catch (e) {
|
|
|
console.log(e, 'e');
|
|
|
}
|