Simple use of Codemirror, JS comparison text differences (marked out and modified)

Source: Internet
Author: User

Recently the project needs to use the comparison text difference function, in colleague's recommendation, uses the JS script comparison, therefore codemirror becomes the choice.

Of course, there are other functions in the Codemirror, compare text differences is just one of the features, I do not introduce here, interested can login his official website

http://codemirror.net/for more information.

The first step: Download Codemirror jar package, in the above mentioned official website, I use is codemirror-4.11 this version.

Second step: Introduce it CSS style and JS file on the page

Diff_math_patch.js may not be downloaded in the package, so download it yourself,

<script src="//cdnjs.cloudflare.com/ajax/libs/diff_match_patch/20121119/diff_match_patch.js  "></script>

This is its original path, if it is not found, send an email to contact me.

Step three: Below is the div and the key JS script.

<div Id=view></div><script type="Text/javascript">varValue, ORIG1, Orig2, dv, panes =2, highlight =true, connect =NULL, collapse =false; function Initui () {if(Value = =NULL)return; vartarget = document.getElementById ("View"); Target.innerhtml=""; DV=Codemirror.mergeview (target, {value:value, origleft:panes==3&&!collapse &&!connect? ORIG1:NULL, Orig:orig2, Linenumbers:true, Mode:"text/html", Highlightdifferences:highlight, Connect:connect, collapseidentical:collapse});} function Toggledifferences () {dv.setshowdifferences (Highlight= !highlight);} Window.onload=function () {Value='leftssss\nsssdfd\nsdf\nsdf\nsdsdss\naaasdf\nsdfsd\nbbb\nnaaddddddddddddddddddddddddddddddddddddddd\nadssa\ Nasdas\nasdf\nssss\ndddd\nffff\nccc\naaa\nzzzz\nsss\nbbb\nfff\nvvv\n\ndfddd\nsss\nzzz\noooo\nppp\nyyy\ncccc\ Nssss\nhhhh\nssss'; ORIG1='leftssss\nsssdfd\nsdf\nsdf\nsdsdss\naaasdf\nsdfsd\nbbb\naaddddddddddddddddddddddddddddddddddddddda\nadssa\ Nasdas\nasdf\nssss\ndddd\nffff\nccc\naaa\nzzzz\nsss\nbbb\nfff\nvvv\n\ndfddd\nsss\nzzz\noooo\nppp\nyyyncccc\ Nssss\nhhhh\nsass'; Orig2='rightssss\nsssdfd\nsdf\nsdf\nsdsdss\naas\nsdfsd\nbbb\naaa\nadssa\ Nasdddddddddddddddddddddddddddddddddddddddddddas\nasdf\nsdd\ndffd\nffdg\nccc\naaa\nzzzz\nsss\nbbb\nfff\nvvv\n\ NDFDDD\NSSS\NZS\NOODF\NPPFDGP\NYYYNDFGCC\NSSDFGDSS\NHHSSSSH\NSDFGDFG'; Initui ();}; function Mergeviewheight (mergeview) {function editorheight (editor) {if(!editor)return 0; returneditor.getscrollinfo (). Height; }  returnMath.max (Editorheight (Mergeview.leftoriginal ()), Editorheight (Mergeview.editor ()), Editorheight (Mergeview.rightoriginal ()));} function Resize (mergeview) {varHeight =mergeviewheight (Mergeview);  for(;;) {    if(Mergeview.leftoriginal ()) mergeview.leftoriginal (). SetSize (NULL, height); Mergeview.editor (). SetSize (NULL, height); if(Mergeview.rightoriginal ()) mergeview.rightoriginal (). SetSize (NULL, height); varNewheight =mergeviewheight (Mergeview); if(newheight >= height) Break; ElseHeight =Newheight; } mergeView.wrap.style.height= height +"px";}</script>

, the left is the original text, the right side is the comparison, the difference of the script will be automatically marked, the two sides can move up and down together.

JS in the value of my test data to write casually, the code is basically the demo, everyone in the use of the time as long as the attention, the value of window.onload, and the Orig1,orig2 assignment on the line, replace you want to compare the two text.

If there are bad places, please criticize and correct them.

Simple use of Codemirror, JS comparison text differences (marked out and modified)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.