"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PageBreakNode = void 0;
exports.$createPageBreakNode = $createPageBreakNode;
exports.$isPageBreakNode = $isPageBreakNode;
const tslib_1 = require("tslib");
const LexicalComposerContext_1 = require("@lexical/react/LexicalComposerContext");
const useLexicalNodeSelection_1 = require("@lexical/react/useLexicalNodeSelection");
const utils_1 = require("@lexical/utils");
const lexical_1 = require("lexical");
const React = tslib_1.__importStar(require("react"));
const react_1 = require("react");
function PageBreakComponent({ nodeKey }) {
    const [editor] = (0, LexicalComposerContext_1.useLexicalComposerContext)();
    const [isSelected, setSelected, clearSelection] = (0, useLexicalNodeSelection_1.useLexicalNodeSelection)(nodeKey);
    const onDelete = (0, react_1.useCallback)((event) => {
        event.preventDefault();
        if (isSelected && (0, lexical_1.$isNodeSelection)((0, lexical_1.$getSelection)())) {
            const node = (0, lexical_1.$getNodeByKey)(nodeKey);
            if ($isPageBreakNode(node)) {
                node.remove();
                return true;
            }
        }
        return false;
    }, [isSelected, nodeKey]);
    (0, react_1.useEffect)(() => (0, utils_1.mergeRegister)(editor.registerCommand(lexical_1.CLICK_COMMAND, (event) => {
        const pbElem = editor.getElementByKey(nodeKey);
        if (event.target === pbElem) {
            if (!event.shiftKey) {
                clearSelection();
            }
            setSelected(!isSelected);
            return true;
        }
        return false;
    }, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_DELETE_COMMAND, onDelete, lexical_1.COMMAND_PRIORITY_LOW), editor.registerCommand(lexical_1.KEY_BACKSPACE_COMMAND, onDelete, lexical_1.COMMAND_PRIORITY_LOW)), [clearSelection, editor, isSelected, nodeKey, onDelete, setSelected]);
    (0, react_1.useEffect)(() => {
        const pbElem = editor.getElementByKey(nodeKey);
        if (pbElem !== null) {
            pbElem.className = isSelected ? 'selected' : '';
        }
    }, [editor, isSelected, nodeKey]);
    return null;
}
class PageBreakNode extends lexical_1.DecoratorNode {
    static getType() {
        return 'page-break';
    }
    static clone(node) {
        return new PageBreakNode(node.__key);
    }
    static importJSON() {
        return $createPageBreakNode();
    }
    static importDOM() {
        return {
            figure: (domNode) => {
                const tp = domNode.getAttribute('type');
                if (tp !== this.getType())
                    return null;
                return {
                    conversion: convertPageBreakElement,
                    priority: lexical_1.COMMAND_PRIORITY_HIGH,
                };
            },
        };
    }
    exportJSON() {
        return {
            type: this.getType(),
            version: 1,
        };
    }
    createDOM() {
        const el = document.createElement('figure');
        el.style.pageBreakAfter = 'always';
        el.setAttribute('type', this.getType());
        return el;
    }
    getTextContent() {
        return '\n';
    }
    isInline() {
        return false;
    }
    updateDOM() {
        return false;
    }
    decorate() {
        return React.createElement(PageBreakComponent, { nodeKey: this.__key });
    }
}
exports.PageBreakNode = PageBreakNode;
function convertPageBreakElement() {
    return { node: $createPageBreakNode() };
}
function $createPageBreakNode() {
    return new PageBreakNode();
}
function $isPageBreakNode(node) {
    return node instanceof PageBreakNode;
}
//# sourceMappingURL=index.js.map