"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Code = exports.codeType = exports.codeMarkName = void 0;
const tslib_1 = require("tslib");
const prosemirror_codemark_1 = tslib_1.__importDefault(require("prosemirror-codemark"));
const prosemirror_commands_1 = require("prosemirror-commands");
const prosemirror_state_1 = require("prosemirror-state");
const actions_1 = require("../../../utils/actions.js");
const keymap_1 = require("../../../utils/keymap.js");
const CodeSpecs_1 = require("./CodeSpecs/index.js");
require("./code.css");
var CodeSpecs_2 = require("./CodeSpecs/index.js");
Object.defineProperty(exports, "codeMarkName", { enumerable: true, get: function () { return CodeSpecs_2.codeMarkName; } });
Object.defineProperty(exports, "codeType", { enumerable: true, get: function () { return CodeSpecs_2.codeType; } });
const codeAction = 'code';
const Code = (builder, opts) => {
    builder.use(CodeSpecs_1.CodeSpecs);
    builder.addAction(codeAction, ({ schema }) => (0, actions_1.createToggleMarkAction)((0, CodeSpecs_1.codeType)(schema)));
    if (opts?.codeKey) {
        const { codeKey } = opts;
        builder.addKeymap(({ schema }) => ({
            [codeKey]: (0, keymap_1.withLogAction)('code_inline', (0, prosemirror_commands_1.toggleMark)((0, CodeSpecs_1.codeType)(schema))),
        }));
    }
    builder
        // codemark adds inputRule for `code`,
        // adds fake cursor behaviour when crossing inline_code boundaries,
        // wrap current selected text to inline_code when '`' was pressed.
        // See demo: https://curvenote.github.io/prosemirror-codemark/
        .addPlugin(({ schema }) => (0, prosemirror_codemark_1.default)({ markType: (0, CodeSpecs_1.codeType)(schema) }));
    builder.addPlugin(() => 
    // apply codemark when typing text between ``
    new prosemirror_state_1.Plugin({
        props: {
            handleTextInput: (view, from, to, text) => {
                const { selection: { $anchor }, tr, schema, } = view.state;
                if ($anchor.nodeBefore?.text?.endsWith('`') &&
                    $anchor.nodeAfter?.text?.startsWith('`')) {
                    view.dispatch(tr.replaceRangeWith(from - 1, to + 1, view.state.schema.text(text, [(0, CodeSpecs_1.codeType)(schema).create()])));
                    return true;
                }
                return false;
            },
        },
    }));
};
exports.Code = Code;
//# sourceMappingURL=index.js.map
