1 line
7.5 KiB
JavaScript
1 line
7.5 KiB
JavaScript
(()=>{"use strict";const e="__theme__",t=()=>{const t=localStorage.getItem(e);return null!=t?t:"light"};new class{constructor(){this.listeners={}}on(e,t){this.listeners[e]||(this.listeners[e]=[]),this.listeners[e].push(t)}off(e,t){const n=this.listeners[e];n&&(this.listeners[e]=n.filter((e=>e!==t)))}emit(e,...t){const n=this.listeners[e];n&&n.length&&n.forEach((e=>{e(...t)}))}};const n=e=>Promise.all(e.map((e=>new Promise(((t,n)=>{if("css"===e.type){const o=document.createElement("link");o.href=e.url,o.rel="stylesheet",o.onload=()=>t(),o.onerror=()=>n(new Error(`Failed to load CSS from ${e.url}`)),document.head.appendChild(o)}else if("js"===e.type){const o=document.createElement("script");o.src=e.url,e.id&&(o.id=e.id),o.onload=()=>t(),o.onerror=()=>n(new Error(`Failed to load JS from ${e.url}`)),document.body.appendChild(o)}else n(new Error("Invalid resource type specified."))}))))),o=e=>{const{enable:t,jsCdn:o,cssCdn:r}=e;t&&n([{type:"css",url:r},{type:"js",url:o}]).then((e=>{e&&(e=>{const t=/\$\$(.+?)\$\$/g,n=/\\\[(.+?)\\\]/g;function o(e,t,n){let o;const r=[];for(;null!==(o=t.exec(e.nodeValue));){const t=document.createRange();t.setStart(e,o.index),t.setEnd(e,o.index+o[0].length),r.push(t)}for(const e of r){const t=e.toString().replace(/^\$\$|\$\$$|\\\[|\\\]$/g,""),o=document.createElement("span");o.innerHTML=window.katex.renderToString(t,{throwOnError:!1,displayMode:n}),e.deleteContents(),e.insertNode(o)}}document.querySelectorAll(e).forEach((e=>{const r=document.createTreeWalker(e,NodeFilter.SHOW_TEXT,null);let s;for(;s=r.nextNode();)o(s,t,!1),o(s,n,!0)}))})(".article-container")}))};var r=function(e,t){var n={};for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&t.indexOf(o)<0&&(n[o]=e[o]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(o=Object.getOwnPropertySymbols(e);r<o.length;r++)t.indexOf(o[r])<0&&Object.prototype.propertyIsEnumerable.call(e,o[r])&&(n[o[r]]=e[o[r]])}return n};const s=e=>{const{enable:t,cdn:o}=e,s=r(e,["enable","cdn"]);t&&n([{type:"js",url:o}]).then((e=>{e&&(((e="vcomments",t=".article-container")=>{const n=document.querySelector(t);if(!n)return;const o=document.createElement("div");o.id=e,n.appendChild(o)})(),new window.Valine(Object.assign({el:"#vcomments"},s)))}))},c=[{icon:'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><path d="M17 15l1.55 1.55c-.96 1.69-3.33 3.04-5.55 3.37V11h3V9h-3V7.82C14.16 7.4 15 6.3 15 5c0-1.65-1.35-3-3-3S9 3.35 9 5c0 1.3.84 2.4 2 2.82V9H8v2h3v8.92c-2.22-.33-4.59-1.68-5.55-3.37L7 15l-4-3v3c0 3.88 4.92 7 9 7s9-3.12 9-7v-3l-4 3zM12 4c.55 0 1 .45 1 1s-.45 1-1 1s-1-.45-1-1s.45-1 1-1z" fill="currentColor"></path></svg>',value:"锚点"},{icon:'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><path d="M20 2H4c-1.1 0-2 .9-2 2v18l4-4h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm0 14H6l-2 2V4h16v12zM7 9h2v2H7zm4 0h2v2h-2zm4 0h2v2h-2z" fill="currentColor"></path></svg>',value:"写点想法"}],l=document.querySelector(".article-container article"),i=(e,t)=>{const{startContainer:n,startOffset:o}=t;e.classList.add("select-wrap");const r=document.createElement("div");r.classList.add("text-tab");const s=document.createElement("ul");c.forEach((t=>{const n=document.createElement("li");n.innerHTML=`${t.icon}`,n.classList.add("tip","right"),n.setAttribute("data-tip",t.value),n.addEventListener("click",(()=>((e,t)=>{if("锚点"===t.value){e.querySelectorAll(".flag").forEach((e=>e.remove()));const n=document.createElement("span");n.classList.add("flag"),n.innerHTML=t.icon,e.appendChild(n),e.classList.add("flag-paragraph")}})(e,t))),s.appendChild(n)})),r.append(s),e.appendChild(r),document.removeEventListener("selectionchange",d)},a=()=>[...document.querySelectorAll(".text-tab")].forEach((e=>e.remove())),d=()=>{const e=()=>{const e=window.getSelection();if(e&&e.rangeCount>0&&!e.isCollapsed){const t=e.getRangeAt(0),{startContainer:n,endContainer:o}=t;if(l.contains(n)&&l.contains(o)){const e=(e=>{let t=e;for(;t&&t.parentElement&&"ARTICLE"!==t.parentElement.tagName;)t=t.parentElement;return t})(n);e&&l.contains(e)&&(a(),i(e,t))}}else setTimeout((()=>a()),0)},t=window.getSelection();t&&t.rangeCount>0&&!t.isCollapsed?document.addEventListener("mouseup",e):document.removeEventListener("mouseup",e)},m='<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 5H7a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-2"></path><rect x="9" y="3" width="6" height="4" rx="2"></rect></g></svg>',u=e=>{document.querySelectorAll(".toc a").forEach((e=>e.classList.remove("active")));const t=document.querySelector('.toc a[href="'+e+'"]');t&&t.classList.add("active")};document.addEventListener("click",(function(e){const t=e.target.closest(".toc-link");if(t){const e=t.getAttribute("href");e&&u(e)}})),document.addEventListener("DOMContentLoaded",(function(){(e=>{const t=document.querySelector(e);if(t){const e=localStorage.getItem("scrolledHeight");e&&(t.scrollTop=Number(e))}})("main.scrollbar-obtrusive"),window.location.hash&&u(window.location.hash);const{mermaid:e,katex:r,valine:c}=window;o(Object.assign(Object.assign({},r),{enable:window.page.use.indexOf("katex")>-1})),(e=>{var t;const{enable:o,cdn:r,theme:s}=e;if(o){const e=document.getElementById("src-mermaid");e&&(null===(t=e.parentNode)||void 0===t||t.removeChild(e)),n([{type:"js",url:r,id:"src-mermaid"}]).then((e=>{e&&window.mermaid.initialize({startOnLoad:!0,theme:s||"neutral"})}))}})(Object.assign(Object.assign({},e),{enable:window.page.use.indexOf("mermaid")>-1})),s(Object.assign(Object.assign({},c),{enable:window.page.use.indexOf("valine")>-1})),document.querySelectorAll(".article-container article pre > code").forEach((e=>{const t=e.parentNode;if(!t)return;const n=document.createElement("div");n.classList.add("code-wrapper"),t.parentNode.insertBefore(n,t),n.appendChild(t);const o=document.createElement("div");o.classList.add("btn-wrapper");const r=t.getAttribute("data-language"),s=document.createElement("span");s.classList.add("lang"),s.textContent=r,o.appendChild(s);const c=document.createElement("span");c.className="copy-button",c.innerHTML=m,c.addEventListener("click",(function(){const t=e.textContent;navigator.clipboard.writeText(t).then((()=>{c.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 5H7a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7a2 2 0 0 0-2-2h-2"></path><rect x="9" y="3" width="6" height="4" rx="2"></rect><path d="M9 14l2 2l4-4"></path></g></svg>',setTimeout((()=>{c.innerHTML=m}),2e3)}))})),o.appendChild(c),n.appendChild(o)})),document.addEventListener("selectionchange",d),p();const l=document.getElementById("toggle-theme");null==l||l.addEventListener("click",(()=>{const e=document.querySelector("#prism-theme");e?e.setAttribute("href",`/lib/prism/one-${t()}.css`):p()}))})),document.addEventListener("DOMContentLoaded",(function(){const e=document.querySelector("main.scrollbar-obtrusive");e&&e.addEventListener("scroll",(()=>(e=>{const t=document.querySelector(e);if(t){const e=t.scrollTop;localStorage.setItem("scrolledHeight",String(e))}})("main.scrollbar-obtrusive")))}));const p=()=>{const e=document.createElement("link");e.setAttribute("id","prism-theme"),e.rel="stylesheet",e.type="text/css",e.href=`/lib/prism/one-${t()}.css`,document.head.append(e)}})(); |