UNPKG

ll-package

Version:

3 lines (2 loc) 6.54 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const m=require("../../../_virtual/_commonjsHelpers.js"),x=require("../../fast-diff/diff.js"),g=require("../../lodash.clonedeep/index.js"),_=require("../../lodash.isequal/index.js");require("./AttributeMap.js");const E=require("./Op.js"),j=require("../../../_virtual/AttributeMap.js");var v=m.commonjsGlobal&&m.commonjsGlobal.__importDefault||function(n){return n&&n.__esModule?n:{default:n}},y=v(x.diff_1),M=v(g.lodash_clonedeepExports),b=v(_.lodash_isequalExports),d=v(j.__exports),l=v(E.__require()),T="\0",q=function(){function n(e){Array.isArray(e)?this.ops=e:e!=null&&Array.isArray(e.ops)?this.ops=e.ops:this.ops=[]}return n.prototype.insert=function(e,t){var r={};return typeof e=="string"&&e.length===0?this:(r.insert=e,t!=null&&typeof t=="object"&&Object.keys(t).length>0&&(r.attributes=t),this.push(r))},n.prototype.delete=function(e){return e<=0?this:this.push({delete:e})},n.prototype.retain=function(e,t){if(e<=0)return this;var r={retain:e};return t!=null&&typeof t=="object"&&Object.keys(t).length>0&&(r.attributes=t),this.push(r)},n.prototype.push=function(e){var t=this.ops.length,r=this.ops[t-1];if(e=M.default(e),typeof r=="object"){if(typeof e.delete=="number"&&typeof r.delete=="number")return this.ops[t-1]={delete:r.delete+e.delete},this;if(typeof r.delete=="number"&&e.insert!=null&&(t-=1,r=this.ops[t-1],typeof r!="object"))return this.ops.unshift(e),this;if(b.default(e.attributes,r.attributes)){if(typeof e.insert=="string"&&typeof r.insert=="string")return this.ops[t-1]={insert:r.insert+e.insert},typeof e.attributes=="object"&&(this.ops[t-1].attributes=e.attributes),this;if(typeof e.retain=="number"&&typeof r.retain=="number")return this.ops[t-1]={retain:r.retain+e.retain},typeof e.attributes=="object"&&(this.ops[t-1].attributes=e.attributes),this}}return t===this.ops.length?this.ops.push(e):this.ops.splice(t,0,e),this},n.prototype.chop=function(){var e=this.ops[this.ops.length-1];return e&&e.retain&&!e.attributes&&this.ops.pop(),this},n.prototype.filter=function(e){return this.ops.filter(e)},n.prototype.forEach=function(e){this.ops.forEach(e)},n.prototype.map=function(e){return this.ops.map(e)},n.prototype.partition=function(e){var t=[],r=[];return this.forEach(function(i){var s=e(i)?t:r;s.push(i)}),[t,r]},n.prototype.reduce=function(e,t){return this.ops.reduce(e,t)},n.prototype.changeLength=function(){return this.reduce(function(e,t){return t.insert?e+l.default.length(t):t.delete?e-t.delete:e},0)},n.prototype.length=function(){return this.reduce(function(e,t){return e+l.default.length(t)},0)},n.prototype.slice=function(e,t){e===void 0&&(e=0),t===void 0&&(t=1/0);for(var r=[],i=l.default.iterator(this.ops),s=0;s<t&&i.hasNext();){var a=void 0;s<e?a=i.next(e-s):(a=i.next(t-s),r.push(a)),s+=l.default.length(a)}return new n(r)},n.prototype.compose=function(e){var t=l.default.iterator(this.ops),r=l.default.iterator(e.ops),i=[],s=r.peek();if(s!=null&&typeof s.retain=="number"&&s.attributes==null){for(var a=s.retain;t.peekType()==="insert"&&t.peekLength()<=a;)a-=t.peekLength(),i.push(t.next());s.retain-a>0&&r.next(s.retain-a)}for(var u=new n(i);t.hasNext()||r.hasNext();)if(r.peekType()==="insert")u.push(r.next());else if(t.peekType()==="delete")u.push(t.next());else{var p=Math.min(t.peekLength(),r.peekLength()),o=t.next(p),f=r.next(p);if(typeof f.retain=="number"){var h={};typeof o.retain=="number"?h.retain=p:h.insert=o.insert;var c=d.default.compose(o.attributes,f.attributes,typeof o.retain=="number");if(c&&(h.attributes=c),u.push(h),!r.hasNext()&&b.default(u.ops[u.ops.length-1],h)){var k=new n(t.rest());return u.concat(k).chop()}}else typeof f.delete=="number"&&typeof o.retain=="number"&&u.push(f)}return u.chop()},n.prototype.concat=function(e){var t=new n(this.ops.slice());return e.ops.length>0&&(t.push(e.ops[0]),t.ops=t.ops.concat(e.ops.slice(1))),t},n.prototype.diff=function(e,t){if(this.ops===e.ops)return new n;var r=[this,e].map(function(p){return p.map(function(o){if(o.insert!=null)return typeof o.insert=="string"?o.insert:T;var f=p===e?"on":"with";throw new Error("diff() called "+f+" non-document")}).join("")}),i=new n,s=y.default(r[0],r[1],t),a=l.default.iterator(this.ops),u=l.default.iterator(e.ops);return s.forEach(function(p){for(var o=p[1].length;o>0;){var f=0;switch(p[0]){case y.default.INSERT:f=Math.min(u.peekLength(),o),i.push(u.next(f));break;case y.default.DELETE:f=Math.min(o,a.peekLength()),a.next(f),i.delete(f);break;case y.default.EQUAL:f=Math.min(a.peekLength(),u.peekLength(),o);var h=a.next(f),c=u.next(f);b.default(h.insert,c.insert)?i.retain(f,d.default.diff(h.attributes,c.attributes)):i.push(c).delete(f);break}o-=f}}),i.chop()},n.prototype.eachLine=function(e,t){t===void 0&&(t=` `);for(var r=l.default.iterator(this.ops),i=new n,s=0;r.hasNext();){if(r.peekType()!=="insert")return;var a=r.peek(),u=l.default.length(a)-r.peekLength(),p=typeof a.insert=="string"?a.insert.indexOf(t,u)-u:-1;if(p<0)i.push(r.next());else if(p>0)i.push(r.next(p));else{if(e(i,r.next(1).attributes||{},s)===!1)return;s+=1,i=new n}}i.length()>0&&e(i,{},s)},n.prototype.invert=function(e){var t=new n;return this.reduce(function(r,i){if(i.insert)t.delete(l.default.length(i));else{if(i.retain&&i.attributes==null)return t.retain(i.retain),r+i.retain;if(i.delete||i.retain&&i.attributes){var s=i.delete||i.retain,a=e.slice(r,r+s);return a.forEach(function(u){i.delete?t.push(u):i.retain&&i.attributes&&t.retain(l.default.length(u),d.default.invert(i.attributes,u.attributes))}),r+s}}return r},0),t.chop()},n.prototype.transform=function(e,t){if(t===void 0&&(t=!1),t=!!t,typeof e=="number")return this.transformPosition(e,t);for(var r=e,i=l.default.iterator(this.ops),s=l.default.iterator(r.ops),a=new n;i.hasNext()||s.hasNext();)if(i.peekType()==="insert"&&(t||s.peekType()!=="insert"))a.retain(l.default.length(i.next()));else if(s.peekType()==="insert")a.push(s.next());else{var u=Math.min(i.peekLength(),s.peekLength()),p=i.next(u),o=s.next(u);if(p.delete)continue;o.delete?a.push(o):a.retain(u,d.default.transform(p.attributes,o.attributes,t))}return a.chop()},n.prototype.transformPosition=function(e,t){t===void 0&&(t=!1),t=!!t;for(var r=l.default.iterator(this.ops),i=0;r.hasNext()&&i<=e;){var s=r.peekLength(),a=r.peekType();if(r.next(),a==="delete"){e-=Math.min(s,e-i);continue}else a==="insert"&&(i<e||!t)&&(e+=s);i+=s}return e},n.Op=l.default,n.AttributeMap=d.default,n}(),A=q;const N=m.getDefaultExportFromCjs(A);exports.default=N;