@xiee/utils
Advanced tools
Comparing version 1.13.65 to 1.13.66
document.querySelectorAll('h1,h2,h3,h4,h5,h6').forEach(h => { | ||
if (h.id && !h.querySelector('.anchor')) | ||
h.insertAdjacentHTML('beforeend', ` <span class="anchor"><a href="#${h.id}">#</a></span>`); | ||
h.insertAdjacentHTML('beforeend', ` <span class="anchor"><a href="#${h.id}"></a></span>`); | ||
}); |
@@ -1,2 +0,2 @@ | ||
document.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach((e=>{e.id&&!e.querySelector(".anchor")&&e.insertAdjacentHTML("beforeend",` <span class="anchor"><a href="#${e.id}">#</a></span>`)})); | ||
document.querySelectorAll("h1,h2,h3,h4,h5,h6").forEach((e=>{e.id&&!e.querySelector(".anchor")&&e.insertAdjacentHTML("beforeend",` <span class="anchor"><a href="#${e.id}"></a></span>`)})); | ||
//# sourceMappingURL=heading-anchor.min.js.map |
@@ -8,3 +8,7 @@ // move elements into page boxes such that each box contains as many elements as | ||
const tpl = d.createElement('div'), book = $$('h1').length > 1, fr_cls = 'pagesjs-fragmented'; | ||
const tpl = d.createElement('div'), book = $$('h1').length > 1, boxes = [], | ||
fr_cls = 'pagesjs-fragmented', tb = ['top', 'bottom'].map(i => { | ||
const v = getComputedStyle(d.documentElement).getPropertyValue(`--paper-margin-${i}`); | ||
return +v.replace('px', '') || 0; | ||
}); // top/bottom page margin | ||
tpl.className = 'pagesjs-page'; | ||
@@ -19,8 +23,12 @@ tpl.innerHTML = `<div class="pagesjs-header"></div> | ||
box_cls.length && box.classList.add(...box_cls); | ||
boxes.push(box); // store new pages in boxes | ||
return box; | ||
} | ||
function testHeight(box, done) { | ||
const ret = box.scrollHeight > H; | ||
(done || ret) && box.classList.add('pagesjs-done'); | ||
return ret; | ||
function addOffset(box) { | ||
const h = box.scrollHeight; | ||
if (h > H && !box.dataset.pagesOffset) { | ||
const n = calcPages(box, h); | ||
if (n > 1) box.dataset.pagesOffset = n; | ||
} | ||
box.classList.add('pagesjs-done'); | ||
} | ||
@@ -34,2 +42,3 @@ function removeBlank(el) { | ||
function fill(el) { | ||
const n1 = boxes.length; | ||
// if the element is already a page, just use it as the box | ||
@@ -42,16 +51,18 @@ if (el.classList.contains('pagesjs-page')) { | ||
// TODO: should we fragment this page if it's too long? | ||
testHeight(box, true); | ||
box.after(newPage()); // create a new empty page | ||
} | ||
return; | ||
} else { | ||
// create a new box when too much content (exceeding original height) | ||
if (box.scrollHeight > H) { | ||
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 | ||
nChild(box_body2) > 1 && box_body.append(box_body2.lastChild); | ||
} | ||
box_body.append(el); | ||
fragment(el); | ||
} | ||
// create a new box when too much content (exceeding original height) | ||
if (testHeight(box)) { | ||
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 | ||
nChild(box_body2) > 1 && box_body.append(box_body2.lastChild); | ||
} | ||
box_body.append(el); | ||
fragment(el); | ||
const n2 = boxes.length; | ||
// mark all boxes (except the last one) with class 'pagesjs-done' | ||
n2 > n1 && n2 > 1 && boxes.splice(0, n2 - 1).forEach(addOffset); | ||
} | ||
@@ -84,3 +95,3 @@ // break elements that are relatively easy to break (such as <ul>) | ||
if (item.nodeName.startsWith('#')) continue; | ||
if (testHeight(box_cur)) { | ||
if (box_cur.scrollHeight > H) { | ||
// move item back to el if the clone el2 is not the only element on page or has more than one child | ||
@@ -98,3 +109,3 @@ (prev || nChild(el2) > 1) && el.insertBefore(item, el.firstChild); | ||
code2.push(i); el2.innerHTML = code2.join('\n'); | ||
if (testHeight(box_cur)) { | ||
if (box_cur.scrollHeight > H) { | ||
code2.pop(); el2.innerHTML = code2.join('\n'); | ||
@@ -120,7 +131,3 @@ break; | ||
const main = shortTitle($('h1.title, .frontmatter h1, .title, h1')), // main title | ||
ps = (book ? 'h1' : 'h2') + ':not(.frontmatter *)', // page title selector | ||
tb = ['top', 'bottom'].map(i => { | ||
const v = getComputedStyle(d.documentElement).getPropertyValue(`--paper-margin-${i}`); | ||
return +v.replace('px', '') || 0; | ||
}); // top/bottom page margin | ||
ps = (book ? 'h1' : 'h2') + ':not(.frontmatter *)'; // page title selector | ||
@@ -130,6 +137,5 @@ // calculate how many new pages we need for overflowed content (this is just | ||
// to provide a number manually) | ||
function calcPages(box) { | ||
function calcPages(box, h) { | ||
let n = +box.dataset.pagesOffset; | ||
if (n) return n; | ||
const h = box.scrollHeight; | ||
n = Math.ceil(h/H); | ||
@@ -188,2 +194,3 @@ if (n <= 1) return n; | ||
}); | ||
boxes.forEach(addOffset); // mark the rest of pages as done | ||
cls.remove('pagesjs-filling'); | ||
@@ -195,3 +202,2 @@ | ||
if (removeBlank(box)) return; // remove empty pages | ||
box.classList.remove('pagesjs-done'); | ||
if (book) { | ||
@@ -201,3 +207,3 @@ if ($('.frontmatter', box)) return; // skip book frontmatter page | ||
} | ||
const N = calcPages(box); | ||
const N = +box.dataset.pagesOffset || 1; | ||
if (N > 1) box.classList.add('page-multiple'); | ||
@@ -227,2 +233,5 @@ i += N; | ||
}); | ||
// unhide all pages | ||
$$('.pagesjs-done').forEach(box => box.classList.remove('pagesjs-done')); | ||
} | ||
@@ -229,0 +238,0 @@ addEventListener('beforeprint', paginate); |
@@ -1,2 +0,2 @@ | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function n(t,n=e){return n?n.querySelectorAll(t):[]}function s(e){return e.childElementCount}const r=e.createElement("div"),a=n("h1").length>1,i="pagesjs-fragmented";r.className="pagesjs-page",r.innerHTML='<div class="pagesjs-header"></div>\n<div class="pagesjs-body"></div>\n<div class="pagesjs-footer"></div>';let o,c,l,d=[];function f(e){return e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",r.innerHTML),o=e||r.cloneNode(!0),c=o.children[1],d.length&&o.classList.add(...d),o}function p(e,t){const n=e.scrollHeight>l;return(t||n)&&e.classList.add("pagesjs-done"),n}function g(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function h(e){if(e.classList.contains("pagesjs-page"))return o.after(f(e)),void(s(e)>3&&(c.append(...[...e.children].slice(3)),p(o,!0),o.after(f())));if(p(o)){const[e,t]=[o,c];e.after(f()),s(t)>1&&c.append(t.lastChild)}c.append(e),u(e)}function u(e,t,n,r){const a=e.tagName,d="CODE"===a;if((!d||!t)&&o.scrollHeight<=l)return;e.classList.add(i);const h=r||o,m=e.cloneNode();if(t?t.append(m):(c.append(m),h.after(f()),c.append(e)),"PRE"===a){const t=e.firstElementChild;"CODE"==t?.tagName&&/\n/.test(t.innerHTML)&&u(t,m,e,h)}else"DIV"===a&&1===s(e)&&u(e.firstElementChild,m,e,h);const L=m.previousElementSibling;if(["UL","OL","BLOCKQUOTE"].includes(a)&&s(e)>1)for(;;){const t=e.firstChild;if(!t)break;if(m.append(t),!t.nodeName.startsWith("#")&&p(h)){(L||s(m)>1)&&e.insertBefore(t,e.firstChild),"OL"===a&&(e.start+=s(m));break}}if(d){const t=e.innerHTML.split("\n"),s=[];for(let e of t)if(s.push(e),m.innerHTML=s.join("\n"),p(h)){s.pop(),m.innerHTML=s.join("\n");break}if(s.length>0&&(e.innerHTML=t.slice(s.length).join("\n"),g(n)))return}const b=g(m);b&&e.classList.remove(i),(!b||d||L)&&u(t?n:e)}function m(e){return e&&(e.dataset.shortTitle||e.innerText)}const L=m(t("h1.title, .frontmatter h1, .title, h1")),b=(a?"h1":"h2")+":not(.frontmatter *)",j=["top","bottom"].map((t=>+getComputedStyle(e.documentElement).getPropertyValue(`--paper-margin-${t}`).replace("px","")||0));function E(){if("complete"!==e.readyState)return setTimeout(E,10);const s=e.body.classList;if(s.contains("pagesjs"))return;s.add("pagesjs"),e.body.insertAdjacentElement("afterbegin",f()),l=o.clientHeight||window.innerHeight,n(":is(#TOC, .footnotes, .chapter-before, .chapter-after):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),s.add("pagesjs-filling"),n('#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"})),[t(".frontmatter"),t("#TOC"),t(".abstract")].forEach((e=>{e&&(h(e),a&&o.after(f()))})),n(".body").forEach((e=>{d=["chapter","appendix"].filter((t=>e.classList.contains(t))),a&&(""===o.innerText?f(o):o.after(f())),[...e.children].map(h),g(e.parentNode),g(e)})),s.remove("pagesjs-filling");let r,i=0;n(".pagesjs-page").forEach((e=>{if(g(e))return;if(e.classList.remove("pagesjs-done"),a){if(t(".frontmatter",e))return;t(b,e)&&(r="")}const s=function(e){let t=+e.dataset.pagesOffset;if(t)return t;const s=e.scrollHeight;if(t=Math.ceil(s/l),t<=1)return t;const r=j.concat([...n("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!r)return t;function a(){return Math.ceil((s+(t-1)*r)/l)}let i=a();for(;i>t;)t=i,i=a();return t}(e);s>1&&e.classList.add("page-multiple"),i+=s,e.classList.add("page-"+(i%2==0?"even":"odd"));const o={pageNumber:i,mainTitle:L,pageTitle:r};let c;[e.children[0],e.children[2]].forEach((e=>{for(const t in o)o[t]&&(e.dataset[t]=o[t])})),r=m([...n(b,e)].pop())||r,n(".footnotes",e).forEach(((t,n)=>{0===n?(c=t,e.children[1].after(t)):(c.append(...t.children),t.remove())}))})),n('#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",E);let T=sessionStorage.getItem("pagesjs");T&&E(),addEventListener("keypress",(e=>"p"===e.key&&(E(),T=T?"":"1",sessionStorage.setItem("pagesjs",T),T||location.reload())))})(document); | ||
(e=>{function t(t,n=e){return n?.querySelector(t)}function n(t,n=e){return n?n.querySelectorAll(t):[]}function s(e){return e.childElementCount}const a=e.createElement("div"),r=n("h1").length>1,i=[],o="pagesjs-fragmented",c=["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 l,d,f,p=[];function g(e){return e&&!t(".pagesjs-body",e)&&e.insertAdjacentHTML("afterbegin",a.innerHTML),l=e||a.cloneNode(!0),d=l.children[1],p.length&&l.classList.add(...p),i.push(l),l}function h(e){const t=e.scrollHeight;if(t>f&&!e.dataset.pagesOffset){const s=function(e,t){let s=+e.dataset.pagesOffset;if(s)return s;if(s=Math.ceil(t/f),s<=1)return s;const a=c.concat([...n("thead",e)].map((e=>+e.offsetHeight))).reduce(((e,t)=>e+t));if(!a)return s;function r(){return Math.ceil((t+(s-1)*a)/f)}let i=r();for(;i>s;)s=i,i=r();return s}(e,t);s>1&&(e.dataset.pagesOffset=s)}e.classList.add("pagesjs-done")}function u(e){if(!e)return!1;const t=""===e.innerText.trim();return t&&e.remove(),t}function m(e){const t=i.length;if(e.classList.contains("pagesjs-page"))l.after(g(e)),s(e)>3&&(d.append(...[...e.children].slice(3)),l.after(g()));else{if(l.scrollHeight>f){const[e,t]=[l,d];e.after(g()),s(t)>1&&d.append(t.lastChild)}d.append(e),E(e)}const n=i.length;n>t&&n>1&&i.splice(0,n-1).forEach(h)}function E(e,t,n,a){const r=e.tagName,i="CODE"===r;if((!i||!t)&&l.scrollHeight<=f)return;e.classList.add(o);const c=a||l,p=e.cloneNode();if(t?t.append(p):(d.append(p),c.after(g()),d.append(e)),"PRE"===r){const t=e.firstElementChild;"CODE"==t?.tagName&&/\n/.test(t.innerHTML)&&E(t,p,e,c)}else"DIV"===r&&1===s(e)&&E(e.firstElementChild,p,e,c);const h=p.previousElementSibling;if(["UL","OL","BLOCKQUOTE"].includes(r)&&s(e)>1)for(;;){const t=e.firstChild;if(!t)break;if(p.append(t),!t.nodeName.startsWith("#")&&c.scrollHeight>f){(h||s(p)>1)&&e.insertBefore(t,e.firstChild),"OL"===r&&(e.start+=s(p));break}}if(i){const t=e.innerHTML.split("\n"),s=[];for(let e of t)if(s.push(e),p.innerHTML=s.join("\n"),c.scrollHeight>f){s.pop(),p.innerHTML=s.join("\n");break}if(s.length>0&&(e.innerHTML=t.slice(s.length).join("\n"),u(n)))return}const m=u(p);m&&e.classList.remove(o),(!m||i||h)&&E(t?n:e)}function L(e){return e&&(e.dataset.shortTitle||e.innerText)}const j=L(t("h1.title, .frontmatter h1, .title, h1")),b=(r?"h1":"h2")+":not(.frontmatter *)";function T(){if("complete"!==e.readyState)return setTimeout(T,10);const s=e.body.classList;if(s.contains("pagesjs"))return;s.add("pagesjs"),e.body.insertAdjacentElement("afterbegin",g()),f=l.clientHeight||window.innerHeight,n(":is(#TOC, .footnotes, .chapter-before, .chapter-after):is(.side-left, .side-right).side").forEach((e=>{e.classList.remove("side","side-left","side-right")})),s.add("pagesjs-filling"),n('#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"})),[t(".frontmatter"),t("#TOC"),t(".abstract")].forEach((e=>{e&&(m(e),r&&l.after(g()))})),n(".body").forEach((e=>{p=["chapter","appendix"].filter((t=>e.classList.contains(t))),r&&(""===l.innerText?g(l):l.after(g())),[...e.children].map(m),u(e.parentNode),u(e)})),i.forEach(h),s.remove("pagesjs-filling");let a,o=0;n(".pagesjs-page").forEach((e=>{if(u(e))return;if(r){if(t(".frontmatter",e))return;t(b,e)&&(a="")}const s=+e.dataset.pagesOffset||1;s>1&&e.classList.add("page-multiple"),o+=s,e.classList.add("page-"+(o%2==0?"even":"odd"));const i={pageNumber:o,mainTitle:j,pageTitle:a};let c;[e.children[0],e.children[2]].forEach((e=>{for(const t in i)i[t]&&(e.dataset[t]=i[t])})),a=L([...n(b,e)].pop())||a,n(".footnotes",e).forEach(((t,n)=>{0===n?(c=t,e.children[1].after(t)):(c.append(...t.children),t.remove())}))})),n('#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:""})),n(".pagesjs-done").forEach((e=>e.classList.remove("pagesjs-done")))}addEventListener("beforeprint",T);let v=sessionStorage.getItem("pagesjs");v&&T(),addEventListener("keypress",(e=>"p"===e.key&&(T(),v=v?"":"1",sessionStorage.setItem("pagesjs",v),v||location.reload())))})(document); | ||
//# sourceMappingURL=pages.min.js.map |
{ | ||
"name": "@xiee/utils", | ||
"version": "1.13.65", | ||
"version": "1.13.66", | ||
"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
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
240229
2936