@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 7.5 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 e}from"@lexical/react/LexicalComposerContext";import{createCommand as t,COMMAND_PRIORITY_LOW as n,KEY_ARROW_DOWN_COMMAND as o,KEY_ARROW_UP_COMMAND as l,KEY_ESCAPE_COMMAND as r,KEY_TAB_COMMAND as i,KEY_ENTER_COMMAND as u,$getSelection as s,$isRangeSelection as c,$getNodeByKey as a}from"lexical";import m,{useLayoutEffect as d,useEffect as p,useRef as f,useCallback as g,useState as h,useMemo as y}from"react";import{mergeRegister as v}from"@lexical/utils";import w from"react-dom";import{jsx as b,jsxs as C}from"react/jsx-runtime";const x="startTransition";const E="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement,R=E?d:p;class I{key;ref;icon;title;constructor(e){this.key=e,this.ref={current:null},this.setRefElement=this.setRefElement.bind(this)}setRefElement(e){this.ref={current:e}}}const O=e=>{const t=document.getElementById("typeahead-menu");if(!t)return;const n=t.getBoundingClientRect();n.top+n.height>window.innerHeight&&t.scrollIntoView({block:"center"}),n.top<0&&t.scrollIntoView({block:"center"}),e.scrollIntoView({block:"nearest"})};function S(e,t){const n=e.getBoundingClientRect(),o=t.getBoundingClientRect();return n.top>=o.top-6&&n.top<=o.bottom+6}function A(t,n,o,l){const[r]=e();p(()=>{if(null!=n&&null!=t){const e=r.getRootElement(),t=null!=e?function(e){let t=getComputedStyle(e);const n="absolute"===t.position,o=/(auto|scroll)/;if("fixed"===t.position)return document.body;for(let l=e;l=l.parentElement;)if(t=getComputedStyle(l),(!n||"static"!==t.position)&&o.test(t.overflow+t.overflowY+t.overflowX))return l;return document.body}(e):document.body;let i=!1,u=S(n,t);const s=function(){i||(window.requestAnimationFrame(function(){o(),i=!1}),i=!0);const e=S(n,t);e!==u&&(u=e,null!=l&&l(e))},c=new ResizeObserver(o);return window.addEventListener("resize",o),document.addEventListener("scroll",s,{capture:!0,passive:!0}),c.observe(n),()=>{c.unobserve(n),window.removeEventListener("resize",o),document.removeEventListener("scroll",s,!0)}}},[n,r,l,o,t])}const P=t("SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND");function k({index:e,isSelected:t,onClick:n,onMouseEnter:o,option:l}){let r="item";return t&&(r+=" selected"),C("li",{tabIndex:-1,className:r,ref:l.setRefElement,role:"option","aria-selected":t,id:"typeahead-item-"+e,onMouseEnter:o,onClick:n,children:[l.icon,b("span",{className:"text",children:l.title})]},l.key)}function N({close:e,editor:t,anchorElementRef:a,resolution:m,options:d,menuRenderFn:f,onSelectOption:C,shouldSplitNodeWithQuery:x=!1,commandPriority:E=n,preselectFirstItem:I=!0}){const[S,A]=h(null),N=null!==S?Math.min(d.length-1,S):null,D=m.match&&m.match.matchingString;p(()=>{I&&A(0)},[D,I]);const T=g(n=>{t.update(()=>{const t=null!=m.match&&x?function(e){const t=s();if(!c(t)||!t.isCollapsed())return null;const n=t.anchor;if("text"!==n.type)return null;const o=n.getNode();if(!o.isSimpleText())return null;const l=n.offset,r=o.getTextContent().slice(0,l),i=e.replaceableString.length,u=l-function(e,t,n){let o=n;for(let n=o;n<=t.length;n++)e.slice(-n)===t.substring(0,n)&&(o=n);return o}(r,e.matchingString,i);if(u<0)return null;let a;return 0===u?[a]=o.splitText(l):[,a]=o.splitText(u,l),a}(m.match):null;C(n,t,e,m.match?m.match.matchingString:"")})},[t,x,m.match,C,e]),B=g(e=>{const n=t.getRootElement();null!==n&&(n.setAttribute("aria-activedescendant","typeahead-item-"+e),A(e))},[t]),L=g(()=>a.current&&d.length?w.createPortal(b("div",{className:"typeahead-popover mentions-menu",children:b("ul",{children:d.map((e,t)=>b(k,{index:t,isSelected:N===t,onClick:()=>{A(t),T(e)},onMouseEnter:()=>{A(t)},option:e},e.key))})}),a.current):null,[a,d,N,T,A]);p(()=>()=>{const e=t.getRootElement();null!==e&&e.removeAttribute("aria-activedescendant")},[t]),R(()=>{null===d?A(null):null===N&&I&&B(0)},[d,N,B,I]),p(()=>v(t.registerCommand(P,({option:e})=>!(!e.ref||null==e.ref.current)&&(O(e.ref.current),!0),E)),[t,B,E]),p(()=>v(t.registerCommand(o,e=>{const n=e;if(null!==d&&d.length){const e=null===N?0:N!==d.length-1?N+1:0;B(e);const o=d[e];if(!o)return B(-1),n.preventDefault(),n.stopImmediatePropagation(),!0;o.ref&&o.ref.current&&t.dispatchCommand(P,{index:e,option:o}),n.preventDefault(),n.stopImmediatePropagation()}return!0},E),t.registerCommand(l,e=>{const t=e;if(null!==d&&d.length){const e=null===N?d.length-1:0!==N?N-1:d.length-1;B(e);const n=d[e];if(!n)return B(-1),t.preventDefault(),t.stopImmediatePropagation(),!0;n.ref&&n.ref.current&&O(n.ref.current),t.preventDefault(),t.stopImmediatePropagation()}return!0},E),t.registerCommand(r,t=>{const n=t;return n.preventDefault(),n.stopImmediatePropagation(),e(),!0},E),t.registerCommand(i,e=>{const t=e;return null!==d&&null!==N&&null!=d[N]&&(t.preventDefault(),t.stopImmediatePropagation(),T(d[N]),!0)},E),t.registerCommand(u,e=>null!==d&&null!==N&&null!=d[N]&&(null!==e&&(e.preventDefault(),e.stopImmediatePropagation()),T(d[N]),!0),E)),[T,e,t,d,N,B,E]);const $=y(()=>({options:d,selectOptionAndCleanUp:T,selectedIndex:N,setHighlightedIndex:A}),[T,N,d]);return null!=f?f(a,$,m.match?m.match.matchingString:""):L()}function D(e,t){null!=t&&(e.className=t),e.setAttribute("aria-label","Typeahead menu"),e.setAttribute("role","listbox"),e.style.display="block",e.style.position="absolute"}function T({options:t,nodeKey:o,onClose:l,onOpen:r,onSelectOption:i,menuRenderFn:u,anchorClassName:s,commandPriority:c=n,parent:d}){const[y]=e(),[v,w]=h(null),C=function(t,n,o,l=(E?document.body:void 0),r=!0){const[i]=e(),u=E?document.createElement("div"):null,s=f(u),c=g(()=>{if(null===s.current||void 0===l)return;s.current.style.top=s.current.style.bottom;const e=i.getRootElement(),n=s.current,u=n.firstChild;if(null!==e&&null!==t){const{left:i,top:c,width:a,height:m}=t.getRect(),d=s.current.offsetHeight;if(n.style.top=`${c+d+3+(r?window.pageYOffset:0)}px`,n.style.left=`${i+window.pageXOffset}px`,n.style.height=`${m}px`,n.style.width=`${a}px`,null!==u){u.style.top=`${c}`;const t=u.getBoundingClientRect(),o=t.height,l=t.width,s=e.getBoundingClientRect();i+l>s.right&&(n.style.left=`${s.right-l+window.pageXOffset}px`),(c+o>window.innerHeight||c+o>s.bottom)&&c-s.top>o+m&&(n.style.top=`${c-o-m+(r?window.pageYOffset:0)}px`)}n.isConnected||(D(n,o),l.append(n)),n.setAttribute("id","typeahead-menu"),e.setAttribute("aria-controls","typeahead-menu")}},[i,t,r,o,l]);p(()=>{const e=i.getRootElement();return null!==t&&c(),()=>{null!==e&&e.removeAttribute("aria-controls");const t=s.current;null!==t&&t.isConnected&&(t.remove(),t.removeAttribute("id"))}},[i,c,t]);const a=g(e=>{null!==t&&(e||n(null))},[t,n]);return A(t,s.current,c,a),null!=u&&u===s.current&&(D(u,o),null!=l&&l.append(u)),s}(v,w,s,d),R=g(()=>{w(null),null!=l&&null!==v&&l()},[l,v]),I=g(e=>{w(e),null!=r&&null===v&&r(e)},[r,v]),O=g(()=>{o?y.update(()=>{const e=a(o),t=y.getElementByKey(o);var n;null!=e&&null!=t&&null==v&&(n=()=>I({getRect:()=>t.getBoundingClientRect()}),x in m?m[x](n):n())}):null==o&&null!=v&&R()},[R,y,o,I,v]);return p(()=>{O()},[O,o]),p(()=>{if(null!=o)return y.registerUpdateListener(({dirtyElements:e})=>{e.get(o)&&O()})},[y,O,o]),null===C.current||null===v||null===y?null:b(N,{close:R,resolution:v,editor:y,anchorElementRef:C,options:t,menuRenderFn:u,onSelectOption:i,commandPriority:c})}export{T as LexicalNodeMenuPlugin,I as MenuOption};