-
-
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.
- Loading branch information
Showing
36 changed files
with
1,839 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"importOrder": [ | ||
"^node:", | ||
"^react", | ||
"^next", | ||
"<THIRD_PARTY_MODULES>", | ||
"^\\~icons/", | ||
"^\\@/", | ||
|
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
138 changes: 138 additions & 0 deletions
138
packages/jsrepl/src/app/repl/components/code-editor.tsx
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,138 @@ | ||
import React, { useEffect, useMemo, useRef, useState } from 'react' | ||
import { useTheme } from 'next-themes' | ||
import * as monaco from 'monaco-editor' | ||
import { loadMonacoTheme } from '@/lib/monaco-themes' | ||
import { PrettierFormattingProvider } from '@/lib/prettier-formatting-provider' | ||
import { Themes } from '@/lib/themes' | ||
import { cn } from '@/lib/utils' | ||
import { Theme } from '@/types' | ||
|
||
setupMonaco() | ||
setupTailwindCSS() | ||
|
||
export default function CodeEditor() { | ||
const containerRef = useRef<HTMLDivElement>(null) | ||
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null) | ||
const [isThemeLoaded, setIsThemeLoaded] = useState(false) | ||
const { resolvedTheme } = useTheme() | ||
const themeId = resolvedTheme as Theme | ||
|
||
const theme = useMemo(() => Themes.find((theme) => theme.id === themeId) ?? Themes[0], [themeId]) | ||
|
||
const editorInitialOptions = useRef<monaco.editor.IStandaloneEditorConstructionOptions>({ | ||
value: '<div class="bg-red-500 ml-4">Hello World</div>', | ||
language: 'html', | ||
|
||
automaticLayout: true, | ||
padding: { top: 20, bottom: 20 }, | ||
// TODO: make it configurable | ||
fontSize: 16, | ||
minimap: { enabled: false }, | ||
theme: theme.id, | ||
quickSuggestions: { | ||
other: true, | ||
comments: true, | ||
|
||
// Tailwind CSS intellisense autosuggestion in TSX (otherwise it works only by pressing Ctrl+Space manually, unlike in html/css). | ||
strings: true, | ||
}, | ||
tabSize: 2, | ||
// TODO: make it configurable | ||
renderLineHighlight: 'none', | ||
scrollBeyondLastLine: false, | ||
}) | ||
|
||
useEffect(() => { | ||
loadMonacoTheme(theme).then(() => { | ||
editorRef.current?.updateOptions({ theme: theme.id }) | ||
setIsThemeLoaded(true) | ||
}) | ||
}, [theme]) | ||
|
||
useEffect(() => { | ||
const editor = monaco.editor.create(containerRef.current!, editorInitialOptions.current) | ||
editorRef.current = editor | ||
|
||
return () => { | ||
editor.dispose() | ||
} | ||
}, []) | ||
|
||
return ( | ||
<> | ||
<div className={cn('h-96 w-full', { 'opacity-0': !isThemeLoaded })} ref={containerRef} /> | ||
</> | ||
) | ||
} | ||
|
||
function setupMonaco() { | ||
self.MonacoEnvironment = { | ||
getWorker(workerId, label) { | ||
switch (label) { | ||
case 'json': | ||
return new Worker( | ||
new URL('monaco-editor/esm/vs/language/json/json.worker', import.meta.url) | ||
) | ||
case 'css': | ||
case 'scss': | ||
case 'less': | ||
return new Worker( | ||
new URL('monaco-editor/esm/vs/language/css/css.worker', import.meta.url) | ||
) | ||
case 'html': | ||
case 'handlebars': | ||
case 'razor': | ||
return new Worker( | ||
new URL('monaco-editor/esm/vs/language/html/html.worker', import.meta.url) | ||
) | ||
case 'typescript': | ||
case 'javascript': | ||
return new Worker( | ||
new URL('monaco-editor/esm/vs/language/typescript/ts.worker', import.meta.url) | ||
) | ||
case 'tailwindcss': | ||
return new Worker(new URL('@/lib/monaco-tailwindcss.worker', import.meta.url)) | ||
default: | ||
return new Worker(new URL('monaco-editor/esm/vs/editor/editor.worker', import.meta.url)) | ||
} | ||
}, | ||
} | ||
|
||
monaco.editor.addKeybindingRules([ | ||
{ | ||
keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, | ||
command: 'editor.action.formatDocument', | ||
when: 'editorHasDocumentFormattingProvider && editorTextFocus && !editorReadonly', | ||
}, | ||
]) | ||
|
||
const prettierFormattingProvider = new PrettierFormattingProvider() | ||
monaco.languages.registerDocumentFormattingEditProvider( | ||
[ | ||
{ language: 'typescript', exclusive: true }, | ||
{ language: 'javascript', exclusive: true }, | ||
{ language: 'html', exclusive: true }, | ||
{ language: 'css', exclusive: true }, | ||
], | ||
prettierFormattingProvider | ||
) | ||
|
||
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ | ||
validate: true, | ||
allowComments: true, | ||
trailingCommas: 'ignore', | ||
// schemas: [], | ||
}) | ||
} | ||
|
||
async function setupTailwindCSS() { | ||
const { tailwindcssData } = await import('@nag5000/monaco-tailwindcss') | ||
|
||
monaco.languages.css.cssDefaults.setOptions({ | ||
data: { | ||
dataProviders: { | ||
tailwindcssData, | ||
}, | ||
}, | ||
}) | ||
} |
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,18 @@ | ||
import Link from 'next/link' | ||
import Logo from '@/components/logo' | ||
|
||
export default function HeaderBase({ children }: { children?: React.ReactNode }) { | ||
return ( | ||
<header className="h-repl-header flex items-stretch gap-2 border-b px-2 leading-[calc(theme(height.repl-header)-1px)]"> | ||
<Link | ||
href="/" | ||
className="mr-24 inline-flex items-center gap-2 text-lg font-light tracking-wide max-[840px]:mr-6" | ||
> | ||
<Logo width="1.25rem" height="1.25rem" /> | ||
<span className="text-foreground/80">JSREPL</span> | ||
</Link> | ||
|
||
{children} | ||
</header> | ||
) | ||
} |
Oops, something went wrong.