@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 6.4 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.
*
*/
import{useLexicalComposerContext as t}from"@lexical/react/LexicalComposerContext";import{eventFiles as e}from"@lexical/rich-text";import{mergeRegister as n,isHTMLElement as r,calculateZoomLevel as o}from"@lexical/utils";import{DRAGOVER_COMMAND as i,COMMAND_PRIORITY_LOW as s,DROP_COMMAND as l,COMMAND_PRIORITY_HIGH as a,$getNodeByKey as c,$getNearestNodeFromDOMNode as u,$getRoot as h}from"lexical";import{useRef as m,useState as g,useCallback as f,useEffect as p}from"react";import{createPortal as d}from"react-dom";import{jsxs as y,Fragment as _,jsx as x}from"react/jsx-runtime";class b{_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 T{_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 b){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 T(t,e,n,r)}static fromLTRB(t,e,n,r){return new T(t,e,n,r)}static fromLWTH(t,e,n,r){return new T(t,n,t+e,n+r)}static fromPoints(t,e){const{y:n,x:r}=t,{y:o,x:i}=e;return T.fromLTRB(r,n,i,o)}static fromDOM(t){const{top:e,width:n,left:r,height:o}=t.getBoundingClientRect();return T.fromLWTH(r,n,e,o)}}const B="application/x-lexical-drag-block";let C=1/0;function w(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 v(t,e,n,r=!1){const i=t.getBoundingClientRect(),s=function(t){return t.getEditorState().read(()=>h().getChildrenKeys())}(e);let l=null;return e.getEditorState().read(()=>{if(r){const[t,r]=[e.getElementByKey(s[0]),e.getElementByKey(s[s.length-1])],[i,a]=[null!=t?t.getBoundingClientRect():void 0,null!=r?r.getBoundingClientRect():void 0];if(i&&a){const e=o(t),s=o(r);if(n.y/e<i.top?l=t:n.y/s>a.bottom&&(l=r),l)return}}let t=0===(a=s.length)?1/0:C>=0&&C<a?C:Math.floor(a/2);var a;let c=0;for(;t>=0&&t<s.length;){const r=s[t],a=e.getElementByKey(r);if(null===a)break;const u=o(a),h=new b(n.x/u,n.y/u),m=T.fromDOM(a),{marginTop:g,marginBottom:f}=w(a),p=m.generateNewRect({bottom:m.bottom+f,left:i.left,right:i.right,top:m.top-g}),{result:d,reason:{isOnTopSide:y,isOnBottomSide:_}}=p.contains(h);if(d){l=a,C=t;break}0===c&&(c=y?-1:_?1:1/0),t+=c}}),l}function D(t,h,b,T,C,D,R,E,S){const M=h.parentElement,L=m(!1),[O,K]=g(null),Y=f(t=>{K(t),S&&S(t)},[S]);return p(()=>{function e(e){const n=e.target;if(!r(n))return void Y(null);if(E(n))return;const o=v(h,t,e);Y(o)}function n(){Y(null)}return null!=M&&(M.addEventListener("mousemove",e),M.addEventListener("mouseleave",n)),()=>{null!=M&&(M.removeEventListener("mousemove",e),M.removeEventListener("mouseleave",n))}},[M,h,t,E,Y]),p(()=>{b.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)`}(O,b.current,h)},[h,O,b]),p(()=>{function m(n){if(!L.current)return!1;const[i]=e(n);if(i)return!1;const{pageY:s,target:l}=n;if(!r(l))return!1;const a=v(h,t,n,!0),c=T.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}=w(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,s/o(l),h),n.preventDefault(),!0)}return n(t.registerCommand(i,t=>m(t),s),t.registerCommand(l,n=>function(n){if(!L.current)return!1;const[i]=e(n);if(i)return!1;const{target:s,dataTransfer:l,pageY:a}=n,m=null!=l?l.getData(B):"",g=c(m);if(!g)return!1;if(!r(s))return!1;const f=v(h,t,n,!0);if(!f)return!1;const p=u(f);if(!p)return!1;if(p===g)return!0;const d=f.getBoundingClientRect().top;return a/o(s)>=d?p.insertAfter(g):p.insertBefore(g),Y(null),!0}(n),a))},[h,t,T,Y]),d(y(_,{children:[x("div",{draggable:!0,onDragStart:function(e){const n=e.dataTransfer;if(!n||!O)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,O);let r="";t.update(()=>{const t=u(O);t&&(r=t.getKey())}),L.current=!0,n.setData(B,r)},onDragEnd:function(){var t;L.current=!1,(t=T.current)&&(t.style.opacity="0",t.style.transform="translate(-10000px, -10000px)")},children:C&&D}),R]}),h)}function R({anchorElem:e=document.body,menuRef:n,targetLineRef:r,menuComponent:o,targetLineComponent:i,isOnMenu:s,onElementChanged:l}){const[a]=t();return D(a,e,n,r,a._editable,o,i,s,l)}export{R as DraggableBlockPlugin_EXPERIMENTAL};