import React from 'react'; import type { ChangeEvent, FC, HTMLAttributes } from 'react'; import { EditorState, useEditorState } from '../editor/EditorContext'; export const BtnStyles = createDropdown('Styles', [ ['Normal', 'formatBlock', 'DIV'], ['𝗛𝗲𝗮𝗱𝗲𝗿 𝟭', 'formatBlock', 'H1'], ['Header 2', 'formatBlock', 'H2'], ['𝙲𝚘𝚍𝚎', 'formatBlock', 'PRE'], ]); export function createDropdown( title: string, items: DropDownItem[], ): FC { DropdownFactory.displayName = title; return DropdownFactory; function DropdownFactory(props: DropDownFactoryProps) { const editorState = useEditorState(); const { $el, $selection, htmlMode } = editorState; if (htmlMode) { return null; } const activeIndex = items.findIndex( (item) => item[1] === 'formatBlock' && $selection?.nodeName === item[2], ); return ( ); function onChange(e: ChangeEvent) { const target = e.target; const selectedValue = target.value; const selectedIndex = parseInt(selectedValue, 10); const [, command, commandArgument] = items[selectedIndex] || []; e.preventDefault(); if (document.activeElement !== $el) { $el?.focus(); } if (typeof command === 'function') { command(editorState); } else if (command) { document.execCommand(command, false, commandArgument); } setTimeout(() => (target.value = selectedValue), 10); } } } export function Dropdown({ items, selected, ...inputProps }: DropdownProps) { return ( ); } export type DropDownItem = [ string, string | ((editorState: EditorState) => void), string, ]; export interface DropDownFactoryProps extends HTMLAttributes { selected?: number; } export interface DropdownProps extends DropDownFactoryProps { items: DropDownItem[]; }