-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Console] Migrate output editor to Monaco
- Loading branch information
1 parent
a2b100c
commit 9d19377
Showing
6 changed files
with
124 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/plugins/console/public/application/containers/editor/monaco/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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 { MonacoEditor } from './monaco_editor'; | ||
export { MonacoEditorOutput } from './monaco_editor_output'; |
70 changes: 70 additions & 0 deletions
70
src/plugins/console/public/application/containers/editor/monaco/monaco_editor_output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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, { FunctionComponent, useEffect, useState } from 'react'; | ||
import { CodeEditor } from '@kbn/code-editor'; | ||
import { css } from '@emotion/react'; | ||
import { VectorTile } from '@mapbox/vector-tile'; | ||
import Protobuf from 'pbf'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { EuiScreenReaderOnly } from '@elastic/eui'; | ||
import { useEditorReadContext, useRequestReadContext } from '../../../contexts'; | ||
import { convertMapboxVectorTileToJson } from '../legacy/console_editor/mapbox_vector_tile'; | ||
import { isJSONContentType, isMapboxVectorTile, safeExpandLiteralStrings } from '../utilities'; | ||
|
||
export const MonacoEditorOutput: FunctionComponent = () => { | ||
const { settings: readOnlySettings } = useEditorReadContext(); | ||
const { | ||
lastResult: { data }, | ||
} = useRequestReadContext(); | ||
const [value, setValue] = useState(''); | ||
|
||
useEffect(() => { | ||
if (data) { | ||
setValue( | ||
data | ||
.map((result) => { | ||
const { value: newValue, contentType } = result.response; | ||
|
||
let editorOutput; | ||
if (readOnlySettings.tripleQuotes && isJSONContentType(contentType)) { | ||
editorOutput = safeExpandLiteralStrings(newValue as string); | ||
} else if (isMapboxVectorTile(contentType)) { | ||
const vectorTile = new VectorTile(new Protobuf(newValue as ArrayBuffer)); | ||
const vectorTileJson = convertMapboxVectorTileToJson(vectorTile); | ||
editorOutput = safeExpandLiteralStrings(vectorTileJson as string); | ||
} else { | ||
editorOutput = newValue; | ||
} | ||
|
||
return editorOutput; | ||
}) | ||
.join('\n') | ||
); | ||
} else { | ||
setValue(''); | ||
} | ||
}, [readOnlySettings, data, value]); | ||
|
||
return ( | ||
<div | ||
css={css` | ||
width: 100%; | ||
`} | ||
> | ||
<EuiScreenReaderOnly> | ||
<label htmlFor={'ConAppOutputTextarea'}> | ||
{i18n.translate('console.outputTextarea', { | ||
defaultMessage: 'Dev Tools Console output', | ||
})} | ||
</label> | ||
</EuiScreenReaderOnly> | ||
<CodeEditor languageId={'json'} value={value} fullWidth={true} /> | ||
</div> | ||
); | ||
}; |
9 changes: 9 additions & 0 deletions
9
src/plugins/console/public/application/containers/editor/utilities/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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 * from './output_data'; |
26 changes: 26 additions & 0 deletions
26
src/plugins/console/public/application/containers/editor/utilities/output_data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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 { expandLiteralStrings } from '../../../../shared_imports'; | ||
|
||
export const isJSONContentType = (contentType?: string) => | ||
Boolean(contentType && contentType.indexOf('application/json') >= 0); | ||
|
||
export const isMapboxVectorTile = (contentType?: string) => | ||
contentType?.includes('application/vnd.mapbox-vector-tile') ?? false; | ||
|
||
/** | ||
* Best effort expand literal strings | ||
*/ | ||
export const safeExpandLiteralStrings = (data: string): string => { | ||
try { | ||
return expandLiteralStrings(data); | ||
} catch (e) { | ||
return data; | ||
} | ||
}; |