@wangeditor-next/yjs 6.4.2 → 6.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/index.d.ts +124 -0
  2. package/dist/index.js +2 -2
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +2 -2
  5. package/dist/index.mjs.map +1 -1
  6. package/package.json +14 -15
  7. package/dist/yjs/src/applyToSlate/index.d.ts +0 -19
  8. package/dist/yjs/src/applyToSlate/textEvent.d.ts +0 -3
  9. package/dist/yjs/src/applyToYjs/index.d.ts +0 -3
  10. package/dist/yjs/src/applyToYjs/node/index.d.ts +0 -3
  11. package/dist/yjs/src/applyToYjs/node/insertNode.d.ts +0 -3
  12. package/dist/yjs/src/applyToYjs/node/mergeNode.d.ts +0 -3
  13. package/dist/yjs/src/applyToYjs/node/moveNode.d.ts +0 -3
  14. package/dist/yjs/src/applyToYjs/node/removeNode.d.ts +0 -3
  15. package/dist/yjs/src/applyToYjs/node/setNode.d.ts +0 -3
  16. package/dist/yjs/src/applyToYjs/node/splitNode.d.ts +0 -3
  17. package/dist/yjs/src/applyToYjs/text/index.d.ts +0 -3
  18. package/dist/yjs/src/applyToYjs/text/insertText.d.ts +0 -3
  19. package/dist/yjs/src/applyToYjs/text/removeText.d.ts +0 -3
  20. package/dist/yjs/src/applyToYjs/types.d.ts +0 -8
  21. package/dist/yjs/src/index.d.ts +0 -6
  22. package/dist/yjs/src/module/custom-types.d.ts +0 -41
  23. package/dist/yjs/src/plugins/index.d.ts +0 -6
  24. package/dist/yjs/src/plugins/withCursors.d.ts +0 -38
  25. package/dist/yjs/src/plugins/withYHistory.d.ts +0 -19
  26. package/dist/yjs/src/plugins/withYjs.d.ts +0 -42
  27. package/dist/yjs/src/utils/clone.d.ts +0 -4
  28. package/dist/yjs/src/utils/convert.d.ts +0 -7
  29. package/dist/yjs/src/utils/delta.d.ts +0 -7
  30. package/dist/yjs/src/utils/location.d.ts +0 -11
  31. package/dist/yjs/src/utils/object.d.ts +0 -9
  32. package/dist/yjs/src/utils/position.d.ts +0 -19
  33. package/dist/yjs/src/utils/slate.d.ts +0 -2
  34. package/dist/yjs/src/utils/yjs.d.ts +0 -4
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- 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,u;if("attributes"in r&&"retain"in r)for(var d=l(E(t,a-r.retain),2),h=d[0],y=d[1],m=l(E(t,a,{assoc:-1}),2),x=m[0],w=m[1],O=x;O>=h;O-=1){var T=t.children[O],R=f(f([],l(n),!1),[O],!1);if(e.isText(T)){var S=r.attributes,P=p.apply(void 0,f([t],l(Object.keys(r.attributes)),!1));if(O===h||O===x){var j=O===h?y:0;if((A=O===x?w:T.text.length)!==T.text.length&&i.push({type:"split_node",path:R,position:A,properties:F(T)}),0!==j){i.push({type:"split_node",path:R,position:j,properties:v(s(s({},F(T)),S))});continue}}i.push({type:"set_node",newProperties:S,path:R,properties:P})}}if("retain"in r&&(a-=r.retain),"delete"in r){var C=l(E(t,a-r.delete),2),k=(h=C[0],y=C[1],l(E(t,a,{assoc:-1}),2));for(x=k[0],O=0===(w=k[1])?x-1:x;O>=h;O-=1)if(T=t.children[O],R=f(f([],l(n),!1),[O],!1),!e.isText(T)||O!==h&&O!==x)i.push({type:"remove_node",node:T,path:R}),a-=b(T);else{j=O===h?y:0;var A=O===x?w:T.text.length;i.push({type:"remove_text",offset:j,text:T.text.slice(j,A),path:R}),a-=A-j}}else if("insert"in r){var M=l(E(t,a,{insert:!0}),2),_=(O=M[0],M[1]);if(T=t.children[O],"string"==typeof r.insert&&O>0&&(null==T||0===_)){var D=O-1,L=t.children[D];e.isText(L)&&g(null!==(c=r.attributes)&&void 0!==c?c:{},F(L))&&(O=D,_=L.text.length,T=L)}if(R=f(f([],l(n),!1),[O],!1),e.isText(T)){var X=i[i.length-1],Y=null!=X&&"insert_node"===X.type?X.node:F(T),z=[];if(null==X||"insert_node"!==X.type&&"insert_text"!==X.type&&"split_node"!==X.type&&"set_node"!==X.type||(z=X.path),"string"==typeof r.insert&&g(null!==(u=r.attributes)&&void 0!==u?u:{},Y)&&(null==X||o.equals(R,z)))return i.push({type:"insert_text",offset:_,text:r.insert,path:R});var U=I(r);return 0===_?i.push({type:"insert_node",path:R,node:U}):(_<T.text.length&&i.push({type:"split_node",path:R,position:_,properties:F(T)}),i.push({type:"insert_node",path:o.next(R),node:U}))}return i.push({type:"insert_node",path:R,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),v=d.textRange.end-d.textRange.start,m=i.path[i.path.length-1],w=o.equals(o.parent(i.path),a)&&m<s?h.textRange.end:h.textRange.start;d.yParent.delete(d.textRange.start,v),d.yParent===h.yParent&&d.textRange.start<w&&(w-=v);var b=x(y(h.yParent)),E=Math.min(w,b),T=f([{retain:E}],l(g),!1);h.yParent.applyDelta(T,{sanitize:!1}),_(t,h.yParent,p,g,E,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};
2
- //# sourceMappingURL=index.mjs.map
1
+ import*as e from"yjs";import{Editor as t,Element as n,Node as r,Path as i,Text as a,Transforms as o}from"slate";function s(e){return Object.prototype.toString.call(e)===`[object Object]`}function c(e){if(!s(e))return!1;let t=e.constructor;if(t===void 0)return!0;let n=t.prototype;return!(s(n)===!1||Object.prototype.hasOwnProperty.call(n,`isPrototypeOfOf`))}function l(e,t){for(let n in e){let r=e[n],i=t[n];if(c(r)&&c(i)){if(!l(r,i))return!1}else if(Array.isArray(r)&&Array.isArray(i)){if(r.length!==i.length)return!1;for(let e=0;e<r.length;e+=1)if(r[e]!==i[e])return!1}else if(r!==i)return!1}for(let n in t)if(e[n]===void 0&&t[n]!==void 0)return!1;return!0}function u(e,...t){return Object.fromEntries(Object.entries(e).filter(([e])=>t.includes(e)))}function d(e,...t){return Object.fromEntries(Object.entries(e).filter(([e])=>!t.includes(e)))}function f(e){return Object.fromEntries(Object.entries(e).filter(([,e])=>e!==null))}function p(e){"@babel/helpers - typeof";return p=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},p(e)}function m(e,t){if(p(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(p(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function h(e){var t=m(e,`string`);return p(t)==`symbol`?t:t+``}function g(e,t,n){return(t=h(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function _(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function v(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]==null?{}:arguments[t];t%2?_(Object(n),!0).forEach(function(t){g(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):_(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function y(e){let t=[];for(let i of e){var n,r;if(typeof i.insert==`string`&&i.insert.length===0)continue;let e=t[t.length-1];if(!e||typeof e.insert!=`string`||typeof i.insert!=`string`){t.push(i);continue}if(e.attributes===i.attributes||!e.attributes==!i.attributes&&l((n=e.attributes)==null?{}:n,(r=i.attributes)==null?{}:r)){e.insert+=i.insert;continue}t.push(i)}return t}function b(e){return y(e.toDelta())}function x({insert:e}){return typeof e==`string`?e.length:1}function S(e){return e.reduce((e,t)=>e+x(t),0)}function C(e,t,n){if(n<1)return[];let r=0,i=[],a=t+n;for(let n=0;n<e.length&&!(r>=a);n+=1){let o=e[n],s=x(o);if(r+s<=t){r+=s;continue}if(typeof o.insert!=`string`){r+=s,i.push(o);continue}let c=Math.max(0,t-r),l=Math.min(s,s-(r+s-a));i.push(v(v({},o),{},{insert:o.insert.slice(c,l)})),r+=s}return i}function w(e){return e?a.isText(e)?e.text.length:1:0}function ee(e,t){return e.children.slice(0,t).reduce((e,t)=>e+w(t),0)}function T(t,n,r){var i;if(r.length===0)throw Error(`Path has to a have a length >= 1`);if(a.isText(n))throw Error(`Cannot descent into slate text`);let[o,...s]=r,c=ee(n,o),l=n.children[o],u=b(t),d=w(l),f=C(u,c,d);if(f.length>1)throw Error(`Path doesn't match yText, yTarget spans multiple nodes`);let p=(i=f[0])==null?void 0:i.insert;if(s.length>0){if(!(p instanceof e.XmlText))throw Error(`Path doesn't match yText, cannot descent into non-yText`);return T(p,l,s)}return{yParent:t,textRange:{start:c,end:c+d},yTarget:p instanceof e.XmlText?p:void 0,slateParent:n,slateTarget:l,targetDelta:f}}function E(e,t,n={}){let{assoc:r=0,insert:i=!1}=n,o=0,s=0;for(let n=0;n<e.children.length;n+=1){let i=e.children[n],c=a.isText(i)?i.text.length:1;c>0&&(s=n);let l=o+c;if(c>0&&(r>=0?l>t:l>=t))return[n,t-o];o+=c}if(t>o+(i?1:0))throw Error(`yOffset out of bounds`);if(i)return[e.children.length,0];let c=e.children[s],l=a.isText(c)?c.text.length:1;return[s,l]}function D(t,n,r){let i=[r];for(;i[0]!==t;){let{parent:t}=i[0];if(!t)throw Error(`yText isn't a descendant of root element`);if(!(t instanceof e.XmlText))throw Error(`Unexpected y parent type`);i.unshift(t)}if(i.length<2)return[];let o=n;return i.reduce((e,t,n)=>{let r=i[n+1];if(!r)return e;let s=0,c=b(t);for(let e of c){if(e.insert===r)break;s+=typeof e.insert==`string`?e.insert.length:1}if(a.isText(o))throw Error(`Cannot descent into slate text`);let[l]=E(o,s);return o=o.children[l],e.concat(l)},[])}function O(e){if(!e.doc)throw Error(`shared type isn't attached to a document`)}const k=`__slateYjsStoredPosition_`;function A(t,n,r){let{yTarget:i,yParent:a,textRange:o}=T(t,n,r.path);if(i)throw Error(`Slate point points to a non-text element inside sharedRoot`);let s=o.start+r.offset;return e.createRelativePositionFromTypeIndex(a,s,s===o.end?-1:0)}function te(t,i,{type:o,index:s,assoc:c}){if(!(o instanceof e.XmlText))throw Error(`Absolute position points to a non-XMLText`);let l=D(t,i,o),u=r.get(i,l);if(a.isText(u)||!n.isElement(u))throw Error(`Absolute position doesn't match slateRoot, cannot descent into text and EditorRoot`);let[d,f]=E(u,s,{assoc:c}),p=u.children[d];return a.isText(p)?{path:[...l,d],offset:f}:null}function j(t,n,r){if(!t.doc)throw Error(`sharedRoot isn't attach to a yDoc`);let i=e.createAbsolutePositionFromRelativePosition(r,t.doc);return i&&te(t,n,i)}function ne(t,n){let r=t.getAttribute(k+n);return r?e.decodeRelativePosition(r):null}function re(t){return Object.fromEntries(Object.entries(t.getAttributes()).filter(([e])=>e.startsWith(k)).map(([t,n])=>[t.slice(25),e.createRelativePositionFromJSON(n)]))}function ie(t){return O(t),Object.fromEntries(Object.entries(t.getAttributes()).filter(([e])=>e.startsWith(k)).map(([n,r])=>[n.slice(25),e.createAbsolutePositionFromRelativePosition(e.decodeRelativePosition(r),t.doc)]).filter(([,e])=>e))}function ae(e,t){e.removeAttribute(k+t)}function M(t,n,r){t.setAttribute(k+n,e.encodeRelativePosition(r))}function N(e,t,n){return Object.fromEntries(Object.entries(e).filter(([,e])=>e.type===t?n?e.assoc>=0?e.index>=n.start&&e.index<n.end:e.index>n.start&&e.index>=n.end:!0:!1))}function P(t,n,r=``){let i={[r]:N(t,n)};return b(n).forEach(({insert:n},a)=>{n instanceof e.XmlText&&Object.assign(i,P(t,n,r?`${r}.${a}`:a.toString()))}),i}function F(t,n,r,i=0){let a=ie(t),o={"":N(a,n,{start:i,end:i+S(r)})};return r.forEach(({insert:t},n)=>{t instanceof e.XmlText&&Object.assign(o,P(a,t,n.toString()))}),o}function I(t,n,r,i,a=0,o=0,s=``){let c=r[s];c&&Object.entries(c).forEach(([r,i])=>{M(t,r,e.createRelativePositionFromTypeIndex(n,i.index-o+a,i.assoc))}),i.forEach(({insert:n},i)=>{n instanceof e.XmlText&&I(t,n,r,b(n),0,0,s?`${s}.${i}`:i.toString())})}function L(e,t,n){return{anchor:A(e,t,n.anchor),focus:A(e,t,n.focus)}}function R(e,t,n){let r=j(e,t,n.anchor);if(!r)return null;let i=j(e,t,n.focus);return i?{anchor:r,focus:i}:null}function z(e){return d(e,a.isText(e)?`text`:`children`)}function oe(e,t){if(e==null)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}function B(e,t){if(e==null)return{};var n,r,i=oe(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}const se=[`children`];function V(e){let t=b(e),n=t.length>0?t.map(H):[{text:``}];return v(v({},e.getAttributes()),{},{children:n})}function H(e){return typeof e.insert==`string`?v(v({},e.attributes),{},{text:e.insert}):V(e.insert)}function U(e){return e.map(e=>a.isText(e)?{insert:e.text,attributes:z(e)}:{insert:W(e)})}function W(t){let{children:n}=t,r=B(t,se),i=new e.XmlText;return Object.entries(r).forEach(([e,t])=>{i.setAttribute(e,t)}),i.applyDelta(U(n),{sanitize:!1}),i}function ce(e,t,n){let r=[],o=n.reduce((e,t)=>`retain`in t?e+t.retain:`delete`in t?e+t.delete:e,0);return n.reverse().forEach(n=>{if(`attributes`in n&&`retain`in n){let[i,s]=E(e,o-n.retain),[c,l]=E(e,o,{assoc:-1});for(let o=c;o>=i;--o){let d=e.children[o],p=[...t,o];if(!a.isText(d))continue;let m=n.attributes,h=u(e,...Object.keys(n.attributes));if(o===i||o===c){let e=o===i?s:0,t=o===c?l:d.text.length;if(t!==d.text.length&&r.push({type:`split_node`,path:p,position:t,properties:z(d)}),e!==0){r.push({type:`split_node`,path:p,position:e,properties:f(v(v({},z(d)),m))});continue}}r.push({type:`set_node`,newProperties:m,path:p,properties:h})}}if(`retain`in n&&(o-=n.retain),`delete`in n){let[i,s]=E(e,o-n.delete),[c,l]=E(e,o,{assoc:-1});for(let n=l===0?c-1:c;n>=i;--n){let u=e.children[n],d=[...t,n];if(a.isText(u)&&(n===i||n===c)){let e=n===i?s:0,t=n===c?l:u.text.length;r.push({type:`remove_text`,offset:e,text:u.text.slice(e,t),path:d}),o-=t-e;continue}r.push({type:`remove_node`,node:u,path:d}),o-=w(u)}return}if(`insert`in n){let[u,d]=E(e,o,{insert:!0}),f=e.children[u];if(typeof n.insert==`string`&&u>0&&(f==null||d===0)){var s;let t=u-1,r=e.children[t];a.isText(r)&&l((s=n.attributes)==null?{}:s,z(r))&&(u=t,d=r.text.length,f=r)}let p=[...t,u];if(a.isText(f)){var c;let e=r[r.length-1],t=e!=null&&e.type===`insert_node`?e.node:z(f),a=[];if(e!=null&&(e.type===`insert_node`||e.type===`insert_text`||e.type===`split_node`||e.type===`set_node`)&&(a=e.path),typeof n.insert==`string`&&l((c=n.attributes)==null?{}:c,t)&&(e==null||i.equals(p,a)))return r.push({type:`insert_text`,offset:d,text:n.insert,path:p});let o=H(n);return d===0?r.push({type:`insert_node`,path:p,node:o}):(d<f.text.length&&r.push({type:`split_node`,path:p,position:d,properties:z(f)}),r.push({type:`insert_node`,path:i.next(p),node:o}))}return r.push({type:`insert_node`,path:p,node:H(n)})}}),r}function le(t,n,i){let{target:o,changes:s}=i,c=i.delta;if(!(o instanceof e.XmlText))throw Error(`Unexpected target node type`);let l=[],u=D(t,n,o),d=r.get(n,u);if(a.isText(d))throw Error(`Cannot apply yTextEvent to text node`);let f=Array.from(s.keys.entries());if(u.length>0&&f.length>0){let e=Object.fromEntries(f.map(([e,t])=>[e,t.action===`delete`?null:o.getAttribute(e)])),t=Object.fromEntries(f.map(([e])=>[e,d[e]]));l.push({type:`set_node`,newProperties:e,properties:t,path:u})}return c.length>0&&l.push(...ce(d,u,c)),l}function ue(t,n,r){if(r instanceof e.YTextEvent)return le(t,n,r);throw Error(`Unexpected Y event type`)}function G(e,n,r){t.withoutNormalizing(n,()=>{r.forEach(t=>{ue(e,n,t).forEach(e=>{n.apply(e)})})})}function de(e,t,n){let{yParent:r,textRange:i}=T(e,t,n.path);if(a.isText(n.node))return r.insert(i.start,n.node.text,z(n.node));r.insertEmbed(i.start,W(n.node))}function K(e){return e.map(e=>typeof e.insert==`string`?e:v(v({},e),{},{insert:fe(e.insert)}))}function fe(t){let n=new e.XmlText,r=t.getAttributes();return Object.entries(r).forEach(([e,t])=>{n.setAttribute(e,t)}),n.applyDelta(K(b(t)),{sanitize:!1}),n}function pe(e,t,n){let o=T(e,t,n.path),s=T(o.yParent,o.slateParent,i.previous(n.path.slice(-1)));if(!o.yTarget!=!s.yTarget)throw Error(`Cannot merge y text with y element`);if(!s.yTarget||!o.yTarget){let{yParent:e,textRange:s,slateTarget:c}=o;if(!c)throw Error(`Expected Slate target node for merge op.`);let l=r.get(t,i.previous(n.path));if(!a.isText(l))throw Error(`Path points to Y.Text but not a Slate text node.`);let u=z(c),d=z(l),f=Object.keys(u).reduce((e,t)=>t in d?e:v(v({},e),{},{[t]:null}),{});return e.format(s.start,s.end-s.start,v(v({},f),d))}let c=s.yTarget.length,l=b(o.yTarget),u=K(l),d=F(e,o.yTarget,l,c),f=[{retain:c},...u];s.yTarget.applyDelta(f,{sanitize:!1}),o.yParent.delete(o.textRange.start,o.textRange.end-o.textRange.start),I(e,s.yTarget,d,u,c)}function me(e,t,n){let o=i.parent(n.newPath),s=n.newPath[n.newPath.length-1],c=r.get(t,o);if(a.isText(c))throw Error(`Cannot move slate node into text element`);let l=[...o,Math.min(s,c.children.length)],u=T(e,t,n.path),d=T(e,t,l),f=K(u.targetDelta),p=F(e,u.yParent,u.targetDelta),m=u.textRange.end-u.textRange.start,h=n.path[n.path.length-1],g=i.equals(i.parent(n.path),o)&&h<s?d.textRange.end:d.textRange.start;u.yParent.delete(u.textRange.start,m),u.yParent===d.yParent&&u.textRange.start<g&&(g-=m);let _=S(b(d.yParent)),v=Math.min(g,_),y=[{retain:v},...f];d.yParent.applyDelta(y,{sanitize:!1}),I(e,d.yParent,p,f,v,u.textRange.start)}function he(e,t,n){let{yParent:r,textRange:i}=T(e,t,n.path);r.delete(i.start,i.end-i.start)}function ge(e,t,n){let{yTarget:r,textRange:i,yParent:a}=T(e,t,n.path);if(r)return Object.entries(n.newProperties).forEach(([e,t])=>{if(t===null)return r.removeAttribute(e);r.setAttribute(e,t)}),Object.entries(n.properties).forEach(([e])=>{Object.prototype.hasOwnProperty.call(n.newProperties,e)||r.removeAttribute(e)});let o=v(v({},Object.fromEntries(Object.keys(n.properties).map(e=>[e,null]))),n.newProperties);a.format(i.start,i.end-i.start,o)}function _e(t,n,r){let i=T(t,n,r.path);if(!i.slateTarget)throw Error(`Y target without corresponding slate node`);if(!i.yTarget){if(!a.isText(i.slateTarget))throw Error(`Mismatch node type between y target and slate node`);let e={};return i.targetDelta.forEach(t=>{t.attributes&&Object.keys(t.attributes).forEach(t=>{e[t]=null})}),i.yParent.format(i.textRange.start,i.textRange.end-i.textRange.start,v(v({},e),r.properties))}if(a.isText(i.slateTarget))throw Error(`Mismatch node type between y target and slate node`);let o=T(i.yTarget,i.slateTarget,[r.position]),s=i.slateTarget.children.slice(0,r.position).reduce((e,t)=>e+w(t),0),c=i.slateTarget.children.reduce((e,t)=>e+w(t),0),l=C(b(i.yTarget),s,c-s),u=K(l),d=F(t,i.yTarget,l,s),f=new e.XmlText;f.applyDelta(u,{sanitize:!1}),Object.entries(r.properties).forEach(([e,t])=>{f.setAttribute(e,t)}),i.yTarget.delete(o.textRange.start,i.yTarget.length-o.textRange.start),i.yParent.insertEmbed(i.textRange.end,f),I(t,f,d,u,0,s)}const ve={insert_node:de,remove_node:he,set_node:ge,merge_node:pe,move_node:me,split_node:_e};function ye(e,t,n){let{yParent:i,textRange:o}=T(e,t,n.path),s=r.get(t,n.path);if(!a.isText(s))throw Error(`Cannot insert text into non-text node`);i.insert(o.start+n.offset,n.text,z(s))}function be(e,t,n){let{yParent:r,textRange:i}=T(e,t,n.path);r.delete(i.start+n.offset,n.text.length)}const xe=v(v(v({},{insert_text:ye,remove_text:be}),ve),{},{set_selection:()=>{}});function Se(e,t,n){let r=xe[n.type];if(!r)throw Error(`Unknown operation: ${n.type}`);r(e,t,n)}const Ce=Symbol(`slate-yjs-operation`),we=Symbol(`slate-yjs-position-storage`),q=new WeakMap,J=new WeakMap,Y=new WeakSet,X={isYjsEditor(n){return t.isEditor(n)&&n.sharedRoot instanceof e.XmlText&&`localOrigin`in n&&`positionStorageOrigin`in n&&typeof n.applyRemoteEvents==`function`&&typeof n.storeLocalChange==`function`&&typeof n.flushLocalChanges==`function`&&typeof n.isLocalOrigin==`function`&&typeof n.connect==`function`&&typeof n.disconnect==`function`},localChanges(e){var t;return(t=J.get(e))==null?[]:t},applyRemoteEvents(e,t,n){e.applyRemoteEvents(t,n)},storeLocalChange(e,t){e.storeLocalChange(t)},flushLocalChanges(e){e.flushLocalChanges()},connected(e){return Y.has(e)},connect(e){e.connect()},disconnect(e){e.disconnect()},isLocal(e){return e.isLocalOrigin(X.origin(e))},origin(e){let t=q.get(e);return t===void 0?e.localOrigin:t},withOrigin(e,t,n){let r=X.origin(e);q.set(e,t),n(),q.set(e,r)},storePosition(e,t,n){let{sharedRoot:r,positionStorageOrigin:i}=e;O(r);let a=A(r,e,n);r.doc.transact(()=>{M(r,t,a)},i)},removeStoredPosition(e,t){let{sharedRoot:n,positionStorageOrigin:r}=e;O(n),n.doc.transact(()=>{ae(n,t)},r)},position(e,t){let n=ne(e.sharedRoot,t);if(n)return j(e.sharedRoot,e,n)},storedPositionsRelative(e){return re(e.sharedRoot)}};function Te(e,n={}){return function(r){var i,a;let o=r;o.sharedRoot=e,o.localOrigin=(i=n.localOrigin)==null?Ce:i,o.positionStorageOrigin=(a=n.positionStorageOrigin)==null?we:a,o.applyRemoteEvents=(e,n)=>{X.flushLocalChanges(o),t.withoutNormalizing(o,()=>{X.withOrigin(o,n,()=>{G(o.sharedRoot,o,e)})})},o.isLocalOrigin=e=>e===o.localOrigin;let s=(e,t)=>{o.isLocalOrigin(t.origin)||X.applyRemoteEvents(o,e,t.origin)},c=null;n.autoConnect&&(c=setTimeout(()=>{c=null,X.connect(o)})),o.connect=()=>{if(X.connected(o))throw Error(`already connected`);o.sharedRoot.observeDeep(s),o.children=V(o.sharedRoot).children,Y.add(o),t.normalize(r,{force:!0}),r.operations.length||r.onChange()},o.disconnect=()=>{c&&clearTimeout(c),X.flushLocalChanges(o),o.sharedRoot.unobserveDeep(s),Y.delete(o)},o.storeLocalChange=e=>{J.set(o,[...X.localChanges(o),{op:e,doc:r.children,origin:X.origin(o)}])},o.flushLocalChanges=()=>{O(o.sharedRoot);let e=X.localChanges(o);J.delete(o);let t=[];e.forEach(e=>{let n=t[t.length-1];if(n&&n[0].origin===e.origin)return n.push(e);t.push([e])}),t.forEach(e=>{O(o.sharedRoot),o.sharedRoot.doc.transact(()=>{e.forEach(e=>{O(o.sharedRoot),Se(o.sharedRoot,{children:e.doc},e.op)})},e[0].origin)})};let{apply:l,onChange:u}=o;return o.apply=e=>{X.connected(o)&&X.isLocal(o)&&X.storeLocalChange(o,e),l(e)},o.onChange=()=>{X.connected(o)&&X.flushLocalChanges(o),u()},o}}const Z=new WeakMap,Q={isCursorEditor(e){return X.isYjsEditor(e)&&e.awareness&&typeof e.cursorDataField==`string`&&typeof e.selectionStateField==`string`&&typeof e.sendCursorPosition==`function`&&typeof e.sendCursorData==`function`},sendCursorPosition(e,t=e.selection){e.sendCursorPosition(t)},sendCursorData(e,t){e.sendCursorData(t)},on(e,t,n){var r;if(t!==`change`)return;let i=(r=Z.get(e))==null?new Set:r;i.add(n),e&&Z.set(e,i)},off(e,t,n){if(t!==`change`)return;let r=Z.get(e);r&&r.delete(n)},cursorState(e,t){var n;if(t===e.awareness.clientID||!X.connected(e))return null;let r=e.awareness.getStates().get(t);return r?{relativeSelection:(n=r[e.selectionStateField])==null?null:n,data:r[e.cursorDataField],clientId:t}:null},cursorStates(e){return X.connected(e)?Object.fromEntries(Array.from(e.awareness.getStates().entries(),([t,n])=>t===e.awareness.clientID||!n?null:[t,{relativeSelection:n[e.selectionStateField],data:n[e.cursorDataField]}]).filter(Array.isArray)):{}}};function Ee(t,n={}){return function(r){let{cursorStateField:i=`selection`,cursorDataField:a=`data`,autoSend:o=!0,data:s}=n,c=r;c.awareness=t,c.cursorDataField=a,c.selectionStateField=i,c.sendCursorData=e=>{c.awareness.setLocalStateField(c.cursorDataField,e)},c.sendCursorPosition=t=>{let n=c.awareness.getLocalState(),r=n==null?void 0:n[i];if(!t){r&&c.awareness.setLocalStateField(c.selectionStateField,null);return}let{anchor:a,focus:o}=L(c.sharedRoot,c,t);(!r||!e.compareRelativePositions(a,r)||!e.compareRelativePositions(o,r))&&c.awareness.setLocalStateField(c.selectionStateField,{anchor:a,focus:o})};let l=e=>{let t=Z.get(c);if(!t)return;let n=c.awareness.clientID,r={added:e.added.filter(e=>e!==n),removed:e.removed.filter(e=>e!==n),updated:e.updated.filter(e=>e!==n)};(r.added.length>0||r.removed.length>0||r.updated.length>0)&&t.forEach(e=>e(r))},{connect:u,disconnect:d}=c;return c.connect=()=>{if(u(),c.awareness.on(`change`,l),l({removed:[],added:Array.from(c.awareness.getStates().keys()),updated:[]}),o){s&&Q.sendCursorData(c,s);let{onChange:e}=c;c.onChange=()=>{e(),X.connected(c)&&Q.sendCursorPosition(c)}}},c.disconnect=()=>{c.awareness.off(`change`,l),l({removed:Array.from(c.awareness.getStates().keys()),added:[],updated:[]}),d()},c}}const De=[`withoutSavingOrigin`,`trackedOrigins`],$=new WeakMap,Oe=Symbol(`slate-yjs-history-without-saving`),ke={isYHistoryEditor(t){return X.isYjsEditor(t)&&t.undoManager instanceof e.UndoManager&&typeof t.undo==`function`&&typeof t.redo==`function`&&`withoutSavingOrigin`in t},canUndo(e){return e.undoManager.undoStack.length>0},canRedo(e){return e.undoManager.redoStack.length>0},isSaving(e){return e.undoManager.trackedOrigins.has(X.origin(e))},withoutSaving(e,t){X.withOrigin(e,e.withoutSavingOrigin,t)}};function Ae(t={}){return function(n){let r=n,{withoutSavingOrigin:i=Oe,trackedOrigins:a=new Set([r.localOrigin])}=t,s=B(t,De);r.undoManager=new e.UndoManager(r.sharedRoot,v({trackedOrigins:a},s)),r.withoutSavingOrigin=i;let{onChange:c,isLocalOrigin:l}=r;r.onChange=()=>{c(),$.set(r,r.selection&&L(r.sharedRoot,r,r.selection))},r.isLocalOrigin=e=>e===r.withoutSavingOrigin||l(e);let u=({stackItem:e})=>{e.meta.set(`selection`,r.selection&&L(r.sharedRoot,r,r.selection)),e.meta.set(`selectionBefore`,$.get(r))},d=({stackItem:e})=>{e.meta.set(`selection`,r.selection&&L(r.sharedRoot,r,r.selection))},f=({stackItem:e,type:t})=>{let n=t===`undo`?r.undoManager.redoStack:r.undoManager.undoStack,i=n[n.length-1];i&&(i.meta.set(`selection`,e.meta.get(`selectionBefore`)),i.meta.set(`selectionBefore`,e.meta.get(`selection`)));let a=e.meta.get(`selectionBefore`);if(!a)return;let s=R(r.sharedRoot,r,a);s&&o.select(r,s)},{connect:p,disconnect:m}=r;return r.connect=()=>{p(),r.undoManager.on(`stack-item-added`,u),r.undoManager.on(`stack-item-popped`,f),r.undoManager.on(`stack-item-updated`,d)},r.disconnect=()=>{r.undoManager.off(`stack-item-added`,u),r.undoManager.off(`stack-item-popped`,f),r.undoManager.off(`stack-item-updated`,d),m()},r.undo=()=>{X.connected(r)&&(X.flushLocalChanges(r),r.undoManager.undo())},r.redo=()=>{X.connected(r)&&(X.flushLocalChanges(r),r.undoManager.redo())},r}}export{Q as CursorEditor,ke as YHistoryEditor,X as YjsEditor,j as relativePositionToSlatePoint,R as relativeRangeToSlateRange,U as slateNodesToInsertDelta,A as slatePointToRelativePosition,L as slateRangeToRelativeRange,Ee as withCursors,Ae as withYHistory,Te as withYjs,V as yTextToSlateElement};
2
+ //# sourceMappingURL=index.mjs.map