UNPKG

@atlaskit/editor-plugin-media

Version:

Media plugin for @atlaskit/editor-core

82 lines (81 loc) 4.81 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.CommentBadgeWrapper = void 0; var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); var _react = _interopRequireWildcard(require("react")); var _analytics = require("@atlaskit/editor-common/analytics"); var _hooks = require("@atlaskit/editor-common/hooks"); var _mediaSingle = require("@atlaskit/editor-common/media-single"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } var selector = function selector(states) { var _states$annotationSta, _states$annotationSta2, _states$annotationSta3; return { selectedAnnotations: (_states$annotationSta = states.annotationState) === null || _states$annotationSta === void 0 ? void 0 : _states$annotationSta.selectedAnnotations, isInlineCommentViewClosed: (_states$annotationSta2 = states.annotationState) === null || _states$annotationSta2 === void 0 ? void 0 : _states$annotationSta2.isInlineCommentViewClosed, annotations: (_states$annotationSta3 = states.annotationState) === null || _states$annotationSta3 === void 0 ? void 0 : _states$annotationSta3.annotations }; }; var CommentBadgeWrapper = exports.CommentBadgeWrapper = function CommentBadgeWrapper(_ref) { var api = _ref.api, mediaNode = _ref.mediaNode, view = _ref.view, getPos = _ref.getPos, isDrafting = _ref.isDrafting; var _useState = (0, _react.useState)(false), _useState2 = (0, _slicedToArray2.default)(_useState, 2), entered = _useState2[0], setEntered = _useState2[1]; var _useSharedPluginState = (0, _hooks.useSharedPluginStateWithSelector)(api, ['annotation'], selector), selectedAnnotations = _useSharedPluginState.selectedAnnotations, isInlineCommentViewClosed = _useSharedPluginState.isInlineCommentViewClosed, annotations = _useSharedPluginState.annotations; var _view$state$schema = view.state.schema, media = _view$state$schema.nodes.media, annotation = _view$state$schema.marks.annotation, state = view.state, dispatch = view.dispatch; var status = (0, _react.useMemo)(function () { if (!selectedAnnotations || !mediaNode) { return 'default'; } return selectedAnnotations.some(function (annotation) { return !!mediaNode.marks.find(function (mark) { return mark.attrs.id === annotation.id; }); }) && !isInlineCommentViewClosed ? 'active' : 'default'; }, [selectedAnnotations, isInlineCommentViewClosed, mediaNode]); var onClick = (0, _react.useCallback)(function () { if (api !== null && api !== void 0 && api.annotation && mediaNode) { var showCommentForBlockNode = api.annotation.actions.showCommentForBlockNode; showCommentForBlockNode(mediaNode, _analytics.VIEW_METHOD.BADGE)(state, dispatch); } }, [api === null || api === void 0 ? void 0 : api.annotation, dispatch, mediaNode, state]); var pos = getPos(); var hasNoComments = !Number.isFinite(pos) || !annotations || !mediaNode || mediaNode.type !== media || mediaNode.marks.every(function (maybeAnnotation) { return maybeAnnotation.type !== annotation || !(maybeAnnotation.attrs.id in annotations) || annotations[maybeAnnotation.attrs.id]; }); if (!isDrafting && hasNoComments || !mediaNode) { return null; } var maybeMediaSingleElement = view.domAtPos(pos + 1).node; var mediaSingleElement = maybeMediaSingleElement instanceof HTMLElement ? maybeMediaSingleElement : null; return /*#__PURE__*/_react.default.createElement(_mediaSingle.CommentBadgeNext, { onClick: onClick, mediaSingleElement: mediaSingleElement, status: entered ? 'entered' : status // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , onMouseEnter: function onMouseEnter() { return setEntered(true); } // eslint-disable-next-line @atlassian/perf-linting/no-unstable-inline-props -- Ignored via go/ees017 (to be fixed) , onMouseLeave: function onMouseLeave() { return setEntered(false); } }); };