()
| 7 | import { useNodeFocused } from './use-node-focused' |
| 8 | |
| 9 | const useGridSelection = () => { |
| 10 | const editor = useEditableStatic() |
| 11 | const grid = useGrid() |
| 12 | // selection |
| 13 | const [selection, setSelection] = React.useState<GridSelection | null>(null) |
| 14 | const nodeFocused = useNodeFocused() |
| 15 | |
| 16 | useIsomorphicLayoutEffect(() => { |
| 17 | if (grid && nodeFocused) { |
| 18 | const selection = Grid.getSelection(editor, [grid, Editable.findPath(editor, grid)]) |
| 19 | if (selection) { |
| 20 | setSelection(prev => { |
| 21 | if ( |
| 22 | !prev || |
| 23 | !Path.equals(prev.start, selection.start) || |
| 24 | !Path.equals(prev.end, selection.end) |
| 25 | ) { |
| 26 | const path = Editable.findPath(editor, grid) |
| 27 | const startPath = path.concat(selection.start) |
| 28 | const endPath = path.concat(selection.end) |
| 29 | const edgeSelection = Grid.edges(editor, [grid, path], selection) |
| 30 | const { start: tableStart, end: tableEnd } = Grid.span( |
| 31 | editor, |
| 32 | [grid, path], |
| 33 | edgeSelection, |
| 34 | ) |
| 35 | const selStart = path.concat(tableStart) |
| 36 | const selEnd = path.concat(tableEnd) |
| 37 | // 有合并的单元格时选择区域会变大,所以需要重新select |
| 38 | if (!Path.equals(startPath, selStart) || !Path.equals(endPath, selEnd)) { |
| 39 | Grid.select(editor, [grid, path], edgeSelection) |
| 40 | return prev |
| 41 | } |
| 42 | return selection |
| 43 | } |
| 44 | return prev |
| 45 | }) |
| 46 | return |
| 47 | } |
| 48 | } |
| 49 | setSelection(null) |
| 50 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| 51 | }, [editor, editor.selection, nodeFocused]) |
| 52 | |
| 53 | return selection |
| 54 | } |
| 55 | |
| 56 | export { useGridSelection } |
no test coverage detected
searching dependent graphs…