|
1 | | -import React, {useCallback, useEffect, useRef} from 'react' |
2 | | -import AceEditor from 'react-ace' |
| 1 | +import React, {Suspense, useCallback, useEffect, useRef} from 'react' |
3 | 2 | import styled from 'styled-components' |
4 | 3 | import {Box} from '@sanity/ui' |
5 | 4 | import {ACE_EDITOR_PROPS, ACE_SET_OPTIONS} from './config' |
6 | 5 | import createHighlightMarkers from './createHighlightMarkers' |
7 | 6 | import {CodeInputType, CodeInputValue} from './types' |
8 | | -import './ace-editor/editorSupport' |
| 7 | +import {useAceEditor} from './ace-editor/AceEditorLazy' |
9 | 8 |
|
10 | 9 | const PreviewContainer = styled(Box)` |
11 | 10 | position: relative; |
@@ -54,34 +53,39 @@ export default function PreviewCode(props: PreviewCodeProps) { |
54 | 53 | const fixedLanguage = type?.options?.language |
55 | 54 |
|
56 | 55 | const mode = value?.language || fixedLanguage || 'text' |
| 56 | + const AceEditor = useAceEditor() |
57 | 57 |
|
58 | 58 | return ( |
59 | 59 | <PreviewContainer> |
60 | 60 | <PreviewInner padding={4}> |
61 | | - <AceEditor |
62 | | - ref={aceEditorRef} |
63 | | - focus={false} |
64 | | - mode={mode} |
65 | | - theme="monokai" |
66 | | - width="100%" |
67 | | - onChange={handleEditorChange} |
68 | | - maxLines={200} |
69 | | - readOnly |
70 | | - wrapEnabled |
71 | | - showPrintMargin={false} |
72 | | - highlightActiveLine={false} |
73 | | - cursorStart={-1} |
74 | | - value={(value && value.code) || ''} |
75 | | - markers={ |
76 | | - value && value.highlightedLines |
77 | | - ? createHighlightMarkers(value.highlightedLines) |
78 | | - : undefined |
79 | | - } |
80 | | - tabSize={2} |
81 | | - showGutter={false} |
82 | | - setOptions={ACE_SET_OPTIONS} |
83 | | - editorProps={ACE_EDITOR_PROPS} |
84 | | - /> |
| 61 | + {AceEditor && ( |
| 62 | + <Suspense fallback={<div>Loading code editor...</div>}> |
| 63 | + <AceEditor |
| 64 | + ref={aceEditorRef} |
| 65 | + focus={false} |
| 66 | + mode={mode} |
| 67 | + theme="monokai" |
| 68 | + width="100%" |
| 69 | + onChange={handleEditorChange} |
| 70 | + maxLines={200} |
| 71 | + readOnly |
| 72 | + wrapEnabled |
| 73 | + showPrintMargin={false} |
| 74 | + highlightActiveLine={false} |
| 75 | + cursorStart={-1} |
| 76 | + value={(value && value.code) || ''} |
| 77 | + markers={ |
| 78 | + value && value.highlightedLines |
| 79 | + ? createHighlightMarkers(value.highlightedLines) |
| 80 | + : undefined |
| 81 | + } |
| 82 | + tabSize={2} |
| 83 | + showGutter={false} |
| 84 | + setOptions={ACE_SET_OPTIONS} |
| 85 | + editorProps={ACE_EDITOR_PROPS} |
| 86 | + /> |
| 87 | + </Suspense> |
| 88 | + )} |
85 | 89 | </PreviewInner> |
86 | 90 | </PreviewContainer> |
87 | 91 | ) |
|
0 commit comments