Skip to content

Commit

Permalink
Merge branch 'master' of github.com:elastic/kibana into maps/tracktil…
Browse files Browse the repository at this point in the history
…eloadstatus
  • Loading branch information
thomasneirynck committed Feb 17, 2021
2 parents a5c281f + e095a6a commit 18b8f47
Show file tree
Hide file tree
Showing 30 changed files with 382 additions and 389 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ import { DocViewRenderProps } from '../../doc_views/doc_views_types';
jest.mock('../../../kibana_services', () => {
let registry: any[] = [];
return {
getServices: () => ({
uiSettings: {
get: jest.fn(),
},
}),
getDocViewsRegistry: () => ({
addDocView(view: any) {
registry.push(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@
*/

import React from 'react';
import { EuiCallOut, EuiCodeBlock } from '@elastic/eui';
import { formatMsg, formatStack } from '../../../../../kibana_legacy/public';
import { EuiErrorBoundary } from '@elastic/eui';

interface Props {
error: Error | string;
}

export function DocViewerError({ error }: Props) {
const errMsg = formatMsg(error);
const errStack = typeof error === 'object' ? formatStack(error) : '';
const DocViewerErrorWrapper = ({ error }: Props) => {
throw error;
};

return (
<EuiCallOut title={errMsg} color="danger" iconType="cross" data-test-subj="docViewerError">
{errStack && <EuiCodeBlock>{errStack}</EuiCodeBlock>}
</EuiCallOut>
);
}
export const DocViewerError = ({ error }: Props) => (
<EuiErrorBoundary data-test-subj="docViewerError">
<DocViewerErrorWrapper error={error} />
</EuiErrorBoundary>
);
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { I18nProvider } from '@kbn/i18n/react';
import { DocViewRenderTab } from './doc_viewer_render_tab';
import { DocViewerError } from './doc_viewer_render_error';
import { DocViewRenderFn, DocViewRenderProps } from '../../doc_views/doc_views_types';
import { getServices } from '../../../kibana_services';
import { KibanaContextProvider } from '../../../../../kibana_react/public';

interface Props {
component?: React.ComponentType<DocViewRenderProps>;
Expand Down Expand Up @@ -72,7 +74,9 @@ export class DocViewerTab extends React.Component<Props, State> {
const Component = component as any;
return (
<I18nProvider>
<Component {...renderProps} />
<KibanaContextProvider services={{ uiSettings: getServices().uiSettings }}>
<Component {...renderProps} />
</KibanaContextProvider>
</I18nProvider>
);
}
Expand Down

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.dscJsonCodeEditor {
width: 100%
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@

import React from 'react';
import { shallow } from 'enzyme';
import { JsonCodeBlock } from './json_code_block';
import { IndexPattern } from '../../../../../data/public';
import { JsonCodeEditor } from './json_code_editor';

it('returns the `JsonCodeEditor` component', () => {
const props = {
hit: { _index: 'test', _type: 'doc', _id: 'foo', _score: 1, _source: { test: 123 } },
columns: [],
indexPattern: {} as IndexPattern,
filter: jest.fn(),
onAddColumn: jest.fn(),
onRemoveColumn: jest.fn(),
const value = {
_index: 'test',
_type: 'doc',
_id: 'foo',
_score: 1,
_source: { test: 123 },
};
expect(shallow(<JsonCodeBlock {...props} />)).toMatchSnapshot();
expect(shallow(<JsonCodeEditor hit={value} />)).toMatchSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import './json_code_editor.scss';

import React, { useCallback } from 'react';
import { i18n } from '@kbn/i18n';
import { monaco, XJsonLang } from '@kbn/monaco';
import { EuiButtonEmpty, EuiCopy, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui';
import { CodeEditor } from '../../../../../kibana_react/public';
import { DocViewRenderProps } from '../../../application/doc_views/doc_views_types';

const codeEditorAriaLabel = i18n.translate('discover.json.codeEditorAriaLabel', {
defaultMessage: 'Read only JSON view of an elasticsearch document',
});
const copyToClipboardLabel = i18n.translate('discover.json.copyToClipboardLabel', {
defaultMessage: 'Copy to clipboard',
});

export const JsonCodeEditor = ({ hit }: DocViewRenderProps) => {
const jsonValue = JSON.stringify(hit, null, 2);

// setting editor height based on lines height and count to stretch and fit its content
const setEditorCalculatedHeight = useCallback((editor) => {
const editorElement = editor.getDomNode();

if (!editorElement) {
return;
}

const lineHeight = editor.getOption(monaco.editor.EditorOption.lineHeight);
const lineCount = editor.getModel()?.getLineCount() || 1;
const height = editor.getTopForLineNumber(lineCount + 1) + lineHeight;

editorElement.style.height = `${height}px`;
editor.layout();
}, []);

return (
<EuiFlexGroup className="dscJsonCodeEditor" direction="column" gutterSize="s">
<EuiFlexItem grow={true}>
<EuiSpacer size="s" />
<div className="eui-textRight">
<EuiCopy textToCopy={jsonValue}>
{(copy) => (
<EuiButtonEmpty size="xs" flush="right" iconType="copyClipboard" onClick={copy}>
{copyToClipboardLabel}
</EuiButtonEmpty>
)}
</EuiCopy>
</div>
</EuiFlexItem>
<EuiFlexItem grow={true}>
<CodeEditor
languageId={XJsonLang.ID}
value={jsonValue}
onChange={() => {}}
editorDidMount={setEditorCalculatedHeight}
aria-label={codeEditorAriaLabel}
options={{
automaticLayout: true,
fontSize: 12,
minimap: {
enabled: false,
},
overviewRulerBorder: false,
readOnly: true,
scrollbar: {
alwaysConsumeMouseWheel: false,
},
scrollBeyondLastLine: false,
wordWrap: 'on',
wrappingIndent: 'indent',
}}
/>
</EuiFlexItem>
</EuiFlexGroup>
);
};
4 changes: 2 additions & 2 deletions src/plugins/discover/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { UrlGeneratorState } from '../../share/public';
import { DocViewInput, DocViewInputFn } from './application/doc_views/doc_views_types';
import { DocViewsRegistry } from './application/doc_views/doc_views_registry';
import { DocViewTable } from './application/components/table/table';
import { JsonCodeBlock } from './application/components/json_code_block/json_code_block';
import { JsonCodeEditor } from './application/components/json_code_editor/json_code_editor';
import {
setDocViewsRegistry,
setUrlTracker,
Expand Down Expand Up @@ -187,7 +187,7 @@ export class DiscoverPlugin
defaultMessage: 'JSON',
}),
order: 20,
component: JsonCodeBlock,
component: JsonCodeEditor,
});

const {
Expand Down
Loading

0 comments on commit 18b8f47

Please sign in to comment.