@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 6.47 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
"use strict";var t=require("@lexical/react/LexicalComposerContext"),e=require("@lexical/rich-text"),n=require("@lexical/utils"),r=require("lexical"),o=require("react"),i=require("react-dom"),s=require("react/jsx-runtime");class l{_x;_y;constructor(t,e){this._x=t,this._y=e}get x(){return this._x}get y(){return this._y}equals({x:t,y:e}){return this.x===t&&this.y===e}calcDeltaXTo({x:t}){return this.x-t}calcDeltaYTo({y:t}){return this.y-t}calcHorizontalDistanceTo(t){return Math.abs(this.calcDeltaXTo(t))}calcVerticalDistance(t){return Math.abs(this.calcDeltaYTo(t))}calcDistanceTo(t){return Math.sqrt(Math.pow(this.calcDeltaXTo(t),2)+Math.pow(this.calcDeltaYTo(t),2))}}class a{_left;_top;_right;_bottom;constructor(t,e,n,r){const[o,i]=e<=r?[e,r]:[r,e],[s,l]=t<=n?[t,n]:[n,t];this._top=o,this._right=l,this._left=s,this._bottom=i}get top(){return this._top}get right(){return this._right}get bottom(){return this._bottom}get left(){return this._left}get width(){return Math.abs(this._left-this._right)}get height(){return Math.abs(this._bottom-this._top)}equals({top:t,left:e,bottom:n,right:r}){return t===this._top&&n===this._bottom&&e===this._left&&r===this._right}contains(t){if(t instanceof l){const{x:e,y:n}=t,r=n<this._top,o=n>this._bottom,i=e<this._left,s=e>this._right;return{reason:{isOnBottomSide:o,isOnLeftSide:i,isOnRightSide:s,isOnTopSide:r},result:!(r||o||i||s)}}{const{top:e,left:n,bottom:r,right:o}=t;return e>=this._top&&e<=this._bottom&&r>=this._top&&r<=this._bottom&&n>=this._left&&n<=this._right&&o>=this._left&&o<=this._right}}intersectsWith(t){const{left:e,top:n,width:r,height:o}=t,{left:i,top:s,width:l,height:a}=this;return(e+r>=i+l?e+r:i+l)-(e<=i?e:i)<=r+l&&(n+o>=s+a?n+o:s+a)-(n<=s?n:s)<=o+a}generateNewRect({left:t=this.left,top:e=this.top,right:n=this.right,bottom:r=this.bottom}){return new a(t,e,n,r)}static fromLTRB(t,e,n,r){return new a(t,e,n,r)}static fromLWTH(t,e,n,r){return new a(t,n,t+e,n+r)}static fromPoints(t,e){const{y:n,x:r}=t,{y:o,x:i}=e;return a.fromLTRB(r,n,i,o)}static fromDOM(t){const{top:e,width:n,left:r,height:o}=t.getBoundingClientRect();return a.fromLWTH(r,n,e,o)}}const c="application/x-lexical-drag-block";let u=1/0;function g(t){const e=(t,e)=>t?parseFloat(window.getComputedStyle(t)[e]):0,{marginTop:n,marginBottom:r}=window.getComputedStyle(t),o=e(t.previousElementSibling,"marginBottom"),i=e(t.nextElementSibling,"marginTop"),s=Math.max(parseFloat(n),o);return{marginBottom:Math.max(parseFloat(r),i),marginTop:s}}function h(t,e,o,i=!1){const s=t.getBoundingClientRect(),c=function(t){return t.getEditorState().read(()=>r.$getRoot().getChildrenKeys())}(e);let h=null;return e.getEditorState().read(()=>{if(i){const[t,r]=[e.getElementByKey(c[0]),e.getElementByKey(c[c.length-1])],[i,s]=[null!=t?t.getBoundingClientRect():void 0,null!=r?r.getBoundingClientRect():void 0];if(i&&s){const e=n.calculateZoomLevel(t),l=n.calculateZoomLevel(r);if(o.y/e<i.top?h=t:o.y/l>s.bottom&&(h=r),h)return}}let t=0===(r=c.length)?1/0:u>=0&&u<r?u:Math.floor(r/2);var r;let m=0;for(;t>=0&&t<c.length;){const r=c[t],i=e.getElementByKey(r);if(null===i)break;const f=n.calculateZoomLevel(i),p=new l(o.x/f,o.y/f),d=a.fromDOM(i),{marginTop:_,marginBottom:y}=g(i),x=d.generateNewRect({bottom:d.bottom+y,left:s.left,right:s.right,top:d.top-_}),{result:b,reason:{isOnTopSide:T,isOnBottomSide:C}}=x.contains(p);if(b){h=i,u=t;break}0===m&&(m=T?-1:C?1:1/0),t+=m}}),h}function m(t,l,a,u,m,f,p,d,_){const y=l.parentElement,x=o.useRef(!1),[b,T]=o.useState(null),C=o.useCallback(t=>{T(t),_&&_(t)},[_]);return o.useEffect(()=>{function e(e){const r=e.target;if(!n.isHTMLElement(r))return void C(null);if(d(r))return;const o=h(l,t,e);C(o)}function r(){C(null)}return null!=y&&(y.addEventListener("mousemove",e),y.addEventListener("mouseleave",r)),()=>{null!=y&&(y.removeEventListener("mousemove",e),y.removeEventListener("mouseleave",r))}},[y,l,t,d,C]),o.useEffect(()=>{a.current&&function(t,e,n){if(!t)return e.style.opacity="0",void(e.style.transform="translate(-10000px, -10000px)");const r=t.getBoundingClientRect(),o=window.getComputedStyle(t),i=e.getBoundingClientRect(),s=n.getBoundingClientRect();let l=parseInt(o.lineHeight,10);isNaN(l)&&(l=r.bottom-r.top);const a=r.top+(l-i.height)/2-s.top+n.scrollTop;e.style.opacity="1",e.style.transform=`translate(4px, ${a}px)`}(b,a.current,l)},[l,b,a]),o.useEffect(()=>{function o(r){if(!x.current)return!1;const[o]=e.eventFiles(r);if(o)return!1;const{pageY:i,target:s}=r;if(!n.isHTMLElement(s))return!1;const a=h(l,t,r,!0),c=u.current;return null!==a&&null!==c&&(function(t,e,n,r){const{top:o,height:i}=e.getBoundingClientRect(),{top:s,width:l}=r.getBoundingClientRect(),{marginTop:a,marginBottom:c}=g(e);let u=o;n>=o?u+=i+c/2:u-=a/2;const h=u-s-2+r.scrollTop;t.style.transform=`translate(24px, ${h}px)`,t.style.width=l-48+"px",t.style.opacity=".4"}(c,a,i/n.calculateZoomLevel(s),l),r.preventDefault(),!0)}return n.mergeRegister(t.registerCommand(r.DRAGOVER_COMMAND,t=>o(t),r.COMMAND_PRIORITY_LOW),t.registerCommand(r.DROP_COMMAND,o=>function(o){if(!x.current)return!1;const[i]=e.eventFiles(o);if(i)return!1;const{target:s,dataTransfer:a,pageY:u}=o,g=null!=a?a.getData(c):"",m=r.$getNodeByKey(g);if(!m)return!1;if(!n.isHTMLElement(s))return!1;const f=h(l,t,o,!0);if(!f)return!1;const p=r.$getNearestNodeFromDOMNode(f);if(!p)return!1;if(p===m)return!0;const d=f.getBoundingClientRect().top;return u/n.calculateZoomLevel(s)>=d?p.insertAfter(m):p.insertBefore(m),C(null),!0}(o),r.COMMAND_PRIORITY_HIGH))},[l,t,u,C]),i.createPortal(s.jsxs(s.Fragment,{children:[s.jsx("div",{draggable:!0,onDragStart:function(e){const n=e.dataTransfer;if(!n||!b)return;!function(t,e){const{transform:n}=e.style;e.style.transform="translateZ(0)",t.setDragImage(e,0,0),setTimeout(()=>{e.style.transform=n})}(n,b);let o="";t.update(()=>{const t=r.$getNearestNodeFromDOMNode(b);t&&(o=t.getKey())}),x.current=!0,n.setData(c,o)},onDragEnd:function(){var t;x.current=!1,(t=u.current)&&(t.style.opacity="0",t.style.transform="translate(-10000px, -10000px)")},children:m&&f}),p]}),l)}exports.DraggableBlockPlugin_EXPERIMENTAL=function({anchorElem:e=document.body,menuRef:n,targetLineRef:r,menuComponent:o,targetLineComponent:i,isOnMenu:s,onElementChanged:l}){const[a]=t.useLexicalComposerContext();return m(a,e,n,r,a._editable,o,i,s,l)};