reactjs-tiptap-editor
Version:
A modern WYSIWYG rich text editor based on tiptap and shadcn ui for React
34 lines (33 loc) • 27.1 kB
JavaScript
"use strict";const C=require("./index-CSvnpArE.cjs"),ie=require("tippy.js"),d=require("react"),X=require("react-dom");class oe{constructor({editor:e,element:t,view:r,tippyOptions:i={},updateDelay:o=250,shouldShow:f}){this.preventHide=!1,this.shouldShow=({view:s,state:p,from:l,to:c})=>{const{doc:u,selection:m}=p,{empty:v}=m,E=!u.textBetween(l,c).length&&C.isTextSelection(p.selection),y=this.element.contains(document.activeElement);return!(!(s.hasFocus()||y)||v||E||!this.editor.isEditable)},this.mousedownHandler=()=>{this.preventHide=!0},this.dragstartHandler=()=>{this.hide()},this.focusHandler=()=>{setTimeout(()=>this.update(this.editor.view))},this.blurHandler=({event:s})=>{var p;if(this.preventHide){this.preventHide=!1;return}s!=null&&s.relatedTarget&&(!((p=this.element.parentNode)===null||p===void 0)&&p.contains(s.relatedTarget))||(s==null?void 0:s.relatedTarget)!==this.editor.view.dom&&this.hide()},this.tippyBlurHandler=s=>{this.blurHandler({event:s})},this.handleDebouncedUpdate=(s,p)=>{const l=!(p!=null&&p.selection.eq(s.state.selection)),c=!(p!=null&&p.doc.eq(s.state.doc));!l&&!c||(this.updateDebounceTimer&&clearTimeout(this.updateDebounceTimer),this.updateDebounceTimer=window.setTimeout(()=>{this.updateHandler(s,l,c,p)},this.updateDelay))},this.updateHandler=(s,p,l,c)=>{var u,m,v;const{state:E,composing:y}=s,{selection:_}=E;if(y||!p&&!l)return;this.createTooltip();const{ranges:D}=_,S=Math.min(...D.map(a=>a.$from.pos)),h=Math.max(...D.map(a=>a.$to.pos));if(!((u=this.shouldShow)===null||u===void 0?void 0:u.call(this,{editor:this.editor,element:this.element,view:s,state:E,oldState:c,from:S,to:h}))){this.hide();return}(m=this.tippy)===null||m===void 0||m.setProps({getReferenceClientRect:((v=this.tippyOptions)===null||v===void 0?void 0:v.getReferenceClientRect)||(()=>{if(C.isNodeSelection(E.selection)){let a=s.nodeDOM(S);if(a){const g=a.dataset.nodeViewWrapper?a:a.querySelector("[data-node-view-wrapper]");if(g&&(a=g.firstChild),a)return a.getBoundingClientRect()}}return C.posToDOMRect(s,S,h)})}),this.show()},this.editor=e,this.element=t,this.view=r,this.updateDelay=o,f&&(this.shouldShow=f),this.element.addEventListener("mousedown",this.mousedownHandler,{capture:!0}),this.view.dom.addEventListener("dragstart",this.dragstartHandler),this.editor.on("focus",this.focusHandler),this.editor.on("blur",this.blurHandler),this.tippyOptions=i,this.element.remove(),this.element.style.visibility="visible"}createTooltip(){const{element:e}=this.editor.options,t=!!e.parentElement;this.tippy||!t||(this.tippy=ie(e,{duration:0,getReferenceClientRect:null,content:this.element,interactive:!0,trigger:"manual",placement:"top",hideOnClick:"toggle",...this.tippyOptions}),this.tippy.popper.firstChild&&this.tippy.popper.firstChild.addEventListener("blur",this.tippyBlurHandler))}update(e,t){const{state:r}=e,i=r.selection.from!==r.selection.to;if(this.updateDelay>0&&i){this.handleDebouncedUpdate(e,t);return}const o=!(t!=null&&t.selection.eq(e.state.selection)),f=!(t!=null&&t.doc.eq(e.state.doc));this.updateHandler(e,o,f,t)}show(){var e;(e=this.tippy)===null||e===void 0||e.show()}hide(){var e;(e=this.tippy)===null||e===void 0||e.hide()}destroy(){var e,t;!((e=this.tippy)===null||e===void 0)&&e.popper.firstChild&&this.tippy.popper.firstChild.removeEventListener("blur",this.tippyBlurHandler),(t=this.tippy)===null||t===void 0||t.destroy(),this.element.removeEventListener("mousedown",this.mousedownHandler,{capture:!0}),this.view.dom.removeEventListener("dragstart",this.dragstartHandler),this.editor.off("focus",this.focusHandler),this.editor.off("blur",this.blurHandler)}}const Z=n=>new C.Plugin({key:typeof n.pluginKey=="string"?new C.PluginKey(n.pluginKey):n.pluginKey,view:e=>new oe({view:e,...n})});C.Extension.create({name:"bubbleMenu",addOptions(){return{element:null,tippyOptions:{},pluginKey:"bubbleMenu",updateDelay:void 0,shouldShow:null}},addProseMirrorPlugins(){return this.options.element?[Z({pluginKey:this.options.pluginKey,editor:this.editor,element:this.options.element,tippyOptions:this.options.tippyOptions,updateDelay:this.options.updateDelay,shouldShow:this.options.shouldShow})]:[]}});function se(n){return n&&n.__esModule&&Object.prototype.hasOwnProperty.call(n,"default")?n.default:n}var U={exports:{}},H={};/**
* @license React
* use-sync-external-store-shim.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var K;function ue(){if(K)return H;K=1;var n=d;function e(u,m){return u===m&&(u!==0||1/u===1/m)||u!==u&&m!==m}var t=typeof Object.is=="function"?Object.is:e,r=n.useState,i=n.useEffect,o=n.useLayoutEffect,f=n.useDebugValue;function s(u,m){var v=m(),E=r({inst:{value:v,getSnapshot:m}}),y=E[0].inst,_=E[1];return o(function(){y.value=v,y.getSnapshot=m,p(y)&&_({inst:y})},[u,v,m]),i(function(){return p(y)&&_({inst:y}),u(function(){p(y)&&_({inst:y})})},[u]),f(v),v}function p(u){var m=u.getSnapshot;u=u.value;try{var v=m();return!t(u,v)}catch{return!0}}function l(u,m){return m()}var c=typeof window>"u"||typeof window.document>"u"||typeof window.document.createElement>"u"?l:s;return H.useSyncExternalStore=n.useSyncExternalStore!==void 0?n.useSyncExternalStore:c,H}var B={};/**
* @license React
* use-sync-external-store-shim.development.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var W;function ae(){return W||(W=1,process.env.NODE_ENV!=="production"&&function(){typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart=="function"&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var n=d,e=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function t(S){{for(var h=arguments.length,b=new Array(h>1?h-1:0),a=1;a<h;a++)b[a-1]=arguments[a];r("error",S,b)}}function r(S,h,b){{var a=e.ReactDebugCurrentFrame,g=a.getStackAddendum();g!==""&&(h+="%s",b=b.concat([g]));var w=b.map(function(O){return String(O)});w.unshift("Warning: "+h),Function.prototype.apply.call(console[S],console,w)}}function i(S,h){return S===h&&(S!==0||1/S===1/h)||S!==S&&h!==h}var o=typeof Object.is=="function"?Object.is:i,f=n.useState,s=n.useEffect,p=n.useLayoutEffect,l=n.useDebugValue,c=!1,u=!1;function m(S,h,b){c||n.startTransition!==void 0&&(c=!0,t("You are using an outdated, pre-release alpha of React 18 that does not support useSyncExternalStore. The use-sync-external-store shim will not work correctly. Upgrade to a newer pre-release."));var a=h();if(!u){var g=h();o(a,g)||(t("The result of getSnapshot should be cached to avoid an infinite loop"),u=!0)}var w=f({inst:{value:a,getSnapshot:h}}),O=w[0].inst,T=w[1];return p(function(){O.value=a,O.getSnapshot=h,v(O)&&T({inst:O})},[S,a,h]),s(function(){v(O)&&T({inst:O});var A=function(){v(O)&&T({inst:O})};return S(A)},[S]),l(a),a}function v(S){var h=S.getSnapshot,b=S.value;try{var a=h();return!o(b,a)}catch{return!0}}function E(S,h,b){return h()}var y=typeof window<"u"&&typeof window.document<"u"&&typeof window.document.createElement<"u",_=!y,R=_?E:m,D=n.useSyncExternalStore!==void 0?n.useSyncExternalStore:R;B.useSyncExternalStore=D,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop=="function"&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}()),B}process.env.NODE_ENV==="production"?U.exports=ue():U.exports=ae();var L=U.exports;const de=(...n)=>e=>{n.forEach(t=>{typeof t=="function"?t(e):t&&(t.current=e)})},ce=({contentComponent:n})=>{const e=L.useSyncExternalStore(n.subscribe,n.getSnapshot,n.getServerSnapshot);return d.createElement(d.Fragment,null,Object.values(e))};function le(){const n=new Set;let e={};return{subscribe(t){return n.add(t),()=>{n.delete(t)}},getSnapshot(){return e},getServerSnapshot(){return e},setRenderer(t,r){e={...e,[t]:X.createPortal(r.reactElement,r.element,t)},n.forEach(i=>i())},removeRenderer(t){const r={...e};delete r[t],e=r,n.forEach(i=>i())}}}class he extends d.Component{constructor(e){var t;super(e),this.editorContentRef=d.createRef(),this.initialized=!1,this.state={hasContentComponentInitialized:!!(!((t=e.editor)===null||t===void 0)&&t.contentComponent)}}componentDidMount(){this.init()}componentDidUpdate(){this.init()}init(){const e=this.props.editor;if(e&&!e.isDestroyed&&e.options.element){if(e.contentComponent)return;const t=this.editorContentRef.current;t.append(...e.options.element.childNodes),e.setOptions({element:t}),e.contentComponent=le(),this.state.hasContentComponentInitialized||(this.unsubscribeToContentComponent=e.contentComponent.subscribe(()=>{this.setState(r=>r.hasContentComponentInitialized?r:{hasContentComponentInitialized:!0}),this.unsubscribeToContentComponent&&this.unsubscribeToContentComponent()})),e.createNodeViews(),this.initialized=!0}}componentWillUnmount(){const e=this.props.editor;if(!e||(this.initialized=!1,e.isDestroyed||e.view.setProps({nodeViews:{}}),this.unsubscribeToContentComponent&&this.unsubscribeToContentComponent(),e.contentComponent=null,!e.options.element.firstChild))return;const t=document.createElement("div");t.append(...e.options.element.childNodes),e.setOptions({element:t})}render(){const{editor:e,innerRef:t,...r}=this.props;return d.createElement(d.Fragment,null,d.createElement("div",{ref:de(t,this.editorContentRef),...r}),(e==null?void 0:e.contentComponent)&&d.createElement(ce,{contentComponent:e.contentComponent}))}}const fe=d.forwardRef((n,e)=>{const t=d.useMemo(()=>Math.floor(Math.random()*4294967295).toString(),[n.editor]);return d.createElement(he,{key:t,innerRef:e,...n})}),pe=d.memo(fe);var me=function n(e,t){if(e===t)return!0;if(e&&t&&typeof e=="object"&&typeof t=="object"){if(e.constructor!==t.constructor)return!1;var r,i,o;if(Array.isArray(e)){if(r=e.length,r!=t.length)return!1;for(i=r;i--!==0;)if(!n(e[i],t[i]))return!1;return!0}if(e instanceof Map&&t instanceof Map){if(e.size!==t.size)return!1;for(i of e.entries())if(!t.has(i[0]))return!1;for(i of e.entries())if(!n(i[1],t.get(i[0])))return!1;return!0}if(e instanceof Set&&t instanceof Set){if(e.size!==t.size)return!1;for(i of e.entries())if(!t.has(i[0]))return!1;return!0}if(ArrayBuffer.isView(e)&&ArrayBuffer.isView(t)){if(r=e.length,r!=t.length)return!1;for(i=r;i--!==0;)if(e[i]!==t[i])return!1;return!0}if(e.constructor===RegExp)return e.source===t.source&&e.flags===t.flags;if(e.valueOf!==Object.prototype.valueOf)return e.valueOf()===t.valueOf();if(e.toString!==Object.prototype.toString)return e.toString()===t.toString();if(o=Object.keys(e),r=o.length,r!==Object.keys(t).length)return!1;for(i=r;i--!==0;)if(!Object.prototype.hasOwnProperty.call(t,o[i]))return!1;for(i=r;i--!==0;){var f=o[i];if(!(f==="_owner"&&e.$$typeof)&&!n(e[f],t[f]))return!1}return!0}return e!==e&&t!==t},ve=se(me),$={exports:{}},j={};/**
* @license React
* use-sync-external-store-shim/with-selector.production.min.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var z;function Se(){if(z)return j;z=1;var n=d,e=L;function t(l,c){return l===c&&(l!==0||1/l===1/c)||l!==l&&c!==c}var r=typeof Object.is=="function"?Object.is:t,i=e.useSyncExternalStore,o=n.useRef,f=n.useEffect,s=n.useMemo,p=n.useDebugValue;return j.useSyncExternalStoreWithSelector=function(l,c,u,m,v){var E=o(null);if(E.current===null){var y={hasValue:!1,value:null};E.current=y}else y=E.current;E=s(function(){function R(a){if(!D){if(D=!0,S=a,a=m(a),v!==void 0&&y.hasValue){var g=y.value;if(v(g,a))return h=g}return h=a}if(g=h,r(S,a))return g;var w=m(a);return v!==void 0&&v(g,w)?g:(S=a,h=w)}var D=!1,S,h,b=u===void 0?null:u;return[function(){return R(c())},b===null?void 0:function(){return R(b())}]},[c,u,m,v]);var _=i(l,E[0],E[1]);return f(function(){y.hasValue=!0,y.value=_},[_]),p(_),_},j}var P={};/**
* @license React
* use-sync-external-store-shim/with-selector.development.js
*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/var G;function ye(){return G||(G=1,process.env.NODE_ENV!=="production"&&function(){typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart=="function"&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStart(new Error);var n=d,e=L;function t(c,u){return c===u&&(c!==0||1/c===1/u)||c!==c&&u!==u}var r=typeof Object.is=="function"?Object.is:t,i=e.useSyncExternalStore,o=n.useRef,f=n.useEffect,s=n.useMemo,p=n.useDebugValue;function l(c,u,m,v,E){var y=o(null),_;y.current===null?(_={hasValue:!1,value:null},y.current=_):_=y.current;var R=s(function(){var b=!1,a,g,w=function(M){if(!b){b=!0,a=M;var x=v(M);if(E!==void 0&&_.hasValue){var N=_.value;if(E(N,x))return g=N,N}return g=x,x}var re=a,V=g;if(r(re,M))return V;var I=v(M);return E!==void 0&&E(V,I)?V:(a=M,g=I,I)},O=m===void 0?null:m,T=function(){return w(u())},A=O===null?void 0:function(){return w(O())};return[T,A]},[u,m,v,E]),D=R[0],S=R[1],h=i(c,D,S);return f(function(){_.hasValue=!0,_.value=h},[h]),p(h),h}P.useSyncExternalStoreWithSelector=l,typeof __REACT_DEVTOOLS_GLOBAL_HOOK__<"u"&&typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop=="function"&&__REACT_DEVTOOLS_GLOBAL_HOOK__.registerInternalModuleStop(new Error)}()),P}process.env.NODE_ENV==="production"?$.exports=Se():$.exports=ye();var Ee=$.exports;const _e=typeof window<"u"?d.useLayoutEffect:d.useEffect;class ge{constructor(e){this.transactionNumber=0,this.lastTransactionNumber=0,this.subscribers=new Set,this.editor=e,this.lastSnapshot={editor:e,transactionNumber:0},this.getSnapshot=this.getSnapshot.bind(this),this.getServerSnapshot=this.getServerSnapshot.bind(this),this.watch=this.watch.bind(this),this.subscribe=this.subscribe.bind(this)}getSnapshot(){return this.transactionNumber===this.lastTransactionNumber?this.lastSnapshot:(this.lastTransactionNumber=this.transactionNumber,this.lastSnapshot={editor:this.editor,transactionNumber:this.transactionNumber},this.lastSnapshot)}getServerSnapshot(){return{editor:null,transactionNumber:0}}subscribe(e){return this.subscribers.add(e),()=>{this.subscribers.delete(e)}}watch(e){if(this.editor=e,this.editor){const t=()=>{this.transactionNumber+=1,this.subscribers.forEach(i=>i())},r=this.editor;return r.on("transaction",t),()=>{r.off("transaction",t)}}}}function be(n){var e;const[t]=d.useState(()=>new ge(n.editor)),r=Ee.useSyncExternalStoreWithSelector(t.subscribe,t.getSnapshot,t.getServerSnapshot,n.selector,(e=n.equalityFn)!==null&&e!==void 0?e:ve);return _e(()=>t.watch(n.editor),[n.editor,t]),d.useDebugValue(r),r}const J=process.env.NODE_ENV!=="production",q=typeof window>"u",Oe=q||!!(typeof window<"u"&&window.next);class F{constructor(e){this.editor=null,this.subscriptions=new Set,this.isComponentMounted=!1,this.previousDeps=null,this.instanceId="",this.options=e,this.subscriptions=new Set,this.setEditor(this.getInitialEditor()),this.scheduleDestroy(),this.getEditor=this.getEditor.bind(this),this.getServerSnapshot=this.getServerSnapshot.bind(this),this.subscribe=this.subscribe.bind(this),this.refreshEditorInstance=this.refreshEditorInstance.bind(this),this.scheduleDestroy=this.scheduleDestroy.bind(this),this.onRender=this.onRender.bind(this),this.createEditor=this.createEditor.bind(this)}setEditor(e){this.editor=e,this.instanceId=Math.random().toString(36).slice(2,9),this.subscriptions.forEach(t=>t())}getInitialEditor(){if(this.options.current.immediatelyRender===void 0)return q||Oe?(J&&console.warn("Tiptap Error: SSR has been detected, please set `immediatelyRender` explicitly to `false` to avoid hydration mismatches."),null):this.createEditor();if(this.options.current.immediatelyRender&&q&&J)throw new Error("Tiptap Error: SSR has been detected, and `immediatelyRender` has been set to `true` this is an unsupported configuration that may result in errors, explicitly set `immediatelyRender` to `false` to avoid hydration mismatches.");return this.options.current.immediatelyRender?this.createEditor():null}createEditor(){const e={...this.options.current,onBeforeCreate:(...r)=>{var i,o;return(o=(i=this.options.current).onBeforeCreate)===null||o===void 0?void 0:o.call(i,...r)},onBlur:(...r)=>{var i,o;return(o=(i=this.options.current).onBlur)===null||o===void 0?void 0:o.call(i,...r)},onCreate:(...r)=>{var i,o;return(o=(i=this.options.current).onCreate)===null||o===void 0?void 0:o.call(i,...r)},onDestroy:(...r)=>{var i,o;return(o=(i=this.options.current).onDestroy)===null||o===void 0?void 0:o.call(i,...r)},onFocus:(...r)=>{var i,o;return(o=(i=this.options.current).onFocus)===null||o===void 0?void 0:o.call(i,...r)},onSelectionUpdate:(...r)=>{var i,o;return(o=(i=this.options.current).onSelectionUpdate)===null||o===void 0?void 0:o.call(i,...r)},onTransaction:(...r)=>{var i,o;return(o=(i=this.options.current).onTransaction)===null||o===void 0?void 0:o.call(i,...r)},onUpdate:(...r)=>{var i,o;return(o=(i=this.options.current).onUpdate)===null||o===void 0?void 0:o.call(i,...r)},onContentError:(...r)=>{var i,o;return(o=(i=this.options.current).onContentError)===null||o===void 0?void 0:o.call(i,...r)},onDrop:(...r)=>{var i,o;return(o=(i=this.options.current).onDrop)===null||o===void 0?void 0:o.call(i,...r)},onPaste:(...r)=>{var i,o;return(o=(i=this.options.current).onPaste)===null||o===void 0?void 0:o.call(i,...r)}};return new C.Editor(e)}getEditor(){return this.editor}getServerSnapshot(){return null}subscribe(e){return this.subscriptions.add(e),()=>{this.subscriptions.delete(e)}}static compareOptions(e,t){return Object.keys(e).every(r=>["onCreate","onBeforeCreate","onDestroy","onUpdate","onTransaction","onFocus","onBlur","onSelectionUpdate","onContentError","onDrop","onPaste"].includes(r)?!0:r==="extensions"&&e.extensions&&t.extensions?e.extensions.length!==t.extensions.length?!1:e.extensions.every((i,o)=>{var f;return i===((f=t.extensions)===null||f===void 0?void 0:f[o])}):e[r]===t[r])}onRender(e){return()=>(this.isComponentMounted=!0,clearTimeout(this.scheduledDestructionTimeout),this.editor&&!this.editor.isDestroyed&&e.length===0?F.compareOptions(this.options.current,this.editor.options)||this.editor.setOptions({...this.options.current,editable:this.editor.isEditable}):this.refreshEditorInstance(e),()=>{this.isComponentMounted=!1,this.scheduleDestroy()})}refreshEditorInstance(e){if(this.editor&&!this.editor.isDestroyed){if(this.previousDeps===null){this.previousDeps=e;return}if(this.previousDeps.length===e.length&&this.previousDeps.every((r,i)=>r===e[i]))return}this.editor&&!this.editor.isDestroyed&&this.editor.destroy(),this.setEditor(this.createEditor()),this.previousDeps=e}scheduleDestroy(){const e=this.instanceId,t=this.editor;this.scheduledDestructionTimeout=setTimeout(()=>{if(this.isComponentMounted&&this.instanceId===e){t&&t.setOptions(this.options.current);return}t&&!t.isDestroyed&&(t.destroy(),this.instanceId===e&&this.setEditor(null))},1)}}function we(n={},e=[]){const t=d.useRef(n);t.current=n;const[r]=d.useState(()=>new F(t)),i=L.useSyncExternalStore(r.subscribe,r.getEditor,r.getServerSnapshot);return d.useDebugValue(i),d.useEffect(r.onRender(e)),be({editor:i,selector:({transactionNumber:o})=>n.shouldRerenderOnTransaction===!1?null:n.immediatelyRender&&o===0?0:o+1}),i}const k=d.createContext({editor:null});k.Consumer;const De=()=>d.useContext(k),Ce=n=>{const[e,t]=d.useState(null),{editor:r}=De();return d.useEffect(()=>{var i;if(!e||!((i=n.editor)===null||i===void 0)&&i.isDestroyed||r!=null&&r.isDestroyed)return;const{pluginKey:o="bubbleMenu",editor:f,tippyOptions:s={},updateDelay:p,shouldShow:l=null}=n,c=f||r;if(!c){console.warn("BubbleMenu component is not rendered inside of an editor component or does not have editor prop.");return}const u=Z({updateDelay:p,editor:c,element:e,pluginKey:o,shouldShow:l,tippyOptions:s});return c.registerPlugin(u),()=>{c.unregisterPlugin(o)}},[n.editor,r,e]),d.createElement("div",{ref:t,className:n.className,style:{visibility:"hidden"}},n.children)},ee=d.createContext({onDragStart:void 0}),Re=()=>d.useContext(ee),Te=d.forwardRef((n,e)=>{const{onDragStart:t}=Re(),r=n.as||"div";return d.createElement(r,{...n,ref:e,"data-node-view-wrapper":"",onDragStart:t,style:{whiteSpace:"normal",...n.style}})});function Y(n){return!!(typeof n=="function"&&n.prototype&&n.prototype.isReactComponent)}function Q(n){return!!(typeof n=="object"&&n.$$typeof&&(n.$$typeof.toString()==="Symbol(react.forward_ref)"||n.$$typeof.description==="react.forward_ref"))}function Me(n){return!!(typeof n=="object"&&n.$$typeof&&(n.$$typeof.toString()==="Symbol(react.memo)"||n.$$typeof.description==="react.memo"))}function Le(n){if(Y(n)||Q(n))return!0;if(Me(n)){const e=n.type;if(e)return Y(e)||Q(e)}return!1}function Ae(){try{if(d.version)return parseInt(d.version.split(".")[0],10)>=19}catch{}return!1}class te{constructor(e,{editor:t,props:r={},as:i="div",className:o=""}){this.ref=null,this.id=Math.floor(Math.random()*4294967295).toString(),this.component=e,this.editor=t,this.props=r,this.element=document.createElement(i),this.element.classList.add("react-renderer"),o&&this.element.classList.add(...o.split(" ")),this.editor.isInitialized?X.flushSync(()=>{this.render()}):this.render()}render(){var e;const t=this.component,r=this.props,i=this.editor,o=Ae(),f=Le(t),s={...r};s.ref&&!(o||f)&&delete s.ref,!s.ref&&(o||f)&&(s.ref=p=>{this.ref=p}),this.reactElement=d.createElement(t,{...s}),(e=i==null?void 0:i.contentComponent)===null||e===void 0||e.setRenderer(this.id,this)}updateProps(e={}){this.props={...this.props,...e},this.render()}destroy(){var e;const t=this.editor;(e=t==null?void 0:t.contentComponent)===null||e===void 0||e.removeRenderer(this.id)}updateAttributes(e){Object.keys(e).forEach(t=>{this.element.setAttribute(t,e[t])})}}class xe extends C.NodeView{mount(){const e={editor:this.editor,node:this.node,decorations:this.decorations,innerDecorations:this.innerDecorations,view:this.view,selected:!1,extension:this.extension,HTMLAttributes:this.HTMLAttributes,getPos:()=>this.getPos(),updateAttributes:(l={})=>this.updateAttributes(l),deleteNode:()=>this.deleteNode(),ref:d.createRef()};if(!this.component.displayName){const l=c=>c.charAt(0).toUpperCase()+c.substring(1);this.component.displayName=l(this.extension.name)}const i={onDragStart:this.onDragStart.bind(this),nodeViewContentRef:l=>{l&&this.contentDOMElement&&l.firstChild!==this.contentDOMElement&&l.appendChild(this.contentDOMElement)}},o=this.component,f=d.memo(l=>d.createElement(ee.Provider,{value:i},d.createElement(o,l)));f.displayName="ReactNodeView",this.node.isLeaf?this.contentDOMElement=null:this.options.contentDOMElementTag?this.contentDOMElement=document.createElement(this.options.contentDOMElementTag):this.contentDOMElement=document.createElement(this.node.isInline?"span":"div"),this.contentDOMElement&&(this.contentDOMElement.dataset.nodeViewContentReact="",this.contentDOMElement.style.whiteSpace="inherit");let s=this.node.isInline?"span":"div";this.options.as&&(s=this.options.as);const{className:p=""}=this.options;this.handleSelectionUpdate=this.handleSelectionUpdate.bind(this),this.renderer=new te(f,{editor:this.editor,props:e,as:s,className:`node-${this.node.type.name} ${p}`.trim()}),this.editor.on("selectionUpdate",this.handleSelectionUpdate),this.updateElementAttributes()}get dom(){var e;if(this.renderer.element.firstElementChild&&!(!((e=this.renderer.element.firstElementChild)===null||e===void 0)&&e.hasAttribute("data-node-view-wrapper")))throw Error("Please use the NodeViewWrapper component for your node view.");return this.renderer.element}get contentDOM(){return this.node.isLeaf?null:this.contentDOMElement}handleSelectionUpdate(){const{from:e,to:t}=this.editor.state.selection,r=this.getPos();if(typeof r=="number")if(e<=r&&t>=r+this.node.nodeSize){if(this.renderer.props.selected)return;this.selectNode()}else{if(!this.renderer.props.selected)return;this.deselectNode()}}update(e,t,r){const i=o=>{this.renderer.updateProps(o),typeof this.options.attrs=="function"&&this.updateElementAttributes()};if(e.type!==this.node.type)return!1;if(typeof this.options.update=="function"){const o=this.node,f=this.decorations,s=this.innerDecorations;return this.node=e,this.decorations=t,this.innerDecorations=r,this.options.update({oldNode:o,oldDecorations:f,newNode:e,newDecorations:t,oldInnerDecorations:s,innerDecorations:r,updateProps:()=>i({node:e,decorations:t,innerDecorations:r})})}return e===this.node&&this.decorations===t&&this.innerDecorations===r||(this.node=e,this.decorations=t,this.innerDecorations=r,i({node:e,decorations:t,innerDecorations:r})),!0}selectNode(){this.renderer.updateProps({selected:!0}),this.renderer.element.classList.add("ProseMirror-selectednode")}deselectNode(){this.renderer.updateProps({selected:!1}),this.renderer.element.classList.remove("ProseMirror-selectednode")}destroy(){this.renderer.destroy(),this.editor.off("selectionUpdate",this.handleSelectionUpdate),this.contentDOMElement=null}updateElementAttributes(){if(this.options.attrs){let e={};if(typeof this.options.attrs=="function"){const t=this.editor.extensionManager.attributes,r=C.getRenderedAttributes(this.node,t);e=this.options.attrs({node:this.node,HTMLAttributes:r})}else e=this.options.attrs;this.renderer.updateAttributes(e)}}}function Ne(n,e){return t=>t.editor.contentComponent?new xe(n,t,e):{}}function ne(n){var e,t,r="";if(typeof n=="string"||typeof n=="number")r+=n;else if(typeof n=="object")if(Array.isArray(n)){var i=n.length;for(e=0;e<i;e++)n[e]&&(t=ne(n[e]))&&(r&&(r+=" "),r+=t)}else for(t in n)n[t]&&(r&&(r+=" "),r+=t);return r}function Ve(){for(var n,e,t=0,r="",i=arguments.length;t<i;t++)(n=arguments[t])&&(e=ne(n))&&(r&&(r+=" "),r+=e);return r}function Ie(n){try{return JSON.stringify(n)}catch{return JSON.stringify({})}}function He(n,e=!1){return t=>{const r=n.startsWith("data-")?n:`data-${n}`;let i=decodeURIComponent(t.getAttribute(r));if(i==null||typeof i=="string"&&i==="null")try{const s=t.outerHTML.match(/([\S\s])+?="([\S\s])+?"/g);s&&s.length>0&&(i=(s.map(l=>l.trim()).reduce((l,c)=>{const u=c.indexOf("="),m=[c.slice(0,u),c.slice(u+1).slice(1,-1)];return l[m[0]]=m[1],l},{})[n.toLowerCase()]||"").replaceAll(""",'"'))}catch(f){console.error("Error getDatasetAttribute ",f.message,t)}if(e)try{return JSON.parse(i)}catch{return{}}if(i.includes("%")||i.includes("auto"))return i;const o=Number.parseInt(i);return o!==o?i:o}}function Be(n){const{attrs:e}=n;return Object.keys(e).reduce((t,r)=>{const i=e[r];if(i==null)return t;let o="";return typeof i=="object"?o=Ie(i):o=i,t[r]=o,t},Object.create(null))}exports.BubbleMenu=Ce;exports.EditorContent=pe;exports.NodeViewWrapper=Te;exports.ReactNodeViewRenderer=Ne;exports.ReactRenderer=te;exports.clsx=Ve;exports.getDatasetAttribute=He;exports.nodeAttrsToDataset=Be;exports.useEditor=we;