mirror of https://github.com/sveltejs/svelte
parent
a7fe66960f
commit
8cc12b73e1
@ -0,0 +1,48 @@
|
||||
import Renderer from '../../Renderer';
|
||||
import Block from '../../Block';
|
||||
import Action from '../../../nodes/Action';
|
||||
|
||||
export default function addActions(
|
||||
block: Block,
|
||||
target: string,
|
||||
actions: Action[]
|
||||
) {
|
||||
actions.forEach(action => {
|
||||
const { expression } = action;
|
||||
let snippet, dependencies;
|
||||
if (expression) {
|
||||
snippet = expression.snippet;
|
||||
dependencies = expression.dependencies;
|
||||
|
||||
expression.declarations.forEach(declaration => {
|
||||
block.builders.init.addBlock(declaration);
|
||||
});
|
||||
}
|
||||
|
||||
const name = block.getUniqueName(
|
||||
`${action.name.replace(/[^a-zA-Z0-9_$]/g, '_')}_action`
|
||||
);
|
||||
|
||||
block.addVariable(name);
|
||||
const fn = `ctx.${action.name}`;
|
||||
|
||||
block.builders.mount.addLine(
|
||||
`${name} = ${fn}.call(null, ${target}${snippet ? `, ${snippet}` : ''}) || {};`
|
||||
);
|
||||
|
||||
if (dependencies && dependencies.size > 0) {
|
||||
let conditional = `typeof ${name}.update === 'function' && `;
|
||||
const deps = [...dependencies].map(dependency => `changed.${dependency}`).join(' || ');
|
||||
conditional += dependencies.size > 1 ? `(${deps})` : deps;
|
||||
|
||||
block.builders.update.addConditional(
|
||||
conditional,
|
||||
`${name}.update.call(null, ${snippet});`
|
||||
);
|
||||
}
|
||||
|
||||
block.builders.destroy.addLine(
|
||||
`if (${name} && typeof ${name}.destroy === 'function') ${name}.destroy();`
|
||||
);
|
||||
});
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
import Block from '../../Block';
|
||||
import EventHandler from '../../../nodes/EventHandler';
|
||||
|
||||
export default function addEventHandlers(
|
||||
block: Block,
|
||||
target: string,
|
||||
handlers: EventHandler[]
|
||||
) {
|
||||
handlers.forEach(handler => {
|
||||
const modifiers = [];
|
||||
if (handler.modifiers.has('preventDefault')) modifiers.push('event.preventDefault();');
|
||||
if (handler.modifiers.has('stopPropagation')) modifiers.push('event.stopPropagation();');
|
||||
|
||||
const opts = ['passive', 'once', 'capture'].filter(mod => handler.modifiers.has(mod));
|
||||
if (opts.length) {
|
||||
const optString = (opts.length === 1 && opts[0] === 'capture')
|
||||
? 'true'
|
||||
: `{ ${opts.map(opt => `${opt}: true`).join(', ')} }`;
|
||||
|
||||
block.builders.hydrate.addLine(
|
||||
`@addListener(${target}, "${handler.name}", ${handler.snippet}, ${optString});`
|
||||
);
|
||||
|
||||
block.builders.destroy.addLine(
|
||||
`@removeListener(${target}, "${handler.name}", ${handler.snippet}, ${optString});`
|
||||
);
|
||||
} else {
|
||||
block.builders.hydrate.addLine(
|
||||
`@addListener(${target}, "${handler.name}", ${handler.snippet});`
|
||||
);
|
||||
|
||||
block.builders.destroy.addLine(
|
||||
`@removeListener(${target}, "${handler.name}", ${handler.snippet});`
|
||||
);
|
||||
}
|
||||
|
||||
if (handler.expression) {
|
||||
handler.expression.declarations.forEach(declaration => {
|
||||
block.builders.init.addBlock(declaration);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Reference in new issue