react MonacoEditor react-monaco-editor的使用编辑功能很强大,react MonacoEditor包含断点、代码提示,自定义黑色背景,功能齐全。可以直接使用

1、css样式

.breakpoints {
    background: red;
    width: 10px !important;
    height: 10px !important;
    left: 0px !important;
    top: 3px;
    border-radius: 5px;
}

.breakpoints-fake {
    background: red;
    width: 10px !important;
    height: 10px !important;
    left: 0px !important;
    top: 3px;
    border-radius: 5px;
}

2、直接上我封装好了的组件、拿去不用谢。

import React from 'react';
import MonacoEditor from 'react-monaco-editor';
import style from "./index.css"

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      code: "test",
    }
    this.editor = null;
    this.monaco = null;
    this.decorations = [];
    this.onChange = this.onChange.bind(this);
    this.editorDidMount = this.editorDidMount.bind(this);
  }

  editorDidMount(editor, monaco) {
    if (!this.editor) this.editor = editor, this.monaco = monaco, this.editorMousDown(), this.onMouseMove();
    editor.focus();
  }

  onChange(newValue, e) {
    this.props.parent(this.props.data._id, newValue);
  }

  onMouseMove() {
    this.editor.onMouseMove(e => {
      if (!this.isJsEditor()) return
      this.removeFakeBreakPoint()
      if (e.target.detail && e.target.detail.offsetX && e.target.detail.offsetX >= 0 && e.target.detail.offsetX <= 10) {
        let line = e.target.position.lineNumber
        this.addFakeBreakPoint(line)
      }
    })
    this.editor.onMouseLeave(() => {
      this.removeFakeBreakPoint()
    })
    this.editor.onKeyDown(e => {
      if (e.code === 'Enter') {
        this.removeFakeBreakPoint()
      }
    })
  }

  trigger(id) {
    if (!this.editor) return
    this.editor.trigger('anyString', id)
  }

  insertContent(text) {
    if (this.editor) {
      let selection = this.editor.getSelection()
      let range = new this.monaco.Range(selection.startLineNumber, selection.startColumn, selection.endLineNumber, selection.endColumn)
      let id = {
        major: 1,
        minor: 1
      }
      let op = {
        identifier: id,
        range: range,
        text: text,
        forceMoveMarkers: true
      }
      this.editor.executeEdits(this.root, [op])
      this.editor.focus()
    }
  }

  editorMousDown() {
    this.editor.onMouseDown(e => {
      if (!this.isJsEditor()) return
      if (e.target.detail && e.target.detail.offsetX && e.target.detail.offsetX >= 0 && e.target.detail.offsetX <= 10) {
        let line = e.target.position.lineNumber
        if (this.editor.getModel().getLineContent(line).trim() === '') {
          return
        }
        if (!this.hasBreakPoint(line)) {
          this.addBreakPoint(line)
        } else {
          this.removeBreakPoint(line)
        }
        if (this.lastPosition) {
          this.editor.setPosition(this.lastPosition)
        } else {
          document.activeElement.blur()
        }
      }
      if (e.target.type === 6 || e.target.type === 7) {
        this.lastPosition = this.editor.getPosition()
      }
    })
  }

  isJsEditor() {
    return this.editor.getModel().getLanguageIdentifier().language === 'javascript'
  }

  addFakeBreakPoint(line) {
    if (this.hasBreakPoint(line)) return;
    let value = {
      range: new this.monaco.Range(line, 1, line, 1),
      options: {
        isWholeLine: true,
        linesDecorationsClassName: 'breakpoints-fake'
      }
    }
    this.decorations = this.editor.deltaDecorations(this.decorations, [value])
  }

  removeFakeBreakPoint() {
    this.decorations = this.editor.deltaDecorations(this.decorations, [])
  }

  async addBreakPoint(line) {
    let model = this.editor.getModel()
    if (!model) return
    let value = {
      range: new this.monaco.Range(line, 1, line, 1),
      options: {
        isWholeLine: true,
        linesDecorationsClassName: 'breakpoints'
      }
    }
    model.deltaDecorations([], [value])
  }

  async removeBreakPoint(line) {
    let model = this.editor.getModel()
    if (!model) return
    let decorations
    let ids = []
    if (line !== undefined) {
      decorations = this.editor.getLineDecorations(line)
    } else {
      decorations = this.editor.getAllDecorations()
    }
    for (let decoration of decorations) {
      if (decoration.options.linesDecorationsClassName === 'breakpoints') {
        ids.push(decoration.id)
      }
    }
    if (ids && ids.length) {
      model.deltaDecorations(ids, [])
    }
  }

  hasBreakPoint(line) {
    let decorations = this.editor.getLineDecorations(line)
    for (let decoration of decorations) {
      if (decoration.options.linesDecorationsClassName === 'breakpoints') {
        return true
      }
    }
    return false
  }

  render() {
    const code = this.state.code;
    const options = {
      selectOnLineNumbers: true
    };
    return (
      <MonacoEditor
        className="monacoEditorWrapper"
        height="100vh"
        width= "1617px"
        language="javascript"
        theme="vs-dark"
        value={code}
        options={options}
        onChange={this.onChange}
        editorDidMount={this.editorDidMount}
      />
    );
  }
}

3、使用直接引入到别的组件中即可使用

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
React-Monaco-Editor是一个基于React代码编辑器组件,它使用Monaco Editor作为底层编辑器。代码补全是React-Monaco-Editor的一个重要功能,可以帮助开发者在编写代码时提供自动补全的建议。 要实现React-Monaco-Editor代码补全功能,你需要进行以下几个步骤: 1. 安装React-Monaco-Editor:首先,在你的React项目中安装React-Monaco-Editor依赖包。你可以使用npm或者yarn进行安装,具体命令如下: ``` npm install react-monaco-editor ``` 或者 ``` yarn add react-monaco-editor ``` 2. 导入React-Monaco-Editor组件:在你的代码文件中,导入React-Monaco-Editor组件,并将其作为一个React组件来使用。具体代码如下: ```jsx import React from 'react'; import MonacoEditor from 'react-monaco-editor'; class CodeEditor extends React.Component { render() { return ( <MonacoEditor language="javascript" // 设置编辑器语言 theme="vs-dark" // 设置编辑器主题 options={{ // 设置编辑器选项 automaticLayout: true, // 自动调整布局 suggestOnTriggerCharacters: true, // 在输入特定字符时触发建议 wordBasedSuggestions: true, // 基于单词的建议 suggestSelection: 'first', // 选择第一个建议 ...其他选项 }} // 其他属性 /> ); } } ``` 3. 配置代码补全:在上述代码中的`options`属性中,你可以配置编辑器的各种选项,包括代码补全相关的选项。具体来说,你可以设置`suggestOnTriggerCharacters`为`true`,以在输入特定字符时触发建议。你还可以设置`wordBasedSuggestions`为`true`,以基于单词进行建议。此外,你还可以设置其他相关的选项来自定义代码补全的行为。 以上就是使用React-Monaco-Editor实现代码补全的基本步骤。你可以根据自己的需求进行进一步的配置和定制化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值