|
| 1 | +"use client"; |
| 2 | + |
| 3 | +/* |
| 4 | +const mockModule = { hello: 'world' }; |
| 5 | +const require = (moduleName) => { |
| 6 | + if (moduleName === 'mock-module') return mockModule; |
| 7 | + throw new Error(`Module '${moduleName}' not found`); |
| 8 | +}; |
| 9 | +
|
| 10 | +const someVariable = 42; |
| 11 | +
|
| 12 | +import tailwindcssUtopia from "https://esm.sh/tailwindcss-utopia@0.0.3"; |
| 13 | +
|
| 14 | +@type {import('tailwindcss').Config} |
| 15 | +
|
| 16 | +export default { |
| 17 | + tailwindcssUtopia, |
| 18 | + someVariable, |
| 19 | + mockModule: require('mock-module'), |
| 20 | +}; |
| 21 | +
|
| 22 | +*/ |
| 23 | + |
| 24 | +import { useEffect, useRef, useState } from "react"; |
| 25 | +import * as monaco from "monaco-editor"; |
| 26 | +import { Editor as MonacoEditor, loader } from "@monaco-editor/react"; |
| 27 | +import { |
| 28 | + configureMonacoTailwindcss, |
| 29 | + MonacoTailwindcss, |
| 30 | + TailwindConfig, |
| 31 | + tailwindcssData, |
| 32 | +} from "monaco-tailwindcss"; |
| 33 | +import { css, generateSrcDoc, html, js } from "@/lib/utils"; |
| 34 | + |
| 35 | +type Refs = Parameters< |
| 36 | + NonNullable<React.ComponentProps<typeof MonacoEditor>["onMount"]> |
| 37 | +>; |
| 38 | +type EditorRef = Refs[0]; |
| 39 | +type MonacoEditorRef = Refs[1]; |
| 40 | + |
| 41 | +loader.config({ monaco }); |
| 42 | + |
| 43 | +export default function Editor({ |
| 44 | + activeTab, |
| 45 | + onChange, |
| 46 | +}: { |
| 47 | + activeTab: "html" | "css" | "config"; |
| 48 | + onChange: (content: string) => void; |
| 49 | +}) { |
| 50 | + const editorRef = useRef<EditorRef | null>(null); |
| 51 | + const monacoEditorRef = useRef<MonacoEditorRef | null>(null); |
| 52 | + |
| 53 | + const [tabs] = useState(() => ({ |
| 54 | + html: monaco.editor.createModel( |
| 55 | + html`<h1>Heading H1</h1> |
| 56 | +<h2>Heading H2</h2> |
| 57 | +<h3>Heading H3</h3> |
| 58 | +<h4>Heading H4</h4> |
| 59 | +<h5>Heading H5</h5> |
| 60 | +<h6>Heading H6</h6> |
| 61 | +`, |
| 62 | + "html", |
| 63 | + ), |
| 64 | + css: monaco.editor.createModel( |
| 65 | + css`@tailwind base; |
| 66 | +@tailwind components; |
| 67 | +@tailwind utilities; |
| 68 | +
|
| 69 | +h1, |
| 70 | +h2, |
| 71 | +h3, |
| 72 | +h4, |
| 73 | +h5, |
| 74 | +h6 { |
| 75 | + @apply font-bold; |
| 76 | +} |
| 77 | +
|
| 78 | +h1 { |
| 79 | + @apply ~text-x5; |
| 80 | +} |
| 81 | +
|
| 82 | +h2 { |
| 83 | + @apply ~text-x4; |
| 84 | +} |
| 85 | +
|
| 86 | +h3 { |
| 87 | + @apply ~text-x3; |
| 88 | +} |
| 89 | +
|
| 90 | +h4 { |
| 91 | + @apply ~text-x2; |
| 92 | +} |
| 93 | +
|
| 94 | +h5 { |
| 95 | + @apply ~text-x1; |
| 96 | +} |
| 97 | +
|
| 98 | +body { |
| 99 | + @apply ~text-1; |
| 100 | +} |
| 101 | +`, |
| 102 | + "css", |
| 103 | + ), |
| 104 | + config: monaco.editor.createModel(js`export default { |
| 105 | + theme: {}, |
| 106 | + plugins: [], /* plugins should be empty */ |
| 107 | +}; |
| 108 | +`, "typescript"), |
| 109 | + })); |
| 110 | + |
| 111 | + const monacoTailwindcssRef = useRef<MonacoTailwindcss>(null); |
| 112 | + |
| 113 | + const handleChange = async () => { |
| 114 | + console.log('a'); |
| 115 | + if (!monacoTailwindcssRef.current) return; |
| 116 | + console.log('b'); |
| 117 | + |
| 118 | + const css = await monacoTailwindcssRef.current.generateStylesFromContent( |
| 119 | + tabs.css.getValue(), |
| 120 | + [{ content: tabs.html.getValue() }], |
| 121 | + ); |
| 122 | + const html = tabs.html.getValue(); |
| 123 | + // const config = tabs.config.getValue(); |
| 124 | + |
| 125 | + const content = generateSrcDoc(html, css); |
| 126 | + |
| 127 | + onChange(content); |
| 128 | + }; |
| 129 | + |
| 130 | + useEffect(() => { |
| 131 | + const onResize = () => { |
| 132 | + if (!editorRef.current) return; |
| 133 | + |
| 134 | + editorRef.current.layout(); |
| 135 | + }; |
| 136 | + |
| 137 | + window.addEventListener("resize", onResize); |
| 138 | + |
| 139 | + return () => { |
| 140 | + window.removeEventListener("resize", onResize); |
| 141 | + }; |
| 142 | + }, []); |
| 143 | + |
| 144 | + useEffect(() => { |
| 145 | + tabs["config"].onDidChangeContent(() => { |
| 146 | + const code = tabs["config"].getValue(); |
| 147 | + |
| 148 | + let evaluatedCode; |
| 149 | + |
| 150 | + const blob = new Blob([code], { type: "application/javascript" }); |
| 151 | + const url = URL.createObjectURL(blob); |
| 152 | + |
| 153 | + import(/* webpackIgnore: true */ url) |
| 154 | + .then((module) => { |
| 155 | + evaluatedCode = module.default; |
| 156 | + |
| 157 | + monacoTailwindcssRef.current?.setTailwindConfig( |
| 158 | + evaluatedCode as TailwindConfig, |
| 159 | + ); |
| 160 | + }) |
| 161 | + .catch((error) => { |
| 162 | + console.error("Error evaluating code:", error.message); |
| 163 | + evaluatedCode = null; |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + Object.values(tabs).map((tab) => { |
| 168 | + tab.onDidChangeContent(handleChange); |
| 169 | + }); |
| 170 | + }, []); |
| 171 | + |
| 172 | + useEffect(() => { |
| 173 | + if (!editorRef.current) return; |
| 174 | + |
| 175 | + editorRef.current.setModel(tabs[activeTab]); |
| 176 | + }, [editorRef, tabs, activeTab]); |
| 177 | + |
| 178 | + return ( |
| 179 | + <MonacoEditor |
| 180 | + beforeMount={() => { |
| 181 | + monaco.languages.css.cssDefaults.setOptions({ |
| 182 | + data: { |
| 183 | + dataProviders: { |
| 184 | + tailwindcssData, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }); |
| 188 | + |
| 189 | + monacoTailwindcssRef.current = configureMonacoTailwindcss(monaco); |
| 190 | + }} |
| 191 | + onMount={(editor, monaco) => { |
| 192 | + editorRef.current = editor; |
| 193 | + monacoEditorRef.current = monaco; |
| 194 | + |
| 195 | + editor.setModel(tabs[activeTab]); |
| 196 | + |
| 197 | + handleChange(); |
| 198 | + }} |
| 199 | + /> |
| 200 | + ); |
| 201 | +} |
0 commit comments