-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lexical-playground] Table Hover Action Buttons (#6355)
Co-authored-by: Ivaylo Pavlov <[email protected]>
- Loading branch information
Showing
4 changed files
with
252 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
247 changes: 247 additions & 0 deletions
247
packages/lexical-playground/src/plugins/TableHoverActionsPlugin/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext'; | ||
import { | ||
$getTableColumnIndexFromTableCellNode, | ||
$getTableRowIndexFromTableCellNode, | ||
$insertTableColumn__EXPERIMENTAL, | ||
$insertTableRow__EXPERIMENTAL, | ||
$isTableCellNode, | ||
$isTableNode, | ||
TableCellNode, | ||
TableNode, | ||
TableRowNode, | ||
} from '@lexical/table'; | ||
import {$findMatchingParent, mergeRegister} from '@lexical/utils'; | ||
import {$getNearestNodeFromDOMNode} from 'lexical'; | ||
import {useEffect, useRef, useState} from 'react'; | ||
import * as React from 'react'; | ||
import {createPortal} from 'react-dom'; | ||
|
||
import {useDebounce} from '../CodeActionMenuPlugin/utils'; | ||
|
||
const BUTTON_WIDTH_PX = 20; | ||
|
||
function TableHoverActionsContainer({ | ||
anchorElem, | ||
}: { | ||
anchorElem: HTMLElement; | ||
}): JSX.Element { | ||
const [editor] = useLexicalComposerContext(); | ||
const [isShownRow, setShownRow] = useState<boolean>(false); | ||
const [isShownColumn, setShownColumn] = useState<boolean>(false); | ||
const [shouldListenMouseMove, setShouldListenMouseMove] = | ||
useState<boolean>(false); | ||
const [position, setPosition] = useState({}); | ||
const codeSetRef = useRef<Set<string>>(new Set()); | ||
const tableDOMNodeRef = useRef<HTMLElement | null>(null); | ||
|
||
const debouncedOnMouseMove = useDebounce( | ||
(event: MouseEvent) => { | ||
const {isOutside, tableDOMNode} = getMouseInfo(event); | ||
|
||
if (isOutside) { | ||
setShownRow(false); | ||
setShownColumn(false); | ||
return; | ||
} | ||
|
||
if (!tableDOMNode) { | ||
return; | ||
} | ||
|
||
tableDOMNodeRef.current = tableDOMNode; | ||
|
||
let hoveredRowNode: TableCellNode | null = null; | ||
let hoveredColumnNode: TableCellNode | null = null; | ||
let tableDOMElement: HTMLElement | null = null; | ||
|
||
editor.update(() => { | ||
const maybeTableCell = $getNearestNodeFromDOMNode(tableDOMNode); | ||
|
||
if ($isTableCellNode(maybeTableCell)) { | ||
const table = $findMatchingParent(maybeTableCell, (node) => | ||
$isTableNode(node), | ||
); | ||
if (!$isTableNode(table)) { | ||
return; | ||
} | ||
|
||
tableDOMElement = editor.getElementByKey(table?.getKey()); | ||
|
||
if (tableDOMElement) { | ||
const rowCount = table.getChildrenSize(); | ||
const colCount = ( | ||
(table as TableNode).getChildAtIndex(0) as TableRowNode | ||
)?.getChildrenSize(); | ||
|
||
const rowIndex = $getTableRowIndexFromTableCellNode(maybeTableCell); | ||
const colIndex = | ||
$getTableColumnIndexFromTableCellNode(maybeTableCell); | ||
|
||
if (rowIndex === rowCount - 1) { | ||
hoveredRowNode = maybeTableCell; | ||
} else if (colIndex === colCount - 1) { | ||
hoveredColumnNode = maybeTableCell; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
if (tableDOMElement) { | ||
const { | ||
width: tableElemWidth, | ||
y: tableElemY, | ||
x: tableElemX, | ||
right: tableElemRight, | ||
bottom: tableElemBottom, | ||
height: tableElemHeight, | ||
} = (tableDOMElement as HTMLTableElement).getBoundingClientRect(); | ||
|
||
const {y: editorElemY} = anchorElem.getBoundingClientRect(); | ||
|
||
if (hoveredRowNode) { | ||
setShownRow(true); | ||
setPosition({ | ||
height: BUTTON_WIDTH_PX, | ||
left: tableElemX, | ||
top: tableElemBottom - editorElemY + 5, | ||
width: tableElemWidth, | ||
}); | ||
} else if (hoveredColumnNode) { | ||
setShownColumn(true); | ||
setPosition({ | ||
height: tableElemHeight, | ||
left: tableElemRight + 5, | ||
top: tableElemY - editorElemY, | ||
width: BUTTON_WIDTH_PX, | ||
}); | ||
} | ||
} | ||
}, | ||
50, | ||
250, | ||
); | ||
|
||
useEffect(() => { | ||
if (!shouldListenMouseMove) { | ||
return; | ||
} | ||
|
||
document.addEventListener('mousemove', debouncedOnMouseMove); | ||
|
||
return () => { | ||
setShownRow(false); | ||
setShownColumn(false); | ||
debouncedOnMouseMove.cancel(); | ||
document.removeEventListener('mousemove', debouncedOnMouseMove); | ||
}; | ||
}, [shouldListenMouseMove, debouncedOnMouseMove]); | ||
|
||
useEffect(() => { | ||
return mergeRegister( | ||
editor.registerMutationListener(TableNode, (mutations) => { | ||
editor.getEditorState().read(() => { | ||
for (const [key, type] of mutations) { | ||
switch (type) { | ||
case 'created': | ||
codeSetRef.current.add(key); | ||
setShouldListenMouseMove(codeSetRef.current.size > 0); | ||
break; | ||
|
||
case 'destroyed': | ||
codeSetRef.current.delete(key); | ||
setShouldListenMouseMove(codeSetRef.current.size > 0); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
}); | ||
}), | ||
); | ||
}, []); | ||
|
||
const insertAction = (insertRow: boolean) => { | ||
editor.update(() => { | ||
if (tableDOMNodeRef.current) { | ||
const maybeTableNode = $getNearestNodeFromDOMNode( | ||
tableDOMNodeRef.current, | ||
); | ||
maybeTableNode?.selectEnd(); | ||
if (insertRow) { | ||
$insertTableRow__EXPERIMENTAL(); | ||
setShownRow(false); | ||
} else { | ||
$insertTableColumn__EXPERIMENTAL(); | ||
setShownColumn(false); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
{isShownRow && ( | ||
<button | ||
className={'PlaygroundEditorTheme__tableAddRows'} | ||
style={{...position}} | ||
onClick={() => insertAction(true)} | ||
/> | ||
)} | ||
{isShownColumn && ( | ||
<button | ||
className={'PlaygroundEditorTheme__tableAddColumns'} | ||
style={{...position}} | ||
onClick={() => insertAction(false)} | ||
/> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
function getMouseInfo(event: MouseEvent): { | ||
tableDOMNode: HTMLElement | null; | ||
isOutside: boolean; | ||
} { | ||
const target = event.target; | ||
|
||
if (target && target instanceof HTMLElement) { | ||
const tableDOMNode = target.closest<HTMLElement>( | ||
'td.PlaygroundEditorTheme__tableCell, th.PlaygroundEditorTheme__tableCell', | ||
); | ||
|
||
const isOutside = !( | ||
tableDOMNode || | ||
target.closest<HTMLElement>( | ||
'button.PlaygroundEditorTheme__tableAddRows', | ||
) || | ||
target.closest<HTMLElement>( | ||
'button.PlaygroundEditorTheme__tableAddColumns', | ||
) || | ||
target.closest<HTMLElement>('div.TableCellResizer__resizer') | ||
); | ||
|
||
return {isOutside, tableDOMNode}; | ||
} else { | ||
return {isOutside: true, tableDOMNode: null}; | ||
} | ||
} | ||
|
||
export default function TableHoverActionsPlugin({ | ||
anchorElem = document.body, | ||
}: { | ||
anchorElem?: HTMLElement; | ||
}): React.ReactPortal | null { | ||
return createPortal( | ||
<TableHoverActionsContainer anchorElem={anchorElem} />, | ||
anchorElem, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters