// imports import clsx from 'clsx'; import { Combobox } from '@headlessui/react'; import React, { useRef, useEffect } from 'react'; // Component for rendering the slash command combobox const SlashCombobox = ({ items, onSelect, editor, editorRef, selectedIndex }) => { // Reference to the combobox DOM element const ref = useRef(null); // Position the combobox above the editor useEffect(() => { if (items.length > 0 && editorRef.current) { // Get the combobox element const el = ref.current; // Get the bounding rectangle of the editor const editorRect = editorRef.current.getBoundingClientRect(); // Position the combobox above the editor el.style.bottom = `${window.innerHeight - editorRect.top}px`; el.style.left = `${editorRect.left}px`; el.style.width = `${editorRect.width}px`; } }, [items.length, editorRef]); // Render the combobox return (
{items.map((item, index) => ( /{item.name} {item.description} ))}
); }; // Custom plugin to handle slash commands in the editor export const withSlashCommands = editor => { const { isInline, isVoid } = editor; // Override isInline to treat slash commands as inline elements editor.isInline = element => { return element.type === 'slash-command' ? true : isInline(element); }; // Override isVoid to treat slash commands as void elements editor.isVoid = element => { return element.type === 'slash-command' ? true : isVoid(element); }; return editor; }; /** * slash combobox */ export default SlashCombobox;