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)在線(xiàn)代碼編譯器效果
- Vue進(jìn)階之CodeMirror的應(yīng)用小結(jié)
- vue3中如何使用codemirror6增加代碼提示功能
相關(guān)文章
js將字符串中的每一個(gè)單詞的首字母變?yōu)榇髮?xiě)其余均為小寫(xiě)
本文主要介紹了javascript將字符串中的每一個(gè)單詞的首字母變?yōu)榇髮?xiě)其余均為小寫(xiě)的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01JavaScript trim 去除字符串空格的三種方法(附代碼詳解)
個(gè)人認(rèn)為最好的方法.采用的是正則表達(dá)式,這是最核心的原理.因?yàn)榭崭裼卸喾N形式。2010-05-05火狐下input焦點(diǎn)無(wú)法重復(fù)獲取問(wèn)題的解決方法
input輸入框顯示的時(shí)候,需要自動(dòng)獲取焦點(diǎn),用focus可以輕松搞定,但在火狐下input無(wú)法獲取焦點(diǎn),下面與大家分享下不錯(cuò)的解決方法2014-06-06javascript制作的網(wǎng)頁(yè)側(cè)邊彈出框思路及實(shí)現(xiàn)代碼
這篇文章主要介紹了javascript制作的網(wǎng)頁(yè)側(cè)邊彈出框思路及實(shí)現(xiàn)代碼,需要的朋友可以參考下2014-05-05javascript動(dòng)態(tài)添加checkbox復(fù)選框的方法
這篇文章主要介紹了javascript動(dòng)態(tài)添加checkbox復(fù)選框的方法的相關(guān)資料,需要的朋友可以參考下2015-12-12javascript 在firebug調(diào)試時(shí)用console.log的方法
當(dāng)你使用console.log()函數(shù)時(shí),下面的firebug一定要打開(kāi),不然這函數(shù)在用firefox運(yùn)行時(shí)無(wú)效且影響正常程序,如果用IE打開(kāi),將會(huì)出錯(cuò)2012-05-05Javascript 高性能之遞歸,迭代,查表法詳解及實(shí)例
這篇文章主要介紹了Javascript 高性能之遞歸,迭代,查表法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-01-01