当前位置: 首页 > news >正文

React 中如何使用 Monaco

Monaco 是微软开源的一个编辑器,VSCode 也是基于 Monaco 进行开发的。如果在 React 中如何使用 Monaco,本文将介绍如何在 React 中引入 Monaco。

安装 React 依赖

yarn add react-app-rewired --dev
yarn add monaco-editor-webpack-plugin --dev
yarn add monaco-editor
yarn add react-monaco-editor

创建Webpack 配置文件并添加插件

在项目的根目录下创建 config-overrides.js

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');module.exports = function override(config, env) {config.plugins.push(new MonacoWebpackPlugin({languages: ['json']}));return config;
}

引入 Monaco 组件

核心函数 handleEditorDidMount,行选中时,在行尾添加一句话,通过事件监听函数 onDidChangeCursorSelection 和 Decoration 进行实现

import logo from './logo.svg';
import './App.css';
import MonacoEditor from 'react-monaco-editor';
import { useRef, useState,useEffect }  from 'react';function App() {const editorRef = useRef(null);const monacoRef = useRef(null);const decorationsRef = useRef([]);const handleEditorDidMount = (editor, monaco) => {editorRef.current = editor;monacoRef.current = monaco;// Add an event listener for cursor position changeseditor.onDidChangeCursorSelection(() => {const selection = editor.getSelection();// if (selection.isEmpty()) {//   // Remove decorations if selection is empty//   editor.deltaDecorations(decorationsRef.current, []);//   return;// }const lineNumber = selection.positionLineNumber;const lineContent = editor.getModel().getLineContent(lineNumber);if (lineContent !== "") {// Add decoration if the line starts with 'var'const newDecorations = editor.deltaDecorations(decorationsRef.current, [{range: new monaco.Range(lineNumber, 1, lineNumber, 1),options: {isWholeLine: true,afterContentClassName: 'myAfterContentDecoration'}}]);decorationsRef.current = newDecorations;} else {// Remove decorations if the line does not start with 'var'editor.deltaDecorations(decorationsRef.current, []);}});};useEffect(() => {// Define custom styles for the decorationsconst style = document.createElement('style');style.innerHTML = `.myAfterContentDecoration::after {content: ' // 备注';color: green;font-weight: bold;}`;document.head.appendChild(style);}, []);return (<div style={{'margin':'100px auto', 'width': '800px'}}><MonacoEditorwidth="800"height="600"language="javascript"theme="vs-dark"value={""}options={""}editorDidMount={handleEditorDidMount}/></div>);
}export default App;

在这里插入图片描述

总结

Monaco 功能非常强大,API 也比较复杂,后面后陆续看看它的 API 如何使用。

http://www.lryc.cn/news/391358.html

相关文章:

  • 开源RAG个人知识库项目开发分析
  • 事务底层与高可用原理
  • 树状数组基础知识
  • 【3分钟准备前端面试】vue3
  • 【数据采集】亮数据浏览器、亮网络解锁器实战指南
  • 暑期编程预习指南
  • 将带有 商店idr 商品信息的json导入到mongodb后,能不能根据商店id把所有商品全部提取并转为电子表格
  • 深入解析 androidx.databinding.BaseObservable
  • MySQL数据恢复(适用于误删后马上发现)
  • [数据结构】——七种常见排序
  • CPU占用率飙升至100%:是攻击还是正常现象?
  • java如何替换字符串中给定索引的字符
  • 基于RK3588的GMSL、FPDLink 、VByone及MIPI等多种摄像模组,适用于车载、机器人工业图像识别领域
  • Windows 的 MFC开发的使用示例——讲得挺好的
  • Spring4.3.x xml配置文件搜索和解析过程
  • 网络爬虫(一)深度优先爬虫与广度优先爬虫
  • JavaScript懒加载图像
  • Git指令
  • DllImport进阶:参数配置与高级主题探究
  • HTTP与HTTPS协议区别及应用场景
  • Vue2-Vue Router前端路由实现思路
  • 2024 年 亚太赛 APMCM (C题)中文赛道国际大学生数学建模挑战赛 | 量子计算的物流配送 | 数学建模完整代码+建模过程全解全析
  • 客观分析-自己和本科学生之间的差距
  • 清华镜像源
  • 大语言模型测评工具-ChatHub和ChatAll
  • 使用redis分布式锁,不要把锁放在本地事务内部
  • Python学生信息管理系统(完整代码)
  • 【大功率汽车大灯升压方案】LED恒流驱动芯片FP7208升压车灯调光应用,PWM内部转模拟,调光深度1%,无频闪顾虑,低亮无抖动
  • uniapp应用如何实现传感器数据采集和分析
  • 读书笔记-Java并发编程的艺术-第3章(Java内存模型)-第6节(final域的内存语义)