/*
This file is part of the Notesnook project (https://notesnook.com/)

Copyright (C) 2023 Streetwriters (Private) Limited

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
import { Extension } from "@tiptap/core";
import { isInTable } from "@tiptap/pm/tables";
import { isListActive } from "../../utils/prosemirror";
import { CodeBlock } from "../code-block";
export const KeyMap = Extension.create({
    name: "key-map",
    addKeyboardShortcuts() {
        return {
            Tab: ({ editor }) => {
                if (isListActive(editor) ||
                    isInTable(editor.state) ||
                    editor.isActive(CodeBlock.name))
                    return false;
                return editor.commands.insertContent("\t");
            },
            "Shift-Tab": ({ editor }) => {
                if (isListActive(editor))
                    return false;
                return true;
            },
            "Mod-\\": ({ editor }) => {
                editor
                    .chain()
                    .focus()
                    .clearNodes()
                    .unsetAllMarks()
                    .unsetMark("link")
                    .run();
                return true;
            },
            "Shift-Mod-L": ({ editor }) => {
                var _a, _b;
                (_b = (_a = editor.storage).createInternalLink) === null || _b === void 0 ? void 0 : _b.call(_a).then((link) => {
                    if (!link)
                        return;
                    const selectedText = editor.state.doc.textBetween(editor.state.selection.from, editor.state.selection.to);
                    editor.commands.setLink(Object.assign(Object.assign({}, link), { title: selectedText || link.title }));
                });
                return true;
            }
        };
    }
});
