forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply event modifiers to <svelte:body> events (sveltejs#4279)
- Loading branch information
1 parent
c0c47be
commit a7471cf
Showing
6 changed files
with
33 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
import Block from '../Block'; | ||
import Wrapper from './shared/Wrapper'; | ||
import { b } from 'code-red'; | ||
import { x } from 'code-red'; | ||
import Body from '../../nodes/Body'; | ||
import { Identifier } from 'estree'; | ||
import EventHandler from './Element/EventHandler'; | ||
import add_event_handlers from './shared/add_event_handlers'; | ||
import { TemplateNode } from '../../../interfaces'; | ||
import Renderer from '../Renderer'; | ||
|
||
export default class BodyWrapper extends Wrapper { | ||
node: Body; | ||
handlers: EventHandler[]; | ||
|
||
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) { | ||
this.node.handlers | ||
.map(handler => new EventHandler(handler, this)) | ||
.forEach(handler => { | ||
const snippet = handler.get_snippet(block); | ||
|
||
block.chunks.init.push(b` | ||
@_document.body.addEventListener("${handler.node.name}", ${snippet}); | ||
`); | ||
constructor(renderer: Renderer, block: Block, parent: Wrapper, node: TemplateNode) { | ||
super(renderer, block, parent, node); | ||
this.handlers = this.node.handlers.map(handler => new EventHandler(handler, this)); | ||
} | ||
|
||
block.chunks.destroy.push(b` | ||
@_document.body.removeEventListener("${handler.node.name}", ${snippet}); | ||
`); | ||
}); | ||
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) { | ||
add_event_handlers(block, x`@_document.body`, this.handlers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
src/compiler/compile/render_dom/wrappers/shared/add_event_handlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/runtime/samples/event-handler-modifier-body-once/_config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default { | ||
async test({ assert, component, window }) { | ||
const event = new window.MouseEvent('click'); | ||
|
||
await window.document.body.dispatchEvent(event); | ||
assert.equal(component.count, 1); | ||
|
||
await window.document.body.dispatchEvent(event); | ||
assert.equal(component.count, 1); | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
5
test/runtime/samples/event-handler-modifier-body-once/main.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
export let count = 0; | ||
</script> | ||
|
||
<svelte:body on:click|once="{() => count += 1}"/> |