/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@prosemirror-adapter/react@0.5.3/dist/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{Fragment,createContext,createElement,useCallback,useContext,useLayoutEffect,useMemo,useRef,useState}from"react";import{CoreMarkView,CoreNodeView,CorePluginView,CoreWidgetView}from"@prosemirror-adapter/core";import{createPortal,flushSync}from"react-dom";import{Decoration}from"prosemirror-view";const markViewContext=createContext({contentRef:()=>{},view:null,mark:null}),useMarkViewContext=()=>useContext(markViewContext),createMarkViewContext=createContext((e=>{throw new Error("No ProsemirrorAdapterProvider detected, maybe you need to wrap the component with the Editor with ProsemirrorAdapterProvider?")})),useMarkViewFactory=()=>useContext(createMarkViewContext);var AbstractReactMarkView=class extends CoreMarkView{constructor(...e){super(...e),this.context={contentRef:this.contentRef,view:this.view,mark:this.mark},this.updateContext=()=>{Object.assign(this.context,{mark:this.mark})}}},ReactMarkView=class extends AbstractReactMarkView{constructor(...e){super(...e),this.render=()=>{const e=this.component;return createPortal(createElement(markViewContext.Provider,{value:this.context},createElement(e,null)),this.dom,this.key)}}};function buildReactMarkViewCreator(e,t,r){return function(o){return function(n,i,s){const a=new r({mark:n,view:i,inline:s,options:{...o,destroy(){o.destroy?.(),t(a)}}});return e(a,!1),a}}}function useReactMarkViewCreator(e,t){return useMemo((()=>buildReactMarkViewCreator(e,t,ReactMarkView)),[e,t])}const nodeViewContext=createContext({contentRef:()=>{},view:null,getPos:()=>0,setAttrs:()=>{},node:null,selected:!1,decorations:[],innerDecorations:null}),useNodeViewContext=()=>useContext(nodeViewContext),createNodeViewContext=createContext((e=>{throw new Error("No ProsemirrorAdapterProvider detected, maybe you need to wrap the component with the Editor with ProsemirrorAdapterProvider?")})),useNodeViewFactory=()=>useContext(createNodeViewContext);var AbstractReactNodeView=class extends CoreNodeView{constructor(...e){super(...e),this.context={contentRef:this.contentRef,view:this.view,getPos:this.getPos,setAttrs:this.setAttrs,node:this.node,selected:this.selected,decorations:this.decorations,innerDecorations:this.innerDecorations},this.updateContext=()=>{Object.assign(this.context,{node:this.node,selected:this.selected,decorations:this.decorations,innerDecorations:this.innerDecorations})}}},ReactNodeView=class extends AbstractReactNodeView{constructor(...e){super(...e),this.render=()=>{const e=this.component;return createPortal(createElement(nodeViewContext.Provider,{value:this.context},createElement(e,null)),this.dom,this.key)}}};function buildReactNodeViewCreator(e,t,r){return function(o){return function(n,i,s,a,c){const u=new r({node:n,view:i,getPos:s,decorations:a,innerDecorations:c,options:{...o,onUpdate(){o.onUpdate?.(),e(u)},selectNode(){o.selectNode?.(),e(u)},deselectNode(){o.deselectNode?.(),e(u)},destroy(){o.destroy?.(),t(u)}}});return e(u,!1),u}}}function useReactNodeViewCreator(e,t){return useMemo((()=>buildReactNodeViewCreator(e,t,ReactNodeView)),[e,t])}const pluginViewContext=createContext({view:null}),usePluginViewContext=()=>useContext(pluginViewContext),createPluginViewContext=createContext((e=>{throw new Error("out of scope")})),usePluginViewFactory=()=>useContext(createPluginViewContext);var ReactPluginView=class extends CorePluginView{constructor(...e){super(...e),this.context={view:this.view,prevState:this.prevState},this.updateContext=()=>{Object.assign(this.context,{view:this.view,prevState:this.prevState})},this.render=()=>{const e=this.component;return createPortal(createElement(pluginViewContext.Provider,{value:this.context},createElement(e,null)),this.root,this.key)}}};function useReactPluginViewCreator(e,t){return useCallback((r=>o=>{const n=new ReactPluginView({view:o,options:{...r,update:(t,o)=>{r.update?.(t,o),e(n)},destroy:()=>{r.destroy?.(),t(n)}}});return e(n,!1),n}),[t,e])}function updateRenderer(e,t){const[r,o]=e,n=t.key,i=createElement(Fragment,{key:n},t.render()),s=r.indexOf(n);if(-1===s)return[[...r,n],[...o,i]];{const e=[...o];return e[s]=i,[r,e]}}function removeRenderer(e,t){const[r,o]=e,n=r.indexOf(t.key);if(-1===n)return e;const i=[...r],s=[...o];return i.splice(n,1),s.splice(n,1),[i,s]}function useReactRenderer(){const[e,t]=useState([[],[]]),r=useRef(!1);useLayoutEffect((()=>(r.current=!0,()=>{r.current=!1})),[]);const o=useCallback((e=>{r.current?flushSync(e):e()}),[]);return{render:()=>e[1],renderReactRenderer:useCallback(((e,r=!0)=>{o((()=>(r&&e.updateContext(),t((t=>updateRenderer(t,e))))))}),[o]),removeReactRenderer:useCallback((e=>{o((()=>t((t=>removeRenderer(t,e)))))}),[o])}}const widgetViewContext=createContext({view:null,getPos:()=>{}}),useWidgetViewContext=()=>useContext(widgetViewContext),createWidgetViewContext=createContext((e=>{throw new Error("out of scope")})),useWidgetViewFactory=()=>useContext(createWidgetViewContext);var ReactWidgetView=class extends CoreWidgetView{constructor(...e){super(...e),this.context={view:this.view,getPos:this.getPos,spec:this.spec},this.updateContext=()=>{Object.assign(this.context,{view:this.view,getPos:this.getPos,spec:this.spec})},this.render=()=>{const e=this.component;return createPortal(createElement(widgetViewContext.Provider,{value:this.context},createElement(e,null)),this.dom,this.key)}}};function useReactWidgetViewCreator(e,t){return useCallback((r=>(o,n={})=>{const i=new ReactWidgetView({pos:o,options:r}),s={...n,destroy:e=>{n.destroy?.(e),t(i)}};return i.spec=s,Decoration.widget(o,((t,r)=>(i.bind(t,r),e(i),i.dom)),s)}),[t,e])}const ProsemirrorAdapterProvider=({children:e})=>{const{renderReactRenderer:t,removeReactRenderer:r,render:o}=useReactRenderer(),n=useReactNodeViewCreator(t,r),i=useReactMarkViewCreator(t,r),s=useReactPluginViewCreator(t,r),a=useReactWidgetViewCreator(t,r);return createElement(createNodeViewContext.Provider,{value:n},createElement(createMarkViewContext.Provider,{value:i},createElement(createPluginViewContext.Provider,{value:s},createElement(createWidgetViewContext.Provider,{value:a},createElement(Fragment,{key:1},e),createElement(Fragment,{key:2},o())))))};export{AbstractReactMarkView,AbstractReactNodeView,ProsemirrorAdapterProvider,ReactMarkView,ReactNodeView,ReactPluginView,ReactWidgetView,buildReactMarkViewCreator,buildReactNodeViewCreator,createMarkViewContext,createNodeViewContext,createPluginViewContext,createWidgetViewContext,markViewContext,nodeViewContext,pluginViewContext,useMarkViewContext,useMarkViewFactory,useNodeViewContext,useNodeViewFactory,usePluginViewContext,usePluginViewFactory,useReactRenderer,useWidgetViewContext,useWidgetViewFactory,widgetViewContext};
//# sourceMappingURL=/sm/c35e4558ad03f5aef17a0fb90c5f9d61fc14f228b7a5abc86c580f7c8a8a7490.map