From bfdcf936eaa493695ce63cc21fa1e301e7de8e43 Mon Sep 17 00:00:00 2001 From: Chris Reeves Date: Fri, 11 Jan 2019 01:14:00 -0500 Subject: [PATCH] gather event listeners for slot default elements and render in a condition - fixes #1977 --- src/compile/render-dom/Block.ts | 50 ++++++++++--------- src/compile/render-dom/wrappers/Slot.ts | 4 ++ .../Nested.html | 7 +++ .../_config.js | 3 ++ .../main.html | 7 +++ 5 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 test/runtime/samples/component-slot-used-with-default-event/Nested.html create mode 100644 test/runtime/samples/component-slot-used-with-default-event/_config.js create mode 100644 test/runtime/samples/component-slot-used-with-default-event/main.html diff --git a/src/compile/render-dom/Block.ts b/src/compile/render-dom/Block.ts index bee2072ef1..a7b53161a4 100644 --- a/src/compile/render-dom/Block.ts +++ b/src/compile/render-dom/Block.ts @@ -234,29 +234,7 @@ export default class Block { this.builders.mount.addLine(`${this.autofocus}.focus();`); } - if (this.event_listeners.length > 0) { - this.addVariable('#dispose'); - - if (this.event_listeners.length === 1) { - this.builders.hydrate.addLine( - `#dispose = ${this.event_listeners[0]};` - ); - - this.builders.destroy.addLine( - `#dispose();` - ) - } else { - this.builders.hydrate.addBlock(deindent` - #dispose = [ - ${this.event_listeners.join(',\n')} - ]; - `); - - this.builders.destroy.addLine( - `@run_all(#dispose);` - ); - } - } + this.renderListeners(); const properties = new CodeBuilder(); @@ -403,6 +381,32 @@ export default class Block { }); } + renderListeners(chunk: string = '') { + if (this.event_listeners.length > 0) { + this.addVariable(`#dispose${chunk}`); + + if (this.event_listeners.length === 1) { + this.builders.hydrate.addLine( + `#dispose${chunk} = ${this.event_listeners[0]};` + ); + + this.builders.destroy.addLine( + `#dispose${chunk}();` + ) + } else { + this.builders.hydrate.addBlock(deindent` + #dispose${chunk} = [ + ${this.event_listeners.join(',\n')} + ]; + `); + + this.builders.destroy.addLine( + `@run_all(#dispose${chunk});` + ); + } + } + } + toString() { const localKey = this.key && this.getUniqueName('key'); diff --git a/src/compile/render-dom/wrappers/Slot.ts b/src/compile/render-dom/wrappers/Slot.ts index 954c4c56ef..2788fc075b 100644 --- a/src/compile/render-dom/wrappers/Slot.ts +++ b/src/compile/render-dom/wrappers/Slot.ts @@ -72,7 +72,11 @@ export default class SlotWrapper extends Wrapper { block.builders.update.pushCondition(`!${content_name}`); block.builders.destroy.pushCondition(`!${content_name}`); + const listeners = block.event_listeners; + block.event_listeners = []; this.fragment.render(block, parentNode, parentNodes); + block.renderListeners(`_${content_name}`); + block.event_listeners = listeners; block.builders.create.popCondition(); block.builders.hydrate.popCondition(); diff --git a/test/runtime/samples/component-slot-used-with-default-event/Nested.html b/test/runtime/samples/component-slot-used-with-default-event/Nested.html new file mode 100644 index 0000000000..2682f7168b --- /dev/null +++ b/test/runtime/samples/component-slot-used-with-default-event/Nested.html @@ -0,0 +1,7 @@ + + +

+ +

\ No newline at end of file diff --git a/test/runtime/samples/component-slot-used-with-default-event/_config.js b/test/runtime/samples/component-slot-used-with-default-event/_config.js new file mode 100644 index 0000000000..4a174a9a05 --- /dev/null +++ b/test/runtime/samples/component-slot-used-with-default-event/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

Hello

', show: true +}; diff --git a/test/runtime/samples/component-slot-used-with-default-event/main.html b/test/runtime/samples/component-slot-used-with-default-event/main.html new file mode 100644 index 0000000000..06a1f64cae --- /dev/null +++ b/test/runtime/samples/component-slot-used-with-default-event/main.html @@ -0,0 +1,7 @@ + + + + Hello + \ No newline at end of file