From 5a533fff8bc09e1520276c3fbb91740b432c8a3b Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 7 Dec 2019 16:54:34 +0000 Subject: [PATCH] Updated codeblock editor to work with fade animation - Added fadeIn to animation JS service. - Updated overlay to use anim service and to recieve a callback for after-anim actions. - Updated code editor popup to refresh Codemirror instance layout after animation has completed. Fixes #1672 --- resources/js/components/overlay.js | 29 ++++++++--------------------- resources/js/services/animations.js | 16 ++++++++++++++++ resources/js/services/code.js | 11 ++++++++++- resources/js/vues/code-editor.js | 4 +++- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/resources/js/components/overlay.js b/resources/js/components/overlay.js index ad6a01061..6963ba9d1 100644 --- a/resources/js/components/overlay.js +++ b/resources/js/components/overlay.js @@ -1,3 +1,4 @@ +import {fadeIn, fadeOut} from "../services/animations"; class Overlay { @@ -19,29 +20,15 @@ class Overlay { } } - hide() { this.toggle(false); } - show() { this.toggle(true); } + hide(onComplete = null) { this.toggle(false, onComplete); } + show(onComplete = null) { this.toggle(true, onComplete); } - toggle(show = true) { - let start = Date.now(); - let duration = 240; - - function setOpacity() { - let elapsedTime = (Date.now() - start); - let targetOpacity = show ? (elapsedTime / duration) : 1-(elapsedTime / duration); - this.container.style.opacity = targetOpacity; - if (elapsedTime > duration) { - this.container.style.display = show ? 'flex' : 'none'; - if (show) { - this.focusOnBody(); - } - this.container.style.opacity = ''; - } else { - requestAnimationFrame(setOpacity.bind(this)); - } + toggle(show = true, onComplete) { + if (show) { + fadeIn(this.container, 240, onComplete); + } else { + fadeOut(this.container, 240, onComplete); } - - requestAnimationFrame(setOpacity.bind(this)); } focusOnBody() { diff --git a/resources/js/services/animations.js b/resources/js/services/animations.js index b6158ea5f..278a765d5 100644 --- a/resources/js/services/animations.js +++ b/resources/js/services/animations.js @@ -5,6 +5,22 @@ */ const animateStylesCleanupMap = new WeakMap(); +/** + * Fade in the given element. + * @param {Element} element + * @param {Number} animTime + * @param {Function|null} onComplete + */ +export function fadeIn(element, animTime = 400, onComplete = null) { + cleanupExistingElementAnimation(element); + element.style.display = 'block'; + animateStyles(element, { + opacity: ['0', '1'] + }, animTime, () => { + if (onComplete) onComplete(); + }); +} + /** * Fade out the given element. * @param {Element} element diff --git a/resources/js/services/code.js b/resources/js/services/code.js index 3fcf74125..533940e3b 100644 --- a/resources/js/services/code.js +++ b/resources/js/services/code.js @@ -258,10 +258,18 @@ function setMode(cmInstance, modeSuggestion, content) { function setContent(cmInstance, codeContent) { cmInstance.setValue(codeContent); setTimeout(() => { - cmInstance.refresh(); + updateLayout(cmInstance); }, 10); } +/** + * Update the layout (codemirror refresh) of a cm instance. + * @param cmInstance + */ +function updateLayout(cmInstance) { + cmInstance.refresh(); +} + /** * Get a CodeMirror instance to use for the markdown editor. * @param {HTMLElement} elem @@ -301,6 +309,7 @@ export default { popupEditor: popupEditor, setMode: setMode, setContent: setContent, + updateLayout: updateLayout, markdownEditor: markdownEditor, getMetaKey: getMetaKey, }; diff --git a/resources/js/vues/code-editor.js b/resources/js/vues/code-editor.js index e0472a973..48b4e1766 100644 --- a/resources/js/vues/code-editor.js +++ b/resources/js/vues/code-editor.js @@ -3,7 +3,9 @@ import codeLib from "../services/code"; const methods = { show() { if (!this.editor) this.editor = codeLib.popupEditor(this.$refs.editor, this.language); - this.$refs.overlay.components.overlay.show(); + this.$refs.overlay.components.overlay.show(() => { + codeLib.updateLayout(this.editor); + }); }, hide() { this.$refs.overlay.components.overlay.hide();