This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-09-02 14:31:24 +08:00
import { Group, View } from "../ui/view";
2021-09-02 11:39:51 +08:00
import { layoutConfig } from "./layoutconfig";
2021-09-02 12:03:19 +08:00
export var jsx;
(function (jsx) {
2021-09-02 14:31:24 +08:00
function addElement(group, v) {
if (v instanceof Array) {
v.forEach(e => addElement(group, e));
}
else if (v instanceof Fragment) {
v.children.forEach(e => addElement(group, e));
}
else if (v instanceof View) {
group.addChild(v);
}
else {
throw new Error(`Can only use view as child`);
}
}
2021-09-02 12:03:19 +08:00
function createElement(constructor, config, ...children) {
2021-09-01 10:47:40 +08:00
const e = new constructor();
2021-09-02 11:39:51 +08:00
e.layoutConfig = layoutConfig().fit();
2021-09-01 10:47:40 +08:00
if (config) {
2021-09-02 11:39:51 +08:00
e.apply(config);
2021-09-01 10:47:40 +08:00
}
if (children && children.length > 0) {
if (e instanceof Group) {
2021-09-02 12:03:19 +08:00
children.forEach((child) => {
2021-09-02 14:31:24 +08:00
addElement(e, child);
2021-09-02 12:03:19 +08:00
});
2021-09-01 10:47:40 +08:00
}
else {
throw new Error(`Can only add child to group view, do not support ${constructor.name}`);
}
}
return e;
2021-09-02 12:03:19 +08:00
}
jsx.createElement = createElement;
class Fragment extends Group {
}
jsx.Fragment = Fragment;
})(jsx || (jsx = {}));