Transmit actual Nodes across Syndicate bus, rather than HTML string

This commit is contained in:
Tony Garnock-Jones 2021-01-29 15:40:56 +01:00
parent b0fe7baf25
commit b5a4a150ac
1 changed files with 6 additions and 15 deletions

View File

@ -129,7 +129,7 @@ function spawnUIFragmentFactory<T>(thisFacet: Facet<T>) {
assert P.UIFragmentVersion(fragmentId, this.version) when (this.version > 0);
let selector: string;
let html: string;
let html: Array<ChildNode>;
let orderBy: NodeOrderKey;
let anchorNodes: Array<Element> = [];
let eventRegistrations =
@ -142,13 +142,13 @@ function spawnUIFragmentFactory<T>(thisFacet: Facet<T>) {
on stop updateEventListeners([ selector, eventType ], false);
}
on asserted P.UIFragment(fragmentId, $newSelector: string, $newHtml: string, $newOrderBy) => {
on asserted P.UIFragment(fragmentId, $newSelector: string, $newHtml, $newOrderBy) => {
if (!isNodeOrderKey(newOrderBy)) return;
removeNodes();
selector = newSelector;
html = newHtml;
html = newHtml as Array<ChildNode>;
orderBy = newOrderBy;
anchorNodes = (selector !== null) ? selectorMatch(document.body, selector) : [];
@ -158,7 +158,7 @@ function spawnUIFragmentFactory<T>(thisFacet: Facet<T>) {
anchorNodes.forEach(anchorNode => {
let insertionPoint = findInsertionPoint(anchorNode, orderBy, fragmentId);
htmlToNodes(anchorNode, html).forEach(newNode => {
html.forEach(newNode => {
setSortKey(newNode, orderBy, fragmentId);
anchorNode.insertBefore(newNode, insertionPoint);
configureNode(newNode);
@ -307,12 +307,6 @@ function findInsertionPoint(n: Node, orderBy: NodeOrderKey, fragmentId: Fragment
}
}
function htmlToNodes(parent: Element, html: string): Array<ChildNode> {
let e = parent.cloneNode(false) as Element;
e.innerHTML = html;
return Array.from(e.childNodes);
}
function configureNode(n: ChildNode) {
// Runs post-insertion configuration of nodes.
// TODO: review this design.
@ -486,11 +480,8 @@ export class Anchor {
return new Anchor({ fragmentId: this.fragmentId + '__' + extn });
}
html(selector: string, html: HtmlFragments | string, orderBy: NodeOrderKey = ''): Record {
return P.UIFragment(this.fragmentId,
selector,
typeof html === 'string' ? html : html.toString(),
orderBy);
html(selector: string, html: HtmlFragments, orderBy: NodeOrderKey = ''): Record {
return P.UIFragment(this.fragmentId, selector, html.nodes(), orderBy);
}
}