New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@slate-yjs/core

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@slate-yjs/core - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

2

dist/index.modern.js

@@ -1,2 +0,2 @@

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

@@ -1,2 +0,2 @@

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

@@ -1,2 +0,2 @@

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

@@ -5,2 +5,3 @@ import * as Y from 'yjs';

undoManager: Y.UndoManager;
withoutSavingOrigin: unknown;
undo: () => void;

@@ -13,5 +14,9 @@ redo: () => void;

canRedo(editor: YHistoryEditor): boolean;
isSaving(editor: YHistoryEditor): boolean;
withoutSaving(editor: YHistoryEditor, fn: () => void): void;
};
export declare type WithYHistoryOptions = NonNullable<ConstructorParameters<typeof Y.UndoManager>[1]>;
export declare function withYHistory<T extends YjsEditor>(editor: T, { trackedOrigins, ...options }?: WithYHistoryOptions): T & YHistoryEditor;
export declare type WithYHistoryOptions = NonNullable<ConstructorParameters<typeof Y.UndoManager>[1]> & {
withoutSavingOrigin?: unknown;
};
export declare function withYHistory<T extends YjsEditor>(editor: T, { withoutSavingOrigin, trackedOrigins, ...options }?: WithYHistoryOptions): T & YHistoryEditor;
//# sourceMappingURL=withYHistory.d.ts.map

@@ -6,2 +6,3 @@ import { BaseEditor, Descendant, Editor, Operation, Point } from 'slate';

doc: Descendant[];
origin: unknown;
};

@@ -13,4 +14,5 @@ export declare type YjsEditor = BaseEditor & {

applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
storeLocalOperation: (op: Operation) => void;
storeLocalChange: (op: Operation) => void;
flushLocalChanges: () => void;
isLocalOrigin: (origin: unknown) => boolean;
connect: () => void;

@@ -23,3 +25,3 @@ disconnect: () => void;

applyRemoteEvents(editor: YjsEditor, events: Y.YEvent[], origin: unknown): void;
storeLocalOperation(editor: YjsEditor, op: Operation): void;
storeLocalChange(editor: YjsEditor, op: Operation): void;
flushLocalChanges(editor: YjsEditor): void;

@@ -29,4 +31,5 @@ connected(editor: YjsEditor): boolean;

disconnect(editor: YjsEditor): void;
remoteOrigin(editor: YjsEditor): unknown | undefined;
asRemote(editor: YjsEditor, origin: unknown, fn: () => void): void;
isLocal(editor: YjsEditor): boolean;
origin(editor: YjsEditor): unknown;
withOrigin(editor: YjsEditor, origin: unknown, fn: () => void): void;
storePosition(editor: YjsEditor, key: string, point: Point): void;

@@ -33,0 +36,0 @@ removeStoredPosition(editor: YjsEditor, key: string): void;

@@ -1,3 +0,3 @@

import { Descendant, Text } from 'slate';
export declare function getProperties<TNode extends Descendant>(node: TNode): Omit<TNode, TNode extends Text ? 'text' : 'children'>;
import { BaseText, Descendant } from 'slate';
export declare function getProperties<TNode extends Descendant>(node: TNode): Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
//# sourceMappingURL=slate.d.ts.map
{
"name": "@slate-yjs/core",
"sideEffects": false,
"version": "0.1.1",
"version": "0.2.0",
"description": "Yjs binding for Slate.",

@@ -6,0 +6,0 @@ "keywords": [

@@ -11,2 +11,5 @@ import { Editor, Transforms } from 'slate';

const LAST_SELECTION: WeakMap<Editor, RelativeRange | null> = new WeakMap();
const DEFAULT_WITHOUT_SAVING_ORIGIN = Symbol(
'slate-yjs-history-without-saving'
);

@@ -16,2 +19,4 @@ export type YHistoryEditor = YjsEditor & {

withoutSavingOrigin: unknown;
undo: () => void;

@@ -27,3 +32,4 @@ redo: () => void;

typeof (value as YHistoryEditor).undo === 'function' &&
typeof (value as YHistoryEditor).redo === 'function'
typeof (value as YHistoryEditor).redo === 'function' &&
'withoutSavingOrigin' in value
);

@@ -39,2 +45,10 @@ },

},
isSaving(editor: YHistoryEditor): boolean {
return editor.undoManager.trackedOrigins.has(YjsEditor.origin(editor));
},
withoutSaving(editor: YHistoryEditor, fn: () => void) {
YjsEditor.withOrigin(editor, editor.withoutSavingOrigin, fn);
},
};

@@ -44,3 +58,5 @@

ConstructorParameters<typeof Y.UndoManager>[1]
>;
> & {
withoutSavingOrigin?: unknown;
};

@@ -50,2 +66,3 @@ export function withYHistory<T extends YjsEditor>(

{
withoutSavingOrigin = DEFAULT_WITHOUT_SAVING_ORIGIN,
trackedOrigins = new Set([editor.localOrigin]),

@@ -62,3 +79,6 @@ ...options

const { onChange } = e;
e.undoManager = undoManager;
e.withoutSavingOrigin = withoutSavingOrigin;
const { onChange, isLocalOrigin } = e;
e.onChange = () => {

@@ -73,2 +93,5 @@ onChange();

e.isLocalOrigin = (origin) =>
origin === e.withoutSavingOrigin || isLocalOrigin(origin);
undoManager.on(

@@ -134,4 +157,2 @@ 'stack-item-added',

e.undoManager = undoManager;
e.undo = () => {

@@ -138,0 +159,0 @@ YjsEditor.flushLocalChanges(e);

@@ -16,3 +16,7 @@ import { BaseEditor, Descendant, Editor, Operation, Point } from 'slate';

type LocalChange = { op: Operation; doc: Descendant[] };
type LocalChange = {
op: Operation;
doc: Descendant[];
origin: unknown;
};

@@ -33,5 +37,8 @@ const DEFAULT_LOCAL_ORIGIN = Symbol('slate-yjs-operation');

applyRemoteEvents: (events: Y.YEvent[], origin: unknown) => void;
storeLocalOperation: (op: Operation) => void;
storeLocalChange: (op: Operation) => void;
flushLocalChanges: () => void;
isLocalOrigin: (origin: unknown) => boolean;
connect: () => void;

@@ -46,7 +53,8 @@ disconnect: () => void;

(value as YjsEditor).sharedRoot instanceof Y.XmlText &&
(value as YjsEditor).localOrigin !== undefined &&
(value as YjsEditor).positionStorageOrigin !== undefined &&
'localOrigin' in value &&
'positionStorageOrigin' in value &&
typeof (value as YjsEditor).applyRemoteEvents === 'function' &&
typeof (value as YjsEditor).storeLocalOperation === 'function' &&
typeof (value as YjsEditor).storeLocalChange === 'function' &&
typeof (value as YjsEditor).flushLocalChanges === 'function' &&
typeof (value as YjsEditor).isLocalOrigin === 'function' &&
typeof (value as YjsEditor).connect === 'function' &&

@@ -69,4 +77,4 @@ typeof (value as YjsEditor).disconnect === 'function'

storeLocalOperation(editor: YjsEditor, op: Operation): void {
editor.storeLocalOperation(op);
storeLocalChange(editor: YjsEditor, op: Operation): void {
editor.storeLocalChange(op);
},

@@ -90,17 +98,15 @@

remoteOrigin(editor: YjsEditor): unknown | undefined {
return ORIGIN.get(editor);
isLocal(editor: YjsEditor): boolean {
return editor.isLocalOrigin(YjsEditor.origin(editor));
},
asRemote(editor: YjsEditor, origin: unknown, fn: () => void): void {
const prev = YjsEditor.remoteOrigin(editor);
origin(editor: YjsEditor): unknown {
return ORIGIN.get(editor) ?? editor.localOrigin;
},
withOrigin(editor: YjsEditor, origin: unknown, fn: () => void): void {
const prev = YjsEditor.origin(editor);
ORIGIN.set(editor, origin);
fn();
if (prev === undefined) {
ORIGIN.delete(editor);
} else {
ORIGIN.set(editor, prev);
}
ORIGIN.set(editor, prev);
},

@@ -171,5 +177,7 @@

e.applyRemoteEvents = (events, eventOrigin) => {
e.applyRemoteEvents = (events, origin) => {
YjsEditor.flushLocalChanges(e);
Editor.withoutNormalizing(e, () => {
YjsEditor.asRemote(e, eventOrigin, () => {
YjsEditor.withOrigin(e, origin, () => {
applyYjsEvents(e.sharedRoot, e, events);

@@ -180,4 +188,6 @@ });

e.isLocalOrigin = (origin) => origin === e.localOrigin;
const handleYEvents = (events: Y.YEvent[], transaction: Y.Transaction) => {
if (transaction.origin === e.localOrigin) {
if (e.isLocalOrigin(transaction.origin)) {
return;

@@ -215,6 +225,6 @@ }

e.storeLocalOperation = (op) => {
e.storeLocalChange = (op) => {
LOCAL_CHANGES.set(e, [
...YjsEditor.localChanges(e),
{ op, doc: editor.children },
{ op, doc: editor.children, origin: YjsEditor.origin(e) },
]);

@@ -228,7 +238,23 @@ };

e.sharedRoot.doc.transact(() => {
localChanges.forEach((change) => {
applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
});
}, e.localOrigin);
// Group local changes by origin so we can apply them in the correct order
// with the correct origin with a minimal amount of transactions.
const txGroups: LocalChange[][] = [];
localChanges.forEach((change) => {
const currentGroup = txGroups[txGroups.length - 1];
if (currentGroup && currentGroup[0].origin === change.origin) {
return currentGroup.push(change);
}
txGroups.push([change]);
});
txGroups.forEach((txGroup) => {
assertDocumentAttachment(e.sharedRoot);
e.sharedRoot.doc.transact(() => {
txGroup.forEach((change) => {
assertDocumentAttachment(e.sharedRoot);
applySlateOp(e.sharedRoot, { children: change.doc }, change.op);
});
}, txGroup[0].origin);
});
};

@@ -238,4 +264,4 @@

e.apply = (op) => {
if (YjsEditor.connected(e) && YjsEditor.remoteOrigin(e) === undefined) {
YjsEditor.storeLocalOperation(e, op);
if (YjsEditor.connected(e) && YjsEditor.isLocal(e)) {
YjsEditor.storeLocalChange(e, op);
}

@@ -242,0 +268,0 @@

@@ -1,2 +0,2 @@

import { BasePoint, BaseRange, Node, Point, Text } from 'slate';
import { BasePoint, BaseRange, Node, Text } from 'slate';
import * as Y from 'yjs';

@@ -3,0 +3,0 @@ import { InsertDelta, RelativeRange, TextRange } from '../model/types';

@@ -1,2 +0,2 @@

import { Descendant, Text } from 'slate';
import { BaseText, Descendant, Text } from 'slate';
import { omit } from './object';

@@ -6,7 +6,7 @@

node: TNode
): Omit<TNode, TNode extends Text ? 'text' : 'children'> {
): Omit<TNode, TNode extends BaseText ? 'text' : 'children'> {
return omit(
node,
(Text.isText(node) ? 'text' : 'children') as keyof TNode
) as unknown as Omit<TNode, TNode extends Text ? 'text' : 'children'>;
) as Omit<TNode, TNode extends BaseText ? 'text' : 'children'>;
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc