UNPKG

ace-diff

Version:

A diff/merging wrapper for Ace Editor built on google-diff-match-patch

4 lines 25.6 kB
function e(e){let[t,n]=e;return[t,n]}function t(e,t){let n=e,r=t,i=n.length,a=r.length;if(i===0||a===0)return 0;i>a?n=n.substring(i-a):i<a&&(r=r.substring(0,i));let o=Math.min(i,a);if(n===r)return o;let s=0,c=1;for(let e=0;e!==-1;){let t=n.substring(o-c);if(e=r.indexOf(t),e===-1)return s;c+=e,(e===0||n.substring(o-c)===r.substring(0,c))&&(s=c,c++)}return s}function n(e,t){if(!e||!t||e[0]!==t[0])return 0;let n=0,r=Math.min(e.length,t.length),i=r,a=0;for(;n<i;)e.substring(a,i)===t.substring(a,i)?(n=i,a=n):r=i,i=Math.floor((r-n)/2+n);return i}function r(e,t){if(!e||!t||e[e.length-1]!==t[t.length-1])return 0;let n=0,r=Math.min(e.length,t.length),i=r,a=0;for(;n<i;)e.substring(e.length-i,e.length-a)===t.substring(t.length-i,t.length-a)?(n=i,a=n):r=i,i=Math.floor((r-n)/2+n);return i}function i(e){let t=e.charCodeAt(0);return t>=55296&&t<=56319}function a(e){let t=e.charCodeAt(0);return t>=56320&&t<=57343}function o(e,t,n){let r=e.length,i=t.length,a=Math.ceil((r+i)/2),o=a,c=2*a,l=Array(c),u=Array(c);for(let e=0;e<c;e++)l[e]=-1,u[e]=-1;l[o+1]=0,u[o+1]=0;let d=r-i,f=d%2!=0,p=0,m=0,h=0,g=0;for(let _=0;_<a&&!(Date.now()>n);_++){for(let a=-_+p;a<=_-m;a+=2){let h=o+a,g;g=a===-_||a!==_&&l[h-1]<l[h+1]?l[h+1]:l[h-1]+1;let v=g-a;for(;g<r&&v<i&&e.charAt(g)===t.charAt(v);)g++,v++;if(l[h]=g,g>r)m+=2;else if(v>i)p+=2;else if(f){let i=o+d-a;if(i>=0&&i<c&&u[i]!==-1){let a=r-u[i];if(g>=a)return s(e,t,g,v,n)}}}for(let a=-_+h;a<=_-g;a+=2){let p=o+a,m;m=a===-_||a!==_&&u[p-1]<u[p+1]?u[p+1]:u[p-1]+1;let v=m-a;for(;m<r&&v<i&&e.charAt(r-m-1)===t.charAt(i-v-1);)m++,v++;if(u[p]=m,m>r)g+=2;else if(v>i)h+=2;else if(!f){let i=o+d-a;if(i>=0&&i<c&&l[i]!==-1){let a=l[i],c=o+a-i;if(m=r-m,a>=m)return s(e,t,a,c,n)}}}}return[[y,e],[b,t]]}function s(e,t,n,r,i){let a=e.substring(0,n),o=t.substring(0,r),s=e.substring(n),c=t.substring(r),l=C(a,o,{checkLines:!1,deadline:i}),u=C(s,c,{checkLines:!1,deadline:i});return l.concat(u)}function c(e,t,n=1){if(n<=0)return null;let r=e.length>t.length?e:t,i=e.length>t.length?t:e;if(r.length<4||i.length*2<r.length)return null;let a=l(r,i,Math.ceil(r.length/4)),o=l(r,i,Math.ceil(r.length/2)),s;if(a&&o)s=a[4].length>o[4].length?a:o;else{if(!a&&!o)return null;o?a||(s=o):s=a}if(!s)throw Error(`Unable to find a half match.`);let c,u,d,f;e.length>t.length?(c=s[0],u=s[1],d=s[2],f=s[3]):(d=s[0],f=s[1],c=s[2],u=s[3]);let p=s[4];return[c,u,d,f,p]}function l(e,t,i){let a=e.slice(i,i+Math.floor(e.length/4)),o=-1,s=``,c,l,u,d;for(;(o=t.indexOf(a,o+1))!==-1;){let a=n(e.slice(i),t.slice(o)),f=r(e.slice(0,i),t.slice(0,o));s.length<f+a&&(s=t.slice(o-f,o)+t.slice(o,o+a),c=e.slice(0,i-f),l=e.slice(i+a),u=t.slice(0,o-f),d=t.slice(o+a))}return s.length*2>=e.length?[c||``,l||``,u||``,d||``,s||``]:null}function u(e,t){for(let n=0;n<e.length;n++){let r=e[n][1],i=[];for(let e=0;e<r.length;e++)i[e]=t[r.charCodeAt(e)];e[n][1]=i.join(``)}}function d(e,t){let n=[],r={};n[0]=``;function i(e){let t=``,i=0,o=-1,s=n.length;for(;o<e.length-1;){o=e.indexOf(` `,i),o===-1&&(o=e.length-1);let c=e.slice(i,o+1);(r.hasOwnProperty?r.hasOwnProperty(c):r[c]!==void 0)?t+=String.fromCharCode(r[c]):(s===a&&(c=e.slice(i),o=e.length),t+=String.fromCharCode(s),r[c]=s,n[s++]=c),i=o+1}return t}let a=4e4,o=i(e);return a=65535,{chars1:o,chars2:i(t),lineArray:n}}function f(e,t,n){let r=e,i=t,a=d(r,i);r=a.chars1,i=a.chars2;let o=a.lineArray,s=C(r,i,{checkLines:!1,deadline:n.deadline});u(s,o),s=A(s),s.push([x,``]);let c=0,l=0,f=0,p=``,m=``;for(;c<s.length;){switch(s[c][0]){case b:f++,m+=s[c][1];break;case y:l++,p+=s[c][1];break;case x:if(l>=1&&f>=1){s.splice(c-l-f,l+f),c=c-l-f;let e=C(p,m,{checkLines:!1,deadline:n.deadline});for(let t=e.length-1;t>=0;t--)s.splice(c,0,e[t]);c+=e.length}f=0,l=0,p=``,m=``;break;default:throw Error(`Unknown diff operation.`)}c++}return s.pop(),s}function p(e,t,n){let r;if(!e)return[[b,t]];if(!t)return[[y,e]];let i=e.length>t.length?e:t,a=e.length>t.length?t:e,s=i.indexOf(a);if(s!==-1)return r=[[b,i.substring(0,s)],[x,a],[b,i.substring(s+a.length)]],e.length>t.length&&(r[0][0]=y,r[2][0]=y),r;if(a.length===1)return[[y,e],[b,t]];let l=c(e,t);if(l){let e=l[0],t=l[1],r=l[2],i=l[3],a=l[4],o=C(e,r,n),s=C(t,i,n);return o.concat([[x,a]],s)}return n.checkLines&&e.length>100&&t.length>100?f(e,t,n):o(e,t,n.deadline)}var m=Object.defineProperty,h=Object.getOwnPropertySymbols,g=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable,v=(e,t,n)=>t in e?m(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,ee=(e,t)=>{for(var n in t||={})g.call(t,n)&&v(e,n,t[n]);if(h)for(var n of h(t))_.call(t,n)&&v(e,n,t[n]);return e};const y=-1,b=1,x=0;function S(e,t,n){if(e===null||t===null)throw Error(`Null input. (diff)`);let r=C(e,t,w(n||{}));return k(r),r}function C(e,t,i){let a=e,o=t;if(a===o)return a?[[0,a]]:[];let s=n(a,o),c=a.substring(0,s);a=a.substring(s),o=o.substring(s),s=r(a,o);let l=a.substring(a.length-s);a=a.substring(0,a.length-s),o=o.substring(0,o.length-s);let u=p(a,o,i);return c&&u.unshift([0,c]),l&&u.push([0,l]),u=L(u),u}function te(e){let t=1;return typeof e<`u`&&(t=e<=0?Number.MAX_VALUE:e),Date.now()+t*1e3}function w(e){return ee({checkLines:!0,deadline:te(e.timeout||1)},e)}function T(e,t,n){return n===1?e+t:t+e}function E(e,t){return t===1?[e.substring(0,e.length-1),e[e.length-1]]:[e.substring(1),e[0]]}function D(e,t,n,r){return r===1?e[t][1][e[t][1].length-1]===e[n][1][e[n][1].length-1]:e[t][1][0]===e[n][1][0]}function O(e,t,n){let r=n===1?-1:1,i=null,a=null,o=t+n;for(;o>=0&&o<e.length&&(i===null||a===null);o+=n){let[s,c]=e[o];if(c.length!==0){if(s===1){i===null&&(i=o);continue}else if(s===-1){a===null&&(a=o);continue}else if(s===0){if(i===null&&a===null){let[i,a]=E(e[t][1],n);e[t][1]=i,e[o][1]=T(e[o][1],a,r);return}break}}}if(i!==null&&a!==null&&D(e,i,a,n)){let[o,s]=E(e[i][1],r),[c]=E(e[a][1],r);e[i][1]=o,e[a][1]=c,e[t][1]=T(e[t][1],s,n);return}let[s,c]=E(e[t][1],n);e[t][1]=s,i===null?(e.splice(o,0,[1,c]),a!==null&&a>=o&&a++):e[i][1]=T(e[i][1],c,r),a===null?e.splice(o,0,[-1,c]):e[a][1]=T(e[a][1],c,r)}function k(e){for(let t=0;t<e.length;t++){let[n,r]=e[t];if(r.length===0)continue;let o=r[0],s=r[r.length-1];i(s)&&n===0&&O(e,t,1),a(o)&&n===0&&O(e,t,-1)}for(let t=0;t<e.length;t++)e[t][1].length===0&&e.splice(t,1)}function A(n){let r=n.map(t=>e(t)),i=!1,a=[],o=0,s=null,c=0,l=0,u=0,d=0,f=0;for(;c<r.length;)r[c][0]===0?(a[o++]=c,l=d,u=f,d=0,f=0,s=r[c][1]):(r[c][0]===1?d+=r[c][1].length:f+=r[c][1].length,s&&s.length<=Math.max(l,u)&&s.length<=Math.max(d,f)&&(r.splice(a[o-1],0,[-1,s]),r[a[o-1]+1][0]=1,o--,o--,c=o>0?a[o-1]:-1,l=0,u=0,d=0,f=0,s=null,i=!0)),c++;for(i&&(r=L(r)),r=I(r),c=1;c<r.length;){if(r[c-1][0]===-1&&r[c][0]===1){let e=r[c-1][1],n=r[c][1],i=t(e,n),a=t(n,e);i>=a?(i>=e.length/2||i>=n.length/2)&&(r.splice(c,0,[0,n.substring(0,i)]),r[c-1][1]=e.substring(0,e.length-i),r[c+1][1]=n.substring(i),c++):(a>=e.length/2||a>=n.length/2)&&(r.splice(c,0,[0,e.substring(0,a)]),r[c-1][0]=1,r[c-1][1]=n.substring(0,n.length-a),r[c+1][0]=-1,r[c+1][1]=e.substring(a),c++),c++}c++}return r}const j=/[^a-zA-Z0-9]/,M=/\s/,N=/[\r\n]/,P=/\n\r?\n$/,F=/^\r?\n\r?\n/;function I(t){let n=t.map(t=>e(t));function i(e,t){if(!e||!t)return 6;let n=e.charAt(e.length-1),r=t.charAt(0),i=n.match(j),a=r.match(j),o=i&&n.match(M),s=a&&r.match(M),c=o&&n.match(N),l=s&&r.match(N),u=c&&e.match(P),d=l&&t.match(F);return u||d?5:c||l?4:i&&!o&&s?3:o||s?2:i||a?1:0}let a=1;for(;a<n.length-1;){if(n[a-1][0]===0&&n[a+1][0]===0){let e=n[a-1][1],t=n[a][1],o=n[a+1][1],s=r(e,t);if(s){let n=t.substring(t.length-s);e=e.substring(0,e.length-s),t=n+t.substring(0,t.length-s),o=n+o}let c=e,l=t,u=o,d=i(e,t)+i(t,o);for(;t.charAt(0)===o.charAt(0);){e+=t.charAt(0),t=t.substring(1)+o.charAt(0),o=o.substring(1);let n=i(e,t)+i(t,o);n>=d&&(d=n,c=e,l=t,u=o)}n[a-1][1]!==c&&(c?n[a-1][1]=c:(n.splice(a-1,1),a--),n[a][1]=l,u?n[a+1][1]=u:(n.splice(a+1,1),a--))}a++}return n}function L(t){let i=t.map(t=>e(t));i.push([0,``]);let a=0,o=0,s=0,c=``,l=``,u;for(;a<i.length;)switch(i[a][0]){case 1:s++,l+=i[a][1],a++;break;case-1:o++,c+=i[a][1],a++;break;case 0:o+s>1?(o!==0&&s!==0&&(u=n(l,c),u!==0&&(a-o-s>0&&i[a-o-s-1][0]===0?i[a-o-s-1][1]+=l.substring(0,u):(i.splice(0,0,[0,l.substring(0,u)]),a++),l=l.substring(u),c=c.substring(u)),u=r(l,c),u!==0&&(i[a][1]=l.substring(l.length-u)+i[a][1],l=l.substring(0,l.length-u),c=c.substring(0,c.length-u))),a-=o+s,i.splice(a,o+s),c.length&&(i.splice(a,0,[-1,c]),a++),l.length&&(i.splice(a,0,[1,l]),a++),a++):a!==0&&i[a-1][0]===0?(i[a-1][1]+=i[a][1],i.splice(a,1)):a++,s=0,o=0,c=``,l=``;break;default:throw Error(`Unknown diff operation`)}i[i.length-1][1]===``&&i.pop();let d=!1;for(a=1;a<i.length-1;)i[a-1][0]===0&&i[a+1][0]===0&&(i[a][1].substring(i[a][1].length-i[a-1][1].length)===i[a-1][1]?(i[a][1]=i[a-1][1]+i[a][1].substring(0,i[a][1].length-i[a-1][1].length),i[a+1][1]=i[a-1][1]+i[a+1][1],i.splice(a-1,1),d=!0):i[a][1].substring(0,i[a+1][1].length)===i[a+1][1]&&(i[a-1][1]+=i[a+1][1],i[a][1]=i[a][1].substring(i[a+1][1].length)+i[a+1][1],i.splice(a+1,1),d=!0)),a++;return d&&(i=L(i)),i}function R(e,t,n=!1){let r=null,i=!0;return function(...a){let o=n&&i,s=()=>{e.apply(this,a),r=null};o&&(i=!1,s()),r||=setTimeout(s,t)}}function z(e,t){let n;return function(...r){clearTimeout(n),n=setTimeout(()=>e.apply(this,r),t)}}function B(e=``){return(e??``).replace(/\r\n/g,` `)}function V(e,t,n,r){let i=e+(n-e)/2;return`M ${e} ${t} C ${i},${t} ${i},${r} ${n},${r}`}const H=`right`,U=`left`,W=`specific`,G=`broad`;var K={DIFF_EQUAL:0,DIFF_DELETE:-1,DIFF_INSERT:1,EDITOR_RIGHT:H,EDITOR_LEFT:U,RTL:`rtl`,LTR:`ltr`,SVG_NS:`http://www.w3.org/2000/svg`,DIFF_GRANULARITY_SPECIFIC:W,DIFF_GRANULARITY_BROAD:G};function q(e,t){let{mode:n}=e.options;return t===U&&e.options.left.mode!==null&&(n=e.options.left.mode??n),t===H&&e.options.right.mode!==null&&(n=e.options.right.mode??n),n}function J(e,t){let{theme:n}=e.options;return t===U&&e.options.left.theme!==null&&(n=e.options.left.theme??n),t===H&&e.options.right.theme!==null&&(n=e.options.right.theme??n),n}function ne(e,t){return e.ace.getSession().doc.getLine(t)}function re(e){let t=e.options.left.id;return t?document.getElementById(t)?.offsetHeight??0:0}function Y(e){let t=document.createElement(`div`),n={class:e.className,style:`top:${e.topOffset}px`,title:e.tooltip,"data-diff-index":e.diffIndex};for(let e in n)t.setAttribute(e,String(n[e]));return t.innerHTML=e.arrowContent,t}function X(e,t){let n=`js-${t}-${Math.random().toString(36).substr(2,5)}`,r=e.querySelector(`.${t}`);if(r)return r.id=r.id||n,r.id;let i=document.createElement(`div`);return e.appendChild(i),i.className=t,i.id=n,i.id}function Z(e,t,n,r){let i=e===`document`?document:document.querySelector(e);i&&i.addEventListener(t,e=>{let t=i.querySelectorAll(n),a=e.target;for(let n=0,o=t.length;n<o;n+=1){let o=a,s=t[n];for(;o&&o!==i;)o===s&&r.call(s,e),o=o.parentNode}})}let Q=null;function ie(e){if(e.Range)return e.Range;let t=e.acequire||e.require;return t?t(`ace/range`):!1}const ae={ace:void 0,mode:null,theme:null,element:null,diffGranularity:G,lockScrolling:!0,showDiffs:!0,showConnectors:!0,charDiffs:!0,maxDiffs:5e3,left:{id:null,content:null,mode:null,theme:null,editable:!0,copyLinkEnabled:!0},right:{id:null,content:null,mode:null,theme:null,editable:!0,copyLinkEnabled:!0},classes:{gutterID:`acediff__gutter`,diff:`acediff__diffLine`,diffChar:`acediff__diffChar`,diffGutter:`acediff__diffGutter`,connector:`acediff__connector`,newCodeConnectorLink:`acediff__newCodeConnector`,newCodeConnectorLinkContent:`&#8594;`,deletedCodeConnectorLink:`acediff__deletedCodeConnector`,deletedCodeConnectorLinkContent:`&#8592;`,copyRightContainer:`acediff__copy--right`,copyLeftContainer:`acediff__copy--left`},connectorYOffset:0,onDiffReady:null};var $=class{options;el;editors;diffs=[];lineHeight=0;gutterSVG=null;gutterWidth=0;gutterHeight=0;copyLeftContainer=null;copyRightContainer=null;connectorYOffset=0;removeEventHandlers=null;constructor(e={}){let t=JSON.parse(JSON.stringify(ae));this.options={...t,...e,left:{...t.left,...e.left},right:{...t.right,...e.right},classes:{...t.classes,...e.classes}};let n=()=>typeof window<`u`?window.ace:void 0;this.options.ace||(this.options.ace=n());let{ace:r}=this.options;if(!r){let e="No ace editor found nor supplied - `options.ace` or `window.ace` is missing";throw console.error(e),Error(e)}let i=ie(r);if(!i){let e=`Could not require Range module for Ace. Depends on your bundling strategy, but it usually comes with Ace itself. See https://ace.c9.io/api/range.html, open an issue on GitHub ace-diff/ace-diff`;throw console.error(e),Error(e)}if(Q=i,this.options.element===null){let e="You need to specify an element for Ace-diff - `options.element` is missing";throw console.error(e),Error(e)}if(this.options.element instanceof HTMLElement)this.el=this.options.element;else{let e=document.body.querySelector(this.options.element);if(!e){let e=`Can't find the specified element ${this.options.element}`;throw console.error(e),Error(e)}this.el=e}this.options.left.id=X(this.el,`acediff__left`),this.options.classes.gutterID=X(this.el,`acediff__gutter`),this.options.right.id=X(this.el,`acediff__right`),this.el.innerHTML=`<div class="acediff acediff__wrap">${this.el.innerHTML}</div>`,this.editors={left:{ace:r.edit(this.options.left.id),markers:[],lineLengths:[],diffGutters:[]},right:{ace:r.edit(this.options.right.id),markers:[],lineLengths:[],diffGutters:[]},editorHeight:null},this.editors.left.ace.getSession().setMode(q(this,U)??``),this.editors.right.ace.getSession().setMode(q(this,H)??``),this.editors.left.ace.setReadOnly(!this.options.left.editable),this.editors.right.ace.setReadOnly(!this.options.right.editable),this.editors.left.ace.setShowFoldWidgets(!1),this.editors.right.ace.setShowFoldWidgets(!1),this.editors.left.ace.setTheme(J(this,U)??``),this.editors.right.ace.setTheme(J(this,H)??``),this.editors.left.ace.setValue(B(this.options.left.content??null),-1),this.editors.right.ace.setValue(B(this.options.right.content??null),-1),this.editors.editorHeight=re(this),setTimeout(()=>{this.lineHeight=this.editors.left.ace.renderer.lineHeight,this.addEventHandlers(),this.createCopyContainers(),this.createGutter(),this.diff()},1)}setOptions(e){this.options={...this.options,...e,left:{...this.options.left,...e.left},right:{...this.options.right,...e.right},classes:{...this.options.classes,...e.classes}},this.diff()}getNumDiffs(){return this.diffs.length}getEditors(){return{left:this.editors.left.ace,right:this.editors.right.ace}}diff(){let e=this.editors.left.ace.getSession().getValue(),t=A(S(this.editors.right.ace.getSession().getValue(),e));this.editors.left.lineLengths=this.getLineLengths(this.editors.left),this.editors.right.lineLengths=this.getLineLengths(this.editors.right);let n=[],r={left:0,right:0};t.forEach(e=>{let t=e[0],i=e[1];i.length!==0&&(t===0?(r.left+=i.length,r.right+=i.length):t===-1?(n.push(this.computeDiff(-1,r.left,r.right,i)),r.right+=i.length):t===1&&(n.push(this.computeDiff(1,r.left,r.right,i)),r.left+=i.length))}),this.diffs=this.simplifyDiffs(n),!(this.diffs.length>this.options.maxDiffs)&&(this.clearDiffs(),this.decorate(),typeof this.options.onDiffReady==`function`&&this.options.onDiffReady(this.diffs))}clear(){this.clearDiffs(),this.clearGutter(),this.clearArrows()}destroy(){let e=this.editors.left.ace.getValue();this.editors.left.ace.destroy();let t=this.editors.left.ace.container,n=t.cloneNode(!1);n.textContent=e,t.parentNode?.replaceChild(n,t);let r=this.editors.right.ace.getValue();this.editors.right.ace.destroy(),t=this.editors.right.ace.container,n=t.cloneNode(!1),n.textContent=r,t.parentNode?.replaceChild(n,t);let i=document.getElementById(this.options.classes.gutterID);i&&(i.innerHTML=``),this.removeEventHandlers?.()}addEventHandlers(){let e=!1,t=(t,n)=>{if(!this.options.lockScrolling||e)return;let r=t.ace.getSession(),i=n.ace.getSession(),a=r.getScrollTop(),o=r.getLength()*this.lineHeight,s=t.ace.renderer.$size.scrollerHeight,c=Math.max(0,o-s),l=c>0?a/c:0,u=i.getLength()*this.lineHeight,d=n.ace.renderer.$size.scrollerHeight,f=l*Math.max(0,u-d);e=!0,i.setScrollTop(f),e=!1};this.editors.left.ace.getSession().on(`changeScrollTop`,R(()=>{t(this.editors.left,this.editors.right),this.updateGap()},16)),this.editors.right.ace.getSession().on(`changeScrollTop`,R(()=>{t(this.editors.right,this.editors.left),this.updateGap()},16));let n=this.diff.bind(this);this.editors.left.ace.on(`change`,n),this.editors.right.ace.on(`change`,n),this.options.left.copyLinkEnabled&&Z(`#${this.options.classes.gutterID}`,`click`,`.${this.options.classes.newCodeConnectorLink}`,e=>this.copy(e,`ltr`)),this.options.right.copyLinkEnabled&&Z(`#${this.options.classes.gutterID}`,`click`,`.${this.options.classes.deletedCodeConnectorLink}`,e=>this.copy(e,`rtl`));let r=z(()=>{let e=document.getElementById(this.options.left.id);e&&(this.editors.availableHeight=e.offsetHeight),this.diff()},250);window.addEventListener(`resize`,r),this.removeEventHandlers=()=>{window.removeEventListener(`resize`,r)}}copy(e,t){let n=e.target,r=parseInt(n.getAttribute(`data-diff-index`)??`0`,10),i=this.diffs[r];if(!i||t===`ltr`&&!this.options.right.editable||t===`rtl`&&!this.options.left.editable)return;let a,o,s,c,l,u;t===`ltr`?(a=this.editors.left,o=this.editors.right,s=i.leftStartOffset,c=i.leftEndOffset,l=i.rightStartOffset,u=i.rightEndOffset):(a=this.editors.right,o=this.editors.left,s=i.rightStartOffset,c=i.rightEndOffset,l=i.leftStartOffset,u=i.leftEndOffset);let d=a.ace.getValue().substring(s,c),f=o.ace.getSession().doc,p=f.indexToPosition(l,0),m=f.indexToPosition(u,0),h=o.ace.getSession().getScrollTop();Q&&o.ace.getSession().replace(new Q(p.row,p.column,m.row,m.column),d),o.ace.getSession().setScrollTop(parseInt(String(h),10)),this.diff()}getLineLengths(e){return e.ace.getSession().doc.getAllLines().map(e=>e.length+1)}showDiff(e,t,n,r,i){let a=this.editors[e],o=n;o<t&&(o=t);let s=`${i} ${o>t?`lines`:`targetOnly`} ${e}`,c=o;if(c>t&&--c,Q&&a.markers.push(a.ace.session.addMarker(new Q(t,0,c,1),s,`fullLine`)),this.options.charDiffs&&r&&r.length>0){let t=`${this.options.classes.diffChar} ${e}`;r.forEach(e=>{Q&&a.markers.push(a.ace.session.addMarker(new Q(e.lineStart,e.start,e.lineEnd-1,e.end),t,`text`))})}let l=`${this.options.classes.diffGutter} ${e}`;for(let e=t;e<o;e+=1)a.ace.session.addGutterDecoration(e,l),a.diffGutters.push({line:e,className:l})}updateGap(){this.clearDiffs(),this.decorate(),this.positionCopyContainers()}clearDiffs(){this.editors.left.markers.forEach(e=>{this.editors.left.ace.getSession().removeMarker(e)}),this.editors.right.markers.forEach(e=>{this.editors.right.ace.getSession().removeMarker(e)}),this.editors.left.markers=[],this.editors.right.markers=[],this.editors.left.diffGutters.forEach(e=>{this.editors.left.ace.session.removeGutterDecoration(e.line,e.className)}),this.editors.right.diffGutters.forEach(e=>{this.editors.right.ace.session.removeGutterDecoration(e.line,e.className)}),this.editors.left.diffGutters=[],this.editors.right.diffGutters=[]}addConnector(e,t,n,r){let i=this.editors.left.ace.getSession().getScrollTop(),a=this.editors.right.ace.getSession().getScrollTop();this.connectorYOffset=1;let o=e*this.lineHeight-i+.5,s=this.gutterWidth+1,c=n*this.lineHeight-a+.5,l=t*this.lineHeight-i+this.connectorYOffset+.5,u=this.gutterWidth+1,d=r*this.lineHeight-a+this.connectorYOffset+.5,f=V(-1,o,s,c),p=V(u,d,-1,l),m=`${f} ${`L${s},${c} ${u},${d}`} ${p} ${`L-1,${l} -1,${o}`}`,h=document.createElementNS(K.SVG_NS,`path`);h.setAttribute(`d`,m),h.setAttribute(`class`,this.options.classes.connector??``),this.gutterSVG?.appendChild(h)}addCopyArrows(e,t){if(e.leftEndLine>e.leftStartLine&&this.options.left.copyLinkEnabled&&this.options.right.editable){let n=Y({className:this.options.classes.newCodeConnectorLink??``,topOffset:e.leftStartLine*this.lineHeight,tooltip:`Copy to right`,diffIndex:t,arrowContent:this.options.classes.newCodeConnectorLinkContent??``});this.copyRightContainer?.appendChild(n)}if(e.rightEndLine>e.rightStartLine&&this.options.right.copyLinkEnabled&&this.options.left.editable){let n=Y({className:this.options.classes.deletedCodeConnectorLink??``,topOffset:e.rightStartLine*this.lineHeight,tooltip:`Copy to left`,diffIndex:t,arrowContent:this.options.classes.deletedCodeConnectorLinkContent??``});this.copyLeftContainer?.appendChild(n)}}positionCopyContainers(){let e=this.editors.left.ace.getSession().getScrollTop(),t=this.editors.right.ace.getSession().getScrollTop();this.copyRightContainer&&(this.copyRightContainer.style.cssText=`top: ${-e}px`),this.copyLeftContainer&&(this.copyLeftContainer.style.cssText=`top: ${-t}px`)}computeDiff(e,t,n,r){let i={};if(e===1){let e=this.getSingleDiffInfo(this.editors.left,t,r),a=this.getLineForCharPosition(this.editors.right,n),o=this.getCharsOnLine(this.editors.right,a),s=this.getCharsOnLine(this.editors.left,e.startLine),c=a,l=e.startLine===e.endLine,u=0;(e.startChar>0||l&&r.length<s)&&o>0&&e.startChar<s&&u++,i={leftStartLine:e.startLine,leftEndLine:e.endLine+1,rightStartLine:c,rightEndLine:c+u,leftStartOffset:t,leftEndOffset:t+r.length,rightStartOffset:n,rightEndOffset:n,leftStartChar:e.startChar,leftEndChar:e.endChar}}else{let e=this.getSingleDiffInfo(this.editors.right,n,r),a=this.getLineForCharPosition(this.editors.left,t),o=this.getCharsOnLine(this.editors.left,a),s=this.getCharsOnLine(this.editors.right,e.startLine),c=a,l=e.startLine===e.endLine,u=0;(e.startChar>0||l&&r.length<s)&&o>0&&e.startChar<s&&u++,i={leftStartLine:c,leftEndLine:c+u,rightStartLine:e.startLine,rightEndLine:e.endLine+1,leftStartOffset:t,leftEndOffset:t,rightStartOffset:n,rightEndOffset:n+r.length,rightStartChar:e.startChar,rightEndChar:e.endChar}}return i}getSingleDiffInfo(e,t,n){let r={startLine:0,startChar:0,endLine:0,endChar:0},i=t+n.length,a=0,o=!1,s=!1;e.lineLengths.forEach((e,n)=>{a+=e,!o&&t<a&&(r.startLine=n,r.startChar=t-a+e,o=!0),!s&&i<=a&&(r.endLine=n,r.endChar=i-a+e,s=!0)}),r.startChar>0&&this.getCharsOnLine(e,r.startLine)===r.startChar&&(r.startLine++,r.startChar=0),r.endChar===0&&r.endLine--;let c=/\n$/.test(n);return r.startChar>0&&c&&r.endLine++,r}getCharsOnLine(e,t){return ne(e,t).length}getLineForCharPosition(e,t){let n=e.ace.getSession().doc.getAllLines(),r=0,i=0;for(let e=0;e<n.length;e+=1){let a=n[e];if(a!==void 0&&(i+=a.length+1),t<=i){r=e,t===i&&e<n.length-1&&(r+=1);break}}return i>=e.ace.getSession().getValue().length&&(r+=1),r}createGutter(){let e=document.getElementById(this.options.classes.gutterID);if(!e)return;this.gutterHeight=e.clientHeight,this.gutterWidth=e.clientWidth;let t=this.getTotalHeight(U),n=this.getTotalHeight(H),r=Math.max(t,n,this.gutterHeight);this.gutterSVG=document.createElementNS(K.SVG_NS,`svg`),this.gutterSVG.setAttribute(`width`,String(this.gutterWidth)),this.gutterSVG.setAttribute(`height`,String(r)),e.appendChild(this.gutterSVG)}getTotalHeight(e){return(e===U?this.editors.left:this.editors.right).ace.getSession().getLength()*this.lineHeight}createCopyContainers(){this.copyRightContainer=document.createElement(`div`),this.copyRightContainer.setAttribute(`class`,this.options.classes.copyRightContainer??``),this.copyLeftContainer=document.createElement(`div`),this.copyLeftContainer.setAttribute(`class`,this.options.classes.copyLeftContainer??``);let e=document.getElementById(this.options.classes.gutterID);e&&(e.appendChild(this.copyRightContainer),e.appendChild(this.copyLeftContainer))}clearGutter(){let e=document.getElementById(this.options.classes.gutterID);e&&this.gutterSVG&&e.removeChild(this.gutterSVG),this.createGutter()}clearArrows(){this.copyLeftContainer&&(this.copyLeftContainer.innerHTML=``),this.copyRightContainer&&(this.copyRightContainer.innerHTML=``)}simplifyDiffs(e){let t=[],n=e=>this.options.diffGranularity===W?e<1:e<=1,r=e=>{let t={...e,leftChars:[],rightChars:[]};return e.leftEndChar!==void 0&&t.leftChars.push({start:e.leftStartChar??0,end:e.leftEndChar,lineStart:e.leftStartLine??0,lineEnd:e.leftEndLine??0}),e.rightEndChar!==void 0&&t.rightChars.push({start:e.rightStartChar??0,end:e.rightEndChar,lineStart:e.rightStartLine??0,lineEnd:e.rightEndLine??0}),t};return e.forEach((e,i)=>{if(i===0){t.push(r(e));return}let a=!1;for(let r=0;r<t.length;r+=1)if(n(Math.abs(e.leftStartLine-t[r].leftEndLine))&&n(Math.abs(e.rightStartLine-t[r].rightEndLine))){t[r].leftStartLine=Math.min(e.leftStartLine,t[r].leftStartLine),t[r].rightStartLine=Math.min(e.rightStartLine,t[r].rightStartLine),t[r].leftEndLine=Math.max(e.leftEndLine,t[r].leftEndLine),t[r].rightEndLine=Math.max(e.rightEndLine,t[r].rightEndLine),t[r].leftStartOffset=Math.min(e.leftStartOffset,t[r].leftStartOffset),t[r].leftEndOffset=Math.max(e.leftEndOffset,t[r].leftEndOffset),t[r].rightStartOffset=Math.min(e.rightStartOffset,t[r].rightStartOffset),t[r].rightEndOffset=Math.max(e.rightEndOffset,t[r].rightEndOffset),e.leftEndChar!==void 0&&t[r].leftChars.push({start:e.leftStartChar??0,end:e.leftEndChar,lineStart:e.leftStartLine,lineEnd:e.leftEndLine}),e.rightEndChar!==void 0&&t[r].rightChars.push({start:e.rightStartChar??0,end:e.rightEndChar,lineStart:e.rightStartLine,lineEnd:e.rightEndLine}),a=!0;break}a||t.push(r(e))}),t.filter(e=>!(e.leftStartLine===e.leftEndLine&&e.rightStartLine===e.rightEndLine))}decorate(){this.clearGutter(),this.clearArrows(),this.diffs.forEach((e,t)=>{this.options.showDiffs&&(this.showDiff(U,e.leftStartLine,e.leftEndLine,e.leftChars,this.options.classes.diff??``),this.showDiff(H,e.rightStartLine,e.rightEndLine,e.rightChars,this.options.classes.diff??``),this.options.showConnectors&&this.addConnector(e.leftStartLine,e.leftEndLine,e.rightStartLine,e.rightEndLine),this.addCopyArrows(e,t))})}};export{$ as AceDiff,$ as default}; //# sourceMappingURL=index.mjs.map