UNPKG

@atlaskit/editor-plugin-insert-block

Version:

Insert block plugin for @atlaskit/editor-core

159 lines • 7.17 kB
/* RegisteredInsertMenu.tsx generated by @compiled/babel-plugin v3.0.4 */ import _slicedToArray from "@babel/runtime/helpers/slicedToArray"; import "./RegisteredInsertMenu.compiled.css"; import { ax, ix } from "@compiled/react/runtime"; import React, { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { useIntl } from 'react-intl'; import { insertSelectedItem } from '@atlaskit/editor-common/insert'; import { toolbarInsertBlockMessages } from '@atlaskit/editor-common/messages/insert-block'; import { MENU } from '@atlaskit/editor-common/quick-insert/keys'; import { RegisteredMenuContext } from '@atlaskit/editor-common/registered-menu/context'; import { createRegisteredMenuSurfaceContext } from '@atlaskit/editor-common/registered-menu/surface-context'; import { typeAheadListMessages } from '@atlaskit/editor-common/type-ahead/messages'; import { Box } from '@atlaskit/primitives/compiled/box'; import Textfield from '@atlaskit/textfield/text-field'; var DEFAULT_MENU_MAX_HEIGHT = 520; var SEARCH_HEIGHT = 64; var styles = { menu: "_2rko1mok _1reo15vq _18m915vq _bfhk1bhr _16qs130s _1e0c1txw _2lx21bp4 _1bsb1edt", hidden: "_1e0cglyw", search: "_18zrutpp _1o9zidpf _1q51utpp _85i5u2gc" }; export var RegisteredInsertMenu = function RegisteredInsertMenu(props) { var _props$maxHeight, _interaction$expanded; var components = props.components, editorView = props.editorView, isOffline = props.isOffline, onSelect = props.onSelect; var _useIntl = useIntl(), formatMessage = _useIntl.formatMessage; var _useState = useState(), _useState2 = _slicedToArray(_useState, 2), interaction = _useState2[0], setInteraction = _useState2[1]; var registerInteraction = useCallback(function (next) { setInteraction(next); return function () { return setInteraction(function (current) { return current === next ? undefined : current; }); }; }, []); var searchRef = useRef(null); var hasInteraction = Boolean(interaction); useLayoutEffect(function () { if (hasInteraction) { var _searchRef$current; (_searchRef$current = searchRef.current) === null || _searchRef$current === void 0 || _searchRef$current.focus({ preventScroll: true }); } }, [hasInteraction]); var maxHeight = (_props$maxHeight = props.maxHeight) !== null && _props$maxHeight !== void 0 ? _props$maxHeight : DEFAULT_MENU_MAX_HEIGHT; var menuStyle = useMemo(function () { return { maxHeight: maxHeight }; }, [maxHeight]); var _useState3 = useState(function () { return Symbol('registered-insert-menu-open'); }), _useState4 = _slicedToArray(_useState3, 1), menuOpenId = _useState4[0]; var _useState5 = useState(''), _useState6 = _slicedToArray(_useState5, 2), query = _useState6[0], setQuery = _useState6[1]; var surfaceContext = useMemo(function () { return createRegisteredMenuSurfaceContext({ menuOpenId: menuOpenId, surface: 'toolbar' }); }, [menuOpenId]); var Root = useMemo(function () { var _components$find; return (_components$find = components.find(function (_ref) { var key = _ref.key, type = _ref.type, parents = _ref.parents; return key === MENU.key && type === MENU.type && !(parents !== null && parents !== void 0 && parents.length); })) === null || _components$find === void 0 ? void 0 : _components$find.component; }, [components]); // Interaction updates should not recreate the registered component's own props. var menu = useMemo(function () { return Root && /*#__PURE__*/React.createElement(Root, null, null); }, [Root]); var select = useCallback(function (selection) { onSelect(); queueMicrotask(function () { var state = editorView.state; var insert = function insert(content) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return insertSelectedItem(content, options)(state, state.tr, state.selection.head); }; var transaction = selection(insert); if (transaction) { editorView.dispatch(transaction); } }); }, [editorView, onSelect]); var registeredMenu = useMemo(function () { return { components: components, onClose: props.onClose, maxHeight: Math.max(0, maxHeight - SEARCH_HEIGHT), registerInteraction: registerInteraction, editorView: editorView, isOffline: isOffline, menuOpenId: menuOpenId, rootComponent: MENU, query: query, select: select, surfaceContext: surfaceContext }; }, [components, props.onClose, maxHeight, registerInteraction, editorView, isOffline, menuOpenId, query, select, surfaceContext]); var onQueryChange = useCallback(function (event) { return setQuery(event.currentTarget.value); }, []); var onKeyDown = useCallback(function (event) { if (event.key === 'Escape') { var _interaction$onClose; event.preventDefault(); interaction === null || interaction === void 0 || (_interaction$onClose = interaction.onClose) === null || _interaction$onClose === void 0 || _interaction$onClose.call(interaction); props.onClose(); return; } // Some IME key events report 229 without setting isComposing. Let the input finish // composing text before handling menu navigation or selection. if (event.target !== searchRef.current || event.nativeEvent.isComposing || event.keyCode === 229) { return; } if (interaction !== null && interaction !== void 0 && interaction.onKeyDown(event.nativeEvent)) { event.preventDefault(); } }, [interaction, props.onClose]); return /*#__PURE__*/React.createElement(RegisteredMenuContext.Provider, { value: registeredMenu }, Root && /*#__PURE__*/React.createElement("div", { "data-registered-insert-menu-root": "", onKeyDownCapture: onKeyDown, "data-keyboard-navigation-independent": "", "data-testid": "registered-insert-menu", // eslint-disable-next-line @atlaskit/ui-styling-standard/enforce-style-prop -- The popup supplies the available height at runtime. style: menuStyle, className: ax([styles.menu, !interaction && styles.hidden]) }, /*#__PURE__*/React.createElement(Box, { xcss: styles.search }, /*#__PURE__*/React.createElement(Textfield, { "aria-label": (interaction === null || interaction === void 0 ? void 0 : interaction.label) || formatMessage(toolbarInsertBlockMessages.insertMenu), "aria-activedescendant": interaction === null || interaction === void 0 ? void 0 : interaction.activeDescendantId, "aria-controls": interaction === null || interaction === void 0 ? void 0 : interaction.listId, "aria-expanded": (_interaction$expanded = interaction === null || interaction === void 0 ? void 0 : interaction.expanded) !== null && _interaction$expanded !== void 0 ? _interaction$expanded : false, "aria-autocomplete": "list", placeholder: formatMessage(typeAheadListMessages.quickInsertInputPlaceholderLabel), ref: searchRef, role: "combobox", value: query, onChange: onQueryChange })), menu)); };