From f8d6d999304999508ec4f2025f6f6815c9a7544f Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 25 Feb 2018 11:20:13 -0500 Subject: [PATCH] implement remount on individual nodes --- src/generators/nodes/Component.ts | 34 ++++---------------------- src/generators/nodes/EachBlock.ts | 5 ++++ src/generators/nodes/Element.ts | 9 +++++++ src/generators/nodes/MustacheTag.ts | 4 +++ src/generators/nodes/RawMustacheTag.ts | 4 +++ src/generators/nodes/Text.ts | 4 +++ src/generators/nodes/shared/Node.ts | 4 +++ 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/generators/nodes/Component.ts b/src/generators/nodes/Component.ts index d2cf47e22d..566b1c83a1 100644 --- a/src/generators/nodes/Component.ts +++ b/src/generators/nodes/Component.ts @@ -320,7 +320,7 @@ export default class Component extends Node { ${name} = new ${switch_vars.value}(${switch_vars.props}(state)); ${name}._fragment.c(); - ${this.children.map(child => remount(generator, child, name))} + ${this.children.map(child => child.remount(name))} ${name}._mount(${updateMountNode}, ${anchor}); ${eventHandlers.map(handler => deindent` @@ -398,6 +398,10 @@ export default class Component extends Node { `); } } + + remount(name: string) { + return `${this.var}._mount(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`; + } } function mungeAttribute(attribute: Node, block: Block): Attribute { @@ -547,32 +551,4 @@ function isComputed(node: Node) { } return false; -} - -function remount(generator: DomGenerator, node: Node, name: string) { - // TODO make this a method of the nodes - - if (node.type === 'Component') { - return `${node.var}._mount(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`; - } - - if (node.type === 'Element') { - const slot = node.attributes.find(attribute => attribute.name === 'slot'); - if (slot) { - return `@appendNode(${node.var}, ${name}._slotted.${node.getStaticAttributeValue('slot')});`; - } - - return `@appendNode(${node.var}, ${name}._slotted${generator.legacy ? `["default"]` : `.default`});`; - } - - if (node.type === 'Text' || node.type === 'MustacheTag' || node.type === 'RawMustacheTag') { - return `@appendNode(${node.var}, ${name}._slotted${generator.legacy ? `["default"]` : `.default`});`; - } - - if (node.type === 'EachBlock') { - // TODO consider keyed blocks - return `for (var #i = 0; #i < ${node.iterations}.length; #i += 1) ${node.iterations}[#i].m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`; - } - - return `${node.var}.m(${name}._slotted${generator.legacy ? `["default"]` : `.default`}, null);`; } \ No newline at end of file diff --git a/src/generators/nodes/EachBlock.ts b/src/generators/nodes/EachBlock.ts index 11a8a0c632..97671c37b0 100644 --- a/src/generators/nodes/EachBlock.ts +++ b/src/generators/nodes/EachBlock.ts @@ -594,4 +594,9 @@ export default class EachBlock extends Node { block.builders.destroy.addBlock(`@destroyEach(${iterations});`); } + + remount(name: string) { + // TODO consider keyed blocks + return `for (var #i = 0; #i < ${this.iterations}.length; #i += 1) ${this.iterations}[#i].m(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`; + } } diff --git a/src/generators/nodes/Element.ts b/src/generators/nodes/Element.ts index 13f8ccff5c..2a75f1fc36 100644 --- a/src/generators/nodes/Element.ts +++ b/src/generators/nodes/Element.ts @@ -671,6 +671,15 @@ export default class Element extends Node { isMediaNode() { return this.name === 'audio' || this.name === 'video'; } + + remount(name: string) { + const slot = this.attributes.find(attribute => attribute.name === 'slot'); + if (slot) { + return `@appendNode(${this.var}, ${name}._slotted.${this.getStaticAttributeValue('slot')});`; + } + + return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`; + } } function getRenderStatement( diff --git a/src/generators/nodes/MustacheTag.ts b/src/generators/nodes/MustacheTag.ts index 21b3b30df2..7960f06949 100644 --- a/src/generators/nodes/MustacheTag.ts +++ b/src/generators/nodes/MustacheTag.ts @@ -26,4 +26,8 @@ export default class MustacheTag extends Tag { parentNode ); } + + remount(name: string) { + return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`; + } } \ No newline at end of file diff --git a/src/generators/nodes/RawMustacheTag.ts b/src/generators/nodes/RawMustacheTag.ts index 6e9e5e664e..0d01135fa7 100644 --- a/src/generators/nodes/RawMustacheTag.ts +++ b/src/generators/nodes/RawMustacheTag.ts @@ -89,4 +89,8 @@ export default class RawMustacheTag extends Tag { addAnchorAfter(); } } + + remount(name: string) { + return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`; + } } \ No newline at end of file diff --git a/src/generators/nodes/Text.ts b/src/generators/nodes/Text.ts index b0e5a823c2..c1e401a665 100644 --- a/src/generators/nodes/Text.ts +++ b/src/generators/nodes/Text.ts @@ -58,4 +58,8 @@ export default class Text extends Node { parentNode ); } + + remount(name: string) { + return `@appendNode(${this.var}, ${name}._slotted${this.generator.legacy ? `["default"]` : `.default`});`; + } } \ No newline at end of file diff --git a/src/generators/nodes/shared/Node.ts b/src/generators/nodes/shared/Node.ts index 3461ddd6f2..1dad916983 100644 --- a/src/generators/nodes/shared/Node.ts +++ b/src/generators/nodes/shared/Node.ts @@ -161,4 +161,8 @@ export default class Node { getUpdateMountNode(anchor: string) { return this.parent.isDomNode() ? this.parent.var : `${anchor}.parentNode`; } + + remount(name: string) { + return `${this.var}.m(${name}._slotted${this.generator.legacy ? `["default"]` : `.default`}, null);`; + } } \ No newline at end of file