-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathvdom-lit-html.js
63 lines (63 loc) · 2.29 KB
/
vdom-lit-html.js
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
import { createElement, updateElement, Fragment } from './vdom-my';
import { render, svg, html, noChange } from 'lit-html';
import { directive, Directive, PartType } from 'lit-html/directive.js';
import { unsafeHTML } from 'lit-html/directives/unsafe-html.js';
import app from './apprun';
function _render(element, vdom, parent) {
if (!vdom)
return;
if (typeof vdom === 'string') {
if (!element['_$litPart$'])
element.replaceChildren();
render(html `${unsafeHTML(vdom)}`, element);
}
else if (vdom['_$litType$']) {
if (!element['_$litPart$'])
element.replaceChildren();
render(vdom, element);
}
else {
updateElement(element, vdom, parent);
element['_$litPart$'] = undefined;
}
}
export class RunDirective extends Directive {
constructor(partInfo) {
super(partInfo);
// When necessary, validate part in constructor using `part.type`
if (partInfo.type !== PartType.EVENT) {
throw new Error('${run} can only be used in event handlers');
}
}
// Optional: override update to perform any direct DOM manipulation
update(part, params) {
/* Any imperative updates to DOM/parts would go here */
let { element, name } = part;
const getComponent = () => {
let component = element['_component'];
while (!component && element) {
element = element.parentElement;
component = element && element['_component'];
}
console.assert(!!component, 'Component not found.');
return component;
};
const [event, ...args] = params;
if (typeof event === 'string') {
element[`on${name}`] = e => {
const component = getComponent();
component ? component.run(event, ...args, e) : app.run(event, ...args, e);
};
}
else if (typeof event === 'function') {
element[`on${name}`] = e => getComponent().setState(event(getComponent().state, ...args, e));
}
return this.render();
}
render() {
return noChange;
}
}
const run = directive(RunDirective);
export { createElement, Fragment, html, svg, _render as render, run };
//# sourceMappingURL=vdom-lit-html.js.map