Skip to content

Commit 9e9a078

Browse files
committed
set window scroll from bindings on initialisation - fixes #938
1 parent dbab1a8 commit 9e9a078

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

Diff for: src/compile/dom/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Ast, CompileOptions, Node } from '../../interfaces';
1818
export class DomTarget {
1919
blocks: (Block|string)[];
2020
readonly: Set<string>;
21-
metaBindings: string[];
21+
metaBindings: CodeBuilder;
2222

2323
hasIntroTransitions: boolean;
2424
hasOutroTransitions: boolean;
@@ -29,7 +29,7 @@ export class DomTarget {
2929
this.readonly = new Set();
3030

3131
// initial values for e.g. window.innerWidth, if there's a <svelte:window> meta tag
32-
this.metaBindings = [];
32+
this.metaBindings = new CodeBuilder();
3333
}
3434
}
3535

Diff for: src/compile/nodes/Window.ts

+30-10
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,10 @@ export default class Window extends Node {
121121
const property = properties[binding.name] || binding.name;
122122

123123
if (!events[associatedEvent]) events[associatedEvent] = [];
124-
events[associatedEvent].push(
125-
`${binding.value.node.name}: this.${property}`
126-
);
127-
128-
// add initial value
129-
compiler.target.metaBindings.push(
130-
`this._state.${binding.value.node.name} = window.${property};`
131-
);
124+
events[associatedEvent].push({
125+
name: binding.value.node.name,
126+
value: property
127+
});
132128
});
133129

134130
const lock = block.getUniqueName(`window_updating`);
@@ -137,13 +133,37 @@ export default class Window extends Node {
137133

138134
Object.keys(events).forEach(event => {
139135
const handlerName = block.getUniqueName(`onwindow${event}`);
140-
const props = events[event].join(',\n');
136+
const props = events[event];
141137

142138
if (event === 'scroll') {
143139
// TODO other bidirectional bindings...
144140
block.addVariable(lock, 'false');
145141
block.addVariable(clear, `function() { ${lock} = false; }`);
146142
block.addVariable(timeout);
143+
144+
const condition = [
145+
bindings.scrollX && `"${bindings.scrollX}" in this._state`,
146+
bindings.scrollY && `"${bindings.scrollY}" in this._state`
147+
].filter(Boolean).join(' || ');
148+
149+
const x = bindings.scrollX && `this._state.${bindings.scrollX}`;
150+
const y = bindings.scrollY && `this._state.${bindings.scrollY}`;
151+
152+
compiler.target.metaBindings.addBlock(deindent`
153+
if (${condition}) {
154+
window.scrollTo(${x || 'window.pageXOffset'}, ${y || 'window.pageYOffset'});
155+
}
156+
157+
${x && `${x} = window.pageXOffset;`}
158+
159+
${y && `${y} = window.pageYOffset;`}
160+
`);
161+
} else {
162+
props.forEach(prop => {
163+
compiler.target.metaBindings.addLine(
164+
`this._state.${prop.name} = window.${prop.value};`
165+
);
166+
});
147167
}
148168

149169
const handlerBody = deindent`
@@ -154,7 +174,7 @@ export default class Window extends Node {
154174
${compiler.options.dev && `component._updatingReadonlyProperty = true;`}
155175
156176
#component.set({
157-
${props}
177+
${props.map(prop => `${prop.name}: this.${prop.value}`)}
158178
});
159179
160180
${compiler.options.dev && `component._updatingReadonlyProperty = false;`}

Diff for: test/js/samples/computed-collapsed-if/expected-bundle.js

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ function create_main_fragment(component, ctx) {
149149
function SvelteComponent(options) {
150150
init(this, options);
151151
this._state = assign({}, options.data);
152+
152153
this._recompute({ x: 1 }, this._state);
153154

154155
this._fragment = create_main_fragment(this, this._state);

Diff for: test/js/samples/computed-collapsed-if/expected.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ function create_main_fragment(component, ctx) {
2727
function SvelteComponent(options) {
2828
init(this, options);
2929
this._state = assign({}, options.data);
30+
3031
this._recompute({ x: 1 }, this._state);
3132

3233
this._fragment = create_main_fragment(this, this._state);

Diff for: test/js/samples/dev-warning-missing-data-computed/expected-bundle.js

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ function SvelteComponent(options) {
206206
if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option");
207207
init(this, options);
208208
this._state = assign({ Math : Math }, options.data);
209+
209210
this._recompute({ foo: 1 }, this._state);
210211
if (!('foo' in this._state)) console.warn("<SvelteComponent> was created without expected data property 'foo'");
211212

Diff for: test/js/samples/dev-warning-missing-data-computed/expected.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function SvelteComponent(options) {
4646
if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option");
4747
init(this, options);
4848
this._state = assign({ Math : Math }, options.data);
49+
4950
this._recompute({ foo: 1 }, this._state);
5051
if (!('foo' in this._state)) console.warn("<SvelteComponent> was created without expected data property 'foo'");
5152

Diff for: test/js/samples/window-binding-scroll/expected-bundle.js

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ function create_main_fragment(component, ctx) {
198198
function SvelteComponent(options) {
199199
init(this, options);
200200
this._state = assign({}, options.data);
201+
if ("y" in this._state) {
202+
window.scrollTo(window.pageXOffset, this._state.y);
203+
}
204+
201205
this._state.y = window.pageYOffset;
202206

203207
this._fragment = create_main_fragment(this, this._state);

Diff for: test/js/samples/window-binding-scroll/expected.js

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function create_main_fragment(component, ctx) {
5656
function SvelteComponent(options) {
5757
init(this, options);
5858
this._state = assign({}, options.data);
59+
if ("y" in this._state) {
60+
window.scrollTo(window.pageXOffset, this._state.y);
61+
}
62+
5963
this._state.y = window.pageYOffset;
6064

6165
this._fragment = create_main_fragment(this, this._state);

0 commit comments

Comments
 (0)