/**
 * 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{$insertDataTransferForPlainText as e,$handlePlainTextDrop as t,$writeDragSourceToDataTransfer as r,$getHtmlContent as n}from"@lexical/clipboard";import{DragonExtension as o}from"@lexical/dragon";import{NormalizeInlineElementsExtension as a,NormalizeTripleClickSelectionExtension as i}from"@lexical/extension";import{$shouldOverrideDefaultCharacterSelection as s,$moveCharacter as l}from"@lexical/selection";import{objectKlassEquals as m}from"@lexical/utils";import{mergeRegister as c,DELETE_CHARACTER_COMMAND as u,$getSelection as d,$isRangeSelection as f,COMMAND_PRIORITY_EDITOR as p,DELETE_WORD_COMMAND as C,DELETE_LINE_COMMAND as g,CONTROLLED_TEXT_INSERTION_COMMAND as x,REMOVE_TEXT_COMMAND as v,INSERT_LINE_BREAK_COMMAND as D,INSERT_PARAGRAPH_COMMAND as h,KEY_ARROW_LEFT_COMMAND as T,KEY_ARROW_RIGHT_COMMAND as b,KEY_BACKSPACE_COMMAND as y,IS_IOS as K,CAN_USE_BEFORE_INPUT as L,KEY_DELETE_COMMAND as k,KEY_ENTER_COMMAND as B,IS_SAFARI as E,IS_APPLE_WEBKIT as W,SELECT_ALL_COMMAND as N,$selectAll as j,$getSlotFrame as q,COPY_COMMAND as w,CUT_COMMAND as z,PASTE_COMMAND as A,DROP_COMMAND as F,DRAGSTART_COMMAND as G,defineExtension as H,CUT_TAG as I,PASTE_TAG as J}from"lexical";function M(e,t){t.update(()=>{if(null!==e){const r=m(e,KeyboardEvent)?null:e.clipboardData,o=d();if(null!==o&&!o.isCollapsed()&&null!=r){e.preventDefault();const a=n(t);null!==a&&r.setData("text/html",a),r.setData("text/plain",o.getTextContent())}}})}function O(n){return c(n.registerCommand(u,e=>{const t=d();return!!f(t)&&(t.deleteCharacter(e),!0)},p),n.registerCommand(C,e=>{const t=d();return!!f(t)&&(t.deleteWord(e),!0)},p),n.registerCommand(g,e=>{const t=d();return!!f(t)&&(t.deleteLine(e),!0)},p),n.registerCommand(x,t=>{const r=d();if(!f(r))return!1;if("string"==typeof t)r.insertText(t);else{const n=t.dataTransfer;if(null!=n)e(n,r);else{const e=t.data;e&&r.insertText(e)}}return!0},p),n.registerCommand(v,()=>{const e=d();return!!f(e)&&(e.removeText(),!0)},p),n.registerCommand(D,e=>{const t=d();return!!f(t)&&(t.insertLineBreak(e),!0)},p),n.registerCommand(h,()=>{const e=d();return!!f(e)&&(e.insertLineBreak(),!0)},p),n.registerCommand(T,e=>{const t=d();if(!f(t))return!1;const r=e,n=r.shiftKey;return!!s(t,!0)&&(r.preventDefault(),l(t,n,!0),!0)},p),n.registerCommand(b,e=>{const t=d();if(!f(t))return!1;const r=e,n=r.shiftKey;return!!s(t,!1)&&(r.preventDefault(),l(t,n,!1),!0)},p),n.registerCommand(y,e=>{const t=d();return!!f(t)&&((!K||!L)&&(e.preventDefault(),n.dispatchCommand(u,!0)))},p),n.registerCommand(k,e=>{const t=d();return!!f(t)&&(e.preventDefault(),n.dispatchCommand(u,!1))},p),n.registerCommand(B,e=>{const t=d();if(!f(t))return!1;if(null!==e){if((K||E||W)&&L)return!1;e.preventDefault()}return n.dispatchCommand(D,!1)},p),n.registerCommand(N,()=>{const e=d();return j(f(e)&&null!==q(e.anchor.getNode())?e:null),!0},p),n.registerCommand(w,e=>{const t=d();return!!f(t)&&(M(e,n),!0)},p),n.registerCommand(z,e=>{const t=d();return!!f(t)&&(function(e,t){M(e,t),t.update(()=>{const e=d();f(e)&&e.removeText()},{tag:I})}(e,n),!0)},p),n.registerCommand(A,t=>{const r=d();return!!f(r)&&(function(t,r){t.preventDefault(),r.update(()=>{const r=d(),n=m(t,ClipboardEvent)?t.clipboardData:null;null!=n&&f(r)&&e(n,r)},{tag:J})}(t,n),!0)},p),n.registerCommand(F,e=>t(e,n),p),n.registerCommand(G,e=>{const t=d();return!!f(t)&&(t.isCollapsed()||null===e.dataTransfer||r(e.dataTransfer,n),!0)},p))}const P=/* @__PURE__ */H({conflictsWith:["@lexical/rich-text"],dependencies:[o,a,i],name:"@lexical/plain-text",register:O});export{P as PlainTextExtension,O as registerPlainText};
