wowchemy-slides.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. global RevealMarkdown, RevealHighlight, RevealSearch, RevealNotes, RevealMath, RevealZoom, Reveal, mermaid, RevealMenu
  3. */
  4. import * as params from '@params';
  5. import {fixMermaid} from './wowchemy-utils';
  6. // Enable core slide features.
  7. var enabledPlugins = [RevealMarkdown, RevealHighlight, RevealSearch, RevealNotes, RevealMath.MathJax3, RevealZoom];
  8. const isObject = function (o) {
  9. return o === Object(o) && !isArray(o) && typeof o !== 'function';
  10. };
  11. const isArray = function (a) {
  12. return Array.isArray(a);
  13. };
  14. const toCamelCase = function (s) {
  15. return s.replace(/([-_][a-z])/gi, function (term) {
  16. return term.toUpperCase().replace('-', '').replace('_', '');
  17. });
  18. };
  19. const keysToCamelCase = function (o) {
  20. if (isObject(o)) {
  21. const n = {};
  22. Object.keys(o).forEach(function (k) {
  23. n[toCamelCase(k)] = keysToCamelCase(o[k]);
  24. });
  25. return n;
  26. } else if (isArray(o)) {
  27. return o.map(function (i) {
  28. return keysToCamelCase(i);
  29. });
  30. }
  31. return o;
  32. };
  33. // reveal configurations can be included in front matter under slides.reveal
  34. var pluginOptions = {};
  35. if (typeof params.slides.reveal_options !== 'undefined') {
  36. pluginOptions = params.slides.reveal_options;
  37. }
  38. pluginOptions = keysToCamelCase(pluginOptions);
  39. //enable menu by default if not set
  40. if (typeof pluginOptions.menu_enabled === 'undefined') {
  41. pluginOptions.menu_enabled = true;
  42. }
  43. // configure menu if enabled
  44. if (pluginOptions.menu_enabled) {
  45. enabledPlugins.push(RevealMenu);
  46. }
  47. pluginOptions['plugins'] = enabledPlugins;
  48. Reveal.initialize(pluginOptions);
  49. // The following functions are used to render Mermaid diagrams
  50. // after Reveal slides have been successfully loaded
  51. // since content of slides is lazy loaded, if diagrams are
  52. // rendered at start of presentation their sizes will be off
  53. // get all slides that are:
  54. // 1- data loaded
  55. // 2- display set to block
  56. // 3- has a mermaid element that is not processed (data-processed dne)
  57. function mermaidSlidesReadyToRender(mslide) {
  58. var diag = mslide.querySelector('.mermaid');
  59. if (diag) {
  60. var background = mslide.slideBackgroundElement;
  61. // render if we are 1 slide away horizontally
  62. // current visible slide index
  63. var currentHorizontalIndex = Reveal.getState()['indexh'];
  64. // mermaid slide index
  65. var diagramSlideIndex = Reveal.getIndices(mslide)['h'];
  66. if (
  67. // find slides with non-rendered mermaid tags
  68. // these will not have the attribute data-processed
  69. !diag.hasAttribute('data-processed') &&
  70. // check also that reveal slide is already loaded
  71. // reveal slides seem to be lazily loaded
  72. // things could be easier if reveal had a slide-loaded event
  73. background.hasAttribute('data-loaded') &&
  74. // loaded slides must also have the display attribute set to block
  75. background.style.display === 'block' &&
  76. // render diagrams that are 1 slide away
  77. diagramSlideIndex - currentHorizontalIndex <= 1
  78. )
  79. return mslide;
  80. }
  81. return null;
  82. }
  83. function renderMermaidSlides() {
  84. // find all slides with diagrams that are ready to render
  85. var diagramSlides = Reveal.getSlides().filter(mermaidSlidesReadyToRender);
  86. // render the diagram for each slide with ready to render diagrams
  87. diagramSlides.forEach(function (item) {
  88. mermaid.init(item.querySelector('.mermaid'));
  89. });
  90. }
  91. // render mermaid slides for slides that are ready
  92. Reveal.on('slidechanged', function () {
  93. renderMermaidSlides();
  94. });
  95. // render mermaid slides for slides that are ready on startup
  96. Reveal.on('Ready', function () {
  97. if (Reveal.isReady()) {
  98. renderMermaidSlides();
  99. }
  100. });
  101. // Disable Mermaid by default.
  102. if (typeof params.slides.diagram === 'undefined') {
  103. params.slides.diagram = false;
  104. }
  105. // Configure Mermaid only if diagrams are enabled.
  106. if (params.slides.diagram) {
  107. //mermaid options
  108. // mermaid: front matter configuration can be used to set mermaid options
  109. // You can also use directives (see mermaid documentation)
  110. var mermaidOptions = {};
  111. if (typeof params.slides.diagram_options !== 'undefined') {
  112. mermaidOptions = params.slides.diagram_options;
  113. }
  114. // `startOnLoad` must be false since diagrams are lazily rendered.
  115. mermaidOptions['startOnLoad'] = false;
  116. mermaid.initialize(mermaidOptions);
  117. // Fix Mermaid conflict with Hightlight JS.
  118. document.addEventListener('DOMContentLoaded', function () {
  119. fixMermaid(false);
  120. });
  121. }