@lexical/react
Version:
This package provides Lexical components and hooks for React applications.
10 lines (8 loc) • 4.01 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{useEffect as t,useState as n,useMemo as r}from"react";import{OverflowNode as o,$isOverflowNode as i,$createOverflowNode as s}from"@lexical/overflow";import{$rootTextContent as l}from"@lexical/text";import{mergeRegister as c,$dfs as a,$unwrapNode as f}from"@lexical/utils";import{DELETE_CHARACTER_COMMAND as g,$getSelection as m,$isRangeSelection as u,$isElementNode as d,COMMAND_PRIORITY_LOW as h,$isTextNode as p,$isLeafNode as x,$setSelection as C}from"lexical";import{jsx as S}from"react/jsx-runtime";function v(e,n,r=Object.freeze({})){const{strlen:s=(e=>e.length),remainingCharacters:S=(()=>{})}=r;t((()=>{e.hasNodes([o])||function(e,...t){const n=new URL("https://lexical.dev/docs/error"),r=new URLSearchParams;r.append("code",e);for(const e of t)r.append("v",e);throw n.search=r.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}(57)}),[e]),t((()=>{let t=e.getEditorState().read(l),r=0;return c(e.registerTextContentListener((e=>{t=e})),e.registerUpdateListener((({dirtyLeaves:o,dirtyElements:l})=>{const c=e.isComposing(),g=o.size>0||l.size>0;if(c||!g)return;const d=s(t),h=d>n||null!==r&&r>n;if(S(n-d),null===r||h){const r=function(e,t,n){const r=Intl.Segmenter;let o=0,i=0;if("function"==typeof r){const s=(new r).segment(e);for(const{segment:e}of s){const r=i+n(e);if(r>t)break;i=r,o+=e.length}}else{const r=Array.from(e),s=r.length;for(let e=0;e<s;e++){const s=r[e],l=i+n(s);if(l>t)break;i=l,o+=s.length}}return o}(t,n,s);e.update((()=>{!function(e){const t=a(),n=t.length;let r=0;for(let o=0;o<n;o+=1){const{node:n}=t[o];if(i(n)){const t=r;if(r+n.getTextContentSize()<=e){const e=n.getParent(),t=n.getPreviousSibling(),r=n.getNextSibling();f(n);const o=m();!u(o)||o.anchor.getNode().isAttached()&&o.focus.getNode().isAttached()||(p(t)?t.select():p(r)?r.select():null!==e&&e.select())}else if(t<e){const r=n.getFirstDescendant(),o=t+(null!==r?r.getTextContentSize():0);(p(r)&&r.isSimpleText()||o<=e)&&f(n)}}else if(x(n)){const t=r;if(r+=n.getTextContentSize(),r>e&&!i(n.getParent())){const r=m();let o;if(t<e&&p(n)&&n.isSimpleText()){const[,r]=n.splitText(e-t);o=w(r)}else o=w(n);null!==r&&C(r),T(o)}}}}(r)}),{tag:"history-merge"})}r=d})),e.registerCommand(g,(e=>{const t=m();if(!u(t))return!1;const n=t.anchor.getNode().getParent(),r=n?n.getParent():null,o=r?r.getNextSibling():null;return t.deleteCharacter(e),r&&r.isEmpty()?r.remove():d(o)&&o.isEmpty()&&o.remove(),!0}),h))}),[e,n,S,s])}function w(e){const t=s();return e.replace(t),t.append(e),t}function T(e){const t=e.getPreviousSibling();if(!i(t))return;const n=e.getFirstChild(),r=t.getChildren(),o=r.length;if(null===n)e.append(...r);else for(let e=0;e<o;e++)n.insertBefore(r[e]);const s=m();if(u(s)){const n=s.anchor,r=n.getNode(),i=s.focus,l=n.getNode();r.is(t)?n.set(e.getKey(),n.offset,"element"):r.is(e)&&n.set(e.getKey(),o+n.offset,"element"),l.is(t)?i.set(e.getKey(),i.offset,"element"):l.is(e)&&i.set(e.getKey(),o+i.offset,"element")}t.remove()}let y=null;function N(e){const t=void 0===window.TextEncoder?null:(null===y&&(y=new window.TextEncoder),y);if(null===t){const t=encodeURIComponent(e).match(/%[89ABab]/g);return e.length+(t?t.length:0)}return t.encode(e).length}function b({remainingCharacters:e}){return S("span",{className:"characters-limit "+(e<0?"characters-limit-exceeded":""),children:e})}function E({charset:t="UTF-16",maxLength:o=5,renderer:i=b}){const[s]=e(),[l,c]=n(o);return v(s,o,r((()=>({remainingCharacters:c,strlen:e=>{if("UTF-8"===t)return N(e);if("UTF-16"===t)return e.length;throw new Error("Unrecognized charset")}})),[t])),i({remainingCharacters:l})}export{E as CharacterLimitPlugin};