"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.gapCursor = void 0;
const prosemirror_model_1 = require("prosemirror-model");
const prosemirror_state_1 = require("prosemirror-state");
const prosemirror_view_1 = require("prosemirror-view");
const selection_1 = require("../../../utils/selection.js");
const BaseSchema_1 = require("../../base/BaseSchema/index.js");
const Placeholder_1 = require("../Placeholder/index.js");
const GapCursorSelection_1 = require("./GapCursorSelection.js");
require("./gapcursor.css");
const key = new prosemirror_state_1.PluginKey('gapCursorPlugin');
const gapCursor = () => new prosemirror_state_1.Plugin({
    key,
    state: {
        init: () => false,
        apply: (_tr, _pluginState, _oldState, newState) => {
            const sel = newState.selection;
            return (0, GapCursorSelection_1.isGapCursorSelection)(sel) || (0, selection_1.isNodeSelection)(sel);
        },
    },
    view() {
        return {
            update(view) {
                const pluginState = key.getState(view.state);
                view.dom.classList.toggle('Prosemirror-hide-cursor', pluginState);
            },
        };
    },
    props: {
        handleKeyPress(view) {
            const { state, state: { selection: sel }, } = view;
            if ((0, GapCursorSelection_1.isGapCursorSelection)(sel)) {
                // Replace GapCursorSelection with empty textblock before run all other handlers.
                // This should be done before all inputRules and other handlers, that handle text input.
                // Thus, entering text into a native textblock and into a "virtual" one – GapCursor – will be the same.
                const tr = state.tr.replaceSelectionWith((0, BaseSchema_1.pType)(state.schema).create());
                tr.setSelection(prosemirror_state_1.TextSelection.create(tr.doc, sel.pos + 1));
                view.dispatch(tr.scrollIntoView());
            }
            return false;
        },
        decorations: ({ doc, selection }) => {
            if ((0, GapCursorSelection_1.isGapCursorSelection)(selection)) {
                const position = selection.head;
                return prosemirror_view_1.DecorationSet.create(doc, [
                    prosemirror_view_1.Decoration.widget(position, toDOM, {
                        key: 'gapcursor',
                        side: -1,
                    }),
                ]);
            }
            return null;
        },
    },
});
exports.gapCursor = gapCursor;
const toDOM = (view, getPos) => {
    const node = (0, BaseSchema_1.pType)(view.state.schema).create();
    const element = prosemirror_model_1.DOMSerializer.fromSchema(view.state.schema).serializeNode(node);
    const placeholderDOM = (0, Placeholder_1.createPlaceholder)(node, null, true);
    if (placeholderDOM)
        element.appendChild(placeholderDOM);
    element.classList.add('g-md-gapcursor');
    element.addEventListener('mousedown', () => {
        const pos = getPos();
        if (pos !== undefined) {
            view.dispatch(view.state.tr.replaceSelectionWith(node));
        }
    });
    return element;
};
//# sourceMappingURL=gapcursor.js.map
