import React, { useEffect, useRef, useState } from 'react'; import { ICommand } from '.'; import { IMarkdownEditor, ToolBarProps } from '..'; interface FullscreenButtonProps extends Omit, 'onClick'> { command: ICommand; editorProps: IMarkdownEditor & ToolBarProps; onClick?: (evn: React.MouseEvent, isFull: boolean) => void; } export const FullscreenButton: React.FC = (props) => { const { editorProps, command, onClick, ...reset } = props; const $height = useRef(0); const [full, setFull] = useState(false); const fullRef = useRef(full); const entriesHandle: ResizeObserverCallback = (entries: ResizeObserverEntry[]) => { for (const entry of entries) { if (!$height.current) { $height.current = entry.target.clientHeight; } if (editorProps.editor?.current?.view?.dom) { if (fullRef.current) { editorProps.editor.current.view.dom.style.height = `${entry.target.clientHeight}px`; } else { editorProps.editor.current.view.dom.removeAttribute('style'); } } } robserver.current?.disconnect(); robserver.current = undefined; }; const robserver = useRef(new ResizeObserver(entriesHandle)); useEffect(() => { if (!robserver.current) { robserver.current = new ResizeObserver(entriesHandle); } if ( editorProps.containerEditor && editorProps.containerEditor.current && editorProps.containerEditor.current.parentElement && robserver.current ) { const parentElement = editorProps.containerEditor.current.parentElement; robserver.current.observe(parentElement); } return () => { if (robserver.current) { robserver.current.disconnect(); robserver.current = undefined; } }; }, [editorProps.containerEditor, entriesHandle, editorProps.editor, full, robserver]); useEffect(() => { if (!document) return; if (editorProps && editorProps.container && editorProps.container.current && editorProps.editor) { const container = editorProps.container.current; document.body.style.overflow = full ? 'hidden' : 'initial'; full ? document.body.classList.add(`${editorProps.prefixCls}-fullscreen`) : document.body.classList.remove(`${editorProps.prefixCls}-fullscreen`); if (container && full) { container.style.zIndex = '999'; container.style.position = 'fixed'; container.style.top = '0px'; container.style.bottom = '0px'; container.style.left = '0px'; container.style.right = '0px'; } else if (container) { container.style.position = 'initial'; container.style.top = 'initial'; container.style.bottom = 'initial'; container.style.left = 'initial'; container.style.right = 'initial'; } } }, [full, editorProps]); const click = (evn: React.MouseEvent) => { let isFull = !full; fullRef.current = isFull; setFull(isFull); onClick?.(evn, isFull); }; return ( ); }; export const fullscreen: ICommand = { name: 'fullscreen', keyCommand: 'fullscreen', button: (command, props, opts) => , icon: ( ), };