import { KeyboardKey } from '@/services/ioService'; import { WebApplication } from '@/ui_models/application'; import { AppState } from '@/ui_models/app_state'; import { Disclosure, DisclosureButton, DisclosurePanel, } from '@reach/disclosure'; import { ComponentArea, IconType, SNComponent, SNNote, } from '@standardnotes/snjs'; import { FunctionComponent } from 'preact'; import { useEffect, useRef, useState } from 'preact/hooks'; import { Icon } from '../Icon'; import { createEditorMenuGroups } from './changeEditor/createEditorMenuGroups'; import { ChangeEditorMenu } from './changeEditor/ChangeEditorMenu'; import { calculateSubmenuStyle, SubmenuStyle, } from '@/utils/calculateSubmenuStyle'; type ChangeEditorOptionProps = { appState: AppState; application: WebApplication; note: SNNote; closeOnBlur: (event: { relatedTarget: EventTarget | null }) => void; }; type AccordionMenuGroup = { icon?: IconType; iconClassName?: string; title: string; items: Array; }; export type EditorMenuItem = { name: string; component?: SNComponent; isEntitled: boolean; }; export type EditorMenuGroup = AccordionMenuGroup; export const ChangeEditorOption: FunctionComponent = ({ application, closeOnBlur, note, }) => { const [changeEditorMenuOpen, setChangeEditorMenuOpen] = useState(false); const [changeEditorMenuVisible, setChangeEditorMenuVisible] = useState(false); const [menuStyle, setMenuStyle] = useState({ right: 0, bottom: 0, maxHeight: 'auto', }); const changeEditorMenuRef = useRef(null); const changeEditorButtonRef = useRef(null); const [editors] = useState(() => application.componentManager .componentsForArea(ComponentArea.Editor) .sort((a, b) => { return a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1; }) ); const [editorMenuGroups, setEditorMenuGroups] = useState( [] ); const [selectedEditor, setSelectedEditor] = useState(() => application.componentManager.editorForNote(note) ); useEffect(() => { setEditorMenuGroups(createEditorMenuGroups(application, editors)); }, [application, editors]); useEffect(() => { setSelectedEditor(application.componentManager.editorForNote(note)); }, [application, note]); const toggleChangeEditorMenu = () => { if (!changeEditorMenuOpen) { const menuStyle = calculateSubmenuStyle(changeEditorButtonRef.current); if (menuStyle) { setMenuStyle(menuStyle); } } setChangeEditorMenuOpen(!changeEditorMenuOpen); }; useEffect(() => { if (changeEditorMenuOpen) { setTimeout(() => { const newMenuStyle = calculateSubmenuStyle( changeEditorButtonRef.current, changeEditorMenuRef.current ); if (newMenuStyle) { setMenuStyle(newMenuStyle); setChangeEditorMenuVisible(true); } }); } }, [changeEditorMenuOpen]); return ( { if (event.key === KeyboardKey.Escape) { setChangeEditorMenuOpen(false); } }} onBlur={closeOnBlur} ref={changeEditorButtonRef} className="sn-dropdown-item justify-between" >
Change editor
{ if (event.key === KeyboardKey.Escape) { setChangeEditorMenuOpen(false); changeEditorButtonRef.current?.focus(); } }} style={{ ...menuStyle, position: 'fixed', }} className="sn-dropdown flex flex-col max-h-120 min-w-68 fixed overflow-y-auto" > {changeEditorMenuOpen && ( )}
); };