UNPKG

@wangeditor-next/yjs-for-react

Version:

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

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