-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
object-editor.tsx
144 lines (134 loc) · 6.17 KB
/
object-editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {languages} from 'monaco-editor/esm/vs/editor/editor.api';
import * as React from 'react';
import {createRef, useEffect, useState} from 'react';
import MonacoEditor from 'react-monaco-editor';
import {uiUrl} from '../../base';
import {ScopedLocalStorage} from '../../scoped-local-storage';
import {Button} from '../button';
import {parse, stringify} from '../object-parser';
import {PhaseIcon} from '../phase-icon';
interface Props<T> {
type?: string;
value: T;
buttons?: React.ReactNode;
onChange?: (value: T) => void;
}
const defaultLang = 'yaml';
export const ObjectEditor = <T extends any>({type, value, buttons, onChange}: Props<T>) => {
const storage = new ScopedLocalStorage('object-editor');
const [error, setError] = useState<Error>();
const [lang, setLang] = useState<string>(storage.getItem('lang', defaultLang));
const [text, setText] = useState<string>(stringify(value, lang));
useEffect(() => storage.setItem('lang', lang, defaultLang), [lang]);
useEffect(() => setText(stringify(value, lang)), [value]);
useEffect(() => setText(stringify(parse(text), lang)), [lang]);
useEffect(() => {
// we ONLY want to change the text, if the normalized version has changed, this prevents white-space changes
// from resulting in a significant change
const editorText = stringify(parse(editor.current.editor.getValue()), lang);
const editorLang = editor.current.editor.getValue().startsWith('{') ? 'json' : 'yaml';
if (text !== editorText || lang !== editorLang) {
editor.current.editor.setValue(stringify(parse(text), lang));
}
}, [text, lang]);
useEffect(() => {
if (type && lang === 'json') {
const uri = uiUrl('assets/jsonschema/schema.json');
fetch(uri)
.then(res => res.json())
.then(swagger => {
// adds auto-completion to JSON only
languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemas: [
{
uri,
fileMatch: ['*'],
schema: {
$id: 'http://workflows.argoproj.io/' + type + '.json',
$ref: '#/definitions/' + type,
$schema: 'http://json-schema.org/draft-07/schema',
definitions: swagger.definitions
}
}
]
});
})
.catch(setError);
}
}, [lang, type]);
const editor = createRef<MonacoEditor>();
// this calculation is rough, it is probably hard to work for for every case, essentially it is:
// some pixels above and below for buttons, plus a bit of a buffer/padding
const height = Math.max(600, window.innerHeight * 0.9 - 250);
return (
<>
<div style={{paddingBottom: '1em'}}>
<Button outline={true} onClick={() => setLang(lang === 'yaml' ? 'json' : 'yaml')}>
<span style={{fontWeight: lang === 'json' ? 'bold' : 'normal'}}>JSON</span>/<span style={{fontWeight: lang === 'yaml' ? 'bold' : 'normal'}}>YAML</span>
</Button>
{Object.keys(value).map(x => (
<Button
key={x}
icon='caret-right'
outline={true}
onClick={() => {
// Attempt to move the correct section of the document. Ideally, we'd have the line at the top of the
// editor, but Monaco editor does not have method for this (e.g. `revealLineAtTop`).
// find the line for the section in either YAML or JSON
const index = text.split('\n').findIndex((y, i) => (lang === 'yaml' ? y.startsWith(x + ':') : y.includes('"' + x + '":')));
if (index >= 0) {
const lineNumber = index + 1;
editor.current.editor.revealLineInCenter(lineNumber);
editor.current.editor.setPosition({lineNumber, column: 0});
editor.current.editor.focus();
}
}}>
{x}
</Button>
))}
{buttons}
</div>
<div>
<MonacoEditor
ref={editor}
key='editor'
defaultValue={text}
language={lang}
height={height + 'px'}
options={{
readOnly: onChange === null,
minimap: {enabled: false},
lineNumbers: 'off',
guides: {
indentation: false
},
scrollBeyondLastLine: true
}}
onChange={v => {
if (onChange) {
try {
onChange(parse(v));
setError(null);
} catch (e) {
setError(e);
}
}
}}
/>
</div>
{error && (
<div style={{paddingTop: '1em'}}>
<PhaseIcon value='Error' /> {error.message}
</div>
)}
{onChange && (
<div>
<i className='fa fa-info-circle' />{' '}
{lang === 'json' ? <>Full auto-completion enabled.</> : <>Basic completion for YAML. Switch to JSON for full auto-completion.</>}{' '}
<a href='https://argoproj.github.io/argo-workflows/ide-setup/'>Learn how to get auto-completion in your IDE.</a>
</div>
)}
</>
);
};