@wangeditor-next/yjs 6.4.2 → 6.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +124 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -15
- package/dist/yjs/src/applyToSlate/index.d.ts +0 -19
- package/dist/yjs/src/applyToSlate/textEvent.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/index.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/index.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/insertNode.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/mergeNode.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/moveNode.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/removeNode.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/setNode.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/node/splitNode.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/text/index.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/text/insertText.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/text/removeText.d.ts +0 -3
- package/dist/yjs/src/applyToYjs/types.d.ts +0 -8
- package/dist/yjs/src/index.d.ts +0 -6
- package/dist/yjs/src/module/custom-types.d.ts +0 -41
- package/dist/yjs/src/plugins/index.d.ts +0 -6
- package/dist/yjs/src/plugins/withCursors.d.ts +0 -38
- package/dist/yjs/src/plugins/withYHistory.d.ts +0 -19
- package/dist/yjs/src/plugins/withYjs.d.ts +0 -42
- package/dist/yjs/src/utils/clone.d.ts +0 -4
- package/dist/yjs/src/utils/convert.d.ts +0 -7
- package/dist/yjs/src/utils/delta.d.ts +0 -7
- package/dist/yjs/src/utils/location.d.ts +0 -11
- package/dist/yjs/src/utils/object.d.ts +0 -9
- package/dist/yjs/src/utils/position.d.ts +0 -19
- package/dist/yjs/src/utils/slate.d.ts +0 -2
- package/dist/yjs/src/utils/yjs.d.ts +0 -4
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as Y from "yjs";
|
|
2
|
+
import { BaseEditor, BasePoint, BaseRange, Descendant, Editor, Element, Node, Operation, Point, Range } from "slate";
|
|
3
|
+
import { Awareness } from "y-protocols/awareness";
|
|
4
|
+
|
|
5
|
+
//#region src/module/custom-types.d.ts
|
|
6
|
+
|
|
7
|
+
type DeltaInsert = {
|
|
8
|
+
insert: string | Y.XmlText;
|
|
9
|
+
attributes?: Record<string, unknown>;
|
|
10
|
+
};
|
|
11
|
+
type InsertDelta = Array<DeltaInsert>;
|
|
12
|
+
type RelativeRange = {
|
|
13
|
+
anchor: Y.RelativePosition;
|
|
14
|
+
focus: Y.RelativePosition;
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/plugins/withYjs.d.ts
|
|
18
|
+
type LocalChange = {
|
|
19
|
+
op: Operation;
|
|
20
|
+
doc: Descendant[];
|
|
21
|
+
origin: unknown;
|
|
22
|
+
};
|
|
23
|
+
type YjsEditor = BaseEditor & {
|
|
24
|
+
sharedRoot: Y.XmlText;
|
|
25
|
+
localOrigin: unknown;
|
|
26
|
+
positionStorageOrigin: unknown;
|
|
27
|
+
applyRemoteEvents: (events: Y.YEvent<Y.XmlText>[], origin: unknown) => void;
|
|
28
|
+
storeLocalChange: (op: Operation) => void;
|
|
29
|
+
flushLocalChanges: () => void;
|
|
30
|
+
isLocalOrigin: (origin: unknown) => boolean;
|
|
31
|
+
connect: () => void;
|
|
32
|
+
disconnect: () => void;
|
|
33
|
+
};
|
|
34
|
+
declare const YjsEditor: {
|
|
35
|
+
isYjsEditor(value: unknown): value is YjsEditor;
|
|
36
|
+
localChanges(editor: YjsEditor): LocalChange[];
|
|
37
|
+
applyRemoteEvents(editor: YjsEditor, events: Y.YEvent<Y.XmlText>[], origin: unknown): void;
|
|
38
|
+
storeLocalChange(editor: YjsEditor, op: Operation): void;
|
|
39
|
+
flushLocalChanges(editor: YjsEditor): void;
|
|
40
|
+
connected(editor: YjsEditor): boolean;
|
|
41
|
+
connect(editor: YjsEditor): void;
|
|
42
|
+
disconnect(editor: YjsEditor): void;
|
|
43
|
+
isLocal(editor: YjsEditor): boolean;
|
|
44
|
+
origin(editor: YjsEditor): unknown;
|
|
45
|
+
withOrigin(editor: YjsEditor, origin: unknown, fn: () => void): void;
|
|
46
|
+
storePosition(editor: YjsEditor, key: string, point: Point): void;
|
|
47
|
+
removeStoredPosition(editor: YjsEditor, key: string): void;
|
|
48
|
+
position(editor: YjsEditor, key: string): Point | null | undefined;
|
|
49
|
+
storedPositionsRelative(editor: YjsEditor): Record<string, Y.RelativePosition>;
|
|
50
|
+
};
|
|
51
|
+
type WithYjsOptions = {
|
|
52
|
+
autoConnect?: boolean;
|
|
53
|
+
localOrigin?: unknown;
|
|
54
|
+
positionStorageOrigin?: unknown;
|
|
55
|
+
};
|
|
56
|
+
declare function withYjs(sharedRoot: Y.XmlText, options?: WithYjsOptions): <T extends Editor>(editor: T) => T & YjsEditor;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/plugins/withCursors.d.ts
|
|
59
|
+
type CursorStateChangeEvent = {
|
|
60
|
+
added: number[];
|
|
61
|
+
updated: number[];
|
|
62
|
+
removed: number[];
|
|
63
|
+
};
|
|
64
|
+
type RemoteCursorChangeEventListener = (event: CursorStateChangeEvent) => void;
|
|
65
|
+
type CursorState<TCursorData extends Record<string, unknown> = Record<string, unknown>> = {
|
|
66
|
+
relativeSelection: RelativeRange | null;
|
|
67
|
+
data?: TCursorData;
|
|
68
|
+
clientId: number;
|
|
69
|
+
};
|
|
70
|
+
type CursorEditor<TCursorData extends Record<string, unknown> = Record<string, unknown>> = YjsEditor & {
|
|
71
|
+
awareness: Awareness;
|
|
72
|
+
cursorDataField: string;
|
|
73
|
+
selectionStateField: string;
|
|
74
|
+
sendCursorPosition: (range: Range | null) => void;
|
|
75
|
+
sendCursorData: (data: TCursorData) => void;
|
|
76
|
+
};
|
|
77
|
+
declare const CursorEditor: {
|
|
78
|
+
isCursorEditor(value: unknown): value is CursorEditor;
|
|
79
|
+
sendCursorPosition<TCursorData extends Record<string, unknown>>(editor: CursorEditor<TCursorData>, range?: Range | null): void;
|
|
80
|
+
sendCursorData<TCursorData extends Record<string, unknown>>(editor: CursorEditor<TCursorData>, data: TCursorData): void;
|
|
81
|
+
on<TCursorData extends Record<string, unknown>>(editor: CursorEditor<TCursorData>, event: "change", handler: RemoteCursorChangeEventListener): void;
|
|
82
|
+
off<TCursorData extends Record<string, unknown>>(editor: CursorEditor<TCursorData>, event: "change", listener: RemoteCursorChangeEventListener): void;
|
|
83
|
+
cursorState<TCursorData extends Record<string, unknown>>(editor: CursorEditor<TCursorData>, clientId: number): CursorState<TCursorData> | null;
|
|
84
|
+
cursorStates<TCursorData extends Record<string, unknown>>(editor: CursorEditor<TCursorData>): Record<string, CursorState<TCursorData>>;
|
|
85
|
+
};
|
|
86
|
+
type WithCursorsOptions<TCursorData extends Record<string, unknown> = Record<string, unknown>> = {
|
|
87
|
+
cursorStateField?: string;
|
|
88
|
+
cursorDataField?: string;
|
|
89
|
+
data?: TCursorData;
|
|
90
|
+
autoSend?: boolean;
|
|
91
|
+
};
|
|
92
|
+
declare function withCursors<TCursorData extends Record<string, unknown>>(awareness: Awareness, options?: WithCursorsOptions<TCursorData>): <T extends YjsEditor>(editor: T) => T & CursorEditor<TCursorData>;
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/plugins/withYHistory.d.ts
|
|
95
|
+
type YHistoryEditor = YjsEditor & {
|
|
96
|
+
undoManager: Y.UndoManager;
|
|
97
|
+
withoutSavingOrigin: unknown;
|
|
98
|
+
undo: () => void;
|
|
99
|
+
redo: () => void;
|
|
100
|
+
};
|
|
101
|
+
declare const YHistoryEditor: {
|
|
102
|
+
isYHistoryEditor(value: unknown): value is YHistoryEditor;
|
|
103
|
+
canUndo(editor: YHistoryEditor): boolean;
|
|
104
|
+
canRedo(editor: YHistoryEditor): boolean;
|
|
105
|
+
isSaving(editor: YHistoryEditor): boolean;
|
|
106
|
+
withoutSaving(editor: YHistoryEditor, fn: () => void): void;
|
|
107
|
+
};
|
|
108
|
+
type WithYHistoryOptions = NonNullable<ConstructorParameters<typeof Y.UndoManager>[1]> & {
|
|
109
|
+
withoutSavingOrigin?: unknown;
|
|
110
|
+
};
|
|
111
|
+
declare function withYHistory(options?: WithYHistoryOptions): <T extends YjsEditor>(editor: T) => T & YHistoryEditor;
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/utils/convert.d.ts
|
|
114
|
+
declare function yTextToSlateElement(yText: Y.XmlText): Element;
|
|
115
|
+
declare function slateNodesToInsertDelta(nodes: Node[]): InsertDelta;
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/utils/position.d.ts
|
|
118
|
+
declare function slatePointToRelativePosition(sharedRoot: Y.XmlText, slateRoot: Node, point: BasePoint): Y.RelativePosition;
|
|
119
|
+
declare function relativePositionToSlatePoint(sharedRoot: Y.XmlText, slateRoot: Node, pos: Y.RelativePosition): BasePoint | null;
|
|
120
|
+
declare function slateRangeToRelativeRange(sharedRoot: Y.XmlText, slateRoot: Node, range: BaseRange): RelativeRange;
|
|
121
|
+
declare function relativeRangeToSlateRange(sharedRoot: Y.XmlText, slateRoot: Node, range: RelativeRange): BaseRange | null;
|
|
122
|
+
//#endregion
|
|
123
|
+
export { CursorEditor, type CursorState, type CursorStateChangeEvent, type RelativeRange, type RemoteCursorChangeEventListener, type WithCursorsOptions, type WithYHistoryOptions, type WithYjsOptions, YHistoryEditor, YjsEditor, relativePositionToSlatePoint, relativeRangeToSlateRange, slateNodesToInsertDelta, slatePointToRelativePosition, slateRangeToRelativeRange, withCursors, withYHistory, withYjs, yTextToSlateElement };
|
|
124
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("yjs"),require("slate")):"function"==typeof define&&define.amd?define(["exports","yjs","slate"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).WangEditorYjsModule={},t.Y,t.Slate)}(this,function(t,e,n){"use strict";function r(t){var e=Object.create(null);return t&&Object.keys(t).forEach(function(n){if("default"!==n){var r=Object.getOwnPropertyDescriptor(t,n);Object.defineProperty(e,n,r.get?r:{enumerable:!0,get:function(){return t[n]}})}}),e.default=t,Object.freeze(e)}var o=r(e),i=function(){return i=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},i.apply(this,arguments)};function a(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function s(t){var e="function"==typeof Symbol&&Symbol.iterator,n=e&&t[e],r=0;if(n)return n.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&r>=t.length&&(t=void 0),{value:t&&t[r++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),a=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)a.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return a}function u(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}function l(t){return"[object Object]"===Object.prototype.toString.call(t)}function f(t){if(!l(t))return!1;var e=t.constructor;if(void 0===e)return!0;var n=e.prototype;return!1!==l(n)&&!Object.prototype.hasOwnProperty.call(n,"isPrototypeOfOf")}function d(t,e){for(var n in t){var r=t[n],o=e[n];if(f(r)&&f(o)){if(!d(r,o))return!1}else if(Array.isArray(r)&&Array.isArray(o)){if(r.length!==o.length)return!1;for(var i=0;i<r.length;i+=1)if(r[i]!==o[i])return!1}else if(r!==o)return!1}for(var n in e)if(void 0===t[n]&&void 0!==e[n])return!1;return!0}function h(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return Object.fromEntries(Object.entries(t).filter(function(t){var n=c(t,1)[0];return e.includes(n)}))}function g(t){return Object.fromEntries(Object.entries(t).filter(function(t){return null!==c(t,2)[1]}))}function p(t){return function(t){var e,n,r,o,i=[];try{for(var a=s(t),c=a.next();!c.done;c=a.next()){var u=c.value;if("string"!=typeof u.insert||0!==u.insert.length){var l=i[i.length-1];l&&"string"==typeof l.insert&&"string"==typeof u.insert&&(l.attributes===u.attributes||!l.attributes==!u.attributes&&d(null!==(r=l.attributes)&&void 0!==r?r:{},null!==(o=u.attributes)&&void 0!==o?o:{}))?l.insert+=u.insert:i.push(u)}}}catch(t){e={error:t}}finally{try{c&&!c.done&&(n=a.return)&&n.call(a)}finally{if(e)throw e.error}}return i}(t.toDelta())}function v(t){var e=t.insert;return"string"==typeof e?e.length:1}function y(t){return t.reduce(function(t,e){return t+v(e)},0)}function x(t,e,n){if(n<1)return[];for(var r=0,o=[],a=e+n,s=0;s<t.length&&!(r>=a);s+=1){var c=t[s],u=v(c);if(r+u<=e)r+=u;else if("string"==typeof c.insert){var l=Math.max(0,e-r),f=Math.min(u,u-(r+u-a));o.push(i(i({},c),{insert:c.insert.slice(l,f)})),r+=u}else r+=u,o.push(c)}return o}function m(t){return t?n.Text.isText(t)?t.text.length:1:0}function w(t,e,r){var i;if(0===r.length)throw new Error("Path has to a have a length >= 1");if(n.Text.isText(e))throw new Error("Cannot descent into slate text");var a=c(r),s=a[0],u=a.slice(1),l=function(t,e){return t.children.slice(0,e).reduce(function(t,e){return t+m(e)},0)}(e,s),f=e.children[s],d=p(t),h=m(f),g=x(d,l,h);if(g.length>1)throw new Error("Path doesn't match yText, yTarget spans multiple nodes");var v=null===(i=g[0])||void 0===i?void 0:i.insert;if(u.length>0){if(!(v instanceof o.XmlText))throw new Error("Path doesn't match yText, cannot descent into non-yText");return w(v,f,u)}return{yParent:t,textRange:{start:l,end:l+h},yTarget:v instanceof o.XmlText?v:void 0,slateParent:e,slateTarget:f,targetDelta:g}}function b(t,e,r){void 0===r&&(r={});for(var o=r.assoc,i=void 0===o?0:o,a=r.insert,s=void 0!==a&&a,c=0,u=0,l=0;l<t.children.length;l+=1){var f=t.children[l],d=n.Text.isText(f)?f.text.length:1;d>0&&(u=l);var h=c+d;if(d>0&&(i>=0?h>e:h>=e))return[l,e-c];c+=d}if(e>c+(s?1:0))throw new Error("yOffset out of bounds");if(s)return[t.children.length,0];var g=t.children[u];return[u,n.Text.isText(g)?g.text.length:1]}function T(t,e,r){for(var i=[r];i[0]!==t;){var a=i[0].parent;if(!a)throw new Error("yText isn't a descendant of root element");if(!(a instanceof o.XmlText))throw new Error("Unexpected y parent type");i.unshift(a)}if(i.length<2)return[];var u=e;return i.reduce(function(t,e,r){var o,a,l=i[r+1];if(!l)return t;var f=0,d=p(e);try{for(var h=s(d),g=h.next();!g.done;g=h.next()){var v=g.value;if(v.insert===l)break;f+="string"==typeof v.insert?v.insert.length:1}}catch(t){o={error:t}}finally{try{g&&!g.done&&(a=h.return)&&a.call(h)}finally{if(o)throw o.error}}if(n.Text.isText(u))throw new Error("Cannot descent into slate text");var y=c(b(u,f),1)[0];return u=u.children[y],t.concat(y)},[])}function E(t){if(!t.doc)throw new Error("shared type isn't attached to a document")}"function"==typeof SuppressedError&&SuppressedError;var O="__slateYjsStoredPosition_";function P(t,e,n){var r=w(t,e,n.path),i=r.yTarget,a=r.yParent,s=r.textRange;if(i)throw new Error("Slate point points to a non-text element inside sharedRoot");var c=s.start+n.offset;return o.createRelativePositionFromTypeIndex(a,c,c===s.end?-1:0)}function R(t,e,r){if(!t.doc)throw new Error("sharedRoot isn't attach to a yDoc");var i=o.createAbsolutePositionFromRelativePosition(r,t.doc);return i&&function(t,e,r){var i=r.type,a=r.index,s=r.assoc;if(!(i instanceof o.XmlText))throw new Error("Absolute position points to a non-XMLText");var l=T(t,e,i),f=n.Node.get(e,l);if(n.Text.isText(f)||!n.Element.isElement(f))throw new Error("Absolute position doesn't match slateRoot, cannot descent into text and EditorRoot");var d=c(b(f,a,{assoc:s}),2),h=d[0],g=d[1],p=f.children[h];return n.Text.isText(p)?{path:u(u([],c(l),!1),[h],!1),offset:g}:null}(t,e,i)}function S(t,e,n){t.setAttribute(O+e,o.encodeRelativePosition(n))}function j(t,e,n){return Object.fromEntries(Object.entries(t).filter(function(t){var r=c(t,2)[1];return r.type===e&&(!n||(r.assoc>=0?r.index>=n.start&&r.index<n.end:r.index>n.start&&r.index>=n.end))}))}function C(t,e,n){var r;void 0===n&&(n="");var i=((r={})[n]=j(t,e),r);return p(e).forEach(function(e,r){var a=e.insert;a instanceof o.XmlText&&Object.assign(i,C(t,a,n?"".concat(n,".").concat(r):r.toString()))}),i}function k(t,e,n,r){void 0===r&&(r=0);var i=function(t){return E(t),Object.fromEntries(Object.entries(t.getAttributes()).filter(function(t){return c(t,1)[0].startsWith(O)}).map(function(e){var n=c(e,2),r=n[0],i=n[1];return[r.slice(25),o.createAbsolutePositionFromRelativePosition(o.decodeRelativePosition(i),t.doc)]}).filter(function(t){return c(t,2)[1]}))}(t),a={"":j(i,e,{start:r,end:r+y(n)})};return n.forEach(function(t,e){var n=t.insert;n instanceof o.XmlText&&Object.assign(a,C(i,n,e.toString()))}),a}function A(t,e,n,r,i,a,s){void 0===i&&(i=0),void 0===a&&(a=0),void 0===s&&(s="");var u=n[s];u&&Object.entries(u).forEach(function(n){var r=c(n,2),s=r[0],u=r[1];S(t,s,o.createRelativePositionFromTypeIndex(e,u.index-a+i,u.assoc))}),r.forEach(function(e,r){var i=e.insert;i instanceof o.XmlText&&A(t,i,n,p(i),0,0,s?"".concat(s,".").concat(r):r.toString())})}function D(t,e,n){return{anchor:P(t,e,n.anchor),focus:P(t,e,n.focus)}}function M(t,e,n){var r=R(t,e,n.anchor);if(!r)return null;var o=R(t,e,n.focus);return o?{anchor:r,focus:o}:null}function _(t){return function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];return Object.fromEntries(Object.entries(t).filter(function(t){var n=c(t,1)[0];return!e.includes(n)}))}(t,n.Text.isText(t)?"text":"children")}function L(t){var e=p(t),n=e.length>0?e.map(F):[{text:""}];return i(i({},t.getAttributes()),{children:n})}function F(t){return"string"==typeof t.insert?i(i({},t.attributes),{text:t.insert}):L(t.insert)}function Y(t){return t.map(function(t){return n.Text.isText(t)?{insert:t.text,attributes:_(t)}:{insert:X(t)}})}function X(t){var e=t.children,n=a(t,["children"]),r=new o.XmlText;return Object.entries(n).forEach(function(t){var e=c(t,2),n=e[0],o=e[1];r.setAttribute(n,o)}),r.applyDelta(Y(e),{sanitize:!1}),r}function I(t,e,r){var a=r.target,s=r.changes,l=r.delta;if(!(a instanceof o.XmlText))throw new Error("Unexpected target node type");var f=[],p=T(t,e,a),v=n.Node.get(e,p);if(n.Text.isText(v))throw new Error("Cannot apply yTextEvent to text node");var y=Array.from(s.keys.entries());if(p.length>0&&y.length>0){var x=Object.fromEntries(y.map(function(t){var e=c(t,2),n=e[0];return[n,"delete"===e[1].action?null:a.getAttribute(n)]})),w=Object.fromEntries(y.map(function(t){var e=c(t,1)[0];return[e,v[e]]}));f.push({type:"set_node",newProperties:x,properties:w,path:p})}return l.length>0&&f.push.apply(f,u([],c(function(t,e,r){var o=[],a=r.reduce(function(t,e){return"retain"in e?t+e.retain:"delete"in e?t+e.delete:t},0);return r.reverse().forEach(function(r){var s,l;if("attributes"in r&&"retain"in r)for(var f=c(b(t,a-r.retain),2),p=f[0],v=f[1],y=c(b(t,a,{assoc:-1}),2),x=y[0],w=y[1],T=x;T>=p;T-=1){var E=t.children[T],O=u(u([],c(e),!1),[T],!1);if(n.Text.isText(E)){var P=r.attributes,R=h.apply(void 0,u([t],c(Object.keys(r.attributes)),!1));if(T===p||T===x){var S=T===p?v:0;if((k=T===x?w:E.text.length)!==E.text.length&&o.push({type:"split_node",path:O,position:k,properties:_(E)}),0!==S){o.push({type:"split_node",path:O,position:S,properties:g(i(i({},_(E)),P))});continue}}o.push({type:"set_node",newProperties:P,path:O,properties:R})}}if("retain"in r&&(a-=r.retain),"delete"in r){var j=c(b(t,a-r.delete),2),C=(p=j[0],v=j[1],c(b(t,a,{assoc:-1}),2));for(x=C[0],T=0===(w=C[1])?x-1:x;T>=p;T-=1)if(E=t.children[T],O=u(u([],c(e),!1),[T],!1),!n.Text.isText(E)||T!==p&&T!==x)o.push({type:"remove_node",node:E,path:O}),a-=m(E);else{S=T===p?v:0;var k=T===x?w:E.text.length;o.push({type:"remove_text",offset:S,text:E.text.slice(S,k),path:O}),a-=k-S}}else if("insert"in r){var A=c(b(t,a,{insert:!0}),2),D=(T=A[0],A[1]);if(E=t.children[T],"string"==typeof r.insert&&T>0&&(null==E||0===D)){var M=T-1,L=t.children[M];n.Text.isText(L)&&d(null!==(s=r.attributes)&&void 0!==s?s:{},_(L))&&(T=M,D=L.text.length,E=L)}if(O=u(u([],c(e),!1),[T],!1),n.Text.isText(E)){var Y=o[o.length-1],X=null!=Y&&"insert_node"===Y.type?Y.node:_(E),I=[];if(null==Y||"insert_node"!==Y.type&&"insert_text"!==Y.type&&"split_node"!==Y.type&&"set_node"!==Y.type||(I=Y.path),"string"==typeof r.insert&&d(null!==(l=r.attributes)&&void 0!==l?l:{},X)&&(null==Y||n.Path.equals(O,I)))return o.push({type:"insert_text",offset:D,text:r.insert,path:O});var z=F(r);return 0===D?o.push({type:"insert_node",path:O,node:z}):(D<E.text.length&&o.push({type:"split_node",path:O,position:D,properties:_(E)}),o.push({type:"insert_node",path:n.Path.next(O),node:z}))}return o.push({type:"insert_node",path:O,node:F(r)})}}),o}(v,p,l)),!1)),f}function z(t,e,r){n.Editor.withoutNormalizing(e,function(){r.forEach(function(n){(function(t,e,n){if(n instanceof o.YTextEvent)return I(t,e,n);throw new Error("Unexpected Y event type")})(t,e,n).forEach(function(t){e.apply(t)})})})}function N(t){return t.map(function(t){return"string"==typeof t.insert?t:i(i({},t),{insert:W(t.insert)})})}function W(t){var e=new o.XmlText,n=t.getAttributes();return Object.entries(n).forEach(function(t){var n=c(t,2),r=n[0],o=n[1];e.setAttribute(r,o)}),e.applyDelta(N(p(t)),{sanitize:!1}),e}var U={insert_node:function(t,e,r){var o=w(t,e,r.path),i=o.yParent,a=o.textRange;if(n.Text.isText(r.node))return i.insert(a.start,r.node.text,_(r.node));i.insertEmbed(a.start,X(r.node))},remove_node:function(t,e,n){var r=w(t,e,n.path),o=r.yParent,i=r.textRange;o.delete(i.start,i.end-i.start)},set_node:function(t,e,n){var r=w(t,e,n.path),o=r.yTarget,a=r.textRange,s=r.yParent;if(o)return Object.entries(n.newProperties).forEach(function(t){var e=c(t,2),n=e[0],r=e[1];if(null===r)return o.removeAttribute(n);o.setAttribute(n,r)}),Object.entries(n.properties).forEach(function(t){var e=c(t,1)[0];Object.prototype.hasOwnProperty.call(n.newProperties,e)||o.removeAttribute(e)});var u=Object.fromEntries(Object.keys(n.properties).map(function(t){return[t,null]})),l=i(i({},u),n.newProperties);s.format(a.start,a.end-a.start,l)},merge_node:function(t,e,r){var o=w(t,e,r.path),a=w(o.yParent,o.slateParent,n.Path.previous(r.path.slice(-1)));if(!o.yTarget!=!a.yTarget)throw new Error("Cannot merge y text with y element");if(!a.yTarget||!o.yTarget){var s=o.yParent,l=o.textRange,f=o.slateTarget;if(!f)throw new Error("Expected Slate target node for merge op.");var d=n.Node.get(e,n.Path.previous(r.path));if(!n.Text.isText(d))throw new Error("Path points to Y.Text but not a Slate text node.");var h=_(f),g=_(d),v=Object.keys(h).reduce(function(t,e){var n;return e in g?t:i(i({},t),((n={})[e]=null,n))},{});return s.format(l.start,l.end-l.start,i(i({},v),g))}var y=a.yTarget.length,x=p(o.yTarget),m=N(x),b=k(t,o.yTarget,x,y),T=u([{retain:y}],c(m),!1);a.yTarget.applyDelta(T,{sanitize:!1}),o.yParent.delete(o.textRange.start,o.textRange.end-o.textRange.start),A(t,a.yTarget,b,m,y)},move_node:function(t,e,r){var o=n.Path.parent(r.newPath),i=r.newPath[r.newPath.length-1],a=n.Node.get(e,o);if(n.Text.isText(a))throw new Error("Cannot move slate node into text element");var s=u(u([],c(o),!1),[Math.min(i,a.children.length)],!1),l=w(t,e,r.path),f=w(t,e,s),d=N(l.targetDelta),h=k(t,l.yParent,l.targetDelta),g=l.textRange.end-l.textRange.start,v=r.path[r.path.length-1],x=n.Path.equals(n.Path.parent(r.path),o)&&v<i?f.textRange.end:f.textRange.start;l.yParent.delete(l.textRange.start,g),l.yParent===f.yParent&&l.textRange.start<x&&(x-=g);var m=y(p(f.yParent)),b=Math.min(x,m),T=u([{retain:b}],c(d),!1);f.yParent.applyDelta(T,{sanitize:!1}),A(t,f.yParent,h,d,b,l.textRange.start)},split_node:function(t,e,r){var a=w(t,e,r.path);if(!a.slateTarget)throw new Error("Y target without corresponding slate node");if(!a.yTarget){if(!n.Text.isText(a.slateTarget))throw new Error("Mismatch node type between y target and slate node");var s={};return a.targetDelta.forEach(function(t){t.attributes&&Object.keys(t.attributes).forEach(function(t){s[t]=null})}),a.yParent.format(a.textRange.start,a.textRange.end-a.textRange.start,i(i({},s),r.properties))}if(n.Text.isText(a.slateTarget))throw new Error("Mismatch node type between y target and slate node");var u=w(a.yTarget,a.slateTarget,[r.position]),l=a.slateTarget.children.slice(0,r.position).reduce(function(t,e){return t+m(e)},0),f=a.slateTarget.children.reduce(function(t,e){return t+m(e)},0),d=x(p(a.yTarget),l,f-l),h=N(d),g=k(t,a.yTarget,d,l),v=new o.XmlText;v.applyDelta(h,{sanitize:!1}),Object.entries(r.properties).forEach(function(t){var e=c(t,2),n=e[0],r=e[1];v.setAttribute(n,r)}),a.yTarget.delete(u.textRange.start,a.yTarget.length-u.textRange.start),a.yParent.insertEmbed(a.textRange.end,v),A(t,v,g,h,0,l)}};var q=i(i(i({},{insert_text:function(t,e,r){var o=w(t,e,r.path),i=o.yParent,a=o.textRange,s=n.Node.get(e,r.path);if(!n.Text.isText(s))throw new Error("Cannot insert text into non-text node");i.insert(a.start+r.offset,r.text,_(s))},remove_text:function(t,e,n){var r=w(t,e,n.path),o=r.yParent,i=r.textRange;o.delete(i.start+n.offset,n.text.length)}}),U),{set_selection:function(){}});var B=Symbol("slate-yjs-operation"),H=Symbol("slate-yjs-position-storage"),J=new WeakMap,G=new WeakMap,K=new WeakSet,Q={isYjsEditor:function(t){return n.Editor.isEditor(t)&&t.sharedRoot instanceof o.XmlText&&"localOrigin"in t&&"positionStorageOrigin"in t&&"function"==typeof t.applyRemoteEvents&&"function"==typeof t.storeLocalChange&&"function"==typeof t.flushLocalChanges&&"function"==typeof t.isLocalOrigin&&"function"==typeof t.connect&&"function"==typeof t.disconnect},localChanges:function(t){var e;return null!==(e=G.get(t))&&void 0!==e?e:[]},applyRemoteEvents:function(t,e,n){t.applyRemoteEvents(e,n)},storeLocalChange:function(t,e){t.storeLocalChange(e)},flushLocalChanges:function(t){t.flushLocalChanges()},connected:function(t){return K.has(t)},connect:function(t){t.connect()},disconnect:function(t){t.disconnect()},isLocal:function(t){return t.isLocalOrigin(Q.origin(t))},origin:function(t){var e=J.get(t);return void 0!==e?e:t.localOrigin},withOrigin:function(t,e,n){var r=Q.origin(t);J.set(t,e),n(),J.set(t,r)},storePosition:function(t,e,n){var r=t.sharedRoot,o=t.positionStorageOrigin;E(r);var i=P(r,t,n);r.doc.transact(function(){S(r,e,i)},o)},removeStoredPosition:function(t,e){var n=t.sharedRoot,r=t.positionStorageOrigin;E(n),n.doc.transact(function(){!function(t,e){t.removeAttribute(O+e)}(n,e)},r)},position:function(t,e){var n=function(t,e){var n=t.getAttribute(O+e);return n?o.decodeRelativePosition(n):null}(t.sharedRoot,e);if(n)return R(t.sharedRoot,t,n)},storedPositionsRelative:function(t){return e=t.sharedRoot,Object.fromEntries(Object.entries(e.getAttributes()).filter(function(t){return c(t,1)[0].startsWith(O)}).map(function(t){var e=c(t,2),n=e[0],r=e[1];return[n.slice(25),o.createRelativePositionFromJSON(r)]}));var e}};var V=new WeakMap,Z={isCursorEditor:function(t){return Q.isYjsEditor(t)&&t.awareness&&"string"==typeof t.cursorDataField&&"string"==typeof t.selectionStateField&&"function"==typeof t.sendCursorPosition&&"function"==typeof t.sendCursorData},sendCursorPosition:function(t,e){void 0===e&&(e=t.selection),t.sendCursorPosition(e)},sendCursorData:function(t,e){t.sendCursorData(e)},on:function(t,e,n){var r;if("change"===e){var o=null!==(r=V.get(t))&&void 0!==r?r:new Set;o.add(n),t&&V.set(t,o)}},off:function(t,e,n){if("change"===e){var r=V.get(t);r&&r.delete(n)}},cursorState:function(t,e){var n;if(e===t.awareness.clientID||!Q.connected(t))return null;var r=t.awareness.getStates().get(e);return r?{relativeSelection:null!==(n=r[t.selectionStateField])&&void 0!==n?n:null,data:r[t.cursorDataField],clientId:e}:null},cursorStates:function(t){return Q.connected(t)?Object.fromEntries(Array.from(t.awareness.getStates().entries(),function(e){var n=c(e,2),r=n[0],o=n[1];return r!==t.awareness.clientID&&o?[r,{relativeSelection:o[t.selectionStateField],data:o[t.cursorDataField]}]:null}).filter(Array.isArray)):{}}};var $=new WeakMap,tt=Symbol("slate-yjs-history-without-saving"),et={isYHistoryEditor:function(t){return Q.isYjsEditor(t)&&t.undoManager instanceof o.UndoManager&&"function"==typeof t.undo&&"function"==typeof t.redo&&"withoutSavingOrigin"in t},canUndo:function(t){return t.undoManager.undoStack.length>0},canRedo:function(t){return t.undoManager.redoStack.length>0},isSaving:function(t){return t.undoManager.trackedOrigins.has(Q.origin(t))},withoutSaving:function(t,e){Q.withOrigin(t,t.withoutSavingOrigin,e)}};t.CursorEditor=Z,t.YHistoryEditor=et,t.YjsEditor=Q,t.relativePositionToSlatePoint=R,t.relativeRangeToSlateRange=M,t.slateNodesToInsertDelta=Y,t.slatePointToRelativePosition=P,t.slateRangeToRelativeRange=D,t.withCursors=function(t,e){return void 0===e&&(e={}),function(n){var r=e.cursorStateField,i=void 0===r?"selection":r,a=e.cursorDataField,s=void 0===a?"data":a,c=e.autoSend,u=void 0===c||c,l=e.data,f=n;f.awareness=t,f.cursorDataField=s,f.selectionStateField=i,f.sendCursorData=function(t){f.awareness.setLocalStateField(f.cursorDataField,t)},f.sendCursorPosition=function(t){var e=f.awareness.getLocalState(),n=null==e?void 0:e[i];if(t){var r=D(f.sharedRoot,f,t),a=r.anchor,s=r.focus;n&&o.compareRelativePositions(a,n)&&o.compareRelativePositions(s,n)||f.awareness.setLocalStateField(f.selectionStateField,{anchor:a,focus:s})}else n&&f.awareness.setLocalStateField(f.selectionStateField,null)};var d=function(t){var e=V.get(f);if(e){var n=f.awareness.clientID,r={added:t.added.filter(function(t){return t!==n}),removed:t.removed.filter(function(t){return t!==n}),updated:t.updated.filter(function(t){return t!==n})};(r.added.length>0||r.removed.length>0||r.updated.length>0)&&e.forEach(function(t){return t(r)})}},h=f.connect,g=f.disconnect;return f.connect=function(){if(h(),f.awareness.on("change",d),d({removed:[],added:Array.from(f.awareness.getStates().keys()),updated:[]}),u){l&&Z.sendCursorData(f,l);var t=f.onChange;f.onChange=function(){t(),Q.connected(f)&&Z.sendCursorPosition(f)}}},f.disconnect=function(){f.awareness.off("change",d),d({removed:Array.from(f.awareness.getStates().keys()),added:[],updated:[]}),g()},f}},t.withYHistory=function(t){return void 0===t&&(t={}),function(e){var r=e,s=t.withoutSavingOrigin,c=void 0===s?tt:s,u=t.trackedOrigins,l=void 0===u?new Set([r.localOrigin]):u,f=a(t,["withoutSavingOrigin","trackedOrigins"]),d=new o.UndoManager(r.sharedRoot,i({trackedOrigins:l},f));r.undoManager=d,r.withoutSavingOrigin=c;var h=r.onChange,g=r.isLocalOrigin;r.onChange=function(){h(),$.set(r,r.selection&&D(r.sharedRoot,r,r.selection))},r.isLocalOrigin=function(t){return t===r.withoutSavingOrigin||g(t)};var p=function(t){var e=t.stackItem;e.meta.set("selection",r.selection&&D(r.sharedRoot,r,r.selection)),e.meta.set("selectionBefore",$.get(r))},v=function(t){t.stackItem.meta.set("selection",r.selection&&D(r.sharedRoot,r,r.selection))},y=function(t){var e=t.stackItem,o="undo"===t.type?r.undoManager.redoStack:r.undoManager.undoStack,i=o[o.length-1];i&&(i.meta.set("selection",e.meta.get("selectionBefore")),i.meta.set("selectionBefore",e.meta.get("selection")));var a=e.meta.get("selectionBefore");if(a){var s=M(r.sharedRoot,r,a);s&&n.Transforms.select(r,s)}},x=r.connect,m=r.disconnect;return r.connect=function(){x(),r.undoManager.on("stack-item-added",p),r.undoManager.on("stack-item-popped",y),r.undoManager.on("stack-item-updated",v)},r.disconnect=function(){r.undoManager.off("stack-item-added",p),r.undoManager.off("stack-item-popped",y),r.undoManager.off("stack-item-updated",v),m()},r.undo=function(){Q.connected(r)&&(Q.flushLocalChanges(r),r.undoManager.undo())},r.redo=function(){Q.connected(r)&&(Q.flushLocalChanges(r),r.undoManager.redo())},r}},t.withYjs=function(t,e){return void 0===e&&(e={}),function(r){var o,i,a=r;a.sharedRoot=t,a.localOrigin=null!==(o=e.localOrigin)&&void 0!==o?o:B,a.positionStorageOrigin=null!==(i=e.positionStorageOrigin)&&void 0!==i?i:H,a.applyRemoteEvents=function(t,e){Q.flushLocalChanges(a),n.Editor.withoutNormalizing(a,function(){Q.withOrigin(a,e,function(){z(a.sharedRoot,a,t)})})},a.isLocalOrigin=function(t){return t===a.localOrigin};var s=function(t,e){a.isLocalOrigin(e.origin)||Q.applyRemoteEvents(a,t,e.origin)},l=null;e.autoConnect&&(l=setTimeout(function(){l=null,Q.connect(a)})),a.connect=function(){if(Q.connected(a))throw new Error("already connected");a.sharedRoot.observeDeep(s);var t=L(a.sharedRoot);a.children=t.children,K.add(a),n.Editor.normalize(r,{force:!0}),r.operations.length||r.onChange()},a.disconnect=function(){l&&clearTimeout(l),Q.flushLocalChanges(a),a.sharedRoot.unobserveDeep(s),K.delete(a)},a.storeLocalChange=function(t){G.set(a,u(u([],c(Q.localChanges(a)),!1),[{op:t,doc:r.children,origin:Q.origin(a)}],!1))},a.flushLocalChanges=function(){E(a.sharedRoot);var t=Q.localChanges(a);G.delete(a);var e=[];t.forEach(function(t){var n=e[e.length-1];if(n&&n[0].origin===t.origin)return n.push(t);e.push([t])}),e.forEach(function(t){E(a.sharedRoot),a.sharedRoot.doc.transact(function(){t.forEach(function(t){E(a.sharedRoot),function(t,e,n){var r=q[n.type];if(!r)throw new Error("Unknown operation: ".concat(n.type));r(t,e,n)}(a.sharedRoot,{children:t.doc},t.op)})},t[0].origin)})};var f=a.apply,d=a.onChange;return a.apply=function(t){Q.connected(a)&&Q.isLocal(a)&&Q.storeLocalChange(a,t),f(t)},a.onChange=function(){Q.connected(a)&&Q.flushLocalChanges(a),d()},a}},t.yTextToSlateElement=L});
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require(`yjs`),require(`slate`)):typeof define==`function`&&define.amd?define([`exports`,`yjs`,`slate`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.WangEditorYjsModule={},e.Y,e.Slate))})(this,function(e,t,n){var r=Object.create,i=Object.defineProperty,a=Object.getOwnPropertyDescriptor,o=Object.getOwnPropertyNames,s=Object.getPrototypeOf,c=Object.prototype.hasOwnProperty,l=(e,t,n,r)=>{if(t&&typeof t==`object`||typeof t==`function`)for(var s=o(t),l=0,u=s.length,d;l<u;l++)d=s[l],!c.call(e,d)&&d!==n&&i(e,d,{get:(e=>t[e]).bind(null,d),enumerable:!(r=a(t,d))||r.enumerable});return e};t=((e,t,n)=>(n=e==null?{}:r(s(e)),l(t||!e||!e.__esModule?i(n,`default`,{value:e,enumerable:!0}):n,e)))(t);function u(e){return Object.prototype.toString.call(e)===`[object Object]`}function d(e){if(!u(e))return!1;let t=e.constructor;if(t===void 0)return!0;let n=t.prototype;return!(u(n)===!1||Object.prototype.hasOwnProperty.call(n,`isPrototypeOfOf`))}function f(e,t){for(let n in e){let r=e[n],i=t[n];if(d(r)&&d(i)){if(!f(r,i))return!1}else if(Array.isArray(r)&&Array.isArray(i)){if(r.length!==i.length)return!1;for(let e=0;e<r.length;e+=1)if(r[e]!==i[e])return!1}else if(r!==i)return!1}for(let n in t)if(e[n]===void 0&&t[n]!==void 0)return!1;return!0}function p(e,...t){return Object.fromEntries(Object.entries(e).filter(([e])=>t.includes(e)))}function m(e,...t){return Object.fromEntries(Object.entries(e).filter(([e])=>!t.includes(e)))}function h(e){return Object.fromEntries(Object.entries(e).filter(([,e])=>e!==null))}function g(e){"@babel/helpers - typeof";return g=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},g(e)}function _(e,t){if(g(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(g(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function ee(e){var t=_(e,`string`);return g(t)==`symbol`?t:t+``}function te(e,t,n){return(t=ee(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function v(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function y(e){for(var t=1;t<arguments.length;t++){var n=arguments[t]==null?{}:arguments[t];t%2?v(Object(n),!0).forEach(function(t){te(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):v(Object(n)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function ne(e){let t=[];for(let i of e){var n,r;if(typeof i.insert==`string`&&i.insert.length===0)continue;let e=t[t.length-1];if(!e||typeof e.insert!=`string`||typeof i.insert!=`string`){t.push(i);continue}if(e.attributes===i.attributes||!e.attributes==!i.attributes&&f((n=e.attributes)==null?{}:n,(r=i.attributes)==null?{}:r)){e.insert+=i.insert;continue}t.push(i)}return t}function b(e){return ne(e.toDelta())}function x({insert:e}){return typeof e==`string`?e.length:1}function S(e){return e.reduce((e,t)=>e+x(t),0)}function C(e,t,n){if(n<1)return[];let r=0,i=[],a=t+n;for(let n=0;n<e.length&&!(r>=a);n+=1){let o=e[n],s=x(o);if(r+s<=t){r+=s;continue}if(typeof o.insert!=`string`){r+=s,i.push(o);continue}let c=Math.max(0,t-r),l=Math.min(s,s-(r+s-a));i.push(y(y({},o),{},{insert:o.insert.slice(c,l)})),r+=s}return i}function w(e){return e?n.Text.isText(e)?e.text.length:1:0}function re(e,t){return e.children.slice(0,t).reduce((e,t)=>e+w(t),0)}function T(e,r,i){var a;if(i.length===0)throw Error(`Path has to a have a length >= 1`);if(n.Text.isText(r))throw Error(`Cannot descent into slate text`);let[o,...s]=i,c=re(r,o),l=r.children[o],u=b(e),d=w(l),f=C(u,c,d);if(f.length>1)throw Error(`Path doesn't match yText, yTarget spans multiple nodes`);let p=(a=f[0])==null?void 0:a.insert;if(s.length>0){if(!(p instanceof t.XmlText))throw Error(`Path doesn't match yText, cannot descent into non-yText`);return T(p,l,s)}return{yParent:e,textRange:{start:c,end:c+d},yTarget:p instanceof t.XmlText?p:void 0,slateParent:r,slateTarget:l,targetDelta:f}}function E(e,t,r={}){let{assoc:i=0,insert:a=!1}=r,o=0,s=0;for(let r=0;r<e.children.length;r+=1){let a=e.children[r],c=n.Text.isText(a)?a.text.length:1;c>0&&(s=r);let l=o+c;if(c>0&&(i>=0?l>t:l>=t))return[r,t-o];o+=c}if(t>o+(a?1:0))throw Error(`yOffset out of bounds`);if(a)return[e.children.length,0];let c=e.children[s],l=n.Text.isText(c)?c.text.length:1;return[s,l]}function D(e,r,i){let a=[i];for(;a[0]!==e;){let{parent:e}=a[0];if(!e)throw Error(`yText isn't a descendant of root element`);if(!(e instanceof t.XmlText))throw Error(`Unexpected y parent type`);a.unshift(e)}if(a.length<2)return[];let o=r;return a.reduce((e,t,r)=>{let i=a[r+1];if(!i)return e;let s=0,c=b(t);for(let e of c){if(e.insert===i)break;s+=typeof e.insert==`string`?e.insert.length:1}if(n.Text.isText(o))throw Error(`Cannot descent into slate text`);let[l]=E(o,s);return o=o.children[l],e.concat(l)},[])}function O(e){if(!e.doc)throw Error(`shared type isn't attached to a document`)}let k=`__slateYjsStoredPosition_`;function A(e,n,r){let{yTarget:i,yParent:a,textRange:o}=T(e,n,r.path);if(i)throw Error(`Slate point points to a non-text element inside sharedRoot`);let s=o.start+r.offset;return t.createRelativePositionFromTypeIndex(a,s,s===o.end?-1:0)}function ie(e,r,{type:i,index:a,assoc:o}){if(!(i instanceof t.XmlText))throw Error(`Absolute position points to a non-XMLText`);let s=D(e,r,i),c=n.Node.get(r,s);if(n.Text.isText(c)||!n.Element.isElement(c))throw Error(`Absolute position doesn't match slateRoot, cannot descent into text and EditorRoot`);let[l,u]=E(c,a,{assoc:o}),d=c.children[l];return n.Text.isText(d)?{path:[...s,l],offset:u}:null}function j(e,n,r){if(!e.doc)throw Error(`sharedRoot isn't attach to a yDoc`);let i=t.createAbsolutePositionFromRelativePosition(r,e.doc);return i&&ie(e,n,i)}function ae(e,n){let r=e.getAttribute(k+n);return r?t.decodeRelativePosition(r):null}function oe(e){return Object.fromEntries(Object.entries(e.getAttributes()).filter(([e])=>e.startsWith(k)).map(([e,n])=>[e.slice(25),t.createRelativePositionFromJSON(n)]))}function se(e){return O(e),Object.fromEntries(Object.entries(e.getAttributes()).filter(([e])=>e.startsWith(k)).map(([n,r])=>[n.slice(25),t.createAbsolutePositionFromRelativePosition(t.decodeRelativePosition(r),e.doc)]).filter(([,e])=>e))}function ce(e,t){e.removeAttribute(k+t)}function M(e,n,r){e.setAttribute(k+n,t.encodeRelativePosition(r))}function N(e,t,n){return Object.fromEntries(Object.entries(e).filter(([,e])=>e.type===t?n?e.assoc>=0?e.index>=n.start&&e.index<n.end:e.index>n.start&&e.index>=n.end:!0:!1))}function P(e,n,r=``){let i={[r]:N(e,n)};return b(n).forEach(({insert:n},a)=>{n instanceof t.XmlText&&Object.assign(i,P(e,n,r?`${r}.${a}`:a.toString()))}),i}function F(e,n,r,i=0){let a=se(e),o={"":N(a,n,{start:i,end:i+S(r)})};return r.forEach(({insert:e},n)=>{e instanceof t.XmlText&&Object.assign(o,P(a,e,n.toString()))}),o}function I(e,n,r,i,a=0,o=0,s=``){let c=r[s];c&&Object.entries(c).forEach(([r,i])=>{M(e,r,t.createRelativePositionFromTypeIndex(n,i.index-o+a,i.assoc))}),i.forEach(({insert:n},i)=>{n instanceof t.XmlText&&I(e,n,r,b(n),0,0,s?`${s}.${i}`:i.toString())})}function L(e,t,n){return{anchor:A(e,t,n.anchor),focus:A(e,t,n.focus)}}function R(e,t,n){let r=j(e,t,n.anchor);if(!r)return null;let i=j(e,t,n.focus);return i?{anchor:r,focus:i}:null}function z(e){return m(e,n.Text.isText(e)?`text`:`children`)}function le(e,t){if(e==null)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(t.includes(r))continue;n[r]=e[r]}return n}function B(e,t){if(e==null)return{};var n,r,i=le(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(r=0;r<a.length;r++)n=a[r],t.includes(n)||{}.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}let ue=[`children`];function V(e){let t=b(e),n=t.length>0?t.map(H):[{text:``}];return y(y({},e.getAttributes()),{},{children:n})}function H(e){return typeof e.insert==`string`?y(y({},e.attributes),{},{text:e.insert}):V(e.insert)}function U(e){return e.map(e=>n.Text.isText(e)?{insert:e.text,attributes:z(e)}:{insert:W(e)})}function W(e){let{children:n}=e,r=B(e,ue),i=new t.XmlText;return Object.entries(r).forEach(([e,t])=>{i.setAttribute(e,t)}),i.applyDelta(U(n),{sanitize:!1}),i}function de(e,t,r){let i=[],a=r.reduce((e,t)=>`retain`in t?e+t.retain:`delete`in t?e+t.delete:e,0);return r.reverse().forEach(r=>{if(`attributes`in r&&`retain`in r){let[o,s]=E(e,a-r.retain),[c,l]=E(e,a,{assoc:-1});for(let a=c;a>=o;--a){let u=e.children[a],d=[...t,a];if(!n.Text.isText(u))continue;let f=r.attributes,m=p(e,...Object.keys(r.attributes));if(a===o||a===c){let e=a===o?s:0,t=a===c?l:u.text.length;if(t!==u.text.length&&i.push({type:`split_node`,path:d,position:t,properties:z(u)}),e!==0){i.push({type:`split_node`,path:d,position:e,properties:h(y(y({},z(u)),f))});continue}}i.push({type:`set_node`,newProperties:f,path:d,properties:m})}}if(`retain`in r&&(a-=r.retain),`delete`in r){let[o,s]=E(e,a-r.delete),[c,l]=E(e,a,{assoc:-1});for(let r=l===0?c-1:c;r>=o;--r){let u=e.children[r],d=[...t,r];if(n.Text.isText(u)&&(r===o||r===c)){let e=r===o?s:0,t=r===c?l:u.text.length;i.push({type:`remove_text`,offset:e,text:u.text.slice(e,t),path:d}),a-=t-e;continue}i.push({type:`remove_node`,node:u,path:d}),a-=w(u)}return}if(`insert`in r){let[c,l]=E(e,a,{insert:!0}),u=e.children[c];if(typeof r.insert==`string`&&c>0&&(u==null||l===0)){var o;let t=c-1,i=e.children[t];n.Text.isText(i)&&f((o=r.attributes)==null?{}:o,z(i))&&(c=t,l=i.text.length,u=i)}let d=[...t,c];if(n.Text.isText(u)){var s;let e=i[i.length-1],t=e!=null&&e.type===`insert_node`?e.node:z(u),a=[];if(e!=null&&(e.type===`insert_node`||e.type===`insert_text`||e.type===`split_node`||e.type===`set_node`)&&(a=e.path),typeof r.insert==`string`&&f((s=r.attributes)==null?{}:s,t)&&(e==null||n.Path.equals(d,a)))return i.push({type:`insert_text`,offset:l,text:r.insert,path:d});let o=H(r);return l===0?i.push({type:`insert_node`,path:d,node:o}):(l<u.text.length&&i.push({type:`split_node`,path:d,position:l,properties:z(u)}),i.push({type:`insert_node`,path:n.Path.next(d),node:o}))}return i.push({type:`insert_node`,path:d,node:H(r)})}}),i}function fe(e,r,i){let{target:a,changes:o}=i,s=i.delta;if(!(a instanceof t.XmlText))throw Error(`Unexpected target node type`);let c=[],l=D(e,r,a),u=n.Node.get(r,l);if(n.Text.isText(u))throw Error(`Cannot apply yTextEvent to text node`);let d=Array.from(o.keys.entries());if(l.length>0&&d.length>0){let e=Object.fromEntries(d.map(([e,t])=>[e,t.action===`delete`?null:a.getAttribute(e)])),t=Object.fromEntries(d.map(([e])=>[e,u[e]]));c.push({type:`set_node`,newProperties:e,properties:t,path:l})}return s.length>0&&c.push(...de(u,l,s)),c}function pe(e,n,r){if(r instanceof t.YTextEvent)return fe(e,n,r);throw Error(`Unexpected Y event type`)}function me(e,t,r){n.Editor.withoutNormalizing(t,()=>{r.forEach(n=>{pe(e,t,n).forEach(e=>{t.apply(e)})})})}function he(e,t,r){let{yParent:i,textRange:a}=T(e,t,r.path);if(n.Text.isText(r.node))return i.insert(a.start,r.node.text,z(r.node));i.insertEmbed(a.start,W(r.node))}function G(e){return e.map(e=>typeof e.insert==`string`?e:y(y({},e),{},{insert:ge(e.insert)}))}function ge(e){let n=new t.XmlText,r=e.getAttributes();return Object.entries(r).forEach(([e,t])=>{n.setAttribute(e,t)}),n.applyDelta(G(b(e)),{sanitize:!1}),n}function K(e,t,r){let i=T(e,t,r.path),a=T(i.yParent,i.slateParent,n.Path.previous(r.path.slice(-1)));if(!i.yTarget!=!a.yTarget)throw Error(`Cannot merge y text with y element`);if(!a.yTarget||!i.yTarget){let{yParent:e,textRange:a,slateTarget:o}=i;if(!o)throw Error(`Expected Slate target node for merge op.`);let s=n.Node.get(t,n.Path.previous(r.path));if(!n.Text.isText(s))throw Error(`Path points to Y.Text but not a Slate text node.`);let c=z(o),l=z(s),u=Object.keys(c).reduce((e,t)=>t in l?e:y(y({},e),{},{[t]:null}),{});return e.format(a.start,a.end-a.start,y(y({},u),l))}let o=a.yTarget.length,s=b(i.yTarget),c=G(s),l=F(e,i.yTarget,s,o),u=[{retain:o},...c];a.yTarget.applyDelta(u,{sanitize:!1}),i.yParent.delete(i.textRange.start,i.textRange.end-i.textRange.start),I(e,a.yTarget,l,c,o)}function _e(e,t,r){let i=n.Path.parent(r.newPath),a=r.newPath[r.newPath.length-1],o=n.Node.get(t,i);if(n.Text.isText(o))throw Error(`Cannot move slate node into text element`);let s=[...i,Math.min(a,o.children.length)],c=T(e,t,r.path),l=T(e,t,s),u=G(c.targetDelta),d=F(e,c.yParent,c.targetDelta),f=c.textRange.end-c.textRange.start,p=r.path[r.path.length-1],m=n.Path.equals(n.Path.parent(r.path),i)&&p<a?l.textRange.end:l.textRange.start;c.yParent.delete(c.textRange.start,f),c.yParent===l.yParent&&c.textRange.start<m&&(m-=f);let h=S(b(l.yParent)),g=Math.min(m,h),_=[{retain:g},...u];l.yParent.applyDelta(_,{sanitize:!1}),I(e,l.yParent,d,u,g,c.textRange.start)}function ve(e,t,n){let{yParent:r,textRange:i}=T(e,t,n.path);r.delete(i.start,i.end-i.start)}function ye(e,t,n){let{yTarget:r,textRange:i,yParent:a}=T(e,t,n.path);if(r)return Object.entries(n.newProperties).forEach(([e,t])=>{if(t===null)return r.removeAttribute(e);r.setAttribute(e,t)}),Object.entries(n.properties).forEach(([e])=>{Object.prototype.hasOwnProperty.call(n.newProperties,e)||r.removeAttribute(e)});let o=y(y({},Object.fromEntries(Object.keys(n.properties).map(e=>[e,null]))),n.newProperties);a.format(i.start,i.end-i.start,o)}function be(e,r,i){let a=T(e,r,i.path);if(!a.slateTarget)throw Error(`Y target without corresponding slate node`);if(!a.yTarget){if(!n.Text.isText(a.slateTarget))throw Error(`Mismatch node type between y target and slate node`);let e={};return a.targetDelta.forEach(t=>{t.attributes&&Object.keys(t.attributes).forEach(t=>{e[t]=null})}),a.yParent.format(a.textRange.start,a.textRange.end-a.textRange.start,y(y({},e),i.properties))}if(n.Text.isText(a.slateTarget))throw Error(`Mismatch node type between y target and slate node`);let o=T(a.yTarget,a.slateTarget,[i.position]),s=a.slateTarget.children.slice(0,i.position).reduce((e,t)=>e+w(t),0),c=a.slateTarget.children.reduce((e,t)=>e+w(t),0),l=C(b(a.yTarget),s,c-s),u=G(l),d=F(e,a.yTarget,l,s),f=new t.XmlText;f.applyDelta(u,{sanitize:!1}),Object.entries(i.properties).forEach(([e,t])=>{f.setAttribute(e,t)}),a.yTarget.delete(o.textRange.start,a.yTarget.length-o.textRange.start),a.yParent.insertEmbed(a.textRange.end,f),I(e,f,d,u,0,s)}let xe={insert_node:he,remove_node:ve,set_node:ye,merge_node:K,move_node:_e,split_node:be};function Se(e,t,r){let{yParent:i,textRange:a}=T(e,t,r.path),o=n.Node.get(t,r.path);if(!n.Text.isText(o))throw Error(`Cannot insert text into non-text node`);i.insert(a.start+r.offset,r.text,z(o))}function Ce(e,t,n){let{yParent:r,textRange:i}=T(e,t,n.path);r.delete(i.start+n.offset,n.text.length)}let we=y(y(y({},{insert_text:Se,remove_text:Ce}),xe),{},{set_selection:()=>{}});function Te(e,t,n){let r=we[n.type];if(!r)throw Error(`Unknown operation: ${n.type}`);r(e,t,n)}let Ee=Symbol(`slate-yjs-operation`),De=Symbol(`slate-yjs-position-storage`),q=new WeakMap,J=new WeakMap,Y=new WeakSet,X={isYjsEditor(e){return n.Editor.isEditor(e)&&e.sharedRoot instanceof t.XmlText&&`localOrigin`in e&&`positionStorageOrigin`in e&&typeof e.applyRemoteEvents==`function`&&typeof e.storeLocalChange==`function`&&typeof e.flushLocalChanges==`function`&&typeof e.isLocalOrigin==`function`&&typeof e.connect==`function`&&typeof e.disconnect==`function`},localChanges(e){var t;return(t=J.get(e))==null?[]:t},applyRemoteEvents(e,t,n){e.applyRemoteEvents(t,n)},storeLocalChange(e,t){e.storeLocalChange(t)},flushLocalChanges(e){e.flushLocalChanges()},connected(e){return Y.has(e)},connect(e){e.connect()},disconnect(e){e.disconnect()},isLocal(e){return e.isLocalOrigin(X.origin(e))},origin(e){let t=q.get(e);return t===void 0?e.localOrigin:t},withOrigin(e,t,n){let r=X.origin(e);q.set(e,t),n(),q.set(e,r)},storePosition(e,t,n){let{sharedRoot:r,positionStorageOrigin:i}=e;O(r);let a=A(r,e,n);r.doc.transact(()=>{M(r,t,a)},i)},removeStoredPosition(e,t){let{sharedRoot:n,positionStorageOrigin:r}=e;O(n),n.doc.transact(()=>{ce(n,t)},r)},position(e,t){let n=ae(e.sharedRoot,t);if(n)return j(e.sharedRoot,e,n)},storedPositionsRelative(e){return oe(e.sharedRoot)}};function Oe(e,t={}){return function(r){var i,a;let o=r;o.sharedRoot=e,o.localOrigin=(i=t.localOrigin)==null?Ee:i,o.positionStorageOrigin=(a=t.positionStorageOrigin)==null?De:a,o.applyRemoteEvents=(e,t)=>{X.flushLocalChanges(o),n.Editor.withoutNormalizing(o,()=>{X.withOrigin(o,t,()=>{me(o.sharedRoot,o,e)})})},o.isLocalOrigin=e=>e===o.localOrigin;let s=(e,t)=>{o.isLocalOrigin(t.origin)||X.applyRemoteEvents(o,e,t.origin)},c=null;t.autoConnect&&(c=setTimeout(()=>{c=null,X.connect(o)})),o.connect=()=>{if(X.connected(o))throw Error(`already connected`);o.sharedRoot.observeDeep(s),o.children=V(o.sharedRoot).children,Y.add(o),n.Editor.normalize(r,{force:!0}),r.operations.length||r.onChange()},o.disconnect=()=>{c&&clearTimeout(c),X.flushLocalChanges(o),o.sharedRoot.unobserveDeep(s),Y.delete(o)},o.storeLocalChange=e=>{J.set(o,[...X.localChanges(o),{op:e,doc:r.children,origin:X.origin(o)}])},o.flushLocalChanges=()=>{O(o.sharedRoot);let e=X.localChanges(o);J.delete(o);let t=[];e.forEach(e=>{let n=t[t.length-1];if(n&&n[0].origin===e.origin)return n.push(e);t.push([e])}),t.forEach(e=>{O(o.sharedRoot),o.sharedRoot.doc.transact(()=>{e.forEach(e=>{O(o.sharedRoot),Te(o.sharedRoot,{children:e.doc},e.op)})},e[0].origin)})};let{apply:l,onChange:u}=o;return o.apply=e=>{X.connected(o)&&X.isLocal(o)&&X.storeLocalChange(o,e),l(e)},o.onChange=()=>{X.connected(o)&&X.flushLocalChanges(o),u()},o}}let Z=new WeakMap,Q={isCursorEditor(e){return X.isYjsEditor(e)&&e.awareness&&typeof e.cursorDataField==`string`&&typeof e.selectionStateField==`string`&&typeof e.sendCursorPosition==`function`&&typeof e.sendCursorData==`function`},sendCursorPosition(e,t=e.selection){e.sendCursorPosition(t)},sendCursorData(e,t){e.sendCursorData(t)},on(e,t,n){var r;if(t!==`change`)return;let i=(r=Z.get(e))==null?new Set:r;i.add(n),e&&Z.set(e,i)},off(e,t,n){if(t!==`change`)return;let r=Z.get(e);r&&r.delete(n)},cursorState(e,t){var n;if(t===e.awareness.clientID||!X.connected(e))return null;let r=e.awareness.getStates().get(t);return r?{relativeSelection:(n=r[e.selectionStateField])==null?null:n,data:r[e.cursorDataField],clientId:t}:null},cursorStates(e){return X.connected(e)?Object.fromEntries(Array.from(e.awareness.getStates().entries(),([t,n])=>t===e.awareness.clientID||!n?null:[t,{relativeSelection:n[e.selectionStateField],data:n[e.cursorDataField]}]).filter(Array.isArray)):{}}};function ke(e,n={}){return function(r){let{cursorStateField:i=`selection`,cursorDataField:a=`data`,autoSend:o=!0,data:s}=n,c=r;c.awareness=e,c.cursorDataField=a,c.selectionStateField=i,c.sendCursorData=e=>{c.awareness.setLocalStateField(c.cursorDataField,e)},c.sendCursorPosition=e=>{let n=c.awareness.getLocalState(),r=n==null?void 0:n[i];if(!e){r&&c.awareness.setLocalStateField(c.selectionStateField,null);return}let{anchor:a,focus:o}=L(c.sharedRoot,c,e);(!r||!t.compareRelativePositions(a,r)||!t.compareRelativePositions(o,r))&&c.awareness.setLocalStateField(c.selectionStateField,{anchor:a,focus:o})};let l=e=>{let t=Z.get(c);if(!t)return;let n=c.awareness.clientID,r={added:e.added.filter(e=>e!==n),removed:e.removed.filter(e=>e!==n),updated:e.updated.filter(e=>e!==n)};(r.added.length>0||r.removed.length>0||r.updated.length>0)&&t.forEach(e=>e(r))},{connect:u,disconnect:d}=c;return c.connect=()=>{if(u(),c.awareness.on(`change`,l),l({removed:[],added:Array.from(c.awareness.getStates().keys()),updated:[]}),o){s&&Q.sendCursorData(c,s);let{onChange:e}=c;c.onChange=()=>{e(),X.connected(c)&&Q.sendCursorPosition(c)}}},c.disconnect=()=>{c.awareness.off(`change`,l),l({removed:Array.from(c.awareness.getStates().keys()),added:[],updated:[]}),d()},c}}let Ae=[`withoutSavingOrigin`,`trackedOrigins`],$=new WeakMap,je=Symbol(`slate-yjs-history-without-saving`),Me={isYHistoryEditor(e){return X.isYjsEditor(e)&&e.undoManager instanceof t.UndoManager&&typeof e.undo==`function`&&typeof e.redo==`function`&&`withoutSavingOrigin`in e},canUndo(e){return e.undoManager.undoStack.length>0},canRedo(e){return e.undoManager.redoStack.length>0},isSaving(e){return e.undoManager.trackedOrigins.has(X.origin(e))},withoutSaving(e,t){X.withOrigin(e,e.withoutSavingOrigin,t)}};function Ne(e={}){return function(r){let i=r,{withoutSavingOrigin:a=je,trackedOrigins:o=new Set([i.localOrigin])}=e,s=B(e,Ae);i.undoManager=new t.UndoManager(i.sharedRoot,y({trackedOrigins:o},s)),i.withoutSavingOrigin=a;let{onChange:c,isLocalOrigin:l}=i;i.onChange=()=>{c(),$.set(i,i.selection&&L(i.sharedRoot,i,i.selection))},i.isLocalOrigin=e=>e===i.withoutSavingOrigin||l(e);let u=({stackItem:e})=>{e.meta.set(`selection`,i.selection&&L(i.sharedRoot,i,i.selection)),e.meta.set(`selectionBefore`,$.get(i))},d=({stackItem:e})=>{e.meta.set(`selection`,i.selection&&L(i.sharedRoot,i,i.selection))},f=({stackItem:e,type:t})=>{let r=t===`undo`?i.undoManager.redoStack:i.undoManager.undoStack,a=r[r.length-1];a&&(a.meta.set(`selection`,e.meta.get(`selectionBefore`)),a.meta.set(`selectionBefore`,e.meta.get(`selection`)));let o=e.meta.get(`selectionBefore`);if(!o)return;let s=R(i.sharedRoot,i,o);s&&n.Transforms.select(i,s)},{connect:p,disconnect:m}=i;return i.connect=()=>{p(),i.undoManager.on(`stack-item-added`,u),i.undoManager.on(`stack-item-popped`,f),i.undoManager.on(`stack-item-updated`,d)},i.disconnect=()=>{i.undoManager.off(`stack-item-added`,u),i.undoManager.off(`stack-item-popped`,f),i.undoManager.off(`stack-item-updated`,d),m()},i.undo=()=>{X.connected(i)&&(X.flushLocalChanges(i),i.undoManager.undo())},i.redo=()=>{X.connected(i)&&(X.flushLocalChanges(i),i.undoManager.redo())},i}}e.CursorEditor=Q,e.YHistoryEditor=Me,e.YjsEditor=X,e.relativePositionToSlatePoint=j,e.relativeRangeToSlateRange=R,e.slateNodesToInsertDelta=U,e.slatePointToRelativePosition=A,e.slateRangeToRelativeRange=L,e.withCursors=ke,e.withYHistory=Ne,e.withYjs=Oe,e.yTextToSlateElement=V});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|