-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResizableLayoutEditor.jsx
114 lines (99 loc) · 2.92 KB
/
ResizableLayoutEditor.jsx
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
import React from 'react';
import MonacoEditor, { DiffEditor } from '@monaco-editor/react';
import GraphqlPlayground from '../../Playground';
const editorOptions = {
wordWrap: 'on',
minimap: { enabled: false },
folding: false,
lineNumberMinChars: 3,
scrollBeyondLastLine: true,
automaticLayout: true,
formatOnPaste: true,
definitionLinkOpensInPeek: true,
font: 'serif',
};
const getEditorOptions = (options, readOnly) => ({
...options,
readOnly,
});
const getDiffEditorOptions = (options, readOnly) => ({
...options,
readOnly,
});
const getDefaultValues = (launchPadDefault, context, original) => launchPadDefault || context || original;
const ResizableLayoutEditor = ({
fileName,
diffView,
launchPadDefaultSchema,
contextSchema,
originalSQLCode,
launchPadDefaultCode,
contextCode,
originalIndexingCode,
schema,
indexingCode,
isCreateNewIndexer,
onMount,
onChangeSchema,
onChangeCode,
}) => {
const determineEditorProps = () => {
const isSchemaEditor = fileName === 'schema.sql';
const isCodeEditor = fileName === 'indexer.js';
if (!isSchemaEditor && !isCodeEditor) return null;
const defaultValue = isSchemaEditor
? getDefaultValues(launchPadDefaultSchema, contextSchema, originalSQLCode)
: getDefaultValues(launchPadDefaultCode, contextCode, originalIndexingCode);
const value = isSchemaEditor ? schema : indexingCode;
const readOnly = isSchemaEditor && !isCreateNewIndexer;
return {
editorProps: {
onMount,
options: getEditorOptions(editorOptions, readOnly),
defaultValue,
value,
readOnly,
onChange: isSchemaEditor ? onChangeSchema : onChangeCode,
language: isSchemaEditor ? 'sql' : 'typescript',
},
diffProps: {
original: defaultValue,
modified: value,
language: isSchemaEditor ? 'sql' : 'typescript',
readOnly,
options: getDiffEditorOptions(editorOptions, readOnly),
},
};
};
const renderEditor = () => {
if (fileName === 'GraphiQL') return <GraphqlPlayground />;
const { editorProps, diffProps } = determineEditorProps();
if (diffView) {
return (
<DiffEditor
key={`${fileName}-diff`}
original={diffProps.original}
modified={diffProps.modified}
language={diffProps.language}
onMount={editorProps.onMount}
options={diffProps.options}
theme="vs-dark"
/>
);
}
return (
<MonacoEditor
key={`${fileName}-editor`}
value={editorProps.value}
defaultValue={editorProps.defaultValue}
defaultLanguage={editorProps.language}
onMount={editorProps.onMount}
onChange={editorProps.onChange}
options={editorProps.options}
theme="vs-dark"
/>
);
};
return <div className="h-[85vh]">{renderEditor()}</div>;
};
export default React.memo(ResizableLayoutEditor);