-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.min.js
1 lines (1 loc) · 13 KB
/
index.min.js
1
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).SelectionHandian=e()}(this,function(){"use strict";function o(e,t){var n,o=Object.keys(e);return Object.getOwnPropertySymbols&&(n=Object.getOwnPropertySymbols(e),t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),o.push.apply(o,n)),o}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach(function(t){a(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function s(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function a(t,e,n){return e in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}var r={MAX_TIME_OUT:5e3,scroller:void 0,container:document.body,el:document.body,popup:null,offsetX:2,offsetY:15,onEnd:function(){}},l="mobile",p="tablet",d=(a(t={},l,{START:"touchstart",END:"touchend"}),a(t,p,{START:"touchstart",END:"touchend"}),a(t,"pc",{START:"mousedown",END:"mouseup"}),t);function e(){var t=window.navigator.userAgent;return/iPad|iPhone|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP|IEMobile|Symbian/g.test(t)}function c(){return window.screen.width<767&&e()}function u(){var t="pc";return c()?t="mobile":767<window.screen.width&&e()&&(t="tablet"),t}function h(t,n){return t&&n&&((Array.isArray(t)?t:[t]).forEach(function(e){Object.keys(n).forEach(function(t){e.style[t]=n[t]})}),t)}var t=function(){function e(){var n=this,t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),a(this,"injectStyles",function(){var t=document.createElement("style");return t.type="text/css",t.innerHTML='\nbody * {\n -webkit-touch-callout: none;\n}\n.ly-popup-button {\n will-change: transform, opacity;\n transition: all 350ms cubic-bezier(0.4, 0, 0.2, 1);\n}\n.ly-popup-cotainer.pc {\n border-radius: 3px;\n}\n.ly-popup-cotainer.mobile {\n border-top-left-radius: 12px;\n border-top-right-radius: 12px;\n}\n.ly-popup-cotainer .ly-popup-bar {\n position: absolute;\n left: 0;\n top: 0;\n width: 100%;\n height: 60px;\n background-color: transparent;\n text-align: center;\n z-index: 1000;\n line-height: 0;\n}\n.ly-popup-cotainer .ly-popup-bar::after {\n content: "";\n display: inline-block;\n background-color: #BBBABE;\n border-radius: 5px;\n width: 36px;\n height: 5px;\n margin-top: 9px;\n}\n.ly-popup-cotainer .ly-popup-close {\n position: absolute;\n right: 6px;\n top: 7px;\n width: 30px;\n height: 30px;\n z-index: 1001;\n cursor: pointer;\n}\n.ly-popup-cotainer .ly-popup-close > svg {\n width: 100%;\n height: 100%;\n}\n.ly-popup-cotainer .handian-loading {\n opacity: 1;\n will-change: transform, opacity;\n}\n.ly-popup-cotainer .handian-loading.hide {\n transition: opacity,transform 0.25s cubic-bezier(0, 0, 0.2, 1);\n}\n.ly-popup-cotainer.mobile .handian-loading.hide {\n opacity: 0;\n visibility: hidden;\n}\n.ly-popup-cotainer.pc .handian-loading.hide {\n transform: translateY(-100%);\n}\n',document.head.appendChild(t),t}),a(this,"getRelativePos",function(t){return[l,p].includes(n.state.platform)?{x:t.changedTouches[0].clientX||0,y:t.changedTouches[0].clientY||0}:{x:t.clientX||0,y:t.clientY||0}}),a(this,"toggleButton",function(t){n.nodes.button&&(n.state.buttionVisible=void 0===t?!n.state.buttionVisible:t,n.state.buttionVisible?(n.nodes.button.style.opacity=1,n.nodes.button.style.pointerEvents="auto"):(n.nodes.button.style.opacity=0,n.nodes.button.style.pointerEvents="none"))}),a(this,"togglePopup",function(t){n.nodes.popup&&(n.state.popupVisible=void 0===t?!n.state.popupVisible:t,n.state.popupVisible?(h([document.documentElement,n.options.container],{overflow:"hidden"}),n.nodes.popup.style.display="block",n.state.isMobile&&(n.nodes.mask.style.opacity=1,n.nodes.mask.style.visibility="visible",n.nodes.mask.style.zIndex=1200),clearTimeout(n.state.popupTimer)):(h([document.documentElement,n.options.container],{overflow:""}),n.state.isMobile?(n.nodes.popup.style.transform="translateY(100%)",n.nodes.mask.style.opacity=0,n.state.popupTimer=setTimeout(function(){n.nodes.popup.style.display="none",n.nodes.mask.style.zIndex=-1,n.nodes.mask.style.visibility="hidden"},350)):n.nodes.popup.style.display="none"))}),a(this,"toggleLoading",function(t){var e;n.nodes.loading&&(e=document.getElementById("handian-loading"),n.state.loadingVisible=void 0===t?!n.state.loadingVisible:t,n.state.loadingVisible?e.classList.remove("hide"):e.classList.add("handian-loading","hide"))}),a(this,"hideAll",function(){n.toggleButton(!1),n.togglePopup(!1),n.toggleLoading(!1)}),this.options=i(i({},r),t),this.stopPropagationNodes=[],this.state={},this.nodes={},this.init()}var t,n,o;return t=e,(n=[{key:"init",value:function(){this.state.isMobile=c(),this.state.platform=u(),this.state.isMobile&&this.createMask(),this.createPopup(),this.injectStyles(),this.bindEvents(),this.prefetchHanDian()}},{key:"bindEvents",value:function(){var n,o,i,s,a,r,l,p=this,t=this.options.el;t.addEventListener("contextmenu",function(){p.togglePopup(!1)}),t.addEventListener(d[this.state.platform].END,function(t){var e,n;t.target!==p.nodes.button&&("Range"===(e=window.getSelection()).type?(n=e.toString(),(n=(n=n,n=String(n).trim().replace(/([。,!~【】()()])|(\d)|(\s*)/gi,"").replace(/[a-zA-Z]/g,""),/[^\u0000-\u00FF]/.test(n)?n:""))&&(p.createButton(t,n,e),p.togglePopup(!1),p.toggleButton(p.state.trimText!==n||void 0),p.state.trimText=n)):p.hideAll())}),this.stopPropagationNodes.forEach(function(t){t.addEventListener(d[p.state.platform].END,function(t){t.stopPropagation()})}),this.nodes.closeBtn.addEventListener(d[this.state.platform].END,function(){p.hideAll()}),t=this.nodes.bar,n=this.nodes.popup,o=this.hideAll,t&&n&&(i=n.clientHeight,s=n.style.transition,l=r=a=0,t.addEventListener("touchstart",function(t){t.preventDefault();var e=t.changedTouches[0]||t;e.pageX;e=e.pageY;a=e,r=t.timeStamp,s=n.style.transition,i=n.clientHeight}),t.addEventListener("touchmove",function(t){t.preventDefault();t=(t.changedTouches[0]||t).pageY,t=Math.max(0,t-a);n.style.transition="",n.style.transform="translateY(".concat(t,"px)")}),t.addEventListener("touchend",function(t){t.preventDefault();var e=(t.changedTouches[0]||t).pageY-a,t=t.timeStamp-r;l=e/t,n.style.transition=s,i/2<e||1<l?o&&o({moveDis:e,speed:l}):n.style.transform="translateY(0%)"}))}},{key:"createButton",value:function(e,n,t){var o=this,i=this.options.onEnd,s=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,a=t.getRangeAt(0).getBoundingClientRect(),t=this.getRelativePos(e),r=Math.min(a.left+a.width,t.x),l=a.top+a.height+s;return this.nodes.button||((s=document.createElement("div")).setAttribute("class","ly-popup-button"),s.innerText="典",s.title="查汉典",h(s,{position:"absolute",left:0,top:0,display:"flex",justifyContent:"center",alignItems:"center",width:"32px",height:"32px",borderRadius:"3px",backgroundColor:"#f5f5f5",fontFamily:"STSong, STFangSong",fontWeight:500,color:"#9d6a51",cursor:"pointer",zIndex:"1200",fontSize:"18px",userSelect:"none"}),document.body.appendChild(s),this.nodes.button=s),h(this.nodes.button,{transform:"translate(".concat(r+this.options.offsetX,"px, ").concat(l+this.options.offsetY/2,"px)")}),this.nodes.button["on".concat(d[this.state.platform].END)]=function(){o.toggleButton(!1);var t=o.createHanDian(n);o.nodes.popup.appendChild(t),i(e,n,o),o.state.isMobile?h(o.nodes.popup,{left:"0px",bottom:"0px",transform:"translateY(100%)"}):h(o.nodes.popup,{left:"".concat(r,"px"),top:"".concat(l+2*o.options.offsetY,"px")}),o.togglePopup(!0);t=o.calcPosition({x:r,y:l+o.options.offsetY},o.nodes.popup);o.callPosition(o.nodes.popup,t)},this.nodes.button}},{key:"createMask",value:function(){var t=document.createElement("div");t.setAttribute("class","ly-popup-mask ".concat(this.state.platform)),h(t,{position:"fixed",visibility:"hidden",left:0,top:0,width:"100%",height:"100%",zIndex:-1,backgroundColor:"rgba(0,0,0,0.5)","-webkit-tap-highlight-color":"transparent",opacity:0,transition:"opacity 300ms cubic-bezier(0.4, 0, 0.2, 1) 0ms"}),this.options.container.appendChild(t),this.nodes.mask=t}},{key:"createPopup",value:function(){var t=document.createElement("div");t.setAttribute("class","ly-popup-cotainer ".concat(this.state.platform)),h(t,{position:this.state.isMobile?"fixed":"absolute",display:"none",zIndex:"1201",left:0,top:this.state.isMobile?"":"0",minWidth:this.state.isMobile?"100vw":"375px",minHeight:this.state.isMobile?"60vh":"375px",backgroundColor:"#fff",boxShadow:"rgb(0 0 0 / 8%) 1px 2px 13px 0px",overflow:"hidden"});var e=document.createElement("div");e.setAttribute("class","ly-popup-bar");var n=document.createElement("div");n.setAttribute("class","ly-popup-close"),n.innerHTML='<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1636784066087" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="4964" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M512 0a512 512 0 0 0-512 512 512 512 0 0 0 512 512 512 512 0 0 0 512-512 512 512 0 0 0-512-512z" fill="#efd0c9" p-id="4965"></path><path d="M717.165714 306.176a35.986286 35.986286 0 0 0-50.834285 0.146286L512 461.019429 357.668571 306.322286a35.986286 35.986286 0 0 0-50.980571 50.761143L461.165714 512 306.688 666.916571a35.986286 35.986286 0 0 0 50.980571 50.761143L512 562.980571l154.331429 154.843429a35.693714 35.693714 0 0 0 50.834285 0.073143 35.986286 35.986286 0 0 0 0.146286-50.907429L562.834286 512l154.331428-154.916571a35.913143 35.913143 0 0 0 0-50.907429z" fill="#AE060F" p-id="4966"></path></svg>';var o=document.createElement("p");return o.innerHTML="加載結果中,請稍候……",o.id="handian-loading",o.setAttribute("class","handian-loading hide"),h(o,{position:"absolute",left:0,top:0,display:"flex",justifyContent:"center",alignItems:"center",width:"100%",height:this.state.isMobile?"100%":"20px",zIndex:"99",textAlign:"center",fontSize:"12px",backgroundColor:"rgba(248, 249, 251, 0.8)",color:"#555",margin:"0",overflow:"hidden"}),t.appendChild(o),this.state.isMobile&&t.appendChild(e),t.appendChild(n),document.body.appendChild(t),this.nodes.popup=t,this.nodes.loading=o,this.nodes.bar=e,this.nodes.closeBtn=n,this.stopPropagationNodes.push(e,n),t}},{key:"createHanDian",value:function(){var t=this,e=0<arguments.length&&void 0!==arguments[0]?arguments[0]:"",n=document.getElementById("handian_content");n&&this.nodes.popup.removeChild(n),clearTimeout(this.hdTimer);n=document.createElement("iframe");return n.frameBorder="0",n.src="https://www.zdic.net/hans/".concat(e),n.sandbox="allow-same-origin allow-forms",n.seamless="seamless",n.id="handian_content",n.width=this.state.isMobile?window.innerWidth:"375",n.height=this.state.isMobile?.6*window.innerHeight:"400",this.state.isMobile&&h(n,{width:"100%",height:"60vh"}),console.log("加载汉典",Date.now()),this.toggleLoading(!0),n.addEventListener("load",function(){console.log("汉典加载好了",Date.now()),t.toggleLoading(!1)}),n.addEventListener("error",function(){console.error("加载汉典报错",Date.now()),t.toggleLoading(!1)}),this.options.MAX_TIME_OUT&&(this.hdTimer=setTimeout(function(){t.state.loadingVisible&&(console.warn("汉典加载超过".concat(t.options.MAX_TIME_OUT,"ms")),t.toggleLoading(!1))},this.options.MAX_TIME_OUT)),n}},{key:"prefetchHanDian",value:function(){setTimeout(function(){var t=document.createElement("iframe");t.src="//www.zdic.net",t.width=0,t.height=0,t.frameBorder="0",t.sandbox="allow-same-origin allow-forms",t.seamless="seamless",h(t,{width:"0px",height:"0px",overflow:"hidden",opacity:0}),document.body.appendChild(t)},1e3)}},{key:"calcPosition",value:function(t,e){if(!t||!e)return{left:0,top:0};var n=t.x,o=t.y,i=Math.max(window.innerWidth,document.body.scrollWidth),t=Math.max(window.innerHeight,document.body.scrollHeight),e=e.getBoundingClientRect();return{left:Math.min(n,i-e.width),top:Math.min(o,t-e.height)}}},{key:"callPosition",value:function(t,e){var n;e&&t&&(t.style.transition="all 0.35s cubic-bezier(0, 0, 0.2, 1) 0s",this.state.isMobile?t.style.transform="translateY(0px)":(t.style.left="".concat(e.left,"px"),t.style.top="".concat(e.top,"px"),n=e.top+t.clientHeight,e=this.options.scroller?this.options.scroller.scrollTop:Math.max(document.documentElement.scrollTop,document.body.scrollTop),t=window.innerHeight+e,0<(t=Math.max(0,n-t))&&(t={top:e+t,behavior:"smooth"},this.options.scroller?this.options.scroller.scrollTo(t):(document.documentElement.scrollTo(t),document.body.scrollTo(t)))))}}])&&s(t.prototype,n),o&&s(t,o),e}();return new t({onEnd:function(t,e,n){Array.isArray(window.dataLayer)&&window.dataLayer.push({event:"dianClick",selection_text:e,platform:n.state.platform})}}),t});