UNPKG

@wangeditor-next/yjs

Version:
3 lines (2 loc) 23.8 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("yjs"),require("slate")):"function"==typeof define&&define.amd?define(["exports","yjs","slate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).WangEditorYjsModule={},t.Y,t.slate)}(this,function(t,e,n){"use strict";function r(t){var e=Object.create(null);return t&&Object.keys(t).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}}),e.default=t,Object.freeze(e)}var o=r(e),i=function(){return i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},i.apply(this,arguments)};function a(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function s(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 c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)a.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return a}function u(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}function l(t){return"[object Object]"===Object.prototype.toString.call(t)}function f(t){if(!l(t))return!1;var e=t.constructor;if(void 0===e)return!0;var n=e.prototype;return!1!==l(n)&&!Object.prototype.hasOwnProperty.call(n,"isPrototypeOfOf")}function d(t,e){for(var n in t){var r=t[n],o=e[n];if(f(r)&&f(o)){if(!d(r,o))return!1}else if(Array.isArray(r)&&Array.isArray(o)){if(r.length!==o.length)return!1;for(var i=0;i<r.length;i+=1)if(r[i]!==o[i])return!1}else if(r!==o)return!1}for(var n in e)if(void 0===t[n]&&void 0!==e[n])return!1;return!0}function h(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return Object.fromEntries(Object.entries(t).filter(function(t){var n=c(t,1)[0];return e.includes(n)}))}function g(t){return Object.fromEntries(Object.entries(t).filter(function(t){return null!==c(t,2)[1]}))}function p(t){return function(t){var e,n,r,o,i=[];try{for(var a=s(t),c=a.next();!c.done;c=a.next()){var u=c.value;if("string"!=typeof u.insert||0!==u.insert.length){var l=i[i.length-1];l&&"string"==typeof l.insert&&"string"==typeof u.insert&&(l.attributes===u.attributes||!l.attributes==!u.attributes&&d(null!==(r=l.attributes)&&void 0!==r?r:{},null!==(o=u.attributes)&&void 0!==o?o:{}))?l.insert+=u.insert:i.push(u)}}}catch(t){e={error:t}}finally{try{c&&!c.done&&(n=a.return)&&n.call(a)}finally{if(e)throw e.error}}return i}(t.toDelta())}function v(t){var e=t.insert;return"string"==typeof e?e.length:1}function y(t){return t.reduce(function(t,e){return t+v(e)},0)}function x(t,e,n){if(n<1)return[];for(var r=0,o=[],a=e+n,s=0;s<t.length&&!(r>=a);s+=1){var c=t[s],u=v(c);if(r+u<=e)r+=u;else if("string"==typeof c.insert){var l=Math.max(0,e-r),f=Math.min(u,u-(r+u-a));o.push(i(i({},c),{insert:c.insert.slice(l,f)})),r+=u}else r+=u,o.push(c)}return o}function m(t){return t?n.Text.isText(t)?t.text.length:1:0}function w(t,e,r){var i;if(0===r.length)throw new Error("Path has to a have a length >= 1");if(n.Text.isText(e))throw new Error("Cannot descent into slate text");var a=c(r),s=a[0],u=a.slice(1),l=function(t,e){return t.children.slice(0,e).reduce(function(t,e){return t+m(e)},0)}(e,s),f=e.children[s],d=p(t),h=m(f),g=x(d,l,h);if(g.length>1)throw new Error("Path doesn't match yText, yTarget spans multiple nodes");var v=null===(i=g[0])||void 0===i?void 0:i.insert;if(u.length>0){if(!(v instanceof o.XmlText))throw new Error("Path doesn't match yText, cannot descent into non-yText");return w(v,f,u)}return{yParent:t,textRange:{start:l,end:l+h},yTarget:v instanceof o.XmlText?v:void 0,slateParent:e,slateTarget:f,targetDelta:g}}function b(t,e,r){void 0===r&&(r={});for(var o=r.assoc,i=void 0===o?0:o,a=r.insert,s=void 0!==a&&a,c=0,u=0,l=0;l<t.children.length;l+=1){var f=t.children[l],d=n.Text.isText(f)?f.text.length:1;d>0&&(u=l);var h=c+d;if(d>0&&(i>=0?h>e:h>=e))return[l,e-c];c+=d}if(e>c+(s?1:0))throw new Error("yOffset out of bounds");if(s)return[t.children.length,0];var g=t.children[u];return[u,n.Text.isText(g)?g.text.length:1]}function T(t,e,r){for(var i=[r];i[0]!==t;){var a=i[0].parent;if(!a)throw new Error("yText isn't a descendant of root element");if(!(a instanceof o.XmlText))throw new Error("Unexpected y parent type");i.unshift(a)}if(i.length<2)return[];var u=e;return i.reduce(function(t,e,r){var o,a,l=i[r+1];if(!l)return t;var f=0,d=p(e);try{for(var h=s(d),g=h.next();!g.done;g=h.next()){var v=g.value;if(v.insert===l)break;f+="string"==typeof v.insert?v.insert.length:1}}catch(t){o={error:t}}finally{try{g&&!g.done&&(a=h.return)&&a.call(h)}finally{if(o)throw o.error}}if(n.Text.isText(u))throw new Error("Cannot descent into slate text");var y=c(b(u,f),1)[0];return u=u.children[y],t.concat(y)},[])}function E(t){if(!t.doc)throw new Error("shared type isn't attached to a document")}"function"==typeof SuppressedError&&SuppressedError;var O="__slateYjsStoredPosition_";function R(t,e,n){var r=w(t,e,n.path),i=r.yTarget,a=r.yParent,s=r.textRange;if(i)throw new Error("Slate point points to a non-text element inside sharedRoot");var c=s.start+n.offset;return o.createRelativePositionFromTypeIndex(a,c,c===s.end?-1:0)}function P(t,e,r){if(!t.doc)throw new Error("sharedRoot isn't attach to a yDoc");var i=o.createAbsolutePositionFromRelativePosition(r,t.doc);return i&&function(t,e,r){var i=r.type,a=r.index,s=r.assoc;if(!(i instanceof o.XmlText))throw new Error("Absolute position points to a non-XMLText");var l=T(t,e,i),f=n.Node.get(e,l);if(n.Text.isText(f)||!n.Element.isElement(f))throw new Error("Absolute position doesn't match slateRoot, cannot descent into text and EditorRoot");var d=c(b(f,a,{assoc:s}),2),h=d[0],g=d[1],p=f.children[h];return n.Text.isText(p)?{path:u(u([],c(l),!1),[h],!1),offset:g}:null}(t,e,i)}function S(t,e,n){t.setAttribute(O+e,o.encodeRelativePosition(n))}function j(t,e,n){return Object.fromEntries(Object.entries(t).filter(function(t){var r=c(t,2)[1];return r.type===e&&(!n||(r.assoc>=0?r.index>=n.start&&r.index<n.end:r.index>n.start&&r.index>=n.end))}))}function C(t,e,n){var r;void 0===n&&(n="");var i=((r={})[n]=j(t,e),r);return p(e).forEach(function(e,r){var a=e.insert;a instanceof o.XmlText&&Object.assign(i,C(t,a,n?"".concat(n,".").concat(r):r.toString()))}),i}function k(t,e,n,r){void 0===r&&(r=0);var i=function(t){return E(t),Object.fromEntries(Object.entries(t.getAttributes()).filter(function(t){return c(t,1)[0].startsWith(O)}).map(function(e){var n=c(e,2),r=n[0],i=n[1];return[r.slice(25),o.createAbsolutePositionFromRelativePosition(o.decodeRelativePosition(i),t.doc)]}).filter(function(t){return c(t,2)[1]}))}(t),a={"":j(i,e,{start:r,end:r+y(n)})};return n.forEach(function(t,e){var n=t.insert;n instanceof o.XmlText&&Object.assign(a,C(i,n,e.toString()))}),a}function A(t,e,n,r,i,a,s){void 0===i&&(i=0),void 0===a&&(a=0),void 0===s&&(s="");var u=n[s];u&&Object.entries(u).forEach(function(n){var r=c(n,2),s=r[0],u=r[1];S(t,s,o.createRelativePositionFromTypeIndex(e,u.index-a+i,u.assoc))}),r.forEach(function(e,r){var i=e.insert;i instanceof o.XmlText&&A(t,i,n,p(i),0,0,s?"".concat(s,".").concat(r):r.toString())})}function D(t,e,n){return{anchor:R(t,e,n.anchor),focus:R(t,e,n.focus)}}function M(t,e,n){var r=P(t,e,n.anchor);if(!r)return null;var o=P(t,e,n.focus);return o?{anchor:r,focus:o}:null}function _(t){return function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return Object.fromEntries(Object.entries(t).filter(function(t){var n=c(t,1)[0];return!e.includes(n)}))}(t,n.Text.isText(t)?"text":"children")}function L(t){var e=p(t),n=e.length>0?e.map(F):[{text:""}];return i(i({},t.getAttributes()),{children:n})}function F(t){return"string"==typeof t.insert?i(i({},t.attributes),{text:t.insert}):L(t.insert)}function Y(t){return t.map(function(t){return n.Text.isText(t)?{insert:t.text,attributes:_(t)}:{insert:X(t)}})}function X(t){var e=t.children,n=a(t,["children"]),r=new o.XmlText;return Object.entries(n).forEach(function(t){var e=c(t,2),n=e[0],o=e[1];r.setAttribute(n,o)}),r.applyDelta(Y(e),{sanitize:!1}),r}function I(t,e,r){var a=r.target,s=r.changes,l=r.delta;if(!(a instanceof o.XmlText))throw new Error("Unexpected target node type");var f=[],p=T(t,e,a),v=n.Node.get(e,p);if(n.Text.isText(v))throw new Error("Cannot apply yTextEvent to text node");var y=Array.from(s.keys.entries());if(p.length>0&&y.length>0){var x=Object.fromEntries(y.map(function(t){var e=c(t,2),n=e[0];return[n,"delete"===e[1].action?null:a.getAttribute(n)]})),w=Object.fromEntries(y.map(function(t){var e=c(t,1)[0];return[e,v[e]]}));f.push({type:"set_node",newProperties:x,properties:w,path:p})}return l.length>0&&f.push.apply(f,u([],c(function(t,e,r){var o=[],a=r.reduce(function(t,e){return"retain"in e?t+e.retain:"delete"in e?t+e.delete:t},0);return r.reverse().forEach(function(r){var s;if("attributes"in r&&"retain"in r)for(var l=c(b(t,a-r.retain),2),f=l[0],p=l[1],v=c(b(t,a,{assoc:-1}),2),y=v[0],x=v[1],w=y;w>=f;w-=1){var T=t.children[w],E=u(u([],c(e),!1),[w],!1);if(n.Text.isText(T)){var O=r.attributes,R=h.apply(void 0,u([t],c(Object.keys(r.attributes)),!1));if(w===f||w===y){var P=w===f?p:0;if((C=w===y?x:T.text.length)!==T.text.length&&o.push({type:"split_node",path:E,position:C,properties:_(T)}),0!==P){o.push({type:"split_node",path:E,position:P,properties:g(i(i({},_(T)),O))});continue}}o.push({type:"set_node",newProperties:O,path:E,properties:R})}}if("retain"in r&&(a-=r.retain),"delete"in r){var S=c(b(t,a-r.delete),2),j=(f=S[0],p=S[1],c(b(t,a,{assoc:-1}),2));for(y=j[0],w=0===(x=j[1])?y-1:y;w>=f;w-=1)if(T=t.children[w],E=u(u([],c(e),!1),[w],!1),!n.Text.isText(T)||w!==f&&w!==y)o.push({type:"remove_node",node:T,path:E}),a-=m(T);else{P=w===f?p:0;var C=w===y?x:T.text.length;o.push({type:"remove_text",offset:P,text:T.text.slice(P,C),path:E}),a-=C-P}}else if("insert"in r){var k=c(b(t,a,{insert:!0}),2),A=(w=k[0],k[1]);if(T=t.children[w],E=u(u([],c(e),!1),[w],!1),n.Text.isText(T)){var D=o[o.length-1],M=null!=D&&"insert_node"===D.type?D.node:_(T),L=[];if(null==D||"insert_node"!==D.type&&"insert_text"!==D.type&&"split_node"!==D.type&&"set_node"!==D.type||(L=D.path),"string"==typeof r.insert&&d(null!==(s=r.attributes)&&void 0!==s?s:{},M)&&n.Path.equals(E,L))return o.push({type:"insert_text",offset:A,text:r.insert,path:E});var Y=F(r);return 0===A?o.push({type:"insert_node",path:E,node:Y}):(A<T.text.length&&o.push({type:"split_node",path:E,position:A,properties:_(T)}),o.push({type:"insert_node",path:n.Path.next(E),node:Y}))}return o.push({type:"insert_node",path:E,node:F(r)})}}),o}(v,p,l)),!1)),f}function z(t,e,r){n.Editor.withoutNormalizing(e,function(){r.forEach(function(n){(function(t,e,n){if(n instanceof o.YTextEvent)return I(t,e,n);throw new Error("Unexpected Y event type")})(t,e,n).forEach(function(t){e.apply(t)})})})}function N(t){return t.map(function(t){return"string"==typeof t.insert?t:i(i({},t),{insert:W(t.insert)})})}function W(t){var e=new o.XmlText,n=t.getAttributes();return Object.entries(n).forEach(function(t){var n=c(t,2),r=n[0],o=n[1];e.setAttribute(r,o)}),e.applyDelta(N(p(t)),{sanitize:!1}),e}var U={insert_node:function(t,e,r){var o=w(t,e,r.path),i=o.yParent,a=o.textRange;if(n.Text.isText(r.node))return i.insert(a.start,r.node.text,_(r.node));i.insertEmbed(a.start,X(r.node))},remove_node:function(t,e,n){var r=w(t,e,n.path),o=r.yParent,i=r.textRange;o.delete(i.start,i.end-i.start)},set_node:function(t,e,n){var r=w(t,e,n.path),o=r.yTarget,a=r.textRange,s=r.yParent;if(o)return Object.entries(n.newProperties).forEach(function(t){var e=c(t,2),n=e[0],r=e[1];if(null===r)return o.removeAttribute(n);o.setAttribute(n,r)}),Object.entries(n.properties).forEach(function(t){var e=c(t,1)[0];Object.prototype.hasOwnProperty.call(n.newProperties,e)||o.removeAttribute(e)});var u=Object.fromEntries(Object.keys(n.properties).map(function(t){return[t,null]})),l=i(i({},u),n.newProperties);s.format(a.start,a.end-a.start,l)},merge_node:function(t,e,r){var o=w(t,e,r.path),a=w(o.yParent,o.slateParent,n.Path.previous(r.path.slice(-1)));if(!o.yTarget!=!a.yTarget)throw new Error("Cannot merge y text with y element");if(!a.yTarget||!o.yTarget){var s=o.yParent,l=o.textRange,f=o.slateTarget;if(!f)throw new Error("Expected Slate target node for merge op.");var d=n.Node.get(e,n.Path.previous(r.path));if(!n.Text.isText(d))throw new Error("Path points to Y.Text but not a Slate text node.");var h=_(f),g=_(d),v=Object.keys(h).reduce(function(t,e){var n;return e in g?t:i(i({},t),((n={})[e]=null,n))},{});return s.format(l.start,l.end-l.start,i(i({},v),g))}var y=a.yTarget.length,x=p(o.yTarget),m=N(x),b=k(t,o.yTarget,x,y),T=u([{retain:y}],c(m),!1);a.yTarget.applyDelta(T,{sanitize:!1}),o.yParent.delete(o.textRange.start,o.textRange.end-o.textRange.start),A(t,a.yTarget,b,m,y)},move_node:function(t,e,r){var o=n.Path.parent(r.newPath),i=r.newPath[r.newPath.length-1],a=n.Node.get(e,o);if(n.Text.isText(a))throw new Error("Cannot move slate node into text element");var s=u(u([],c(o),!1),[Math.min(i,a.children.length)],!1),l=w(t,e,r.path),f=w(t,e,s),d=N(l.targetDelta),h=k(t,l.yParent,l.targetDelta);l.yParent.delete(l.textRange.start,l.textRange.end-l.textRange.start);var g=y(p(f.yParent)),v=Math.min(f.textRange.start,g),x=u([{retain:v}],c(d),!1);f.yParent.applyDelta(x,{sanitize:!1}),A(t,f.yParent,h,d,v,l.textRange.start)},split_node:function(t,e,r){var a=w(t,e,r.path);if(!a.slateTarget)throw new Error("Y target without corresponding slate node");if(!a.yTarget){if(!n.Text.isText(a.slateTarget))throw new Error("Mismatch node type between y target and slate node");var s={};return a.targetDelta.forEach(function(t){t.attributes&&Object.keys(t.attributes).forEach(function(t){s[t]=null})}),a.yParent.format(a.textRange.start,a.textRange.end-a.textRange.start,i(i({},s),r.properties))}if(n.Text.isText(a.slateTarget))throw new Error("Mismatch node type between y target and slate node");var u=w(a.yTarget,a.slateTarget,[r.position]),l=a.slateTarget.children.slice(0,r.position).reduce(function(t,e){return t+m(e)},0),f=a.slateTarget.children.reduce(function(t,e){return t+m(e)},0),d=x(p(a.yTarget),l,f-l),h=N(d),g=k(t,a.yTarget,d,l),v=new o.XmlText;v.applyDelta(h,{sanitize:!1}),Object.entries(r.properties).forEach(function(t){var e=c(t,2),n=e[0],r=e[1];v.setAttribute(n,r)}),a.yTarget.delete(u.textRange.start,a.yTarget.length-u.textRange.start),a.yParent.insertEmbed(a.textRange.end,v),A(t,v,g,h,0,l)}};var B=i(i(i({},{insert_text:function(t,e,r){var o=w(t,e,r.path),i=o.yParent,a=o.textRange,s=n.Node.get(e,r.path);if(!n.Text.isText(s))throw new Error("Cannot insert text into non-text node");i.insert(a.start+r.offset,r.text,_(s))},remove_text:function(t,e,n){var r=w(t,e,n.path),o=r.yParent,i=r.textRange;o.delete(i.start+n.offset,n.text.length)}}),U),{set_selection:function(){}});var q=Symbol("slate-yjs-operation"),H=Symbol("slate-yjs-position-storage"),J=new WeakMap,G=new WeakMap,K=new WeakSet,Q={isYjsEditor:function(t){return n.Editor.isEditor(t)&&t.sharedRoot instanceof o.XmlText&&"localOrigin"in t&&"positionStorageOrigin"in t&&"function"==typeof t.applyRemoteEvents&&"function"==typeof t.storeLocalChange&&"function"==typeof t.flushLocalChanges&&"function"==typeof t.isLocalOrigin&&"function"==typeof t.connect&&"function"==typeof t.disconnect},localChanges:function(t){var e;return null!==(e=G.get(t))&&void 0!==e?e:[]},applyRemoteEvents:function(t,e,n){t.applyRemoteEvents(e,n)},storeLocalChange:function(t,e){t.storeLocalChange(e)},flushLocalChanges:function(t){t.flushLocalChanges()},connected:function(t){return K.has(t)},connect:function(t){t.connect()},disconnect:function(t){t.disconnect()},isLocal:function(t){return t.isLocalOrigin(Q.origin(t))},origin:function(t){var e=J.get(t);return void 0!==e?e:t.localOrigin},withOrigin:function(t,e,n){var r=Q.origin(t);J.set(t,e),n(),J.set(t,r)},storePosition:function(t,e,n){var r=t.sharedRoot,o=t.positionStorageOrigin;E(r);var i=R(r,t,n);r.doc.transact(function(){S(r,e,i)},o)},removeStoredPosition:function(t,e){var n=t.sharedRoot,r=t.positionStorageOrigin;E(n),n.doc.transact(function(){!function(t,e){t.removeAttribute(O+e)}(n,e)},r)},position:function(t,e){var n=function(t,e){var n=t.getAttribute(O+e);return n?o.decodeRelativePosition(n):null}(t.sharedRoot,e);if(n)return P(t.sharedRoot,t,n)},storedPositionsRelative:function(t){return e=t.sharedRoot,Object.fromEntries(Object.entries(e.getAttributes()).filter(function(t){return c(t,1)[0].startsWith(O)}).map(function(t){var e=c(t,2),n=e[0],r=e[1];return[n.slice(25),o.createRelativePositionFromJSON(r)]}));var e}};var V=new WeakMap,Z={isCursorEditor:function(t){return Q.isYjsEditor(t)&&t.awareness&&"string"==typeof t.cursorDataField&&"string"==typeof t.selectionStateField&&"function"==typeof t.sendCursorPosition&&"function"==typeof t.sendCursorData},sendCursorPosition:function(t,e){void 0===e&&(e=t.selection),t.sendCursorPosition(e)},sendCursorData:function(t,e){t.sendCursorData(e)},on:function(t,e,n){var r;if("change"===e){var o=null!==(r=V.get(t))&&void 0!==r?r:new Set;o.add(n),t&&V.set(t,o)}},off:function(t,e,n){if("change"===e){var r=V.get(t);r&&r.delete(n)}},cursorState:function(t,e){var n;if(e===t.awareness.clientID||!Q.connected(t))return null;var r=t.awareness.getStates().get(e);return r?{relativeSelection:null!==(n=r[t.selectionStateField])&&void 0!==n?n:null,data:r[t.cursorDataField],clientId:e}:null},cursorStates:function(t){return Q.connected(t)?Object.fromEntries(Array.from(t.awareness.getStates().entries(),function(e){var n=c(e,2),r=n[0],o=n[1];return r!==t.awareness.clientID&&o?[r,{relativeSelection:o[t.selectionStateField],data:o[t.cursorDataField]}]:null}).filter(Array.isArray)):{}}};var $=new WeakMap,tt=Symbol("slate-yjs-history-without-saving"),et={isYHistoryEditor:function(t){return Q.isYjsEditor(t)&&t.undoManager instanceof o.UndoManager&&"function"==typeof t.undo&&"function"==typeof t.redo&&"withoutSavingOrigin"in t},canUndo:function(t){return t.undoManager.undoStack.length>0},canRedo:function(t){return t.undoManager.redoStack.length>0},isSaving:function(t){return t.undoManager.trackedOrigins.has(Q.origin(t))},withoutSaving:function(t,e){Q.withOrigin(t,t.withoutSavingOrigin,e)}};t.CursorEditor=Z,t.YHistoryEditor=et,t.YjsEditor=Q,t.relativePositionToSlatePoint=P,t.relativeRangeToSlateRange=M,t.slateNodesToInsertDelta=Y,t.slatePointToRelativePosition=R,t.slateRangeToRelativeRange=D,t.withCursors=function(t,e){return void 0===e&&(e={}),function(n){var r=e.cursorStateField,i=void 0===r?"selection":r,a=e.cursorDataField,s=void 0===a?"data":a,c=e.autoSend,u=void 0===c||c,l=e.data,f=n;f.awareness=t,f.cursorDataField=s,f.selectionStateField=i,f.sendCursorData=function(t){f.awareness.setLocalStateField(f.cursorDataField,t)},f.sendCursorPosition=function(t){var e=f.awareness.getLocalState(),n=null==e?void 0:e[i];if(t){var r=D(f.sharedRoot,f,t),a=r.anchor,s=r.focus;n&&o.compareRelativePositions(a,n)&&o.compareRelativePositions(s,n)||f.awareness.setLocalStateField(f.selectionStateField,{anchor:a,focus:s})}else n&&f.awareness.setLocalStateField(f.selectionStateField,null)};var d=function(t){var e=V.get(f);if(e){var n=f.awareness.clientID,r={added:t.added.filter(function(t){return t!==n}),removed:t.removed.filter(function(t){return t!==n}),updated:t.updated.filter(function(t){return t!==n})};(r.added.length>0||r.removed.length>0||r.updated.length>0)&&e.forEach(function(t){return t(r)})}},h=f.connect,g=f.disconnect;return f.connect=function(){if(h(),f.awareness.on("change",d),d({removed:[],added:Array.from(f.awareness.getStates().keys()),updated:[]}),u){l&&Z.sendCursorData(f,l);var t=f.onChange;f.onChange=function(){t(),Q.connected(f)&&Z.sendCursorPosition(f)}}},f.disconnect=function(){f.awareness.off("change",d),d({removed:Array.from(f.awareness.getStates().keys()),added:[],updated:[]}),g()},f}},t.withYHistory=function(t){return void 0===t&&(t={}),function(e){var r=e,s=t.withoutSavingOrigin,c=void 0===s?tt:s,u=t.trackedOrigins,l=void 0===u?new Set([r.localOrigin]):u,f=a(t,["withoutSavingOrigin","trackedOrigins"]),d=new o.UndoManager(r.sharedRoot,i({trackedOrigins:l},f));r.undoManager=d,r.withoutSavingOrigin=c;var h=r.onChange,g=r.isLocalOrigin;r.onChange=function(){h(),$.set(r,r.selection&&D(r.sharedRoot,r,r.selection))},r.isLocalOrigin=function(t){return t===r.withoutSavingOrigin||g(t)};var p=function(t){var e=t.stackItem;e.meta.set("selection",r.selection&&D(r.sharedRoot,r,r.selection)),e.meta.set("selectionBefore",$.get(r))},v=function(t){t.stackItem.meta.set("selection",r.selection&&D(r.sharedRoot,r,r.selection))},y=function(t){var e=t.stackItem,o="undo"===t.type?r.undoManager.redoStack:r.undoManager.undoStack,i=o[o.length-1];i&&(i.meta.set("selection",e.meta.get("selectionBefore")),i.meta.set("selectionBefore",e.meta.get("selection")));var a=e.meta.get("selectionBefore");if(a){var s=M(r.sharedRoot,r,a);s&&n.Transforms.select(r,s)}},x=r.connect,m=r.disconnect;return r.connect=function(){x(),r.undoManager.on("stack-item-added",p),r.undoManager.on("stack-item-popped",y),r.undoManager.on("stack-item-updated",v)},r.disconnect=function(){r.undoManager.off("stack-item-added",p),r.undoManager.off("stack-item-popped",y),r.undoManager.off("stack-item-updated",v),m()},r.undo=function(){Q.connected(r)&&(Q.flushLocalChanges(r),r.undoManager.undo())},r.redo=function(){Q.connected(r)&&(Q.flushLocalChanges(r),r.undoManager.redo())},r}},t.withYjs=function(t,e){return void 0===e&&(e={}),function(r){var o,i,a=r;a.sharedRoot=t,a.localOrigin=null!==(o=e.localOrigin)&&void 0!==o?o:q,a.positionStorageOrigin=null!==(i=e.positionStorageOrigin)&&void 0!==i?i:H,a.applyRemoteEvents=function(t,e){Q.flushLocalChanges(a),n.Editor.withoutNormalizing(a,function(){Q.withOrigin(a,e,function(){z(a.sharedRoot,a,t)})})},a.isLocalOrigin=function(t){return t===a.localOrigin};var s=function(t,e){a.isLocalOrigin(e.origin)||Q.applyRemoteEvents(a,t,e.origin)},l=null;e.autoConnect&&(l=setTimeout(function(){l=null,Q.connect(a)})),a.connect=function(){if(Q.connected(a))throw new Error("already connected");a.sharedRoot.observeDeep(s);var t=L(a.sharedRoot);a.children=t.children,K.add(a),n.Editor.normalize(r,{force:!0}),r.operations.length||r.onChange()},a.disconnect=function(){l&&clearTimeout(l),Q.flushLocalChanges(a),a.sharedRoot.unobserveDeep(s),K.delete(a)},a.storeLocalChange=function(t){G.set(a,u(u([],c(Q.localChanges(a)),!1),[{op:t,doc:r.children,origin:Q.origin(a)}],!1))},a.flushLocalChanges=function(){E(a.sharedRoot);var t=Q.localChanges(a);G.delete(a);var e=[];t.forEach(function(t){var n=e[e.length-1];if(n&&n[0].origin===t.origin)return n.push(t);e.push([t])}),e.forEach(function(t){E(a.sharedRoot),a.sharedRoot.doc.transact(function(){t.forEach(function(t){E(a.sharedRoot),function(t,e,n){var r=B[n.type];if(!r)throw new Error("Unknown operation: ".concat(n.type));r(t,e,n)}(a.sharedRoot,{children:t.doc},t.op)})},t[0].origin)})};var f=a.apply,d=a.onChange;return a.apply=function(t){Q.connected(a)&&Q.isLocal(a)&&Q.storeLocalChange(a,t),f(t)},a.onChange=function(){Q.connected(a)&&Q.flushLocalChanges(a),d()},a}},t.yTextToSlateElement=L}); //# sourceMappingURL=index.js.map