Refactor: move dom utils to @grafana/ui (#17976)

This commit is contained in:
Ryan McKinley
2019-07-08 10:02:16 -07:00
committed by GitHub
parent 98ba3f34a6
commit ccc3d88cee
5 changed files with 20 additions and 18 deletions
+41
View File
@@ -0,0 +1,41 @@
// Node.closest() polyfill
if ('Element' in window && !Element.prototype.closest) {
Element.prototype.closest = function(this: any, s: string) {
const matches = (this.document || this.ownerDocument).querySelectorAll(s);
let el = this;
let i;
// eslint-disable-next-line
do {
i = matches.length;
// eslint-disable-next-line
while (--i >= 0 && matches.item(i) !== el) {}
el = el.parentElement;
} while (i < 0 && el);
return el;
};
}
export function getPreviousCousin(node: any, selector: string) {
let sibling = node.parentElement.previousSibling;
let el;
while (sibling) {
el = sibling.querySelector(selector);
if (el) {
return el;
}
sibling = sibling.previousSibling;
}
return undefined;
}
export function getNextCharacter(global = window) {
const selection = global.getSelection();
if (!selection || !selection.anchorNode) {
return null;
}
const range = selection.getRangeAt(0);
const text = selection.anchorNode.textContent;
const offset = range.startOffset;
return text!.substr(offset, 1);
}
+4
View File
@@ -7,3 +7,7 @@ export * from './deprecationWarning';
export * from './validate';
export { getFlotPairs } from './flotPairs';
export * from './slate';
// Export with a namespace
import * as DOMUtil from './dom'; // includes Element.closest polyfil
export { DOMUtil };