-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3836 from tanhauhau/tanhauhau/dynamic-event-handler
feat dynamic event handler
- Loading branch information
Showing
18 changed files
with
368 additions
and
78 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
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
69 changes: 69 additions & 0 deletions
69
src/compiler/compile/render_dom/wrappers/Element/EventHandler.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import EventHandler from '../../../nodes/EventHandler'; | ||
import Wrapper from '../shared/Wrapper'; | ||
import Block from '../../Block'; | ||
import { b, x, p } from 'code-red'; | ||
|
||
const TRUE = x`true`; | ||
const FALSE = x`false`; | ||
|
||
export default class EventHandlerWrapper { | ||
node: EventHandler; | ||
parent: Wrapper; | ||
|
||
constructor(node: EventHandler, parent: Wrapper) { | ||
this.node = node; | ||
this.parent = parent; | ||
|
||
if (!node.expression) { | ||
this.parent.renderer.component.add_var({ | ||
name: node.handler_name.name, | ||
internal: true, | ||
referenced: true, | ||
}); | ||
|
||
this.parent.renderer.component.partly_hoisted.push(b` | ||
function ${node.handler_name.name}(event) { | ||
@bubble($$self, event); | ||
} | ||
`); | ||
} | ||
} | ||
|
||
get_snippet(block) { | ||
const snippet = this.node.expression ? this.node.expression.manipulate(block) : x`#ctx.${this.node.handler_name}`; | ||
|
||
if (this.node.reassigned) { | ||
block.maintain_context = true; | ||
return x`function () { ${snippet}.apply(this, arguments); }`; | ||
} | ||
return snippet; | ||
} | ||
|
||
render(block: Block, target: string) { | ||
let snippet = this.get_snippet(block); | ||
|
||
if (this.node.modifiers.has('preventDefault')) snippet = x`@prevent_default(${snippet})`; | ||
if (this.node.modifiers.has('stopPropagation')) snippet = x`@stop_propagation(${snippet})`; | ||
if (this.node.modifiers.has('self')) snippet = x`@self(${snippet})`; | ||
|
||
const args = []; | ||
|
||
const opts = ['passive', 'once', 'capture'].filter(mod => this.node.modifiers.has(mod)); | ||
if (opts.length) { | ||
args.push((opts.length === 1 && opts[0] === 'capture') | ||
? TRUE | ||
: x`{ ${opts.map(opt => p`${opt}: true`)} }`); | ||
} else if (block.renderer.options.dev) { | ||
args.push(FALSE); | ||
} | ||
|
||
if (block.renderer.options.dev) { | ||
args.push(this.node.modifiers.has('stopPropagation') ? TRUE : FALSE); | ||
args.push(this.node.modifiers.has('preventDefault') ? TRUE : FALSE); | ||
} | ||
|
||
block.event_listeners.push( | ||
x`@listen(${target}, "${this.node.name}", ${snippet}, ${args})` | ||
); | ||
} | ||
} |
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
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
33 changes: 2 additions & 31 deletions
33
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,10 @@ | ||
import Block from '../../Block'; | ||
import EventHandler from '../../../nodes/EventHandler'; | ||
import { x, p } from 'code-red'; | ||
|
||
const TRUE = x`true`; | ||
const FALSE = x`false`; | ||
import EventHandler from '../Element/EventHandler'; | ||
|
||
export default function add_event_handlers( | ||
block: Block, | ||
target: string, | ||
handlers: EventHandler[] | ||
) { | ||
handlers.forEach(handler => { | ||
let snippet = handler.render(block); | ||
if (handler.modifiers.has('preventDefault')) snippet = x`@prevent_default(${snippet})`; | ||
if (handler.modifiers.has('stopPropagation')) snippet = x`@stop_propagation(${snippet})`; | ||
if (handler.modifiers.has('self')) snippet = x`@self(${snippet})`; | ||
|
||
const args = []; | ||
|
||
const opts = ['passive', 'once', 'capture'].filter(mod => handler.modifiers.has(mod)); | ||
if (opts.length) { | ||
args.push((opts.length === 1 && opts[0] === 'capture') | ||
? TRUE | ||
: x`{ ${opts.map(opt => p`${opt}: true`)} }`); | ||
} else if (block.renderer.options.dev) { | ||
args.push(FALSE); | ||
} | ||
|
||
if (block.renderer.options.dev) { | ||
args.push(handler.modifiers.has('stopPropagation') ? TRUE : FALSE); | ||
args.push(handler.modifiers.has('preventDefault') ? TRUE : FALSE); | ||
} | ||
|
||
block.event_listeners.push( | ||
x`@listen(${target}, "${handler.name}", ${snippet}, ${args})` | ||
); | ||
}); | ||
handlers.forEach(handler => handler.render(block, target)); | ||
} |
Oops, something went wrong.