layout.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // fonction s'éxecute après le rendu de pagedJS
  2. // cf https://pagedjs.org/documentation/10-handlers-hooks-and-custom-javascript/
  3. class setMarginTexts extends Paged.Handler {
  4. constructor(chunker, polisher, caller) {
  5. super(chunker, polisher, caller);
  6. }
  7. afterPreview(pages) {
  8. // afficher correctement les éléments en marge
  9. let topLeftBoxes = document.getElementsByClassName('pagedjs_margin-left-top');
  10. for (let i = 0; i < topLeftBoxes.length; i++) {
  11. topLeftBoxes[i].childNodes[0].style.marginTop = topLeftBoxes[i].childNodes[0].offsetWidth / 2 - topLeftBoxes[i].childNodes[0].offsetHeight / 2 + "px";
  12. }
  13. let bottomLeftBoxes = document.getElementsByClassName('pagedjs_margin-left-bottom');
  14. for (let i = 0; i < bottomLeftBoxes.length; i++) {
  15. bottomLeftBoxes[i].childNodes[0].style.marginBottom = bottomLeftBoxes[i].childNodes[0].offsetWidth / 2 - bottomLeftBoxes[i].childNodes[0].offsetHeight / 2 + "px";
  16. }
  17. // cleaner pour que les paragraphes tombent sur la marge haute
  18. for(let i = 0; i < labeurs.length; i++) {
  19. if (labeurs[i] == labeurs[i].parentNode.firstElementChild
  20. && !labeurs[i].firstElementChild.hasAttribute("data-split-from")) {
  21. labeurs[i].firstElementChild.style.marginTop = "0px";
  22. }
  23. }
  24. // pareil pour les titres temps
  25. for (let i = 0; i < moments.length; i++) {
  26. if (moments[i].previousElementSibling?.tagName === "P"
  27. && moments[i].previousElementSibling == moments[i].parentNode.firstElementChild) {
  28. moments[i].parentNode.removeChild(moments[i].parentNode.firstElementChild);
  29. }
  30. }
  31. // enlever les paragraphes vides
  32. let paragraphes = document.querySelectorAll("p");
  33. for (let i = 0; i < paragraphes.length; i++) {
  34. if (paragraphes[i].innerHTML == "") {
  35. paragraphes[i].remove();
  36. }
  37. }
  38. // images collées en bas
  39. let bottomImgs = document.querySelectorAll('.bottomimg, .tripleimgs_bottom, .tripleimgs2_bottom');
  40. for (let i = 0; i < bottomImgs.length; i++) {
  41. let page = bottomImgs[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
  42. bottomImgs[i].style.transform = 'translateY(' + (page.getBoundingClientRect().bottom - bottomImgs[i].lastElementChild.getBoundingClientRect().bottom) + 'px)';
  43. }
  44. // éléments justifiés pas nécessaires
  45. let justifiedSplitEl = document.querySelectorAll("[data-align-last-split-element='justify']");
  46. for (let i = 0; i < justifiedSplitEl.length; i++) {
  47. for (let j = 0; j < justifiedSplitEl[i].childNodes.length; j++) {
  48. let elStyle = justifiedSplitEl[i].childNodes[j].style;
  49. if (elStyle != undefined) {
  50. elStyle.textAlignLast = "left";
  51. }
  52. }
  53. }
  54. // images fullspread
  55. let fullspreadEl = document.querySelectorAll('.imgfullspreadleft, .imgfullspreadright, .imgfullspreadright_bleedtop, .imgfullspreadright_bleed');
  56. for (let i = 0; i < fullspreadEl.length; i++) {
  57. let imgSrc = fullspreadEl[i].firstElementChild.getAttribute('src');
  58. let nextPage = fullspreadEl[i].closest('.pagedjs_page').nextElementSibling.querySelectorAll('.pagedjs_page_content');
  59. let imgOverflowEl = document.createElement('div');
  60. if (fullspreadEl[i].classList.contains('imgfullspreadleft')) {
  61. imgOverflowEl.setAttribute('class', 'imgfullspreadleft-right');
  62. } else if (fullspreadEl[i].classList.contains('imgfullspreadright')) {
  63. imgOverflowEl.setAttribute('class', 'imgfullspreadright-right');
  64. } else if (fullspreadEl[i].classList.contains('imgfullspreadright_bleedtop')) {
  65. imgOverflowEl.setAttribute('class', 'imgfullspreadright_bleedtop-right');
  66. } else if (fullspreadEl[i].classList.contains('imgfullspreadright_bleed')) {
  67. imgOverflowEl.setAttribute('class', 'imgfullspreadright_bleed-right');
  68. }
  69. let imgOverflow = document.createElement('img');
  70. imgOverflow.src = imgSrc;
  71. imgOverflowEl.append(imgOverflow);
  72. nextPage[0].append(imgOverflowEl);
  73. if (fullspreadEl[i].classList.contains('imgfullspreadright')
  74. || fullspreadEl[i].classList.contains('imgfullspreadright_bleedtop')
  75. || fullspreadEl[i].classList.contains('imgfullspreadright_bleed')) {
  76. let imgMargin = imgOverflowEl.getBoundingClientRect().right - fullspreadEl[i].firstElementChild.getBoundingClientRect().right;
  77. fullspreadEl[i].firstElementChild.style.marginLeft = imgMargin + 'px';
  78. }
  79. }
  80. // fullpage img page gauche
  81. let fullPageImg = document.getElementsByClassName('fullpageimage');
  82. for (let i = 0; i < fullPageImg.length; i++) {
  83. if (fullPageImg[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  84. .classList.contains('pagedjs_left_page')) {
  85. fullPageImg[i].style.marginLeft = '-22mm';
  86. }
  87. }
  88. // encarts bleed
  89. let encarts = document.querySelectorAll('.latour, .lampe')
  90. for (let i = 0; i < encarts.length; i++) {
  91. if (encarts[i].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  92. .classList.contains('pagedjs_right_page')) {
  93. encarts[i].style.marginRight = '-22mm';
  94. encarts[i].style.paddingRight = '22mm';
  95. }
  96. if (encarts[i].hasAttribute('data-split-original')) {
  97. let plainColor = document.createElement('div');
  98. let plainHeight = encarts[i]
  99. .parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  100. .getBoundingClientRect().bottom - encarts[i].getBoundingClientRect().bottom;
  101. plainColor.setAttribute('class', 'encart-split');
  102. if (encarts[i]
  103. .parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  104. .classList.contains('pagedjs_right_page')) {
  105. plainColor.style.width = "149mm";
  106. } else {
  107. plainColor.style.width = "129mm";
  108. }
  109. plainColor.style.height = plainHeight + "px";
  110. encarts[i].parentNode.append(plainColor);
  111. } else if (encarts[i].hasAttribute('data-split-from')) {
  112. let plainColor = document.createElement('div');
  113. plainColor.setAttribute('class', 'encart-split');
  114. if (encarts[i]
  115. .parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
  116. .classList.contains('pagedjs_right_page')) {
  117. plainColor.style.width = "149mm";
  118. } else {
  119. plainColor.style.width = "129mm";
  120. }
  121. plainColor.style.height = "22mm";
  122. plainColor.style.top = "-22mm";
  123. encarts[i].parentNode.prepend(plainColor);
  124. }
  125. }
  126. }
  127. }
  128. Paged.registerHandlers(setMarginTexts);