import { Editor } from "@tiptap/react";

export const addImage = (editor: Editor) => {
    const url = window.prompt('Enter image URL:');
    if (url) {
        editor.chain().focus().setImage({ src: url }).run();
    }
};

export const addHorizontalRule = (editor: Editor) => {
    editor.chain().focus().setHorizontalRule().run();
};

export const addTable = (editor: Editor) => {
    editor
        .chain()
        .focus()
        .insertTable({ rows: 3, cols: 3, withHeaderRow: true })
        .run();
};

export const setLink = (editor: Editor) => {
    const previousUrl = editor.getAttributes('link').href;
    const url = window.prompt('Enter URL:', previousUrl);

    if (url === null) {
        return;
    }

    if (url === '') {
        editor.chain().focus().unsetLink().run();
        return;
    }

    editor.chain().focus().setLink({ href: url }).run();
};