UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

66 lines (64 loc) 3.81 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 React, { useEffect } from 'react'; import { useAnalyticsEvents } from '@atlaskit/analytics-next/useAnalyticsEvents'; import { useSmartLinkContext } from '@atlaskit/link-provider'; import { setResolvedToolbarAttributes } from '../pm-plugins/actions'; import { getPluginState } from '../pm-plugins/util/state'; import { getResolvedAttributesFromStore } from '../pm-plugins/utils'; import { EditorAnalyticsContext } from './EditorAnalyticsContext'; var areResolvedAttributesEqual = function areResolvedAttributesEqual(current, next) { return (current === null || current === void 0 ? void 0 : current.displayCategory) === (next === null || next === void 0 ? void 0 : next.displayCategory) && (current === null || current === void 0 ? void 0 : current.extensionKey) === (next === null || next === void 0 ? void 0 : next.extensionKey) && (current === null || current === void 0 ? void 0 : current.status) === (next === null || next === void 0 ? void 0 : next.status) && (current === null || current === void 0 ? void 0 : current.statusDetails) === (next === null || next === void 0 ? void 0 : next.statusDetails); }; /** * Handles firing the toolbar viewed event */ var ToolbarViewedEventBase = function ToolbarViewedEventBase(_ref) { var url = _ref.url, display = _ref.display, cardContext = _ref.cardContext, editorView = _ref.editorView; var _useAnalyticsEvents = useAnalyticsEvents(), createAnalyticsEvent = _useAnalyticsEvents.createAnalyticsEvent; var store = cardContext === null || cardContext === void 0 ? void 0 : cardContext.store; useEffect(function () { var _getPluginState; var resolvedAttributes = getResolvedAttributesFromStore(url, display, store); createAnalyticsEvent({ action: 'viewed', actionSubject: 'inlineDialog', actionSubjectId: 'editLinkToolbar', eventType: 'ui', attributes: _objectSpread(_objectSpread({}, resolvedAttributes), {}, { display: display }) }).fire('media'); if (!editorView) { return; } var currentResolvedAttributes = (_getPluginState = getPluginState(editorView.state)) === null || _getPluginState === void 0 ? void 0 : _getPluginState.resolvedToolbarAttributesByUrl[url]; if (areResolvedAttributesEqual(currentResolvedAttributes, resolvedAttributes)) { return; } editorView.dispatch(setResolvedToolbarAttributes(url, resolvedAttributes)(editorView.state.tr)); }, [createAnalyticsEvent, display, editorView, url, store]); return null; }; /** * Provides analytics context and card context */ export var ToolbarViewedEvent = function ToolbarViewedEvent(_ref2) { var url = _ref2.url, display = _ref2.display, editorView = _ref2.editorView; var cardContext = useSmartLinkContext(); return /*#__PURE__*/React.createElement(EditorAnalyticsContext, { editorView: editorView }, url ? /*#__PURE__*/React.createElement(ToolbarViewedEventBase, { url: url, display: display, cardContext: cardContext, editorView: editorView }) : null); };