import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
import { $getSelection, $isTextNode } from "lexical";
import { EditorLookupDropdownBase } from "@sparrowengg/twigs-editor-react";
export const SlashesPlugin = () => {
const [editor] = useLexicalComposerContext();
const commands = [
{ label: "Show alert", value: "show-alert" },
{ label: "Show prompt", value: "show-prompt" },
{ label: "Show confirm", value: "show-confirm" },
];
return (
<EditorLookupDropdownBase
getResults={(searchString) => {
return commands.filter((command) =>
command.label
.toLowerCase()
.includes((searchString || "").toLowerCase())
);
}}
triggerFunction={(text) => getPossibleQueryMatch(text)}
onMenuItemSelect={(item, closeMenu) => {
let promptValue = '';
if (item.data.value === "show-alert") {
alert("Alert!");
} else if (item.data.value === "show-prompt") {
promptValue = prompt("Prompt!");
} else if (item.data.value === "show-confirm") {
confirm("Confirm!");
}
editor.update(() => {
const selection = $getSelection();
const nodes = selection?.getNodes();
if (nodes?.[0] && $isTextNode(nodes[0])) {
nodes[0].setTextContent(promptValue || '');
}
});
closeMenu();
return true; // Prevents default action after selection
}}
/>
);
};