|
| 1 | +/**********************/ |
| 2 | +/* SHAPE.js */ |
| 3 | +/* JDMCreator, 2016 */ |
| 4 | +/* Under MIT License */ |
| 5 | +/* V 0.1a */ |
| 6 | +/**********************/ |
| 7 | +!function(){"use strict" |
| 8 | +var e,t=void 0,n=window.document,r=function(e){return new s(e)},i=function(e){return!e&&0!==e&&""!==e&&"0"!==e},o=1e3,a={},s=function(e){if(i(e.top)||i(e.left)||i(e.width)||i(e.height))throw"" |
| 9 | +this.options=e,this.id="S"+o++,i(e.hyphenCharacter)||_.insertRule('.SHAPE-hyphen[data-shape-id="'+this.id+"\"]::before { content:'"+e.hyphenCharacter+"'; }",_.rules.length),this.destroy=function(){this.remove(),delete a[this.id],r=t |
| 10 | +for(var e in this)this.hasOwnProperty(e)&&delete this[e]},this.remove=function(e,t){e=e||n |
| 11 | +var r=null,i=n.createRange(),o=e.querySelectorAll("[data-shape-id='"+this.id+"']") |
| 12 | +i.setStartBefore(o[0]),i.setEndAfter(o[1]),r=i.commonAncestorContainer,i.detach() |
| 13 | +for(var s,h=0,l=o.length;l>h;h++)s=o[h],v(s).removeChild(s) |
| 14 | +if(this.cleanMemory(),!t){o=r.querySelectorAll("[data-shape-id]") |
| 15 | +for(var s,f={},h=0,l=o.length;l>h;h++){s=o[h] |
| 16 | +var d=s.getAttribute("data-shape-id") |
| 17 | +if(!f[d]){f[d]=!0 |
| 18 | +var u=a[d] |
| 19 | +u&&u.refresh()}}}return this},this._memory=[],this.resetMemory=function(){return this._memory=[],this},this.cleanMemory=function(){for(var e,t=[],n=0;n<this._memory.length;n++)e=this._memory[n],e.querySelectorAll("[data-shape-id='"+this.id+"']").length>0&&t.push(e) |
| 20 | +return this._memory=t,this},this.refresh=function(){return n.body.normalize(),this.applyToNodes(this._memory),this},this.applyToNodes=function(e){e=e||n.elementFromPoint(l,h) |
| 21 | +for(var t,r=0,i=Math.max(arguments.length,1);i>r;r++){t=0===r?e:arguments[r],t=t.length||0===t.length?t:[t] |
| 22 | +for(var o,a=0;a<t.length;a++)o=t[a],(1==o.nodeType||3==o.nodeType)&&s(o)}return this},this.shape=d |
| 23 | +var r=this,s=function(e){for(var t=!1,i=0;i<r._memory.length;i++)e===r._memory[i]&&(t=!0) |
| 24 | +t||r._memory.push(e) |
| 25 | +var o=y(e) |
| 26 | +o.push(r),o=o.sort(function(e,t){e.left-t.left}),A(e,e,o,function(e,t,r){var i,o=!1 |
| 27 | +if(3==e.nodeType){if(this.options.hyphenation){var a=u(this.options.hyphenation,e,t) |
| 28 | +a&&(e=a.node,t=a.offset,o=a.hyphen)}t>0&&(e=e.splitText(t),i=n.createRange(),i.setStart(e,0),i.setEnd(e,1),r=f(i.getBoundingClientRect()))}var s=m(this.id),h=this.shape.right-r.left+1 |
| 29 | +if(0===h)return e |
| 30 | +var l |
| 31 | +if(e.previousSibling){var d=e.previousSibling |
| 32 | +1==d.nodeType&&d.hasAttribute("data-shape-id")&&(o=!1)}return o?(s.className="SHAPE-hyphen",v(e).insertBefore(s,e),s.style.paddingRight=h-s.offsetWidth+1+"px",l=f(s.getBoundingClientRect())):(s.style.paddingRight=h+"px",v(e).insertBefore(s,e),l=f(s.getBoundingClientRect())),s.shape={o:this,left:l.left,right:l.right},i&&i.detach(),s})},h=e.top-(e.margin||0),l=e.left-(e.margin||0),d=(e.width+(e.margin||0),e.height+(e.margin||0),{top:h,left:l,right:l+e.width+2*(e.margin||0),bottom:h+e.height+2*(e.margin||0)}) |
| 33 | +this.shape=d,a[this.id]=this |
| 34 | +this.id},h=function(e,n,r){var i=/\s+/gi |
| 35 | +n||0===n||(n=e.data.length-1) |
| 36 | +for(var o=[e],a=[],s=n;s>=0;s--)if(a=[r.startContainer,r.startOffset],r.setStart(e,s),i.test(""+r))return r.setStart(a[0],a[1]),{nodes:o,word:!0} |
| 37 | +var l=g(e) |
| 38 | +if(l){var f=h(l,t,r) |
| 39 | +if(o=o.concat(f.nodes),f.word)return{nodes:o,word:!0}}return{nodes:o,word:!1}},l=function(e,t,n){for(var r=/\s+/gi,t=t||0,i=[e],o=[],a=t;a<e.data.length;a++)if(o=[n.endContainer,n.endOffset],n.setEnd(e,a),r.test(""+n))return n.setEnd(o[0],o[1]),{nodes:i,word:!0} |
| 40 | +var s=p(e) |
| 41 | +if(s){var h=l(s,0,n) |
| 42 | +if(i=i.concat(h.nodes),h.word)return{nodes:i,word:!0}}return{nodes:i,word:!1}},f=function(t){if(!n.body)return t |
| 43 | +e||(e=n.createElement("div"),e.style.position="absolute",e.style.left=e.style.top="0",n.body.appendChild(e)) |
| 44 | +var r=e.getBoundingClientRect() |
| 45 | +return{top:t.top-r.top,left:t.left-r.left,right:t.right-r.right,bottom:t.bottom-r.bottom,width:t.width,height:t.height}},d=/^[A-Za-z\u00C0-\u017F]{1}$/,u=function(e,t,n){var r=c(t,n) |
| 46 | +if(!r)return{hyphen:!1,node:t,offset:n} |
| 47 | +var i=r.maxoffset,o=r.originalOffset,a=r.leftNodes.nodes,s=e.call(this,r.word,i) |
| 48 | +s=isNaN(s)?i:Math.min(s,i) |
| 49 | +for(var h=0==s?!1:d.test(r.word.charAt(s-1)),l=o-s,f=0;f<a.length;f++){var u=a[f],g=0===f?n:u.data.length |
| 50 | +if(!(l>g))return{node:u,offset:Math.max(g-l,0),hyphen:h} |
| 51 | +l-=g}},c=function(e,t){var r=n.createRange() |
| 52 | +if(r.setStart(e,t),r.setEnd(e,t+1),/^\s+$/.test(""+r))return null |
| 53 | +var i=h(e,t,r),o=(""+r).length-1,a=o |
| 54 | +l(e,t,r) |
| 55 | +var s=""+r |
| 56 | +return d.test(s.charAt(o-1))&&o--,r.detach(),{word:s,leftNodes:i,maxoffset:o,originalOffset:a}},g=function(e){for(;e;){for(var t,n=!1;t=n||e.previousSibling;){if(e=t,n=!1,3==e.nodeType)return e |
| 57 | +1==e.nodeType&&e.lastChild&&(n=e.lastChild)}e=v(e)}return null},p=function(e){for(;e;){for(var t,n=!1;t=n||e.nextSibling;){if(e=t,n=!1,3==e.nodeType)return e |
| 58 | +1==e.nodeType&&e.firstChild&&(n=e.firstChild)}e=v(e)}return null},v=function(e){var t=e.parentNode |
| 59 | +return t&&1===t.nodeType?t:null},y=function(e){for(var t,n=e.querySelectorAll("span[data-shape-id]"),r=[],i={},o=0,s=n.length;s>o;o++){t=n[o] |
| 60 | +var h=t.getAttribute("data-shape-id") |
| 61 | +i[h]||(i[h]=!0,r.push(a[h]))}return r},m=function(e){var t=n.createElement("span") |
| 62 | +return t.setAttribute("data-shape-id",e),t},C=function(e,t){for(var n=0;n<t.length;n++)if(b(e,t[n].shape))return t[n] |
| 63 | +return!1},b=function(e,t){return e.left<=t.right&&t.left<=e.right&&e.top<=t.bottom&&t.top<=e.bottom},A=function(e,t,n,r){var e=N(e,n) |
| 64 | +return e?w(e,t,n,r):null},w=function(e,t,r,i){var o |
| 65 | +if(3==e.nodeType){for(var a=-1,s=!1,h=n.createRange();a++<e.data.length-1;){h.setStart(e,a),h.setEnd(e,a+1) |
| 66 | +var l=f(h.getBoundingClientRect()),d=C(l,r) |
| 67 | +if(d){s=!0,o=i.call(d,e,a,l) |
| 68 | +break}}h.detach(),s||(o=e)}else if(1==e.nodeType){var l=e.getBoudingClientRect(),d=C(l,r) |
| 69 | +o=i.call(d,e,null,l)}return o?(o=T(o,t,r),o?w(o,t,r,i):null):null},R=function(e){var t=e.textContent||e.innerText |
| 70 | +return 0===e.children||!t||/^\s*$/.test(t)},S=function(e){return!e.style.position||"static"==e.style.position},E=function(e){var t=e.shape,n=t.left,r=(t.right,f(e.getBoundingClientRect())) |
| 71 | +if(t=a[e.getAttribute("data-shape-id")],t&&(t=t.shape),r.left!=n){var i=v(e) |
| 72 | +if(i)return i.removeChild(e)}if(t&&!b(r,t)){var i=v(e) |
| 73 | +if(i)return i.removeChild(e)}},T=function(e,t,r){for(var i=e,o=null;e!==t&&(e=e.nextSibling||o);)if(o=null,1==e.nodeType)if(R(e)){if(e.hasAttribute("data-shape-id"))o=e.nextSibling,E(e),o=v(e)?null:o |
| 74 | +else if(C(f(e.getBoundingClientRect()),r)&&!S(e))return e}else if(C(f(e.getBoundingClientRect()),r)){var a=N(e,r) |
| 75 | +if(a)return a}else for(var s=e.querySelectorAll("span[data-shape-id]"),h=0,l=s.length;l>h;h++)E(s[h]) |
| 76 | +else if(3==e.nodeType){var d=n.createRange() |
| 77 | +if(d.selectNodeContents(e),C(f(d.getBoundingClientRect()),r))return d.detach(),e |
| 78 | +d.detach()}return i!==t&&v(i)?T(v(i),t,r):null},N=function(e,t){for(var r,i=0,o=e.childNodes,a=o.length;a>i&&(r=o[i],r);i++)if(1==r.nodeType)if(R(r)){if(r.hasAttribute("data-shape-id"))E(r),r||i-- |
| 79 | +else if(C(f(r.getBoundingClientRect()),t)&&!S(r))return r}else{var s=N(r,t) |
| 80 | +if(s)return s}else if(3==r.nodeType){var h=n.createRange() |
| 81 | +if(h.selectNodeContents(r),C(f(h.getBoundingClientRect()),t))return h.detach(),r |
| 82 | +h.detach()}return null},B={ANYWHERE:function(e,t){return t},HYPHER:function(e){return function(t,n){for(var r,i=e.hyphenate(t),o="",a=0;a<i.length;a++){if(r=i[a],!(r.length+o.length<=n))return o.length |
| 83 | +o+=r}return o.length}},NH_ANYWHERE:function(){},NH_WORD:function(e,t){return 0}} |
| 84 | +r.forEach=function(e){for(var n in a)if(a.hasOwnProperty(n)){var r=e.call(a[n]) |
| 85 | +if(!r&&r!==t)break}},r.removeAll=function(e){e=e||n.body |
| 86 | +for(var t,r=n.body.querySelectorAll("[data-shape-id]"),i=0;i<r.length;i++){t=r[i] |
| 87 | +var o=v(t) |
| 88 | +o&&o.removeChild(t)}e=1===e.nodeType?e:n.body,e.normalize()},r.refreshAll=function(e){e&&r.removeAll(),r.forEach(function(){this.refresh()})},r.HYPHENATION=B |
| 89 | +var H=!0 |
| 90 | +window.addEventListener("resize",function(){H&&r.refreshOnResize&&(H=!1,window.requestAnimationFrame(function(){r.refreshAll(!0),H=!0}))},!1),r.changeDefaultHyphenCharacter=function(e){_.rules[0].style.content='"'+e+'"'},r.version="0.1a",r.build="100",r.refreshOnResize=!0 |
| 91 | +var x=n.createElement("style") |
| 92 | +n.head.appendChild(x) |
| 93 | +var _=x.sheet |
| 94 | +_.insertRule(".SHAPE-hyphen::before { content:'-'; }",0),window.SHAPE=r}() |
0 commit comments