fix animateElement() when animation is disabled

This commit is contained in:
tophf 2020-11-06 21:04:10 +03:00
parent aac0f476b2
commit 71cabc2029
8 changed files with 41 additions and 43 deletions

View File

@ -134,27 +134,31 @@ function scrollElementIntoView(element, {invalidMarginRatio = 0} = {}) {
} }
function animateElement( /**
element, { * @param {HTMLElement} el
className = 'highlight', * @param {string} [cls] - class name that defines or starts an animation
removeExtraClasses = [], * @param [removeExtraClasses] - class names to remove at animation end in the *same* paint frame,
onComplete, * which is needed in e.g. Firefox as it may call resolve() in the next frame
} = {}) { * @returns {Promise<void>}
return element && new Promise(resolve => { */
element.addEventListener('animationend', () => { function animateElement(el, cls = 'highlight', ...removeExtraClasses) {
element.classList.remove( return !el ? Promise.resolve(el) : new Promise(resolve => {
className, let onDone = () => {
// In Firefox, `resolve()` might be called one frame later. el.classList.remove(cls, ...removeExtraClasses);
// This is helpful to clean-up on the same frame onDone = null;
...removeExtraClasses
);
// TODO: investigate why animation restarts for 'display' modification in .then()
if (typeof onComplete === 'function') {
onComplete.call(element);
}
resolve(); resolve();
}, {once: true}); };
element.classList.add(className); requestAnimationFrame(() => {
if (onDone) {
const style = getComputedStyle(el);
if (style.animationName === 'none' || !parseFloat(style.animationDuration)) {
el.removeEventListener('animationend', onDone);
onDone();
}
}
});
el.addEventListener('animationend', onDone, {once: true});
el.classList.add(cls);
}); });
} }

View File

@ -70,7 +70,6 @@ function configDialog(style) {
if (isPopup) { if (isPopup) {
adjustSizeForPopup(box); adjustSizeForPopup(box);
box.style.animationDuration = '0s';
} }
box.addEventListener('change', onchange); box.addEventListener('change', onchange);

View File

@ -21,10 +21,9 @@ onDOMready().then(() => {
this.classList.remove('fadeout'); this.classList.remove('fadeout');
} }
}, },
ondragend() { async ondragend() {
animateElement(this, {className: 'fadeout', removeExtraClasses: ['dropzone']}).then(() => { await animateElement(this, 'fadeout', 'dropzone');
this.style.animationDuration = ''; this.style.animationDuration = '';
});
}, },
ondragleave(event) { ondragleave(event) {
try { try {

View File

@ -69,7 +69,7 @@ onDOMready().then(() => {
focusedLink = $('.style-name-link', found); focusedLink = $('.style-name-link', found);
focusedName = found.styleNameLowerCase; focusedName = found.styleNameLowerCase;
scrollElementIntoView(found, {invalidMarginRatio: .25}); scrollElementIntoView(found, {invalidMarginRatio: .25});
animateElement(found, {className: 'highlight-quick'}); animateElement(found, 'highlight-quick');
replaceInlineStyle({ replaceInlineStyle({
width: focusedLink.offsetWidth + 'px', width: focusedLink.offsetWidth + 'px',
height: focusedLink.offsetHeight + 'px', height: focusedLink.offsetHeight + 'px',

View File

@ -728,15 +728,13 @@ function embedOptions() {
options.focus(); options.focus();
} }
function unembedOptions() { async function unembedOptions() {
const options = $('#stylus-embedded-options'); const options = $('#stylus-embedded-options');
if (options) { if (options) {
options.contentWindow.document.body.classList.add('scaleout'); options.contentWindow.document.body.classList.add('scaleout');
options.classList.add('fadeout'); options.classList.add('fadeout');
animateElement(options, { await animateElement(options, 'fadeout');
className: 'fadeout', options.remove();
onComplete: () => options.remove(),
});
} }
} }

View File

@ -12,6 +12,10 @@
-moz-user-select: none; -moz-user-select: none;
} }
#stylus-popup #message-box {
animation: none;
}
#message-box > div { #message-box > div {
top: 3rem; top: 3rem;
right: 3rem; right: 3rem;

View File

@ -94,10 +94,8 @@ function messageBox({
function resolveWith(value) { function resolveWith(value) {
unbindGlobalListeners(); unbindGlobalListeners();
setTimeout(messageBox.resolve, 0, value); setTimeout(messageBox.resolve, 0, value);
animateElement(messageBox.element, { animateElement(messageBox.element, 'fadeout')
className: 'fadeout', .then(removeSelf);
onComplete: removeSelf,
});
if (messageBox.element.contains(document.activeElement)) { if (messageBox.element.contains(document.activeElement)) {
messageBox.originalFocus.focus(); messageBox.originalFocus.focus();
} }

View File

@ -495,10 +495,8 @@ Object.assign(handleEvent, {
const menuActive = $('.menu[data-display=true]'); const menuActive = $('.menu[data-display=true]');
if (menuActive) { if (menuActive) {
// fade-out style menu // fade-out style menu
animateElement(menu, { animateElement(menu, 'lights-on')
className: 'lights-on', .then(() => (menu.dataset.display = false));
onComplete: () => (menu.dataset.display = false),
});
window.onkeydown = null; window.onkeydown = null;
} else { } else {
$('.menu-title', entry).textContent = $('.style-name', entry).textContent; $('.menu-title', entry).textContent = $('.style-name', entry).textContent;
@ -567,10 +565,8 @@ Object.assign(handleEvent, {
function confirm(ok) { function confirm(ok) {
if (ok) { if (ok) {
// fade-out deletion confirmation dialog // fade-out deletion confirmation dialog
animateElement(box, { animateElement(box, 'lights-on')
className: 'lights-on', .then(() => (box.dataset.display = false));
onComplete: () => (box.dataset.display = false),
});
window.onkeydown = null; window.onkeydown = null;
API.deleteStyle(id); API.deleteStyle(id);
} else { } else {