-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
466 additions
and
102 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -1,43 +1,87 @@ | ||
import { SandpackProvider, CodeEditor as TheEditorFromSandpack } from '@codesandbox/sandpack-react' | ||
import React from 'react' | ||
import styles from '../../styles/ui.module.css' | ||
import { CodeBlockEditorProps } from '../codeblock' | ||
import { useCodeBlockEditorContext } from '../codeblock/CodeBlockNode' | ||
import { readOnly$ } from '../core' | ||
import { useCodeMirrorRef } from '../sandpack/useCodeMirrorRef' | ||
import { useCellValues } from '@mdxeditor/gurx' | ||
import { codeMirrorTheme$ } from '.' | ||
|
||
import { EditorState, Extension } from '@codemirror/state' | ||
import { EditorView, lineNumbers } from '@codemirror/view' | ||
import { basicLight } from 'cm6-theme-basic-light' | ||
import { basicSetup } from 'codemirror' | ||
import { languages } from '@codemirror/language-data' | ||
import { useCodeMirrorRef } from '../sandpack/useCodeMirrorRef' | ||
import { codeMirrorAutoLoadLanguageSupport$, codeMirrorExtensions$ } from '.' | ||
|
||
export const COMMON_STATE_CONFIG_EXTENSIONS: Extension[] = [] | ||
|
||
export const CodeMirrorEditor = ({ language, nodeKey, code, focusEmitter }: CodeBlockEditorProps) => { | ||
const codeMirrorRef = useCodeMirrorRef(nodeKey, 'codeblock', 'jsx', focusEmitter) | ||
const [readOnly, theme] = useCellValues(readOnly$, codeMirrorTheme$) | ||
const [readOnly, codeMirrorExtensions, autoLoadLanguageSupport] = useCellValues( | ||
readOnly$, | ||
codeMirrorExtensions$, | ||
codeMirrorAutoLoadLanguageSupport$ | ||
) | ||
|
||
const codeMirrorRef = useCodeMirrorRef(nodeKey, 'codeblock', language, focusEmitter) | ||
const { setCode } = useCodeBlockEditorContext() | ||
const editorViewRef = React.useRef<EditorView | null>(null) | ||
const elRef = React.useRef<HTMLDivElement | null>(null) | ||
|
||
const setCodeRef = React.useRef(setCode) | ||
setCodeRef.current = setCode | ||
codeMirrorRef.current = { | ||
getCodemirror: () => editorViewRef.current! | ||
} | ||
|
||
React.useEffect(() => { | ||
codeMirrorRef.current?.getCodemirror()?.dom.addEventListener('paste', (e) => { | ||
e.stopPropagation() | ||
}) | ||
}, [codeMirrorRef, language]) | ||
void (async () => { | ||
const extensions = [ | ||
...codeMirrorExtensions, | ||
basicSetup, | ||
basicLight, | ||
lineNumbers(), | ||
EditorView.lineWrapping, | ||
EditorView.updateListener.of(({ state }) => { | ||
setCodeRef.current(state.doc.toString()) | ||
}) | ||
] | ||
if (readOnly) { | ||
extensions.push(EditorState.readOnly.of(true)) | ||
} | ||
if (language !== '' && autoLoadLanguageSupport) { | ||
const languageData = languages.find((l) => { | ||
return l.name === language || l.alias.includes(language) || l.extensions.includes(language) | ||
}) | ||
if (languageData) { | ||
try { | ||
const languageSupport = await languageData.load() | ||
extensions.push(languageSupport.extension) | ||
} catch (e) { | ||
console.warn('failed to load language support for', language) | ||
} | ||
} | ||
} | ||
elRef.current!.innerHTML = '' | ||
editorViewRef.current = new EditorView({ | ||
parent: elRef.current!, | ||
state: EditorState.create({ doc: code, extensions }) | ||
}) | ||
})() | ||
return () => { | ||
editorViewRef.current?.destroy() | ||
editorViewRef.current = null | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [readOnly, language]) | ||
|
||
return ( | ||
<div | ||
className={styles.sandpackWrapper} | ||
className={styles.codeMirrorWrapper} | ||
onKeyDown={(e) => { | ||
e.stopPropagation() | ||
}} | ||
> | ||
<SandpackProvider theme={theme}> | ||
<TheEditorFromSandpack | ||
readOnly={readOnly} | ||
showLineNumbers | ||
initMode="immediate" | ||
key={language} | ||
filePath={`file.${language || 'txt'}`} | ||
code={code} | ||
onCodeUpdate={setCode} | ||
ref={codeMirrorRef} | ||
/> | ||
</SandpackProvider> | ||
<div ref={elRef} /> | ||
</div> | ||
) | ||
} |
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
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