redo 9e9a078d on top of current master

pull/1382/head
Rich Harris 6 years ago
parent 004a7a24ed
commit d6f25a6a21

@ -2,6 +2,7 @@ import Block from './Block';
import { CompileOptions } from '../../interfaces'; import { CompileOptions } from '../../interfaces';
import Component from '../Component'; import Component from '../Component';
import FragmentWrapper from './wrappers/Fragment'; import FragmentWrapper from './wrappers/Fragment';
import CodeBuilder from '../../utils/CodeBuilder';
export default class Renderer { export default class Renderer {
component: Component; // TODO Maybe Renderer shouldn't know about Component? component: Component; // TODO Maybe Renderer shouldn't know about Component?
@ -10,7 +11,7 @@ export default class Renderer {
blocks: (Block | string)[]; blocks: (Block | string)[];
readonly: Set<string>; readonly: Set<string>;
slots: Set<string>; slots: Set<string>;
metaBindings: string[]; metaBindings: CodeBuilder;
bindingGroups: string[]; bindingGroups: string[];
block: Block; block: Block;
@ -35,7 +36,7 @@ export default class Renderer {
this.fileVar = options.dev && this.component.getUniqueName('file'); this.fileVar = options.dev && this.component.getUniqueName('file');
// initial values for e.g. window.innerWidth, if there's a <svelte:window> meta tag // initial values for e.g. window.innerWidth, if there's a <svelte:window> meta tag
this.metaBindings = []; this.metaBindings = new CodeBuilder();
this.bindingGroups = []; this.bindingGroups = [];

@ -97,14 +97,10 @@ export default class WindowWrapper extends Wrapper {
const property = properties[binding.name] || binding.name; const property = properties[binding.name] || binding.name;
if (!events[associatedEvent]) events[associatedEvent] = []; if (!events[associatedEvent]) events[associatedEvent] = [];
events[associatedEvent].push( events[associatedEvent].push({
`${binding.value.node.name}: this.${property}` name: binding.value.node.name,
); value: property
});
// add initial value
renderer.metaBindings.push(
`this._state.${binding.value.node.name} = window.${property};`
);
}); });
const lock = block.getUniqueName(`window_updating`); const lock = block.getUniqueName(`window_updating`);
@ -113,13 +109,35 @@ export default class WindowWrapper extends Wrapper {
Object.keys(events).forEach(event => { Object.keys(events).forEach(event => {
const handlerName = block.getUniqueName(`onwindow${event}`); const handlerName = block.getUniqueName(`onwindow${event}`);
const props = events[event].join(',\n'); const props = events[event];
if (event === 'scroll') { if (event === 'scroll') {
// TODO other bidirectional bindings... // TODO other bidirectional bindings...
block.addVariable(lock, 'false'); block.addVariable(lock, 'false');
block.addVariable(clear, `function() { ${lock} = false; }`); block.addVariable(clear, `function() { ${lock} = false; }`);
block.addVariable(timeout); block.addVariable(timeout);
const condition = [
bindings.scrollX && `"${bindings.scrollX}" in this._state`,
bindings.scrollY && `"${bindings.scrollY}" in this._state`
].filter(Boolean).join(' || ');
const x = bindings.scrollX && `this._state.${bindings.scrollX}`;
const y = bindings.scrollY && `this._state.${bindings.scrollY}`;
renderer.metaBindings.addBlock(deindent`
if (${condition}) {
window.scrollTo(${x || 'window.pageXOffset'}, ${y || 'window.pageYOffset'});
}
${x && `${x} = window.pageXOffset;`}
${y && `${y} = window.pageYOffset;`}
`);
} else {
props.forEach(prop => {
renderer.metaBindings.addLine(
`this._state.${prop.name} = window.${prop.value};`
);
});
} }
const handlerBody = deindent` const handlerBody = deindent`
@ -130,7 +148,7 @@ export default class WindowWrapper extends Wrapper {
${component.options.dev && `component._updatingReadonlyProperty = true;`} ${component.options.dev && `component._updatingReadonlyProperty = true;`}
#component.set({ #component.set({
${props} ${props.map(prop => `${prop.name}: this.${prop.value}`)}
}); });
${component.options.dev && `component._updatingReadonlyProperty = false;`} ${component.options.dev && `component._updatingReadonlyProperty = false;`}
@ -185,7 +203,7 @@ export default class WindowWrapper extends Wrapper {
`); `);
// add initial value // add initial value
renderer.metaBindings.push( renderer.metaBindings.addLine(
`this._state.${bindings.online} = navigator.onLine;` `this._state.${bindings.online} = navigator.onLine;`
); );

@ -59,7 +59,6 @@ function SvelteComponent(options) {
if ("y" in this._state) { if ("y" in this._state) {
window.scrollTo(window.pageXOffset, this._state.y); window.scrollTo(window.pageXOffset, this._state.y);
} }
this._state.y = window.pageYOffset; this._state.y = window.pageYOffset;
this._intro = true; this._intro = true;

Loading…
Cancel
Save