Skip to content

Commit

Permalink
use listen helper for <svelte:body> events (sveltejs#4278)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Jan 17, 2020
1 parent bda254e commit 62d5480
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
25 changes: 11 additions & 14 deletions src/compiler/compile/render_dom/wrappers/Body.ts
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import EventHandler from '../../../nodes/EventHandler';
import Wrapper from '../shared/Wrapper';
import Block from '../../Block';
import { b, x, p } from 'code-red';
import { Expression } from 'estree';

const TRUE = x`true`;
const FALSE = x`false`;
Expand Down Expand Up @@ -35,7 +36,7 @@ export default class EventHandlerWrapper {
return snippet;
}

render(block: Block, target: string) {
render(block: Block, target: string | Expression) {
let snippet = this.get_snippet(block);

if (this.node.modifiers.has('preventDefault')) snippet = x`@prevent_default(${snippet})`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import Block from '../../Block';
import EventHandler from '../Element/EventHandler';
import { Expression } from 'estree';

export default function add_event_handlers(
block: Block,
target: string,
target: string | Expression,
handlers: EventHandler[]
) {
handlers.forEach(handler => add_event_handler(block, target, handler));
}

export function add_event_handler(
block: Block,
target: string,
target: string | Expression,
handler: EventHandler
) {
handler.render(block, target);
Expand Down

0 comments on commit 62d5480

Please sign in to comment.