-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathmarp-custom-element.ts
47 lines (35 loc) · 1.24 KB
/
marp-custom-element.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
type Constructor<T = {}> = new (...args: any[]) => T // eslint-disable-line @typescript-eslint/ban-types
export const createMarpCustomElement = <T extends Constructor<HTMLElement>>(
Base: T,
{ attrs = {}, style }: { attrs?: Record<string, string>; style?: string },
) =>
class MarpCustomElement extends Base {
shadowRoot!: ShadowRoot
constructor(...args: any[]) {
super(...args)
for (const [key, value] of Object.entries(attrs)) {
if (!this.hasAttribute(key)) this.setAttribute(key, value)
}
this.attachShadow({ mode: 'open' })
}
static get observedAttributes() {
return ['data-auto-scaling']
}
connectedCallback() {
this._update()
}
attributeChangedCallback() {
this._update()
}
_update() {
const styleTag = style ? `<style>:host { ${style} }</style>` : ''
let slotTag = '<slot></slot>'
const { autoScaling } = this.dataset
if (autoScaling !== undefined) {
const downscale =
autoScaling === 'downscale-only' ? 'data-downscale-only' : ''
slotTag = `<marp-auto-scaling exportparts="svg:auto-scaling" ${downscale}>${slotTag}</marp-auto-scaling>`
}
this.shadowRoot.innerHTML = styleTag + slotTag
}
}