useSpeak-591a8844-1734950731618.js 7.4 KB

1
  1. import{e as q,q as b,T as R,aR as L}from"./index-efb66432-1734950731618.js";const U="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAAXNSR0IArs4c6QAAAQJJREFUWEftmD0PAUEQhmdWcw0aLjlR+fhbOtHQoFQq7xQaEZ2/RVRESBTCiUjsiER19nZOKE4y197s7jvPO7tze+j5miBFD4ogxg0hxJXrfxBSiAMNsLdloxByWpNvilEKu5rgbB0P4GqiYTTGSCijsL7u4tI2YW1MxfBGRtHKwcKmjQfb+PKIandNi/QLKvnUiap0sjBftfBoy7A6IfcS0s5oWQJClSnlrydovBHiqj7u/beWxc2LIog7qYWQEAKAJAej7DKudQghISS9jGspQkgIvQj85weaF1AzaqECyGqi4JNr0LaHM64UEhH61Q+JbV+x67EBz4xEEOerEEoToQcvexD4nAzBpwAAAABJRU5ErkJggg==",Q="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADgAAAA4CAYAAACohjseAAAAAXNSR0IArs4c6QAAAodJREFUaEPtmz9oFFEQxn+zhSnuzs4gQcFLxMLCTkghEivFQtDWXjClhY2YImJjYWcE+7QKgmCXIBaBlIKgmD3BEES7271Cix15m+xxSe7cP9m97D3etjc7M983376dt29OyHip6onvPa6pcgu4iDKjMAM0M7ooahYK7CDsAJ9FeHuuwZqI/M3iUNKMOqGejpQllLvAyTT7Mf3eRVj1hOV2U37+L+ZIgKo61Ql5hPJAoTGmxHOFEeghPG83eSoif4bdPBSgqZpGvFGYzxXxmIwFNsTj9rBqHgLoh3pJlXcoZ44p32JhhW2Em3NN+TToYB/Avedtc+LAJYiEbU+4PFjJPsD4mQtYnxRZjiqzkWu7xULyTPYB+oEuq/K4mD7qdZcIT2ZbsmSyigHuLSrf6rpa5qXPrK7icd5INQa4FegKyv28jmptL7yca8mimA7FD/hdo5d4Wbx1Z1uckk6o16OI92V5rZMfz+OG+IG+UGWxTomVlYsIKwbgmioLZTmtk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zd46kBxwUt+hT1yJaZdPFO0Zl46zFoDegRtKC61AoGJYeHM7WCHkELqkutYFByeDgT+8FvGv/RffyXL7Ffn0V/ARr9FbZbeaMuQnCA0ZeROMioC4EcYPSlXHmq9o04i/Hy17yoyylzyKgLYnPIqEuah3cl0Ral/731qupnBb8BLD56cQ63aRAAAAAASUVORK5CYII=",K=(O,N)=>{const g=O?"#"+O:"#musicContent",I=N?"#"+N:"#selectionCouser",n=q({showDom:!1,synth:null,selectOptions:{startIndex:0,anchorOffset:0,endIndex:0,focusOffset:0},isSpeak:!1}),C=e=>{var r;const a=document.createDocumentFragment();return(r=e.childNodes)==null||r.forEach(o=>{if(o.nodeType===Node.TEXT_NODE){const s=o.textContent.split(new RegExp("(?<=[,,;;。])\\s*"));s==null||s.forEach(f=>{if(f.trim()){const d=document.createElement("label");d.textContent=f.trim(),d.classList.add("speak-label"),a.appendChild(d)}})}else if(o.nodeType===Node.ELEMENT_NODE){const s=document.createElement(o.nodeName.toLowerCase());Array.from(o.attributes).forEach(d=>{s.setAttribute(d.name,d.value)});const f=C(o);s.appendChild(f),a.appendChild(s)}}),a},m=e=>{if(e.rangeCount>0){const a=e.anchorNode,r=e.anchorOffset,o=e.focusNode,s=e.focusOffset;if(a===o)return r<s?"up":"down";{const f=e.getRangeAt(0),d=f.startContainer,h=f.endContainer;return d===a&&h===o?"up":"down"}}else return"up"},S=()=>{const e=window.getSelection(),a=e.rangeCount>0?e.getRangeAt(0):null;if(e.toString().length>0){n.showDom=!0;const r=document.querySelector(g),o=r==null?void 0:r.querySelectorAll("label.speak-label");let s=0,f=0,d=0,h=0;if(e.focusOffset===0&&e.anchorOffset===0){const t=e.anchorNode.parentNode.parentElement,i=t.childNodes[0],A=t.childNodes[t.childNodes.length-1];o==null||o.forEach((l,u)=>{l===i&&(s=u,f=0),l===A&&(d=u,h=A.textContent.length)})}else{const c=m(e)==="up"?e.anchorNode.parentNode:e.focusNode.parentNode,t=m(e)==="down"?e.anchorNode.parentNode:e.focusNode.parentNode;m(e)==="up"?(f=e.anchorOffset,h=e.focusOffset):(f=e.focusOffset,h=e.anchorOffset),o==null||o.forEach((i,A)=>{i===c&&(s=A,f=m(e)==="up"?e.anchorOffset:e.focusOffset),i===t&&(d=A,h=m(e)==="down"?e.anchorOffset:e.focusOffset)})}n.selectOptions.startIndex=s,n.selectOptions.anchorOffset=f,n.selectOptions.endIndex=d,n.selectOptions.focusOffset=h}else n.showDom=!1;if(setTimeout(()=>{e.type!=="Range"&&(n.showDom=!1)},200),a&&!e.isCollapsed){const r=a.getClientRects();if(r.length>0){const o=r[r.length-1],s=o.right,f=o.top;o.bottom;const d=o.height,h=document.querySelector(g),c=h==null?void 0:h.getBoundingClientRect(),t=document.querySelector(I),i=t==null?void 0:t.getBoundingClientRect();t&&(t.style.top=(f-(c==null?void 0:c.top)+(i.height+d/2)+(h==null?void 0:h.scrollTop)).toFixed(2)+"px",(c==null?void 0:c.right)-s>=(c==null?void 0:c.width)-i.width?(t.style.right=((c==null?void 0:c.width)-i.width).toFixed(2)+"px",t.style.left="auto"):(t.style.right=((c==null?void 0:c.right)-s-6).toFixed(2)+"px",t.style.left="auto"))}}},v=()=>{x(),E({startIndex:n.selectOptions.startIndex,anchorOffset:n.selectOptions.anchorOffset})},D=()=>{x(),E(n.selectOptions)};function T(){var e;window.getSelection?(e=window.getSelection())==null||e.removeAllRanges():document.selection&&document.selection.empty(),n.showDom=!1}const x=()=>{var r;(r=n.synth)==null||r.cancel(),n.isSpeak=!1;const e=document.querySelector(g),a=e==null?void 0:e.querySelectorAll("label.speak-label");a==null||a.forEach((o,s)=>{o.classList.toggle("highlight",s===-1)}),T()},F=()=>{T();try{E({startIndex:0})}catch(e){console.log(e,"12")}},E=e=>{const a=document.querySelector(g),r=a==null?void 0:a.querySelectorAll("label.speak-label");let o=e.startIndex||0;const s=e.endIndex===void 0?r.length-1:e.endIndex,f=t=>{r==null||r.forEach((A,l)=>{A.classList.toggle("highlight",l===t)});const i=a==null?void 0:a.querySelector(".highlight");h(i)};function d(t,i){let A=0;for(;t&&t!==i;)A+=t.offsetTop,t=t.offsetParent;return A}function h(t){var y;const i=document.querySelector(g),A=i.getBoundingClientRect(),l=d(t,i),u=A.height/2;let p=0;console.log(d(t,i),"12121"),l-u>=0?p=l-u:p=0,(y=document.querySelector(g))==null||y.scrollTo({top:p,behavior:"smooth"})}const c=()=>{try{n.synth=window.speechSynthesis,n.synth.speaking&&n.synth.cancel();let t=r[o].textContent;if(t.length<=0){console.error("暂无播放内容");return}e.startIndex===e.endIndex&&e.endIndex!==void 0?t=t.substr(e.anchorOffset,(e.focusOffset||0)-(e.anchorOffset||0)):(e.startIndex===o&&(t=t.substr(e.anchorOffset,t.length)),e.endIndex===o&&(t=t.substr(0,e.focusOffset)));const i=["长笛","打击乐","乐曲","曲"],A=["尝笛","打击月","月取","取"];t&&i.forEach((u,p)=>{if(t.includes(u)){const y=new RegExp(u,"g");t=t.replace(y,A[p])}}),console.log(t,o,s,"---------");const l=new SpeechSynthesisUtterance(t);l.lang="zh-CN",l.volume=1,l.rate=.8,l.pitch=1.5,l&&(l.onstart=null,l.onend=null,l.onerror=null),l.onstart=()=>{n.isSpeak=!0,f(o)},l.onend=()=>{console.log("朗读结束"),o++,o<=s&&n.isSpeak?c():(o=0,f(-1),n.isSpeak=!1)},l.onerror=()=>{o++,o<=s&&n.isSpeak?c():n.isSpeak=!1},setTimeout(()=>{n.synth.speak(l)},80)}catch(t){console.log(t,"e")}};c()},w=()=>{document.removeEventListener("mouseup",S),document.removeEventListener("touchend",S),x()};return b(async()=>{document.addEventListener("mouseup",S),document.addEventListener("touchend",S)}),R(()=>{w()}),{...L(n),onAllSpeak:F,onTextStart:v,onDestory:w,onCloseSpeak:x,onTextReadOnly:D,processNode:C}};export{Q as a,B as b,U as i,K as u};