@xiee/utils
Advanced tools
Comparing version 1.13.66 to 1.13.67
@@ -9,3 +9,4 @@ // move elements into page boxes such that each box contains as many elements as | ||
const tpl = d.createElement('div'), book = $$('h1').length > 1, boxes = [], | ||
fr_cls = 'pagesjs-fragmented', tb = ['top', 'bottom'].map(i => { | ||
cd_cls = String.fromCharCode(...Array(10).fill(0).map(x => 97 + Math.random() * 26)), | ||
fr_cls = 'pagesjs-fragmented', tb = ['top', 'bottom'].map(i => { | ||
const v = getComputedStyle(d.documentElement).getPropertyValue(`--paper-margin-${i}`); | ||
@@ -80,3 +81,11 @@ return +v.replace('px', '') || 0; | ||
const code = el.firstElementChild; | ||
code?.tagName == 'CODE' && /\n/.test(code.innerHTML) && fragment(code, el2, el, box_cur); | ||
if (code?.tagName === 'CODE') { | ||
const lines = code.innerHTML.replace(/\n$/, '').split('\n'); | ||
if (lines.length > 1) { | ||
// add <i> to each line to measure the line heights | ||
code.innerHTML = lines.concat('').map(x => `<i class="${cd_cls}"></i>${x}`).join('\n'); | ||
$$(`i.${cd_cls}`, code).forEach(i => i.dataset.top = i.offsetTop); | ||
fragment(code, el2, el, box_cur); | ||
} | ||
} | ||
} else if (tag === 'DIV' && nChild(el) === 1) { | ||
@@ -101,14 +110,27 @@ fragment(el.firstElementChild, el2, el, box_cur); | ||
} | ||
// split lines in <code> and try to move them into el2 line by line | ||
// split lines in <code> and try to move as many lines into el2 as possible | ||
if (is_code) { | ||
const code = el.innerHTML.split('\n'), code2 = []; | ||
for (let i of code) { | ||
code2.push(i); el2.innerHTML = code2.join('\n'); | ||
const code = el.innerHTML.split('\n'), delta = H - box_cur.scrollHeight, | ||
tops = [...$$(`i.${cd_cls}`, el)].map(i => i.dataset.top); | ||
//if (el.innerHTML.match('Humpty')) debugger; | ||
let i = 0; | ||
function fillCode() { | ||
el2.innerHTML = code.slice(0, i).join('\n'); | ||
} | ||
if (delta > 0) { | ||
for (i = 0; i < tops.length; i++) { | ||
if (tops[i + 1] - tops[0] > delta) break; | ||
} | ||
if (i > 0) fillCode(); | ||
} | ||
// remove/add lines from el2 one by one if box is too long/short | ||
const dir = box_cur.scrollHeight > H ? -1 : 1; | ||
while (i > 0 && i <= code.length) { | ||
i += dir; fillCode(); | ||
if (box_cur.scrollHeight > H) { | ||
code2.pop(); el2.innerHTML = code2.join('\n'); | ||
break; | ||
i += -1; fillCode(); break; | ||
} | ||
} | ||
if (code2.length > 0) { | ||
el.innerHTML = code.slice(code2.length).join('\n'); | ||
if (i > 0) { | ||
el.innerHTML = code.slice(i).join('\n'); | ||
if (removeBlank(parent)) return; // exit if <pre> is empty | ||
@@ -190,2 +212,3 @@ } | ||
boxes.forEach(addOffset); // mark the rest of pages as done | ||
$$(`pre > code i.${cd_cls}`).forEach(el => el.remove()); | ||
cls.remove('pagesjs-filling'); | ||
@@ -192,0 +215,0 @@ |
@@ -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 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); | ||
(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=String.fromCharCode(...Array(10).fill(0).map((e=>97+26*Math.random()))),c="pagesjs-fragmented",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,g=[];function h(e){return 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),i.push(f),f}function m(e){const t=e.scrollHeight;if(t>p&&!e.dataset.pagesOffset){const s=function(e,t){let s=+e.dataset.pagesOffset;if(s)return s;if(s=Math.ceil(t/p),s<=1)return s;const a=l.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)/p)}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 E(e){const t=i.length;if(e.classList.contains("pagesjs-page"))f.after(h(e)),s(e)>3&&(d.append(...[...e.children].slice(3)),f.after(h()));else{if(f.scrollHeight>p){const[e,t]=[f,d];e.after(h()),s(t)>1&&d.append(t.lastChild)}d.append(e),L(e)}const n=i.length;n>t&&n>1&&i.splice(0,n-1).forEach(m)}function L(e,t,a,r){const i=e.tagName,l="CODE"===i;if((!l||!t)&&f.scrollHeight<=p)return;e.classList.add(c);const g=r||f,m=e.cloneNode();if(t?t.append(m):(d.append(m),g.after(h()),d.append(e)),"PRE"===i){const T=e.firstElementChild;if("CODE"===T?.tagName){const H=T.innerHTML.replace(/\n$/,"").split("\n");H.length>1&&(T.innerHTML=H.concat("").map((e=>`<i class="${o}"></i>${e}`)).join("\n"),n(`i.${o}`,T).forEach((e=>e.dataset.top=e.offsetTop)),L(T,m,e,g))}}else"DIV"===i&&1===s(e)&&L(e.firstElementChild,m,e,g);const E=m.previousElementSibling;if(["UL","OL","BLOCKQUOTE"].includes(i)&&s(e)>1)for(;;){const v=e.firstChild;if(!v)break;if(m.append(v),!v.nodeName.startsWith("#")&&g.scrollHeight>p){(E||s(m)>1)&&e.insertBefore(v,e.firstChild),"OL"===i&&(e.start+=s(m));break}}if(l){const C=e.innerHTML.split("\n"),O=p-g.scrollHeight,y=[...n(`i.${o}`,e)].map((e=>e.dataset.top));let M=0;function j(){m.innerHTML=C.slice(0,M).join("\n")}if(O>0){for(M=0;M<y.length&&!(y[M+1]-y[0]>O);M++);M>0&&j()}const S=g.scrollHeight>p?-1:1;for(;M>0&&M<=C.length;)if(M+=S,j(),g.scrollHeight>p){M+=-1,j();break}if(M>0&&(e.innerHTML=C.slice(M).join("\n"),u(a)))return}const b=u(m);b&&e.classList.remove(c),(!b||l||E)&&L(t?a:e)}function j(e){return e&&(e.dataset.shortTitle||e.innerText)}const b=j(t("h1.title, .frontmatter h1, .title, h1")),T=(r?"h1":"h2")+":not(.frontmatter *)";function H(){if("complete"!==e.readyState)return setTimeout(H,10);const s=e.body.classList;if(s.contains("pagesjs"))return;s.add("pagesjs"),e.body.insertAdjacentElement("afterbegin",h()),p=f.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&&(E(e),r&&f.after(h()))})),n(".body").forEach((e=>{g=["chapter","appendix"].filter((t=>e.classList.contains(t))),r&&(""===f.innerText?h(f):f.after(h())),[...e.children].map(E),u(e.parentNode),u(e)})),i.forEach(m),n(`pre > code i.${o}`).forEach((e=>e.remove())),s.remove("pagesjs-filling");let a,c=0;n(".pagesjs-page").forEach((e=>{if(u(e))return;if(r){if(t(".frontmatter",e))return;t(T,e)&&(a="")}const s=+e.dataset.pagesOffset||1;s>1&&e.classList.add("page-multiple"),c+=s,e.classList.add("page-"+(c%2==0?"even":"odd"));const i={pageNumber:c,mainTitle:b,pageTitle:a};let o;[e.children[0],e.children[2]].forEach((e=>{for(const t in i)i[t]&&(e.dataset[t]=i[t])})),a=j([...n(T,e)].pop())||a,n(".footnotes",e).forEach(((t,n)=>{0===n?(o=t,e.children[1].after(t)):(o.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",H);let v=sessionStorage.getItem("pagesjs");v&&H(),addEventListener("keypress",(e=>"p"===e.key&&(H(),v=v?"":"1",sessionStorage.setItem("pagesjs",v),v||location.reload())))})(document); | ||
//# sourceMappingURL=pages.min.js.map |
{ | ||
"name": "@xiee/utils", | ||
"version": "1.13.66", | ||
"version": "1.13.67", | ||
"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
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
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
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
242183
2959