UNPKG

@atlaskit/editor-plugin-media

Version:

Media plugin for @atlaskit/editor-core

87 lines (85 loc) 4.28 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { withAnalytics } from '@atlaskit/editor-common/editor-analytics'; import { NodeSelection } from '@atlaskit/editor-prosemirror/state'; import { getMediaSingleOrInlineNodeFromSelection, isMediaSingleOrInlineNodeSelected } from '../../pm-plugins/utils/media-common'; import { getNodeType } from '../../ui/toolbar/commands'; import { createCommand } from './index'; var createCommandWithAnalytics = function createCommandWithAnalytics(actionType, action, transform) { return function (editorAnalyticsAPI) { return withAnalytics(editorAnalyticsAPI, function (state) { var mediaNode = getMediaSingleOrInlineNodeFromSelection(state); var type = getNodeType(state); return { action: actionType, actionSubject: ACTION_SUBJECT.MEDIA, actionSubjectId: ACTION_SUBJECT_ID.ALT_TEXT, eventType: EVENT_TYPE.TRACK, attributes: { type: type, // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion mediaType: mediaNode.attrs.type } }; })(createCommand(action, transform)); }; }; // pass in undefined to close the alt text menu without saving export var closeMediaAltTextMenuAndSave = function closeMediaAltTextMenuAndSave(altText) { var commandTransform = typeof altText === 'string' ? updateAltTextTransform(altText) : undefined; return createCommand(function (state) { if (isMediaSingleOrInlineNodeSelected(state)) { return { type: 'closeMediaAltTextMenu' }; } return false; }, commandTransform); }; export var closeMediaAltTextMenu = closeMediaAltTextMenuAndSave(); export var openMediaAltTextMenu = createCommandWithAnalytics(ACTION.OPENED, function (state) { if (isMediaSingleOrInlineNodeSelected(state)) { return { type: 'openMediaAltTextMenu' }; } return false; }, function (tr) { return tr.setMeta('scrollIntoView', false); }); var updateAltTextTransform = function updateAltTextTransform(newAltText) { return function (tr, state) { var mediaNode = getMediaSingleOrInlineNodeFromSelection(state); if (mediaNode) { // mediaSingle or mediaInline var originalSelectionPos = tr.selection.from; var mediaPos = mediaNode.type === state.schema.nodes.media ? originalSelectionPos + 1 // media inside mediaSingle : originalSelectionPos; // mediaInline /** * Any changes to attributes of a node count the node as "recreated" in Prosemirror[1] * This makes it so Prosemirror resets the selection to the child i.e. "media" instead of "media-single" * The recommended fix is to reset the selection.[2] * * [1] https://discuss.prosemirror.net/t/setnodemarkup-loses-current-nodeselection/976 * [2] https://discuss.prosemirror.net/t/setnodemarkup-and-deselect/3673 */ tr.setMeta('scrollIntoView', false).setNodeMarkup(mediaPos, undefined, _objectSpread(_objectSpread({}, mediaNode.attrs), {}, { alt: newAltText })).setSelection(NodeSelection.create(tr.doc, originalSelectionPos)); } return tr; }; }; export var updateAltText = function updateAltText(newAltText) { return createCommand(function (state) { if (isMediaSingleOrInlineNodeSelected(state)) { return { type: 'updateAltText' }; } return false; }, updateAltTextTransform(newAltText)); };