monaco editor 高亮碰到的问题
使用 monaco editor 做一个简单的指定字符高亮功能:
<template>
<div id="container" :style="{ zIndex, width }">
</div>
</template>
<script>
export default {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | mounted() { this .$nextTick(() => this .init()} }, methods: { init() { const muri = monaco.Uri.parse( 'file' , + 'rance/abl.c' ) let model = monaco.editor.getModel(muri) if (!model){ model = monaco.editor.createModel( this .value, 'cpp' , muri) } this .editor = monaco.editor.create( this .$refs.editor, { value: this .value, theme: 'vs' , language: 'c' , automoaticLayout: true , readOnly: true , mimimap: { enabled: false } } this .update() }, update() { const model = this .editor.getModel() model.deltaDecorations( this .model, []) if (model) { let matches = model.findMoatches( 'int' , true , false , true ) this .decorations = matches.map((match) => ( { range: new monaco.Range(match.range.startLineNumber, match.range.startColumn, match.range.endLineNumber,match.range.endColumn), options: { isWholeLine: false , className: 'high' } })) this .model = model.deltaDecorations([], this .decorations)<br> }<br> }<br>}<br><br> |
};
</script>
<style scoped>
这里一定要注意类名权重,不加入可能会使用官方自带高亮类名样式背景,再次刷新界面会使得代码不渲染高亮背景色,.high不生效。
#containr .high {
background: red;
}
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报