当前位置: 代码网 > it编程>编程语言>Javascript > 在Monaco Editor中实现断点设置的方法详解

在Monaco Editor中实现断点设置的方法详解

2024年05月15日 Javascript 我要评论
monaco editor 是 vscode 等产品使用的代码编辑器,功能强大(且复杂),由微软维护。编辑器没有原生提供设置断点的功能,本文在 react + typescript(vite)框架下使

monaco editor 是 vscode 等产品使用的代码编辑器,功能强大(且复杂),由微软维护。编辑器没有原生提供设置断点的功能,本文在 react + typescript(vite)框架下使用 @monaco-editor/react 并介绍开发断点显示时踩到的坑。最终展示 鼠标悬浮显示断点按钮,点击后进行断点的设置或移除

本文不涉及调试功能的具体实现,可阅读 dap 文档

最终实现可直接拉到文末。

搭建 playground

react + typescript,使用的封装为 @monaco-editor/react

建立项目并配置依赖:

yarn create vite monaco-breakpoint
...
yarn add @monaco-editor/react

依赖处理完成后,我们编写简单的代码将编辑器展示到页面中:(app.tsx)

import editor from '@monaco-editor/react'
import './app.css'

function app() {
  return (
    <>
      <div style={{ width: '600px', height: '450px' }}>
        <editor theme='vs-dark' defaultvalue='// some comment...' />
      </div>
    </>
  )
}
export default app

接下来在该编辑器的基础上添加设置断点的能力。

一种暴力的办法:手动编写断点组件

不想阅读 monaco 文档,最自然而然的想法就是手动在编辑器的左侧手搭断点组件并显示在编辑器旁边。

首先手动设置如下选项

  • 编辑器高度为完全展开(通过设置行高并指定 height 属性)
  • 禁用代码折叠选项
  • 为编辑器外部容器添加滚动属性
  • 禁用小地图
  • 禁用内部滚动条的消费滚动
  • 禁用超出滚动

编辑器代码将变为:

const [code, setcode] = usestate('')

...

<div style={{ width: '600px', height: '450px', overflow: 'auto' }}>
<editor
  height={code.split('\n').length * 20}
  onchange={(value) => setcode(value!)}
  theme='vs-dark'
  value=[code]
  language='python'
  options={{
	lineheight: 20,
	scrollbeyondlastline: false,
	scrollbeyondlastcolumn: 0,
	minimap: { enabled: false },
	scrollbar: { alwaysconsumemousewheel: false },
	fold: false,
  }}
/>
</div>

现在编辑器的滚动由父容器的滚动条接管,再来编写展示断点的组件。我们希望断点组件展示在编辑器的左侧。 先设置父容器水平布局:

display: 'flex', flexdirection: 'row'

编写断点组件(示例):

  <div style={{ width: '600px', height: '450px', overflow: 'auto', display: 'flex', flexdirection: 'row' }}>
	<div
	  style={{
		width: '20px',
		height: code.split('\n').length * 20,
		background: 'black',
		display: 'flex',
		flexdirection: 'column',
	  }}
>
	  {[...array(code.split('\n').length)].map((_, index) => (
		<div style={{ width: '20px', height: '20px', background: 'red', borderradius: '50%' }} key={index} />
	  ))}
	</div>
	<editor ...

目前断点组件是能够展示了,但本文不在此方案下进行进一步的开发。这个方案的问题:

  • 强行设置组件高度能够展示所有代码,把 monaco 的性能优化整个吃掉了。这样的编辑器展示代码行数超过一定量后页面会变的非常卡,性能问题严重。
  • 小地图、超行滚动、代码块折叠等能力需要禁用,限制大。

本来目标是少读点 monaco 的超长文档,结果实际上动了那么多编辑器的配置,最终也还是没逃脱翻文档的结局。果然还是要使用更聪明的办法做断点展示。

使用 decoration

monaco editor playground 中有使用行装饰器的例子,一些博文也提到了可以使用 deltadecoration 为编辑器添加行装饰器。不过跟着写实现的时候出现了一些诡异的问题,于是查到 monaco 的 changelog 表示该方法已被弃用,应当使用 createdecorationscollection。 这个 api 的文档在 这里

为了能使用 editor 的方法,我们先拿到编辑器的实例(本文使用的封装库需要这么操作。如果你直接使用了原始的 js 库或者其他封装,应当可以用其他的方式拿到实例)。 安装 monaco-editor js 库:

yarn add monaco-editor

将 playground 代码修改如下:

import editor from '@monaco-editor/react'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import './app.css'
import { usestate } from 'react'

function app() {
  const [code, setcode] = usestate('')

  function handleeditordidmount(editor: monaco.editor.istandalonecodeeditor) {
	// 在这里就拿到了 editor 实例,可以存到 ref 里后面继续用
  }
  return (
    <>
      <div style={{ width: '600px', height: '450px' }}>
        <editor
		    onchange={(value) => setcode(value!)}
		    theme='vs-dark'
	        value=[code]
	        language='python'
	        onmount={handleeditordidmount}
        />
      </div>
    </>
  )
}

export default app

monaco editor 的装饰器是怎样设置的?

方法 createdecorationscollection 的参数由 imodeldeltadecoration 指定。其含有两个参数,分别为 imodeldecorationoptions 和 irange。options 参数指定了装饰器的样式等配置信息,range 指定了装饰器的显示范围(由第几行到第几行等等)。

// 为从第四行到第四行的范围(即仅第四行)添加样式名为breakpoints的行装饰器

const collections: monaco.editor.imodeldeltadecoration[] = []
collections.push({
	range: new monaco.range(4, 1, 4, 1),
	options: {
		iswholeline: true,
		linesdecorationsclassname: 'breakpoints',
		linesdecorationstooltip: '点击添加断点',
	},
})

const bpc = editor.createdecorationscollection(collections)

该方法的返回实例提供了一系列方法,用于添加、清除、更新、查询该装饰器集合状态。详见 ieditordecorationscollection

维护单个装饰器组:样式表

我们先写一些 css:

.breakpoints {
  width: 10px !important;
  height: 10px !important;
  left: 5px !important;
  top: 5px;
  border-radius: 50%;
  display: inline-block;
  cursor: pointer;
}

.breakpoints:hover {
  background-color: rgba(255, 0, 0, 0.5);
}

.breakpoints-active {
  background-color: red;
}

添加装饰器。我们先添加一个 1 到 9999 行的范围来查看效果(省事)。当然更好的办法是监听行号变动。

const collections: monaco.editor.imodeldeltadecoration[] = []
collections.push({
	range: new monaco.range(1, 1, 9999, 1),
	options: {
		iswholeline: true,
		linesdecorationsclassname: 'breakpoints',
		linesdecorationstooltip: '点击添加断点',
	},
})

const bpc = editor.createdecorationscollection(collections)

装饰器有了,监听断点组件的点击事件。使用 monaco 的 onmousedown listener。

editor.onmousedown((e) => {
  if (e.event.target.classlist.contains('breakpoints')) 
	e.event.target.classlist.add('breakpoints-active')
})

看起来似乎没有问题?monaco 的行是动态生成的,这意味着设置的 css 样式并不能持久显示。

看来还得另想办法。

维护单个装饰器组:手动维护 range 列表

通过维护 ranges 列表可以解决上述问题。 不过我们显然能注意到一个问题:我们希望设置的断点是行绑定的。

假设我们手动维护所有设置了断点的行数,显示断点时若有行号变动,断点将保留在原先的行号所在行。监听前后行号变动非常复杂,显然不利于实现。有没有什么办法能够直接利用 monaco 的机制?

维护两个装饰器组

我们维护两个装饰器组。一个用于展示未设置断点时供点击的按钮(①),另一个用来展示设置后的断点(②)。行号更新后,自动为所有行设置装饰器①,用户点击①时,获取实时行号并设置②。断点的移除和获取均通过装饰器组②实现。

  • 点击①时,调用装饰器组②在当前行设置展示已设置断点。
  • 点击②时,装饰器组②直接移除当前行已设置的断点,露出装饰器①。

如需获取设置的断点情况,可直接调用装饰器组②的 getranges 方法。

多个装饰器组在编辑器里是怎样渲染的?

两个装饰器都会出现在编辑器中,和行号在同一个父布局下。

顺便折腾下怎么拿到当前行号:设置的装饰器和展示行号的组件在同一父布局下且为相邻元素,暂且先用一个笨办法拿到。

// onmousedown事件下
const linenum = parseint(e.event.target.nextelementsibling?.innerhtml as string)

实现

回到 playground:

import editor from '@monaco-editor/react'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import './app.css'
import { usestate } from 'react'

function app() {
  const [code, setcode] = usestate('')

  function handleeditordidmount(editor: monaco.editor.istandalonecodeeditor) {
	// 在这里就拿到了 editor 实例,可以存到 ref 里后面继续用
  }
  return (
    <>
      <div style={{ width: '600px', height: '450px' }}>
        <editor
		    onchange={(value) => setcode(value!)}
		    theme='vs-dark'
	        value=[code]
	        language='python'
	        onmount={handleeditordidmount}
	        options={{ glyphmargin: true }} // 加一行设置monaco展示边距,避免遮盖行号
        />
      </div>
    </>
  )
}

export default app

断点装饰器样式:

.breakpoints {
    width: 10px !important;
    height: 10px !important;
    left: 5px !important;
    top: 5px;
    border-radius: 50%;
    display: inline-block;
    cursor: pointer;
}

.breakpoints:hover {
    background-color: rgba(255, 0, 0, 0.5);
}

.breakpoints-active {
    width: 10px !important;
    height: 10px !important;
    left: 5px !important;
    top: 5px;
    background-color: red;
    border-radius: 50%;
    display: inline-block;
    cursor: pointer;
    z-index: 5;
}

整理下代码:

  const bpoption = {
    iswholeline: true,
    linesdecorationsclassname: 'breakpoints',
    linesdecorationstooltip: '点击添加断点',
  }

  const activebpoption = {
    iswholeline: true,
    linesdecorationsclassname: 'breakpoints-active',
    linesdecorationstooltip: '点击移除断点',
  }

  function handleeditordidmount(editor: monaco.editor.istandalonecodeeditor) {
    const activecollections: monaco.editor.imodeldeltadecoration[] = []
    const collections: monaco.editor.imodeldeltadecoration[] = [
      {
        range: new monaco.range(1, 1, 9999, 1),
        options: bpoption,
      },
    ]

    const bpc = editor.createdecorationscollection(collections)
    const activebpc = editor.createdecorationscollection(activecollections)

    editor.onmousedown((e) => {
      // 加断点
      if (e.event.target.classlist.contains('breakpoints')) {
        const linenum = parseint(e.event.target.nextelementsibling?.innerhtml as string)
        const acc: monaco.editor.imodeldeltadecoration[] = []
        activebpc
          .getranges()
          .filter((item, index) => activebpc.getranges().indexof(item) === index) // 去重
          .foreach((erange) => {
            acc.push({
              range: erange,
              options: activebpoption,
            })
          })
        acc.push({
          range: new monaco.range(linenum, 1, linenum, 1),
          options: activebpoption,
        })
        activebpc.set(acc)
      }
      
      // 删断点
      if (e.event.target.classlist.contains('breakpoints-active')) {
        const linenum = parseint(e.event.target.nextelementsibling?.innerhtml as string)
        const acc: monaco.editor.imodeldeltadecoration[] = []
        activebpc
          .getranges()
          .filter((item, index) => activebpc.getranges().indexof(item) === index)
          .foreach((erange) => {
            if (erange.startlinenumber !== linenum)
              acc.push({
                range: erange,
                options: activebpoption,
              })
          })
        activebpc.set(acc)
      }
    })

    // 内容变动时更新装饰器①
    editor.ondidchangemodelcontent(() => {
      bpc.set(collections)
    })
  }

看起来基本没有什么问题了。

注意到空行换行时的内部处理策略是跟随断点,可以在内容变动时进一步清洗。

    editor.ondidchangemodelcontent(() => {
      bpc.set(collections)
      const acc: monaco.editor.imodeldeltadecoration[] = []
      activebpc
        .getranges()
        .filter((item, index) => activebpc.getranges().indexof(item) === index)
        .foreach((erange) => {
          acc.push({
            range: new monaco.range(erange.startlinenumber, 1, erange.startlinenumber, 1), // here
            options: {
              iswholeline: true,
              linesdecorationsclassname: 'breakpoints-active',
              linesdecorationstooltip: '点击移除断点',
            },
          })
        })
      activebpc.set(acc)
      props.onbpchange(activebpc.getranges())
    })

完整实现

app.tsx:

import editor from '@monaco-editor/react'
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api'
import './app.css'
import { usestate } from 'react'
import './editor-style.css'

function app() {
  const [code, setcode] = usestate('# some code here...')

  const bpoption = {
    iswholeline: true,
    linesdecorationsclassname: 'breakpoints',
    linesdecorationstooltip: '点击添加断点',
  }

  const activebpoption = {
    iswholeline: true,
    linesdecorationsclassname: 'breakpoints-active',
    linesdecorationstooltip: '点击移除断点',
  }

  function handleeditordidmount(editor: monaco.editor.istandalonecodeeditor) {
    const activecollections: monaco.editor.imodeldeltadecoration[] = []
    const collections: monaco.editor.imodeldeltadecoration[] = [
      {
        range: new monaco.range(1, 1, 9999, 1),
        options: bpoption,
      },
    ]

    const bpc = editor.createdecorationscollection(collections)
    const activebpc = editor.createdecorationscollection(activecollections)

    editor.onmousedown((e) => {
      // 加断点
      if (e.event.target.classlist.contains('breakpoints')) {
        const linenum = parseint(e.event.target.nextelementsibling?.innerhtml as string)
        const acc: monaco.editor.imodeldeltadecoration[] = []
        activebpc
          .getranges()
          .filter((item, index) => activebpc.getranges().indexof(item) === index) // 去重
          .foreach((erange) => {
            acc.push({
              range: erange,
              options: activebpoption,
            })
          })
        acc.push({
          range: new monaco.range(linenum, 1, linenum, 1),
          options: activebpoption,
        })
        activebpc.set(acc)
      }
      // 删断点
      if (e.event.target.classlist.contains('breakpoints-active')) {
        const linenum = parseint(e.event.target.nextelementsibling?.innerhtml as string)
        const acc: monaco.editor.imodeldeltadecoration[] = []
        activebpc
          .getranges()
          .filter((item, index) => activebpc.getranges().indexof(item) === index)
          .foreach((erange) => {
            if (erange.startlinenumber !== linenum)
              acc.push({
                range: erange,
                options: activebpoption,
              })
          })
        activebpc.set(acc)
      }
    })

    // 内容变动时更新装饰器①
    editor.ondidchangemodelcontent(() => {
      bpc.set(collections)
    })
  }
  return (
    <>
      <div style={{ width: '600px', height: '450px' }}>
        <editor
          onchange={(value) => {
            setcode(value!)
          }}
          theme='vs-dark'
          value=[code]
          language='python'
          onmount={handleeditordidmount}
          options={{ glyphmargin: true, folding: false }}
        />
      </div>
    </>
  )
}

export default app

editor-style.css:

.breakpoints {
  width: 10px !important;
  height: 10px !important;
  left: 5px !important;
  top: 5px;
  border-radius: 50%;
  display: inline-block;
  cursor: pointer;
}

.breakpoints:hover {
  background-color: rgba(255, 0, 0, 0.5);
}

.breakpoints-active {
  width: 10px !important;
  height: 10px !important;
  left: 5px !important;
  top: 5px;
  background-color: red;
  border-radius: 50%;
  display: inline-block;
  cursor: pointer;
  z-index: 5;
}

以上就是在monaco editor中实现断点设置的方法详解的详细内容,更多关于monaco editor断点设置的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com