亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

CodeMirror實現(xiàn)代碼對比功能(插件react vue)

 更新時間:2022年05月11日 10:14:43   作者:接著奏樂接著舞。  
這篇文章主要介紹了CodeMirror實現(xiàn)代碼對比功能,用到的插件有vue或者react都需要這一步且同樣的下載方式,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

要實現(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 trim 去除字符串空格的三種方法(附代碼詳解)

    個人認為最好的方法.采用的是正則表達式,這是最核心的原理.因為空格有多種形式。
    2010-05-05
  • 火狐下input焦點無法重復(fù)獲取問題的解決方法

    火狐下input焦點無法重復(fù)獲取問題的解決方法

    input輸入框顯示的時候,需要自動獲取焦點,用focus可以輕松搞定,但在火狐下input無法獲取焦點,下面與大家分享下不錯的解決方法
    2014-06-06
  • javascript制作的網(wǎng)頁側(cè)邊彈出框思路及實現(xiàn)代碼

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

    這篇文章主要介紹了javascript制作的網(wǎng)頁側(cè)邊彈出框思路及實現(xiàn)代碼,需要的朋友可以參考下
    2014-05-05
  • JS中位置與大小的獲取方法

    JS中位置與大小的獲取方法

    下面小編就為大家?guī)硪黄狫S中位置與大小的獲取方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • javascript動態(tài)添加checkbox復(fù)選框的方法

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

    這篇文章主要介紹了javascript動態(tài)添加checkbox復(fù)選框的方法的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • javascript 在firebug調(diào)試時用console.log的方法

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

    當你使用console.log()函數(shù)時,下面的firebug一定要打開,不然這函數(shù)在用firefox運行時無效且影響正常程序,如果用IE打開,將會出錯
    2012-05-05
  • Javascript 高性能之遞歸,迭代,查表法詳解及實例

    Javascript 高性能之遞歸,迭代,查表法詳解及實例

    這篇文章主要介紹了Javascript 高性能之遞歸,迭代,查表法詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • 淺析JavaScript中的類型和對象

    淺析JavaScript中的類型和對象

    這篇文章主要介紹了一些關(guān)于類型和對象的例子,大家看完例子后可能更容易理解類型和對象之間的聯(lián)系
    2013-11-11
  • 最新評論