Unverified Commit 5610af01 authored by Elliot Hesp's avatar Elliot Hesp Committed by GitHub

Add monaco-editor example (#11232)

parent 71474795
# Example app with Monaco Editor
This example adds support for [Monaco Editor](https://github.com/Microsoft/monaco-editor) integration using the
[`react-monaco-editor`](https://github.com/react-monaco-editor/react-monaco-editor) library.
## Deploy your own
Deploy the example using [ZEIT Now](https://zeit.co/now):
[![Deploy with ZEIT Now](https://zeit.co/button)](https://zeit.co/import/project?template=https://github.com/zeit/next.js/tree/canary/examples/with-monaco-editor)
## How to use
### Using `create-next-app`
Execute [`create-next-app`](https://github.com/zeit/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
```bash
npx create-next-app --example with-monaco-editor with-monaco-editor-app
# or
yarn create next-app --example with-monaco-editor with-monaco-editor-app
```
### Download manually
Download the example:
```bash
curl https://codeload.github.com/zeit/next.js/tar.gz/canary | tar -xz --strip=2 next.js-canary/examples/with-monaco-editor
cd with-monaco-editor
```
Install it and run:
```bash
npm install
npm run dev
# or
yarn
yarn dev
```
Deploy it to the cloud with [ZEIT Now](https://zeit.co/import?filter=next.js&utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
export default `class Animal {
constructor(public name: string) { }
move(meters: number) {
console.log(this.name + " moved " + meters + "m.");
}
}
class Snake extends Animal {
move() {
console.log("Slithering...");
super.move(5);
}
}
class Horse extends Animal {
move() {
console.log("Galloping...");
super.move(45);
}
}
var sam = new Snake("Sammy the Python")
var tom: Animal = new Horse("Tommy the Palomino")
sam.move()
tom.move(34)`
const withCSS = require('@zeit/next-css')
const withFonts = require('next-fonts')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
module.exports = withCSS(
withFonts({
webpack: config => {
config.plugins.push(
new MonacoWebpackPlugin({
// Add languages as needed...
languages: ['javascript', 'typescript'],
filename: 'static/[name].worker.js',
})
)
return config
},
})
)
{
"name": "with-monaco-editor",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
},
"dependencies": {
"@zeit/next-css": "^1.0.1",
"monaco-editor": "^0.20.0",
"monaco-editor-webpack-plugin": "^1.9.0",
"next": "9.3.1",
"next-fonts": "^1.0.3",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-monaco-editor": "^0.34.0"
}
}
import React from 'react'
import dynamic from 'next/dynamic'
import sample from '../code-sample'
const MonacoEditor = dynamic(import('react-monaco-editor'), { ssr: false })
function IndexPage() {
return (
<MonacoEditor
height={'600px'}
language="typescript"
theme="vs-dark"
value={sample}
onChange={console.log}
editorDidMount={() => {
window.MonacoEnvironment.getWorkerUrl = (moduleId, label) => {
if (label === 'json') return '_next/static/json.worker.js'
if (label === 'css') return '_next/static/css.worker.js'
if (label === 'html') return '_next/static/html.worker.js'
if (label === 'typescript' || label === 'javascript')
return '_next/static/ts.worker.js'
return '_next/static/editor.worker.js'
}
}}
/>
)
}
export default IndexPage
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment