UNPKG

@blocknote/react

Version:

A "Notion-style" block-based extensible text editor built on top of Prosemirror and Tiptap.

2 lines 8.92 kB
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,n)=>{let r={};for(var i in e)t(r,i,{get:e[i],enumerable:!0});return n||t(r,Symbol.toStringTag,{value:`Module`}),r},s=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},c=(n,r,o)=>(o=n==null?{}:e(i(n)),s(r||!n||!n.__esModule||!a.call(n,`default`)?t(o,`default`,{value:n,enumerable:!0}):o,n));let l=require("react"),u=require("@floating-ui/react"),d=require("use-sync-external-store/shim/with-selector"),f=require("@blocknote/core"),p=require("react/jsx-runtime"),m=require("fast-deep-equal/es6/react.js");m=c(m,1);let h=require("@tiptap/core");var g=(0,l.createContext)(void 0);function _(e){return(0,l.useContext)(g)}function v(e){let t=_(e);if(!t?.editor)throw Error(`useBlockNoteEditor was called outside of a BlockNoteContext provider or BlockNoteView component`);return t.editor}function y(e,t=e=>e){return(0,d.useSyncExternalStoreWithSelector)(e.subscribe,()=>e.state,()=>e.state,t,b)}function b(e,t){if(Object.is(e,t))return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t)return!1;if(e instanceof Map&&t instanceof Map){if(e.size!==t.size)return!1;for(let[n,r]of e)if(!t.has(n)||!Object.is(r,t.get(n)))return!1;return!0}if(e instanceof Set&&t instanceof Set){if(e.size!==t.size)return!1;for(let n of e)if(!t.has(n))return!1;return!0}if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();let n=x(e);return n.length===x(t).length&&n.every(n=>Object.prototype.hasOwnProperty.call(t,n)&&Object.is(e[n],t[n]))}function x(e){return Object.keys(e).concat(Object.getOwnPropertySymbols(e))}function S(e,t){let n=(t?.editor??v()).getExtension(e);if(!n)throw Error(`Extension not found`,{cause:{plugin:e}});return n}function C(e,t){let{store:n}=S(e,t);if(!n)throw Error(`Store not found on plugin`,{cause:{plugin:e}});return y(n,t?.selector)}function w(e){let t=new DOMRect,n=`getBoundingClientRect`in e?()=>e.getBoundingClientRect():()=>e.element.getBoundingClientRect();return()=>e.element&&(e.cacheMountedBoundingClientRect??!0)?(e.element.isConnected&&(t=n()),t):n()}function T(e,t){return e?t?(n,r,i)=>{let a=e(n,r,i),o=t(n,r,i);return()=>{a?.(),o?.()}}:e:t}var E=e=>{let t=v(),n=e.portalElement===null?typeof document<`u`?document.body:void 0:e.portalElement??t?.portalElement;if(!n)throw Error(`Portal element not found`);let{whileElementsMounted:r,...i}=e.useFloatingOptions??{},{refs:a,floatingStyles:o,context:s}=(0,u.useFloating)({whileElementsMounted:T(u.autoUpdate,e.useFloatingOptions?.whileElementsMounted),...i}),{isMounted:c,styles:d}=(0,u.useTransitionStyles)(s,e.useTransitionStylesProps),{status:f}=(0,u.useTransitionStatus)(s,e.useTransitionStatusProps),m=(0,u.useDismiss)(s,e.useDismissProps),h=(0,u.useHover)(s,{enabled:!1,...e.useHoverProps}),{getFloatingProps:g}=(0,u.useInteractions)([m,h]),_=(0,l.useRef)(``),y=(0,l.useRef)(null),b=(0,u.useMergeRefs)([y,a.setFloating]);if((0,l.useEffect)(()=>{if(e.reference){let n=`element`in e.reference?e.reference.element:void 0;n!==void 0&&(e.focusManagerProps?.disabled||!t.isWithinEditor(n))&&a.setReference(n);let r=`getClientRects`in e.reference?e.reference.getClientRects:void 0;a.setPositionReference({getBoundingClientRect:w(e.reference),...r?{getClientRects:r}:{},contextElement:n})}},[e.reference,a,e.focusManagerProps?.disabled,t]),(0,l.useEffect)(()=>{(f===`initial`||f===`open`)&&y.current?.innerHTML&&(_.current=y.current.innerHTML)},[f,e.reference,e.children]),!c)return!1;let x={...e.elementProps,style:{display:`flex`,...e.elementProps?.style,zIndex:`calc(var(--bn-ui-base-z-index, 0) + ${e.elementProps?.style?.zIndex||0})`,...o,...d},...g()};return f===`close`?(0,p.jsx)(u.FloatingPortal,{root:n,children:(0,p.jsx)(`div`,{ref:b,...x,dangerouslySetInnerHTML:{__html:_.current}})}):e.focusManagerProps?.disabled?(0,p.jsx)(u.FloatingPortal,{root:n,children:(0,p.jsx)(`div`,{ref:b,...x,children:e.children})}):(0,p.jsx)(u.FloatingPortal,{root:n,children:(0,p.jsx)(u.FloatingFocusManager,{...e.focusManagerProps,context:s,children:(0,p.jsx)(`div`,{ref:b,...x,children:e.children})})})},D=(0,l.createContext)(void 0);function O(){return(0,l.useContext)(D)}function k(){return _().editor.dictionary}var A=typeof window<`u`?l.useLayoutEffect:l.useEffect,j=class{transactionNumber=0;lastTransactionNumber=0;lastSnapshot;editor;subscribers=new Set;constructor(e){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,t){if(this.editor=e,this.editor){let e=()=>{this.transactionNumber+=1,this.subscribers.forEach(e=>e())},n=this.editor._tiptapEditor,r={all:[`transaction`,`create`,`mount`,`unmount`],mount:[`create`,`mount`,`unmount`],selection:[`selectionUpdate`],change:[`update`]};for(let i of r[t])n.on(i,e);return()=>{for(let i of r[t])n.off(i,e)}}}};function M(e){let t=_(),n=e.editor||t?.editor||null,r=e.on||`all`,[i]=(0,l.useState)(()=>new j(n)),a=(0,d.useSyncExternalStoreWithSelector)(i.subscribe,i.getSnapshot,i.getServerSnapshot,e.selector,e.equalityFn??m.default);return A(()=>i.watch(n,r),[n,i,r]),(0,l.useDebugValue)(a),a}function N(e){let t=_();return e||=t?.editor,M({editor:e,selector:e=>e.editor?.domElement,equalityFn:(e,t)=>e===t,on:`mount`})}var P=e=>{let{position:t,children:n,portalElement:r,...i}=e,{from:a,to:o}=t||{},s=v(),c=N(),u=(0,l.useMemo)(()=>{if(a!==void 0&&o!==void 0)return{element:c?.firstElementChild||void 0,getBoundingClientRect:()=>(0,h.posToDOMRect)(s.prosemirrorView,a,o??a)}},[s,c,a,o]);return(0,p.jsx)(E,{reference:u,portalElement:r,...i,children:t!==void 0&&n})},F=(e={},t=[])=>(0,l.useMemo)(()=>{let t=f.BlockNoteEditor.create(e);return window&&(window.ProseMirror=t._tiptapEditor),t},t),I=e=>{let[t,n]=(0,l.useState)(!1),r=M({editor:e.editor,selector:({editor:e})=>e.isEmpty}),i=O(),a=(0,l.useCallback)(()=>{n(!0)},[]),o=(0,l.useCallback)(()=>{n(!1)},[]);return(0,l.useEffect)(()=>{e.editable&&e.autoFocus&&e.editor.focus()},[e.autoFocus,e.editable,e.editor]),(0,p.jsxs)(p.Fragment,{children:[(0,p.jsx)(i.Comments.Editor,{autoFocus:e.autoFocus,className:`bn-comment-editor`,editor:e.editor,onFocus:a,onBlur:o,editable:e.editable}),e.actions&&(0,p.jsx)(`div`,{className:`bn-comment-actions-wrapper`,children:e.actions({isFocused:t,isEmpty:r})})]})},{textColor:L,backgroundColor:R,...z}=f.defaultStyleSpecs,B=f.BlockNoteSchema.create({blockSpecs:{paragraph:(0,f.createParagraphBlockSpec)()},styleSpecs:z});function V(e){return!e.hasUnsavedContent||!e.confirmBeforeDiscard||(e.confirm??(e=>window.confirm(e)))(e.message)}Object.defineProperty(exports,"_",{enumerable:!0,get:function(){return v}}),Object.defineProperty(exports,"a",{enumerable:!0,get:function(){return P}}),Object.defineProperty(exports,"b",{enumerable:!0,get:function(){return o}}),Object.defineProperty(exports,"c",{enumerable:!0,get:function(){return k}}),Object.defineProperty(exports,"d",{enumerable:!0,get:function(){return E}}),Object.defineProperty(exports,"f",{enumerable:!0,get:function(){return w}}),Object.defineProperty(exports,"g",{enumerable:!0,get:function(){return y}}),Object.defineProperty(exports,"h",{enumerable:!0,get:function(){return b}}),Object.defineProperty(exports,"i",{enumerable:!0,get:function(){return F}}),Object.defineProperty(exports,"l",{enumerable:!0,get:function(){return D}}),Object.defineProperty(exports,"m",{enumerable:!0,get:function(){return C}}),Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return B}}),Object.defineProperty(exports,"o",{enumerable:!0,get:function(){return N}}),Object.defineProperty(exports,"p",{enumerable:!0,get:function(){return S}}),Object.defineProperty(exports,"r",{enumerable:!0,get:function(){return I}}),Object.defineProperty(exports,"s",{enumerable:!0,get:function(){return M}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return V}}),Object.defineProperty(exports,"u",{enumerable:!0,get:function(){return O}}),Object.defineProperty(exports,"v",{enumerable:!0,get:function(){return g}}),Object.defineProperty(exports,"x",{enumerable:!0,get:function(){return c}}),Object.defineProperty(exports,"y",{enumerable:!0,get:function(){return _}}); //# sourceMappingURL=confirmDiscardUnsavedComment-BoTC-g7c.cjs.map