@atlaskit/renderer
Version:
Renderer component
96 lines (95 loc) • 3.56 kB
JavaScript
import React, { useMemo, useCallback, useContext } from 'react';
import { AnnotationMarkStates } from '@atlaskit/adf-schema';
import { AnnotationUpdateEvent } from '@atlaskit/editor-common/types';
import { MarkComponent } from './mark';
import { useInlineCommentsFilter } from '../hooks/use-inline-comments-filter';
import { useInlineCommentSubscriberContext } from '../hooks/use-inline-comment-subscriber';
import { useHasFocusEvent } from '../hooks/use-events';
import { InlineCommentsStateContext } from '../context';
import { useAnnotationManagerDispatch } from '../contexts/AnnotationManagerContext';
var MarkElement = function MarkElement(_ref) {
var annotationParentIds = _ref.annotationParentIds,
children = _ref.children,
dataAttributes = _ref.dataAttributes,
id = _ref.id,
useBlockLevel = _ref.useBlockLevel;
var updateSubscriber = useInlineCommentSubscriberContext();
var states = useContext(InlineCommentsStateContext);
var _useHasFocusEvent = useHasFocusEvent({
id: id,
updateSubscriber: updateSubscriber
}),
hasFocus = _useHasFocusEvent.hasFocus,
isHovered = _useHasFocusEvent.isHovered;
var dataAttributesMemorized = useMemo(function () {
return dataAttributes;
}, [dataAttributes]);
var _useAnnotationManager = useAnnotationManagerDispatch(),
dispatch = _useAnnotationManager.dispatch,
annotationManager = _useAnnotationManager.annotationManager;
var onClick = useCallback(function (props) {
if (!updateSubscriber) {
return;
}
if (useBlockLevel) {
return;
}
var eventTarget = props.eventTarget,
annotationIds = props.annotationIds;
if (annotationManager) {
if (hasFocus) {
dispatch({
type: 'resetSelectedAnnotation'
});
return;
}
annotationManager.checkPreemptiveGate().then(function (canSelect) {
if (canSelect) {
// if there is a draft, clear it first
annotationManager === null || annotationManager === void 0 || annotationManager.clearDraft();
// use setIsAnnotationSelected won't work here if there is a draft in progress
// so we need to use dispatch to update the state directly
dispatch({
type: 'updateAnnotation',
data: {
id: annotationIds[0],
selected: true
}
});
dispatch({
type: 'setSelectedMarkRef',
data: {
markRef: eventTarget
}
});
} else {
// TODO: EDITOR-595 - If the preemptive gate returns false, should we track the analytics event?
}
}).catch(function (_error) {
// TODO: EDITOR-595 - An error occurred while checking the preemptive gate. We should report this error.
});
} else {
updateSubscriber.emit(AnnotationUpdateEvent.ON_ANNOTATION_CLICK, {
annotationIds: annotationIds,
eventTarget: eventTarget
});
}
}, [updateSubscriber, useBlockLevel, dispatch, annotationManager, hasFocus]);
var activeParentIds = useInlineCommentsFilter({
annotationIds: annotationParentIds,
filter: {
state: AnnotationMarkStates.ACTIVE
}
});
return /*#__PURE__*/React.createElement(MarkComponent, {
id: id,
dataAttributes: dataAttributesMemorized,
annotationParentIds: activeParentIds,
onClick: onClick,
hasFocus: hasFocus,
isHovered: isHovered,
state: states[id],
useBlockLevel: useBlockLevel
}, children);
};
export { MarkElement };