Refactor: move dom utils to @grafana/ui (#17976)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
// Node.closest() polyfill
|
// Node.closest() polyfill
|
||||||
if ('Element' in window && !Element.prototype.closest) {
|
if ('Element' in window && !Element.prototype.closest) {
|
||||||
Element.prototype.closest = function(this: any, s) {
|
Element.prototype.closest = function(this: any, s: string) {
|
||||||
const matches = (this.document || this.ownerDocument).querySelectorAll(s);
|
const matches = (this.document || this.ownerDocument).querySelectorAll(s);
|
||||||
let el = this;
|
let el = this;
|
||||||
let i;
|
let i;
|
||||||
@@ -15,7 +15,7 @@ if ('Element' in window && !Element.prototype.closest) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPreviousCousin(node, selector) {
|
export function getPreviousCousin(node: any, selector: string) {
|
||||||
let sibling = node.parentElement.previousSibling;
|
let sibling = node.parentElement.previousSibling;
|
||||||
let el;
|
let el;
|
||||||
while (sibling) {
|
while (sibling) {
|
||||||
@@ -30,12 +30,12 @@ export function getPreviousCousin(node, selector) {
|
|||||||
|
|
||||||
export function getNextCharacter(global = window) {
|
export function getNextCharacter(global = window) {
|
||||||
const selection = global.getSelection();
|
const selection = global.getSelection();
|
||||||
if (!selection.anchorNode) {
|
if (!selection || !selection.anchorNode) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const range = selection.getRangeAt(0);
|
const range = selection.getRangeAt(0);
|
||||||
const text = selection.anchorNode.textContent;
|
const text = selection.anchorNode.textContent;
|
||||||
const offset = range.startOffset;
|
const offset = range.startOffset;
|
||||||
return text.substr(offset, 1);
|
return text!.substr(offset, 1);
|
||||||
}
|
}
|
||||||
@@ -7,3 +7,7 @@ export * from './deprecationWarning';
|
|||||||
export * from './validate';
|
export * from './validate';
|
||||||
export { getFlotPairs } from './flotPairs';
|
export { getFlotPairs } from './flotPairs';
|
||||||
export * from './slate';
|
export * from './slate';
|
||||||
|
|
||||||
|
// Export with a namespace
|
||||||
|
import * as DOMUtil from './dom'; // includes Element.closest polyfil
|
||||||
|
export { DOMUtil };
|
||||||
|
|||||||
+2
-2
@@ -4,7 +4,7 @@ import Plain from 'slate-plain-serializer';
|
|||||||
|
|
||||||
import QueryField from './query_field';
|
import QueryField from './query_field';
|
||||||
import debounce from 'lodash/debounce';
|
import debounce from 'lodash/debounce';
|
||||||
import { getNextCharacter } from 'app/features/explore/utils/dom';
|
import { DOMUtil } from '@grafana/ui';
|
||||||
|
|
||||||
import { KEYWORDS, functionTokens, operatorTokens, grafanaMacros } from './kusto/kusto';
|
import { KEYWORDS, functionTokens, operatorTokens, grafanaMacros } from './kusto/kusto';
|
||||||
// import '../sass/editor.base.scss';
|
// import '../sass/editor.base.scss';
|
||||||
@@ -203,7 +203,7 @@ export default class KustoQueryField extends QueryField {
|
|||||||
|
|
||||||
// Modify suggestion based on context
|
// Modify suggestion based on context
|
||||||
|
|
||||||
const nextChar = getNextCharacter();
|
const nextChar = DOMUtil.getNextCharacter();
|
||||||
if (suggestion.type === 'function') {
|
if (suggestion.type === 'function') {
|
||||||
if (!nextChar || nextChar !== '(') {
|
if (!nextChar || nextChar !== '(') {
|
||||||
suggestionText += '(';
|
suggestionText += '(';
|
||||||
|
|||||||
@@ -10,13 +10,12 @@ import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explor
|
|||||||
|
|
||||||
// Utils & Services
|
// Utils & Services
|
||||||
// dom also includes Element polyfills
|
// dom also includes Element polyfills
|
||||||
import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom';
|
|
||||||
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { LokiQuery } from '../types';
|
import { LokiQuery } from '../types';
|
||||||
import { TypeaheadOutput, HistoryItem } from 'app/types/explore';
|
import { TypeaheadOutput, HistoryItem } from 'app/types/explore';
|
||||||
import { DataSourceApi, ExploreQueryFieldProps, DataSourceStatus } from '@grafana/ui';
|
import { DataSourceApi, ExploreQueryFieldProps, DataSourceStatus, DOMUtil } from '@grafana/ui';
|
||||||
import { AbsoluteTimeRange } from '@grafana/data';
|
import { AbsoluteTimeRange } from '@grafana/data';
|
||||||
|
|
||||||
function getChooserText(hasSyntax: boolean, hasLogLabels: boolean, datasourceStatus: DataSourceStatus) {
|
function getChooserText(hasSyntax: boolean, hasLogLabels: boolean, datasourceStatus: DataSourceStatus) {
|
||||||
@@ -36,7 +35,7 @@ function willApplySuggestion(suggestion: string, { typeaheadContext, typeaheadTe
|
|||||||
// Modify suggestion based on context
|
// Modify suggestion based on context
|
||||||
switch (typeaheadContext) {
|
switch (typeaheadContext) {
|
||||||
case 'context-labels': {
|
case 'context-labels': {
|
||||||
const nextChar = getNextCharacter();
|
const nextChar = DOMUtil.getNextCharacter();
|
||||||
if (!nextChar || nextChar === '}' || nextChar === ',') {
|
if (!nextChar || nextChar === '}' || nextChar === ',') {
|
||||||
suggestion += '=';
|
suggestion += '=';
|
||||||
}
|
}
|
||||||
@@ -48,7 +47,7 @@ function willApplySuggestion(suggestion: string, { typeaheadContext, typeaheadTe
|
|||||||
if (!typeaheadText.match(/^(!?=~?"|")/)) {
|
if (!typeaheadText.match(/^(!?=~?"|")/)) {
|
||||||
suggestion = `"${suggestion}`;
|
suggestion = `"${suggestion}`;
|
||||||
}
|
}
|
||||||
if (getNextCharacter() !== '"') {
|
if (DOMUtil.getNextCharacter() !== '"') {
|
||||||
suggestion = `${suggestion}"`;
|
suggestion = `${suggestion}"`;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -130,9 +129,9 @@ export class LokiQueryFieldForm extends React.PureComponent<LokiQueryFieldFormPr
|
|||||||
|
|
||||||
// Get DOM-dependent context
|
// Get DOM-dependent context
|
||||||
const wrapperClasses = Array.from(wrapperNode.classList);
|
const wrapperClasses = Array.from(wrapperNode.classList);
|
||||||
const labelKeyNode = getPreviousCousin(wrapperNode, '.attr-name');
|
const labelKeyNode = DOMUtil.getPreviousCousin(wrapperNode, '.attr-name');
|
||||||
const labelKey = labelKeyNode && labelKeyNode.textContent;
|
const labelKey = labelKeyNode && labelKeyNode.textContent;
|
||||||
const nextChar = getNextCharacter();
|
const nextChar = DOMUtil.getNextCharacter();
|
||||||
|
|
||||||
const result = datasource.languageProvider.provideCompletionItems(
|
const result = datasource.languageProvider.provideCompletionItems(
|
||||||
{ text, value, prefix, wrapperClasses, labelKey },
|
{ text, value, prefix, wrapperClasses, labelKey },
|
||||||
|
|||||||
@@ -10,12 +10,11 @@ import Prism from 'prismjs';
|
|||||||
import { TypeaheadOutput, HistoryItem } from 'app/types/explore';
|
import { TypeaheadOutput, HistoryItem } from 'app/types/explore';
|
||||||
|
|
||||||
// dom also includes Element polyfills
|
// dom also includes Element polyfills
|
||||||
import { getNextCharacter, getPreviousCousin } from 'app/features/explore/utils/dom';
|
|
||||||
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
import BracesPlugin from 'app/features/explore/slate-plugins/braces';
|
||||||
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
import QueryField, { TypeaheadInput, QueryFieldState } from 'app/features/explore/QueryField';
|
||||||
import { PromQuery, PromContext, PromOptions } from '../types';
|
import { PromQuery, PromContext, PromOptions } from '../types';
|
||||||
import { CancelablePromise, makePromiseCancelable } from 'app/core/utils/CancelablePromise';
|
import { CancelablePromise, makePromiseCancelable } from 'app/core/utils/CancelablePromise';
|
||||||
import { ExploreQueryFieldProps, DataSourceStatus, QueryHint } from '@grafana/ui';
|
import { ExploreQueryFieldProps, DataSourceStatus, QueryHint, DOMUtil } from '@grafana/ui';
|
||||||
import { isDataFrame, toLegacyResponseData } from '@grafana/data';
|
import { isDataFrame, toLegacyResponseData } from '@grafana/data';
|
||||||
import { PrometheusDatasource } from '../datasource';
|
import { PrometheusDatasource } from '../datasource';
|
||||||
|
|
||||||
@@ -73,7 +72,7 @@ export function willApplySuggestion(suggestion: string, { typeaheadContext, type
|
|||||||
// Modify suggestion based on context
|
// Modify suggestion based on context
|
||||||
switch (typeaheadContext) {
|
switch (typeaheadContext) {
|
||||||
case 'context-labels': {
|
case 'context-labels': {
|
||||||
const nextChar = getNextCharacter();
|
const nextChar = DOMUtil.getNextCharacter();
|
||||||
if (!nextChar || nextChar === '}' || nextChar === ',') {
|
if (!nextChar || nextChar === '}' || nextChar === ',') {
|
||||||
suggestion += '=';
|
suggestion += '=';
|
||||||
}
|
}
|
||||||
@@ -85,7 +84,7 @@ export function willApplySuggestion(suggestion: string, { typeaheadContext, type
|
|||||||
if (!typeaheadText.match(/^(!?=~?"|")/)) {
|
if (!typeaheadText.match(/^(!?=~?"|")/)) {
|
||||||
suggestion = `"${suggestion}`;
|
suggestion = `"${suggestion}`;
|
||||||
}
|
}
|
||||||
if (getNextCharacter() !== '"') {
|
if (DOMUtil.getNextCharacter() !== '"') {
|
||||||
suggestion = `${suggestion}"`;
|
suggestion = `${suggestion}"`;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -282,9 +281,9 @@ class PromQueryField extends React.PureComponent<PromQueryFieldProps, PromQueryF
|
|||||||
|
|
||||||
// Get DOM-dependent context
|
// Get DOM-dependent context
|
||||||
const wrapperClasses = Array.from(wrapperNode.classList);
|
const wrapperClasses = Array.from(wrapperNode.classList);
|
||||||
const labelKeyNode = getPreviousCousin(wrapperNode, '.attr-name');
|
const labelKeyNode = DOMUtil.getPreviousCousin(wrapperNode, '.attr-name');
|
||||||
const labelKey = labelKeyNode && labelKeyNode.textContent;
|
const labelKey = labelKeyNode && labelKeyNode.textContent;
|
||||||
const nextChar = getNextCharacter();
|
const nextChar = DOMUtil.getNextCharacter();
|
||||||
|
|
||||||
const result = this.languageProvider.provideCompletionItems(
|
const result = this.languageProvider.provideCompletionItems(
|
||||||
{ text, value, prefix, wrapperClasses, labelKey },
|
{ text, value, prefix, wrapperClasses, labelKey },
|
||||||
|
|||||||
Reference in New Issue
Block a user