Lint and format src/runtime/internal/style_manager.js

pull/8569/head
S. Elliott Johnson 3 years ago
parent d2e0844f8b
commit bfd5dbdc7f

@ -11,8 +11,7 @@ let active = 0;
function hash(str) { function hash(str) {
let hash = 5381; let hash = 5381;
let i = str.length; let i = str.length;
while (i--) while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
return hash >>> 0; return hash >>> 0;
} }
/** @param {Document | ShadowRoot} doc /** @param {Document | ShadowRoot} doc
@ -50,7 +49,9 @@ export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) {
stylesheet.insertRule(`@keyframes ${name} ${rule}`, stylesheet.cssRules.length); stylesheet.insertRule(`@keyframes ${name} ${rule}`, stylesheet.cssRules.length);
} }
const animation = node.style.animation || ''; const animation = node.style.animation || '';
node.style.animation = `${animation ? `${animation}, ` : ''}${name} ${duration}ms linear ${delay}ms 1 both`; node.style.animation = `${
animation ? `${animation}, ` : ''
}${name} ${duration}ms linear ${delay}ms 1 both`;
active += 1; active += 1;
return name; return name;
} }
@ -60,7 +61,8 @@ export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) {
*/ */
export function delete_rule(node, name) { export function delete_rule(node, name) {
const previous = (node.style.animation || '').split(', '); const previous = (node.style.animation || '').split(', ');
const next = previous.filter(name const next = previous.filter(
name
? (anim) => anim.indexOf(name) < 0 // remove specific animation ? (anim) => anim.indexOf(name) < 0 // remove specific animation
: (anim) => anim.indexOf('__svelte') === -1 // remove all Svelte animations : (anim) => anim.indexOf('__svelte') === -1 // remove all Svelte animations
); );
@ -68,28 +70,22 @@ export function delete_rule(node, name) {
if (deleted) { if (deleted) {
node.style.animation = next.join(', '); node.style.animation = next.join(', ');
active -= deleted; active -= deleted;
if (!active) if (!active) clear_rules();
clear_rules();
} }
} }
/** @returns {void} */ /** @returns {void} */
export function clear_rules() { export function clear_rules() {
raf(() => { raf(() => {
if (active) if (active) return;
return;
managed_styles.forEach((info) => { managed_styles.forEach((info) => {
const { ownerNode } = info.stylesheet; const { ownerNode } = info.stylesheet;
// there is no ownerNode if it runs on jsdom. // there is no ownerNode if it runs on jsdom.
if (ownerNode) if (ownerNode) detach(ownerNode);
detach(ownerNode);
}); });
managed_styles.clear(); managed_styles.clear();
}); });
} }
/** @typedef {Object} StyleInformation /** @typedef {Object} StyleInformation
* @property {CSSStyleSheet} stylesheet * @property {CSSStyleSheet} stylesheet
* @property {Record<string,true>} rules * @property {Record<string,true>} rules

Loading…
Cancel
Save