@atlaskit/editor-plugin-media
Version:
Media plugin for @atlaskit/editor-core
72 lines • 2.52 kB
JavaScript
import React from 'react';
import { addAltText, ToolTipContent } from '@atlaskit/editor-common/keymaps';
import { MediaSharedClassNames as ClassNames } from '@atlaskit/editor-common/styles';
import { openMediaAltTextMenu } from '../pm-plugins/alt-text/commands';
import { messages } from '../pm-plugins/alt-text/messages';
import AltTextEdit, { CONTAINER_WIDTH_IN_PX } from '../pm-plugins/alt-text/ui/AltTextEdit';
import { getMediaNodeFromSelection } from '../utils/media-common';
const testId = 'alt-text-edit-button';
export const altTextButton = (intl, state, editorAnalyticsAPI) => {
const mediaNode = getMediaNodeFromSelection(state);
const message = mediaNode && mediaNode.attrs.alt ? messages.editAltText : messages.altText;
const title = intl.formatMessage(message);
return {
title,
id: 'editor.media.altText',
type: 'button',
onClick: openMediaAltTextMenu(editorAnalyticsAPI),
showTitle: true,
testId,
tooltipContent: /*#__PURE__*/React.createElement(ToolTipContent, {
description: title,
keymap: addAltText
})
};
};
export const altTextEditComponent = options => {
return {
type: 'custom',
fallback: [],
disableArrowNavigation: true,
render: (view, idx) => {
if (!view) {
return null;
}
const mediaNode = getMediaNodeFromSelection(view.state);
if (!mediaNode) {
return null;
}
/** Focus should move to the 'Alt text' button when the toolbar closes
* and not close the floating toolbar.
*/
const handleEsc = () => {
var _options$forceFocusSe;
const {
state: {
tr
},
dispatch
} = view;
const newTr = options === null || options === void 0 ? void 0 : (_options$forceFocusSe = options.forceFocusSelector) === null || _options$forceFocusSe === void 0 ? void 0 : _options$forceFocusSe.call(options, `[data-testid="${testId}"]`)(tr);
if (newTr) {
dispatch(newTr);
}
};
return /*#__PURE__*/React.createElement(AltTextEdit, {
view: view,
key: idx,
value: mediaNode.attrs.alt,
altTextValidator: options && options.altTextValidator,
onEscape: handleEsc
});
}
};
};
export const getAltTextToolbar = (toolbarBaseConfig, options) => {
return {
...toolbarBaseConfig,
width: CONTAINER_WIDTH_IN_PX,
className: ClassNames.FLOATING_TOOLBAR_COMPONENT,
items: [altTextEditComponent(options)]
};
};