@atlaskit/renderer
Version:
Renderer component
70 lines • 3.62 kB
JavaScript
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 { useContext, useEffect } from 'react';
import { AnnotationUpdateEvent } from '@atlaskit/editor-common/types';
import { ProvidersContext } from '../context';
import { RendererContext as ActionsContext } from '../../RendererActionsContext';
import { useAnnotationManagerDispatch } from '../contexts/AnnotationManagerContext';
export var useLoadAnnotations = function useLoadAnnotations(_ref) {
var adfDocument = _ref.adfDocument,
isNestedRender = _ref.isNestedRender,
onLoadComplete = _ref.onLoadComplete;
var actions = useContext(ActionsContext);
var providers = useContext(ProvidersContext);
var _useAnnotationManager = useAnnotationManagerDispatch(),
annotationManager = _useAnnotationManager.annotationManager,
dispatch = _useAnnotationManager.dispatch;
var isAnnotationManagerEnabled = !!annotationManager;
useEffect(function () {
if (!providers) {
return;
}
var _providers$inlineComm = providers.inlineComment,
inlineCommentGetState = _providers$inlineComm.getState,
updateSubscriberInlineComment = _providers$inlineComm.updateSubscriber;
var annotations = actions.getAnnotationMarks();
// we don't want to request integrators for state with an empty list of ids.
if (!annotations.length) {
if (!isNestedRender) {
// inlineCommentGetState handles empty lists gracefully. It has a side-effect of clearing state, which is why this call is needed
inlineCommentGetState([], isNestedRender);
}
onLoadComplete && onLoadComplete({
numberOfUnresolvedInlineComments: 0
});
return;
}
var ids = annotations.map(function (mark) {
return mark.attrs.id;
});
var cb = function cb(data) {
if (!updateSubscriberInlineComment) {
return;
}
var payload = data.reduce(function (acc, value) {
return _objectSpread(_objectSpread({}, acc), {}, _defineProperty({}, value.id, value));
}, {});
if (isAnnotationManagerEnabled) {
dispatch({
type: 'loadAnnotation',
data: Object.keys(payload).map(function (id) {
var _payload$id$state;
return {
id: id,
markState: (_payload$id$state = payload[id].state) !== null && _payload$id$state !== void 0 ? _payload$id$state : undefined
};
})
});
} else {
updateSubscriberInlineComment.emit(AnnotationUpdateEvent.SET_ANNOTATION_STATE, payload);
}
onLoadComplete && onLoadComplete({
numberOfUnresolvedInlineComments: data.filter(function (data) {
return data.state === 'active';
}).length
});
};
inlineCommentGetState(ids, isNestedRender).then(cb);
}, [actions, providers, adfDocument, isNestedRender, onLoadComplete, dispatch, isAnnotationManagerEnabled]);
};