Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(playground): add monaco-editor #490

Merged
merged 11 commits into from
Sep 25, 2024
Prev Previous commit
Next Next commit
feat: add extra libs (fail)
  • Loading branch information
aralroca committed Sep 25, 2024
commit 0f305b20b6c2eeddb5d3ca15a13eec3386463152
38 changes: 38 additions & 0 deletions packages/www/src/helpers/monaco-extra-libs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from 'node:path';
import fs from 'node:fs';
import { fileSystemRouter } from 'brisa/server';

/**
* TODO: It's not working yet ... We are missing some configuration after that
*/
export default function getMonacoEditorExtraLibs() {
const fileExtensions = ['.d.ts', '.json'];
const root = path.resolve(path.join(process.cwd(), '..', '..'));
const nodeModules = path.join(root, 'node_modules');
const brisaDep = fileSystemRouter({
dir: path.join(nodeModules, 'brisa'),
fileExtensions,
});
const cssType = fileSystemRouter({
dir: path.join(nodeModules, 'csstype'),
fileExtensions,
});
let extraLibs = '';

for (const [pathname] of brisaDep.routes) {
if (pathname.includes('node_modules') || pathname.includes('src')) continue;
const route = brisaDep.match(pathname);
const filePath = route.filePath.replace(root, '');
const fileRaw = fs.readFileSync(route.filePath, 'utf-8');
extraLibs += `monaco.languages.typescript.typescriptDefaults.addExtraLib(\`${fileRaw}\`, 'file://${filePath}');`;
}

for (const [pathname] of cssType.routes) {
const route = cssType.match(pathname);
const filePath = route.filePath.replace(root, '');
const fileRaw = fs.readFileSync(route.filePath, 'utf-8');
extraLibs += `monaco.languages.typescript.typescriptDefaults.addExtraLib(\`${fileRaw}\`, 'file://${filePath}');`;
}

return extraLibs;
}
2 changes: 1 addition & 1 deletion packages/www/src/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function PreviewLayout({ children }: { children: JSX.Element }) {
`)}
</script>
</head>
<body>{children}</body>
<body style={{ backgroundColor: 'white' }}>{children}</body>
</html>
);
}
Expand Down
20 changes: 13 additions & 7 deletions packages/www/src/pages/playground/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { dangerHTML } from 'brisa';
import getMonacoEditorExtraLibs from '@/helpers/monaco-extra-libs';

const defaultValue = `// src/web-components/wc-counter.tsx
import type { WebContext } from 'brisa';
Expand Down Expand Up @@ -44,19 +45,24 @@ export default function Playground() {
const existingModel = monaco.editor.getModels().find(m => m.uri.toString() === modelUri.toString());
const codeModel = existingModel ?? monaco.editor.createModel(\`${defaultValue}\`, "typescript", modelUri);

monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
jsx: monaco.languages.typescript.JsxEmit.React,
target: monaco.languages.typescript.ScriptTarget.ESNext,
allowNonTsExtensions: true,
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
jsx: "react",
target: monaco.languages.typescript.ScriptTarget.ES2020,
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.Classic,
allowNonTsExtensions: true
});

${getMonacoEditorExtraLibs()}

monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
noSemanticValidation: false,
noSyntaxValidation: false
});
const preview = document.querySelector('#preview-iframe');
const editor = monaco.editor.create(document.querySelector('#code-editor'), {
model: codeModel,
language: "typescript",
theme: document.body.classList.contains('dark') ? "vs-dark" : "vs-light",
automaticLayout: true
});
editor.setModel(codeModel);
editor.onDidChangeModelContent((e) => {
preview.contentWindow.postMessage({ code: editor.getValue() }, '*');
});
Expand Down