突然想起来之前一个需求,就是web页面画一个能输入代码的框子,这个我也找了好几个库,比如codemirror,不知道这个库是不是没有更新了还是怎么,怎么都下不下来依赖,最后选择了monaco-editor。产品一开始是希望输入代码还能出提示,出报错,就跟ieda,vscode那样,那不是痴人说梦,哪个在线运行代码能做到这样,连依赖都不确定,就给你报错,Java,Python都不可能给你语法报错的,只有js可能会报个语法错误。言归正传,monaco-editor是有代码高亮度提示,至于一些自定义的代码补全也可以做到,这个我没有深究。仅作为自己备忘
import React from "react";
import Editor from "@monaco-editor/react";
function App() {
return (
<Editor
height="90vh"
defaultLanguage="javascript"
defaultValue="// some comment"
/>
);
}
export default App;
上面是官网的示例,输入js语言可以有代码提示,
但是主流浏览器都加载不出来(或者加载很慢这个编辑器框子,原因是monaco文件来自cdn的资源包,你需要自己本地loader一下,链接在此
即:
import * as monaco from 'monaco-editor';
import { loader } from '@monaco-editor/react';
loader.config({ monaco });
但是引入了这一行js语言就不会出现报错提示了。
另外还需要注意的是,
值得一提的是,当您更改language或value道具时,它们会影响在组件安装时自动创建的同一模型。在大多数情况下没问题,但是当开发人员想要实现一个多模型编辑器来支持像IDEs 中的选项卡/文件时,他们会遇到问题。之前,为了处理多个模型,他们必须在组件之外手动完成。API现在,支持多模型🎉让我们检查一下它是如何工作的。创建模型需要三个参数 -value和( language) 。您可以将最后一个 ( ) 视为模型的标识符。该组件现在有一个道具。当您指定一个prop 时,该组件会检查它是否具有该路径的模型。如果是,将显示现有模型,否则将创建(并存储)一个新模型。使用此技术,您可以将文件与路径对应起来,并创建一个完全多模型的编辑器。你可以打开你的文件,做一些改变,选择另一个文件,当你回到第一个文件时,前一个模型将显示整个视图状态、文本选择、撤消堆栈、滚动位置等
即当你切换不用语言编辑器时候,你最好预设了他们了他们唯一标识符,示例如下:
import React, { useState, useEffect, useRef } from "react";
import Editor from "@monaco-editor/react";
import files from "./files";
import * as monaco from 'monaco-editor';
import { loader } from '@monaco-editor/react';
loader.config({ monaco });
function App() {
const editorRef = useRef(null);
const [fileName, setFileName] = useState("script.js");
const file = files[fileName];
useEffect(() => {
editorRef.current?.focus();
}, [file.name]);
return (
<>
<button
disabled={fileName === "script.js"}
onClick={() => setFileName("script.js")}
>
script.js
</button>
<button
disabled={fileName === "style.css"}
onClick={() => setFileName("style.css")}
>
style.css
</button>
<button
disabled={fileName === "index.html"}
onClick={() => setFileName("index.html")}
>
index.html
</button>
<Editor
height="80vh"
theme="vs-dark"
path={file.name}
defaultLanguage={file.language}
defaultValue={file.value}
onMount={(editor) => (editorRef.current = editor)}
/>
</>
);
}
export default App;
files.js如下
const someJSCodeExample = `
// The source (has been changed) is https://github.com/facebook/react/issues/5465#issuecomment-157888325
const CANCELATION_MESSAGE = {
type: 'cancelation',
msg: 'operation is manually canceled',
};
function makeCancelable(promise) {
let hasCanceled_ = false;
const wrappedPromise = new Promise((resolve, reject) => {
promise.then(val => hasCanceled_ ? reject(CANCELATION_MESSAGE) : resolve(val));
promise.catch(reject);
});
return (wrappedPromise.cancel = () => (hasCanceled_ = true), wrappedPromise);
}
export default makeCancelable;
`;
const someCSSCodeExample = `
@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
outline: none;
-ms-overflow-style: none;
scrollbar-width: none;
}
*::-webkit-scrollbar {
display: none;
}
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
[type=reset], [type=submit], button, html [type=button] {
-webkit-appearance: button;
}
[type=button]{
-webkit-appearance: none;
}
.full-width {
width: 100%;
}
.full-height {
height: 100%;
}
.full-size {
width: 100%;
height: 100%;
}
.ql-editor a {
color: rgba(255, 255, 255, 0.20);
cursor: pointer;
padding-left: 8px;
padding-right: 8px;
text-decoration: none;
}
.ql-editor ul, .ql-editor li, .ql-editor ol {
margin-left: 16px;
}
.ql-editor object {
color: #d32f2f;
}
.ql-editor blockquote {
border-left: 3px solid rgba(255, 255, 255, 0.12);
padding-top: 8px;
padding-left: 24px;
padding-right: 16px;
padding-bottom: 8px;
}
.ql-editor .ql-align-center {
text-align: center;
}
.ql-editor .ql-align-justify {
text-align: justify;
}
.ql-editor .ql-align-right {
text-align: right;
}
.ql-editor a:hover {
text-decoration: underline;
}
`;
const someHTMLCodeExample = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<!-- https://web.dev/uses-rel-preconnect -->
<link rel="preconnect" href="https://storage.googleapis.com">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#111" />
<meta
name="description"
content="Wlist"
data-react-helmet="true"
/>
<meta
property="og:title"
content="Wlist"
data-react-helmet="true"
>
<meta
property="og:description"
content="Wlist"
data-react-helmet="true"
>
<meta
property="og:url"
content="%PUBLIC_URL%"
data-react-helmet="true"
>
<meta
property="og:image"
content="%PUBLIC_URL%/images/cover.png"
data-react-helmet="true"
/>
<meta
name="twitter:card"
content="summary"
data-react-helmet="true"
/>
<meta property="og:type" content="website" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" crossorigin="use-credentials" />
<!-- https://web.dev/defer-non-critical-css/ -->
<link rel="preload" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" as="style" οnlοad="this.οnlοad=null;this.rel='stylesheet'">
<title>Wlist</title>
<!-- ie -->
<script type="text/javascript">
var ua = navigator.userAgent;
var is_ie = ua.indexOf('MSIE ') > -1 || ua.indexOf('Trident/') > -1;
if (is_ie) {
document.ie = 'true';
var ie_script = document.createElement('script');
var ie_styles = document.createElement('link');
ie_script.src = 'no-ie/init.js';
ie_styles.rel = 'stylesheet';
ie_styles.href = 'no-ie/styles.css';
function injectScripts() {
document.body.innerHTML = '';
document.body.appendChild(ie_styles);
document.body.appendChild(ie_script);
}
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', injectScripts);
} else { // before IE 9
document.attachEvent('DOMContentLoaded', injectScripts);
}
}
</script>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<script type="text/javascript">
// set the body color before app initialization, to avoid blinking
var themeMode = localStorage.getItem('theme-mode');
var initialBodyStyles = document.createElement('style');
var currentThemeColor = themeMode === 'light' ? '#fafafa': '#111';
initialBodyStyles.innerText = 'body { background-color: ' + currentThemeColor + ' }';
document.head.appendChild(initialBodyStyles);
// also set meta[name="theme-color"] content
var metaTheme = document.querySelector('meta[name="theme-color"]');
metaTheme.content = currentThemeColor;
</script>
<div id="root"></div>
</body>
</html>
`;
const files = {
"script.js": {
name: "script.js",
language: "javascript",
value: someJSCodeExample
},
"style.css": {
name: "style.css",
language: "css",
value: someCSSCodeExample
},
"index.html": {
name: "index.html",
language: "html",
value: someHTMLCodeExample
}
};
export default files;