UNPKG

@atlaskit/editor-plugin-block-menu

Version:

BlockMenu plugin for @atlaskit/editor-core

54 lines 2.29 kB
import React, { useCallback, createContext, useContext, useRef } from 'react'; var BlockMenuContext = /*#__PURE__*/createContext({ onDropdownOpenChanged: function onDropdownOpenChanged() {}, moveDownRef: /*#__PURE__*/React.createRef(), moveUpRef: /*#__PURE__*/React.createRef(), getFirstSelectedDomNode: function getFirstSelectedDomNode() { return undefined; } }); export var useBlockMenu = function useBlockMenu() { var context = useContext(BlockMenuContext); if (!context) { throw new Error('useBlockMenu must be used within BlockMenuProvider'); } return context; }; export var BlockMenuProvider = function BlockMenuProvider(_ref) { var children = _ref.children, api = _ref.api, editorView = _ref.editorView; var moveUpRef = useRef(null); var moveDownRef = useRef(null); var getFirstSelectedDomNode = useCallback(function () { var _api$selection; var from = api === null || api === void 0 || (_api$selection = api.selection) === null || _api$selection === void 0 || (_api$selection = _api$selection.sharedState.currentState()) === null || _api$selection === void 0 || (_api$selection = _api$selection.selection) === null || _api$selection === void 0 ? void 0 : _api$selection.from; if (from !== undefined) { var nodeDOM = editorView === null || editorView === void 0 ? void 0 : editorView.nodeDOM(from); if (nodeDOM instanceof Element) { return nodeDOM; } } }, [api, editorView]); var onDropdownOpenChanged = useCallback(function (isOpen) { if (!isOpen) { // On Dropdown closed, return focus to editor setTimeout(function () { return requestAnimationFrame(function () { api === null || api === void 0 || api.core.actions.focus({ scrollIntoView: false }); }); }, 1); } }, [api]); 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: onDropdownOpenChanged, moveDownRef: moveDownRef, moveUpRef: moveUpRef, getFirstSelectedDomNode: getFirstSelectedDomNode } }, children); };