-
-
Notifications
You must be signed in to change notification settings - Fork 477
/
html.ts
197 lines (165 loc) · 5.4 KB
/
html.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {ModelEvent} from "@bokehjs/core/bokeh_events"
import type * as p from "@bokehjs/core/properties"
import type {Attrs, Dict} from "@bokehjs/core/types"
import {entries} from "@bokehjs/core/util/object"
import {Markup} from "@bokehjs/models/widgets/markup"
import {PanelMarkupView} from "./layout"
import {serializeEvent} from "./event-to-object"
export class DOMEvent extends ModelEvent {
constructor(readonly node: string, readonly data: unknown) {
super()
}
protected override get event_values(): Attrs {
return {model: this.origin, node: this.node, data: this.data}
}
static {
this.prototype.event_name = "dom_event"
}
}
export function html_decode(input: string): string | null {
const doc = new DOMParser().parseFromString(input, "text/html")
return doc.documentElement.textContent
}
export function run_scripts(node: Element): void {
for (const old_script of node.querySelectorAll("script")) {
const new_script = document.createElement("script")
for (const attr of old_script.attributes) {
new_script.setAttribute(attr.name, attr.value)
}
new_script.append(document.createTextNode(old_script.innerHTML))
const parent_node = old_script.parentNode
if (parent_node != null) {
parent_node.replaceChild(new_script, old_script)
}
}
}
export class HTMLView extends PanelMarkupView {
declare model: HTML
protected readonly _event_listeners: Map<string, Map<string, (event: Event) => void>> = new Map()
override connect_signals(): void {
super.connect_signals()
const {text, visible, events} = this.model.properties
this.on_change(text, () => {
const html = this.process_tex()
this.set_html(html)
})
this.on_change(visible, () => {
if (this.model.visible) {
this.container.style.visibility = "visible"
}
})
this.on_change(events, () => {
this._remove_event_listeners()
this._setup_event_listeners()
})
}
protected rerender() {
this.render()
this.invalidate_layout()
}
set_html(html: string | null): void {
if (html !== null) {
this.container.innerHTML = html
if (this.model.run_scripts) {
run_scripts(this.container)
}
this._setup_event_listeners()
}
}
override render(): void {
super.render()
this.container.style.visibility = "hidden"
this.shadow_el.appendChild(this.container)
if (this.provider.status == "failed" || this.provider.status == "loaded") {
this._has_finished = true
}
const html = this.process_tex()
this.watch_stylesheets()
this.set_html(html)
}
override style_redraw(): void {
if (this.model.visible) {
this.container.style.visibility = "visible"
}
}
override process_tex(): string {
const decoded = html_decode(this.model.text)
const text = decoded ?? this.model.text
if (this.model.disable_math || !this.contains_tex(text)) {
return text
}
const tex_parts = this.provider.MathJax.find_tex(text)
const processed_text: string[] = []
let last_index: number | undefined = 0
for (const part of tex_parts) {
processed_text.push(text.slice(last_index, part.start.n))
processed_text.push(this.provider.MathJax.tex2svg(part.math, {display: part.display}).outerHTML)
last_index = part.end.n
}
if (last_index! < text.length) {
processed_text.push(text.slice(last_index))
}
return processed_text.join("")
}
private contains_tex(html: string): boolean {
if (!this.provider.MathJax) {
return false
}
return this.provider.MathJax.find_tex(html).length > 0
}
private _remove_event_listeners(): void {
for (const [node, callbacks] of this._event_listeners) {
const el = document.getElementById(node)
if (el == null) {
console.warn(`DOM node '${node}' could not be found. Cannot subscribe to DOM events.`)
continue
}
for (const [event_name, event_callback] of callbacks) {
el.removeEventListener(event_name, event_callback)
}
}
this._event_listeners.clear()
}
private _setup_event_listeners(): void {
for (const [node, event_names] of entries(this.model.events)) {
const el = document.getElementById(node)
if (el == null) {
console.warn(`DOM node '${node}' could not be found. Cannot subscribe to DOM events.`)
continue
}
for (const event_name of event_names) {
const callback = (event: Event) => {
this.model.trigger_event(new DOMEvent(node, serializeEvent(event)))
}
el.addEventListener(event_name, callback)
let callbacks = this._event_listeners.get(node)
if (callbacks === undefined) {
this._event_listeners.set(node, callbacks = new Map())
}
callbacks.set(event_name, callback)
}
}
}
}
export namespace HTML {
export type Attrs = p.AttrsOf<Props>
export type Props = Markup.Props & {
events: p.Property<Dict<string[]>>
run_scripts: p.Property<boolean>
}
}
export interface HTML extends HTML.Attrs {}
export class HTML extends Markup {
declare properties: HTML.Props
constructor(attrs?: Partial<HTML.Attrs>) {
super(attrs)
}
static override __module__ = "panel.models.markup"
static {
this.prototype.default_view = HTMLView
this.define<HTML.Props>(({Bool, Str, List, Dict}) => ({
events: [ Dict(List(Str)), {} ],
run_scripts: [ Bool, true ],
}))
}
}