From 0eef790e6be63639bedb45551ec4cf6ac165fc8d Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Tue, 3 Sep 2019 22:59:41 +0800 Subject: [PATCH] update vc-align to 2.4.5 --- components/vc-align/Align.jsx | 9 +++++++-- components/vc-align/index.js | 2 +- components/vc-align/util.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/components/vc-align/Align.jsx b/components/vc-align/Align.jsx index a7922ac68..3ffa82d8a 100644 --- a/components/vc-align/Align.jsx +++ b/components/vc-align/Align.jsx @@ -1,7 +1,7 @@ import PropTypes from '../_util/vue-types'; import { alignElement, alignPoint } from 'dom-align'; import addEventListener from '../_util/Dom/addEventListener'; -import { isWindow, buffer, isSamePoint } from './util'; +import { isWindow, buffer, isSamePoint, isSimilarValue, restoreFocus } from './util'; import { cloneElement } from '../_util/vnode.js'; import clonedeep from 'lodash/cloneDeep'; import { getSlot } from '../_util/props-util'; @@ -73,7 +73,7 @@ export default { if ( !reAlign && source && - (preRect.width !== sourceRect.width || preRect.height !== sourceRect.height) + (!isSimilarValue(preRect.width, sourceRect.width) || !isSimilarValue(preRect.height, sourceRect.height)) ) { reAlign = true; } @@ -121,11 +121,16 @@ export default { const element = getElement(target); const point = getPoint(target); + // IE lose focus after element realign + // We should record activeElement and restore later + const activeElement = document.activeElement; + if (element) { result = alignElement(source, element, align); } else if (point) { result = alignPoint(source, point, align); } + restoreFocus(activeElement, source); this.aligned = true; this.$listeners.align && this.$listeners.align(source, result); } diff --git a/components/vc-align/index.js b/components/vc-align/index.js index 9afdc8c5c..1df9d009d 100644 --- a/components/vc-align/index.js +++ b/components/vc-align/index.js @@ -1,3 +1,3 @@ -// based on vc-align 2.4.3 +// based on vc-align 2.4.5 import Align from './Align'; export default Align; diff --git a/components/vc-align/util.js b/components/vc-align/util.js index fd6f1edb8..e273b9cf6 100644 --- a/components/vc-align/util.js +++ b/components/vc-align/util.js @@ -1,3 +1,4 @@ +import contains from '../_util/Dom/contains'; export function buffer(fn, ms) { let timer; @@ -36,3 +37,19 @@ export function isSamePoint(prev, next) { export function isWindow(obj) { return obj && typeof obj === 'object' && obj.window === obj; } + +export function isSimilarValue(val1, val2) { + const int1 = Math.floor(val1); + const int2 = Math.floor(val2); + return Math.abs(int1 - int2) <= 1; +} + +export function restoreFocus(activeElement, container) { + // Focus back if is in the container + if ( + activeElement !== document.activeElement && + contains(container, activeElement) + ) { + activeElement.focus(); + } +}