From 21760d483293262ca9dd173068d49e2af93eaa9a Mon Sep 17 00:00:00 2001 From: Anton Dosov Date: Fri, 15 Oct 2021 12:47:18 +0200 Subject: [PATCH] Improve `CodeEditor` wrapper (#114500) --- packages/kbn-optimizer/limits.yml | 2 +- .../__snapshots__/source_viewer.test.tsx.snap | 10 ++- .../public/code_editor/code_editor.tsx | 1 + .../public/code_editor/code_editor_field.tsx | 39 +++++++++++ .../kibana_react/public/code_editor/index.tsx | 70 ++++++------------- .../public/code_editor/languages/constants.ts | 12 ++++ .../url_template_editor.tsx | 4 +- .../application/components/markdown_editor.js | 2 +- .../components/panel_config/markdown.tsx | 2 +- 9 files changed, 87 insertions(+), 55 deletions(-) create mode 100644 src/plugins/kibana_react/public/code_editor/code_editor_field.tsx create mode 100644 src/plugins/kibana_react/public/code_editor/languages/constants.ts diff --git a/packages/kbn-optimizer/limits.yml b/packages/kbn-optimizer/limits.yml index f331bb6dae44..acce34d2673e 100644 --- a/packages/kbn-optimizer/limits.yml +++ b/packages/kbn-optimizer/limits.yml @@ -104,7 +104,7 @@ pageLoadAssetSize: dataViews: 41532 expressions: 140958 fieldFormats: 65209 - kibanaReact: 99422 + kibanaReact: 84422 share: 71239 uiActions: 35121 dataEnhanced: 24980 diff --git a/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap b/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap index 3d4a9923fa8c..82d9183f3d39 100644 --- a/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap +++ b/src/plugins/discover/public/application/components/source_viewer/__snapshots__/source_viewer.test.tsx.snap @@ -594,9 +594,13 @@ exports[`Source Viewer component renders json code editor 1`] = ` fallback={} > - +
+ +
diff --git a/src/plugins/kibana_react/public/code_editor/code_editor.tsx b/src/plugins/kibana_react/public/code_editor/code_editor.tsx index 07bda19bf6d1..e0e2c4b3145c 100644 --- a/src/plugins/kibana_react/public/code_editor/code_editor.tsx +++ b/src/plugins/kibana_react/public/code_editor/code_editor.tsx @@ -14,6 +14,7 @@ import { monaco } from '@kbn/monaco'; import { i18n } from '@kbn/i18n'; import { FormattedMessage } from '@kbn/i18n/react'; import classNames from 'classnames'; +import './register_languages'; import { DARK_THEME, diff --git a/src/plugins/kibana_react/public/code_editor/code_editor_field.tsx b/src/plugins/kibana_react/public/code_editor/code_editor_field.tsx new file mode 100644 index 000000000000..0e6ab21159f1 --- /dev/null +++ b/src/plugins/kibana_react/public/code_editor/code_editor_field.tsx @@ -0,0 +1,39 @@ +/* + * 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 React from 'react'; +import darkTheme from '@elastic/eui/dist/eui_theme_dark.json'; +import lightTheme from '@elastic/eui/dist/eui_theme_light.json'; +import { EuiFormControlLayout } from '@elastic/eui'; +import { CodeEditor, Props } from './code_editor'; + +/** + * Renders a Monaco code editor in the same style as other EUI form fields. + */ +export const CodeEditorField: React.FunctionComponent = (props) => { + const { width, height, options, fullWidth, useDarkTheme } = props; + const theme = useDarkTheme ? darkTheme : lightTheme; + const style = { + width, + height, + backgroundColor: options?.readOnly + ? theme.euiFormBackgroundReadOnlyColor + : theme.euiFormBackgroundColor, + }; + + return ( + + ); +}; diff --git a/src/plugins/kibana_react/public/code_editor/index.tsx b/src/plugins/kibana_react/public/code_editor/index.tsx index f7f9c3b7fc8d..9aab815e2354 100644 --- a/src/plugins/kibana_react/public/code_editor/index.tsx +++ b/src/plugins/kibana_react/public/code_editor/index.tsx @@ -7,28 +7,31 @@ */ import React from 'react'; -import { - EuiDelayRender, - EuiErrorBoundary, - EuiLoadingContent, - EuiFormControlLayout, -} from '@elastic/eui'; -import darkTheme from '@elastic/eui/dist/eui_theme_dark.json'; -import lightTheme from '@elastic/eui/dist/eui_theme_light.json'; -import { useUiSetting } from '../ui_settings'; -import { Props } from './code_editor'; -import './register_languages'; +import { EuiDelayRender, EuiErrorBoundary, EuiLoadingContent } from '@elastic/eui'; -export * from './languages'; +import { useUiSetting } from '../ui_settings'; +import type { Props } from './code_editor'; + +export * from './languages/constants'; const LazyBaseEditor = React.lazy(() => import('./code_editor')); - -const Fallback = () => ( - - - +const LazyCodeEditorField = React.lazy(() => + import('./code_editor_field').then((m) => ({ default: m.CodeEditorField })) ); +const Fallback: React.FunctionComponent<{ height: Props['height'] }> = ({ height }) => { + return ( + <> + {/* when height is known, set minHeight to avoid layout shift */} +
+ + + +
+ + ); +}; + export type CodeEditorProps = Props; /** @@ -40,7 +43,7 @@ export const CodeEditor: React.FunctionComponent = (props) => { const darkMode = useUiSetting('theme:darkMode'); return ( - }> + }> @@ -51,38 +54,11 @@ export const CodeEditor: React.FunctionComponent = (props) => { * Renders a Monaco code editor in the same style as other EUI form fields. */ export const CodeEditorField: React.FunctionComponent = (props) => { - const { width, height, options, fullWidth } = props; const darkMode = useUiSetting('theme:darkMode'); - const theme = darkMode ? darkTheme : lightTheme; - const style = { - width, - height, - backgroundColor: options?.readOnly - ? theme.euiFormBackgroundReadOnlyColor - : theme.euiFormBackgroundColor, - }; - return ( - ); diff --git a/src/plugins/kibana_react/public/code_editor/languages/constants.ts b/src/plugins/kibana_react/public/code_editor/languages/constants.ts new file mode 100644 index 000000000000..af80e4ccc56e --- /dev/null +++ b/src/plugins/kibana_react/public/code_editor/languages/constants.ts @@ -0,0 +1,12 @@ +/* + * 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. + */ + +export { LANG as CssLang } from './css/constants'; +export { LANG as MarkdownLang } from './markdown/constants'; +export { LANG as YamlLang } from './yaml/constants'; +export { LANG as HandlebarsLang } from './handlebars/constants'; diff --git a/src/plugins/kibana_react/public/url_template_editor/url_template_editor.tsx b/src/plugins/kibana_react/public/url_template_editor/url_template_editor.tsx index 0fed4d37e4f7..c6250d73a889 100644 --- a/src/plugins/kibana_react/public/url_template_editor/url_template_editor.tsx +++ b/src/plugins/kibana_react/public/url_template_editor/url_template_editor.tsx @@ -66,7 +66,7 @@ export const UrlTemplateEditor: React.FC = ({ return; } - const { dispose } = monaco.languages.registerCompletionItemProvider(HandlebarsLang.ID, { + const { dispose } = monaco.languages.registerCompletionItemProvider(HandlebarsLang, { triggerCharacters: ['{', '/', '?', '&', '='], provideCompletionItems(model, position, context, token) { const { lineNumber } = position; @@ -124,7 +124,7 @@ export const UrlTemplateEditor: React.FC = ({ return (