@atlaskit/editor-plugin-annotation
Version:
Annotation plugin for @atlaskit/editor-core
131 lines (129 loc) • 7.08 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.startCommentExperience = exports.shouldShowCommentButton = exports.isButtonDisabled = exports.fireOnClickAnalyticsEvent = exports.fireCommentButtonViewedAnalyticsEvent = void 0;
var _analytics = require("@atlaskit/editor-common/analytics");
var _mediaSingle = require("@atlaskit/editor-common/media-single");
var _editorPluginConnectivity = require("@atlaskit/editor-plugin-connectivity");
var _editorCommands = require("../../editor-commands");
var _utils = require("../../pm-plugins/utils");
var _types = require("../../types");
var isButtonDisabled = exports.isButtonDisabled = function isButtonDisabled(_ref) {
var _api$connectivity;
var state = _ref.state,
api = _ref.api,
canAddComments = _ref.canAddComments;
var annotationSelectionType = state ? (0, _utils.isSelectionValid)(state) : _types.AnnotationSelectionType.INVALID;
return !canAddComments || annotationSelectionType === _types.AnnotationSelectionType.DISABLED || (0, _editorPluginConnectivity.isOfflineMode)(api === null || api === void 0 || (_api$connectivity = api.connectivity) === null || _api$connectivity === void 0 || (_api$connectivity = _api$connectivity.sharedState) === null || _api$connectivity === void 0 || (_api$connectivity = _api$connectivity.currentState()) === null || _api$connectivity === void 0 ? void 0 : _api$connectivity.mode);
};
var shouldShowCommentButton = exports.shouldShowCommentButton = function shouldShowCommentButton(_ref2) {
var state = _ref2.state,
isVisible = _ref2.isVisible,
annotationSelectionType = _ref2.annotationSelectionType;
var isMediaSelected = state ? (0, _mediaSingle.currentMediaNodeWithPos)(state) : false;
var isDrafting = false;
if (state) {
var _inlineCommentPluginK;
isDrafting = ((_inlineCommentPluginK = _utils.inlineCommentPluginKey.getState(state)) === null || _inlineCommentPluginK === void 0 ? void 0 : _inlineCommentPluginK.isDrafting) || false;
}
// comments on media can only be added via media floating toolbar
if (isDrafting || isMediaSelected || annotationSelectionType === _types.AnnotationSelectionType.INVALID || !isVisible) {
return false;
}
return true;
};
var fireOnClickAnalyticsEvent = exports.fireOnClickAnalyticsEvent = function fireOnClickAnalyticsEvent(_ref3) {
var _api$analytics;
var api = _ref3.api;
(_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.fireAnalyticsEvent({
action: _analytics.ACTION.CLICKED,
actionSubject: _analytics.ACTION_SUBJECT.BUTTON,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.CREATE_INLINE_COMMENT_FROM_HIGHLIGHT_ACTIONS_MENU,
eventType: _analytics.EVENT_TYPE.UI,
attributes: {
source: 'highlightActionsMenu',
pageMode: 'edit'
}
});
};
var fireAnnotationErrorAnalyticsEvent = function fireAnnotationErrorAnalyticsEvent(_ref4) {
var _api$analytics2;
var api = _ref4.api,
errorReason = _ref4.errorReason;
(_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 || (_api$analytics2 = _api$analytics2.actions) === null || _api$analytics2 === void 0 || _api$analytics2.fireAnalyticsEvent({
action: _analytics.ACTION.ERROR,
actionSubject: _analytics.ACTION_SUBJECT.ANNOTATION,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.INLINE_COMMENT,
eventType: _analytics.EVENT_TYPE.OPERATIONAL,
attributes: {
errorReason: errorReason
}
});
};
var fireCommentButtonViewedAnalyticsEvent = exports.fireCommentButtonViewedAnalyticsEvent = function fireCommentButtonViewedAnalyticsEvent(_ref5) {
var _api$analytics3;
var api = _ref5.api,
isNonTextInlineNodeInludedInComment = _ref5.isNonTextInlineNodeInludedInComment,
annotationSelectionType = _ref5.annotationSelectionType;
api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 || _api$analytics3.actions.fireAnalyticsEvent({
action: _analytics.ACTION.VIEWED,
actionSubject: _analytics.ACTION_SUBJECT.BUTTON,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.INLINE_COMMENT,
eventType: _analytics.EVENT_TYPE.UI,
attributes: {
isNonTextInlineNodeInludedInComment: isNonTextInlineNodeInludedInComment,
isDisabled: annotationSelectionType === _types.AnnotationSelectionType.DISABLED,
inputMethod: _analytics.INPUT_METHOD.FLOATING_TB,
mode: _analytics.MODE.EDITOR
}
});
};
var startCommentExperience = exports.startCommentExperience = function startCommentExperience(_ref6) {
var annotationProviders = _ref6.annotationProviders,
api = _ref6.api,
state = _ref6.state,
dispatch = _ref6.dispatch;
var annotationManager = annotationProviders === null || annotationProviders === void 0 ? void 0 : annotationProviders.annotationManager;
if (annotationManager) {
annotationManager.checkPreemptiveGate().then(function (canStartDraft) {
if (canStartDraft) {
var _annotationProviders$, _annotationProviders$2;
(_annotationProviders$ = annotationProviders.createCommentExperience) === null || _annotationProviders$ === void 0 || _annotationProviders$.start({
attributes: {
pageClass: 'editor',
commentType: 'inline',
entryPoint: 'highlightActions'
}
});
(_annotationProviders$2 = annotationProviders.createCommentExperience) === null || _annotationProviders$2 === void 0 || _annotationProviders$2.initExperience.start();
var result = annotationManager.startDraft();
if (!result.success) {
// Fire an analytics event to indicate that the user has clicked the button
// but the action was not completed, the result should contain a reason.
fireAnnotationErrorAnalyticsEvent({
api: api,
errorReason: "toolbar-start-draft-failed/".concat(result.reason)
});
}
}
}).catch(function () {
fireAnnotationErrorAnalyticsEvent({
api: api,
errorReason: "toolbar-start-draft-preemptive-gate-error"
});
});
return true;
} else {
var _annotationProviders$3, _annotationProviders$4, _api$analytics4;
annotationProviders === null || annotationProviders === void 0 || (_annotationProviders$3 = annotationProviders.createCommentExperience) === null || _annotationProviders$3 === void 0 || _annotationProviders$3.start({
attributes: {
pageClass: 'editor',
commentType: 'inline',
entryPoint: 'highlightActions'
}
});
annotationProviders === null || annotationProviders === void 0 || (_annotationProviders$4 = annotationProviders.createCommentExperience) === null || _annotationProviders$4 === void 0 || _annotationProviders$4.initExperience.start();
return (0, _editorCommands.setInlineCommentDraftState)(api === null || api === void 0 || (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 ? void 0 : _api$analytics4.actions, undefined, api)(true)(state, dispatch);
}
};