@slate-yjs/core 0.1.1 → 0.2.2
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.
- package/README.md +3 -0
- package/dist/applyToSlate/index.d.ts +19 -19
- package/dist/applyToSlate/index.d.ts.map +1 -1
- package/dist/applyToSlate/textEvent.d.ts +3 -3
- package/dist/applyToSlate/textEvent.d.ts.map +1 -1
- package/dist/applyToYjs/index.d.ts +3 -3
- package/dist/applyToYjs/node/index.d.ts +3 -3
- package/dist/applyToYjs/node/insertNode.d.ts +3 -3
- package/dist/applyToYjs/node/mergeNode.d.ts +3 -3
- package/dist/applyToYjs/node/moveNode.d.ts +3 -3
- package/dist/applyToYjs/node/removeNode.d.ts +3 -3
- package/dist/applyToYjs/node/setNode.d.ts +3 -3
- package/dist/applyToYjs/node/splitNode.d.ts +3 -3
- package/dist/applyToYjs/text/index.d.ts +3 -3
- package/dist/applyToYjs/text/insertText.d.ts +3 -3
- package/dist/applyToYjs/text/removeText.d.ts +3 -3
- package/dist/applyToYjs/types.d.ts +8 -8
- package/dist/index.cjs +1280 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.global.js +10418 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +1257 -10
- package/dist/index.js.map +1 -1
- package/dist/model/types.d.ts +37 -37
- package/dist/plugins/index.d.ts +3 -3
- package/dist/plugins/withCursors.d.ts +38 -38
- package/dist/plugins/withYHistory.d.ts +19 -14
- package/dist/plugins/withYHistory.d.ts.map +1 -1
- package/dist/plugins/withYjs.d.ts +42 -39
- package/dist/plugins/withYjs.d.ts.map +1 -1
- package/dist/utils/clone.d.ts +4 -4
- package/dist/utils/convert.d.ts +7 -7
- package/dist/utils/delta.d.ts +7 -6
- package/dist/utils/delta.d.ts.map +1 -1
- package/dist/utils/location.d.ts +11 -11
- package/dist/utils/object.d.ts +9 -6
- package/dist/utils/object.d.ts.map +1 -1
- package/dist/utils/position.d.ts +19 -19
- package/dist/utils/position.d.ts.map +1 -1
- package/dist/utils/slate.d.ts +2 -2
- package/dist/utils/slate.d.ts.map +1 -1
- package/dist/utils/yjs.d.ts +4 -4
- package/package.json +12 -12
- package/src/applyToSlate/index.ts +2 -2
- package/src/applyToSlate/textEvent.ts +21 -20
- package/src/plugins/withYHistory.ts +109 -65
- package/src/plugins/withYjs.ts +68 -33
- package/src/utils/delta.ts +36 -1
- package/src/utils/object.ts +65 -20
- package/src/utils/position.ts +1 -1
- package/src/utils/slate.ts +3 -3
- package/test/collaboration/addMark/acrossMarks.tsx +40 -0
- package/test/collaboration/addMark/acrossMarksSame.tsx +36 -0
- package/test/collaboration/addMark/atBeginningOfDocument.tsx +27 -0
- package/test/collaboration/addMark/atEndOfDocument.tsx +30 -0
- package/test/collaboration/addMark/withOtherMarks.tsx +31 -0
- package/test/collaboration/insertNode/atBeginningOfDocument.tsx +28 -0
- package/test/collaboration/insertNode/atEndOfDocument.tsx +28 -0
- package/test/collaboration/insertNode/inTheMiddle.tsx +30 -0
- package/test/collaboration/insertText/atBeginningOfBlock.tsx +26 -0
- package/test/collaboration/insertText/atBeginningOfDocument.tsx +26 -0
- package/test/collaboration/insertText/atEndOfBlock.tsx +26 -0
- package/test/collaboration/insertText/atEndOfDocument.tsx +26 -0
- package/test/collaboration/insertText/inTheMiddle.tsx +32 -0
- package/test/collaboration/insertText/inTheMiddleOfNestedBlock.tsx +30 -0
- package/test/collaboration/insertText/insideMarks.tsx +28 -0
- package/test/collaboration/insertText/withEmptyString.tsx +25 -0
- package/test/collaboration/insertText/withEntities.tsx +28 -0
- package/test/collaboration/insertText/withMarks.tsx +25 -0
- package/test/collaboration/insertText/withUnicode.tsx +28 -0
- package/test/collaboration/mergeNode/afterADeleteBackward.tsx +27 -0
- package/test/collaboration/mergeNode/onMixedNestedNodes.tsx +29 -0
- package/test/collaboration/mergeNode/onMixedTypeNodes.tsx +27 -0
- package/test/collaboration/mergeNode/withUnicode.tsx +25 -0
- package/test/collaboration/moveNode/downward/whenBlockBecomesNested.tsx +43 -0
- package/test/collaboration/moveNode/downward/whenBlockBecomesNonNested.tsx +41 -0
- package/test/collaboration/moveNode/downward/whenBlockStaysNested.tsx +38 -0
- package/test/collaboration/moveNode/downward/whenBlockStaysNonNested.tsx +30 -0
- package/test/collaboration/moveNode/upward/whenBlockBecomesNested.tsx +43 -0
- package/test/collaboration/moveNode/upward/whenBlockBecomesNonNested.tsx +41 -0
- package/test/collaboration/moveNode/upward/whenBlockStaysNested.tsx +38 -0
- package/test/collaboration/moveNode/upward/whenBlockStaysNonNested.tsx +30 -0
- package/test/collaboration/removeMark/inTheMiddleOfText.tsx +43 -0
- package/test/collaboration/removeMark/withAddMark.tsx +31 -0
- package/test/collaboration/removeMark/withOtherMarks.tsx +47 -0
- package/test/collaboration/removeNode/atBeginningOfDocument.tsx +26 -0
- package/test/collaboration/removeNode/atEndOfDocument.tsx +26 -0
- package/test/collaboration/removeNode/nestedBlock.tsx +28 -0
- package/test/collaboration/removeNode/wrapperBlock.tsx +28 -0
- package/test/collaboration/removeText/atBeginningOfDocument.tsx +34 -0
- package/test/collaboration/removeText/atEndOfDocument.tsx +37 -0
- package/test/collaboration/removeText/withUnicode.tsx +29 -0
- package/test/collaboration/setNode/atBeginningOfDocument.tsx +27 -0
- package/test/collaboration/setNode/atEndOfDocument.tsx +27 -0
- package/test/collaboration/setNode/onDataChange.tsx +35 -0
- package/test/collaboration/setNode/onDataChangeOnInline.tsx +35 -0
- package/test/collaboration/setNode/onResetBlock.tsx +27 -0
- package/test/collaboration/setNode/withAChangeOfType.tsx +26 -0
- package/test/collaboration/splitNode/atBeginningOfDocument.tsx +28 -0
- package/test/collaboration/splitNode/atEndOfBlock.tsx +27 -0
- package/test/collaboration/splitNode/atEndOfDocument.tsx +27 -0
- package/test/collaboration/splitNode/onNonDefaultBlock.tsx +27 -0
- package/test/collaboration/splitNode/withMultipleSubNodes.tsx +32 -0
- package/test/collaboration/splitNode/withUnicode.tsx +29 -0
- package/test/index.test.ts +66 -0
- package/test/{types.d.ts → slate.d.ts} +0 -0
- package/test/withTestingElements.ts +46 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/tsup.config.ts +32 -0
- package/dist/applyToSlate/Untitled-1.d.ts +0 -101
- package/dist/applyToSlate/Untitled-1.d.ts.map +0 -1
- package/dist/applyToSlate/index.js +0 -32
- package/dist/applyToSlate/index.js.map +0 -1
- package/dist/applyToSlate/textEvent.js +0 -166
- package/dist/applyToSlate/textEvent.js.map +0 -1
- package/dist/applyToYjs/index.js +0 -17
- package/dist/applyToYjs/index.js.map +0 -1
- package/dist/applyToYjs/node/index.js +0 -15
- package/dist/applyToYjs/node/index.js.map +0 -1
- package/dist/applyToYjs/node/insertNode.js +0 -12
- package/dist/applyToYjs/node/insertNode.js.map +0 -1
- package/dist/applyToYjs/node/mergeNode.js +0 -32
- package/dist/applyToYjs/node/mergeNode.js.map +0 -1
- package/dist/applyToYjs/node/moveNode.js +0 -28
- package/dist/applyToYjs/node/moveNode.js.map +0 -1
- package/dist/applyToYjs/node/removeNode.js +0 -6
- package/dist/applyToYjs/node/removeNode.js.map +0 -1
- package/dist/applyToYjs/node/setNode.js +0 -21
- package/dist/applyToYjs/node/setNode.js.map +0 -1
- package/dist/applyToYjs/node/splitNode.js +0 -50
- package/dist/applyToYjs/node/splitNode.js.map +0 -1
- package/dist/applyToYjs/text/index.js +0 -7
- package/dist/applyToYjs/text/index.js.map +0 -1
- package/dist/applyToYjs/text/insertText.js +0 -12
- package/dist/applyToYjs/text/insertText.js.map +0 -1
- package/dist/applyToYjs/text/removeText.js +0 -6
- package/dist/applyToYjs/text/removeText.js.map +0 -1
- package/dist/applyToYjs/types.js +0 -2
- package/dist/applyToYjs/types.js.map +0 -1
- package/dist/index.modern.js +0 -2
- package/dist/index.modern.js.map +0 -1
- package/dist/index.module.js +0 -2
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.map +0 -1
- package/dist/model/types.js +0 -2
- package/dist/model/types.js.map +0 -1
- package/dist/plugins/index.js +0 -4
- package/dist/plugins/index.js.map +0 -1
- package/dist/plugins/withCursors.js +0 -145
- package/dist/plugins/withCursors.js.map +0 -1
- package/dist/plugins/withYHistory.js +0 -66
- package/dist/plugins/withYHistory.js.map +0 -1
- package/dist/plugins/withYjs.js +0 -155
- package/dist/plugins/withYjs.js.map +0 -1
- package/dist/utils/clone.js +0 -23
- package/dist/utils/clone.js.map +0 -1
- package/dist/utils/convert.js +0 -35
- package/dist/utils/convert.js.map +0 -1
- package/dist/utils/delta.js +0 -42
- package/dist/utils/delta.js.map +0 -1
- package/dist/utils/location.js +0 -111
- package/dist/utils/location.js.map +0 -1
- package/dist/utils/object.js +0 -27
- package/dist/utils/object.js.map +0 -1
- package/dist/utils/position.js +0 -140
- package/dist/utils/position.js.map +0 -1
- package/dist/utils/slate.js +0 -6
- package/dist/utils/slate.js.map +0 -1
- package/dist/utils/yjs.js +0 -7
- package/dist/utils/yjs.js.map +0 -1
- package/test/collaboration.test.ts +0 -395
- package/test/concurrent.test.ts +0 -233
- package/test/testEditor.ts +0 -134
- package/test/utils.ts +0 -64
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,1280 @@
|
|
|
1
|
-
var t=require("slate"),e=require("yjs"),n=require("y-protocols/awareness");function r(t){if(t&&t.__esModule)return 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,e}var o=/*#__PURE__*/r(e);function a(){return a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t},a.apply(this,arguments)}function i(t,e){if(null==t)return{};var n,r,o={},a=Object.keys(t);for(r=0;r<a.length;r++)e.indexOf(n=a[r])>=0||(o[n]=t[n]);return o}function s(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function c(t){return t.toDelta()}function u(t){var e=t.insert;return"string"==typeof e?e.length:1}function l(t){return t.reduce(function(t,e){return t+u(e)},0)}function f(t,e,n){if(n<1)return[];for(var r=0,o=[],i=e+n,s=0;s<t.length&&!(r>=i);s++){var c=t[s],l=u(c);if(r+l<=e)r+=l;else if("string"==typeof c.insert){var f=Math.max(0,e-r),d=Math.min(l,l-(r+l-i));o.push(a({},c,{insert:c.insert.slice(f,d)})),r+=l}else r+=l,o.push(c)}return o}function d(t){return null!==t&&"object"==typeof t}function h(t,e){var n=Object.keys(t),r=Object.keys(e);return n.length===r.length&&n.every(function(n){var r=t[n],o=e[n];return d(r)?d(o)&&h(r,o):r===o})}function p(t){var e=arguments;return Object.fromEntries(Object.entries(t).filter(function(t){var n=t[0];return[].slice.call(e,1).includes(n)}))}function g(e){return function(t){var e=arguments;return Object.fromEntries(Object.entries(t).filter(function(t){var n=t[0];return![].slice.call(e,1).includes(n)}))}(e,t.Text.isText(e)?"text":"children")}var v=["children"];function y(t){var e=c(t),n=e.length>0?e.map(x):[{text:""}];return a({},t.getAttributes(),{children:n})}function x(t){return"string"==typeof t.insert?a({},t.attributes,{text:t.insert}):y(t.insert)}function m(e){return e.map(function(e){return t.Text.isText(e)?{insert:e.text,attributes:g(e)}:{insert:T(e)}})}function T(t){var e=t.children,n=i(t,v),r=new o.XmlText;return Object.entries(n).forEach(function(t){r.setAttribute(t[0],t[1])}),r.applyDelta(m(e),{sanitize:!1}),r}function w(e){return e?t.Text.isText(e)?e.text.length:1:0}function b(e,n,r){var a;if(0===r.length)throw new Error("Path has to a have a length >= 1");if(t.Text.isText(n))throw new Error("Cannot descent into slate text");var i=r[0],s=r.slice(1),u=function(t,e){return t.children.slice(0,e).reduce(function(t,e){return t+w(e)},0)}(n,i),l=n.children[i],d=c(e),h=w(l),p=f(d,u,h);if(p.length>1)throw new Error("Path doesn't match yText, yTarget spans multiple nodes");var g=null==(a=p[0])?void 0:a.insert;if(s.length>0){if(!(g instanceof o.XmlText))throw new Error("Path doesn't match yText, cannot descent into non-yText");return b(g,l,s)}return{yParent:e,textRange:{start:u,end:u+h},yTarget:g instanceof o.XmlText?g:void 0,slateParent:n,slateTarget:l,targetDelta:p}}function E(e,n,r){void 0===r&&(r={});for(var o=r.assoc,a=void 0===o?0:o,i=r.insert,s=void 0!==i&&i,c=0,u=0,l=0;l<e.children.length;l++){var f=e.children[l],d=t.Text.isText(f)?f.text.length:1;d>0&&(u=l);var h=c+d;if(d>0&&(a>=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[e.children.length,0];var p=e.children[u];return[u,t.Text.isText(p)?p.text.length:1]}function P(e,n,r){for(var a=[r];a[0]!==e;){var i=a[0].parent;if(!i)throw new Error("yText isn't a descendant of root element");if(!(i instanceof o.XmlText))throw new Error("Unexpected y parent type");a.unshift(i)}if(a.length<2)return[];var u=n;return a.reduce(function(e,n,r){var o=a[r+1];if(!o)return e;for(var i,l=0,f=function(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return s(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?s(t,e):void 0}}(t))){n&&(t=n);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(c(n));!(i=f()).done;){var d=i.value;if(d.insert===o)break;l+="string"==typeof d.insert?d.insert.length:1}if(t.Text.isText(u))throw new Error("Cannot descent into slate text");var h=E(u,l)[0];return u=u.children[h],e.concat(h)},[])}function R(e,n,r){var o=[],i=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){if("attributes"in r&&"retain"in r)for(var s=E(e,i-r.retain),c=s[0],u=s[1],l=E(e,i),f=l[0],d=l[1],v=0===d?f-1:f;v>=c;v--){var y=e.children[v],m=[].concat(n,[v]),T=r.attributes;if(t.Text.isText(y)&&(v===c||v===f)){var b=v===c?u:0,P=v===f?d:y.text.length,R=g(y);0!==b&&(o.push({type:"split_node",path:m,position:b,properties:a({},R,T)}),m=t.Path.next(m),P-=b),P!==y.text.length&&o.push({type:"split_node",path:m,position:P,properties:a({},R,T)})}var O=p.apply(void 0,[e].concat(Object.keys(r.attributes)));o.push({type:"set_node",newProperties:T,path:m,properties:O})}if("retain"in r&&(i-=r.retain),"delete"in r)for(var S=E(e,i-r.delete),j=S[0],_=S[1],C=E(e,i,{assoc:-1}),A=C[0],D=C[1],k=0===D?A-1:A;k>=j;k--){var F=e.children[k],M=[].concat(n,[k]);if(!t.Text.isText(F)||k!==j&&k!==A)o.push({type:"remove_node",node:F,path:M}),i-=w(F);else{var L=k===j?_:0,Y=k===A?D:F.text.length;o.push({type:"remove_text",offset:L,text:F.text.slice(L,Y),path:M}),i-=Y-L}}else if("insert"in r){var X=E(e,i,{insert:!0}),I=X[0],N=X[1],U=e.children[I],z=[].concat(n,[I]);if(t.Text.isText(U)){var W;if("string"==typeof r.insert&&h(null!=(W=r.attributes)?W:{},g(U)))return o.push({type:"insert_text",offset:N,text:r.insert,path:z});var B=x(r);return 0===N?o.push({type:"insert_node",path:z,node:B}):(N<U.text.length&&o.push({type:"split_node",path:z,position:N,properties:g(U)}),o.push({type:"insert_node",path:t.Path.next(z),node:B}))}return o.push({type:"insert_node",path:z,node:x(r)})}}),o}function O(t){return t.map(function(t){return"string"==typeof t.insert?t:a({},t,{insert:(e=t.insert,n=new o.XmlText,r=e.getAttributes(),Object.entries(r).forEach(function(t){n.setAttribute(t[0],t[1])}),n.applyDelta(O(c(e)),{sanitize:!1}),n)});var e,n,r})}function S(t){if(!t.doc)throw new Error("shared type isn't attached to a document")}function j(t,e,n){var r=b(t,e,n.path),a=r.yParent,i=r.textRange;if(r.yTarget)throw new Error("Slate point points to a non-text element inside sharedRoot");return o.createRelativePositionFromTypeIndex(a,i.start+n.offset,n.offset===i.end?-1:0)}function _(e,n,r){if(!e.doc)throw new Error("sharedRoot isn't attach to a yDoc");var a=o.createAbsolutePositionFromRelativePosition(r,e.doc);return a&&function(e,n,r){var a=r.type,i=r.index,s=r.assoc;if(!(a instanceof o.XmlText))throw new Error("Absolute position points to a non-XMLText");var c=P(e,n,a),u=t.Node.get(n,c);if(t.Text.isText(u))throw new Error("Absolute position doesn't match slateRoot, cannot descent into text");var l=E(u,i,{assoc:s}),f=l[0],d=l[1];return t.Text.isText(u.children[f])?{path:[].concat(c,[f]),offset:d}:null}(e,n,a)}function C(t,e,n){t.setAttribute("__slateYjsStoredPosition_"+e,o.encodeRelativePosition(n))}function A(t,e,n){return Object.fromEntries(Object.entries(t).filter(function(t){var r=t[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 D(t,e,n){var r;void 0===n&&(n="");var a=((r={})[n]=A(t,e),r);return c(e).forEach(function(e,r){var i=e.insert;i instanceof o.XmlText&&Object.assign(a,D(t,i,n?n+"."+r:r.toString()))}),a}function k(t,e,n,r){void 0===r&&(r=0);var a=function(t){return S(t),Object.fromEntries(Object.entries(t.getAttributes()).filter(function(t){return t[0].startsWith("__slateYjsStoredPosition_")}).map(function(e){var n=e[1];return[e[0].slice("__slateYjsStoredPosition_".length),o.createAbsolutePositionFromRelativePosition(o.createRelativePositionFromJSON(n),t.doc)]}).filter(function(t){return t[1]}))}(t),i={"":A(a,e,{start:r,end:r+l(n)})};return n.forEach(function(t,e){var n=t.insert;n instanceof o.XmlText&&Object.assign(i,D(a,n,e.toString()))}),i}function F(t,e,n,r,a,i,s){void 0===a&&(a=0),void 0===i&&(i=0),void 0===s&&(s="");var u=n[s];u&&Object.entries(u).forEach(function(n){var r=n[1];C(t,n[0],o.createRelativePositionFromTypeIndex(e,r.index-i+a,r.assoc))}),r.forEach(function(e,r){var a=e.insert;a instanceof o.XmlText&&F(t,a,n,c(a),0,0,s?s+"."+r:r.toString())})}function M(t,e,n){return{anchor:j(t,e,n.anchor),focus:j(t,e,n.focus)}}function L(t,e,n){var r=_(t,e,n.anchor);if(!r)return null;var o=_(t,e,n.focus);return o?{anchor:r,focus:o}:null}var Y={insert_node:function(e,n,r){var o=b(e,n,r.path),a=o.yParent,i=o.textRange;if(t.Text.isText(r.node))return a.insert(i.start,r.node.text,g(r.node));a.insertEmbed(i.start,T(r.node))},remove_node:function(t,e,n){var r=b(t,e,n.path),o=r.textRange;r.yParent.delete(o.start,o.end-o.start)},set_node:function(t,e,n){var r=b(t,e,n.path),o=r.yTarget,i=r.textRange,s=r.yParent;if(o)return Object.entries(n.newProperties).forEach(function(t){var e=t[0],n=t[1];if(null===n)return o.removeAttribute(e);o.setAttribute(e,n)}),Object.entries(n.properties).forEach(function(t){var e=t[0];n.newProperties.hasOwnProperty(e)||o.removeAttribute(e)});var c=a({},Object.fromEntries(Object.keys(n.properties).map(function(t){return[t,null]})),n.newProperties);s.format(i.start,i.end-i.start,c)},merge_node:function(e,n,r){var o=b(e,n,r.path),a=b(o.yParent,o.slateParent,t.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 i=o.yParent,s=o.textRange,u=t.Node.get(n,t.Path.previous(r.path));if(!t.Text.isText(u))throw new Error("Path points to a y text but not a slate node");return i.format(s.start,s.start-s.end,g(u))}var l=a.yTarget.length,f=c(o.yTarget),d=O(f),h=k(e,o.yTarget,f,l),p=[{retain:l}].concat(d);a.yTarget.applyDelta(p,{sanitize:!1}),o.yParent.delete(o.textRange.start,o.textRange.end-o.textRange.start),F(e,a.yTarget,h,d,l)},move_node:function(e,n,r){var o=t.Path.parent(r.newPath),a=r.newPath[r.newPath.length-1],i=t.Node.get(n,o);if(t.Text.isText(i))throw new Error("Cannot move slate node into text element");var s=[].concat(o,[Math.min(a,i.children.length)]),u=b(e,n,r.path),f=b(e,n,s),d=O(u.targetDelta),h=k(e,u.yParent,u.targetDelta);u.yParent.delete(u.textRange.start,u.textRange.end-u.textRange.start);var p=l(c(f.yParent)),g=Math.min(f.textRange.start,p),v=[{retain:g}].concat(d);f.yParent.applyDelta(v,{sanitize:!1}),F(e,f.yParent,h,d,g,u.textRange.start)},split_node:function(e,n,r){var i=b(e,n,r.path);if(!i.slateTarget)throw new Error("Y target without corresponding slate node");if(!i.yTarget){if(!t.Text.isText(i.slateTarget))throw new Error("Mismatch node type between y target and slate node");var s={};return i.targetDelta.forEach(function(t){t.attributes&&Object.keys(t.attributes).forEach(function(t){s[t]=null})}),i.yParent.format(i.textRange.start,i.textRange.end-i.textRange.start,a({},s,r.properties))}if(t.Text.isText(i.slateTarget))throw new Error("Mismatch node type between y target and slate node");var u=b(i.yTarget,i.slateTarget,[r.position]),l=i.slateTarget.children.slice(0,r.position).reduce(function(t,e){return t+w(e)},0),d=i.slateTarget.children.reduce(function(t,e){return t+w(e)},0),h=f(c(i.yTarget),l,d-l),p=O(h),g=k(e,i.yTarget,h,l),v=new o.XmlText;v.applyDelta(p,{sanitize:!1}),Object.entries(r.properties).forEach(function(t){v.setAttribute(t[0],t[1])}),i.yTarget.delete(u.textRange.start,i.yTarget.length-u.textRange.start),i.yParent.insertEmbed(i.textRange.end,v),F(e,v,g,p,0,l)}},X=a({},{insert_text:function(e,n,r){var o=b(e,n,r.path),a=o.yParent,i=o.textRange,s=t.Node.get(n,r.path);if(!t.Text.isText(s))throw new Error("Cannot insert text into non-text node");a.insert(i.start+r.offset,r.text,g(s))},remove_text:function(t,e,n){var r=b(t,e,n.path);r.yParent.delete(r.textRange.start+n.offset,n.text.length)}},Y,{set_selection:function(){}}),I=Symbol("slate-yjs-operation"),N=Symbol("slate-yjs-position-storage"),U=new WeakMap,z=new WeakMap,W=new WeakSet,B={isYjsEditor:function(e){return t.Editor.isEditor(e)&&e.sharedRoot instanceof o.XmlText&&void 0!==e.localOrigin&&void 0!==e.positionStorageOrigin&&"function"==typeof e.applyRemoteEvents&&"function"==typeof e.storeLocalOperation&&"function"==typeof e.flushLocalChanges&&"function"==typeof e.connect&&"function"==typeof e.disconnect},localChanges:function(t){var e;return null!=(e=z.get(t))?e:[]},applyRemoteEvents:function(t,e,n){t.applyRemoteEvents(e,n)},storeLocalOperation:function(t,e){t.storeLocalOperation(e)},flushLocalChanges:function(t){t.flushLocalChanges()},connected:function(t){return W.has(t)},connect:function(t){t.connect()},disconnect:function(t){t.disconnect()},remoteOrigin:function(t){return U.get(t)},asRemote:function(t,e,n){var r=B.remoteOrigin(t);U.set(t,e),n(),void 0===r?U.delete(t):U.set(t,r)},storePosition:function(t,e,n){var r=t.sharedRoot,o=t.positionStorageOrigin;S(r);var a=j(r,t,n);r.doc.transact(function(){C(r,e,a)},o)},removeStoredPosition:function(t,e){var n=t.sharedRoot,r=t.positionStorageOrigin;S(n),n.doc.transact(function(){!function(t,e){t.removeAttribute("__slateYjsStoredPosition_"+e)}(n,e)},r)},position:function(t,e){var n=function(t,e){var n=t.getAttribute("__slateYjsStoredPosition_"+e);return n?o.decodeRelativePosition(n):null}(t.sharedRoot,e);if(n)return _(t.sharedRoot,t,n)},storedPositionsRelative:function(t){return Object.fromEntries(Object.entries(t.sharedRoot.getAttributes()).filter(function(t){return t[0].startsWith("__slateYjsStoredPosition_")}).map(function(t){var e=t[1];return[t[0].slice("__slateYjsStoredPosition_".length),o.createRelativePositionFromJSON(e)]}))}},q=["trackedOrigins"],H=new WeakMap,J={isYHistoryEditor:function(t){return B.isYjsEditor(t)&&t.undoManager instanceof o.UndoManager&&"function"==typeof t.undo&&"function"==typeof t.redo},canUndo:function(t){return t.undoManager.undoStack.length>0},canRedo:function(t){return t.undoManager.redoStack.length>0}},$=new WeakMap,G={isCursorEditor:function(t){return B.isYjsEditor(t)&&t.awareness instanceof n.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=$.get(t))?r:new Set;o.add(n),$.set(t,o)}},off:function(t,e,n){if("change"===e){var r=$.get(t);r&&r.delete(n)}},cursorState:function(t,e){var n;if(e===t.awareness.clientID||!B.connected(t))return null;var r=t.awareness.getStates().get(e);return r?{relativeSelection:null!=(n=r[t.selectionStateField])?n:null,data:r[t.cursorDataField],clientId:e}:null},cursorStates:function(t){return B.connected(t)?Object.fromEntries(Array.from(t.awareness.getStates().entries(),function(e){var n=e[0],r=e[1];return n!==t.awareness.clientID&&r?[n,{relativeSelection:r[t.selectionStateField],data:r[t.cursorDataField]}]:null}).filter(Array.isArray)):{}}};exports.CursorEditor=G,exports.YHistoryEditor=J,exports.YjsEditor=B,exports.relativePositionToSlatePoint=_,exports.relativeRangeToSlateRange=L,exports.slateNodesToInsertDelta=m,exports.slatePointToRelativePosition=j,exports.slateRangeToRelativeRange=M,exports.withCursors=function(t,e,n){var r=void 0===n?{}:n,a=r.cursorStateField,i=void 0===a?"selection":a,s=r.cursorDataField,c=void 0===s?"data":s,u=r.autoSend,l=void 0===u||u,f=r.data,d=t;d.awareness=e,d.cursorDataField=c,d.selectionStateField=i,d.sendCursorData=function(t){d.awareness.setLocalStateField(d.cursorDataField,t)},d.sendCursorPosition=function(t){var n=e.getLocalState(),r=null==n?void 0:n[i];if(t){var a=M(d.sharedRoot,d,t),s=a.anchor,c=a.focus;r&&o.compareRelativePositions(s,r)&&o.compareRelativePositions(c,r)||e.setLocalStateField(d.selectionStateField,{anchor:s,focus:c})}else r&&e.setLocalStateField(d.selectionStateField,null)};var h=function(t){var e=$.get(d);if(e){var n=d.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)})}},p=d.connect,g=d.disconnect;return d.connect=function(){if(p(),d.awareness.on("change",h),h({removed:[],added:Array.from(e.getStates().keys()),updated:[]}),l){f&&G.sendCursorData(d,f);var t=d.onChange;d.onChange=function(){t(),B.connected(d)&&G.sendCursorPosition(d)}}},d.disconnect=function(){d.awareness.off("change",h),h({removed:Array.from(e.getStates().keys()),added:[],updated:[]}),g()},d},exports.withYHistory=function(e,n){var r=void 0===n?{}:n,s=r.trackedOrigins,c=void 0===s?new Set([e.localOrigin]):s,u=i(r,q),l=e,f=new o.UndoManager(l.sharedRoot,a({trackedOrigins:c},u)),d=l.onChange;return l.onChange=function(){d(),H.set(l,l.selection&&M(l.sharedRoot,l,l.selection))},f.on("stack-item-added",function(t){var e=t.stackItem;e.meta.set("selection",l.selection&&M(l.sharedRoot,l,l.selection)),e.meta.has("selectionBefore")||e.meta.set("selectionBefore",H.get(l))}),f.on("stack-item-popped",function(e){var n=e.stackItem,r="undo"===e.type?f.redoStack:f.undoStack,o=r[r.length-1];o&&(o.meta.set("selection",n.meta.get("selectionBefore")),o.meta.set("selectionBefore",n.meta.get("selection")));var a=n.meta.get("selectionBefore");if(a){var i=L(l.sharedRoot,l,a);i&&t.Transforms.select(l,i)}}),l.undoManager=f,l.undo=function(){B.flushLocalChanges(l),l.undoManager.undo()},l.redo=function(){B.flushLocalChanges(l),l.undoManager.redo()},l},exports.withYjs=function(e,n,r){var a=void 0===r?{}:r,i=a.localOrigin,s=a.positionStorageOrigin,c=a.autoConnect,u=void 0===c||c,l=e;l.sharedRoot=n,l.localOrigin=null!=i?i:I,l.positionStorageOrigin=null!=s?s:N,l.applyRemoteEvents=function(e,n){t.Editor.withoutNormalizing(l,function(){B.asRemote(l,n,function(){!function(e,n,r){t.Editor.withoutNormalizing(n,function(){r.forEach(function(r){(function(e,n,r){if(r instanceof o.YTextEvent)return function(e,n,r){var a=r.target,i=r.changes,s=r.delta;if(!(a instanceof o.XmlText))throw new Error("Unexpected target node type");var c=[],u=P(e,n,a),l=t.Node.get(n,u);if(t.Text.isText(l))throw new Error("Cannot apply yTextEvent to text node");var f=Array.from(i.keys.entries());if(u.length>0&&f.length>0){var d=Object.fromEntries(f.map(function(t){var e=t[0];return[e,"delete"===t[1].action?null:a.getAttribute(e)]})),h=Object.fromEntries(f.map(function(t){var e=t[0];return[e,l[e]]}));c.push({type:"set_node",newProperties:d,properties:h,path:u})}if(s.length>0){if(t.Text.isText(l))throw new Error("Cannot apply delta to slate text");c.push.apply(c,R(l,u,s))}return c}(e,n,r);throw new Error("Unexpected Y event type")})(e,n,r).forEach(function(t){n.apply(t)})})})}(l.sharedRoot,l,e)})})};var f=function(t,e){e.origin!==l.localOrigin&&B.applyRemoteEvents(l,t,e.origin)},d=null;u&&(d=setTimeout(function(){d=null,B.connect(l)})),l.connect=function(){n.observeDeep(f);var t=y(l.sharedRoot);l.children=t.children,W.add(l),l.onChange()},l.disconnect=function(){d&&clearTimeout(d),B.flushLocalChanges(l),n.unobserveDeep(f),W.delete(l)},l.storeLocalOperation=function(t){z.set(l,[].concat(B.localChanges(l),[{op:t,doc:e.children}]))},l.flushLocalChanges=function(){S(l.sharedRoot);var t=B.localChanges(l);z.delete(l),l.sharedRoot.doc.transact(function(){t.forEach(function(t){!function(t,e,n){var r=X[n.type];if(!r)throw new Error("Unknown operation: "+n.type);r(t,e,n)}(l.sharedRoot,{children:t.doc},t.op)})},l.localOrigin)};var h=l.apply,p=l.onChange;return l.apply=function(t){B.connected(l)&&void 0===B.remoteOrigin(l)&&B.storeLocalOperation(l,t),h(t)},l.onChange=function(){B.connected(l)&&B.flushLocalChanges(l),p()},l},exports.yTextToSlateElement=y;
|
|
2
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
var __objRest = (source, exclude) => {
|
|
25
|
+
var target = {};
|
|
26
|
+
for (var prop in source)
|
|
27
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
if (source != null && __getOwnPropSymbols)
|
|
30
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
31
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
32
|
+
target[prop] = source[prop];
|
|
33
|
+
}
|
|
34
|
+
return target;
|
|
35
|
+
};
|
|
36
|
+
var __export = (target, all) => {
|
|
37
|
+
for (var name in all)
|
|
38
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
39
|
+
};
|
|
40
|
+
var __copyProps = (to, from, except, desc) => {
|
|
41
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
42
|
+
for (let key of __getOwnPropNames(from))
|
|
43
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
44
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
45
|
+
}
|
|
46
|
+
return to;
|
|
47
|
+
};
|
|
48
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
49
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
|
+
|
|
51
|
+
// src/index.ts
|
|
52
|
+
var src_exports = {};
|
|
53
|
+
__export(src_exports, {
|
|
54
|
+
CursorEditor: () => CursorEditor,
|
|
55
|
+
YHistoryEditor: () => YHistoryEditor,
|
|
56
|
+
YjsEditor: () => YjsEditor,
|
|
57
|
+
relativePositionToSlatePoint: () => relativePositionToSlatePoint,
|
|
58
|
+
relativeRangeToSlateRange: () => relativeRangeToSlateRange,
|
|
59
|
+
slateNodesToInsertDelta: () => slateNodesToInsertDelta,
|
|
60
|
+
slatePointToRelativePosition: () => slatePointToRelativePosition,
|
|
61
|
+
slateRangeToRelativeRange: () => slateRangeToRelativeRange,
|
|
62
|
+
withCursors: () => withCursors,
|
|
63
|
+
withYHistory: () => withYHistory,
|
|
64
|
+
withYjs: () => withYjs,
|
|
65
|
+
yTextToSlateElement: () => yTextToSlateElement
|
|
66
|
+
});
|
|
67
|
+
module.exports = __toCommonJS(src_exports);
|
|
68
|
+
|
|
69
|
+
// src/plugins/withYjs.ts
|
|
70
|
+
var import_slate17 = require("slate");
|
|
71
|
+
var Y8 = __toESM(require("yjs"), 1);
|
|
72
|
+
|
|
73
|
+
// src/applyToSlate/index.ts
|
|
74
|
+
var import_slate7 = require("slate");
|
|
75
|
+
var Y4 = __toESM(require("yjs"), 1);
|
|
76
|
+
|
|
77
|
+
// src/applyToSlate/textEvent.ts
|
|
78
|
+
var import_slate5 = require("slate");
|
|
79
|
+
var Y3 = __toESM(require("yjs"), 1);
|
|
80
|
+
|
|
81
|
+
// src/utils/convert.ts
|
|
82
|
+
var import_slate2 = require("slate");
|
|
83
|
+
var Y = __toESM(require("yjs"), 1);
|
|
84
|
+
|
|
85
|
+
// src/utils/object.ts
|
|
86
|
+
function isObject(o) {
|
|
87
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
88
|
+
}
|
|
89
|
+
function isPlainObject(o) {
|
|
90
|
+
if (!isObject(o)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
const ctor = o.constructor;
|
|
94
|
+
if (ctor === void 0) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
const prot = ctor.prototype;
|
|
98
|
+
if (isObject(prot) === false) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
if (prot.hasOwnProperty("isPrototypeOf") === false) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
function deepEquals(node, another) {
|
|
107
|
+
for (const key in node) {
|
|
108
|
+
const a = node[key];
|
|
109
|
+
const b = another[key];
|
|
110
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
111
|
+
if (!deepEquals(a, b)) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
} else if (Array.isArray(a) && Array.isArray(b)) {
|
|
115
|
+
if (a.length !== b.length)
|
|
116
|
+
return false;
|
|
117
|
+
for (let i = 0; i < a.length; i++) {
|
|
118
|
+
if (a[i] !== b[i]) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} else if (a !== b) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
for (const key in another) {
|
|
127
|
+
if (node[key] === void 0 && another[key] !== void 0) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
function pick(obj, ...keys) {
|
|
134
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => keys.includes(key)));
|
|
135
|
+
}
|
|
136
|
+
function omit(obj, ...keys) {
|
|
137
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
138
|
+
}
|
|
139
|
+
function omitNullEntries(obj) {
|
|
140
|
+
return Object.fromEntries(Object.entries(obj).filter(([, value]) => value !== null));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/utils/delta.ts
|
|
144
|
+
function normalizeInsertDelta(delta) {
|
|
145
|
+
var _a, _b;
|
|
146
|
+
const normalized = [];
|
|
147
|
+
for (const element of delta) {
|
|
148
|
+
if (typeof element.insert === "string" && element.insert.length === 0) {
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const prev = normalized[normalized.length - 1];
|
|
152
|
+
if (!prev || typeof prev.insert !== "string" || typeof element.insert !== "string") {
|
|
153
|
+
normalized.push(element);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const merge = prev.attributes === element.attributes || !prev.attributes === !element.attributes && deepEquals((_a = prev.attributes) != null ? _a : {}, (_b = element.attributes) != null ? _b : {});
|
|
157
|
+
if (merge) {
|
|
158
|
+
prev.insert += element.insert;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
normalized.push(element);
|
|
162
|
+
}
|
|
163
|
+
return normalized;
|
|
164
|
+
}
|
|
165
|
+
function yTextToInsertDelta(yText) {
|
|
166
|
+
return normalizeInsertDelta(yText.toDelta());
|
|
167
|
+
}
|
|
168
|
+
function getInsertLength({ insert }) {
|
|
169
|
+
return typeof insert === "string" ? insert.length : 1;
|
|
170
|
+
}
|
|
171
|
+
function getInsertDeltaLength(delta) {
|
|
172
|
+
return delta.reduce((curr, element) => curr + getInsertLength(element), 0);
|
|
173
|
+
}
|
|
174
|
+
function sliceInsertDelta(delta, start, length) {
|
|
175
|
+
if (length < 1) {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
let currentOffset = 0;
|
|
179
|
+
const sliced = [];
|
|
180
|
+
const end = start + length;
|
|
181
|
+
for (let i = 0; i < delta.length; i++) {
|
|
182
|
+
if (currentOffset >= end) {
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
const element = delta[i];
|
|
186
|
+
const elementLength = getInsertLength(element);
|
|
187
|
+
if (currentOffset + elementLength <= start) {
|
|
188
|
+
currentOffset += elementLength;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (typeof element.insert !== "string") {
|
|
192
|
+
currentOffset += elementLength;
|
|
193
|
+
sliced.push(element);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const startOffset = Math.max(0, start - currentOffset);
|
|
197
|
+
const endOffset = Math.min(elementLength, elementLength - (currentOffset + elementLength - end));
|
|
198
|
+
sliced.push(__spreadProps(__spreadValues({}, element), {
|
|
199
|
+
insert: element.insert.slice(startOffset, endOffset)
|
|
200
|
+
}));
|
|
201
|
+
currentOffset += elementLength;
|
|
202
|
+
}
|
|
203
|
+
return sliced;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/utils/slate.ts
|
|
207
|
+
var import_slate = require("slate");
|
|
208
|
+
function getProperties(node) {
|
|
209
|
+
return omit(node, import_slate.Text.isText(node) ? "text" : "children");
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/utils/convert.ts
|
|
213
|
+
function yTextToSlateElement(yText) {
|
|
214
|
+
const delta = yTextToInsertDelta(yText);
|
|
215
|
+
const children = delta.length > 0 ? delta.map(deltaInsertToSlateNode) : [{ text: "" }];
|
|
216
|
+
return __spreadProps(__spreadValues({}, yText.getAttributes()), { children });
|
|
217
|
+
}
|
|
218
|
+
function deltaInsertToSlateNode(insert) {
|
|
219
|
+
if (typeof insert.insert === "string") {
|
|
220
|
+
return __spreadProps(__spreadValues({}, insert.attributes), { text: insert.insert });
|
|
221
|
+
}
|
|
222
|
+
return yTextToSlateElement(insert.insert);
|
|
223
|
+
}
|
|
224
|
+
function slateNodesToInsertDelta(nodes) {
|
|
225
|
+
return nodes.map((node) => {
|
|
226
|
+
if (import_slate2.Text.isText(node)) {
|
|
227
|
+
return { insert: node.text, attributes: getProperties(node) };
|
|
228
|
+
}
|
|
229
|
+
return { insert: slateElementToYText(node) };
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
function slateElementToYText(_a) {
|
|
233
|
+
var _b = _a, {
|
|
234
|
+
children
|
|
235
|
+
} = _b, attributes = __objRest(_b, [
|
|
236
|
+
"children"
|
|
237
|
+
]);
|
|
238
|
+
const yElement = new Y.XmlText();
|
|
239
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
240
|
+
yElement.setAttribute(key, value);
|
|
241
|
+
});
|
|
242
|
+
yElement.applyDelta(slateNodesToInsertDelta(children), { sanitize: false });
|
|
243
|
+
return yElement;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// src/utils/location.ts
|
|
247
|
+
var import_slate4 = require("slate");
|
|
248
|
+
var Y2 = __toESM(require("yjs"), 1);
|
|
249
|
+
function getSlateNodeYLength(node) {
|
|
250
|
+
if (!node) {
|
|
251
|
+
return 0;
|
|
252
|
+
}
|
|
253
|
+
return import_slate4.Text.isText(node) ? node.text.length : 1;
|
|
254
|
+
}
|
|
255
|
+
function slatePathOffsetToYOffset(element, pathOffset) {
|
|
256
|
+
return element.children.slice(0, pathOffset).reduce((yOffset, node) => yOffset + getSlateNodeYLength(node), 0);
|
|
257
|
+
}
|
|
258
|
+
function getYTarget(yRoot, slateRoot, path) {
|
|
259
|
+
var _a;
|
|
260
|
+
if (path.length === 0) {
|
|
261
|
+
throw new Error("Path has to a have a length >= 1");
|
|
262
|
+
}
|
|
263
|
+
if (import_slate4.Text.isText(slateRoot)) {
|
|
264
|
+
throw new Error("Cannot descent into slate text");
|
|
265
|
+
}
|
|
266
|
+
const [pathOffset, ...childPath] = path;
|
|
267
|
+
const yOffset = slatePathOffsetToYOffset(slateRoot, pathOffset);
|
|
268
|
+
const targetNode = slateRoot.children[pathOffset];
|
|
269
|
+
const delta = yTextToInsertDelta(yRoot);
|
|
270
|
+
const targetLength = getSlateNodeYLength(targetNode);
|
|
271
|
+
const targetDelta = sliceInsertDelta(delta, yOffset, targetLength);
|
|
272
|
+
if (targetDelta.length > 1) {
|
|
273
|
+
throw new Error("Path doesn't match yText, yTarget spans multiple nodes");
|
|
274
|
+
}
|
|
275
|
+
const yTarget = (_a = targetDelta[0]) == null ? void 0 : _a.insert;
|
|
276
|
+
if (childPath.length > 0) {
|
|
277
|
+
if (!(yTarget instanceof Y2.XmlText)) {
|
|
278
|
+
throw new Error("Path doesn't match yText, cannot descent into non-yText");
|
|
279
|
+
}
|
|
280
|
+
return getYTarget(yTarget, targetNode, childPath);
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
yParent: yRoot,
|
|
284
|
+
textRange: { start: yOffset, end: yOffset + targetLength },
|
|
285
|
+
yTarget: yTarget instanceof Y2.XmlText ? yTarget : void 0,
|
|
286
|
+
slateParent: slateRoot,
|
|
287
|
+
slateTarget: targetNode,
|
|
288
|
+
targetDelta
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
function yOffsetToSlateOffsets(parent, yOffset, opts = {}) {
|
|
292
|
+
const { assoc = 0, insert = false } = opts;
|
|
293
|
+
let currentOffset = 0;
|
|
294
|
+
let lastNonEmptyPathOffset = 0;
|
|
295
|
+
for (let pathOffset = 0; pathOffset < parent.children.length; pathOffset++) {
|
|
296
|
+
const child2 = parent.children[pathOffset];
|
|
297
|
+
const nodeLength = import_slate4.Text.isText(child2) ? child2.text.length : 1;
|
|
298
|
+
if (nodeLength > 0) {
|
|
299
|
+
lastNonEmptyPathOffset = pathOffset;
|
|
300
|
+
}
|
|
301
|
+
const endOffset = currentOffset + nodeLength;
|
|
302
|
+
if (nodeLength > 0 && (assoc >= 0 ? endOffset > yOffset : endOffset >= yOffset)) {
|
|
303
|
+
return [pathOffset, yOffset - currentOffset];
|
|
304
|
+
}
|
|
305
|
+
currentOffset += nodeLength;
|
|
306
|
+
}
|
|
307
|
+
if (yOffset > currentOffset + (insert ? 1 : 0)) {
|
|
308
|
+
throw new Error("yOffset out of bounds");
|
|
309
|
+
}
|
|
310
|
+
if (insert) {
|
|
311
|
+
return [parent.children.length, 0];
|
|
312
|
+
}
|
|
313
|
+
const child = parent.children[lastNonEmptyPathOffset];
|
|
314
|
+
const textOffset = import_slate4.Text.isText(child) ? child.text.length : 1;
|
|
315
|
+
return [lastNonEmptyPathOffset, textOffset];
|
|
316
|
+
}
|
|
317
|
+
function getSlatePath(sharedRoot, slateRoot, yText) {
|
|
318
|
+
const yNodePath = [yText];
|
|
319
|
+
while (yNodePath[0] !== sharedRoot) {
|
|
320
|
+
const { parent: yParent } = yNodePath[0];
|
|
321
|
+
if (!yParent) {
|
|
322
|
+
throw new Error("yText isn't a descendant of root element");
|
|
323
|
+
}
|
|
324
|
+
if (!(yParent instanceof Y2.XmlText)) {
|
|
325
|
+
throw new Error("Unexpected y parent type");
|
|
326
|
+
}
|
|
327
|
+
yNodePath.unshift(yParent);
|
|
328
|
+
}
|
|
329
|
+
if (yNodePath.length < 2) {
|
|
330
|
+
return [];
|
|
331
|
+
}
|
|
332
|
+
let slateParent = slateRoot;
|
|
333
|
+
return yNodePath.reduce((path, yParent, idx) => {
|
|
334
|
+
const yChild = yNodePath[idx + 1];
|
|
335
|
+
if (!yChild) {
|
|
336
|
+
return path;
|
|
337
|
+
}
|
|
338
|
+
let yOffset = 0;
|
|
339
|
+
const currentDelta = yTextToInsertDelta(yParent);
|
|
340
|
+
for (const element of currentDelta) {
|
|
341
|
+
if (element.insert === yChild) {
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
yOffset += typeof element.insert === "string" ? element.insert.length : 1;
|
|
345
|
+
}
|
|
346
|
+
if (import_slate4.Text.isText(slateParent)) {
|
|
347
|
+
throw new Error("Cannot descent into slate text");
|
|
348
|
+
}
|
|
349
|
+
const [pathOffset] = yOffsetToSlateOffsets(slateParent, yOffset);
|
|
350
|
+
slateParent = slateParent.children[pathOffset];
|
|
351
|
+
return path.concat(pathOffset);
|
|
352
|
+
}, []);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/applyToSlate/textEvent.ts
|
|
356
|
+
function applyDelta(node, slatePath, delta) {
|
|
357
|
+
const ops = [];
|
|
358
|
+
let yOffset = delta.reduce((length, change) => {
|
|
359
|
+
if ("retain" in change) {
|
|
360
|
+
return length + change.retain;
|
|
361
|
+
}
|
|
362
|
+
if ("delete" in change) {
|
|
363
|
+
return length + change.delete;
|
|
364
|
+
}
|
|
365
|
+
return length;
|
|
366
|
+
}, 0);
|
|
367
|
+
delta.reverse().forEach((change) => {
|
|
368
|
+
var _a;
|
|
369
|
+
if ("attributes" in change && "retain" in change) {
|
|
370
|
+
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(node, yOffset - change.retain);
|
|
371
|
+
const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(node, yOffset, { assoc: -1 });
|
|
372
|
+
for (let pathOffset = endPathOffset; pathOffset >= startPathOffset; pathOffset--) {
|
|
373
|
+
const child = node.children[pathOffset];
|
|
374
|
+
const childPath = [...slatePath, pathOffset];
|
|
375
|
+
const newProperties = change.attributes;
|
|
376
|
+
const properties = pick(node, ...Object.keys(change.attributes));
|
|
377
|
+
if (import_slate5.Text.isText(child) && (pathOffset === startPathOffset || pathOffset === endPathOffset)) {
|
|
378
|
+
const start = pathOffset === startPathOffset ? startTextOffset : 0;
|
|
379
|
+
const end = pathOffset === endPathOffset ? endTextOffset : child.text.length;
|
|
380
|
+
if (end !== child.text.length) {
|
|
381
|
+
ops.push({
|
|
382
|
+
type: "split_node",
|
|
383
|
+
path: childPath,
|
|
384
|
+
position: end,
|
|
385
|
+
properties: getProperties(child)
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
if (start !== 0) {
|
|
389
|
+
ops.push({
|
|
390
|
+
type: "split_node",
|
|
391
|
+
path: childPath,
|
|
392
|
+
position: start,
|
|
393
|
+
properties: omitNullEntries(__spreadValues(__spreadValues({}, getProperties(child)), newProperties))
|
|
394
|
+
});
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
ops.push({
|
|
399
|
+
type: "set_node",
|
|
400
|
+
newProperties,
|
|
401
|
+
path: childPath,
|
|
402
|
+
properties
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if ("retain" in change) {
|
|
407
|
+
yOffset -= change.retain;
|
|
408
|
+
}
|
|
409
|
+
if ("delete" in change) {
|
|
410
|
+
const [startPathOffset, startTextOffset] = yOffsetToSlateOffsets(node, yOffset - change.delete);
|
|
411
|
+
const [endPathOffset, endTextOffset] = yOffsetToSlateOffsets(node, yOffset, { assoc: -1 });
|
|
412
|
+
for (let pathOffset = endTextOffset === 0 ? endPathOffset - 1 : endPathOffset; pathOffset >= startPathOffset; pathOffset--) {
|
|
413
|
+
const child = node.children[pathOffset];
|
|
414
|
+
const childPath = [...slatePath, pathOffset];
|
|
415
|
+
if (import_slate5.Text.isText(child) && (pathOffset === startPathOffset || pathOffset === endPathOffset)) {
|
|
416
|
+
const start = pathOffset === startPathOffset ? startTextOffset : 0;
|
|
417
|
+
const end = pathOffset === endPathOffset ? endTextOffset : child.text.length;
|
|
418
|
+
ops.push({
|
|
419
|
+
type: "remove_text",
|
|
420
|
+
offset: start,
|
|
421
|
+
text: child.text.slice(start, end),
|
|
422
|
+
path: childPath
|
|
423
|
+
});
|
|
424
|
+
yOffset -= end - start;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
ops.push({
|
|
428
|
+
type: "remove_node",
|
|
429
|
+
node: child,
|
|
430
|
+
path: childPath
|
|
431
|
+
});
|
|
432
|
+
yOffset -= getSlateNodeYLength(child);
|
|
433
|
+
}
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if ("insert" in change) {
|
|
437
|
+
const [pathOffset, textOffset] = yOffsetToSlateOffsets(node, yOffset, {
|
|
438
|
+
insert: true
|
|
439
|
+
});
|
|
440
|
+
const child = node.children[pathOffset];
|
|
441
|
+
const childPath = [...slatePath, pathOffset];
|
|
442
|
+
if (import_slate5.Text.isText(child)) {
|
|
443
|
+
if (typeof change.insert === "string" && deepEquals((_a = change.attributes) != null ? _a : {}, getProperties(child))) {
|
|
444
|
+
return ops.push({
|
|
445
|
+
type: "insert_text",
|
|
446
|
+
offset: textOffset,
|
|
447
|
+
text: change.insert,
|
|
448
|
+
path: childPath
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
const toInsert = deltaInsertToSlateNode(change);
|
|
452
|
+
if (textOffset === 0) {
|
|
453
|
+
return ops.push({
|
|
454
|
+
type: "insert_node",
|
|
455
|
+
path: childPath,
|
|
456
|
+
node: toInsert
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
if (textOffset < child.text.length) {
|
|
460
|
+
ops.push({
|
|
461
|
+
type: "split_node",
|
|
462
|
+
path: childPath,
|
|
463
|
+
position: textOffset,
|
|
464
|
+
properties: getProperties(child)
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
return ops.push({
|
|
468
|
+
type: "insert_node",
|
|
469
|
+
path: import_slate5.Path.next(childPath),
|
|
470
|
+
node: toInsert
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
return ops.push({
|
|
474
|
+
type: "insert_node",
|
|
475
|
+
path: childPath,
|
|
476
|
+
node: deltaInsertToSlateNode(change)
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return ops;
|
|
481
|
+
}
|
|
482
|
+
function translateYTextEvent(sharedRoot, editor, event) {
|
|
483
|
+
const { target, changes } = event;
|
|
484
|
+
const delta = event.delta;
|
|
485
|
+
if (!(target instanceof Y3.XmlText)) {
|
|
486
|
+
throw new Error("Unexpected target node type");
|
|
487
|
+
}
|
|
488
|
+
const ops = [];
|
|
489
|
+
const slatePath = getSlatePath(sharedRoot, editor, target);
|
|
490
|
+
const targetElement = import_slate5.Node.get(editor, slatePath);
|
|
491
|
+
if (import_slate5.Text.isText(targetElement)) {
|
|
492
|
+
throw new Error("Cannot apply yTextEvent to text node");
|
|
493
|
+
}
|
|
494
|
+
const keyChanges = Array.from(changes.keys.entries());
|
|
495
|
+
if (slatePath.length > 0 && keyChanges.length > 0) {
|
|
496
|
+
const newProperties = Object.fromEntries(keyChanges.map(([key, info]) => [
|
|
497
|
+
key,
|
|
498
|
+
info.action === "delete" ? null : target.getAttribute(key)
|
|
499
|
+
]));
|
|
500
|
+
const properties = Object.fromEntries(keyChanges.map(([key]) => [key, targetElement[key]]));
|
|
501
|
+
ops.push({ type: "set_node", newProperties, properties, path: slatePath });
|
|
502
|
+
}
|
|
503
|
+
if (delta.length > 0) {
|
|
504
|
+
if (import_slate5.Text.isText(targetElement)) {
|
|
505
|
+
throw new Error("Cannot apply delta to slate text");
|
|
506
|
+
}
|
|
507
|
+
ops.push(...applyDelta(targetElement, slatePath, delta));
|
|
508
|
+
}
|
|
509
|
+
return ops;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// src/applyToSlate/index.ts
|
|
513
|
+
function translateYjsEvent(sharedRoot, editor, event) {
|
|
514
|
+
if (event instanceof Y4.YTextEvent) {
|
|
515
|
+
return translateYTextEvent(sharedRoot, editor, event);
|
|
516
|
+
}
|
|
517
|
+
throw new Error("Unexpected Y event type");
|
|
518
|
+
}
|
|
519
|
+
function applyYjsEvents(sharedRoot, editor, events) {
|
|
520
|
+
import_slate7.Editor.withoutNormalizing(editor, () => {
|
|
521
|
+
events.forEach((event) => {
|
|
522
|
+
translateYjsEvent(sharedRoot, editor, event).forEach((op) => {
|
|
523
|
+
editor.apply(op);
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// src/applyToYjs/node/insertNode.ts
|
|
530
|
+
var import_slate8 = require("slate");
|
|
531
|
+
function insertNode(sharedRoot, slateRoot, op) {
|
|
532
|
+
const { yParent, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
533
|
+
if (import_slate8.Text.isText(op.node)) {
|
|
534
|
+
return yParent.insert(textRange.start, op.node.text, getProperties(op.node));
|
|
535
|
+
}
|
|
536
|
+
yParent.insertEmbed(textRange.start, slateElementToYText(op.node));
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// src/applyToYjs/node/mergeNode.ts
|
|
540
|
+
var import_slate11 = require("slate");
|
|
541
|
+
|
|
542
|
+
// src/utils/clone.ts
|
|
543
|
+
var Y5 = __toESM(require("yjs"), 1);
|
|
544
|
+
function cloneInsertDeltaDeep(delta) {
|
|
545
|
+
return delta.map((element) => {
|
|
546
|
+
if (typeof element.insert === "string") {
|
|
547
|
+
return element;
|
|
548
|
+
}
|
|
549
|
+
return __spreadProps(__spreadValues({}, element), { insert: cloneDeep(element.insert) });
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
function cloneDeep(yText) {
|
|
553
|
+
const clone = new Y5.XmlText();
|
|
554
|
+
const attributes = yText.getAttributes();
|
|
555
|
+
Object.entries(attributes).forEach(([key, value]) => {
|
|
556
|
+
clone.setAttribute(key, value);
|
|
557
|
+
});
|
|
558
|
+
clone.applyDelta(cloneInsertDeltaDeep(yTextToInsertDelta(yText)), {
|
|
559
|
+
sanitize: false
|
|
560
|
+
});
|
|
561
|
+
return clone;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// src/utils/position.ts
|
|
565
|
+
var import_slate10 = require("slate");
|
|
566
|
+
var Y6 = __toESM(require("yjs"), 1);
|
|
567
|
+
|
|
568
|
+
// src/utils/yjs.ts
|
|
569
|
+
function assertDocumentAttachment(sharedType) {
|
|
570
|
+
if (!sharedType.doc) {
|
|
571
|
+
throw new Error("shared type isn't attached to a document");
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// src/utils/position.ts
|
|
576
|
+
var STORED_POSITION_PREFIX = "__slateYjsStoredPosition_";
|
|
577
|
+
function slatePointToRelativePosition(sharedRoot, slateRoot, point) {
|
|
578
|
+
const { yTarget, yParent, textRange } = getYTarget(sharedRoot, slateRoot, point.path);
|
|
579
|
+
if (yTarget) {
|
|
580
|
+
throw new Error("Slate point points to a non-text element inside sharedRoot");
|
|
581
|
+
}
|
|
582
|
+
return Y6.createRelativePositionFromTypeIndex(yParent, textRange.start + point.offset, point.offset === textRange.end ? -1 : 0);
|
|
583
|
+
}
|
|
584
|
+
function absolutePositionToSlatePoint(sharedRoot, slateRoot, { type, index, assoc }) {
|
|
585
|
+
if (!(type instanceof Y6.XmlText)) {
|
|
586
|
+
throw new Error("Absolute position points to a non-XMLText");
|
|
587
|
+
}
|
|
588
|
+
const parentPath = getSlatePath(sharedRoot, slateRoot, type);
|
|
589
|
+
const parent = import_slate10.Node.get(slateRoot, parentPath);
|
|
590
|
+
if (import_slate10.Text.isText(parent)) {
|
|
591
|
+
throw new Error("Absolute position doesn't match slateRoot, cannot descent into text");
|
|
592
|
+
}
|
|
593
|
+
const [pathOffset, textOffset] = yOffsetToSlateOffsets(parent, index, {
|
|
594
|
+
assoc
|
|
595
|
+
});
|
|
596
|
+
const target = parent.children[pathOffset];
|
|
597
|
+
if (!import_slate10.Text.isText(target)) {
|
|
598
|
+
return null;
|
|
599
|
+
}
|
|
600
|
+
return { path: [...parentPath, pathOffset], offset: textOffset };
|
|
601
|
+
}
|
|
602
|
+
function relativePositionToSlatePoint(sharedRoot, slateRoot, pos) {
|
|
603
|
+
if (!sharedRoot.doc) {
|
|
604
|
+
throw new Error("sharedRoot isn't attach to a yDoc");
|
|
605
|
+
}
|
|
606
|
+
const absPos = Y6.createAbsolutePositionFromRelativePosition(pos, sharedRoot.doc);
|
|
607
|
+
return absPos && absolutePositionToSlatePoint(sharedRoot, slateRoot, absPos);
|
|
608
|
+
}
|
|
609
|
+
function getStoredPosition(sharedRoot, key) {
|
|
610
|
+
const rawPosition = sharedRoot.getAttribute(STORED_POSITION_PREFIX + key);
|
|
611
|
+
if (!rawPosition) {
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
return Y6.decodeRelativePosition(rawPosition);
|
|
615
|
+
}
|
|
616
|
+
function getStoredPositions(sharedRoot) {
|
|
617
|
+
return Object.fromEntries(Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(([key, position]) => [
|
|
618
|
+
key.slice(STORED_POSITION_PREFIX.length),
|
|
619
|
+
Y6.createRelativePositionFromJSON(position)
|
|
620
|
+
]));
|
|
621
|
+
}
|
|
622
|
+
function getStoredPositionsAbsolute(sharedRoot) {
|
|
623
|
+
assertDocumentAttachment(sharedRoot);
|
|
624
|
+
return Object.fromEntries(Object.entries(sharedRoot.getAttributes()).filter(([key]) => key.startsWith(STORED_POSITION_PREFIX)).map(([key, position]) => [
|
|
625
|
+
key.slice(STORED_POSITION_PREFIX.length),
|
|
626
|
+
Y6.createAbsolutePositionFromRelativePosition(Y6.createRelativePositionFromJSON(position), sharedRoot.doc)
|
|
627
|
+
]).filter(([, position]) => position));
|
|
628
|
+
}
|
|
629
|
+
function removeStoredPosition(sharedRoot, key) {
|
|
630
|
+
sharedRoot.removeAttribute(STORED_POSITION_PREFIX + key);
|
|
631
|
+
}
|
|
632
|
+
function setStoredPosition(sharedRoot, key, position) {
|
|
633
|
+
sharedRoot.setAttribute(STORED_POSITION_PREFIX + key, Y6.encodeRelativePosition(position));
|
|
634
|
+
}
|
|
635
|
+
function getAbsolutePositionsInTextRange(absolutePositions, yTarget, textRange) {
|
|
636
|
+
return Object.fromEntries(Object.entries(absolutePositions).filter(([, position]) => {
|
|
637
|
+
if (position.type !== yTarget) {
|
|
638
|
+
return false;
|
|
639
|
+
}
|
|
640
|
+
if (!textRange) {
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
return position.assoc >= 0 ? position.index >= textRange.start && position.index < textRange.end : position.index > textRange.start && position.index >= textRange.end;
|
|
644
|
+
}));
|
|
645
|
+
}
|
|
646
|
+
function getAbsolutePositionsInYText(absolutePositions, yText, parentPath = "") {
|
|
647
|
+
const positions = {
|
|
648
|
+
[parentPath]: getAbsolutePositionsInTextRange(absolutePositions, yText)
|
|
649
|
+
};
|
|
650
|
+
const insertDelta = yTextToInsertDelta(yText);
|
|
651
|
+
insertDelta.forEach(({ insert }, i) => {
|
|
652
|
+
if (insert instanceof Y6.XmlText) {
|
|
653
|
+
Object.assign(positions, getAbsolutePositionsInYText(absolutePositions, insert, parentPath ? `${parentPath}.${i}` : i.toString()));
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
return positions;
|
|
657
|
+
}
|
|
658
|
+
function getStoredPositionsInDeltaAbsolute(sharedRoot, yText, delta, deltaOffset = 0) {
|
|
659
|
+
const absolutePositions = getStoredPositionsAbsolute(sharedRoot);
|
|
660
|
+
const positions = {
|
|
661
|
+
"": getAbsolutePositionsInTextRange(absolutePositions, yText, {
|
|
662
|
+
start: deltaOffset,
|
|
663
|
+
end: deltaOffset + getInsertDeltaLength(delta)
|
|
664
|
+
})
|
|
665
|
+
};
|
|
666
|
+
delta.forEach(({ insert }, i) => {
|
|
667
|
+
if (insert instanceof Y6.XmlText) {
|
|
668
|
+
Object.assign(positions, getAbsolutePositionsInYText(absolutePositions, insert, i.toString()));
|
|
669
|
+
}
|
|
670
|
+
});
|
|
671
|
+
return positions;
|
|
672
|
+
}
|
|
673
|
+
function restoreStoredPositionsWithDeltaAbsolute(sharedRoot, yText, absolutePositions, delta, newDeltaOffset = 0, previousDeltaOffset = 0, path = "") {
|
|
674
|
+
const toRestore = absolutePositions[path];
|
|
675
|
+
if (toRestore) {
|
|
676
|
+
Object.entries(toRestore).forEach(([key, position]) => {
|
|
677
|
+
setStoredPosition(sharedRoot, key, Y6.createRelativePositionFromTypeIndex(yText, position.index - previousDeltaOffset + newDeltaOffset, position.assoc));
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
delta.forEach(({ insert }, i) => {
|
|
681
|
+
if (insert instanceof Y6.XmlText) {
|
|
682
|
+
restoreStoredPositionsWithDeltaAbsolute(sharedRoot, insert, absolutePositions, yTextToInsertDelta(insert), 0, 0, path ? `${path}.${i}` : i.toString());
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
function slateRangeToRelativeRange(sharedRoot, slateRoot, range) {
|
|
687
|
+
return {
|
|
688
|
+
anchor: slatePointToRelativePosition(sharedRoot, slateRoot, range.anchor),
|
|
689
|
+
focus: slatePointToRelativePosition(sharedRoot, slateRoot, range.focus)
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
function relativeRangeToSlateRange(sharedRoot, slateRoot, range) {
|
|
693
|
+
const anchor = relativePositionToSlatePoint(sharedRoot, slateRoot, range.anchor);
|
|
694
|
+
if (!anchor) {
|
|
695
|
+
return null;
|
|
696
|
+
}
|
|
697
|
+
const focus = relativePositionToSlatePoint(sharedRoot, slateRoot, range.focus);
|
|
698
|
+
if (!focus) {
|
|
699
|
+
return null;
|
|
700
|
+
}
|
|
701
|
+
return { anchor, focus };
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
// src/applyToYjs/node/mergeNode.ts
|
|
705
|
+
function mergeNode(sharedRoot, slateRoot, op) {
|
|
706
|
+
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
707
|
+
const prev = getYTarget(target.yParent, target.slateParent, import_slate11.Path.previous(op.path.slice(-1)));
|
|
708
|
+
if (!target.yTarget !== !prev.yTarget) {
|
|
709
|
+
throw new Error("Cannot merge y text with y element");
|
|
710
|
+
}
|
|
711
|
+
if (!prev.yTarget || !target.yTarget) {
|
|
712
|
+
const { yParent: parent, textRange } = target;
|
|
713
|
+
const previousSibling = import_slate11.Node.get(slateRoot, import_slate11.Path.previous(op.path));
|
|
714
|
+
if (!import_slate11.Text.isText(previousSibling)) {
|
|
715
|
+
throw new Error("Path points to a y text but not a slate node");
|
|
716
|
+
}
|
|
717
|
+
return parent.format(textRange.start, textRange.start - textRange.end, getProperties(previousSibling));
|
|
718
|
+
}
|
|
719
|
+
const deltaApplyYOffset = prev.yTarget.length;
|
|
720
|
+
const targetDelta = yTextToInsertDelta(target.yTarget);
|
|
721
|
+
const clonedDelta = cloneInsertDeltaDeep(targetDelta);
|
|
722
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, targetDelta, deltaApplyYOffset);
|
|
723
|
+
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...clonedDelta];
|
|
724
|
+
prev.yTarget.applyDelta(applyDelta2, {
|
|
725
|
+
sanitize: false
|
|
726
|
+
});
|
|
727
|
+
target.yParent.delete(target.textRange.start, target.textRange.end - target.textRange.start);
|
|
728
|
+
restoreStoredPositionsWithDeltaAbsolute(sharedRoot, prev.yTarget, storedPositions, clonedDelta, deltaApplyYOffset);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// src/applyToYjs/node/moveNode.ts
|
|
732
|
+
var import_slate13 = require("slate");
|
|
733
|
+
function moveNode(sharedRoot, slateRoot, op) {
|
|
734
|
+
const newParentPath = import_slate13.Path.parent(op.newPath);
|
|
735
|
+
const newPathOffset = op.newPath[op.newPath.length - 1];
|
|
736
|
+
const parent = import_slate13.Node.get(slateRoot, newParentPath);
|
|
737
|
+
if (import_slate13.Text.isText(parent)) {
|
|
738
|
+
throw new Error("Cannot move slate node into text element");
|
|
739
|
+
}
|
|
740
|
+
const normalizedNewPath = [
|
|
741
|
+
...newParentPath,
|
|
742
|
+
Math.min(newPathOffset, parent.children.length)
|
|
743
|
+
];
|
|
744
|
+
const origin = getYTarget(sharedRoot, slateRoot, op.path);
|
|
745
|
+
const target = getYTarget(sharedRoot, slateRoot, normalizedNewPath);
|
|
746
|
+
const insertDelta = cloneInsertDeltaDeep(origin.targetDelta);
|
|
747
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, origin.yParent, origin.targetDelta);
|
|
748
|
+
origin.yParent.delete(origin.textRange.start, origin.textRange.end - origin.textRange.start);
|
|
749
|
+
const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent));
|
|
750
|
+
const deltaApplyYOffset = Math.min(target.textRange.start, targetLength);
|
|
751
|
+
const applyDelta2 = [{ retain: deltaApplyYOffset }, ...insertDelta];
|
|
752
|
+
target.yParent.applyDelta(applyDelta2, { sanitize: false });
|
|
753
|
+
restoreStoredPositionsWithDeltaAbsolute(sharedRoot, target.yParent, storedPositions, insertDelta, deltaApplyYOffset, origin.textRange.start);
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
// src/applyToYjs/node/removeNode.ts
|
|
757
|
+
function removeNode(sharedRoot, slateRoot, op) {
|
|
758
|
+
const { yParent: parent, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
759
|
+
parent.delete(textRange.start, textRange.end - textRange.start);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
// src/applyToYjs/node/setNode.ts
|
|
763
|
+
function setNode(sharedRoot, slateRoot, op) {
|
|
764
|
+
const { yTarget, textRange, yParent } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
765
|
+
if (yTarget) {
|
|
766
|
+
Object.entries(op.newProperties).forEach(([key, value]) => {
|
|
767
|
+
if (value === null) {
|
|
768
|
+
return yTarget.removeAttribute(key);
|
|
769
|
+
}
|
|
770
|
+
yTarget.setAttribute(key, value);
|
|
771
|
+
});
|
|
772
|
+
return Object.entries(op.properties).forEach(([key]) => {
|
|
773
|
+
if (!op.newProperties.hasOwnProperty(key)) {
|
|
774
|
+
yTarget.removeAttribute(key);
|
|
775
|
+
}
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
const unset = Object.fromEntries(Object.keys(op.properties).map((key) => [key, null]));
|
|
779
|
+
const newProperties = __spreadValues(__spreadValues({}, unset), op.newProperties);
|
|
780
|
+
yParent.format(textRange.start, textRange.end - textRange.start, newProperties);
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
// src/applyToYjs/node/splitNode.ts
|
|
784
|
+
var import_slate14 = require("slate");
|
|
785
|
+
var Y7 = __toESM(require("yjs"), 1);
|
|
786
|
+
function splitNode(sharedRoot, slateRoot, op) {
|
|
787
|
+
const target = getYTarget(sharedRoot, slateRoot, op.path);
|
|
788
|
+
if (!target.slateTarget) {
|
|
789
|
+
throw new Error("Y target without corresponding slate node");
|
|
790
|
+
}
|
|
791
|
+
if (!target.yTarget) {
|
|
792
|
+
if (!import_slate14.Text.isText(target.slateTarget)) {
|
|
793
|
+
throw new Error("Mismatch node type between y target and slate node");
|
|
794
|
+
}
|
|
795
|
+
const unset = {};
|
|
796
|
+
target.targetDelta.forEach((element) => {
|
|
797
|
+
if (element.attributes) {
|
|
798
|
+
Object.keys(element.attributes).forEach((key) => {
|
|
799
|
+
unset[key] = null;
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
});
|
|
803
|
+
return target.yParent.format(target.textRange.start, target.textRange.end - target.textRange.start, __spreadValues(__spreadValues({}, unset), op.properties));
|
|
804
|
+
}
|
|
805
|
+
if (import_slate14.Text.isText(target.slateTarget)) {
|
|
806
|
+
throw new Error("Mismatch node type between y target and slate node");
|
|
807
|
+
}
|
|
808
|
+
const splitTarget = getYTarget(target.yTarget, target.slateTarget, [
|
|
809
|
+
op.position
|
|
810
|
+
]);
|
|
811
|
+
const ySplitOffset = target.slateTarget.children.slice(0, op.position).reduce((length2, child) => length2 + getSlateNodeYLength(child), 0);
|
|
812
|
+
const length = target.slateTarget.children.reduce((current, child) => current + getSlateNodeYLength(child), 0);
|
|
813
|
+
const splitDelta = sliceInsertDelta(yTextToInsertDelta(target.yTarget), ySplitOffset, length - ySplitOffset);
|
|
814
|
+
const clonedDelta = cloneInsertDeltaDeep(splitDelta);
|
|
815
|
+
const storedPositions = getStoredPositionsInDeltaAbsolute(sharedRoot, target.yTarget, splitDelta, ySplitOffset);
|
|
816
|
+
const toInsert = new Y7.XmlText();
|
|
817
|
+
toInsert.applyDelta(clonedDelta, {
|
|
818
|
+
sanitize: false
|
|
819
|
+
});
|
|
820
|
+
Object.entries(op.properties).forEach(([key, value]) => {
|
|
821
|
+
toInsert.setAttribute(key, value);
|
|
822
|
+
});
|
|
823
|
+
target.yTarget.delete(splitTarget.textRange.start, target.yTarget.length - splitTarget.textRange.start);
|
|
824
|
+
target.yParent.insertEmbed(target.textRange.end, toInsert);
|
|
825
|
+
restoreStoredPositionsWithDeltaAbsolute(sharedRoot, toInsert, storedPositions, clonedDelta, 0, ySplitOffset);
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// src/applyToYjs/node/index.ts
|
|
829
|
+
var NODE_MAPPER = {
|
|
830
|
+
insert_node: insertNode,
|
|
831
|
+
remove_node: removeNode,
|
|
832
|
+
set_node: setNode,
|
|
833
|
+
merge_node: mergeNode,
|
|
834
|
+
move_node: moveNode,
|
|
835
|
+
split_node: splitNode
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
// src/applyToYjs/text/insertText.ts
|
|
839
|
+
var import_slate15 = require("slate");
|
|
840
|
+
function insertText(sharedRoot, slateRoot, op) {
|
|
841
|
+
const { yParent: target, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
842
|
+
const targetNode = import_slate15.Node.get(slateRoot, op.path);
|
|
843
|
+
if (!import_slate15.Text.isText(targetNode)) {
|
|
844
|
+
throw new Error("Cannot insert text into non-text node");
|
|
845
|
+
}
|
|
846
|
+
target.insert(textRange.start + op.offset, op.text, getProperties(targetNode));
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
// src/applyToYjs/text/removeText.ts
|
|
850
|
+
function removeText(sharedRoot, slateRoot, op) {
|
|
851
|
+
const { yParent: target, textRange } = getYTarget(sharedRoot, slateRoot, op.path);
|
|
852
|
+
target.delete(textRange.start + op.offset, op.text.length);
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
// src/applyToYjs/text/index.ts
|
|
856
|
+
var TEXT_MAPPER = {
|
|
857
|
+
insert_text: insertText,
|
|
858
|
+
remove_text: removeText
|
|
859
|
+
};
|
|
860
|
+
|
|
861
|
+
// src/applyToYjs/index.ts
|
|
862
|
+
var NOOP = () => {
|
|
863
|
+
};
|
|
864
|
+
var opMappers = __spreadProps(__spreadValues(__spreadValues({}, TEXT_MAPPER), NODE_MAPPER), {
|
|
865
|
+
set_selection: NOOP
|
|
866
|
+
});
|
|
867
|
+
function applySlateOp(sharedRoot, slateRoot, op) {
|
|
868
|
+
const apply = opMappers[op.type];
|
|
869
|
+
if (!apply) {
|
|
870
|
+
throw new Error(`Unknown operation: ${op.type}`);
|
|
871
|
+
}
|
|
872
|
+
apply(sharedRoot, slateRoot, op);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// src/plugins/withYjs.ts
|
|
876
|
+
var DEFAULT_LOCAL_ORIGIN = Symbol("slate-yjs-operation");
|
|
877
|
+
var DEFAULT_POSITION_STORAGE_ORIGIN = Symbol("slate-yjs-position-storage");
|
|
878
|
+
var ORIGIN = /* @__PURE__ */ new WeakMap();
|
|
879
|
+
var LOCAL_CHANGES = /* @__PURE__ */ new WeakMap();
|
|
880
|
+
var CONNECTED = /* @__PURE__ */ new WeakSet();
|
|
881
|
+
var YjsEditor = {
|
|
882
|
+
isYjsEditor(value) {
|
|
883
|
+
return import_slate17.Editor.isEditor(value) && value.sharedRoot instanceof Y8.XmlText && "localOrigin" in value && "positionStorageOrigin" in value && typeof value.applyRemoteEvents === "function" && typeof value.storeLocalChange === "function" && typeof value.flushLocalChanges === "function" && typeof value.isLocalOrigin === "function" && typeof value.connect === "function" && typeof value.disconnect === "function";
|
|
884
|
+
},
|
|
885
|
+
localChanges(editor) {
|
|
886
|
+
var _a;
|
|
887
|
+
return (_a = LOCAL_CHANGES.get(editor)) != null ? _a : [];
|
|
888
|
+
},
|
|
889
|
+
applyRemoteEvents(editor, events, origin) {
|
|
890
|
+
editor.applyRemoteEvents(events, origin);
|
|
891
|
+
},
|
|
892
|
+
storeLocalChange(editor, op) {
|
|
893
|
+
editor.storeLocalChange(op);
|
|
894
|
+
},
|
|
895
|
+
flushLocalChanges(editor) {
|
|
896
|
+
editor.flushLocalChanges();
|
|
897
|
+
},
|
|
898
|
+
connected(editor) {
|
|
899
|
+
return CONNECTED.has(editor);
|
|
900
|
+
},
|
|
901
|
+
connect(editor) {
|
|
902
|
+
if (YjsEditor.connected(editor)) {
|
|
903
|
+
throw new Error("already connected");
|
|
904
|
+
}
|
|
905
|
+
editor.connect();
|
|
906
|
+
},
|
|
907
|
+
disconnect(editor) {
|
|
908
|
+
editor.disconnect();
|
|
909
|
+
},
|
|
910
|
+
isLocal(editor) {
|
|
911
|
+
return editor.isLocalOrigin(YjsEditor.origin(editor));
|
|
912
|
+
},
|
|
913
|
+
origin(editor) {
|
|
914
|
+
const origin = ORIGIN.get(editor);
|
|
915
|
+
return origin !== void 0 ? origin : editor.localOrigin;
|
|
916
|
+
},
|
|
917
|
+
withOrigin(editor, origin, fn) {
|
|
918
|
+
const prev = YjsEditor.origin(editor);
|
|
919
|
+
ORIGIN.set(editor, origin);
|
|
920
|
+
fn();
|
|
921
|
+
ORIGIN.set(editor, prev);
|
|
922
|
+
},
|
|
923
|
+
storePosition(editor, key, point) {
|
|
924
|
+
const { sharedRoot, positionStorageOrigin: locationStorageOrigin } = editor;
|
|
925
|
+
assertDocumentAttachment(sharedRoot);
|
|
926
|
+
const position = slatePointToRelativePosition(sharedRoot, editor, point);
|
|
927
|
+
sharedRoot.doc.transact(() => {
|
|
928
|
+
setStoredPosition(sharedRoot, key, position);
|
|
929
|
+
}, locationStorageOrigin);
|
|
930
|
+
},
|
|
931
|
+
removeStoredPosition(editor, key) {
|
|
932
|
+
const { sharedRoot, positionStorageOrigin: locationStorageOrigin } = editor;
|
|
933
|
+
assertDocumentAttachment(sharedRoot);
|
|
934
|
+
sharedRoot.doc.transact(() => {
|
|
935
|
+
removeStoredPosition(sharedRoot, key);
|
|
936
|
+
}, locationStorageOrigin);
|
|
937
|
+
},
|
|
938
|
+
position(editor, key) {
|
|
939
|
+
const position = getStoredPosition(editor.sharedRoot, key);
|
|
940
|
+
if (!position) {
|
|
941
|
+
return void 0;
|
|
942
|
+
}
|
|
943
|
+
return relativePositionToSlatePoint(editor.sharedRoot, editor, position);
|
|
944
|
+
},
|
|
945
|
+
storedPositionsRelative(editor) {
|
|
946
|
+
return getStoredPositions(editor.sharedRoot);
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
function withYjs(editor, sharedRoot, {
|
|
950
|
+
localOrigin,
|
|
951
|
+
positionStorageOrigin,
|
|
952
|
+
autoConnect = true
|
|
953
|
+
} = {}) {
|
|
954
|
+
const e = editor;
|
|
955
|
+
e.sharedRoot = sharedRoot;
|
|
956
|
+
e.localOrigin = localOrigin != null ? localOrigin : DEFAULT_LOCAL_ORIGIN;
|
|
957
|
+
e.positionStorageOrigin = positionStorageOrigin != null ? positionStorageOrigin : DEFAULT_POSITION_STORAGE_ORIGIN;
|
|
958
|
+
e.applyRemoteEvents = (events, origin) => {
|
|
959
|
+
YjsEditor.flushLocalChanges(e);
|
|
960
|
+
import_slate17.Editor.withoutNormalizing(e, () => {
|
|
961
|
+
YjsEditor.withOrigin(e, origin, () => {
|
|
962
|
+
applyYjsEvents(e.sharedRoot, e, events);
|
|
963
|
+
});
|
|
964
|
+
});
|
|
965
|
+
};
|
|
966
|
+
e.isLocalOrigin = (origin) => origin === e.localOrigin;
|
|
967
|
+
const handleYEvents = (events, transaction) => {
|
|
968
|
+
if (e.isLocalOrigin(transaction.origin)) {
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
YjsEditor.applyRemoteEvents(e, events, transaction.origin);
|
|
972
|
+
};
|
|
973
|
+
let autoConnectTimeoutId = null;
|
|
974
|
+
if (autoConnect) {
|
|
975
|
+
autoConnectTimeoutId = setTimeout(() => {
|
|
976
|
+
autoConnectTimeoutId = null;
|
|
977
|
+
YjsEditor.connect(e);
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
e.connect = () => {
|
|
981
|
+
sharedRoot.observeDeep(handleYEvents);
|
|
982
|
+
const content = yTextToSlateElement(e.sharedRoot);
|
|
983
|
+
e.children = content.children;
|
|
984
|
+
CONNECTED.add(e);
|
|
985
|
+
e.onChange();
|
|
986
|
+
};
|
|
987
|
+
e.disconnect = () => {
|
|
988
|
+
if (autoConnectTimeoutId) {
|
|
989
|
+
clearTimeout(autoConnectTimeoutId);
|
|
990
|
+
}
|
|
991
|
+
YjsEditor.flushLocalChanges(e);
|
|
992
|
+
sharedRoot.unobserveDeep(handleYEvents);
|
|
993
|
+
CONNECTED.delete(e);
|
|
994
|
+
};
|
|
995
|
+
e.storeLocalChange = (op) => {
|
|
996
|
+
LOCAL_CHANGES.set(e, [
|
|
997
|
+
...YjsEditor.localChanges(e),
|
|
998
|
+
{ op, doc: editor.children, origin: YjsEditor.origin(e) }
|
|
999
|
+
]);
|
|
1000
|
+
};
|
|
1001
|
+
e.flushLocalChanges = () => {
|
|
1002
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
1003
|
+
const localChanges = YjsEditor.localChanges(e);
|
|
1004
|
+
LOCAL_CHANGES.delete(e);
|
|
1005
|
+
const txGroups = [];
|
|
1006
|
+
localChanges.forEach((change) => {
|
|
1007
|
+
const currentGroup = txGroups[txGroups.length - 1];
|
|
1008
|
+
if (currentGroup && currentGroup[0].origin === change.origin) {
|
|
1009
|
+
return currentGroup.push(change);
|
|
1010
|
+
}
|
|
1011
|
+
txGroups.push([change]);
|
|
1012
|
+
});
|
|
1013
|
+
txGroups.forEach((txGroup) => {
|
|
1014
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
1015
|
+
e.sharedRoot.doc.transact(() => {
|
|
1016
|
+
txGroup.forEach((change) => {
|
|
1017
|
+
assertDocumentAttachment(e.sharedRoot);
|
|
1018
|
+
applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
|
|
1019
|
+
});
|
|
1020
|
+
}, txGroup[0].origin);
|
|
1021
|
+
});
|
|
1022
|
+
};
|
|
1023
|
+
const { apply, onChange } = e;
|
|
1024
|
+
e.apply = (op) => {
|
|
1025
|
+
if (YjsEditor.connected(e) && YjsEditor.isLocal(e)) {
|
|
1026
|
+
YjsEditor.storeLocalChange(e, op);
|
|
1027
|
+
}
|
|
1028
|
+
apply(op);
|
|
1029
|
+
};
|
|
1030
|
+
e.onChange = () => {
|
|
1031
|
+
if (YjsEditor.connected(e)) {
|
|
1032
|
+
YjsEditor.flushLocalChanges(e);
|
|
1033
|
+
}
|
|
1034
|
+
onChange();
|
|
1035
|
+
};
|
|
1036
|
+
return e;
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// src/plugins/withYHistory.ts
|
|
1040
|
+
var import_slate18 = require("slate");
|
|
1041
|
+
var Y9 = __toESM(require("yjs"), 1);
|
|
1042
|
+
var LAST_SELECTION = /* @__PURE__ */ new WeakMap();
|
|
1043
|
+
var DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol("slate-yjs-history-without-saving");
|
|
1044
|
+
var YHistoryEditor = {
|
|
1045
|
+
isYHistoryEditor(value) {
|
|
1046
|
+
return YjsEditor.isYjsEditor(value) && value.undoManager instanceof Y9.UndoManager && typeof value.undo === "function" && typeof value.redo === "function" && "withoutSavingOrigin" in value;
|
|
1047
|
+
},
|
|
1048
|
+
canUndo(editor) {
|
|
1049
|
+
return editor.undoManager.undoStack.length > 0;
|
|
1050
|
+
},
|
|
1051
|
+
canRedo(editor) {
|
|
1052
|
+
return editor.undoManager.redoStack.length > 0;
|
|
1053
|
+
},
|
|
1054
|
+
isSaving(editor) {
|
|
1055
|
+
return editor.undoManager.trackedOrigins.has(YjsEditor.origin(editor));
|
|
1056
|
+
},
|
|
1057
|
+
withoutSaving(editor, fn) {
|
|
1058
|
+
YjsEditor.withOrigin(editor, editor.withoutSavingOrigin, fn);
|
|
1059
|
+
}
|
|
1060
|
+
};
|
|
1061
|
+
function withYHistory(editor, _a = {}) {
|
|
1062
|
+
var _b = _a, {
|
|
1063
|
+
withoutSavingOrigin = DEFAULT_WITHOUT_SAVING_ORIGIN,
|
|
1064
|
+
trackedOrigins = /* @__PURE__ */ new Set([editor.localOrigin])
|
|
1065
|
+
} = _b, options = __objRest(_b, [
|
|
1066
|
+
"withoutSavingOrigin",
|
|
1067
|
+
"trackedOrigins"
|
|
1068
|
+
]);
|
|
1069
|
+
const e = editor;
|
|
1070
|
+
const undoManager = new Y9.UndoManager(e.sharedRoot, __spreadValues({
|
|
1071
|
+
trackedOrigins
|
|
1072
|
+
}, options));
|
|
1073
|
+
e.undoManager = undoManager;
|
|
1074
|
+
e.withoutSavingOrigin = withoutSavingOrigin;
|
|
1075
|
+
const { onChange, isLocalOrigin } = e;
|
|
1076
|
+
e.onChange = () => {
|
|
1077
|
+
onChange();
|
|
1078
|
+
LAST_SELECTION.set(e, e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection));
|
|
1079
|
+
};
|
|
1080
|
+
e.isLocalOrigin = (origin) => origin === e.withoutSavingOrigin || isLocalOrigin(origin);
|
|
1081
|
+
const handleStackItemAdded = ({
|
|
1082
|
+
stackItem
|
|
1083
|
+
}) => {
|
|
1084
|
+
stackItem.meta.set("selection", e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection));
|
|
1085
|
+
stackItem.meta.set("selectionBefore", LAST_SELECTION.get(e));
|
|
1086
|
+
};
|
|
1087
|
+
const handleStackItemUpdated = ({
|
|
1088
|
+
stackItem
|
|
1089
|
+
}) => {
|
|
1090
|
+
stackItem.meta.set("selection", e.selection && slateRangeToRelativeRange(e.sharedRoot, e, e.selection));
|
|
1091
|
+
};
|
|
1092
|
+
const handleStackItemPopped = ({
|
|
1093
|
+
stackItem,
|
|
1094
|
+
type
|
|
1095
|
+
}) => {
|
|
1096
|
+
const inverseStack = type === "undo" ? undoManager.redoStack : undoManager.undoStack;
|
|
1097
|
+
const inverseItem = inverseStack[inverseStack.length - 1];
|
|
1098
|
+
if (inverseItem) {
|
|
1099
|
+
inverseItem.meta.set("selection", stackItem.meta.get("selectionBefore"));
|
|
1100
|
+
inverseItem.meta.set("selectionBefore", stackItem.meta.get("selection"));
|
|
1101
|
+
}
|
|
1102
|
+
const relativeSelection = stackItem.meta.get("selectionBefore");
|
|
1103
|
+
if (!relativeSelection) {
|
|
1104
|
+
return;
|
|
1105
|
+
}
|
|
1106
|
+
const selection = relativeRangeToSlateRange(e.sharedRoot, e, relativeSelection);
|
|
1107
|
+
if (!selection) {
|
|
1108
|
+
return;
|
|
1109
|
+
}
|
|
1110
|
+
import_slate18.Transforms.select(e, selection);
|
|
1111
|
+
};
|
|
1112
|
+
const { connect, disconnect } = e;
|
|
1113
|
+
e.connect = () => {
|
|
1114
|
+
connect();
|
|
1115
|
+
undoManager.on("stack-item-added", handleStackItemAdded);
|
|
1116
|
+
undoManager.on("stack-item-popped", handleStackItemPopped);
|
|
1117
|
+
undoManager.on("stack-item-updated", handleStackItemUpdated);
|
|
1118
|
+
};
|
|
1119
|
+
e.disconnect = () => {
|
|
1120
|
+
undoManager.off("stack-item-added", handleStackItemAdded);
|
|
1121
|
+
undoManager.off("stack-item-popped", handleStackItemPopped);
|
|
1122
|
+
undoManager.off("stack-item-updated", handleStackItemUpdated);
|
|
1123
|
+
disconnect();
|
|
1124
|
+
};
|
|
1125
|
+
e.undo = () => {
|
|
1126
|
+
if (YjsEditor.connected(e)) {
|
|
1127
|
+
YjsEditor.flushLocalChanges(e);
|
|
1128
|
+
e.undoManager.undo();
|
|
1129
|
+
}
|
|
1130
|
+
};
|
|
1131
|
+
e.redo = () => {
|
|
1132
|
+
if (YjsEditor.connected(e)) {
|
|
1133
|
+
YjsEditor.flushLocalChanges(e);
|
|
1134
|
+
e.undoManager.redo();
|
|
1135
|
+
}
|
|
1136
|
+
};
|
|
1137
|
+
return e;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// src/plugins/withCursors.ts
|
|
1141
|
+
var import_awareness = require("y-protocols/awareness");
|
|
1142
|
+
var Y10 = __toESM(require("yjs"), 1);
|
|
1143
|
+
var CURSOR_CHANGE_EVENT_LISTENERS = /* @__PURE__ */ new WeakMap();
|
|
1144
|
+
var CursorEditor = {
|
|
1145
|
+
isCursorEditor(value) {
|
|
1146
|
+
return YjsEditor.isYjsEditor(value) && value.awareness instanceof import_awareness.Awareness && typeof value.cursorDataField === "string" && typeof value.selectionStateField === "string" && typeof value.sendCursorPosition === "function" && typeof value.sendCursorData === "function";
|
|
1147
|
+
},
|
|
1148
|
+
sendCursorPosition(editor, range = editor.selection) {
|
|
1149
|
+
editor.sendCursorPosition(range);
|
|
1150
|
+
},
|
|
1151
|
+
sendCursorData(editor, data) {
|
|
1152
|
+
editor.sendCursorData(data);
|
|
1153
|
+
},
|
|
1154
|
+
on(editor, event, handler) {
|
|
1155
|
+
var _a;
|
|
1156
|
+
if (event !== "change") {
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
const listeners = (_a = CURSOR_CHANGE_EVENT_LISTENERS.get(editor)) != null ? _a : /* @__PURE__ */ new Set();
|
|
1160
|
+
listeners.add(handler);
|
|
1161
|
+
CURSOR_CHANGE_EVENT_LISTENERS.set(editor, listeners);
|
|
1162
|
+
},
|
|
1163
|
+
off(editor, event, listener) {
|
|
1164
|
+
if (event !== "change") {
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(editor);
|
|
1168
|
+
if (listeners) {
|
|
1169
|
+
listeners.delete(listener);
|
|
1170
|
+
}
|
|
1171
|
+
},
|
|
1172
|
+
cursorState(editor, clientId) {
|
|
1173
|
+
var _a;
|
|
1174
|
+
if (clientId === editor.awareness.clientID || !YjsEditor.connected(editor)) {
|
|
1175
|
+
return null;
|
|
1176
|
+
}
|
|
1177
|
+
const state = editor.awareness.getStates().get(clientId);
|
|
1178
|
+
if (!state) {
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
return {
|
|
1182
|
+
relativeSelection: (_a = state[editor.selectionStateField]) != null ? _a : null,
|
|
1183
|
+
data: state[editor.cursorDataField],
|
|
1184
|
+
clientId
|
|
1185
|
+
};
|
|
1186
|
+
},
|
|
1187
|
+
cursorStates(editor) {
|
|
1188
|
+
if (!YjsEditor.connected(editor)) {
|
|
1189
|
+
return {};
|
|
1190
|
+
}
|
|
1191
|
+
return Object.fromEntries(Array.from(editor.awareness.getStates().entries(), ([id, state]) => {
|
|
1192
|
+
if (id === editor.awareness.clientID || !state) {
|
|
1193
|
+
return null;
|
|
1194
|
+
}
|
|
1195
|
+
return [
|
|
1196
|
+
id,
|
|
1197
|
+
{
|
|
1198
|
+
relativeSelection: state[editor.selectionStateField],
|
|
1199
|
+
data: state[editor.cursorDataField]
|
|
1200
|
+
}
|
|
1201
|
+
];
|
|
1202
|
+
}).filter(Array.isArray));
|
|
1203
|
+
}
|
|
1204
|
+
};
|
|
1205
|
+
function withCursors(editor, awareness, {
|
|
1206
|
+
cursorStateField: selectionStateField = "selection",
|
|
1207
|
+
cursorDataField = "data",
|
|
1208
|
+
autoSend = true,
|
|
1209
|
+
data
|
|
1210
|
+
} = {}) {
|
|
1211
|
+
const e = editor;
|
|
1212
|
+
e.awareness = awareness;
|
|
1213
|
+
e.cursorDataField = cursorDataField;
|
|
1214
|
+
e.selectionStateField = selectionStateField;
|
|
1215
|
+
e.sendCursorData = (cursorData) => {
|
|
1216
|
+
e.awareness.setLocalStateField(e.cursorDataField, cursorData);
|
|
1217
|
+
};
|
|
1218
|
+
e.sendCursorPosition = (range) => {
|
|
1219
|
+
const localState = awareness.getLocalState();
|
|
1220
|
+
const currentRange = localState == null ? void 0 : localState[selectionStateField];
|
|
1221
|
+
if (!range) {
|
|
1222
|
+
if (currentRange) {
|
|
1223
|
+
awareness.setLocalStateField(e.selectionStateField, null);
|
|
1224
|
+
}
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
const { anchor, focus } = slateRangeToRelativeRange(e.sharedRoot, e, range);
|
|
1228
|
+
if (!currentRange || !Y10.compareRelativePositions(anchor, currentRange) || !Y10.compareRelativePositions(focus, currentRange)) {
|
|
1229
|
+
awareness.setLocalStateField(e.selectionStateField, { anchor, focus });
|
|
1230
|
+
}
|
|
1231
|
+
};
|
|
1232
|
+
const awarenessChangeListener = (yEvent) => {
|
|
1233
|
+
const listeners = CURSOR_CHANGE_EVENT_LISTENERS.get(e);
|
|
1234
|
+
if (!listeners) {
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
const localId = e.awareness.clientID;
|
|
1238
|
+
const event = {
|
|
1239
|
+
added: yEvent.added.filter((id) => id !== localId),
|
|
1240
|
+
removed: yEvent.removed.filter((id) => id !== localId),
|
|
1241
|
+
updated: yEvent.updated.filter((id) => id !== localId)
|
|
1242
|
+
};
|
|
1243
|
+
if (event.added.length > 0 || event.removed.length > 0 || event.updated.length > 0) {
|
|
1244
|
+
listeners.forEach((listener) => listener(event));
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
const { connect, disconnect } = e;
|
|
1248
|
+
e.connect = () => {
|
|
1249
|
+
connect();
|
|
1250
|
+
e.awareness.on("change", awarenessChangeListener);
|
|
1251
|
+
awarenessChangeListener({
|
|
1252
|
+
removed: [],
|
|
1253
|
+
added: Array.from(awareness.getStates().keys()),
|
|
1254
|
+
updated: []
|
|
1255
|
+
});
|
|
1256
|
+
if (autoSend) {
|
|
1257
|
+
if (data) {
|
|
1258
|
+
CursorEditor.sendCursorData(e, data);
|
|
1259
|
+
}
|
|
1260
|
+
const { onChange } = e;
|
|
1261
|
+
e.onChange = () => {
|
|
1262
|
+
onChange();
|
|
1263
|
+
if (YjsEditor.connected(e)) {
|
|
1264
|
+
CursorEditor.sendCursorPosition(e);
|
|
1265
|
+
}
|
|
1266
|
+
};
|
|
1267
|
+
}
|
|
1268
|
+
};
|
|
1269
|
+
e.disconnect = () => {
|
|
1270
|
+
e.awareness.off("change", awarenessChangeListener);
|
|
1271
|
+
awarenessChangeListener({
|
|
1272
|
+
removed: Array.from(awareness.getStates().keys()),
|
|
1273
|
+
added: [],
|
|
1274
|
+
updated: []
|
|
1275
|
+
});
|
|
1276
|
+
disconnect();
|
|
1277
|
+
};
|
|
1278
|
+
return e;
|
|
1279
|
+
}
|
|
1280
|
+
//# sourceMappingURL=index.cjs.map
|