/**
 * 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.
 */
'use strict';var a=require("@lexical/clipboard"),g=require("@lexical/selection"),k=require("@lexical/utils"),l=require("lexical");function m(c,b){return"undefined"!==typeof document.caretRangeFromPoint?(c=document.caretRangeFromPoint(c,b),null===c?null:{node:c.startContainer,offset:c.startOffset}):"undefined"!==document.caretPositionFromPoint?(c=document.caretPositionFromPoint(c,b),null===c?null:{node:c.offsetNode,offset:c.offset}):null}
let p="undefined"!==typeof window&&"undefined"!==typeof window.document&&"undefined"!==typeof window.document.createElement,q=p&&"documentMode"in document?document.documentMode:null;p&&/Mac|iPod|iPhone|iPad/.test(navigator.platform);p&&/^(?!.*Seamonkey)(?=.*Firefox).*/i.test(navigator.userAgent);let r=p&&"InputEvent"in window&&!q?"getTargetRanges"in new window.InputEvent("input"):!1,t=p&&/Version\/[\d.]+.*Safari/.test(navigator.userAgent),u=p&&/iPad|iPhone|iPod/.test(navigator.userAgent)&&!window.MSStream;
p&&/^(?=.*Chrome).*/i.test(navigator.userAgent);let w=l.createCommand("DRAG_DROP_PASTE_FILE");
class x extends l.ElementNode{static getType(){return"quote"}static clone(c){return new x(c.__key)}constructor(c){super(c)}createDOM(c){let b=document.createElement("blockquote");k.addClassNamesToElement(b,c.theme.quote);return b}updateDOM(){return!1}static importDOM(){return{blockquote:()=>({conversion:y,priority:0})}}static importJSON(c){let b=z();b.setFormat(c.format);b.setIndent(c.indent);b.setDirection(c.direction);return b}exportJSON(){return{...super.exportJSON(),type:"quote"}}insertNewAfter(c,
b){c=l.$createParagraphNode();let d=this.getDirection();c.setDirection(d);this.insertAfter(c,b);return c}collapseAtStart(){let c=l.$createParagraphNode();this.getChildren().forEach(b=>c.append(b));this.replace(c);return!0}}function z(){return l.$applyNodeReplacement(new x)}
class A extends l.ElementNode{static getType(){return"heading"}static clone(c){return new A(c.__tag,c.__key)}constructor(c,b){super(b);this.__tag=c}getTag(){return this.__tag}createDOM(c){let b=this.__tag,d=document.createElement(b);c=c.theme.heading;void 0!==c&&k.addClassNamesToElement(d,c[b]);return d}updateDOM(){return!1}static importDOM(){return{h1:()=>({conversion:B,priority:0}),h2:()=>({conversion:B,priority:0}),h3:()=>({conversion:B,priority:0}),h4:()=>({conversion:B,priority:0}),h5:()=>({conversion:B,
priority:0}),h6:()=>({conversion:B,priority:0}),p:c=>{c=c.firstChild;return null!==c&&D(c)?{conversion:()=>({node:null}),priority:3}:null},span:c=>D(c)?{conversion:()=>({node:E("h1")}),priority:3}:null}}static importJSON(c){let b=E(c.tag);b.setFormat(c.format);b.setIndent(c.indent);b.setDirection(c.direction);return b}exportJSON(){return{...super.exportJSON(),tag:this.getTag(),type:"heading",version:1}}insertNewAfter(c,b=!0){c=c?c.anchor.offset:0;c=0<c&&c<this.getTextContentSize()?E(this.getTag()):
l.$createParagraphNode();let d=this.getDirection();c.setDirection(d);this.insertAfter(c,b);return c}collapseAtStart(){let c=this.isEmpty()?l.$createParagraphNode():E(this.getTag());this.getChildren().forEach(b=>c.append(b));this.replace(c);return!0}extractWithChild(){return!0}}function D(c){return"span"===c.nodeName.toLowerCase()?"26pt"===c.style.fontSize:!1}function B(c){c=c.nodeName.toLowerCase();let b=null;if("h1"===c||"h2"===c||"h3"===c||"h4"===c||"h5"===c||"h6"===c)b=E(c);return{node:b}}
function y(){return{node:z()}}function E(c){return l.$applyNodeReplacement(new A(c))}function F(c,b){c.preventDefault();b.update(()=>{let d=l.$getSelection(),e=c instanceof InputEvent||c instanceof KeyboardEvent?null:c.clipboardData;null!=e&&(l.$isRangeSelection(d)||l.DEPRECATED_$isGridSelection(d))&&a.$insertDataTransferForRichText(e,d,b)},{tag:"paste"})}
async function G(c,b){await a.copyToClipboard__EXPERIMENTAL(b,c instanceof ClipboardEvent?c:null);b.update(()=>{let d=l.$getSelection();l.$isRangeSelection(d)?d.removeText():l.$isNodeSelection(d)&&d.getNodes().forEach(e=>e.remove())})}function H(c){let b=null;c instanceof DragEvent?b=c.dataTransfer:c instanceof ClipboardEvent&&(b=c.clipboardData);if(null===b)return[!1,[],!1];var d=b.types;c=d.includes("Files");d=d.includes("text/html")||d.includes("text/plain");return[c,Array.from(b.files),d]}
function I(c,b){var d=l.$getSelection();if(l.$isRangeSelection(d)){var e=new Set;d=d.getNodes();for(let f=0;f<d.length;f++){let n=d[f],v=n.getKey();if(e.has(v))continue;let h=k.$getNearestBlockElementAncestorOrThrow(n),C=h.getKey();h.canInsertTab()?(c(n),e.add(v)):h.canIndent()&&!e.has(C)&&(e.add(C),b(h))}}}function J(c){c=l.$getNearestNodeFromDOMNode(c);return l.$isDecoratorNode(c)}exports.$createHeadingNode=E;exports.$createQuoteNode=z;exports.$isHeadingNode=function(c){return c instanceof A};
exports.$isQuoteNode=function(c){return c instanceof x};exports.DRAG_DROP_PASTE=w;exports.HeadingNode=A;exports.QuoteNode=x;exports.eventFiles=H;
exports.registerRichText=function(c){return k.mergeRegister(c.registerCommand(l.CLICK_COMMAND,()=>{const b=l.$getSelection();return l.$isNodeSelection(b)?(b.clear(),!0):!1},0),c.registerCommand(l.DELETE_CHARACTER_COMMAND,b=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.deleteCharacter(b);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.DELETE_WORD_COMMAND,b=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.deleteWord(b);return!0},l.COMMAND_PRIORITY_EDITOR),
c.registerCommand(l.DELETE_LINE_COMMAND,b=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.deleteLine(b);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.CONTROLLED_TEXT_INSERTION_COMMAND,b=>{const d=l.$getSelection();if("string"===typeof b)l.$isRangeSelection(d)?d.insertText(b):l.DEPRECATED_$isGridSelection(d);else{if(!l.$isRangeSelection(d)&&!l.DEPRECATED_$isGridSelection(d))return!1;const e=b.dataTransfer;null!=e?a.$insertDataTransferForRichText(e,d,c):l.$isRangeSelection(d)&&
(b=b.data)&&d.insertText(b)}return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.REMOVE_TEXT_COMMAND,()=>{const b=l.$getSelection();if(!l.$isRangeSelection(b))return!1;b.removeText();return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.FORMAT_TEXT_COMMAND,b=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.formatText(b);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.FORMAT_ELEMENT_COMMAND,b=>{var d=l.$getSelection();if(!l.$isRangeSelection(d)&&!l.$isNodeSelection(d))return!1;
d=d.getNodes();for(const e of d)k.$getNearestBlockElementAncestorOrThrow(e).setFormat(b);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.INSERT_LINE_BREAK_COMMAND,b=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;d.insertLineBreak(b);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.INSERT_PARAGRAPH_COMMAND,()=>{const b=l.$getSelection();if(!l.$isRangeSelection(b))return!1;b.insertParagraph();return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.INDENT_CONTENT_COMMAND,
()=>{I(()=>{c.dispatchCommand(l.CONTROLLED_TEXT_INSERTION_COMMAND,"\t")},b=>{const d=b.getIndent();10!==d&&b.setIndent(d+1)});return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.OUTDENT_CONTENT_COMMAND,()=>{I(b=>{l.$isTextNode(b)&&(b=b.getTextContent(),"\t"===b[b.length-1]&&c.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!0))},b=>{const d=b.getIndent();0!==d&&b.setIndent(d-1)});return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_ARROW_UP_COMMAND,b=>{var d=l.$getSelection();if(l.$isNodeSelection(d)&&
!J(b.target)){if(b=d.getNodes(),0<b.length)return b[0].selectPrevious(),!0}else if(l.$isRangeSelection(d)){d=l.$getAdjacentNode(d.focus,!0);if(l.$isDecoratorNode(d)&&!d.isIsolated())return d.selectPrevious(),b.preventDefault(),!0;if(l.$isElementNode(d)&&!d.isInline()&&!d.canBeEmpty())return d.select(),b.preventDefault(),!0}return!1},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_ARROW_DOWN_COMMAND,b=>{var d=l.$getSelection();if(l.$isNodeSelection(d)){if(b=d.getNodes(),0<b.length)return b[0].selectNext(0,
0),!0}else if(l.$isRangeSelection(d)){let e=d.focus;if("root"===e.key&&e.offset===l.$getRoot().getChildrenSize())return b.preventDefault(),!0;d=l.$getAdjacentNode(d.focus,!1);if(l.$isDecoratorNode(d)&&!d.isIsolated())return d.selectNext(),b.preventDefault(),!0}return!1},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_ARROW_LEFT_COMMAND,b=>{const d=l.$getSelection();if(l.$isNodeSelection(d)){var e=d.getNodes();if(0<e.length)return b.preventDefault(),e[0].selectPrevious(),!0}return l.$isRangeSelection(d)?
g.$shouldOverrideDefaultCharacterSelection(d,!0)?(e=b.shiftKey,b.preventDefault(),g.$moveCharacter(d,e,!0),!0):!1:!1},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_ARROW_RIGHT_COMMAND,b=>{const d=l.$getSelection();if(l.$isNodeSelection(d)&&!J(b.target)){var e=d.getNodes();if(0<e.length)return b.preventDefault(),e[0].selectNext(0,0),!0}if(!l.$isRangeSelection(d))return!1;e=b.shiftKey;return g.$shouldOverrideDefaultCharacterSelection(d,!1)?(b.preventDefault(),g.$moveCharacter(d,e,!1),!0):!1},l.COMMAND_PRIORITY_EDITOR),
c.registerCommand(l.KEY_BACKSPACE_COMMAND,b=>{if(J(b.target))return!1;const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;b.preventDefault();({anchor:b}=d);const e=b.getNode();return d.isCollapsed()&&0===b.offset&&!l.$isRootNode(e)&&0<k.$getNearestBlockElementAncestorOrThrow(e).getIndent()?c.dispatchCommand(l.OUTDENT_CONTENT_COMMAND,void 0):c.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!0)},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_DELETE_COMMAND,b=>{if(J(b.target))return!1;const d=
l.$getSelection();if(!l.$isRangeSelection(d))return!1;b.preventDefault();return c.dispatchCommand(l.DELETE_CHARACTER_COMMAND,!1)},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_ENTER_COMMAND,b=>{const d=l.$getSelection();if(!l.$isRangeSelection(d))return!1;if(null!==b){if((u||t)&&r)return!1;b.preventDefault();if(b.shiftKey)return c.dispatchCommand(l.INSERT_LINE_BREAK_COMMAND,!1)}return c.dispatchCommand(l.INSERT_PARAGRAPH_COMMAND,void 0)},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.KEY_ESCAPE_COMMAND,
()=>{const b=l.$getSelection();if(!l.$isRangeSelection(b))return!1;c.blur();return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.DROP_COMMAND,b=>{const [,d]=H(b);if(0<d.length){var e=m(b.clientX,b.clientY);if(null!==e){const {offset:n,node:v}=e;var f=l.$getNearestNodeFromDOMNode(v);if(null!==f){e=l.$createRangeSelection();if(l.$isTextNode(f))e.anchor.set(f.getKey(),n,"text"),e.focus.set(f.getKey(),n,"text");else{const h=f.getParentOrThrow().getKey();f=f.getIndexWithinParent()+1;e.anchor.set(h,
f,"element");e.focus.set(h,f,"element")}e=l.$normalizeSelection__EXPERIMENTAL(e);l.$setSelection(e)}c.dispatchCommand(w,d)}b.preventDefault();return!0}b=l.$getSelection();return l.$isRangeSelection(b)?!0:!1},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.DRAGSTART_COMMAND,b=>{[b]=H(b);const d=l.$getSelection();return b&&!l.$isRangeSelection(d)?!1:!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.DRAGOVER_COMMAND,b=>{var [d]=H(b);const e=l.$getSelection();if(d&&!l.$isRangeSelection(e))return!1;d=
m(b.clientX,b.clientY);null!==d&&(d=l.$getNearestNodeFromDOMNode(d.node),l.$isDecoratorNode(d)&&b.preventDefault());return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.COPY_COMMAND,b=>{a.copyToClipboard__EXPERIMENTAL(c,b instanceof ClipboardEvent?b:null);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.CUT_COMMAND,b=>{G(b,c);return!0},l.COMMAND_PRIORITY_EDITOR),c.registerCommand(l.PASTE_COMMAND,b=>{const [,d,e]=H(b);if(0<d.length&&!e)return c.dispatchCommand(w,d),!0;const f=l.$getSelection();
return l.$isRangeSelection(f)||l.DEPRECATED_$isGridSelection(f)?(F(b,c),!0):!1},l.COMMAND_PRIORITY_EDITOR))}
