mirror of https://github.com/sveltejs/svelte
parent
a4b86fb02d
commit
82d17c5a6f
@ -1,67 +1,63 @@
|
|||||||
import { element } from './dom';
|
|
||||||
import { framerate } from 'svelte/environment';
|
import { framerate } from 'svelte/environment';
|
||||||
enum SVELTE {
|
|
||||||
RULE = `__svelte_`,
|
|
||||||
STYLESHEET = `__svelte_stylesheet`,
|
|
||||||
RULESET = `__svelte_rules`,
|
|
||||||
}
|
|
||||||
interface ExtendedDoc extends Document {
|
|
||||||
[SVELTE.STYLESHEET]: CSSStyleSheet;
|
|
||||||
[SVELTE.RULESET]: Set<string>;
|
|
||||||
}
|
|
||||||
|
|
||||||
const active_documents = new Set<ExtendedDoc>();
|
let documents_uid = 0;
|
||||||
let running_animations = 0;
|
let running_animations = 0;
|
||||||
|
|
||||||
function add_rule(node): [CSSStyleSheet, Set<string>] {
|
const document_uid = new Map();
|
||||||
const { ownerDocument } = node;
|
const document_stylesheets = new Map();
|
||||||
if (!active_documents.has(ownerDocument)) {
|
|
||||||
active_documents.add(ownerDocument);
|
|
||||||
if (!(SVELTE.STYLESHEET in ownerDocument))
|
|
||||||
ownerDocument[SVELTE.STYLESHEET] = ownerDocument.head.appendChild(element('style')).sheet;
|
|
||||||
if (!(SVELTE.RULESET in ownerDocument)) ownerDocument[SVELTE.RULESET] = new Set();
|
|
||||||
}
|
|
||||||
return [ownerDocument[SVELTE.STYLESHEET], ownerDocument[SVELTE.RULESET]];
|
|
||||||
}
|
|
||||||
|
|
||||||
function hash(str: string) {
|
const current_rules = new Set();
|
||||||
// darkskyapp/string-hash
|
export const animate_css = Function.prototype.call.bind(function animate_css(
|
||||||
let hash = 5381;
|
this: HTMLElement,
|
||||||
let i = str.length;
|
css: (t: number) => string,
|
||||||
while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
|
duration: number,
|
||||||
return hash >>> 0;
|
delay = 0
|
||||||
}
|
) {
|
||||||
const gen = (step, css) => {
|
if (!document_uid.has(this.ownerDocument)) {
|
||||||
|
document_uid.set(this.ownerDocument, documents_uid++);
|
||||||
|
document_stylesheets.set(
|
||||||
|
this.ownerDocument,
|
||||||
|
this.ownerDocument.head.appendChild(this.ownerDocument.createElement('style')).sheet
|
||||||
|
);
|
||||||
|
}
|
||||||
let rule = '{\n';
|
let rule = '{\n';
|
||||||
for (let t = 0; t < 1; t += step) rule += `${100 * t}%{${css(t)}}\n`;
|
for (let t = 0, step = framerate / duration; t < 1; t += step) rule += `${100 * t}%{${css(t)}}\n`;
|
||||||
rule += `100% {${css(1)}}\n}`;
|
rule += `100% {${css(1)}}\n}`;
|
||||||
const name = SVELTE.RULE + hash(rule);
|
|
||||||
return [name, `@keyframes ${name} ${rule}`];
|
// darkskyapp/string-hash
|
||||||
};
|
let i = rule.length,
|
||||||
function animate(this: HTMLElement, css: (t: number) => string, duration: number, delay = 0) {
|
hash = 5381;
|
||||||
const [name, rule] = gen(Math.max(1 / 1000, framerate / duration), css);
|
while (i--) hash = ((hash << 5) - hash) ^ rule.charCodeAt(i);
|
||||||
const [stylesheet, rules] = add_rule(this);
|
const name = '_' + (hash >>> 0) + document_uid.get(this.ownerDocument);
|
||||||
if (!rules.has(name)) {
|
|
||||||
rules.add(name);
|
if (!current_rules.has(name)) {
|
||||||
stylesheet.insertRule(rule, stylesheet.cssRules.length);
|
current_rules.add(name);
|
||||||
|
const stylesheet = document_stylesheets.get(this.ownerDocument);
|
||||||
|
stylesheet.insertRule(`@keyframes ${name} ${rule}`, stylesheet.cssRules.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
const previous = this.style.animation;
|
const previous = this.style.animation;
|
||||||
this.style.animation = `${
|
this.style.animation = `${
|
||||||
previous ? `${previous}, ` : ''
|
previous ? `${previous}, ` : ''
|
||||||
} ${duration}ms linear ${delay}ms 1 normal both running ${name}`;
|
} ${duration}ms linear ${delay}ms 1 normal both running ${name}`;
|
||||||
|
|
||||||
running_animations++;
|
running_animations++;
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const prev = (this.style.animation || '').split(', ');
|
const prev = (this.style.animation || '').split(', ');
|
||||||
const next = prev.filter((anim) => !anim.includes(name));
|
const next = prev.filter((anim) => !anim.includes(name));
|
||||||
if (prev.length !== next.length) this.style.animation = next.join(', ');
|
if (prev.length !== next.length) this.style.animation = next.join(', ');
|
||||||
if (--running_animations) return;
|
if (--running_animations === 0) {
|
||||||
active_documents.forEach(({ [SVELTE.STYLESHEET]: stylesheet, [SVELTE.RULESET]: ruleset }) => {
|
document_stylesheets.forEach((stylesheet) => {
|
||||||
let i = stylesheet.cssRules.length;
|
let i = stylesheet.cssRules.length;
|
||||||
while (i--) stylesheet.deleteRule(i);
|
while (i--) stylesheet.deleteRule(i);
|
||||||
ruleset.clear();
|
|
||||||
});
|
});
|
||||||
active_documents.clear();
|
current_rules.clear();
|
||||||
|
if (1 !== documents_uid) {
|
||||||
|
document_stylesheets.clear();
|
||||||
|
document_uid.clear();
|
||||||
|
documents_uid = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
});
|
||||||
export const animate_css = Function.prototype.call.bind(animate);
|
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
import * as assert from 'assert';
|
||||||
|
type TestSetup = {
|
||||||
|
test: ({ assert: module.assert, component, mod, target, window, raf, compileOptions }) => void | Promise<void>;
|
||||||
|
};
|
Loading…
Reference in new issue