Skip to content

Commit

Permalink
Enhancement - EUICodeEditor for Visualize JSON (elastic#58679)
Browse files Browse the repository at this point in the history
Switched out the textfield in the advanced JSON editor for the EUI code editor component. removed legacy import isValidJson in favor of using Ace Editor's default json validation.
  • Loading branch information
ThomThomson committed Mar 11, 2020
1 parent e320d0f commit 04e4c37
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 119 deletions.
1 change: 0 additions & 1 deletion src/legacy/core_plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export {
isStringType,
isType,
isValidInterval,
isValidJson,
METRIC_TYPES,
OptionedParamType,
parentPipelineType,
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/core_plugins/data/public/search/aggs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export { toAbsoluteDates } from './buckets/lib/date_utils';
export { convertIPRangeToString } from './buckets/ip_range';
export { aggTypeFilters, propFilter } from './filter';
export { OptionedParamType } from './param_types/optioned';
export { isValidJson, isValidInterval } from './utils';
export { isValidInterval } from './utils';
export { BUCKET_TYPES } from './buckets/bucket_agg_types';
export { METRIC_TYPES } from './metrics/metric_agg_types';

Expand Down
49 changes: 0 additions & 49 deletions src/legacy/core_plugins/data/public/search/aggs/utils.test.tsx

This file was deleted.

29 changes: 0 additions & 29 deletions src/legacy/core_plugins/data/public/search/aggs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,6 @@
import { leastCommonInterval } from 'ui/vis/lib/least_common_interval';
import { isValidEsInterval } from '../../../common';

/**
* Check a string if it's a valid JSON.
*
* @param {string} value a string that should be validated
* @returns {boolean} true if value is a valid JSON or if value is an empty string, or a string with whitespaces, otherwise false
*/
export function isValidJson(value: string): boolean {
if (!value || value.length === 0) {
return true;
}

const trimmedValue = value.trim();

if (trimmedValue.length === 0) {
return true;
}

if (trimmedValue[0] === '{' || trimmedValue[0] === '[') {
try {
JSON.parse(trimmedValue);
return true;
} catch (e) {
return false;
}
} else {
return false;
}
}

export function isValidInterval(value: string, baseInterval?: string) {
if (baseInterval) {
return _parseWithBase(value, baseInterval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,91 @@
* under the License.
*/

import React, { useEffect } from 'react';
import React, { useState, useMemo, useCallback } from 'react';

import { EuiFormRow, EuiIconTip, EuiTextArea } from '@elastic/eui';
import { EuiFormRow, EuiIconTip, EuiCodeEditor, EuiScreenReaderOnly } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';

import { isValidJson } from '../../legacy_imports';
import { AggParamEditorProps } from '../agg_param_props';

function RawJsonParamEditor({
agg,
showValidation,
value = '',
setValidity,
setValue,
setTouched,
}: AggParamEditorProps<string>) {
const label = (
<>
<FormattedMessage id="visDefaultEditor.controls.jsonInputLabel" defaultMessage="JSON input" />{' '}
<EuiIconTip
position="right"
content={i18n.translate('visDefaultEditor.controls.jsonInputTooltip', {
defaultMessage:
"Any JSON formatted properties you add here will be merged with the elasticsearch aggregation definition for this section. For example 'shard_size' on a terms aggregation.",
})}
type="questionInCircle"
/>
</>
const [isFieldValid, setFieldValidity] = useState(true);
const [editorReady, setEditorReady] = useState(false);

const editorTooltipText = useMemo(
() =>
i18n.translate('visDefaultEditor.controls.jsonInputTooltip', {
defaultMessage:
"Any JSON formatted properties you add here will be merged with the elasticsearch aggregation definition for this section. For example 'shard_size' on a terms aggregation.",
}),
[]
);
const isValid = isValidJson(value);

const onChange = (ev: React.ChangeEvent<HTMLTextAreaElement>) => {
const textValue = ev.target.value;
setValue(textValue);
setValidity(isValidJson(textValue));
};
const jsonEditorLabelText = useMemo(
() =>
i18n.translate('visDefaultEditor.controls.jsonInputLabel', {
defaultMessage: 'JSON input',
}),
[]
);

useEffect(() => {
setValidity(isValid);
}, [isValid]);
const label = useMemo(
() => (
<>
{jsonEditorLabelText}{' '}
<EuiIconTip position="right" content={editorTooltipText} type="questionInCircle" />
</>
),
[jsonEditorLabelText, editorTooltipText]
);

const onEditorValidate = useCallback(
(annotations: unknown[]) => {
// The first onValidate returned from EuiCodeEditor is a false negative
if (editorReady) {
const validity = annotations.length === 0;
setFieldValidity(validity);
setValidity(validity);
} else {
setEditorReady(true);
}
},
[setValidity, editorReady]
);

return (
<EuiFormRow
label={label}
isInvalid={showValidation ? !isValid : false}
isInvalid={showValidation ? !isFieldValid : false}
fullWidth={true}
compressed
>
<EuiTextArea
id={`visEditorRawJson${agg.id}`}
isInvalid={showValidation ? !isValid : false}
value={value}
onChange={onChange}
rows={2}
fullWidth={true}
onBlur={setTouched}
compressed
/>
<>
<EuiCodeEditor
mode="json"
theme="github"
width="100%"
height="250px"
value={value}
onValidate={onEditorValidate}
setOptions={{
fontSize: '14px',
}}
onChange={setValue}
onBlur={setTouched}
aria-label={jsonEditorLabelText}
aria-describedby="jsonEditorDescription"
/>
<EuiScreenReaderOnly>
<p id="jsonEditorDescription">{editorTooltipText}</p>
</EuiScreenReaderOnly>
</>
</EuiFormRow>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export { parentPipelineType } from 'ui/agg_types';
export { siblingPipelineType } from 'ui/agg_types';
export { isType, isStringType } from 'ui/agg_types';
export { OptionedValueProp, OptionedParamEditorProps, OptionedParamType } from 'ui/agg_types';
export { isValidJson, isValidInterval } from 'ui/agg_types';
export { isValidInterval } from 'ui/agg_types';
export { AggParamOption } from 'ui/agg_types';
export { CidrMask } from 'ui/agg_types';

Expand Down
1 change: 0 additions & 1 deletion src/legacy/ui/public/agg_types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export {
isStringType,
isType,
isValidInterval,
isValidJson,
OptionedParamType,
parentPipelineType,
propFilter,
Expand Down

0 comments on commit 04e4c37

Please sign in to comment.