@atlaskit/editor-plugin-block-menu
Version:
BlockMenu plugin for @atlaskit/editor-core
51 lines • 2.13 kB
JavaScript
import React, { useCallback, createContext, useContext, useRef } from 'react';
const BlockMenuContext = /*#__PURE__*/createContext({
onDropdownOpenChanged: () => {},
moveDownRef: /*#__PURE__*/React.createRef(),
moveUpRef: /*#__PURE__*/React.createRef(),
getFirstSelectedDomNode: () => 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 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 onDropdownOpenChanged = useCallback(isOpen => {
if (!isOpen) {
// On Dropdown closed, return focus to editor
setTimeout(() => requestAnimationFrame(() => {
api === null || api === void 0 ? 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,
moveDownRef,
moveUpRef,
getFirstSelectedDomNode
}
}, children);
};