| 1 | /// <reference path="./reveal.d.ts" /> |
| 2 | |
| 3 | import { RevealConfig } from './config.ts'; |
| 4 | import type { RevealApi } from './reveal'; |
| 5 | |
| 6 | // @ts-ignore |
| 7 | import Deck, { VERSION } from './reveal.js'; |
| 8 | |
| 9 | /** |
| 10 | * Expose the Reveal class to the window. To create a |
| 11 | * new instance: |
| 12 | * let deck = new Reveal( document.querySelector( '.reveal' ), { |
| 13 | * controls: false |
| 14 | * } ); |
| 15 | * deck.initialize().then(() => { |
| 16 | * // reveal.js is ready |
| 17 | * }); |
| 18 | */ |
| 19 | const Reveal: { |
| 20 | initialize: (options?: RevealConfig) => Promise<RevealApi>; |
| 21 | [key: string]: any; |
| 22 | } = Deck; |
| 23 | |
| 24 | /** |
| 25 | * The below is a thin shell that mimics the pre 4.0 |
| 26 | * reveal.js API and ensures backwards compatibility. |
| 27 | * This API only allows for one Reveal instance per |
| 28 | * page, whereas the new API above lets you run many |
| 29 | * presentations on the same page. |
| 30 | * |
| 31 | * Reveal.initialize( { controls: false } ).then(() => { |
| 32 | * // reveal.js is ready |
| 33 | * }); |
| 34 | */ |
| 35 | |
| 36 | type RevealApiFunction = (deck: RevealApi) => any; |
| 37 | |
| 38 | const enqueuedAPICalls: RevealApiFunction[] = []; |
| 39 | |
| 40 | Reveal.initialize = (options?: RevealConfig) => { |
| 41 | const revealElement = document.querySelector('.reveal'); |
| 42 | |
| 43 | if (!(revealElement instanceof HTMLElement)) { |
| 44 | throw new Error('Unable to find presentation root (<div class="reveal">).'); |
| 45 | } |
| 46 | |
| 47 | // Create our singleton reveal.js instance |
| 48 | Object.assign(Reveal, new Deck(revealElement, options)); |
| 49 | |
| 50 | // Invoke any enqueued API calls |
| 51 | enqueuedAPICalls.map((method) => method(Reveal as RevealApi)); |
| 52 | |
| 53 | return Reveal.initialize(); |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * The pre 4.0 API let you add event listener before |
| 58 | * initializing. We maintain the same behavior by |
| 59 | * queuing up premature API calls and invoking all |
| 60 | * of them when Reveal.initialize is called. |
| 61 | */ |
| 62 | ( |
| 63 | ['configure', 'on', 'off', 'addEventListener', 'removeEventListener', 'registerPlugin'] as const |
| 64 | ).forEach((method) => { |
| 65 | Reveal[method] = (...args: any) => { |
| 66 | enqueuedAPICalls.push((deck) => (deck[method] as any).call(null, ...args)); |
| 67 | }; |
| 68 | }); |
| 69 | |
| 70 | Reveal.isReady = () => false; |
| 71 | |
| 72 | Reveal.VERSION = VERSION; |
| 73 | |
| 74 | export default Reveal; |
| 75 |