赞
踩
先上效果
有本地和远程,不同的位置会有背景色并且颜色标识
接下来上代码
npm install codemirror
yarn add codemirror
import { useEffect, 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'; import styles from './index.less'; export default function CodeMirrorComps(props: any) { const { firstValue, secondValue } = props; window.diff_match_patch = DiffMatchPatch; window.DIFF_DELETE = -1; window.DIFF_INSERT = 1; window.DIFF_EQUAL = 0; useEffect(() => { initUI(); }, [firstValue, secondValue]); const codeMirror = useRef(null); const initUI = () => { const target: any = codeMirror.current; target.innerHTML = ''; CodeMirror.MergeView(target, { value: firstValue ?? '', origLeft: null, orig: secondValue ?? '', lineNumbers: true, // 显示行号 mode: 'text/html', highlightDifferences: true, connect: 'align', readOnly: true, // 只读 不可修改 }); }; return ( <div style={{ width: '100%', height: '100%' }}> <div ref={codeMirror} className={styles['code-contrast']} style={{ width: '100%', height: '100%' }} ></div> </div> ); }
.CodeMirror-merge-r-chunk { background: rgba(30, 144, 255, 0.5); } .CodeMirror-merge-r-chunk-start { border-top: 1px solid dodgerblue; } .CodeMirror-merge-r-chunk-end { border-bottom: 1px solid dodgerblue; } .CodeMirror-merge-r-connect { fill: rgba(30, 144, 255, 0.5); stroke: rgba(30, 144, 255, 0.5); stroke-width: 1px; } .CodeMirror-merge-l-chunk { background: rgba(30, 144, 255, 0.5); } .CodeMirror-merge-l-chunk-start { border-top: 1px solid dodgerblue; } .CodeMirror-merge-l-chunk-end { border-bottom: 1px solid dodgerblue; } .CodeMirror-merge-l-connect { fill: rgba(30, 144, 255, 0.5); stroke: rgba(30, 144, 255, 0.5); stroke-width: 1px; } .CodeMirror-merge-l-chunk { background: rgba(30, 144, 255, 0.5); } .CodeMirror-merge-r-chunk { background: rgba(30, 144, 255, 0.5); } .CodeMirror-merge-l-chunk-start { border-top: 1px solid dodgerblue; } .CodeMirror-merge-r-chunk-start { border-top: 1px solid dodgerblue; } .CodeMirror-merge-l-chunk-end { border-bottom: 1px solid dodgerblue; } .CodeMirror-merge-r-chunk-end { border-bottom: 1px solid dodgerblue; } .code-contrast .CodeMirror-merge-copy, .CodeMirror-merge-scrolllock-wrap { display: none !important; } .code-contrast { :global { .CodeMirror-merge { border: none; } .CodeMirror-merge { height: 100% !important; } .CodeMirror-merge .CodeMirror { height: 100% !important; } .CodeMirror-merge-gap { width: 2%; height: 0px !important; } .CodeMirror-merge-2pane .CodeMirror-merge-pane { border-radius: 2px; border: 1px solid #edeef6; width: 49%; } } }
样式可以根据自己的需求再去调整
其实就是根据 CodeMirror这个插件来做的,这个插件库很强大,有时间再跟大家讲下其他功能
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。