0.1.11 • Published 2 years ago

monacoeditor-for-react v0.1.11

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

monacoeditor-for-react

介绍

monaco-editor组件化封装 react版

安装

yarn add monacoeditor-for-react
或
npm install monacoeditor-for-react

相关说明文档

编辑器组件使用说明


属性类型声明
export interface MonacoEditorProps {
	  renderLoader?: (errorMsg: string) => React.ReactNode;
	  src?: string;
	  language?: string;
	  theme?: string;
	  options?: monaco.editor.IEditorOptions & monaco.editor.IGlobalEditorOptions;
	  dimension?: monaco.editor.IDimension;
	  tabSize?: number;
	  value?: string;
	  onMount?: (editorObj: monaco.editor.IStandaloneCodeEditor) => void;
	  enableCompile?: boolean;
	  onSave?: (compiling: boolean, content: string, compiled?: string) => void;
	  onCompileError?: (error: string) => void; // 编译异常报错信息
	  compileResultHandle?: (error: string | undefined, compiledCode: string) => void;
	  renderLoadError?: (error: any) => React.ReactNode;
	  className?: string;
	  style?: React.CSSProperties;
	  onChange?: (value: string, editorObj: monaco.editor.IStandaloneCodeEditor) => void;
	  debounceTime?: number;
	  babelSrc?: string;
	  fontSize?: number;
	  readonly?: boolean;
}
API
参数说明类型是否必填默认值
renderLoader加载状态渲染React.ReactNode正在加载...
srcvs生产依赖库加载路径stringhttps://cdn.bootcdn.net/ajax/libs/monaco-editor/0.26.1/min/vs
language语言类型stirngtext
theme主题'vs-light''vs-dark''hc-black'light
options编辑器参数,可参考官方文档monaco.editor.IEditorOptions & monaco.editor.IGlobalEditorOptions
dimension编辑器宽高monaco.editor.IDimension
tabSizetab空格个数number2
value内容文本string
onMount编辑器渲染完成后回调,编辑内容提示等功能可参考官方对editorObj进行处理(editorObj: monaco.editor.IStandaloneCodeEditor) => void;
enableCompile是否启用编辑功能,开启后会加载babel相关库booleanfalse
onSavectrl+s保存的事件回调(compiling: boolean, content: string, compiled?: string) => void;
onCompileError编译异常回调(error: string) => void;
compileResultHandle编译结果处理器,也是编译结果的回调(error: stringundefined, compiledCode: string) => void;
renderLoadError加载异常时渲染React.ReactNode加载异常
className
style
onChange内容变更触发事件(value: string, editorObj: monaco.editor.IStandaloneCodeEditor) => void;
debounceTime编译防抖时间number500
babelSrcbabel编译库依赖路径stringhttps://weapp.eteams.cn/ecode
fontSize字体大小number
readonly是否只读booleanfalse
Demo示例
import React, { useState } from 'react';

import MonacoEditor from 'monacoeditor-for-react';
import * as monaco from 'monaco-editor';


function App() {
  const [editorObj, setEditorObj] = useState<monaco.editor.IStandaloneCodeEditor>();

  return (
    // 注意默认情况下只需要对包裹的div做大小控制即可,编辑器会撑满外层div
    <div style={{ height: 500, width: 800, border: '1px solid pink', margin: 'auto', marginTop: 100 }}>
      <MonacoEditor 
        // compileWorker={new WorkerApi()}
        className="sss"
        value={`console.log("11")`}
        language="javascript"
        src={"/vs"}
        enableCompile
        onCompileError={(err: any) => {
          console.log('编译异常 => ', err);
        }}
        compileResultHandle={(err, code) => {
          console.log('编译后代码 => ', code);
        }}
        renderLoadError={(err: any) => {
          console.log('加载异常 => ', err);
          return <div>111</div>;
        }}
        onSave={(code, compiledCode) => {
          console.log('保存后的代码 => ', code, compiledCode);
        }}
        onMount={(editorObj) => {
          setEditorObj(editorObj);
        }}
        tabSize={2}
        theme="vs-dark"
        onChange={(val) => console.log('change => ', val)}
      />
    </div>
  );
}

export default App;

npm.io

0.1.10

2 years ago

0.1.11

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.4

2 years ago

0.0.5

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.0.7

2 years ago

0.1.5

2 years ago

0.0.6

2 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago