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