footnotes.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // passage des notes du texte à la fin de la partie
  2. if (debug) console.log('start footnotes');
  3. (function() {
  4. // détecter la partie des notes
  5. function findH3InAncestorsAndSiblings(element, findNext) {
  6. let currentElement = element;
  7. let siblingSelector = findNext ? 'nextElementSibling' : 'previousElementSibling';
  8. while (currentElement !== null) {
  9. if (currentElement.tagName === 'H3') {
  10. return currentElement;
  11. }
  12. currentElement = currentElement.parentElement;
  13. }
  14. currentElement = element;
  15. while (currentElement !== null) {
  16. let sibling = currentElement[siblingSelector];
  17. while (sibling !== null) {
  18. if (sibling.tagName === 'H3') {
  19. return sibling;
  20. }
  21. let descendantH3 = sibling.querySelector('h3');
  22. if (descendantH3 !== null) {
  23. return descendantH3;
  24. }
  25. sibling = sibling[siblingSelector];
  26. }
  27. currentElement = currentElement.parentElement;
  28. }
  29. return null;
  30. }
  31. // note dans le texte ou note d'image
  32. function isImgNote(note) {
  33. if (
  34. note.parentElement.classList.contains('imgsmall') ||
  35. note.parentElement.classList.contains('imgsmallsmall') ||
  36. note.parentElement.classList.contains('imgsmall_bottom') ||
  37. note.parentElement.classList.contains('fullpage2imgs') ||
  38. note.parentElement.classList.contains('fullpageimage') ||
  39. note.parentElement.classList.contains('tripleimgs') ||
  40. note.parentElement.classList.contains('tripleimgs_bottom') ||
  41. note.parentElement.classList.contains('tripleimgs2') ||
  42. note.parentElement.classList.contains('tripleimgs2_bottom') ||
  43. note.parentElement.classList.contains('bottomimg') ||
  44. note.parentElement.classList.contains('imgfullspreadleft') ||
  45. note.parentElement.classList.contains('imgfullspreadright') ||
  46. note.parentElement.classList.contains('imgfullspreadright_bleedtop') ||
  47. note.parentElement.classList.contains('imgfullspreadright_bleed') ||
  48. note.parentElement.classList.contains('doublepage_bigright') ||
  49. note.parentElement.classList.contains('doublepage_bigleft')
  50. ) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. // notes
  57. let noteNumber = 1;
  58. let prevPartie;
  59. let partie = 0;
  60. let notes = document.querySelectorAll('.note');
  61. let noteTitleNextEl;
  62. let notesContent = [];
  63. for (let note of notes) {
  64. let currentPartie = findH3InAncestorsAndSiblings(note, false);
  65. let noteContent = note.innerText;
  66. if (currentPartie === prevPartie) {
  67. noteNumber++;
  68. } else {
  69. // créer la page des notes
  70. let notePageTitle = document.createElement('h4');
  71. notePageTitle.innerText = "Notes";
  72. // insert before the next partie page
  73. findH3InAncestorsAndSiblings(note, true).parentNode.insertBefore(notePageTitle, findH3InAncestorsAndSiblings(note, true));
  74. noteNumber = 1;
  75. notesContent = [];
  76. partie++;
  77. notePageTitle.setAttribute('id', 'note_page_title_' + partie);
  78. noteTitleNextEl = document.getElementById(`note_page_title_${partie}`).nextElementSibling;
  79. let notesContentEl = document.createElement('div');
  80. notesContentEl.setAttribute('id', `notes_content_${partie}`);
  81. notesContentEl.classList.add('note_text');
  82. noteTitleNextEl.parentNode.insertBefore(notesContentEl, noteTitleNextEl);
  83. }
  84. if (!notesContent.includes(noteContent)) {
  85. notesContent.push(noteContent);
  86. let noteTextEl = document.createElement('p');
  87. noteTextEl.setAttribute('class', 'note_text');
  88. noteTextEl.innerHTML = `<p class="note_content_${ notesContent.length }"><span class="note_texte_number">` + noteNumber + '</span><span> ' + noteContent + '</span></p>';
  89. let notesContentEl = document.getElementById(`notes_content_${partie}`);
  90. notesContentEl.append(noteTextEl);
  91. } else {
  92. let notesPageEl = document.querySelector(`#notes_content_${partie}`);
  93. let noteContentEl = notesPageEl.querySelector(`.note_content_${notesContent.indexOf(noteContent) + 1}`);
  94. console.log(noteContentEl);
  95. let spanToAdd = document.createElement('span');
  96. spanToAdd.classList.add('note_texte_number');
  97. spanToAdd.innerText = `, ${ noteNumber }`;
  98. let lastSpan = noteContentEl.lastElementChild.previousElementSibling;
  99. console.log(lastSpan, spanToAdd, noteContent);
  100. lastSpan.append(spanToAdd);
  101. }
  102. prevPartie = currentPartie;
  103. if (isImgNote(note)) {
  104. note.innerText = '';
  105. note.classList.add('note_img');
  106. note.setAttribute('id', `note_img_${noteNumber}`);
  107. } else {
  108. note.innerText = noteNumber;
  109. }
  110. }
  111. })();
  112. if (debug) console.log('end footnotes');