UNPKG

@wangeditor-next/yjs-for-react

Version:

React specific components/utils for wangeditor-next-yjs.

3 lines (2 loc) 7.53 kB
import e,{createContext as t,useContext as n,useReducer as r,useRef as o,useEffect as u,useCallback as i,useState as a,useLayoutEffect as c,useMemo as l}from"react";import{relativeRangeToSlateRange as f,CursorEditor as s}from"@wangeditor-next/yjs";import{DomEditor as v}from"@wangeditor-next/editor";import{Range as d,Editor as h,Text as p,Path as y}from"slate";var S=t(null),O=function(){var e=n(S);return e||console.warn("The `useEditorStatic` hook must be used inside the <EditorContext> component's context."),e},g=function(){return g=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},g.apply(this,arguments)};function b(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var r,o,u=n.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(r=u.next()).done;)i.push(r.value)}catch(e){o={error:e}}finally{try{r&&!r.done&&(n=u.return)&&n.call(u)}finally{if(o)throw o.error}}return i}"function"==typeof SuppressedError&&SuppressedError;var m=new WeakMap;function w(e,t){if(!t.relativeSelection)return null;var n=m.get(e.children);n||(n=new WeakMap,m.set(e.children,n));var r=n.get(t);if(void 0===r)try{r=f(e.sharedRoot,e,t.relativeSelection),n.set(t,r)}catch(e){return null}return r}function x(e,t,n){var r,o,u=n.yOffset,i=n.xOffset,a=n.shouldGenerateOverlay,c=b(d.edges(t),2),l=c[0],f=c[1],s=function(e,t){try{return v.toDOMRange(e,t)}catch(e){return null}}(e,t);if(!s)return{caretPosition:null,selectionRects:[]};var S=[],O=h.nodes(e,{at:t,match:function(e,t){return p.isText(e)&&(!a||a(e,t))}}),g=null,m=d.isBackward(t);try{for(var w=function(e){var t="function"==typeof Symbol&&Symbol.iterator,n=t&&e[t],r=0;if(n)return n.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}(O),x=w.next();!x.done;x=w.next()){var E=b(x.value,2),j=E[0],R=E[1],k=v.toDOMNode(e,j),C=y.equals(R,l.path),M=y.equals(R,f.path),P=null;if(C||M){var W=document.createRange();W.selectNode(k),C&&W.setStart(s.startContainer,s.startOffset),M&&W.setEnd(s.endContainer,s.endOffset),P=W.getClientRects()}else P=k.getClientRects();for(var z=m?C:M,V=0;V<P.length;V+=1){var D=P.item(V);if(D){var G=z&&(m?0===V:V===P.length-1),q=D.top-u,T=D.left-i;G&&(g={height:D.height,top:q,left:T+(m||d.isCollapsed(t)?0:D.width)}),S.push({width:D.width,height:D.height,top:q,left:T})}}}}catch(e){r={error:e}}finally{try{x&&!x.done&&(o=w.return)&&o.call(w)}finally{if(r)throw r.error}}return{selectionRects:S,caretPosition:g}}function E(){var e=O();return s.isCursorEditor(e)||console.warn("Cannot use useSyncExternalStore outside the context of a RemoteCursorEditor"),e}var j,R,k={exports:{}},C={};function M(){return R||(R=1,k.exports=function(){if(j)return C;j=1;var t=e,n="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},r=t.useState,o=t.useEffect,u=t.useLayoutEffect,i=t.useDebugValue;function a(e){var t=e.getSnapshot;e=e.value;try{var r=t();return!n(e,r)}catch(e){return!0}}var c="undefined"==typeof window||void 0===window.document||void 0===window.document.createElement?function(e,t){return t()}:function(e,t){var n=t(),c=r({inst:{value:n,getSnapshot:t}}),l=c[0].inst,f=c[1];return u(function(){l.value=n,l.getSnapshot=t,a(l)&&f({inst:l})},[e,n,t]),o(function(){return a(l)&&f({inst:l}),e(function(){a(l)&&f({inst:l})})},[e]),i(n),n};return C.useSyncExternalStore=void 0!==t.useSyncExternalStore?t.useSyncExternalStore:c,C}()),k.exports}var P,W,z=M(),V={exports:{}},D={};var G=(W||(W=1,V.exports=function(){if(P)return D;P=1;var t=e,n=M(),r="function"==typeof Object.is?Object.is:function(e,t){return e===t&&(0!==e||1/e==1/t)||e!=e&&t!=t},o=n.useSyncExternalStore,u=t.useRef,i=t.useEffect,a=t.useMemo,c=t.useDebugValue;return D.useSyncExternalStoreWithSelector=function(e,t,n,l,f){var s=u(null);if(null===s.current){var v={hasValue:!1,value:null};s.current=v}else v=s.current;s=a(function(){function e(e){if(!i){if(i=!0,o=e,e=l(e),void 0!==f&&v.hasValue){var t=v.value;if(f(t,e))return u=t}return u=e}if(t=u,r(o,e))return t;var n=l(e);return void 0!==f&&f(t,n)?(o=e,t):(o=e,u=n)}var o,u,i=!1,a=void 0===n?null:n;return[function(){return e(t())},null===a?void 0:function(){return e(a())}]},[t,n,l,f]);var d=o(e,s[0],s[1]);return i(function(){v.hasValue=!0,v.value=d},[d]),c(d),d},D}()),V.exports),q=new WeakMap;function T(e){var t=q.get(e);if(t)return t;var n=function(e){var t={},n=new Set,r=n.add.bind(n),o=new Set,u=null;return[function(t){return o.add(t),u||(u=function(e){e.added.forEach(r),e.removed.forEach(r),e.updated.forEach(r),o.forEach(function(e){return e()})},s.on(e,"change",u)),function(){o.delete(t),u&&0===o.size&&(s.off(e,"change",u),u=null)}},function(){return 0===n.size?t:(n.forEach(function(n){var r=s.cursorState(e,n);null!==r?t[n]=r:delete t[n.toString()]}),n.clear(),t=g({},t))}]}(e);return e&&q.set(e,n),n}function A(){return T(E())}function B(){var e=b(A(),2),t=e[0],n=e[1];return z.useSyncExternalStore(t,n)}function F(e,t){var n=b(A(),2),r=n[0],o=n[1];return G.useSyncExternalStoreWithSelector(r,o,null,e,t)}var N=Object.freeze([]);function I(e){var t;void 0===e&&(e={});var n,f,s,v=e.containerRef,d=e.shouldGenerateOverlay,h=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(e,["containerRef","shouldGenerateOverlay"]),p=E(),y=B(),S=(n=b(r(function(e){return e+1},0),2)[1],f=o(null),u(s=function(){f.current&&(cancelAnimationFrame(f.current),f.current=0)}),u(function(){return s},[]),i(function(e){void 0===e&&(e=!1),e?n():f.current||(f.current=requestAnimationFrame(n))},[])),O=o(new WeakMap),m=b(a({}),2),j=m[0],R=m[1],k=!("refreshOnResize"in h)||(null===(t=h.refreshOnResize)||void 0===t||t);!function(e,t){var n=o(t);n.current=t;var r=b(a(function(){return new ResizeObserver(function(){n.current()})}),1)[0];u(function(){if(null==e?void 0:e.current){var t=e.current;return r.observe(t),function(){return r.unobserve(t)}}},[r,e])}(k?v:void 0,function(){O.current=new WeakMap,S("debounced"!==k)}),c(function(){var e,t,n;if(!v||v.current){var r=null===(e=null==v?void 0:v.current)||void 0===e?void 0:e.getBoundingClientRect(),o=null!==(t=null==r?void 0:r.x)&&void 0!==t?t:0,u=null!==(n=null==r?void 0:r.y)&&void 0!==n?n:0,i=Object.keys(j).length!==Object.keys(y).length,a=Object.fromEntries(Object.entries(y).map(function(e){var t=b(e,2),n=t[0],r=t[1],a=r.relativeSelection&&w(p,r);if(!a)return[n,N];var c=O.current.get(a);if(c)return[n,c];var l=x(p,a,{xOffset:o,yOffset:u,shouldGenerateOverlay:d});return i=!0,O.current.set(a,l),[n,l]}));i&&R(a)}});var C=l(function(){return Object.entries(y).map(function(e){var t,n,r=b(e,2),o=r[0],u=r[1],i=u.relativeSelection&&w(p,u),a=j[o];return g(g({},u),{range:i,caretPosition:null!==(t=null==a?void 0:a.caretPosition)&&void 0!==t?t:null,selectionRects:null!==(n=null==a?void 0:a.selectionRects)&&void 0!==n?n:N})})},[y,p,j]);return[C,i(function(){O.current=new WeakMap,S(!0)},[S])]}export{S as EditorContext,w as getCursorRange,O as useEditorStatic,I as useRemoteCursorOverlayPositions,B as useRemoteCursorStates,F as useRemoteCursorStatesSelector}; //# sourceMappingURL=index.mjs.map