CodeMirror實(shí)現(xiàn)代碼對(duì)比功能(插件react vue)
要實(shí)現(xiàn)一個(gè)需求:一個(gè)代碼編輯器,用戶(hù)上次寫(xiě)的代碼和現(xiàn)在的代碼要區(qū)分出不同。網(wǎng)上找了幾個(gè)案例,拿過(guò)去一用差點(diǎn)氣吐血,報(bào)錯(cuò)像雪花一樣,浪費(fèi)時(shí)間!
本文中的代碼,一鍵粘貼拿來(lái)即用,代碼就是我在寫(xiě)這篇文章時(shí)寫(xiě)的demo,絕無(wú)報(bào)錯(cuò)。
1.第一步:下載插件
vue或者react都需要這一步且同樣的下載方式。
npm install diff-match-patch save npm install codemirror save 用yarn的話(huà) npm install 替換成 yarn add …
2.vue代碼如下:
建一個(gè)空白.vue文件然后一鍵復(fù)制以下代碼:
<template>
<div ref="codeMirror" class="code-contrast" style="width:100%;height:100%" />
</template>
<script>
import CodeMirror from 'codemirror'
import 'codemirror/lib/codemirror.css'
import 'codemirror/addon/merge/merge.js'
import 'codemirror/addon/merge/merge.css'
import DiffMatchPatch from 'diff-match-patch'
window.diff_match_patch = DiffMatchPatch
window.DIFF_DELETE = -1
window.DIFF_INSERT = 1
window.DIFF_EQUAL = 0
export default {
name: 'CodeMirror',
data() {
return {
oldValue: '我們到現(xiàn)在還是一樣的',
newValue: '我們到現(xiàn)在還是一樣的,這幾個(gè)字給我標(biāo)個(gè)紅吧!',
isReadOnly: false
}
},
watch: {
oldValue: {
immediate: true,
handler() {
this.$nextTick(() => {
this.initUI()
})
}
},
newValue: {
immediate: true,
handler() {
this.$nextTick(() => {
this.initUI()
})
}
}
},
methods: {
initUI() {
if (this.newValue == null) return
const target = this.$refs.codeMirror
target.innerHTML = ''
CodeMirror.MergeView(target, {
value: this.oldValue, // 上次內(nèi)容
origLeft: null,
orig: this.newValue, // 本次內(nèi)容
lineNumbers: true, // 顯示行號(hào)
mode: 'text/html',
highlightDifferences: true,
connect: 'align',
readOnly: this.isReadOnly // 只讀 不可修改
})
}
}
}
</script>
<style lang="scss">
.code-contrast .CodeMirror-merge-copy,
.CodeMirror-merge-scrolllock-wrap {
display: none !important;
}
</style>效果圖:

細(xì)節(jié)處對(duì)比:

3.react hooks代碼如下:
記得先下載第一步的文件然后再一鍵復(fù)制。
對(duì)比效果圖如下:

import React, { useEffect, useState, useRef } from 'react'
import CodeMirror from 'codemirror'
import 'codemirror/lib/codemirror.css'
import 'codemirror/addon/merge/merge.js'
import 'codemirror/addon/merge/merge.css'
import DiffMatchPatch from 'diff-match-patch'
export default function Demo () {
window.diff_match_patch = DiffMatchPatch
window.DIFF_DELETE = -1
window.DIFF_INSERT = 1
window.DIFF_EQUAL = 0
const [num, setNum] = useState(10)
const [oldValue, setOldValue] = useState('11111111111')
const [newValue, setNewValue] = useState('11111111112222222222')
const [isReadOnly, setIsReadOnly] = useState(false)
const codeMirror = useRef(null)
const initUI = () => {
if (newValue == null) return
const target = codeMirror.current
console.log(target, '00000000000')
target.innerHTML = ''
CodeMirror.MergeView(target, {
value: oldValue, // 上次內(nèi)容
origLeft: null,
orig: newValue, // 本次內(nèi)容
lineNumbers: true, // 顯示行號(hào)
mode: 'text/html',
highlightDifferences: true,
connect: 'align',
readOnly: isReadOnly // 只讀 不可修改
})
}
useEffect(() => {
initUI()
}, [newValue])
useEffect(() => {
initUI()
}, [oldValue])
return (
<div>
<div ref={codeMirror} className="code-contrast" style={{ width: '100%', height: '100%' }}></div>
</div>
)
}4.一點(diǎn)心得
使用方法是:
有兩個(gè)數(shù)據(jù),newvalue和oldvalue,舊數(shù)據(jù)代表著你上次的數(shù)據(jù),新數(shù)據(jù)代表你現(xiàn)在正在寫(xiě)的數(shù)據(jù),實(shí)際使用中,你先需要保存用戶(hù)上次寫(xiě)的數(shù)據(jù),然后保存用戶(hù)新寫(xiě)的數(shù)據(jù),再去對(duì)比。
可能會(huì)出現(xiàn)的問(wèn)題:
看見(jiàn)我的樣式了沒(méi) 用的scss有的人項(xiàng)目可能沒(méi)用這個(gè),你可以把我這個(gè)樣式改寫(xiě)成普通css形式,反正就幾行改起來(lái)很簡(jiǎn)單。
到此這篇關(guān)于CodeMirror實(shí)現(xiàn)代碼對(duì)比功能的文章就介紹到這了,更多相關(guān)CodeMirror代碼對(duì)比內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue實(shí)現(xiàn)codemirror代碼編輯器中的SQL代碼格式化功能
- 在vue項(xiàng)目中使用codemirror插件實(shí)現(xiàn)代碼編輯器功能
- 在vue里使用codemirror遇到的問(wèn)題
- vue使用codemirror的兩種用法
- vue中使用codemirror的實(shí)例詳解
- 前端插件庫(kù)之vue3使用vue-codemirror插件的步驟和實(shí)例
- vue?codemirror實(shí)現(xiàn)在線代碼編譯器效果
- Vue進(jìn)階之CodeMirror的應(yīng)用小結(jié)
- vue3中如何使用codemirror6增加代碼提示功能
相關(guān)文章
理解JavaScript中window對(duì)象的一些用途
這篇文章主要介紹了理解JavaScript中的window對(duì)象,本文通過(guò)實(shí)例代碼詳細(xì)介紹Window對(duì)象的一些重要用途,需要的朋友可以參考下2022-07-07
JavaScript+html5 canvas制作的圓中圓效果實(shí)例
這篇文章主要介紹了JavaScript+html5 canvas制作的圓中圓效果,結(jié)合完整實(shí)例形式分析了基于JavaScript與html5 canvas技術(shù)實(shí)現(xiàn)的圖形繪制與顏色隨機(jī)填充技巧,需要的朋友可以參考下2016-01-01
從柯里化分析JavaScript重要的高階函數(shù)實(shí)例
這篇文章主要為大家介紹了從柯里化分析JavaScript重要的高階函數(shù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
javascript DOM 操作基礎(chǔ)知識(shí)小結(jié)
經(jīng)常用到j(luò)avascript對(duì)dom,喜歡這方便的朋友也很多,要想更好的對(duì)dom進(jìn)行操作,這些基礎(chǔ)一定要知道的。2010-04-04

