CodeMirror實現(xiàn)代碼對比功能(插件react vue)
要實現(xiàn)一個需求:一個代碼編輯器,用戶上次寫的代碼和現(xiàn)在的代碼要區(qū)分出不同。網(wǎng)上找了幾個案例,拿過去一用差點氣吐血,報錯像雪花一樣,浪費時間!
本文中的代碼,一鍵粘貼拿來即用,代碼就是我在寫這篇文章時寫的demo,絕無報錯。
1.第一步:下載插件
vue或者react都需要這一步且同樣的下載方式。
npm install diff-match-patch save npm install codemirror save 用yarn的話 npm install 替換成 yarn add …
2.vue代碼如下:
建一個空白.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)在還是一樣的,這幾個字給我標個紅吧!', 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, // 顯示行號 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>
效果圖:
細節(jié)處對比:
3.react hooks代碼如下:
記得先下載第一步的文件然后再一鍵復(fù)制。
對比效果圖如下:
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, // 顯示行號 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.一點心得
使用方法是:
有兩個數(shù)據(jù),newvalue和oldvalue,舊數(shù)據(jù)代表著你上次的數(shù)據(jù),新數(shù)據(jù)代表你現(xiàn)在正在寫的數(shù)據(jù),實際使用中,你先需要保存用戶上次寫的數(shù)據(jù),然后保存用戶新寫的數(shù)據(jù),再去對比。
可能會出現(xiàn)的問題:
看見我的樣式了沒 用的scss有的人項目可能沒用這個,你可以把我這個樣式改寫成普通css形式,反正就幾行改起來很簡單。
到此這篇關(guān)于CodeMirror實現(xiàn)代碼對比功能的文章就介紹到這了,更多相關(guān)CodeMirror代碼對比內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章

JavaScript trim 去除字符串空格的三種方法(附代碼詳解)

javascript制作的網(wǎng)頁側(cè)邊彈出框思路及實現(xiàn)代碼

javascript動態(tài)添加checkbox復(fù)選框的方法

javascript 在firebug調(diào)試時用console.log的方法