import type { FC } from 'react'; import { useState } from 'react'; import { styled } from '@toeverything/components/ui'; import { BlockEditor } from '@toeverything/framework/virgo'; const GRID_ADD_HANDLE_NAME = 'grid-add-handle'; type GridHandleProps = { editor: BlockEditor; onDrag?: (e: MouseEvent) => void; onMouseDown?: React.MouseEventHandler; blockId: string; enabledAddItem: boolean; draggable: boolean; alertHandleId: string; onMouseEnter?: React.MouseEventHandler; }; export const GridHandle: FC = function ({ blockId, editor, enabledAddItem, onDrag, onMouseDown, draggable, alertHandleId, onMouseEnter, }) { const [isMouseDown, setIsMouseDown] = useState(false); const handleMouseDown: React.MouseEventHandler = e => { if (draggable) { const cb = (e: MouseEvent) => { onDrag && onDrag(e); }; onMouseDown && onMouseDown(e); setIsMouseDown(true); editor.mouseManager.onMouseMove(cb); editor.mouseManager.onMouseupEventOnce(() => { setIsMouseDown(false); editor.mouseManager.offMouseMove(cb); }); } }; const handleCreateGridItem = async () => { const [, textBlock] = await editor.commands.blockCommands.createGridItem(blockId); if (textBlock) { editor.selectionManager.setActivatedNodeId(textBlock.id); } }; const handleMouseEnter: React.MouseEventHandler = e => { onMouseEnter && onMouseEnter(e); }; return ( {enabledAddItem ? ( + ) : null} ); }; const GridHandleContainer = styled('div')<{ isMouseDown: boolean; isAlert: boolean; }>(({ theme, isMouseDown, isAlert }) => ({ position: 'relative', width: '10px', flexGrow: '0', flexShrink: '0', padding: '5px 4.5px', minHeight: '15px', cursor: 'col-resize', borderRadius: '1px', backgroundClip: 'content-box', ' &:hover': { backgroundColor: isAlert ? 'red' : theme.affine.palette.primary, [`.${GRID_ADD_HANDLE_NAME}`]: { display: 'block', }, }, ...(isMouseDown && (isAlert ? { backgroundColor: 'red' } : { backgroundColor: theme.affine.palette.primary })), })); const AddGridHandle = styled('div')(({ theme }) => ({ display: 'none', position: 'absolute', transition: 'all 0.2s ease-in-out', height: '20px', lineHeight: '18px', width: '10px', borderRadius: '6px', top: 'calc(50% - 10px)', left: '0px', cursor: 'pointer', color: theme.affine.palette.primary, overflow: 'hidden', textAlign: 'center', backgroundColor: theme.affine.palette.white, '&:hover': { fontWeight: 'bold', }, }));