UNPKG

@atlaskit/renderer

Version:
104 lines (103 loc) 4.87 kB
import React, { useContext } from 'react'; import { AnnotationTypes } from '@atlaskit/adf-schema'; import { AnnotationView } from './view'; import { AnnotationsContextWrapper } from './wrapper'; import { ProvidersContext, InlineCommentsStateContext } from './context'; import { useLoadAnnotations } from './hooks/use-load-annotations'; import { useAnnotationStateByTypeEvent } from './hooks/use-events'; import { useAnalyticsEvents } from '@atlaskit/analytics-next'; import { AnnotationRangeProvider } from './contexts/AnnotationRangeContext'; import { AnnotationHoverContext } from './contexts/AnnotationHoverContext'; import { AnnotationManagerProvider } from './contexts/AnnotationManagerContext'; const LoadAnnotations = /*#__PURE__*/React.memo(({ adfDocument, isNestedRender, onLoadComplete }) => { useLoadAnnotations({ adfDocument, isNestedRender, onLoadComplete }); return null; }); // This is used by renderers when setting the data-start-pos attribute on commentable nodes // By default it is 1 (the possible starting position of any document). // The bodied extension component then sets a new value for this context based on its on position // in the document. export const AnnotationsPositionContext = /*#__PURE__*/React.createContext({ startPos: 1 }); export const AnnotationsWrapperInner = props => { var _providers$inlineComm, _providers$inlineComm2; const { children, rendererRef, adfDocument, isNestedRender, onLoadComplete } = props; const providers = useContext(ProvidersContext); const updateSubscriber = providers && providers.inlineComment && providers.inlineComment.updateSubscriber; const inlineCommentAnnotationsState = useAnnotationStateByTypeEvent({ type: AnnotationTypes.INLINE_COMMENT, updateSubscriber: updateSubscriber || null }); const { createAnalyticsEvent } = useAnalyticsEvents(); return /*#__PURE__*/React.createElement(InlineCommentsStateContext.Provider, { value: inlineCommentAnnotationsState }, /*#__PURE__*/React.createElement(AnnotationRangeProvider, { allowCommentsOnMedia: (_providers$inlineComm = providers === null || providers === void 0 ? void 0 : (_providers$inlineComm2 = providers.inlineComment) === null || _providers$inlineComm2 === void 0 ? void 0 : _providers$inlineComm2.allowCommentsOnMedia) !== null && _providers$inlineComm !== void 0 ? _providers$inlineComm : false, isNestedRender: isNestedRender }, /*#__PURE__*/React.createElement(AnnotationHoverContext, null, /*#__PURE__*/React.createElement(AnnotationsContextWrapper, { createAnalyticsEvent: createAnalyticsEvent, rendererRef: rendererRef, isNestedRender: isNestedRender }, /*#__PURE__*/React.createElement(LoadAnnotations, { adfDocument: adfDocument, isNestedRender: isNestedRender, onLoadComplete: onLoadComplete }), /*#__PURE__*/React.createElement(AnnotationView, { isNestedRender: isNestedRender, createAnalyticsEvent: createAnalyticsEvent }), children)))); }; export const AnnotationsWrapper = props => { const { children, annotationProvider, rendererRef, adfDocument, isNestedRender, onLoadComplete } = props; if (!isNestedRender && annotationProvider !== null && annotationProvider !== void 0 && annotationProvider.annotationManager) { var _annotationProvider$i, _annotationProvider$i2; // We need to ensure there is a single instance of the annotation manager for the whole document // and that it is the same instance for all annotations. // This is because the annotation manager is responsible for managing the state of ALL annotations. // This includes annotations inside extensions. return /*#__PURE__*/React.createElement(ProvidersContext.Provider, { value: annotationProvider }, /*#__PURE__*/React.createElement(AnnotationManagerProvider, { annotationManager: annotationProvider.annotationManager, updateSubscriber: (_annotationProvider$i = annotationProvider === null || annotationProvider === void 0 ? void 0 : (_annotationProvider$i2 = annotationProvider.inlineComment) === null || _annotationProvider$i2 === void 0 ? void 0 : _annotationProvider$i2.updateSubscriber) !== null && _annotationProvider$i !== void 0 ? _annotationProvider$i : undefined }, /*#__PURE__*/React.createElement(AnnotationsWrapperInner, { rendererRef: rendererRef, adfDocument: adfDocument, isNestedRender: isNestedRender, onLoadComplete: onLoadComplete }, children))); } else { return /*#__PURE__*/React.createElement(ProvidersContext.Provider, { value: annotationProvider }, /*#__PURE__*/React.createElement(AnnotationsWrapperInner, { rendererRef: rendererRef, adfDocument: adfDocument, isNestedRender: isNestedRender, onLoadComplete: onLoadComplete }, children)); } };