useSpeak-d55c73d9-1723427255937.js 4.3 KB

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