Skip to content

Commit 5108948

Browse files
committed
Fix Blink, allow strings to be rendered as children of nodes/scopes.
1 parent 5205045 commit 5108948

File tree

12 files changed

+139
-73
lines changed

12 files changed

+139
-73
lines changed

packages/webio/dist/blink.bundle.js

Lines changed: 47 additions & 47 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/dist/blink.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/dist/mux.bundle.js

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/dist/mux.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/webio/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"@babel/core": "^7.1.2",
1313
"@babel/polyfill": "^7.0.0",
14+
"@babel/runtime": "^7.1.2",
1415
"@types/debug": "0.0.30",
1516
"debug": "^4.0.1"
1617
},

packages/webio/src/DomNode.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export interface DomNodeData extends WebIONodeDataBase {
8080

8181
class WebIODomNode extends WebIONode {
8282
readonly element: WebIODomElement;
83-
children: Array<WebIOScope | WebIODomNode>;
83+
children: Array<WebIOScope | WebIODomNode | string>;
8484
private eventListeners: {[eventType: string]: EventListenerOrEventListenerObject | undefined} = {};
8585

8686
private static createElement(data: DomNodeData) {
@@ -102,11 +102,18 @@ class WebIODomNode extends WebIONode {
102102
this.applyProps(nodeData.props);
103103

104104
// Create children and append to this node's element.
105-
this.children = nodeData.children.map((nodeData) => (
106-
createNode(nodeData, {webIO: this.webIO, scope: this.scope})
107-
));
105+
this.children = nodeData.children.map((nodeData) => {
106+
if (typeof nodeData === "string") {
107+
return nodeData;
108+
}
109+
return createNode(nodeData, {webIO: this.webIO, scope: this.scope})
110+
});
108111
for (const child of this.children) {
109-
this.element.appendChild(child.element);
112+
if (typeof child === "string") {
113+
this.element.appendChild(document.createTextNode(child));
114+
} else {
115+
this.element.appendChild(child.element);
116+
}
110117
}
111118
}
112119

packages/webio/src/Node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const enum WebIONodeType {
3636
export interface WebIONodeDataBase {
3737
type: "node";
3838
nodeType: WebIONodeType;
39-
children: WebIONodeData[];
39+
children: Array<WebIONodeData | string>;
4040
}
4141

4242
export interface WebIONodeParams {
@@ -53,7 +53,7 @@ export interface WebIONodeParams {
5353
*/
5454
abstract class WebIONode {
5555
abstract readonly element: WebIODomElement;
56-
abstract children: Array<WebIOScope | WebIODomNode>;
56+
abstract children: Array<WebIOScope | WebIODomNode | string>;
5757
readonly scope?: WebIOScope;
5858
readonly webIO: WebIO;
5959

packages/webio/src/Scope.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class WebIOScope extends WebIONode {
7777

7878
readonly id: string;
7979
readonly element: HTMLDivElement;
80-
children: Array<WebIOScope | WebIODomNode>;
80+
children: Array<WebIOScope | WebIODomNode | string>;
8181
handlers: ScopeListeners;
8282
observables: {[observableName: string]: WebIOObservable};
8383
promises: ScopePromises;
@@ -130,11 +130,18 @@ class WebIOScope extends WebIONode {
130130
// this.promises = {importsLoaded, connected};
131131

132132
// Create children and append to this node's element.
133-
this.children = scopeData.children.map((nodeData) => (
134-
createNode(nodeData, {webIO: this.webIO, scope: this})
135-
));
133+
this.children = scopeData.children.map((nodeData) => {
134+
if (typeof nodeData === "string") {
135+
return nodeData;
136+
}
137+
return createNode(nodeData, {webIO: this.webIO, scope: this})
138+
});
136139
for (const child of this.children) {
137-
this.element.appendChild(child.element);
140+
if (typeof child === "string") {
141+
this.element.appendChild(document.createTextNode(child));
142+
} else {
143+
this.element.appendChild(child.element);
144+
}
138145
}
139146

140147
this.setupScope();

packages/webio/src/WebIO.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,15 @@ class WebIO {
101101
* @param nodeData - The data associated with the WebIO node.
102102
*/
103103
mount(element: WebIODomElement, nodeData: WebIONodeData) {
104+
if (!element) {
105+
console.error("WebIO cannot mount node into element.", {element, nodeData});
106+
throw new Error(`WebIO cannot mount node into element.`);
107+
}
104108
const node = createNode(nodeData, {webIO: this});
105109
if (!element.parentElement) {
106110
throw new Error("Cannot mount WebIO node into HTMLElement that isn't mounted in DOM.")
107111
}
108-
element.parentElement!.replaceChild(node.element, element);
112+
element.parentElement.replaceChild(node.element, element);
109113
}
110114

111115
}

0 commit comments

Comments
 (0)