UNPKG

@wangeditor-next/yjs

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