@ge-ge/highlight
Version:
2 lines (1 loc) • 7.65 kB
JavaScript
import e from"@babel/runtime-corejs3/core-js-stable/instance/slice";import t from"@babel/runtime-corejs3/core-js-stable/array/from";import n from"@babel/runtime-corejs3/core-js-stable/symbol";import r from"@babel/runtime-corejs3/core-js/get-iterator-method";import a from"@babel/runtime-corejs3/helpers/toConsumableArray";import o from"@babel/runtime-corejs3/helpers/classCallCheck";import i from"@babel/runtime-corejs3/helpers/createClass";import s from"@babel/runtime-corejs3/helpers/defineProperty";import l from"@babel/runtime-corejs3/core-js-stable/object/keys";import u from"@babel/runtime-corejs3/core-js-stable/instance/concat";function c(a,o){var i=void 0!==n&&r(a)||a["@@iterator"];if(!i){if(Array.isArray(a)||(i=function(n,r){var a;if(!n)return;if("string"==typeof n)return f(n,r);var o=e(a=Object.prototype.toString.call(n)).call(a,8,-1);"Object"===o&&n.constructor&&(o=n.constructor.name);if("Map"===o||"Set"===o)return t(n);if("Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o))return f(n,r)}(a))||o&&a&&"number"==typeof a.length){i&&(a=i);var s=0,l=function(){};return{s:l,n:function(){return s>=a.length?{done:!0}:{done:!1,value:a[s++]}},e:function(e){throw e},f:l}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var u,c=!0,d=!1;return{s:function(){i=i.call(a)},n:function(){var e=i.next();return c=e.done,e},e:function(e){d=!0,u=e},f:function(){try{c||null==i.return||i.return()}finally{if(d)throw u}}}}function f(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function d(e){var t=arguments.length>1&&void 0!==arguments[1]&&arguments[1];function n(e){var r=[],o=function(e,t){t instanceof Array?e.push.apply(e,a(t)):e.push(t)};if(e.nodeType===Node.TEXT_NODE)return o(r,e),r;if(e.nodeType===Node.ELEMENT_NODE&&"childElementCount"in e&&0===e.childElementCount&&!t)return o(r,e),r;for(var i=e.childNodes.length,s=0;s<i;s++){var l=e.childNodes[s];if(l.nodeType!==Node.TEXT_NODE)o(r,n(l));else o(r,l)}return r}return n(e)}function h(e,t,n){var r=[];if(e.nextSibling){r.push(e.nextSibling);var o=h(e.nextSibling,t);r.push.apply(r,a(o)),n&&n(o)}else if(t&&e.parentNode&&e.parentNode!==t&&e.parentNode.parentNode!==t){var i=h(e.parentNode,t);r.push.apply(r,a(i)),n&&n(i)}return r}function p(e,t,n){var r=[];if(e.previousSibling){r.unshift(e.previousSibling);var o=p(e.previousSibling,t);r.unshift.apply(r,a(o)),n&&n(o)}else if(t&&e.parentNode&&e.parentNode!==t&&e.parentNode.parentNode!==t){var i=p(e.parentNode,t);r.unshift.apply(r,a(i)),n&&n(i)}return r}function m(e,t){var n=[];return function e(r){r!==t&&r.parentNode&&r.parentNode!==t&&(n.push(r.parentNode),e(r.parentNode))}(e),n}function v(e){var t,n=e.startContainer,r=e.endContainer,a=e.commonAncestorContainer;if(n===r)return[];if(n&&r&&n.parentNode===r.parentNode){var o=document.createRange();return o.setStartAfter(n),o.setEndBefore(r),[o]}function i(e){var t=document.createRange();return t.setStartBefore(e[0]),t.setEndAfter(e[e.length-1]),t}var s=[];h(n,a,(function(e){e&&e.length&&s.push(i(e))}));var l=[];p(r,a,(function(e){e&&e.length&&l.push(i(e))}));var c=[],f=!1;if(a.childNodes.length>2)for(var d=m(n,a),v=m(r,a),g=0;g<a.childNodes.length-1;g++){var N=a.childNodes[g];if(N!==d[d.length-1]){if(f){if(N===v[v.length-1])break;c.push(N)}}else f=!0}if(c.length){var y,b=i(c);return u(y=[]).call(y,s,[b],l)}return u(t=[]).call(t,s,l)}function g(e,t){var n=function(e){var t={},n=document.createRange(),r=document.createRange();return e.startContainer===e.endContainer?(n.setStart(e.startContainer,e.startOffset),n.setEnd(e.endContainer,e.endOffset)):(e.startContainer&&(n.setStart(e.startContainer,e.startOffset),e.startContainer.nodeType===Node.TEXT_NODE?n.setEndAfter(e.startContainer):n.setEnd(e.startContainer,e.startContainer.childNodes.length)),r.setStart(e.endContainer,0),r.setEnd(e.endContainer,e.endOffset)),t.start={id:"start",range:n},t.end={id:"end",range:r},t}(e),r=n.start,a=n.end,o=v(e);r&&r.range&&o.unshift(r.range),a&&a.range&&o.push(a.range);var i,s=c(o);try{for(s.s();!(i=s.n()).done;){var l=i.value;if(l){var u=l.extractContents();t&&t(u),l.deleteContents(),l.insertNode(u)}}}catch(e){s.e(e)}finally{s.f()}}function N(a,o){var i=void 0!==n&&r(a)||a["@@iterator"];if(!i){if(Array.isArray(a)||(i=function(n,r){var a;if(!n)return;if("string"==typeof n)return y(n,r);var o=e(a=Object.prototype.toString.call(n)).call(a,8,-1);"Object"===o&&n.constructor&&(o=n.constructor.name);if("Map"===o||"Set"===o)return t(n);if("Arguments"===o||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(o))return y(n,r)}(a))||o&&a&&"number"==typeof a.length){i&&(a=i);var s=0,l=function(){};return{s:l,n:function(){return s>=a.length?{done:!0}:{done:!1,value:a[s++]}},e:function(e){throw e},f:l}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var u,c=!0,f=!1;return{s:function(){i=i.call(a)},n:function(){var e=i.next();return c=e.done,e},e:function(e){f=!0,u=e},f:function(){try{c||null==i.return||i.return()}finally{if(f)throw u}}}}function y(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function b(e,t){l(t).forEach((function(n){e.style[n]=t[n]}))}var E=function(){function e(t){o(this,e),s(this,"style",{}),s(this,"className",""),s(this,"tagName","mark"),s(this,"tagNameUp","MARK"),t&&(this.className=t.className||this.className,this.style=t.style||this.style,this.tagName=t.tagName||this.tagName,this.tagNameUp=this.tagName.toUpperCase())}return i(e,[{key:"reset",value:function(e){e.nodeName===this.tagNameUp&&e.parentNode&&(e.outerHTML=e.textContent||"")}},{key:"highLight",value:function(e){var t,n=this,r=[],a=d(e,!1),o=function(e){var t=n.highLightText(e);if(t){var a={mix:!1,el:t};r.push(a)}},i=N(a);try{for(i.s();!(t=i.n()).done;){var s=t.value;if(s.nodeType===Node.TEXT_NODE&&s.textContent){if(s.parentNode&&s.parentNode.nodeName===this.tagNameUp)continue;o(s)}else if(s.nodeName===this.tagNameUp){var l={mix:!0,el:s};r.push(l)}else d(s,!0).forEach((function(e){o(e)}))}}catch(e){i.e(e)}finally{i.f()}return r}},{key:"addStyle",value:function(e){console.log("addStyle");var t=[];if(e.nodeType===Node.DOCUMENT_NODE||e.nodeType===Node.DOCUMENT_FRAGMENT_NODE)for(var n=0;n<e.childNodes.length;n++){var r=e.childNodes[n];t.push.apply(t,a(this.addStyle(r)))}else if(e.nodeType===Node.ELEMENT_NODE)b(e,this.style),t.push({mix:!1,el:e});else if(e.nodeType===Node.TEXT_NODE){var o=this.highLightText(e);o&&(b(o,this.style),t.push({mix:!1,el:e}))}return t}},{key:"mark",value:function(e){var t=this,n=[];return g(e,(function(e){var r=t.highLight(e);n.push.apply(n,a(r))})),n}},{key:"markStyle",value:function(e){var t=this,n=[];return g(e,(function(e){var r=t.addStyle(e);n.push.apply(n,a(r))})),n}},{key:"createRange",value:function(e,t){var n=document.createRange();return n.setStartBefore(e),n.setEndAfter(t),n}},{key:"highLightText",value:function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,n=arguments.length>2?arguments[2]:void 0;if(e.nodeType===Node.TEXT_NODE&&e.textContent){if(0===e.textContent.length)return;n=n||e.textContent.length-t;var r=e.textContent.substr(t,n),a=this.getMarkElement(r),o=document.createRange();return o.setStart(e,t),o.setEnd(e,t+n),o.deleteContents(),a&&o.insertNode(a),o.detach(),a}}},{key:"getMarkElement",value:function(e){var t=e,n=document.createElement(this.tagName);return this.className&&n.classList.add(this.className),n.textContent=t,n}}]),e}();export{E as default};