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

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

@ -9,20 +9,19 @@ let active = 0;
* @returns {number} * @returns {number}
*/ */
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
* @param {Element & ElementCSSInlineStyle} node * @param {Element & ElementCSSInlineStyle} node
* @returns {{ stylesheet: any; rules: {}; }} * @returns {{ stylesheet: any; rules: {}; }}
*/ */
function create_style_information(doc, node) { function create_style_information(doc, node) {
const info = { stylesheet: append_empty_stylesheet(node), rules: {} }; const info = { stylesheet: append_empty_stylesheet(node), rules: {} };
managed_styles.set(doc, info); managed_styles.set(doc, info);
return info; return info;
} }
/** @param {Element & ElementCSSInlineStyle} node /** @param {Element & ElementCSSInlineStyle} node
* @param {number} a * @param {number} a
@ -35,62 +34,59 @@ function create_style_information(doc, node) {
* @returns {string} * @returns {string}
*/ */
export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) { export function create_rule(node, a, b, duration, delay, ease, fn, uid = 0) {
const step = 16.666 / duration; const step = 16.666 / duration;
let keyframes = '{\n'; let keyframes = '{\n';
for (let p = 0; p <= 1; p += step) { for (let p = 0; p <= 1; p += step) {
const t = a + (b - a) * ease(p); const t = a + (b - a) * ease(p);
keyframes += p * 100 + `%{${fn(t, 1 - t)}}\n`; keyframes += p * 100 + `%{${fn(t, 1 - t)}}\n`;
} }
const rule = keyframes + `100% {${fn(b, 1 - b)}}\n}`; const rule = keyframes + `100% {${fn(b, 1 - b)}}\n}`;
const name = `__svelte_${hash(rule)}_${uid}`; const name = `__svelte_${hash(rule)}_${uid}`;
const doc = get_root_for_style(node); const doc = get_root_for_style(node);
const { stylesheet, rules } = managed_styles.get(doc) || create_style_information(doc, node); const { stylesheet, rules } = managed_styles.get(doc) || create_style_information(doc, node);
if (!rules[name]) { if (!rules[name]) {
rules[name] = true; rules[name] = true;
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 = `${
active += 1; animation ? `${animation}, ` : ''
return name; }${name} ${duration}ms linear ${delay}ms 1 both`;
active += 1;
return name;
} }
/** @param {Element & ElementCSSInlineStyle} node /** @param {Element & ElementCSSInlineStyle} node
* @param {string} name * @param {string} name
* @returns {void} * @returns {void}
*/ */
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(
? (anim) => anim.indexOf(name) < 0 // remove specific animation name
: (anim) => anim.indexOf('__svelte') === -1 // remove all Svelte animations ? (anim) => anim.indexOf(name) < 0 // remove specific animation
); : (anim) => anim.indexOf('__svelte') === -1 // remove all Svelte animations
const deleted = previous.length - next.length; );
if (deleted) { const deleted = previous.length - next.length;
node.style.animation = next.join(', '); if (deleted) {
active -= deleted; node.style.animation = next.join(', ');
if (!active) active -= deleted;
clear_rules(); if (!active) 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) detach(ownerNode);
if (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