-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
browser.ts
59 lines (47 loc) · 1.43 KB
/
browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { applyCustomElements } from './custom-elements/browser'
import { observer } from './observer'
/**
* @internal
* @deprecated
*/
const exportedObserver = observer
export { exportedObserver as observer }
const marpCoreBrowserScript = Symbol()
export interface MarpCoreBrowser {
/** Clean-up observer for applying polyfills. */
(): void
/** Clean-up observer for applying polyfills. */
cleanup: () => void
/**
* Update DOMs to apply custom elements for Marp Core. It should call whenever
* rendered DOMs rendered by Marp.
*
* It has exactly same meaning to call `browser()` with the same arguments
* again.
*/
update: () => MarpCoreBrowser
}
export const browser = (target: ParentNode = document): MarpCoreBrowser => {
if (typeof window === 'undefined') {
throw new Error(
"Marp Core's browser script is valid only in browser context.",
)
}
applyCustomElements(target) // Should call in every update of Marp rendering
if (target[marpCoreBrowserScript]) return target[marpCoreBrowserScript]
const cleanupObserver = observer({ target })
const cleanupFn = () => {
cleanupObserver()
delete target[marpCoreBrowserScript]
}
const cleanup = Object.assign(cleanupFn, {
cleanup: cleanupFn,
update: () => browser(target),
})
Object.defineProperty(target, marpCoreBrowserScript, {
configurable: true,
value: cleanup,
})
return cleanup
}
export default browser