-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
further down the codemirror rabbit hole
- Loading branch information
Showing
4 changed files
with
101 additions
and
47 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,31 @@ | ||
import { useRef, useEffect, useState } from 'react'; | ||
|
||
import { basicSetup } from 'codemirror'; | ||
|
||
import { EditorState} from '@codemirror/state'; | ||
import { EditorView, keymap } from '@codemirror/view'; | ||
import { defaultKeymap, indentWithTab } from '@codemirror/commands'; | ||
import { javascript } from '@codemirror/lang-javascript'; | ||
|
||
import {lightTheme} from "@/codemirror-themes" | ||
|
||
export const Editor = (/*{ setEditorState }*/) => { | ||
const editor = useRef<HTMLDivElement | null>(null); | ||
const [code, setCode] = useState(''); | ||
|
||
const onUpdate = EditorView.updateListener.of((v) => { | ||
setCode(v.state.doc.toString()); | ||
}); | ||
|
||
useEffect(() => { | ||
const state = EditorState.create({ | ||
doc: code, | ||
extensions: [ | ||
basicSetup, | ||
keymap.of([...defaultKeymap, indentWithTab]), | ||
lightTheme, | ||
javascript(), | ||
onUpdate, | ||
], | ||
}); | ||
|
||
if(editor.current === null) { | ||
return; | ||
} | ||
|
||
const view = new EditorView({ state, parent: editor.current }); | ||
|
||
return () => { | ||
view.destroy(); | ||
}; | ||
}, [editor.current]); | ||
|
||
return <div ref={editor}></div>; | ||
}; | ||
import CodeMirror from "@uiw/react-codemirror"; | ||
import { javascript } from "@codemirror/lang-javascript"; | ||
import { lightTheme } from "@/codemirror-themes"; | ||
import { useCell } from "@/hooks/useCell"; | ||
|
||
import { cn } from "@/lib/utils" | ||
|
||
|
||
export const Editor = ({ cellID, className }: { cellID: string, className?: string }) => { | ||
const { content, updateCell } = useCell(cellID); | ||
|
||
const bigclasses = cn( | ||
"flex w-full h-full border border-input bg-transparent text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50", | ||
className | ||
) | ||
|
||
return ( | ||
<CodeMirror | ||
value={content} | ||
width="100%" | ||
height="100%" | ||
// Revert to CodeMirror's default for Accessibility -- not indenting with tab. | ||
// We can bring it back if we put escape in place | ||
indentWithTab={false} | ||
extensions={[javascript({ jsx: true })]} | ||
onChange={updateCell} | ||
className={bigclasses} | ||
theme={lightTheme} | ||
/> | ||
); | ||
}; |