@xiee/utils
Advanced tools
Comparing version 1.13.68 to 1.13.69
@@ -8,3 +8,3 @@ // move elements into page boxes such that each box contains as many elements as | ||
const tpl = d.createElement('div'), book = $$('h1').length > 1, | ||
const tpl = d.createElement('div'), book = $$('h1').length > 1, boxes = [], | ||
fr_cls = 'pagesjs-fragmented', fr_1 = 'fragment-first', fr_2 = 'fragment-last', | ||
@@ -19,3 +19,3 @@ tb = ['top', 'bottom'].map(i => { | ||
<div class="pagesjs-footer"></div>`; | ||
let box, box_body, box_cls = [], boxes = [], box_n = 0, H, h_code, l_code = []; | ||
let box, box_body, box_cls = [], box_n = 0, H, h_code, l_code = []; | ||
function newPage(el) { | ||
@@ -43,5 +43,5 @@ const n = boxes.length; | ||
} | ||
function removeBlank(el) { | ||
function removeBlank(el, html = false) { | ||
if (!el) return false; | ||
const v = el.innerText.trim() === ''; | ||
const v = (html ? el.innerHTML : el.innerText).trim() === ''; | ||
v && el.remove(); | ||
@@ -63,7 +63,10 @@ return v; | ||
if (box.scrollHeight <= H) return; | ||
const cls = el.classList; | ||
const cls = el.classList, h0 = el.innerHTML, n0 = boxes.length; | ||
fragment(el); | ||
// remove empty pages | ||
if (el.tagName === 'PRE') for (let i = boxes.length - 1; i >= n0; i--) { | ||
removeBlank(boxes[i]) && boxes.splice(i, 1); | ||
} | ||
// if el can be fragmented, add class *-last, otherwise remove class (e.g., | ||
// current box can't even hold el's first child so move whole el to new box) | ||
const h0 = el.innerHTML; | ||
fragment(el); | ||
(cls.contains(fr_cls) && el.innerHTML !== h0) ? cls.add(fr_2) : | ||
@@ -74,6 +77,8 @@ [el].concat(...$$(`.${fr_cls}`, el)).forEach(el => el.classList.remove(fr_cls, fr_1)); | ||
} | ||
function fillCode(el, i1, i2) { | ||
el.innerHTML = l_code.slice(i1, i2).join('\n'); | ||
} | ||
// break elements that are relatively easy to break (such as <ul>) | ||
function fragment(el, container, parent, page) { | ||
const tag = el.tagName, is_code = tag === 'CODE', cls = el.classList, | ||
frag = cls.contains(fr_cls); | ||
const tag = el.tagName, cls = el.classList, frag = cls.contains(fr_cls); | ||
parent || frag && cls.remove(fr_1); | ||
@@ -100,5 +105,17 @@ // if <code>, keep fragmenting; otherwise exit when box fits | ||
code.innerHTML = ''; // temporarily empty <code>; will use l_code | ||
fragment(code, el2, el, box_cur); | ||
} | ||
if (fragment(code, el2, el, box_cur)) { | ||
// exit if the rest of lines are empty | ||
if (l_code.join('').trim() === '') { | ||
el2.before(el); el.innerHTML = el2.innerHTML; el2.remove(); | ||
boxes.pop().remove(); newPage(boxes[boxes.length - 1]); l_code = []; | ||
} | ||
if (!l_code.length) return; fragment(el); | ||
} else { | ||
// the current page can't hold the block; fragment it on next page | ||
el2.remove(); [code, el].forEach(el => el.classList.remove(fr_cls, fr_1)); | ||
fragment(el); | ||
} | ||
} else fillCode(code, 0); | ||
} | ||
return removeBlank(el2); | ||
} else if (tag === 'DIV' && nChild(el) === 1) { | ||
@@ -109,3 +126,3 @@ fragment(el.firstElementChild, el2, el, box_cur); | ||
// keep moving el's first item to el2 until page height > H | ||
if (['UL', 'OL', 'BLOCKQUOTE'].includes(tag) && nChild(el) > 1) while (true) { | ||
if (['UL', 'OL', 'BLOCKQUOTE', 'FIELDSET'].includes(tag) && nChild(el) > 1) while (true) { | ||
const item = el.firstChild; | ||
@@ -125,9 +142,6 @@ if (!item) break; | ||
// split lines in <code> and try to move as many lines into el2 as possible | ||
if (is_code) { | ||
if (container && tag === 'CODE') { | ||
// figure out how many lines can fit the box via bisection | ||
let i = 0, i1 = 1, i2 = n = l_code.length; | ||
const sols = []; // solutions tried | ||
function fillCode(el, i1, i2) { | ||
el.innerHTML = l_code.slice(i1, i2).join('\n'); | ||
} | ||
while (i2 - i1 > 1) { | ||
@@ -154,15 +168,11 @@ fillCode(el2, 0, i || 1); | ||
fillCode(el2, 0, i1); l_code.splice(0, i1); | ||
// exit if the rest of lines are empty | ||
if (l_code.join('').trim() === '') { | ||
container.before(parent); el.remove(); parent.append(el2); | ||
boxes.pop().remove(); newPage(boxes[boxes.length - 1]); | ||
return (l_code = []); | ||
} | ||
} else { | ||
el2.innerHTML = ''; fillCode(el, 0); l_code = []; parent.classList.remove(fr_cls, fr_1); | ||
el2.innerHTML = ''; fillCode(el, 0); l_code = []; // not fragmented | ||
} | ||
return i > 0; | ||
} | ||
const el2_empty = removeBlank(el2); | ||
const el2_empty = removeBlank(el2, true); | ||
el2_empty && cls.remove(fr_cls, fr_1); | ||
// if the clone is empty, remove it, otherwise keep fragmenting the remaining el | ||
if (!el2_empty || is_code || prev) fragment(container ? parent : el); | ||
if (!el2_empty || prev) fragment(container ? parent : el); | ||
} | ||
@@ -246,3 +256,2 @@ | ||
let page_title, i = 0; | ||
boxes = boxes.filter(box => !removeBlank(box)); // remove empty pages | ||
boxes.forEach(box => { | ||
@@ -249,0 +258,0 @@ if (book) { |
@@ -1,2 +0,2 @@ | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function s(t,n=e){return n?n.querySelectorAll(t):[]}function r(e){return e.childElementCount}const a=e.createElement("div"),i=s("h1").length>1,o="pagesjs-fragmented",c="fragment-first",l=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));a.className="pagesjs-page",a.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';let f,d,p,h,g=[],u=[],m=0,E=[];function b(e){const n=u.length;return n-m>1&&(u.slice(m,n-1).forEach(L),m=n-1),e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",a.innerHTML),f=e||a.cloneNode(!0),d=f.children[1],g.length&&f.classList.add(...g),u.includes(f)||u.push(f),f}function L(e){const t=e.scrollHeight;if(t>p&&!e.dataset.pagesOffset){const n=function(e,t){let n=+e.dataset.pagesOffset;if(n)return n;if(n=Math.ceil(t/p),n<=1)return n;const r=l.concat([...s("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!r)return n;function a(){return Math.ceil((t+(n-1)*r)/p)}let i=a();for(;i>n;)n=i,i=a();return n}(e,t);n>1&&(e.dataset.pagesOffset=n)}e.remove()}function v(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function T(e){if(e.classList.contains("pagesjs-page"))f.after(b(e)),r(e)>3&&(d.append(...[...e.children].slice(3)),f.after(b()));else{if(d.append(e),f.scrollHeight<=p)return;const t=e.classList,n=e.innerHTML;j(e),t.contains(o)&&e.innerHTML!==n?t.add("fragment-last"):[e].concat(...s(`.${o}`,e)).forEach((e=>e.classList.remove(o,c))),E=[]}}function j(e,t,s,a){const i=e.tagName,l="CODE"===i,g=e.classList,m=g.contains(o);if(s||m&&g.remove(c),!E.length&&f.scrollHeight<=p)return;s?g.add(o):m||g.add(o,c);const L=a||f,T=e.cloneNode();if(t?t.append(T):(d.append(T),L.after(b()),d.append(e)),"PRE"===i){const y=e.firstElementChild;if("CODE"==y?.tagName){const O=E.length;O||(E=y.innerHTML.split("\n"));let M=E.length;M>1&&""===E[M-1]&&(M-=1),M>1&&(O||(h=y.offsetHeight/M),y.innerHTML="",j(y,T,e,L))}}else"DIV"===i&&1===r(e)&&j(e.firstElementChild,T,e,L);const H=T.previousElementSibling;if(["UL","OL","BLOCKQUOTE"].includes(i)&&r(e)>1)for(;;){const N=e.firstChild;if(!N)break;if(T.append(N),!N.nodeName.startsWith("#")&&L.scrollHeight>p){(H||r(T)>1)&&e.insertBefore(N,e.firstChild),"OL"===i&&(e.start+=r(T));break}}if(l){let S=0,x=1,k=n=E.length;const A=[];function C(e,t,n){e.innerHTML=E.slice(t,n).join("\n")}for(;k-x>1;){C(T,0,S||1);const D=p-L.offsetHeight;if(0===D){x=S||1;break}if(D<0){if(S<=0)break;k=S}else x=S;A.push(S);const I=S+Math.round(D/h);S=A.includes(I)?S+(D>0?1:-1):I,D>0&&S>=n&&(k=n+1,S=n)}if(S>0){if(C(T,0,x),E.splice(0,x),""===E.join("").trim())return t.before(s),e.remove(),s.append(T),u.pop().remove(),b(u[u.length-1]),E=[]}else T.innerHTML="",C(e,0),E=[],s.classList.remove(o,c)}(!v(T)||l||H)&&j(t?s:e)}function H(e){return e&&(e.dataset.shortTitle||e.innerText)}const C=H(t("h1.title, .frontmatter h1, .title, h1")),y=(i?"h1":"h2")+":not(.frontmatter *)";function O(){if("complete"!==e.readyState)return setTimeout(O,10);const n=e.body.classList;if(n.contains("pagesjs"))return;n.add("pagesjs"),e.body.insertAdjacentElement("afterbegin",b()),p=f.clientHeight||window.innerHeight,s(":is(#TOC, .footnotes, .chapter-before, .chapter-after):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),n.add("pagesjs-filling"),s('#TOC a[href^="#"]').forEach((t=>{const n=e.createElement("span"),s=t.firstElementChild;for(s?.classList.contains("section-number")?s.after(n):t.insertAdjacentElement("afterbegin",n);n.nextSibling;)n.append(n.nextSibling);t.insertAdjacentHTML("beforeend",'<span class="dot-leader"></span>'),t.dataset.pageNumber="000"}));const r=[];s(".body").forEach((e=>{const t=[...e.children];r.push(t),t.forEach((e=>e.remove()))})),s(".frontmatter, .abstract, #TOC:not(.chapter-toc)").forEach((e=>{T(e),i&&f.after(b())})),s(".body").forEach(((e,t)=>{g=["chapter","appendix"].filter((t=>e.classList.contains(t))),i&&(""===f.innerText?b(f):f.after(b())),r[t].forEach(T),v(e.parentNode),v(e)})),u.slice(m).forEach(L),n.remove("pagesjs-filling");let a,o=0;u=u.filter((e=>!v(e))),u.forEach((e=>{if(i){if(t(".frontmatter",e))return;t(y,e)&&(a="")}const n=+e.dataset.pagesOffset||1;n>1&&e.classList.add("page-multiple"),o+=n,e.classList.add("page-"+(o%2==0?"even":"odd"));const r={pageNumber:o,mainTitle:C,pageTitle:a};let c;[e.children[0],e.children[2]].forEach((e=>{for(const t in r)r[t]&&(e.dataset[t]=r[t])})),a=H([...s(y,e)].pop())||a,s(".footnotes",e).forEach(((t,n)=>{0===n?(c=t,e.children[1].after(t)):(c.append(...t.children),t.remove())}))})),e.body.prepend(...u),s('#TOC a[href^="#"]').forEach((e=>{const n=t(`.pagesjs-page:has(#${CSS.escape(e.getAttribute("href").replace(/^#/,""))}) .pagesjs-header`);e.dataset.pageNumber=n?n.dataset.pageNumber:""}))}addEventListener("beforeprint",O);let M=sessionStorage.getItem("pagesjs");M&&O(),addEventListener("keypress",(e=>"p"===e.key&&(O(),M=M?"":"1",sessionStorage.setItem("pagesjs",M),M||location.reload())))})(document); | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function s(t,n=e){return n?n.querySelectorAll(t):[]}function r(e){return e.childElementCount}const a=e.createElement("div"),i=s("h1").length>1,o=[],c="pagesjs-fragmented",l="fragment-first",f=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));a.className="pagesjs-page",a.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';let d,p,h,g,u=[],m=0,E=[];function L(e){const n=o.length;return n-m>1&&(o.slice(m,n-1).forEach(b),m=n-1),e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",a.innerHTML),d=e||a.cloneNode(!0),p=d.children[1],u.length&&d.classList.add(...u),o.includes(d)||o.push(d),d}function b(e){const t=e.scrollHeight;if(t>h&&!e.dataset.pagesOffset){const n=function(e,t){let n=+e.dataset.pagesOffset;if(n)return n;if(n=Math.ceil(t/h),n<=1)return n;const r=f.concat([...s("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!r)return n;function a(){return Math.ceil((t+(n-1)*r)/h)}let i=a();for(;i>n;)n=i,i=a();return n}(e,t);n>1&&(e.dataset.pagesOffset=n)}e.remove()}function T(e,t=!1){if(!e)return!1;const n=""===(t?e.innerHTML:e.innerText).trim();return n&&e.remove(),n}function v(e){if(e.classList.contains("pagesjs-page"))d.after(L(e)),r(e)>3&&(p.append(...[...e.children].slice(3)),d.after(L()));else{if(p.append(e),d.scrollHeight<=h)return;const t=e.classList,n=e.innerHTML,r=o.length;if(j(e),"PRE"===e.tagName)for(let e=o.length-1;e>=r;e--)T(o[e])&&o.splice(e,1);t.contains(c)&&e.innerHTML!==n?t.add("fragment-last"):[e].concat(...s(`.${c}`,e)).forEach((e=>e.classList.remove(c,l))),E=[]}}function H(e,t,n){e.innerHTML=E.slice(t,n).join("\n")}function j(e,t,s,a){const i=e.tagName,f=e.classList,u=f.contains(c);if(s||u&&f.remove(l),!E.length&&d.scrollHeight<=h)return;s?f.add(c):u||f.add(c,l);const m=a||d,b=e.cloneNode();if(t?t.append(b):(p.append(b),m.after(L()),p.append(e)),"PRE"===i){const t=e.firstElementChild;if("CODE"==t?.tagName){const n=E.length;n||(E=t.innerHTML.split("\n"));let s=E.length;if(s>1&&""===E[s-1]&&(s-=1),s>1)if(n||(g=t.offsetHeight/s),t.innerHTML="",j(t,b,e,m)){if(""===E.join("").trim()&&(b.before(e),e.innerHTML=b.innerHTML,b.remove(),o.pop().remove(),L(o[o.length-1]),E=[]),!E.length)return;j(e)}else b.remove(),[t,e].forEach((e=>e.classList.remove(c,l))),j(e);else H(t,0)}return T(b)}"DIV"===i&&1===r(e)&&j(e.firstElementChild,b,e,m);const v=b.previousElementSibling;if(["UL","OL","BLOCKQUOTE","FIELDSET"].includes(i)&&r(e)>1)for(;;){const t=e.firstChild;if(!t)break;if(b.append(t),!t.nodeName.startsWith("#")&&m.scrollHeight>h){(v||r(b)>1)&&e.insertBefore(t,e.firstChild),"OL"===i&&(e.start+=r(b));break}}if(t&&"CODE"===i){let t=0,s=1,r=n=E.length;const a=[];for(;r-s>1;){H(b,0,t||1);const e=h-m.offsetHeight;if(0===e){s=t||1;break}if(e<0){if(t<=0)break;r=t}else s=t;a.push(t);const i=t+Math.round(e/g);t=a.includes(i)?t+(e>0?1:-1):i,e>0&&t>=n&&(r=n+1,t=n)}return t>0?(H(b,0,s),E.splice(0,s)):(b.innerHTML="",H(e,0),E=[]),t>0}const M=T(b,!0);M&&f.remove(c,l),M&&!v||j(t?s:e)}function M(e){return e&&(e.dataset.shortTitle||e.innerText)}const C=M(t("h1.title, .frontmatter h1, .title, h1")),y=(i?"h1":"h2")+":not(.frontmatter *)";function O(){if("complete"!==e.readyState)return setTimeout(O,10);const n=e.body.classList;if(n.contains("pagesjs"))return;n.add("pagesjs"),e.body.insertAdjacentElement("afterbegin",L()),h=d.clientHeight||window.innerHeight,s(":is(#TOC, .footnotes, .chapter-before, .chapter-after):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),n.add("pagesjs-filling"),s('#TOC a[href^="#"]').forEach((t=>{const n=e.createElement("span"),s=t.firstElementChild;for(s?.classList.contains("section-number")?s.after(n):t.insertAdjacentElement("afterbegin",n);n.nextSibling;)n.append(n.nextSibling);t.insertAdjacentHTML("beforeend",'<span class="dot-leader"></span>'),t.dataset.pageNumber="000"}));const r=[];s(".body").forEach((e=>{const t=[...e.children];r.push(t),t.forEach((e=>e.remove()))})),s(".frontmatter, .abstract, #TOC:not(.chapter-toc)").forEach((e=>{v(e),i&&d.after(L())})),s(".body").forEach(((e,t)=>{u=["chapter","appendix"].filter((t=>e.classList.contains(t))),i&&(""===d.innerText?L(d):d.after(L())),r[t].forEach(v),T(e.parentNode),T(e)})),o.slice(m).forEach(b),n.remove("pagesjs-filling");let a,c=0;o.forEach((e=>{if(i){if(t(".frontmatter",e))return;t(y,e)&&(a="")}const n=+e.dataset.pagesOffset||1;n>1&&e.classList.add("page-multiple"),c+=n,e.classList.add("page-"+(c%2==0?"even":"odd"));const r={pageNumber:c,mainTitle:C,pageTitle:a};let o;[e.children[0],e.children[2]].forEach((e=>{for(const t in r)r[t]&&(e.dataset[t]=r[t])})),a=M([...s(y,e)].pop())||a,s(".footnotes",e).forEach(((t,n)=>{0===n?(o=t,e.children[1].after(t)):(o.append(...t.children),t.remove())}))})),e.body.prepend(...o),s('#TOC a[href^="#"]').forEach((e=>{const n=t(`.pagesjs-page:has(#${CSS.escape(e.getAttribute("href").replace(/^#/,""))}) .pagesjs-header`);e.dataset.pageNumber=n?n.dataset.pageNumber:""}))}addEventListener("beforeprint",O);let N=sessionStorage.getItem("pagesjs");N&&O(),addEventListener("keypress",(e=>"p"===e.key&&(O(),N=N?"":"1",sessionStorage.setItem("pagesjs",N),N||location.reload())))})(document); | ||
//# sourceMappingURL=pages.min.js.map |
{ | ||
"name": "@xiee/utils", | ||
"version": "1.13.68", | ||
"version": "1.13.69", | ||
"description": "Miscellaneous tools and utilities to manipulate HTML pages", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
244454
2990