useSpeak-legacy-923b08dd-1723427255937.js 4.1 KB

1
  1. System.register(["./index-legacy-91c612f3-1723427255937.js"],(function(e,t){"use strict";var o,n,s,c;return{setters:[e=>{o=e.e,n=e.q,s=e.T,c=e.aO}],execute:function(){e("u",(()=>{const e=o({showDom:!1,synth:null,selectOptions:{startIndex:0,anchorOffset:0,endIndex:0,focusOffset:0},isSpeak:!1}),t=e=>{const o=document.createDocumentFragment();return e.childNodes.forEach((e=>{if(e.nodeType===Node.TEXT_NODE)e.textContent.split(/(?<=[,,;;。.])\s*/).forEach((e=>{if(e.trim()){const t=document.createElement("label");t.textContent=e.trim(),t.classList.add("speak-label"),o.appendChild(t)}}));else if(e.nodeType===Node.ELEMENT_NODE){const n=document.createElement(e.nodeName.toLowerCase());Array.from(e.attributes).forEach((e=>{n.setAttribute(e.name,e.value)}));const s=t(e);n.appendChild(s),o.appendChild(n)}})),o},l=e=>{if(e.rangeCount>0){const t=e.anchorNode,o=e.anchorOffset,n=e.focusNode,s=e.focusOffset;if(t===n)return o<s?"up":"down";{const o=e.getRangeAt(0),s=o.startContainer,c=o.endContainer;return s===t&&c===n?"up":"down"}}return"up"},r=()=>{const t=window.getSelection(),o=t.rangeCount>0?t.getRangeAt(0):null;if(t.toString().length>0){e.showDom=!0;const o=document.querySelector("#musicContent"),n=o?.querySelectorAll("label.speak-label");let s=0,c=0,r=0,a=0;console.log(t,"selection");const i="up"===l(t)?t.anchorNode.parentNode:t.focusNode.parentNode,d="down"===l(t)?t.anchorNode.parentNode:t.focusNode.parentNode;"up"===l(t)?(c=t.anchorOffset,a=t.focusOffset):(c=t.focusOffset,a=t.anchorOffset),n.forEach(((e,o)=>{e===i&&(s=o,c="up"===l(t)?t.anchorOffset:t.focusOffset),e===d&&(r=o,a="down"===l(t)?t.anchorOffset:t.focusOffset)})),e.selectOptions.startIndex=s,e.selectOptions.anchorOffset=c,e.selectOptions.endIndex=r,e.selectOptions.focusOffset=a}else e.showDom=!1;if(setTimeout((()=>{"Range"!==t.type&&(e.showDom=!1)}),200),o&&!t.isCollapsed){const e=o.getClientRects();if(e.length>0){const t=e[0],o=t.left,n=t.top,s=t.bottom,c=t.height,l=document.querySelector("#musicContent"),r=l?.getBoundingClientRect(),a=document.getElementById("selectionCouser"),i=a.getBoundingClientRect();n-r?.top>i.height+c/2?a.style.top=(n-r?.top-(i.height+c/2)+l?.scrollTop).toFixed(2)+"px":(console.log(!1,r?.bottom-s+(i.height+c/2)+l?.scrollTop),a.style.top=(n-r?.top+(i.height+c/2)+l?.scrollTop).toFixed(2)+"px"),r?.width-(o-r?.left)>i.width?(a.style.left=(o-r?.left).toFixed(2)+"px",a.style.right="auto"):(a.style.right="0px",a.style.left="auto")}}};function a(){window.getSelection?window.getSelection()?.removeAllRanges():document.selection&&document.selection.empty(),e.showDom=!1}const i=()=>{e.synth?.cancel(),e.isSpeak=!1;const t=document.querySelector("#musicContent"),o=t?.querySelectorAll("label.speak-label");o.forEach(((e,t)=>{e.classList.toggle("highlight",-1===t)})),a()},d=t=>{const o=document.querySelector("#musicContent"),n=o?.querySelectorAll("label.speak-label");let s=t.startIndex||0;const c=t.endIndex||n.length-1,l=e=>{n.forEach(((t,o)=>{t.classList.toggle("highlight",o===e)}));const t=o?.querySelector(".highlight");t?.scrollIntoView({behavior:"smooth",block:"center"})},r=()=>{try{e.synth=window.speechSynthesis,e.synth.speaking&&e.synth.cancel();let o=n[s].textContent;if(o.length<=0)return void console.error("暂无播放内容");t.startIndex===t.endIndex?o=o.substr(t.anchorOffset,(t.focusOffset||0)-(t.anchorOffset||0)):(t.startIndex===s&&(o=o.substr(t.anchorOffset,o.length-1)),t.endIndex===s&&(o=o.substr(0,t.focusOffset)));const a=new SpeechSynthesisUtterance;a.lang="zh-CN",a.volume=1,a.rate=.8,a.pitch=1.5,a.text=o,a&&(a.onstart=null,a.onend=null,a.onerror=null),a.onstart=()=>{e.isSpeak=!0,l(s)},a.onend=()=>{console.log("朗读结束"),s++,s<=c&&e.isSpeak?r():(s=0,l(-1),e.isSpeak=!1)},a.onerror=()=>{s++,s<=c&&e.isSpeak?r():e.isSpeak=!1},setTimeout((()=>{e.synth.speak(a)}),100)}catch(o){console.log(o,"e")}};r()};return n((async()=>{document.addEventListener("mouseup",r)})),s((()=>{document.removeEventListener("mouseup",r),i()})),{...c(e),onAllSpeak:()=>{a();try{d({startIndex:0})}catch(e){console.log(e,"12")}},onTextStart:()=>{i(),d({startIndex:e.selectOptions.startIndex,anchorOffset:e.selectOptions.anchorOffset})},onCloseSpeak:i,onTextReadOnly:()=>{i(),d(e.selectOptions)},processNode:t}}))}}}));