walter-boente_book-collection/js/numParagraph.js
2026-04-10 11:46:11 +02:00

110 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Handler } from '/csspageweaver/lib/paged.esm.js';
export default class thesis extends Handler {
constructor(chunker, polisher, caller) {
super(chunker, polisher, caller);
}
// Créer un wrapper pour récupérer tous les éléments qui suivent le ol (hors titres et .container-following-note)
// --------------------------------------------------------------------------------------------------------------
beforeParsed(content){
const nums = content.querySelectorAll('ol[type="1"]');
nums.forEach((num) => {
const wrapper = document.createElement('div');
wrapper.classList.add('wrapper-ol');
wrapper.id = 'wrapper-' + (num.getAttribute('start') || '1');
// Collecter les frères/sœurs suivants jusqu'à la prochaine limite
const siblings = [];
let sibling = num.nextElementSibling;
while (sibling) {
if (sibling.matches('ol[type="1"], h1, h2, h3, h4, h5, h6, .container-following-note')) break;
siblings.push(sibling);
sibling = sibling.nextElementSibling;
}
// Insérer le wrapper à la place du ol
num.before(wrapper);
wrapper.appendChild(num);
siblings.forEach(s => wrapper.appendChild(s));
});
// Si le dernier enfant d'un wrapper est un p et que le suivant est aussi un wrapper → .wrapper-indent
const wrappers = content.querySelectorAll('.wrapper-ol');
wrappers.forEach((wrapper) => {
const last = wrapper.lastElementChild;
const next = wrapper.nextElementSibling;
if (last && last.nodeName === 'P' && next && next.classList.contains('wrapper-ol')) {
const firstP = next.querySelector('p');
if (!firstP || !firstP.classList.contains('p-these')) {
next.classList.add('wrapper-indent');
}
}
});
}
afterPageLayout(pageElement, page, breakToken){
const wrappers = pageElement.querySelectorAll('.wrapper-ol');
const minSize = 37; // taille minimal du wrapper pour quil y ait clone (2 lignes)
// Fais un clone du ol pour de la page précédente
// -----------------------------------------------
if (wrappers.length > 0) {
const first = wrappers[0];
if (first.hasAttribute('data-split-from')) {
let idWrapper = first.getAttribute('data-id');
let numPage = pageElement.getAttribute('data-page-number');
let numPrev = parseInt(numPage) - 1;
let prevPage = document.querySelector('#page-' + numPrev);
let olPara = prevPage.querySelector('#' + idWrapper + ' ol[type="1"]');
if (olPara && first.offsetHeight >= minSize) {
const start = olPara.getAttribute('start') || '1';
const olClonePage = document.createElement('ol');
olClonePage.setAttribute('start', start);
olClonePage.setAttribute('type', '1');
olClonePage.classList.add('ol-clone-page');
olClonePage.style.height = first.offsetHeight + 'px';
const li = document.createElement('li');
li.setAttribute('data-item-num', start);
olClonePage.appendChild(li);
first.prepend(olClonePage);
}
}
}
// Fais un clone du ol pour la colonne suivante (dans la même page)
// ----------------------------------------------------------------
wrappers.forEach((wrapper) => {
const ol = wrapper.querySelector('ol[type="1"]');
if (!ol) return;
const rects = wrapper.getClientRects();
if (rects.length === 1) {
ol.style.height = rects[0].height + 'px';
} else if (rects.length === 2) {
ol.style.height = rects[0].height + 'px';
if (rects[1].height >= minSize) {
const olClone = ol.cloneNode(true);
olClone.classList.add('ol-clone');
olClone.style.height = rects[1].height + 'px';
olClone.removeAttribute('id');
ol.after(olClone);
}
}
});
}
}