Skip to content

Commit 06a3114

Browse files
RichieB2BDutchBen
authored andcommitted
Use monaco-editor in WfoJsonCodeBlock
1 parent 82d22ca commit 06a3114

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

.changeset/tango-orange-house.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@orchestrator-ui/orchestrator-ui-components': minor
3+
---
4+
5+
Use monaco-editor in WfoJsonCodeBlock

packages/orchestrator-ui-components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@elastic/eui": "101.3.0",
4242
"@emotion/css": "^11.11.2",
4343
"@emotion/react": "^11.11.4",
44+
"@monaco-editor/react": "^4.7.0",
4445
"@rtk-query/graphql-request-base-query": "^2.3.1",
4546
"graphql-request": "^6.1.0",
4647
"invariant": "^2.2.4",
Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import React, { FC } from 'react';
1+
import React, { FC, useCallback, useEffect, useState } from 'react';
22

3-
import { EuiCodeBlock } from '@elastic/eui';
3+
import { editor } from 'monaco-editor';
44

5-
import { useWithOrchestratorTheme } from '@/hooks';
5+
import Editor from '@monaco-editor/react';
6+
import type { Monaco } from '@monaco-editor/react';
7+
8+
import { useOrchestratorTheme, useWithOrchestratorTheme } from '@/hooks';
69

710
import { getStyles } from './styles';
811

@@ -11,6 +14,17 @@ export type WfoJsonCodeBlockProps = {
1114
isBasicStyle?: boolean;
1215
};
1316

17+
const WFO_THEME = {
18+
light: 'wfo-light',
19+
dark: 'wfo-dark',
20+
};
21+
22+
const MONACO_THEME: { light: editor.BuiltinTheme; dark: editor.BuiltinTheme } =
23+
{
24+
light: 'vs',
25+
dark: 'vs-dark',
26+
};
27+
1428
/**
1529
* WfoJsonCodeBlock is used to render objects in JSON format
1630
* @param data
@@ -21,19 +35,87 @@ export const WfoJsonCodeBlock: FC<WfoJsonCodeBlockProps> = ({
2135
data,
2236
isBasicStyle = false,
2337
}) => {
38+
const { theme, isDarkThemeActive } = useOrchestratorTheme();
2439
const { euiCodeBlockStyle, euiBasicCodeBlockStyle } =
2540
useWithOrchestratorTheme(getStyles);
2641

2742
const json = JSON.stringify(data, null, 4);
2843

44+
const [editorHeight, setEditorHeight] = useState(0);
45+
const [monacoInstance, setMonacoInstance] = useState<Monaco | undefined>(
46+
undefined,
47+
);
48+
49+
const addThemeToEditor = useCallback(
50+
(monaco: Monaco) => {
51+
monaco.editor.defineTheme(
52+
isDarkThemeActive ? WFO_THEME.dark : WFO_THEME.light,
53+
{
54+
base: isDarkThemeActive
55+
? MONACO_THEME.dark
56+
: MONACO_THEME.light,
57+
inherit: true,
58+
rules: [
59+
{
60+
token: '',
61+
foreground: theme.colors.textPrimary.replace(
62+
'#',
63+
'',
64+
),
65+
background:
66+
theme.colors.backgroundBasePlain.replace(
67+
'#',
68+
'',
69+
),
70+
},
71+
],
72+
colors: {
73+
'editor.foreground': theme.colors.textPrimary,
74+
'editor.background': theme.colors.backgroundBasePlain,
75+
},
76+
},
77+
);
78+
},
79+
[isDarkThemeActive, theme],
80+
);
81+
82+
function editorDidMount(
83+
editor: editor.IStandaloneCodeEditor,
84+
monaco: Monaco,
85+
) {
86+
const scrollHeight = editor.getScrollHeight();
87+
setMonacoInstance(monaco);
88+
setEditorHeight(Math.min(scrollHeight, 500));
89+
}
90+
91+
useEffect(() => {
92+
if (monacoInstance) {
93+
addThemeToEditor(monacoInstance);
94+
monacoInstance.editor.setTheme(
95+
isDarkThemeActive ? WFO_THEME.dark : WFO_THEME.light,
96+
);
97+
}
98+
}, [monacoInstance, isDarkThemeActive, addThemeToEditor, theme]);
99+
29100
return (
30-
<EuiCodeBlock
101+
<Editor
102+
height={editorHeight}
31103
css={isBasicStyle ? euiBasicCodeBlockStyle : euiCodeBlockStyle}
32-
isCopyable={true}
104+
theme={isDarkThemeActive ? WFO_THEME.dark : WFO_THEME.light}
105+
options={{
106+
readOnly: true,
107+
lineNumbers: isBasicStyle ? 'off' : 'on',
108+
scrollBeyondLastLine: false,
109+
contextmenu: false,
110+
minimap: { enabled: false },
111+
mouseWheelZoom: true,
112+
renderLineHighlight: 'none',
113+
fontFamily: theme.font.family,
114+
}}
115+
beforeMount={addThemeToEditor}
116+
onMount={editorDidMount}
33117
language="json"
34-
lineNumbers={!isBasicStyle}
35-
>
36-
{json}
37-
</EuiCodeBlock>
118+
value={json}
119+
/>
38120
);
39121
};

0 commit comments

Comments
 (0)