@xiee/utils
Advanced tools
Comparing version 1.13.8 to 1.13.9
@@ -12,5 +12,7 @@ // move elements into page boxes such that each box contains as many elements as | ||
<div class="pagesjs-footer"></div>`; | ||
let box, box_body, H; | ||
let box, box_body, H, box_cls = []; | ||
function newPage(el) { | ||
el && !$('.pagesjs-body', el) && el.insertAdjacentHTML('afterbegin', tpl.innerHTML); | ||
box = el || tpl.cloneNode(true); box_body = box.children[1]; | ||
box_cls.length && box.classList.add(...box_cls); | ||
return box; | ||
@@ -27,28 +29,50 @@ } | ||
if (el.classList.contains('pagesjs-page')) { | ||
box.after(el); | ||
!$('.pagesjs-body', el) && el.insertAdjacentHTML('afterbegin', tpl.innerHTML); | ||
box.after(newPage(el)); | ||
// if current element is not empty, fill its content into the box | ||
if (el.childElementCount > 3) { | ||
el.children[1].append(...[...el.children].slice(3)); | ||
el.after(newPage()); // create a new empty page | ||
} else { | ||
newPage(el); | ||
box_body.append(...[...el.children].slice(3)); | ||
// TODO: should we fragment this page if it's too long? | ||
box.after(newPage()); // create a new empty page | ||
} | ||
return el; | ||
return; | ||
} | ||
// create a new box when too much content (exceeding original height) | ||
if (box.scrollHeight > H) { | ||
const box2 = tpl.cloneNode(true), box_body2 = box2.children[1]; | ||
box.after(box2); | ||
// if there's more than one child in the box, move the last child out | ||
box_body.childElementCount > 1 && box_body2.append(box_body.lastElementChild); | ||
[box, box_body] = [box2, box_body2]; | ||
const [box2, box_body2] = [box, box_body]; // store old box | ||
box2.after(newPage()); | ||
// if there's more than one child in the box, move the last child to next box | ||
box_body2.childElementCount > 1 && box_body.append(box_body2.lastElementChild); | ||
} | ||
box_body.append(el); | ||
return box; | ||
fragment(el); | ||
} | ||
// break elements that are relatively easy to break (such as <ul>) | ||
function fragment(el, container, parent, page) { | ||
if (box.scrollHeight <= H) return; | ||
const box_cur = page || box, el2 = el.cloneNode(); // shallow clone (wrapper only) | ||
// add the clone to current box, and move original el to next box | ||
container ? container.append(el2) : ( | ||
box_body.append(el2), box_cur.after(newPage()), box_body.append(el) | ||
); | ||
// for DIVs containing a single child (e.g., #TOC > ul), try to break the child | ||
if (el.tagName === 'DIV' && el.childElementCount === 1) { | ||
fragment(el.firstElementChild, el2, el, box_cur); | ||
} | ||
// keep moving el's first item to el2 until page height > H | ||
if (['UL', 'BLOCKQUOTE'].indexOf(el.tagName) > -1 && el.childElementCount > 1) while (true) { | ||
const item = el.firstChild; | ||
if (!item) break; | ||
el2.append(item); | ||
if (box_cur.scrollHeight > H) { | ||
el.insertBefore(item, el.firstChild); | ||
break; | ||
} | ||
} | ||
el2.lastChild || el2.remove(); // remove the clone if empty | ||
fragment(container ? parent : el); | ||
} | ||
// use data-short-title of a header if exists, and fall back to inner text | ||
function shortTitle(h) { | ||
return h && (h.dataset['shortTitle'] || h.innerText); | ||
return h && (h.dataset.shortTitle || h.innerText); | ||
} | ||
@@ -66,3 +90,3 @@ const main = shortTitle($('h1.title, .frontmatter h1, .title, h1')), // main title | ||
function calcPages(box) { | ||
let n = +box.dataset['pagesOffset']; | ||
let n = +box.dataset.pagesOffset; | ||
if (n) return n; | ||
@@ -98,10 +122,9 @@ const h = box.scrollHeight; | ||
// iteratively add elements to pages | ||
$$('.frontmatter, #TOC, .abstract').forEach(el => { | ||
book ? (box_body.append(el), box.after(newPage())) : fill(el); | ||
}); | ||
$$('.frontmatter, #TOC, .abstract').forEach(el => (fill(el), book && box.after(newPage()))); | ||
$$('.body').forEach(el => { | ||
// preserve book chapter classes if exist | ||
const extra = ['chapter', 'appendix'].filter(i => el.classList.contains(i)); | ||
box_cls = ['chapter', 'appendix'].filter(i => el.classList.contains(i)); | ||
newPage(box); | ||
book && box.innerText !== '' && box.after(newPage()); | ||
[...el.children].map(c => fill(c).classList.add(...extra)); | ||
[...el.children].map(fill); | ||
el.childElementCount === 0 && el.remove(); | ||
@@ -121,3 +144,3 @@ }); | ||
a.insertAdjacentHTML('beforeend', '<span class="dot-leader"></span>'); | ||
a.dataset['pageNumber'] = '000'; // placeholder for page numbers | ||
a.dataset.pageNumber = '000'; // placeholder for page numbers | ||
}); | ||
@@ -151,3 +174,3 @@ | ||
const p = $(`.pagesjs-page:has(${a.getAttribute('href')}) .pagesjs-header`); | ||
a.dataset['pageNumber'] = p ? p.dataset['pageNumber'] : ''; | ||
a.dataset.pageNumber = p ? p.dataset.pageNumber : ''; | ||
}); | ||
@@ -154,0 +177,0 @@ } |
@@ -1,1 +0,1 @@ | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function n(t,n=e){return n?n.querySelectorAll(t):[]}const a=e.createElement("div"),r=n("h1").length>1;let s,i,o;function c(e){return s=e||a.cloneNode(!0),i=s.children[1],s}function l(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function d(e){if(e.classList.contains("pagesjs-page"))return s.after(e),!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",a.innerHTML),e.childElementCount>3?(e.children[1].append(...[...e.children].slice(3)),e.after(c())):c(e),e;if(s.scrollHeight>o){const e=a.cloneNode(!0),t=e.children[1];s.after(e),i.childElementCount>1&&t.append(i.lastElementChild),[s,i]=[e,t]}return i.append(e),s}function p(e){return e&&(e.dataset.shortTitle||e.innerText)}a.className="pagesjs-page",a.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';const f=p(t("h1.title, .frontmatter h1, .title, h1")),g=(r?"h1":"h2")+":not(.frontmatter *)",h=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));function u(){const a=e.body.classList;if(a.contains("pagesjs"))return;a.add("pagesjs"),r&&a.add("page-book"),e.body.insertAdjacentElement("afterbegin",c()),o=s.clientHeight||window.innerHeight,n(":is(#TOC, .footnotes):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),a.add("pagesjs-filling"),n(".frontmatter, #TOC, .abstract").forEach((e=>{r?(i.append(e),s.after(c())):d(e)})),n(".body").forEach((e=>{const t=["chapter","appendix"].filter((t=>e.classList.contains(t)));r&&""!==s.innerText&&s.after(c()),[...e.children].map((e=>d(e).classList.add(...t))),0===e.childElementCount&&e.remove()})),a.remove("pagesjs-filling"),r&&l(s.nextElementSibling);const u=t("#TOC");n('a[href^="#"]',u).forEach((t=>{const n=e.createElement("span"),a=t.firstElementChild;for(a?.classList.contains("section-number")?a.after(n):t.insertAdjacentElement("afterbegin",n);n.nextSibling;)n.append(n.nextSibling);t.insertAdjacentHTML("beforeend",'<span class="dot-leader"></span>'),t.dataset.pageNumber="000"}));let m,b=0;n(".pagesjs-page").forEach((e=>{if(l(e))return;r&&t(g,e)&&(m="");const a=function(e){let t=+e.dataset.pagesOffset;if(t)return t;const a=e.scrollHeight;if(t=Math.ceil(a/o),t<=1)return t;const r=h.concat([...n("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!r)return t;function s(){return Math.ceil((a+(t-1)*r)/o)}let i=s();for(;i>t;)t=i,i=s();return t}(e);a>1&&e.classList.add("page-multiple"),b+=a;const s={pageNumber:b,mainTitle:f,pageTitle:m};let i;[e.children[0],e.children[2]].forEach((e=>{for(const t in s)s[t]&&(e.dataset[t]=s[t])})),m=p([...n(g,e)].pop())||m,n(".footnotes",e).forEach(((t,n)=>{0===n?(i=t,e.children[1].after(t)):(i.append(...t.children),t.remove())}))})),n('a[href^="#"]',u).forEach((e=>{const n=t(`.pagesjs-page:has(${e.getAttribute("href")}) .pagesjs-header`);e.dataset.pageNumber=n?n.dataset.pageNumber:""}))}addEventListener("beforeprint",u);let m=sessionStorage.getItem("pagesjs");m&&u(),addEventListener("keypress",(e=>"p"===e.key&&(u(),m=m?"":"1",sessionStorage.setItem("pagesjs",m),m||location.reload())))})(document); | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function n(t,n=e){return n?n.querySelectorAll(t):[]}const a=e.createElement("div"),r=n("h1").length>1;a.className="pagesjs-page",a.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';let s,i,o,l=[];function c(e){return e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",a.innerHTML),s=e||a.cloneNode(!0),i=s.children[1],l.length&&s.classList.add(...l),s}function d(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function f(e){if(e.classList.contains("pagesjs-page"))return s.after(c(e)),void(e.childElementCount>3&&(i.append(...[...e.children].slice(3)),s.after(c())));if(s.scrollHeight>o){const[e,t]=[s,i];e.after(c()),t.childElementCount>1&&i.append(t.lastElementChild)}i.append(e),p(e)}function p(e,t,n,a){if(s.scrollHeight<=o)return;const r=a||s,l=e.cloneNode();if(t?t.append(l):(i.append(l),r.after(c()),i.append(e)),"DIV"===e.tagName&&1===e.childElementCount&&p(e.firstElementChild,l,e,r),["UL","BLOCKQUOTE"].indexOf(e.tagName)>-1&&e.childElementCount>1)for(;;){const t=e.firstChild;if(!t)break;if(l.append(t),r.scrollHeight>o){e.insertBefore(t,e.firstChild);break}}l.lastChild||l.remove(),p(t?n:e)}function g(e){return e&&(e.dataset.shortTitle||e.innerText)}const h=g(t("h1.title, .frontmatter h1, .title, h1")),m=(r?"h1":"h2")+":not(.frontmatter *)",u=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));function E(){const a=e.body.classList;if(a.contains("pagesjs"))return;a.add("pagesjs"),r&&a.add("page-book"),e.body.insertAdjacentElement("afterbegin",c()),o=s.clientHeight||window.innerHeight,n(":is(#TOC, .footnotes):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),a.add("pagesjs-filling"),n(".frontmatter, #TOC, .abstract").forEach((e=>(f(e),r&&s.after(c())))),n(".body").forEach((e=>{l=["chapter","appendix"].filter((t=>e.classList.contains(t))),c(s),r&&""!==s.innerText&&s.after(c()),[...e.children].map(f),0===e.childElementCount&&e.remove()})),a.remove("pagesjs-filling"),r&&d(s.nextElementSibling);const i=t("#TOC");n('a[href^="#"]',i).forEach((t=>{const n=e.createElement("span"),a=t.firstElementChild;for(a?.classList.contains("section-number")?a.after(n):t.insertAdjacentElement("afterbegin",n);n.nextSibling;)n.append(n.nextSibling);t.insertAdjacentHTML("beforeend",'<span class="dot-leader"></span>'),t.dataset.pageNumber="000"}));let p,E=0;n(".pagesjs-page").forEach((e=>{if(d(e))return;r&&t(m,e)&&(p="");const a=function(e){let t=+e.dataset.pagesOffset;if(t)return t;const a=e.scrollHeight;if(t=Math.ceil(a/o),t<=1)return t;const r=u.concat([...n("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!r)return t;function s(){return Math.ceil((a+(t-1)*r)/o)}let i=s();for(;i>t;)t=i,i=s();return t}(e);a>1&&e.classList.add("page-multiple"),E+=a;const s={pageNumber:E,mainTitle:h,pageTitle:p};let i;[e.children[0],e.children[2]].forEach((e=>{for(const t in s)s[t]&&(e.dataset[t]=s[t])})),p=g([...n(m,e)].pop())||p,n(".footnotes",e).forEach(((t,n)=>{0===n?(i=t,e.children[1].after(t)):(i.append(...t.children),t.remove())}))})),n('a[href^="#"]',i).forEach((e=>{const n=t(`.pagesjs-page:has(${e.getAttribute("href")}) .pagesjs-header`);e.dataset.pageNumber=n?n.dataset.pageNumber:""}))}addEventListener("beforeprint",E);let b=sessionStorage.getItem("pagesjs");b&&E(),addEventListener("keypress",(e=>"p"===e.key&&(E(),b=b?"":"1",sessionStorage.setItem("pagesjs",b),b||location.reload())))})(document); |
{ | ||
"name": "@xiee/utils", | ||
"version": "1.13.8", | ||
"version": "1.13.9", | ||
"description": "Miscellaneous tools and utilities to manipulate HTML pages", | ||
@@ -5,0 +5,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
206936
180
2353