@atlaskit/editor-plugin-card
Version:
Card plugin for @atlaskit/editor-core
65 lines (63 loc) • 2.73 kB
JavaScript
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';
const areResolvedAttributesEqual = (current, next) => (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
*/
const ToolbarViewedEventBase = ({
url,
display,
cardContext,
editorView
}) => {
const {
createAnalyticsEvent
} = useAnalyticsEvents();
const store = cardContext === null || cardContext === void 0 ? void 0 : cardContext.store;
useEffect(() => {
var _getPluginState;
const resolvedAttributes = getResolvedAttributesFromStore(url, display, store);
createAnalyticsEvent({
action: 'viewed',
actionSubject: 'inlineDialog',
actionSubjectId: 'editLinkToolbar',
eventType: 'ui',
attributes: {
...resolvedAttributes,
display
}
}).fire('media');
if (!editorView) {
return;
}
const 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 const ToolbarViewedEvent = ({
url,
display,
editorView
}) => {
const cardContext = useSmartLinkContext();
return /*#__PURE__*/React.createElement(EditorAnalyticsContext, {
editorView: editorView
}, url ? /*#__PURE__*/React.createElement(ToolbarViewedEventBase, {
url: url,
display: display,
cardContext: cardContext,
editorView: editorView
}) : null);
};