UNPKG

@lexical/react

Version:

This package provides Lexical components and hooks for React applications.

10 lines (8 loc) 4.21 kB
/** * 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}from"@lexical/utils";import{DELETE_CHARACTER_COMMAND as f,$getSelection as g,$isRangeSelection as u,$isElementNode as m,COMMAND_PRIORITY_LOW as d,$isTextNode as h,$isLeafNode as p,$setSelection as x}from"lexical";import{jsx as C}from"react/jsx-runtime";function v(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var w=v((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));function y(e,n,r=Object.freeze({})){const{strlen:s=(e=>e.length),remainingCharacters:C=(()=>{})}=r;t((()=>{e.hasNodes([o])||w(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(),f=o.size>0||l.size>0;if(c||!f)return;const m=s(t),d=m>n||null!==r&&r>n;if(C(n-m),null===r||d){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();T(n);const o=g();!u(o)||o.anchor.getNode().isAttached()&&o.focus.getNode().isAttached()||(h(t)?t.select():h(r)?r.select():null!==e&&e.select())}else if(t<e){const r=n.getFirstDescendant(),o=t+(null!==r?r.getTextContentSize():0);(h(r)&&r.isSimpleText()||o<=e)&&T(n)}}else if(p(n)){const t=r;if(r+=n.getTextContentSize(),r>e&&!i(n.getParent())){const r=g();let o;if(t<e&&h(n)&&n.isSimpleText()){const[,r]=n.splitText(e-t);o=S(r)}else o=S(n);null!==r&&x(r),b(o)}}}}(r)}),{tag:"history-merge"})}r=m})),e.registerCommand(f,(e=>{const t=g();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():m(o)&&o.isEmpty()&&o.remove(),!0}),d))}),[e,n,C,s])}function S(e){const t=s();return e.replace(t),t.append(e),t}function T(e){const t=e.getChildren(),n=t.length;for(let r=0;r<n;r++)e.insertBefore(t[r]);return e.remove(),n>0?t[n-1]:null}function b(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=g();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 N=null;function E(e){const t=void 0===window.TextEncoder?null:(null===N&&(N=new window.TextEncoder),N);if(null===t){const t=encodeURIComponent(e).match(/%[89ABab]/g);return e.length+(t?t.length:0)}return t.encode(e).length}function P({remainingCharacters:e}){return C("span",{className:"characters-limit "+(e<0?"characters-limit-exceeded":""),children:e})}function z({charset:t="UTF-16",maxLength:o=5,renderer:i=P}){const[s]=e(),[l,c]=n(o);return y(s,o,r((()=>({remainingCharacters:c,strlen:e=>{if("UTF-8"===t)return E(e);if("UTF-16"===t)return e.length;throw new Error("Unrecognized charset")}})),[t])),i({remainingCharacters:l})}export{z as CharacterLimitPlugin};