@atlaskit/editor-plugin-insert-block
Version:
Insert block plugin for @atlaskit/editor-core
135 lines • 6.21 kB
JavaScript
/* RegisteredInsertMenu.tsx generated by @compiled/babel-plugin v3.0.4 */
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';
const DEFAULT_MENU_MAX_HEIGHT = 520;
const SEARCH_HEIGHT = 64;
const styles = {
menu: "_2rko1mok _1reo15vq _18m915vq _bfhk1bhr _16qs130s _1e0c1txw _2lx21bp4 _1bsb1edt",
hidden: "_1e0cglyw",
search: "_18zrutpp _1o9zidpf _1q51utpp _85i5u2gc"
};
export const RegisteredInsertMenu = props => {
var _props$maxHeight, _interaction$expanded;
const {
components,
editorView,
isOffline,
onSelect
} = props;
const {
formatMessage
} = useIntl();
const [interaction, setInteraction] = useState();
const registerInteraction = useCallback(next => {
setInteraction(next);
return () => setInteraction(current => current === next ? undefined : current);
}, []);
const searchRef = useRef(null);
const hasInteraction = Boolean(interaction);
useLayoutEffect(() => {
if (hasInteraction) {
var _searchRef$current;
(_searchRef$current = searchRef.current) === null || _searchRef$current === void 0 ? void 0 : _searchRef$current.focus({
preventScroll: true
});
}
}, [hasInteraction]);
const maxHeight = (_props$maxHeight = props.maxHeight) !== null && _props$maxHeight !== void 0 ? _props$maxHeight : DEFAULT_MENU_MAX_HEIGHT;
const menuStyle = useMemo(() => ({
maxHeight
}), [maxHeight]);
const [menuOpenId] = useState(() => Symbol('registered-insert-menu-open'));
const [query, setQuery] = useState('');
const surfaceContext = useMemo(() => createRegisteredMenuSurfaceContext({
menuOpenId,
surface: 'toolbar'
}), [menuOpenId]);
const Root = useMemo(() => {
var _components$find;
return (_components$find = components.find(({
key,
type,
parents
}) => 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.
const menu = useMemo(() => Root && /*#__PURE__*/React.createElement(Root, null, null), [Root]);
const select = useCallback(selection => {
onSelect();
queueMicrotask(() => {
const {
state
} = editorView;
const insert = (content, options = {}) => insertSelectedItem(content, options)(state, state.tr, state.selection.head);
const transaction = selection(insert);
if (transaction) {
editorView.dispatch(transaction);
}
});
}, [editorView, onSelect]);
const registeredMenu = useMemo(() => ({
components,
onClose: props.onClose,
maxHeight: Math.max(0, maxHeight - SEARCH_HEIGHT),
registerInteraction,
editorView,
isOffline,
menuOpenId,
rootComponent: MENU,
query,
select,
surfaceContext
}), [components, props.onClose, maxHeight, registerInteraction, editorView, isOffline, menuOpenId, query, select, surfaceContext]);
const onQueryChange = useCallback(event => setQuery(event.currentTarget.value), []);
const onKeyDown = useCallback(event => {
if (event.key === 'Escape') {
var _interaction$onClose;
event.preventDefault();
interaction === null || interaction === void 0 ? void 0 : (_interaction$onClose = interaction.onClose) === null || _interaction$onClose === void 0 ? 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));
};