UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

105 lines (104 loc) • 5.49 kB
import React, { useCallback, createContext, useContext, useRef } from 'react'; import { getBlockMenuAnchorMetrics } from './utils/fixBlockMenuPositionAndScroll'; const BlockMenuContext = /*#__PURE__*/createContext({ onDropdownOpenChanged: () => {}, moveDownRef: /*#__PURE__*/React.createRef(), moveUpRef: /*#__PURE__*/React.createRef(), getFirstSelectedDomNode: () => undefined, getMovedBlockDomNode: () => undefined, getSelectedBlockDomNode: () => undefined, anchorMetricsRef: { current: undefined } }); export const useBlockMenu = () => { const context = useContext(BlockMenuContext); if (!context) { throw new Error('useBlockMenu must be used within BlockMenuProvider'); } return context; }; export const BlockMenuProvider = ({ children, api, editorView }) => { const moveUpRef = useRef(null); const moveDownRef = useRef(null); const anchorMetricsRef = useRef(undefined); const getFirstSelectedDomNode = useCallback(() => { var _api$selection, _api$selection$shared, _api$selection$shared2; const from = api === null || api === void 0 ? void 0 : (_api$selection = api.selection) === null || _api$selection === void 0 ? void 0 : (_api$selection$shared = _api$selection.sharedState.currentState()) === null || _api$selection$shared === void 0 ? void 0 : (_api$selection$shared2 = _api$selection$shared.selection) === null || _api$selection$shared2 === void 0 ? void 0 : _api$selection$shared2.from; if (from !== undefined) { const nodeDOM = editorView === null || editorView === void 0 ? void 0 : editorView.nodeDOM(from); if (nodeDOM instanceof Element) { return nodeDOM; } } }, [api, editorView]); const getTopLevelBlockDomNodeAt = useCallback(from => { if (!editorView) { return; } const nodeDOM = editorView.nodeDOM(from); const domAtPos = nodeDOM instanceof Element ? nodeDOM : editorView.domAtPos(from).node; const element = domAtPos instanceof Element ? domAtPos : domAtPos.parentElement; if (!element || !editorView.dom.contains(element)) { return; } let block = element; while (block.parentElement && block.parentElement !== editorView.dom) { block = block.parentElement; } return block; }, [editorView]); const getSelectedBlockDomNode = useCallback(() => { var _api$blockControls, _ref, _blockControlsState$a, _blockControlsState$a2, _blockControlsState$m, _blockControlsState$p; const blockControlsState = api === null || api === void 0 ? void 0 : (_api$blockControls = api.blockControls) === null || _api$blockControls === void 0 ? void 0 : _api$blockControls.sharedState.currentState(); // The editor selection is not necessarily in the block the menu was opened for. const from = (_ref = (_blockControlsState$a = blockControlsState === null || blockControlsState === void 0 ? void 0 : (_blockControlsState$a2 = blockControlsState.activeNode) === null || _blockControlsState$a2 === void 0 ? void 0 : _blockControlsState$a2.pos) !== null && _blockControlsState$a !== void 0 ? _blockControlsState$a : blockControlsState === null || blockControlsState === void 0 ? void 0 : (_blockControlsState$m = blockControlsState.menuTriggerByNode) === null || _blockControlsState$m === void 0 ? void 0 : _blockControlsState$m.pos) !== null && _ref !== void 0 ? _ref : blockControlsState === null || blockControlsState === void 0 ? void 0 : (_blockControlsState$p = blockControlsState.preservedSelection) === null || _blockControlsState$p === void 0 ? void 0 : _blockControlsState$p.from; return from === undefined ? undefined : getTopLevelBlockDomNodeAt(from); }, [api, getTopLevelBlockDomNodeAt]); const getMovedBlockDomNode = useCallback(() => { // A move transaction maps its preserved selection onto the node it just moved, so this is // where that node ended up - unlike the block menu state, which keeps stale positions. const from = editorView === null || editorView === void 0 ? void 0 : editorView.state.selection.from; return from === undefined ? undefined : getTopLevelBlockDomNodeAt(from); }, [editorView, getTopLevelBlockDomNodeAt]); const onDropdownOpenChanged = useCallback(isOpen => { anchorMetricsRef.current = undefined; if (isOpen) { // The popup lays itself out next to the drag handle over the next frame or two. let remainingFrames = 3; const captureAnchorMetrics = () => { requestAnimationFrame(() => { anchorMetricsRef.current = getBlockMenuAnchorMetrics(getSelectedBlockDomNode()); remainingFrames -= 1; if (!anchorMetricsRef.current && remainingFrames > 0) { captureAnchorMetrics(); } }); }; captureAnchorMetrics(); return; } // On Dropdown closed, return focus to editor setTimeout(() => requestAnimationFrame(() => { api === null || api === void 0 ? void 0 : api.core.actions.focus({ scrollIntoView: false }); }), 1); }, [api, getSelectedBlockDomNode]); return /*#__PURE__*/React.createElement(BlockMenuContext.Provider, { // eslint-disable-next-line @atlassian/perf-linting/no-inline-context-value, @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) value: { onDropdownOpenChanged, moveDownRef, moveUpRef, getFirstSelectedDomNode, getMovedBlockDomNode, getSelectedBlockDomNode, anchorMetricsRef } }, children); };