UNPKG

editor-document

Version:

applyDelta, Delta, Document, Position, Range

3 lines (2 loc) 13.7 kB
System.register([],(function(t){"use strict";return{execute:function(){function e(t,e){const n=e.start.row,r=e.start.column,i=t[n]||"";switch(e.action){case"insert":1===e.lines.length?t[n]=i.substring(0,r)+e.lines[0]+i.substring(r):(t.splice(n,1,...e.lines),t[n]=i.substring(0,r)+t[n],t[n+e.lines.length-1]+=i.substring(r));break;case"remove":{const o=e.end.column,s=e.end.row;n===s?t[n]=i.substring(0,r)+i.substring(o):t.splice(n,s-n+1,i.substring(0,r)+t[s].substring(o));break}}}t({applyDelta:e,comparePositions:function(t,e){return t.row>e.row?1:t.row<e.row?-1:t.column>e.column?1:t.column<e.column?-1:0},equalPositions:C,isEmptyRange:O,position:D,range:T});var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},n(t,e)};function r(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}function i(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function o(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,i,o=n.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(r=o.next()).done;)s.push(r.value)}catch(t){i={error:t}}finally{try{r&&!r.done&&(n=o.return)&&n.call(o)}finally{if(i)throw i.error}}return s}function s(t,e,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}function c(t){return"function"==typeof t}"function"==typeof SuppressedError&&SuppressedError;var u,a=((u=function(t){return function(e){t(this),this.message=e?e.length+" errors occurred during unsubscription:\n"+e.map((function(t,e){return e+1+") "+t.toString()})).join("\n "):"",this.name="UnsubscriptionError",this.errors=e}}((function(t){Error.call(t),t.stack=(new Error).stack}))).prototype=Object.create(Error.prototype),u.prototype.constructor=u,u);function l(t,e){if(t){var n=t.indexOf(e);0<=n&&t.splice(n,1)}}var h=function(){function t(t){this.initialTeardown=t,this.closed=!1,this._parentage=null,this._finalizers=null}var e;return t.prototype.unsubscribe=function(){var t,e,n,r,u;if(!this.closed){this.closed=!0;var l=this._parentage;if(l)if(this._parentage=null,Array.isArray(l))try{for(var h=i(l),p=h.next();!p.done;p=h.next()){p.value.remove(this)}}catch(e){t={error:e}}finally{try{p&&!p.done&&(e=h.return)&&e.call(h)}finally{if(t)throw t.error}}else l.remove(this);var d=this.initialTeardown;if(c(d))try{d()}catch(t){u=t instanceof a?t.errors:[t]}var g=this._finalizers;if(g){this._finalizers=null;try{for(var m=i(g),y=m.next();!y.done;y=m.next()){var b=y.value;try{f(b)}catch(t){u=null!=u?u:[],t instanceof a?u=s(s([],o(u)),o(t.errors)):u.push(t)}}}catch(t){n={error:t}}finally{try{y&&!y.done&&(r=m.return)&&r.call(m)}finally{if(n)throw n.error}}}if(u)throw new a(u)}},t.prototype.add=function(e){var n;if(e&&e!==this)if(this.closed)f(e);else{if(e instanceof t){if(e.closed||e._hasParent(this))return;e._addParent(this)}(this._finalizers=null!==(n=this._finalizers)&&void 0!==n?n:[]).push(e)}},t.prototype._hasParent=function(t){var e=this._parentage;return e===t||Array.isArray(e)&&e.includes(t)},t.prototype._addParent=function(t){var e=this._parentage;this._parentage=Array.isArray(e)?(e.push(t),e):e?[e,t]:t},t.prototype._removeParent=function(t){var e=this._parentage;e===t?this._parentage=null:Array.isArray(e)&&l(e,t)},t.prototype.remove=function(e){var n=this._finalizers;n&&l(n,e),e instanceof t&&e._removeParent(this)},t.EMPTY=((e=new t).closed=!0,e),t}();function p(t){return t instanceof h||t&&"closed"in t&&c(t.remove)&&c(t.add)&&c(t.unsubscribe)}function f(t){c(t)?t():t.unsubscribe()}h.EMPTY;var d={onUnhandledError:null,onStoppedNotification:null,Promise:void 0,useDeprecatedSynchronousErrorHandling:!1,useDeprecatedNextContext:!1},g={setTimeout:function(t,e){for(var n=[],r=2;r<arguments.length;r++)n[r-2]=arguments[r];return setTimeout.apply(void 0,s([t,e],o(n)))},clearTimeout:function(t){var e=g.delegate;return((null==e?void 0:e.clearTimeout)||clearTimeout)(t)},delegate:void 0};function m(){}var y=function(t){function e(e){var n=t.call(this)||this;return n.isStopped=!1,e?(n.destination=e,p(e)&&e.add(n)):n.destination=x,n}return r(e,t),e.create=function(t,e,n){return new L(t,e,n)},e.prototype.next=function(t){this.isStopped||this._next(t)},e.prototype.error=function(t){this.isStopped||(this.isStopped=!0,this._error(t))},e.prototype.complete=function(){this.isStopped||(this.isStopped=!0,this._complete())},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=!0,t.prototype.unsubscribe.call(this),this.destination=null)},e.prototype._next=function(t){this.destination.next(t)},e.prototype._error=function(t){try{this.destination.error(t)}finally{this.unsubscribe()}},e.prototype._complete=function(){try{this.destination.complete()}finally{this.unsubscribe()}},e}(h),b=Function.prototype.bind;function v(t,e){return b.call(t,e)}var w=function(){function t(t){this.partialObserver=t}return t.prototype.next=function(t){var e=this.partialObserver;if(e.next)try{e.next(t)}catch(t){_(t)}},t.prototype.error=function(t){var e=this.partialObserver;if(e.error)try{e.error(t)}catch(t){_(t)}else _(t)},t.prototype.complete=function(){var t=this.partialObserver;if(t.complete)try{t.complete()}catch(t){_(t)}},t}(),L=function(t){function e(e,n,r){var i,o,s=t.call(this)||this;c(e)||!e?i={next:null!=e?e:void 0,error:null!=n?n:void 0,complete:null!=r?r:void 0}:s&&d.useDeprecatedNextContext?((o=Object.create(e)).unsubscribe=function(){return s.unsubscribe()},i={next:e.next&&v(e.next,o),error:e.error&&v(e.error,o),complete:e.complete&&v(e.complete,o)}):i=e;return s.destination=new w(i),s}return r(e,t),e}(y);function _(t){var e;e=t,g.setTimeout((function(){throw e}))}var x={closed:!0,next:m,error:function(t){throw t},complete:m},M="function"==typeof Symbol&&Symbol.observable||"@@observable";function P(t){return t}var S=function(){function t(t){t&&(this._subscribe=t)}return t.prototype.lift=function(e){var n=new t;return n.source=this,n.operator=e,n},t.prototype.subscribe=function(t,e,n){var r,i=this,o=(r=t)&&r instanceof y||function(t){return t&&c(t.next)&&c(t.error)&&c(t.complete)}(r)&&p(r)?t:new L(t,e,n);return function(){var t=i,e=t.operator,n=t.source;o.add(e?e.call(o,n):n?i._subscribe(o):i._trySubscribe(o))}(),o},t.prototype._trySubscribe=function(t){try{return this._subscribe(t)}catch(e){t.error(e)}},t.prototype.forEach=function(t,e){var n=this;return new(e=E(e))((function(e,r){var i=new L({next:function(e){try{t(e)}catch(t){r(t),i.unsubscribe()}},error:r,complete:e});n.subscribe(i)}))},t.prototype._subscribe=function(t){var e;return null===(e=this.source)||void 0===e?void 0:e.subscribe(t)},t.prototype[M]=function(){return this},t.prototype.pipe=function(){for(var t,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return(0===(t=e).length?P:1===t.length?t[0]:function(e){return t.reduce((function(t,e){return e(t)}),e)})(this)},t.prototype.toPromise=function(t){var e=this;return new(t=E(t))((function(t,n){var r;e.subscribe((function(t){return r=t}),(function(t){return n(t)}),(function(){return t(r)}))}))},t.create=function(e){return new t(e)},t}();function E(t){var e;return null!==(e=null!=t?t:d.Promise)&&void 0!==e?e:Promise}class N{#t=`${Math.random()}`;#e;#n;#r;constructor(t,e,n=e){this.#e=t,this.#n=e,n&&!e&&console.warn(`${t} has elected to bypass monitoring. It may be leaking subscriptions?`)}subscriberCountByEvent(t){return this.#r?this.#r[t].length:0}send(t,e){return this.dispatch(t,e)}post(t,e,n=0){return new Promise(((r,i)=>{window.setTimeout((()=>{try{const n=this.dispatch(t,e);try{r(n)}catch(t){}}catch(t){i(t)}}),n)}))}events(t){return new S((e=>this.addEventListener(t,(function(t){e.next(t)}))))}on(t,e){return this.addEventListener(t,e)}off(t,e){return this.removeEventListener(t,e)}addEventListener(t,e){this.#n&&(this.#t,this.#e),this.#r=this.#r||{};let n=this.#r[t];return n||(n=this.#r[t]=[]),-1===n.indexOf(e)&&n.push(e),()=>{this.removeEventListener(t,e)}}removeEventListener(t,e){this.#n&&(this.#t,this.#e),this.#r=this.#r||{};const n=this.#r[t];if(!n)return;const r=n.indexOf(e);-1!==r&&n.splice(r,1)}dispatch(t,e){let n=(this.#r||{})[t];if(!n)return 0;n=n.slice();let r=0;for(const t of n)t(e),r+=1;return r}}function D(t,e){return{row:t,column:e}}function C(t,e){return t.row===e.row&&t.column===e.column}function T(t,e){return{start:t,end:e}}function O(t){return C(t.start,t.end)}function A(t){return{row:t.row,column:t.column}}function k(t,e){return{row:t,column:e}}const j=0==="aaa".split(/a/).length?function(t){return t.replace(/\r\n|\r/g,"\n").split("\n")}:function(t){return t.split(/\r\n|\r|\n/)},R="change",F="changeNewLineMode";t("Document",class{#i=[];#o="";#s="auto";#c=new N("",!0);change$=this.#c.events(R);#u=new N("",!0);changeNewLineMode$=this.#u.events(F);#a=1;constructor(t){this.#i=[""],0===t.length?this.#i=[""]:Array.isArray(t)?this.insertMergedLines({row:0,column:0},t):this.insert({row:0,column:0},t)}#l(){this.#i.length=0}addRef(){return this.#a++,this.#a}release(){if(this.#a--,0===this.#a)this.#l();else if(this.#a<0)throw new Error("Document refCount is negative.");return this.#a}setValue(t){const e=this.getLength()-1,n=D(0,0),r=D(e,this.getLine(e).length);this.remove(T(n,r)),this.insert({row:0,column:0},t)}getValue(){return this.#i.join(this.getNewLineCharacter())}#h(t){const e=t.match(/^.*?(\r\n|\r|\n)/m);this.#o=e?e[1]:"\n",this.#u.send(F,this.#o)}getNewLineCharacter(){switch(this.#s){case"windows":return"\r\n";case"unix":return"\n";default:return this.#o||"\n"}}setNewLineMode(t){this.#s!==t&&(this.#s=t,this.#u.send(F,this.#s))}getNewLineMode(){return this.#s}isNewLine(t){return"\r\n"===t||"\r"===t||"\n"===t}getLine(t){return this.#i[t]||""}getLines(t,e){const n=e+1;return this.#i.slice(t,n)}getAllLines(){return this.#i.slice(0,this.#i.length)}getLength(){return this.#i.length}getTextRange(t){return this.getLinesForRange(t).join(this.getNewLineCharacter())}getLinesForRange(t){let e;if(t.start.row===t.end.row)e=[this.getLine(t.start.row).substring(t.start.column,t.end.column)];else{e=this.getLines(t.start.row,t.end.row),e[0]=(e[0]||"").substring(t.start.column);const n=e.length-1;t.end.row-t.start.row===n&&(e[n]=e[n].substring(0,t.end.column))}return e}insert(t,e){return this.getLength()<=1&&this.#h(e),this.insertMergedLines(t,j(e))}insertInLine(t,e){const n=this.clippedPos(t.row,t.column),r=k(t.row,t.column+e.length);return this.applyDelta({start:n,end:r,action:"insert",lines:[e]}),A(r)}clippedPos(t,e){const n=this.getLength();let r=!1;void 0===t?t=n:t<0?t=0:t>=n&&(t=n-1,r=!0);const i=this.getLine(t);return r&&(e=i.length),{row:t,column:e=Math.min(Math.max(e,0),i.length)}}insertFullLines(t,e){let n=0;return(t=Math.min(Math.max(t,0),this.getLength()))<this.getLength()?(e=e.concat([""]),n=0):(e=[""].concat(e),t--,n=this.#i[t].length),this.insertMergedLines({row:t,column:n},e)}insertMergedLines(t,e){const n=this.clippedPos(t.row,t.column),r={row:n.row+e.length-1,column:(1===e.length?n.column:0)+e[e.length-1].length};return this.applyDelta({start:n,end:r,action:"insert",lines:e}),A(r)}remove(t){const e=this.clippedPos(t.start.row,t.start.column),n=this.clippedPos(t.end.row,t.end.column);return this.applyDelta({start:e,end:n,action:"remove",lines:this.getLinesForRange({start:e,end:n})}),A(e)}removeInLine(t,e,n){const r=this.clippedPos(t,e),i=this.clippedPos(t,n);return this.applyDelta({start:r,end:i,action:"remove",lines:this.getLinesForRange({start:r,end:i})}),A(r)}removeFullLines(t,e){t=Math.min(Math.max(0,t),this.getLength()-1);const n=(e=Math.min(Math.max(0,e),this.getLength()-1))===this.getLength()-1&&t>0,r=e<this.getLength()-1,i=n?t-1:t,o=n?this.getLine(i).length:0,s=r?e+1:e,c=r?0:this.getLine(s).length,u=D(i,o),a=D(s,c),l=this.getLines(t,e);return this.applyDelta({start:u,end:a,action:"remove",lines:this.getLinesForRange(T(u,a))}),l}removeNewLine(t){t<this.getLength()-1&&t>=0&&this.applyDelta({start:k(t,this.getLine(t).length),end:k(t+1,0),action:"remove",lines:["",""]})}replace(t,e){if(0===e.length&&O(t))return t.end;return e===this.getTextRange(t)?t.end:(this.remove(t),this.insert(t.start,e))}applyDeltas(t){for(let e=0;e<t.length;e++)this.applyDelta(t[e])}revertDeltas(t){for(let e=t.length-1;e>=0;e--)this.revertDelta(t[e])}applyDelta(t){const n="insert"===t.action;(n?t.lines.length<=1&&!t.lines[0]:C(t.start,t.end))||(n&&t.lines.length>2e4&&this.#p(t,2e4),e(this.#i,t),this.#c.send(R,t))}#p(t,e){const n=t.lines,r=n.length,i=t.start.row;let o=t.start.column,s=0,c=0,u=!0;do{s=c,c+=e-1;const a=n.slice(s,c);if(c>r){t.lines=a,t.start.row=i+s,t.start.column=o,u=!1;break}u&&(a.push(""),this.applyDelta({start:k(i+s,o),end:k(i+c,o=0),action:t.action,lines:a}))}while(u)}revertDelta(t){this.applyDelta({start:A(t.start),end:A(t.end),action:"insert"===t.action?"remove":"insert",lines:t.lines.slice()})}indexToPosition(t,e=0){const n=this.#i,r=this.getNewLineCharacter().length,i=n.length;for(let o=e||0;o<i;o++)if((t-=n[o].length+r)<0)return{row:o,column:t+n[o].length+r};return{row:i-1,column:n[i-1].length}}positionToIndex(t,e=0){const n=this.#i,r=this.getNewLineCharacter().length;let i=0;const o=Math.min(t.row,n.length);for(let t=e||0;t<o;++t)i+=n[t].length+r;return i+t.column}})}}})); //# sourceMappingURL=index.min.js.map