UNPKG

text-highlighter-plugin

Version:

一个简单的文本高亮插件,支持自定义样式和多次高亮

1 lines 1.13 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("TextHighlighter",[],t):"object"==typeof exports?exports.TextHighlighter=t():e.TextHighlighter=t()}(this,(function(){return function(){"use strict";var e={};return e.default=class{constructor(e={}){this.options={color:e.color||"#ffeb3b",backgroundColor:e.backgroundColor||"transparent",className:e.className||"text-highlighter"},this.createStyles()}createStyles(){const e=document.createElement("style");e.textContent=`\n .${this.options.className} {\n color: ${this.options.color};\n background-color: ${this.options.backgroundColor};\n transition: all 0.3s ease;\n }\n `,document.head.appendChild(e)}highlight(e,t){if(!e||!t)return;const o=e.textContent,n=new RegExp(t,"gi");e.innerHTML=o.replace(n,(e=>`<span class="${this.options.className}">${e}</span>`))}removeHighlight(e){if(!e)return;const t=e.getElementsByClassName(this.options.className);for(;t.length>0;){t[0].parentNode.replaceChild(document.createTextNode(t[0].textContent),t[0])}}},e=e.default}()}));