-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
1 parent
8a49d06
commit e84b96d
Showing
7 changed files
with
760 additions
and
11 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
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,7 +1,9 @@ | ||
import { Editor } from '@/components/Editor/Editor'; | ||
|
||
export default function Home() { | ||
return ( | ||
<main className="flex min-h-screen flex-col items-center justify-between p-24"> | ||
Home | ||
<main className="flex flex-col w-full h-screen"> | ||
<Editor /> | ||
</main> | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use client'; | ||
|
||
import { EditorView, basicSetup } from 'codemirror'; | ||
import { EditorState } from '@codemirror/state'; | ||
import { useEffect, useRef } from 'react'; | ||
import { | ||
oneDarkTheme, | ||
oneDarkHighlightStyle, | ||
} from '@codemirror/theme-one-dark'; | ||
import { json } from '@codemirror/lang-json'; | ||
import { yaml } from '@codemirror/lang-yaml'; | ||
import { syntaxHighlighting } from '@codemirror/language'; | ||
import { indentWithTab } from '@codemirror/commands'; | ||
import { keymap } from '@codemirror/view'; | ||
|
||
interface ICodeMirrorProps { | ||
language: 'json' | 'yaml'; | ||
value: string; | ||
onChange: (value: string) => void; | ||
readOnly?: boolean; | ||
autoFocus?: boolean; | ||
className?: string; | ||
} | ||
|
||
export const CodeMirror = (props: ICodeMirrorProps) => { | ||
const { language, value, onChange, autoFocus, className } = props; | ||
|
||
const editorRef = useRef<HTMLDivElement>(null); | ||
const editorViewRef = useRef<EditorView>(); | ||
|
||
useEffect(() => { | ||
let currentValue; | ||
if (window) { | ||
currentValue = localStorage.getItem('document') || value; | ||
onChange(currentValue); | ||
} | ||
|
||
if (editorRef.current) { | ||
const theme = EditorView.theme({ | ||
'&': { | ||
backgroundColor: '#252f3f', | ||
color: '#fff', | ||
}, | ||
|
||
'&.cm-editor': { | ||
height: '100%', | ||
width: '100%', | ||
}, | ||
|
||
'&.cm-focused': { | ||
backgroundColor: '#1f2a37', | ||
}, | ||
}); | ||
|
||
const editorState = EditorState.create({ | ||
doc: currentValue, | ||
extensions: [ | ||
basicSetup, | ||
theme, | ||
keymap.of([indentWithTab]), | ||
oneDarkTheme, | ||
syntaxHighlighting(oneDarkHighlightStyle), | ||
language === 'json' ? json() : yaml(), | ||
EditorView.updateListener.of((update) => { | ||
if (update.docChanged) { | ||
onChange(update.state.doc.toString()); | ||
|
||
if (window !== undefined) { | ||
localStorage.setItem('document', update.state.doc.toString()); | ||
} | ||
} | ||
|
||
return false; | ||
}), | ||
], | ||
}); | ||
|
||
editorViewRef.current = new EditorView({ | ||
parent: editorRef.current, | ||
state: editorState, | ||
}); | ||
|
||
if (autoFocus) { | ||
editorViewRef.current.focus(); | ||
} | ||
|
||
return () => { | ||
editorViewRef.current?.destroy(); | ||
}; | ||
} | ||
}, [language]); | ||
|
||
return ( | ||
<div | ||
ref={editorRef} | ||
className={`${className} flex-grow relative overflow-auto`} | ||
/> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use client' | ||
|
||
import { useFilesState } from '@/state/files.state'; | ||
import { CodeMirror } from './CodeMirror'; | ||
|
||
interface IEditorProps {} | ||
|
||
export const Editor = (props: IEditorProps) => { | ||
const { language, content } = useFilesState(state => state.files['asyncapi']); | ||
const handleUpdateFile = useFilesState(state => state.updateFile); | ||
|
||
return ( | ||
<div className="flex flex-1 overflow-hidden"> | ||
<CodeMirror language={language} value={content} onChange={value => handleUpdateFile('asyncapi', { content: value })} /> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.