@slate-yjs/core 0.0.2 → 0.1.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.
- package/dist/applyToSlate/Untitled-1.d.ts +101 -0
- package/dist/applyToSlate/Untitled-1.d.ts.map +1 -0
- package/dist/applyToSlate/textEvent.d.ts.map +1 -1
- package/dist/applyToYjs/node/splitNode.d.ts.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.modern.js +1 -1
- package/dist/index.modern.js.map +1 -1
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/plugins/withCursors.d.ts +12 -10
- package/dist/plugins/withCursors.d.ts.map +1 -1
- package/dist/plugins/withYHistory.d.ts +1 -1
- package/dist/plugins/withYHistory.d.ts.map +1 -1
- package/dist/plugins/withYjs.d.ts +5 -5
- package/dist/plugins/withYjs.d.ts.map +1 -1
- package/dist/utils/position.d.ts.map +1 -1
- package/package.json +5 -4
- package/src/applyToSlate/index.ts +3 -3
- package/src/applyToSlate/textEvent.ts +9 -8
- package/src/applyToYjs/node/splitNode.ts +1 -0
- package/src/index.ts +2 -0
- package/src/plugins/withCursors.ts +42 -48
- package/src/plugins/withYHistory.ts +7 -7
- package/src/plugins/withYjs.ts +20 -16
- package/src/utils/position.ts +3 -3
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
declare const ops: ({
|
|
2
|
+
retain: number;
|
|
3
|
+
delete?: undefined;
|
|
4
|
+
attributes?: undefined;
|
|
5
|
+
insert?: undefined;
|
|
6
|
+
} | {
|
|
7
|
+
delete: number;
|
|
8
|
+
retain?: undefined;
|
|
9
|
+
attributes?: undefined;
|
|
10
|
+
insert?: undefined;
|
|
11
|
+
} | {
|
|
12
|
+
retain: number;
|
|
13
|
+
attributes: {
|
|
14
|
+
italic: boolean;
|
|
15
|
+
};
|
|
16
|
+
delete?: undefined;
|
|
17
|
+
insert?: undefined;
|
|
18
|
+
} | {
|
|
19
|
+
insert: string;
|
|
20
|
+
retain?: undefined;
|
|
21
|
+
delete?: undefined;
|
|
22
|
+
attributes?: undefined;
|
|
23
|
+
})[];
|
|
24
|
+
declare const initial: ({
|
|
25
|
+
bold: boolean;
|
|
26
|
+
text: string;
|
|
27
|
+
} | {
|
|
28
|
+
text: string;
|
|
29
|
+
bold?: undefined;
|
|
30
|
+
})[];
|
|
31
|
+
declare const a: ({
|
|
32
|
+
bold: boolean;
|
|
33
|
+
text: string;
|
|
34
|
+
} | {
|
|
35
|
+
text: string;
|
|
36
|
+
bold?: undefined;
|
|
37
|
+
})[];
|
|
38
|
+
declare const b: ({
|
|
39
|
+
bold: boolean;
|
|
40
|
+
text: string;
|
|
41
|
+
italic?: undefined;
|
|
42
|
+
test?: undefined;
|
|
43
|
+
} | {
|
|
44
|
+
text: string;
|
|
45
|
+
italic: boolean;
|
|
46
|
+
bold: boolean;
|
|
47
|
+
test?: undefined;
|
|
48
|
+
} | {
|
|
49
|
+
test: string;
|
|
50
|
+
bold?: undefined;
|
|
51
|
+
text?: undefined;
|
|
52
|
+
italic?: undefined;
|
|
53
|
+
} | {
|
|
54
|
+
text: string;
|
|
55
|
+
bold?: undefined;
|
|
56
|
+
italic?: undefined;
|
|
57
|
+
test?: undefined;
|
|
58
|
+
})[];
|
|
59
|
+
declare const c: ({
|
|
60
|
+
bold: boolean;
|
|
61
|
+
text: string;
|
|
62
|
+
italic?: undefined;
|
|
63
|
+
test?: undefined;
|
|
64
|
+
} | {
|
|
65
|
+
text: string;
|
|
66
|
+
italic: boolean;
|
|
67
|
+
bold: boolean;
|
|
68
|
+
test?: undefined;
|
|
69
|
+
} | {
|
|
70
|
+
test: string;
|
|
71
|
+
bold?: undefined;
|
|
72
|
+
text?: undefined;
|
|
73
|
+
italic?: undefined;
|
|
74
|
+
} | {
|
|
75
|
+
text: string;
|
|
76
|
+
bold?: undefined;
|
|
77
|
+
italic?: undefined;
|
|
78
|
+
test?: undefined;
|
|
79
|
+
})[];
|
|
80
|
+
declare const e: ({
|
|
81
|
+
bold: boolean;
|
|
82
|
+
text: string;
|
|
83
|
+
italic?: undefined;
|
|
84
|
+
test?: undefined;
|
|
85
|
+
} | {
|
|
86
|
+
text: string;
|
|
87
|
+
italic: boolean;
|
|
88
|
+
bold: boolean;
|
|
89
|
+
test?: undefined;
|
|
90
|
+
} | {
|
|
91
|
+
text: string;
|
|
92
|
+
bold?: undefined;
|
|
93
|
+
italic?: undefined;
|
|
94
|
+
test?: undefined;
|
|
95
|
+
} | {
|
|
96
|
+
test: string;
|
|
97
|
+
bold?: undefined;
|
|
98
|
+
text?: undefined;
|
|
99
|
+
italic?: undefined;
|
|
100
|
+
})[];
|
|
101
|
+
//# sourceMappingURL=Untitled-1.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Untitled-1.d.ts","sourceRoot":"","sources":["../../src/applyToSlate/Untitled-1.js"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;KAME;AAEF;;;;;;KAA8E;AAE9E;;;;;;KAAuE;AAEvE;;;;;;;;;;;;;;;;;;;;KAKE;AAEF;;;;;;;;;;;;;;;;;;;;KAKE;AAEF;;;;;;;;;;;;;;;;;;;;KAME"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"textEvent.d.ts","sourceRoot":"","sources":["../../src/applyToSlate/textEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAiB,SAAS,EAAc,MAAM,OAAO,CAAC;AACrE,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"textEvent.d.ts","sourceRoot":"","sources":["../../src/applyToSlate/textEvent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAiB,SAAS,EAAc,MAAM,OAAO,CAAC;AACrE,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AA2MzB,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,CAAC,CAAC,UAAU,GAClB,SAAS,EAAE,CAyCb"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"splitNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/splitNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAQ,MAAM,OAAO,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AASzB,wBAAgB,SAAS,CACvB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,kBAAkB,GACrB,IAAI,
|
|
1
|
+
{"version":3,"file":"splitNode.d.ts","sourceRoot":"","sources":["../../../src/applyToYjs/node/splitNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,kBAAkB,EAAQ,MAAM,OAAO,CAAC;AACvD,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AASzB,wBAAgB,SAAS,CACvB,UAAU,EAAE,CAAC,CAAC,OAAO,EACrB,SAAS,EAAE,IAAI,EACf,EAAE,EAAE,kBAAkB,GACrB,IAAI,CAmFN"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var t=require("slate"),e=require("yjs");function n(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}require("y-protocols/awareness");var r=/*#__PURE__*/n(e);function o(){return o=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},o.apply(this,arguments)}function i(t,e){if(null==t)return{};var n,r,o={},i=Object.keys(t);for(r=0;r<i.length;r++)e.indexOf(n=i[r])>=0||(o[n]=t[n]);return o}function a(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 s(t){return t.toDelta()}function c(t){var e=t.insert;return"string"==typeof e?e.length:1}function u(t){return t.reduce(function(t,e){return t+c(e)},0)}function l(t,e,n){if(n<1)return[];for(var r=0,i=[],a=e+n,s=0;s<t.length&&!(r>=a);s++){var u=t[s],l=c(u);if(r+l<=e)r+=l;else if("string"==typeof u.insert){var f=Math.max(0,e-r),d=Math.min(l,l-(r+l-a));i.push(o({},u,{insert:u.insert.slice(f,d)})),r+=l}else r+=l,i.push(u)}return i}function f(t){return null!==t&&"object"==typeof t}function d(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 f(r)?f(o)&&d(r,o):r===o})}function h(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 p(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 g(t){var e=s(t),n=e.length>0?e.map(y):[{text:""}];return o({},t.getAttributes(),{children:n})}function y(t){return"string"==typeof t.insert?o({},t.attributes,{text:t.insert}):g(t.insert)}function x(e){return e.map(function(e){return t.Text.isText(e)?{insert:e.text,attributes:p(e)}:{insert:m(e)}})}function m(t){var e=t.children,n=i(t,v),o=new r.XmlText;return Object.entries(n).forEach(function(t){o.setAttribute(t[0],t[1])}),o.applyDelta(x(e),{sanitize:!1}),o}function T(e){return e?t.Text.isText(e)?e.text.length:1:0}function w(e,n,o){var i;if(0===o.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 a=o[0],c=o.slice(1),u=function(t,e){return t.children.slice(0,e).reduce(function(t,e){return t+T(e)},0)}(n,a),f=n.children[a],d=s(e),h=T(f),p=l(d,u,h);if(p.length>1)throw new Error("Path doesn't match yText, yTarget spans multiple nodes");var v=null==(i=p[0])?void 0:i.insert;if(c.length>0){if(!(v instanceof r.XmlText))throw new Error("Path doesn't match yText, cannot descent into non-yText");return w(v,f,c)}return{yParent:e,textRange:{start:u,end:u+h},yTarget:v instanceof r.XmlText?v:void 0,slateParent:n,slateTarget:f,targetDelta:p}}function b(e,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<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&&(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[e.children.length,0];var p=e.children[u];return[u,t.Text.isText(p)?p.text.length:1]}function E(e,n,o){for(var i=[o];i[0]!==e;){var c=i[0].parent;if(!c)throw new Error("yText isn't a descendant of root element");if(!(c instanceof r.XmlText))throw new Error("Unexpected y parent type");i.unshift(c)}if(i.length<2)return[];var u=n;return i.reduce(function(e,n,r){var o=i[r+1];if(!o)return e;for(var c,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 a(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)?a(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.")}(s(n));!(c=f()).done;){var d=c.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=b(u,l)[0];return u=u.children[h],e.concat(h)},[])}function O(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(a){if("attributes"in a&&"retain"in a)for(var s=b(e,i-a.retain),c=s[0],u=s[1],l=b(e,i),f=l[0],v=l[1],g=0===v?f-1:f;g>=c;g--){var x=e.children[g],m=[].concat(n,[g]),w=a.attributes,E=h.apply(void 0,[e].concat(Object.keys(a.attributes)));if(t.Text.isText(x)&&(g===c||g===f)){var O=g===c?u:0,P=g===f?v:x.text.length;0!==O&&(o.push({type:"split_node",path:m,position:O,properties:w}),m=t.Path.next(m),P-=O),P!==x.text.length&&o.push({type:"split_node",path:m,position:P,properties:E})}o.push({type:"set_node",newProperties:w,path:m,properties:E})}if("retain"in a&&(i-=a.retain),"delete"in a)for(var R=b(e,i-a.delete),j=R[0],S=R[1],_=b(e,i,{assoc:-1}),C=_[0],A=_[1],k=0===A?C-1:C;k>=j;k--){var D=e.children[k],M=[].concat(n,[k]);if(!t.Text.isText(D)||k!==j&&k!==C)o.push({type:"remove_node",node:D,path:M}),i-=T(D);else{var F=k===j?S:0,L=k===C?A:D.text.length;o.push({type:"remove_text",offset:F,text:D.text.slice(F,L),path:M}),i-=L-F}}else if("insert"in a){var Y=b(e,i,{insert:!0}),X=Y[0],I=Y[1],N=e.children[X],U=[].concat(n,[X]);if(t.Text.isText(N)){var z;if("string"==typeof a.insert&&d(null!=(z=a.attributes)?z:{},p(N)))return o.push({type:"insert_text",offset:I,text:a.insert,path:U});var W=y(a);return 0===I?o.push({type:"insert_node",path:U,node:W}):(I<r.length&&o.push({type:"split_node",path:U,position:I,properties:p(N)}),o.push({type:"insert_node",path:t.Path.next(U),node:W}))}return o.push({type:"insert_node",path:U,node:y(a)})}}),o}function P(t){return t.map(function(t){return"string"==typeof t.insert?t:o({},t,{insert:(e=t.insert,n=new r.XmlText,i=e.getAttributes(),Object.entries(i).forEach(function(t){n.setAttribute(t[0],t[1])}),n.applyDelta(P(s(e)),{sanitize:!1}),n)});var e,n,i})}function R(t){if(!t.doc)throw new Error("shared type isn't attached to a document")}function j(t,e,n){var o=w(t,e,n.path),i=o.yParent,a=o.textRange;if(o.yTarget)throw new Error("Slate point points to a non-text element inside sharedRoot");return r.createRelativePositionFromTypeIndex(i,a.start+n.offset,n.offset===a.end?-1:0)}function S(e,n,o){if(!e.doc)throw new Error("sharedRoot isn't attach to a yDoc");var i=r.createAbsolutePositionFromRelativePosition(o,e.doc);return i&&function(e,n,o){var i=o.type,a=o.index,s=o.assoc;if(!(i instanceof r.XmlText))throw new Error("Absolute position points to a non-XMLText");var c=E(e,n,i),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=b(u,a,{assoc:s}),f=l[0],d=l[1];return t.Text.isText(u.children[f])?{path:[].concat(c,[f]),offset:d}:null}(e,n,i)}function _(t,e,n){t.setAttribute("__slateYjsStoredPosition_"+e,r.encodeRelativePosition(n))}function C(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 A(t,e,n){var o;void 0===n&&(n="");var i=((o={})[n]=C(t,e),o);return s(e).forEach(function(e,o){var a=e.insert;a instanceof r.XmlText&&Object.assign(i,A(t,a,n?n+"."+o:o.toString()))}),i}function k(t,e,n,o){void 0===o&&(o=0);var i=function(t){return R(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),r.createAbsolutePositionFromRelativePosition(r.createRelativePositionFromJSON(n),t.doc)]}).filter(function(t){return t[1]}))}(t),a={"":C(i,e,{start:o,end:o+u(n)})};return n.forEach(function(t,e){var n=t.insert;n instanceof r.XmlText&&Object.assign(a,A(i,n,e.toString()))}),a}function D(t,e,n,o,i,a,c){void 0===i&&(i=0),void 0===a&&(a=0),void 0===c&&(c="");var u=n[c];u&&Object.entries(u).forEach(function(n){var o=n[1];_(t,n[0],r.createRelativePositionFromTypeIndex(e,o.index-a+i,o.assoc))}),o.forEach(function(e,o){var i=e.insert;i instanceof r.XmlText&&D(t,i,n,s(i),0,0,c?c+"."+o:o.toString())})}function M(t,e,n){return{anchor:j(t,e,n.anchor),focus:j(t,e,n.focus)}}function F(t,e,n){var r=S(t,e,n.anchor);if(!r)return null;var o=S(t,e,n.focus);return o?{anchor:r,focus:o}:null}var L={insert_node:function(e,n,r){var o=w(e,n,r.path),i=o.yParent,a=o.textRange;if(t.Text.isText(r.node))return i.insert(a.start,r.node.text,p(r.node));i.insertEmbed(a.start,m(r.node))},remove_node:function(t,e,n){var r=w(t,e,n.path),o=r.textRange;r.yParent.delete(o.start,o.end-o.start)},set_node:function(t,e,n){var r=w(t,e,n.path),i=r.yTarget,a=r.textRange,s=r.yParent;if(i)return Object.entries(n.newProperties).forEach(function(t){var e=t[0],n=t[1];if(null===n)return i.removeAttribute(e);i.setAttribute(e,n)}),Object.entries(n.properties).forEach(function(t){var e=t[0];n.newProperties.hasOwnProperty(e)||i.removeAttribute(e)});var c=o({},Object.fromEntries(Object.keys(n.properties).map(function(t){return[t,null]})),n.newProperties);s.format(a.start,a.end-a.start,c)},merge_node:function(e,n,r){var o=w(e,n,r.path),i=w(o.yParent,o.slateParent,t.Path.previous(r.path.slice(-1)));if(!o.yTarget!=!i.yTarget)throw new Error("Cannot merge y text with y element");if(!i.yTarget||!o.yTarget){var a=o.yParent,c=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 a.format(c.start,c.start-c.end,p(u))}var l=i.yTarget.length,f=s(o.yTarget),d=P(f),h=k(e,o.yTarget,f,l),v=[{retain:l}].concat(d);i.yTarget.applyDelta(v,{sanitize:!1}),o.yParent.delete(o.textRange.start,o.textRange.end-o.textRange.start),D(e,i.yTarget,h,d,l)},move_node:function(e,n,r){var o=t.Path.parent(r.newPath),i=r.newPath[r.newPath.length-1],a=t.Node.get(n,o);if(t.Text.isText(a))throw new Error("Cannot move slate node into text element");var c=[].concat(o,[Math.min(i,a.children.length)]),l=w(e,n,r.path),f=w(e,n,c),d=P(l.targetDelta),h=k(e,l.yParent,l.targetDelta);l.yParent.delete(l.textRange.start,l.textRange.end-l.textRange.start);var p=u(s(f.yParent)),v=Math.min(f.textRange.start,p),g=[{retain:v}].concat(d);f.yParent.applyDelta(g,{sanitize:!1}),D(e,f.yParent,h,d,v,l.textRange.start)},split_node:function(e,n,i){var a=w(e,n,i.path);if(!a.slateTarget)throw new Error("Y target without corresponding slate node");if(!a.yTarget){if(!t.Text.isText(a.slateTarget))throw new Error("Mismatch node type between y target and slate node");var c={};return a.targetDelta.forEach(function(t){t.attributes&&Object.keys(t.attributes).forEach(function(t){c[t]=null})}),a.yParent.format(a.textRange.start,a.textRange.end-a.textRange.start,o({},c,i.properties))}if(t.Text.isText(a.slateTarget))throw new Error("Mismatch node type between y target and slate node");var u=w(a.yTarget,a.slateTarget,[i.position]),f=a.slateTarget.children.slice(0,i.position).reduce(function(t,e){return t+T(e)},0),d=a.slateTarget.children.reduce(function(t,e){return t+T(e)},0),h=l(s(a.yTarget),f,d-f),p=P(h),v=k(e,a.yTarget,h,f),g=new r.XmlText;g.applyDelta(p,{sanitize:!1}),Object.entries(i.properties).forEach(function(t){g.setAttribute(t[0],t[1])}),a.yTarget.delete(u.textRange.start,a.yTarget.length-u.textRange.start),a.yParent.insertEmbed(a.textRange.end,g),D(e,g,v,p,0,f)}},Y=o({},{insert_text:function(e,n,r){var o=w(e,n,r.path),i=o.yParent,a=o.textRange,s=t.Node.get(n,r.path);if(!t.Text.isText(s))throw new Error("Cannot insert text into non-text node");i.insert(a.start+r.offset,r.text,p(s))},remove_text:function(t,e,n){var r=w(t,e,n.path);r.yParent.delete(r.textRange.start+n.offset,n.text.length)}},L,{set_selection:function(){}}),X=Symbol("slate-yjs-operation"),I=Symbol("slate-yjs-position-storage"),N=new WeakMap,U=new WeakMap,z=new WeakSet,W={isYjsEditor:function(e){return t.Editor.isEditor(e)&&e.sharedRoot instanceof r.XmlText&&void 0!==e.localOrigin&&void 0!==e.positionStorageOrigin&&"function"==typeof e.applyRemoteEvents&&"function"==typeof e.storeLocalOperation&&"function"==typeof e.flushLocalOperations&&"function"==typeof e.connect&&"function"==typeof e.disconnect},localChanges:function(t){var e;return null!=(e=U.get(t))?e:[]},applyRemoveEvents:function(t,e,n){t.applyRemoteEvents(e,n)},storeLocalOperation:function(t,e){t.storeLocalOperation(e)},flushLocalOperations:function(t){t.flushLocalOperations()},connected:function(t){return z.has(t)},connect:function(t){t.connect()},disconnect:function(t){t.disconnect()},remoteOrigin:function(t){return N.get(t)},asRemote:function(t,e,n){var r=W.remoteOrigin(t);N.set(t,e),n(),void 0===r?N.delete(t):N.set(t,r)},storePosition:function(t,e,n){var r=t.sharedRoot,o=t.positionStorageOrigin;R(r);var i=j(r,t,n);r.doc.transact(function(){_(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("__slateYjsStoredPosition_"+e)}(n,e)},r)},position:function(t,e){var n=function(t,e){var n=t.getAttribute("__slateYjsStoredPosition_"+e);return n?r.decodeRelativePosition(n):null}(t.sharedRoot,e);return n?S(t.sharedRoot,t,n):null},storedRelativePositions: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),r.createRelativePositionFromJSON(e)]}))}},B=["trackedOrigins"],q=new WeakMap,H={isYHistoryEditor:function(t){return W.isYjsEditor(t)&&t.undoManager instanceof r.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}},J=new WeakMap,$={isCursorEditor:function(t){return!0},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=J.get(t))?r:new Set;o.add(n),J.set(t,o)}},off:function(t,e,n){if("change"===e){var r=J.get(t);r&&r.delete(n)}},remoteCursor:function(t,e){if(e===t.awareness.clientID||!W.connected(t))return null;var n=t.awareness.getStates().get(e);if(!n)return null;var r=n[t.cursorStateField];return{selection:r?F(t.sharedRoot,t,r):null,data:n[t.cursorDataField]}},remoteCursors:function(t){return W.connected(t)?Object.fromEntries(Array.from(t.awareness.getStates().entries(),function(e){var n=e[0],r=e[1];if(n===t.awareness.clientID)return null;var o=r[t.cursorStateField];return[n,{selection:o?F(t.sharedRoot,t,o):null,data:r[t.cursorDataField]}]}).filter(Array.isArray)):{}}};exports.CursorEditor=$,exports.YHistoryEditor=H,exports.YjsEditor=W,exports.relativePositionToSlatePoint=S,exports.relativeRangeToSlateRange=F,exports.slateNodesToInsertDelta=x,exports.slatePointToRelativePosition=j,exports.slateRangeToRelativeRange=M,exports.withCursors=function(t,e,n){var o=void 0===n?{}:n,i=o.cursorStateField,a=void 0===i?"selection":i,s=o.cursorDataField,c=void 0===s?"data":s,u=o.autoSend,l=void 0===u||u,f=o.data,d=t;d.awareness=e,d.cursorDataField=c,d.cursorStateField=a,d.sendCursorData=function(t){d.awareness.setLocalStateField(d.cursorDataField,t)},d.sendCursorPosition=function(t){var n=e.getLocalState(),o=null==n?void 0:n[a];if(t){var i=M(d.sharedRoot,d,t),s=i.anchor,c=i.focus;o&&r.compareRelativePositions(s,o)&&r.compareRelativePositions(c,o)||e.setLocalStateField(d.cursorStateField,{anchor:s,focus:c})}else o&&e.setLocalStateField(d.cursorStateField,null)};var h=function(t){var e=J.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,v=d.disconnect;return d.connect=function(){if(p(),d.awareness.on("change",h),h({removed:[],added:Array.from(e.getStates().keys()),updated:[]}),l){f&&$.sendCursorData(d,f);var t=d.onChange;d.onChange=function(){t(),W.connected(d)&&$.sendCursorPosition(d)}}},d.disconnect=function(){d.awareness.off("change",h),h({removed:Array.from(e.getStates().keys()),added:[],updated:[]}),v()},d},exports.withYHistory=function(e,n){var a=void 0===n?{}:n,s=a.trackedOrigins,c=void 0===s?new Set([e.localOrigin]):s,u=i(a,B),l=e,f=new r.UndoManager(l.sharedRoot,o({trackedOrigins:c},u)),d=l.onChange;return l.onChange=function(){d(),q.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",q.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 i=n.meta.get("selectionBefore");if(i){var a=F(l.sharedRoot,l,i);a&&t.Transforms.select(l,a)}}),l.undoManager=f,l.undo=function(){W.flushLocalOperations(l),l.undoManager.undo()},l.redo=function(){W.flushLocalOperations(l),l.undoManager.redo()},l},exports.withYjs=function(e,n,o){var i=void 0===o?{}:o,a=i.localOrigin,s=i.positionStorageOrigin,c=i.autoConnect,u=void 0===c||c,l=e;l.sharedRoot=n,l.localOrigin=null!=a?a:X,l.positionStorageOrigin=null!=s?s:I,l.applyRemoteEvents=function(e,n){t.Editor.withoutNormalizing(l,function(){W.asRemote(l,n,function(){!function(e,n,o){t.Editor.withoutNormalizing(n,function(){o.forEach(function(o){(function(e,n,o){if(o instanceof r.YTextEvent)return function(e,n,o){var i=o.target,a=o.changes,s=o.delta;if(!(i instanceof r.XmlText))throw new Error("Unexpected target node type");var c=[],u=E(e,n,i),l=t.Node.get(n,u);if(t.Text.isText(l))throw new Error("Cannot apply yTextEvent to text node");var f=Array.from(a.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:i.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(i))throw new Error("Cannot apply delta to slate text");c.push.apply(c,O(l,u,s))}return c}(e,n,o);throw new Error("Unexpected Y event type")})(e,n,o).forEach(function(t){return n.apply(t)})})})}(l.sharedRoot,l,e)})})};var f=function(t,e){e.origin!==l.localOrigin&&W.applyRemoveEvents(l,t,e.origin)},d=null;u&&(d=setTimeout(function(){d=null,W.connect(l)})),l.connect=function(){n.observeDeep(f);var t=g(l.sharedRoot);l.children=t.children,z.add(l),l.onChange()},l.disconnect=function(){d&&clearTimeout(d),n.unobserveDeep(f),z.delete(l)},l.storeLocalOperation=function(t){U.set(l,[].concat(W.localChanges(l),[{op:t,doc:e.children}]))},l.flushLocalOperations=function(){var t=W.localChanges(l);U.delete(l),R(l.sharedRoot),l.sharedRoot.doc.transact(function(){t.forEach(function(t){!function(t,e,n){var r=Y[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){void 0===W.remoteOrigin(l)&&W.storeLocalOperation(l,t),h(t)},l.onChange=function(){W.flushLocalOperations(l),p()},l},exports.yTextToSlateElement=g;
|
|
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
2
|
//# sourceMappingURL=index.cjs.map
|