@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
27 lines (25 loc) • 8.29 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.
*
*/
;var m=require("@lexical/react/LexicalComposerContext"),p=require("lexical"),z=require("react"),A=require("@lexical/utils"),B=require("react/jsx-runtime");function C(b){"startTransition"in z?z.startTransition(b):b()}let D="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement?z.useLayoutEffect:z.useEffect;
class E{constructor(b){this.key=b;this.ref={current:null};this.setRefElement=this.setRefElement.bind(this)}setRefElement(b){this.ref={current:b}}}let H=b=>{const a=document.getElementById("typeahead-menu");if(a){var c=a.getBoundingClientRect();c.top+c.height>window.innerHeight&&a.scrollIntoView({block:"center"});0>c.top&&a.scrollIntoView({block:"center"});b.scrollIntoView({block:"nearest"})}};
function I(b){var a=p.$getSelection();if(!p.$isRangeSelection(a)||!a.isCollapsed())return null;var c=a.anchor;if("text"!==c.type)return null;a=c.getNode();if(!a.isSimpleText())return null;c=c.offset;let d=a.getTextContent().slice(0,c);var g=b.matchingString;b=b.replaceableString.length;for(let l=b;l<=g.length;l++)d.substr(-l)===g.substr(0,l)&&(b=l);b=c-b;if(0>b)return null;let q;0===b?[q]=a.splitText(c):[,q]=a.splitText(b,c);return q}
function J(b){let a=getComputedStyle(b),c="absolute"===a.position,d=/(auto|scroll)/;if("fixed"===a.position)return document.body;for(;b=b.parentElement;)if(a=getComputedStyle(b),(!c||"static"!==a.position)&&d.test(a.overflow+a.overflowY+a.overflowX))return b;return document.body}function K(b,a){b=b.getBoundingClientRect();a=a.getBoundingClientRect();return b.top>a.top&&b.top<a.bottom}
function L(b,a,c,d){let [g]=m.useLexicalComposerContext();z.useEffect(()=>{if(null!=a&&null!=b){let q=g.getRootElement(),l=null!=q?J(q):document.body,u=!1,t=K(a,l),h=function(){u||(window.requestAnimationFrame(function(){c();u=!1}),u=!0);const k=K(a,l);k!==t&&(t=k,null!=d&&d(k))},e=new ResizeObserver(c);window.addEventListener("resize",c);document.addEventListener("scroll",h,{capture:!0,passive:!0});e.observe(a);return()=>{e.unobserve(a);window.removeEventListener("resize",c);document.removeEventListener("scroll",
h,!0)}}},[a,g,d,c,b])}let M=p.createCommand("SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND");
function N({close:b,editor:a,anchorElementRef:c,resolution:d,options:g,menuRenderFn:q,onSelectOption:l,shouldSplitNodeWithQuery:u=!1,commandPriority:t=p.COMMAND_PRIORITY_LOW}){let [h,e]=z.useState(null);z.useEffect(()=>{e(0)},[d.match&&d.match.matchingString]);let k=z.useCallback(f=>{a.update(()=>{const n=null!=d.match&&u?I(d.match):null;l(f,n,b,d.match?d.match.matchingString:"")})},[a,u,d.match,l,b]),r=z.useCallback(f=>{const n=a.getRootElement();null!==n&&(n.setAttribute("aria-activedescendant",
"typeahead-item-"+f),e(f))},[a]);z.useEffect(()=>()=>{let f=a.getRootElement();null!==f&&f.removeAttribute("aria-activedescendant")},[a]);D(()=>{null===g?e(null):null===h&&r(0)},[g,h,r]);z.useEffect(()=>A.mergeRegister(a.registerCommand(M,({option:f})=>f.ref&&null!=f.ref.current?(H(f.ref.current),!0):!1,t)),[a,r,t]);z.useEffect(()=>A.mergeRegister(a.registerCommand(p.KEY_ARROW_DOWN_COMMAND,f=>{if(null!==g&&g.length&&null!==h){let n=h!==g.length-1?h+1:0;r(n);let v=g[n];null!=v.ref&&v.ref.current&&
a.dispatchCommand(M,{index:n,option:v});f.preventDefault();f.stopImmediatePropagation()}return!0},t),a.registerCommand(p.KEY_ARROW_UP_COMMAND,f=>{if(null!==g&&g.length&&null!==h){var n=0!==h?h-1:g.length-1;r(n);n=g[n];null!=n.ref&&n.ref.current&&H(n.ref.current);f.preventDefault();f.stopImmediatePropagation()}return!0},t),a.registerCommand(p.KEY_ESCAPE_COMMAND,f=>{f.preventDefault();f.stopImmediatePropagation();b();return!0},t),a.registerCommand(p.KEY_TAB_COMMAND,f=>{if(null===g||null===h||null==
g[h])return!1;f.preventDefault();f.stopImmediatePropagation();k(g[h]);return!0},t),a.registerCommand(p.KEY_ENTER_COMMAND,f=>{if(null===g||null===h||null==g[h])return!1;null!==f&&(f.preventDefault(),f.stopImmediatePropagation());k(g[h]);return!0},t)),[k,b,a,g,h,r,t]);let w=z.useMemo(()=>({options:g,selectOptionAndCleanUp:k,selectedIndex:h,setHighlightedIndex:e}),[k,h,g]);return q(c,w,d.match?d.match.matchingString:"")}
function O(b,a,c,d=document.body,g=!0){let [q]=m.useLexicalComposerContext(),l=z.useRef(document.createElement("div")),u=z.useCallback(()=>{l.current.style.top=l.current.style.bottom;const h=q.getRootElement(),e=l.current;var k=e.firstChild;if(null!==h&&null!==b){const {left:w,top:f,width:n,height:v}=b.getRect();e.style.top=`${f+l.current.offsetHeight+3+(g?window.pageYOffset:0)}px`;e.style.left=`${w+window.pageXOffset}px`;e.style.height=`${v}px`;e.style.width=`${n}px`;if(null!==k){k.style.top=`${f}`;
var r=k.getBoundingClientRect();k=r.height;r=r.width;const x=h.getBoundingClientRect();w+r>x.right&&(e.style.left=`${x.right-r+window.pageXOffset}px`);(f+k>window.innerHeight||f+k>x.bottom)&&f-x.top>k+v&&(e.style.top=`${f-k-v+(g?window.pageYOffset:0)}px`)}e.isConnected||(null!=c&&(e.className=c),e.setAttribute("aria-label","Typeahead menu"),e.setAttribute("id","typeahead-menu"),e.setAttribute("role","listbox"),e.style.display="block",e.style.position="absolute",d.append(e));l.current=e;h.setAttribute("aria-controls",
"typeahead-menu")}},[q,b,g,c,d]);z.useEffect(()=>{let h=q.getRootElement();if(null!==b)return u(),()=>{null!==h&&h.removeAttribute("aria-controls");let e=l.current;null!==e&&e.isConnected&&e.remove()}},[q,u,b]);let t=z.useCallback(h=>{null!==b&&(h||a(null))},[b,a]);L(b,l.current,u,t);return l}function P(b,a,c){var d=c.getSelection();if(null===d||!d.isCollapsed)return!1;c=d.anchorNode;d=d.anchorOffset;if(null==c||null==d)return!1;try{a.setStart(c,b),a.setEnd(c,d)}catch(g){return!1}return!0}
function Q(b){let a=null;b.getEditorState().read(()=>{var c=p.$getSelection();if(p.$isRangeSelection(c)){var d=c.anchor;"text"!==d.type?a=null:(c=d.getNode(),c.isSimpleText()?(d=d.offset,a=c.getTextContent().slice(0,d)):a=null)}});return a}function R(b,a){return 0!==a?!1:b.getEditorState().read(()=>{var c=p.$getSelection();return p.$isRangeSelection(c)?(c=c.anchor.getNode().getPreviousSibling(),p.$isTextNode(c)&&c.isTextEntity()):!1})}let S=p.createCommand("SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND");
exports.LexicalTypeaheadMenuPlugin=function({options:b,onQueryChange:a,onSelectOption:c,onOpen:d,onClose:g,menuRenderFn:q,triggerFn:l,anchorClassName:u,commandPriority:t=p.COMMAND_PRIORITY_LOW,parent:h}){let [e]=m.useLexicalComposerContext(),[k,r]=z.useState(null);u=O(k,r,u,h);let w=z.useCallback(()=>{r(null);null!=g&&null!==k&&g()},[g,k]),f=z.useCallback(n=>{r(n);null!=d&&null===k&&d(n)},[d,k]);z.useEffect(()=>{let n=e.registerUpdateListener(()=>{e.getEditorState().read(()=>{const v=e._window||window,
x=v.document.createRange(),F=p.$getSelection(),G=Q(e);if(p.$isRangeSelection(F)&&F.isCollapsed()&&null!==G&&null!==x){var y=l(G,e);a(y?y.matchingString:null);null===y||R(e,y.leadOffset)||null===P(y.leadOffset,x,v)?w():C(()=>f({getRect:()=>x.getBoundingClientRect(),match:y}))}else w()})});return()=>{n()}},[e,l,a,k,w,f]);return null===k||null===e?null:B.jsx(N,{close:w,resolution:k,editor:e,anchorElementRef:u,options:b,menuRenderFn:q,shouldSplitNodeWithQuery:!0,onSelectOption:c,commandPriority:t})};
exports.MenuOption=E;exports.PUNCTUATION="\\.,\\+\\*\\?\\$\\@\\|#{}\\(\\)\\^\\-\\[\\]\\\\/!%'\"~=<>_:;";exports.SCROLL_TYPEAHEAD_OPTION_INTO_VIEW_COMMAND=S;exports.getScrollParent=function(b,a){let c=getComputedStyle(b),d="absolute"===c.position;a=a?/(auto|scroll|hidden)/:/(auto|scroll)/;if("fixed"===c.position)return document.body;for(;b=b.parentElement;)if(c=getComputedStyle(b),(!d||"static"!==c.position)&&a.test(c.overflow+c.overflowY+c.overflowX))return b;return document.body};
exports.useBasicTypeaheadTriggerMatch=function(b,{minLength:a=1,maxLength:c=75}){return z.useCallback(d=>{d=(new RegExp("(^|\\s|\\()(["+b+"]((?:[^"+(b+"\\.,\\+\\*\\?\\$\\@\\|#{}\\(\\)\\^\\-\\[\\]\\\\/!%'\"~=<>_:;\\s]){0,")+c+"}))$")).exec(d);if(null!==d){let g=d[1],q=d[3];if(q.length>=a)return{leadOffset:d.index+g.length,matchingString:q,replaceableString:d[2]}}return null},[c,a,b])};exports.useDynamicPositioning=L