Skip to content

Commit

Permalink
Merge pull request #1 from sveltejs/zxbodya-ts-resolved
Browse files Browse the repository at this point in the history
Resolve merge conflicts
  • Loading branch information
zxbodya authored May 26, 2019
2 parents 33f827c + 285ed4f commit 821133f
Show file tree
Hide file tree
Showing 23 changed files with 205 additions and 20 deletions.
8 changes: 3 additions & 5 deletions site/content/examples/19-7guis/05-7guis-crud/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@

$: selected = filteredPeople[i];

$: {
first = selected ? selected.first : '';
last = selected ? selected.last : '';
}
$: reset_inputs(selected);

function create() {
people = people.concat({ first, last });
Expand All @@ -53,7 +50,8 @@
}

function reset_inputs(person) {
({ first, last } = person);
first = person ? person.first : '';
last = person ? person.last : '';
}
</script>

Expand Down
3 changes: 1 addition & 2 deletions site/content/tutorial/12-actions/01-actions/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { pannable } from './pannable.js';
></div>
```

Open the `pannable.js` file. Like transition functions, an action function receives a `node` and some optional parameters, and returns an action object. That object must have a `destroy` function, which is called when the element is unmounted.
Open the `pannable.js` file. Like transition functions, an action function receives a `node` and some optional parameters, and returns an action object. That object can have a `destroy` function, which is called when the element is unmounted.

We want to fire `panstart` event when the user mouses down on the element, `panmove` events (with `dx` and `dy` properties showing how far the mouse moved) when they drag it, and `panend` events when they mouse up. One possible implementation looks like this:

Expand Down Expand Up @@ -84,4 +84,3 @@ export function pannable(node) {
Update the `pannable` function and try moving the box around.

> This implementation is for demonstration purposes — a more complete one would also consider touch events.
4 changes: 4 additions & 0 deletions site/src/routes/faq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function get(req, res) {
res.writeHead(302, { Location: 'https://github.com/sveltejs/svelte/wiki/FAQ' });
res.end();
}
10 changes: 9 additions & 1 deletion src/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ export default class Element extends Node {
message: `'group' binding can only be used with <input type="checkbox"> or <input type="radio">`
});
}
} else if (name == 'files') {
} else if (name === 'files') {
if (this.name !== 'input') {
component.error(binding, {
code: `invalid-binding`,
Expand All @@ -564,6 +564,14 @@ export default class Element extends Node {
message: `'files' binding can only be used with <input type="file">`
});
}

} else if (name === 'open') {
if (this.name !== 'details') {
component.error(binding, {
code: `invalid-binding`,
message: `'${name}' binding can only be used with <details>`
});
}
} else if (
name === 'currentTime' ||
name === 'duration' ||
Expand Down
13 changes: 12 additions & 1 deletion src/compile/render-dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export default function dom(
});

if (component.compile_options.dev) {
// TODO check no uunexpected props were passed, as well as
// checking that expected ones were passed
const expected = props.filter(prop => !prop.initialised);

Expand Down Expand Up @@ -395,6 +394,16 @@ export default function dom(
return $name;
});

let unknown_props_check;
if (component.compile_options.dev && writable_props.length) {
unknown_props_check = deindent`
const writable_props = [${writable_props.map(prop => `'${prop.export_name}'`).join(', ')}];
Object.keys($$props).forEach(key => {
if (!writable_props.includes(key)) console.warn(\`<${component.tag}> was created with unknown prop '\${key}'\`);
});
`;
}

builder.add_block(deindent`
function ${definition}(${args.join(', ')}) {
${reactive_store_declarations.length > 0 && `let ${reactive_store_declarations.join(', ')};`}
Expand All @@ -405,6 +414,8 @@ export default function dom(
${component.javascript}
${unknown_props_check}
${component.slots.size && `let { $$slots = {}, $$scope } = $$props;`}
${renderer.binding_groups.length > 0 && `const $$binding_groups = [${renderer.binding_groups.map(_ => `[]`).join(', ')}];`}
Expand Down
8 changes: 7 additions & 1 deletion src/compile/render-dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ const events = [
name === 'playbackRate'
},

// details event
{
event_names: ['toggle'],
filter: (node: Element, name: string) =>
node.name === 'details'
},
];

export default class ElementWrapper extends Wrapper {
Expand Down Expand Up @@ -455,7 +461,7 @@ export default class ElementWrapper extends Wrapper {
function ${handler}() {
${animation_frame && deindent`
cancelAnimationFrame(${animation_frame});
if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});`}
if (!${this.var}.paused) ${animation_frame} = @raf(${handler});`}
${needs_lock && `${lock} = true;`}
ctx.${handler}.call(${this.var}${contextual_dependencies.size > 0 ? ', ctx' : ''});
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal/loop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { now } from './utils';
import { now, raf } from './utils';

export interface Task { abort(): void; promise: Promise<undefined> }

Expand All @@ -14,7 +14,7 @@ function run_tasks() {
});

running = tasks.size > 0;
if (running) requestAnimationFrame(run_tasks);
if (running) raf(run_tasks);
}

export function clear_loops() {
Expand All @@ -28,7 +28,7 @@ export function loop(fn: (number)=>void): Task {

if (!running) {
running = true;
requestAnimationFrame(run_tasks);
raf(run_tasks);
}

return {
Expand Down
3 changes: 2 additions & 1 deletion src/internal/style_manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { element } from './dom';
import { raf } from './utils';

let stylesheet;
let active = 0;
Expand Down Expand Up @@ -56,7 +57,7 @@ export function delete_rule(node, name?) {
}

export function clear_rules() {
requestAnimationFrame(() => {
raf(() => {
if (active) return;
let i = stylesheet.cssRules.length;
while (i--) stylesheet.deleteRule(i);
Expand Down
10 changes: 9 additions & 1 deletion src/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ export function exclude_internal_props(props) {
return result;
}

export let now: () => number = typeof window !== 'undefined'
const is_client = typeof window !== 'undefined';

export let now: () => number = is_client
? () => window.performance.now()
: () => Date.now();

export let raf = is_client ? requestAnimationFrame : noop;

// used internally for testing
export function set_now(fn) {
now = fn;
}

export function set_raf(fn) {
raf = fn;
}
69 changes: 69 additions & 0 deletions test/js/samples/bind-open/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* generated by Svelte vX.Y.Z */
import {
SvelteComponent,
detach,
element,
init,
insert,
listen,
noop,
safe_not_equal
} from "svelte/internal";

function create_fragment(ctx) {
var details, dispose;

return {
c() {
details = element("details");
details.innerHTML = `<summary>summary</summary>content
`;
dispose = listen(details, "toggle", ctx.details_toggle_handler);
},

m(target, anchor) {
insert(target, details, anchor);

details.open = ctx.open;
},

p(changed, ctx) {
if (changed.open) details.open = ctx.open;
},

i: noop,
o: noop,

d(detaching) {
if (detaching) {
detach(details);
}

dispose();
}
};
}

function instance($$self, $$props, $$invalidate) {
let { open } = $$props;

function details_toggle_handler() {
open = this.open;
$$invalidate('open', open);
}

$$self.$set = $$props => {
if ('open' in $$props) $$invalidate('open', open = $$props.open);
};

return { open, details_toggle_handler };
}

class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, create_fragment, safe_not_equal, ["open"]);
}
}

export default Component;
7 changes: 7 additions & 0 deletions test/js/samples/bind-open/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
export let open;
</script>

<details bind:open={open}>
<summary>summary</summary>content
</details>
5 changes: 5 additions & 0 deletions test/js/samples/debug-empty/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let { name } = $$props;

const writable_props = ['name'];
Object.keys($$props).forEach(key => {
if (!writable_props.includes(key)) console.warn(`<Component> was created with unknown prop '${key}'`);
});

$$self.$set = $$props => {
if ('name' in $$props) $$invalidate('name', name = $$props.name);
};
Expand Down
5 changes: 5 additions & 0 deletions test/js/samples/debug-foo-bar-baz-things/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let { things, foo, bar, baz } = $$props;

const writable_props = ['things', 'foo', 'bar', 'baz'];
Object.keys($$props).forEach(key => {
if (!writable_props.includes(key)) console.warn(`<Component> was created with unknown prop '${key}'`);
});

$$self.$set = $$props => {
if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
Expand Down
5 changes: 5 additions & 0 deletions test/js/samples/debug-foo/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ function create_fragment(ctx) {
function instance($$self, $$props, $$invalidate) {
let { things, foo } = $$props;

const writable_props = ['things', 'foo'];
Object.keys($$props).forEach(key => {
if (!writable_props.includes(key)) console.warn(`<Component> was created with unknown prop '${key}'`);
});

$$self.$set = $$props => {
if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/debug-hoisted/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ class Component extends SvelteComponentDev {
}
}

export default Component;
export default Component;
5 changes: 5 additions & 0 deletions test/js/samples/dev-warning-missing-data-computed/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function instance($$self, $$props, $$invalidate) {

let bar;

const writable_props = ['foo'];
Object.keys($$props).forEach(key => {
if (!writable_props.includes(key)) console.warn(`<Component> was created with unknown prop '${key}'`);
});

$$self.$set = $$props => {
if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
};
Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/media-bindings/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
insert,
listen,
noop,
raf,
run_all,
safe_not_equal,
time_ranges_to_array
Expand All @@ -18,7 +19,7 @@ function create_fragment(ctx) {

function audio_timeupdate_handler() {
cancelAnimationFrame(audio_animationframe);
if (!audio.paused) audio_animationframe = requestAnimationFrame(audio_timeupdate_handler);
if (!audio.paused) audio_animationframe = raf(audio_timeupdate_handler);
audio_updating = true;
ctx.audio_timeupdate_handler.call(audio);
}
Expand Down
6 changes: 3 additions & 3 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
import * as fs from "fs";
import { rollup } from 'rollup';
import * as virtual from 'rollup-plugin-virtual';
import { clear_loops, set_now } from "../../internal.js";
import { clear_loops, set_now, set_raf } from "../../internal.js";

import {
showOutput,
Expand Down Expand Up @@ -101,15 +101,15 @@ describe("runtime", () => {
}
};
set_now(() => raf.time);
global.requestAnimationFrame = cb => {
set_raf(cb => {
let called = false;
raf.callback = () => {
if (!called) {
called = true;
cb();
}
};
};
});

try {
mod = require(`./samples/${dir}/main.svelte`);
Expand Down
25 changes: 25 additions & 0 deletions test/runtime/samples/binding-details-open/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export default {
html: `
<details><summary>toggle</summary></details>
`,

async test({ assert, component, target, window }) {
const details = target.querySelector('details');
const event = new window.Event('toggle');

details.open = true;
await details.dispatchEvent(event);
assert.equal(component.visible, true);
assert.htmlEqual(target.innerHTML, `
<details open><summary>toggle</summary></details>
<p>hello!</p>
`);

details.open = false;
await details.dispatchEvent(event);
assert.equal(component.visible, false);
assert.htmlEqual(target.innerHTML, `
<details><summary>toggle</summary></details>
`);
}
};
9 changes: 9 additions & 0 deletions test/runtime/samples/binding-details-open/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
export let visible;
</script>

<details bind:open={visible}><summary>toggle</summary></details>

{#if visible}
<p>hello!</p>
{/if}
Loading

0 comments on commit 821133f

Please sign in to comment.