qingshuijun 2023-10-01 13:17 采纳率: 50%
浏览 42
已结题

vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?

回答问题前请先看我的版本我的代码,这一点非常关键

请给你出代码的截图 这是第二点,直接粘贴AI来的代码 我直接无视,三周了 我尝试过各种Ai

请不要粘贴AI 过来的代码,谢谢! 全是不可用的玩意

放置了两个Codemirror,做JSON数据对比,但是大部分资料都是混乱的,5.0 和 6.0 的使用完全不一致,尝试了降低版本,以及各种各博主的方法,但都是碎片

有没有什么方式方法在上面的版本里可以让指定行的指定字符设置class或者style 修改背景颜色的方式方法

document方法就算了,目前就是想把对比出来的数据不一致的行进行标注

vue-codemirror版本6.1.1

codemirror版本6.0.1

codemirror/view 版本6.21.2

codemirror/state 版本 6.2.1

vue版本 3.3.4

<template>
<Codemirror class="codeviewleft" v-model="codeviewleft" :linters="linter" :heightChanged="true" placeholder="Code goes here..." :style="{ height: '96vh' }" :indent-with-tab="true" :tab-size="2" :extensions="extensions" :styleActiveLine="true" @ready="handleReady" @blur="getValue" />
<Codemirror class="codeviewright" v-model="codeviewright" :linters="linter" :heightChanged="true" placeholder="Code goes here..." :style="{ height: '96vh' }" :indent-with-tab="true" :tab-size="2" :extensions="extensions" :styleActiveLine="true"  @blur="getValue" />
<template>
<script setup>
import { ref,reactive,computed,shallowRef,onMounted } from 'vue'
import { Codemirror } from 'vue-codemirror'
import { json,jsonParseLinter  } from '@codemirror/lang-json'  
import { oneDark } from '@codemirror/theme-one-dark'
import { compare } from 'fast-json-patch'
import { countColumn } from '@codemirror/text';
import{EditorState}from"@codemirror/state"
import { Decoration, EditorView, highlightActiveLine } from "@codemirror/view";
const extensions = computed(() => [json(),oneDark])
const codeviewleft =ref('');
const codeviewright =ref('');
const handleReady = (payload) => {
  console.log('↓')
  console.log(payload)
  view.value = payload.view
  state.value = payload.state
}
  const getCodemirrorStates = () => {
    console.log(view.value)
    const state = view.value.state
    const ranges = state.selection.ranges
    const selected = ranges.reduce((r, range) => r + range.to - range.from, 0)
    const cursor = ranges[0].anchor
    const length = state.doc.length
    const lines = state.doc.lines    
    console.log(state)
    console.log(selected)
    console.log(cursor)
    console.log(length)
    console.log(lines)
}
</script>
<style lang='less' scoped>
.highlighted {
  background-color: #ff0000;
}
</style

已安装包如下

    "@codemirror/lang-json": "^6.0.1",
    "@codemirror/state": "^6.2.1",
    "@codemirror/text": "^0.19.6",
    "@codemirror/theme-one-dark": "^6.1.2",
    "@codemirror/view": "^6.20.2"
    "fast-json-patch": "^3.1.1",
    "vue-codemirror": "^6.1.1",
    "codemirror": "^6.0.1"
  • 写回答

12条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-01 13:20
    关注

    引自强大而免费的微信小程序:皆我百晓生。如有帮助,还望采纳。


    要在vue-codemirror中修改指定行和位置的字符的背景颜色或字体颜色,你可以使用Codemirror的Decoration类来实现。首先,你需要获取到EditorView对象,它可以通过ready事件的回调函数参数获得。然后,你可以使用EditorViewaddDecoration方法来添加Decoration对象,从而修改指定位置的字符的样式。

    以下是示例代码,演示如何在指定行、指定位置设置字符的背景颜色:

    <template>
      <Codemirror ref="codemirror" v-model="code" @ready="handleReady" />
    </template>
    
    <script>
    import { ref } from 'vue';
    import { Codemirror } from 'vue-codemirror';
    import { Decoration, EditorView } from '@codemirror/view';
    
    export default {
      components: {
        Codemirror
      },
      setup() {
        const code = ref('');
    
        const handleReady = (editor) => {
          // 获取EditorView对象
          const view = editor.view;
    
          // 创建Decoration对象,设置指定位置的背景颜色
          const deco = Decoration.mark({
            class: 'highlighted', // 样式类名
            attributes: { style: 'background-color: yellow;' } // 背景颜色
          });
    
          // 添加Decoration对象到指定位置
          const from = { line: 2, ch: 5 }; // 指定行和位置
          const to = { line: 2, ch: 10 };
          view.dispatch({ effects: deco.addToRange(EditorView.editable(view), { from, to }) });
        };
    
        return {
          code,
          handleReady
        };
      }
    };
    </script>
    
    <style>
    .highlighted {
      background-color: yellow;
    }
    </style>
    

    在上面的例子中,将class设置为highlighted,然后在<style>中定义了.highlighted的样式,设置了背景颜色为黄色。你可以根据需要修改样式。

    请注意,你需要在CSS样式中定义.highlighted类名,并设置相应的样式,以便修改指定位置字符的背景颜色。

    希望这可以帮助到你!

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月4日
  • 修改了问题 10月4日
  • 修改了问题 10月4日
  • 修改了问题 10月2日
  • 展开全部

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助