@atlaskit/editor-plugin-annotation 2.8.2 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/editor-commands/index.js +50 -1
  3. package/dist/cjs/pm-plugins/annotation-manager-hooks.js +166 -4
  4. package/dist/cjs/pm-plugins/inline-comment.js +169 -30
  5. package/dist/cjs/pm-plugins/plugin-factory.js +51 -1
  6. package/dist/cjs/pm-plugins/reducer.js +18 -1
  7. package/dist/cjs/pm-plugins/toolbar.js +10 -7
  8. package/dist/cjs/pm-plugins/types.js +2 -0
  9. package/dist/cjs/ui/InlineCommentView.js +14 -1
  10. package/dist/es2019/editor-commands/index.js +42 -0
  11. package/dist/es2019/pm-plugins/annotation-manager-hooks.js +160 -5
  12. package/dist/es2019/pm-plugins/inline-comment.js +146 -9
  13. package/dist/es2019/pm-plugins/plugin-factory.js +51 -1
  14. package/dist/es2019/pm-plugins/reducer.js +22 -1
  15. package/dist/es2019/pm-plugins/toolbar.js +10 -8
  16. package/dist/es2019/pm-plugins/types.js +2 -0
  17. package/dist/es2019/ui/InlineCommentView.js +11 -1
  18. package/dist/esm/editor-commands/index.js +49 -0
  19. package/dist/esm/pm-plugins/annotation-manager-hooks.js +167 -5
  20. package/dist/esm/pm-plugins/inline-comment.js +159 -20
  21. package/dist/esm/pm-plugins/plugin-factory.js +51 -1
  22. package/dist/esm/pm-plugins/reducer.js +18 -1
  23. package/dist/esm/pm-plugins/toolbar.js +10 -7
  24. package/dist/esm/pm-plugins/types.js +2 -0
  25. package/dist/esm/ui/InlineCommentView.js +11 -1
  26. package/dist/types/editor-commands/index.d.ts +5 -0
  27. package/dist/types/pm-plugins/annotation-manager-hooks.d.ts +4 -1
  28. package/dist/types/pm-plugins/plugin-factory.d.ts +4 -0
  29. package/dist/types/pm-plugins/types.d.ts +26 -1
  30. package/dist/types/types/index.d.ts +1 -0
  31. package/dist/types-ts4.5/editor-commands/index.d.ts +5 -0
  32. package/dist/types-ts4.5/pm-plugins/annotation-manager-hooks.d.ts +4 -1
  33. package/dist/types-ts4.5/pm-plugins/plugin-factory.d.ts +4 -0
  34. package/dist/types-ts4.5/pm-plugins/types.d.ts +26 -1
  35. package/dist/types-ts4.5/types/index.d.ts +1 -0
  36. package/package.json +7 -4
@@ -9,6 +9,7 @@ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/de
9
9
  var _utils = require("@atlaskit/editor-common/utils");
10
10
  var _state = require("@atlaskit/editor-prosemirror/state");
11
11
  var _view = require("@atlaskit/editor-prosemirror/view");
12
+ var _platformFeatureFlags = require("@atlaskit/platform-feature-flags");
12
13
  var _reducer = _interopRequireDefault(require("./reducer"));
13
14
  var _utils2 = require("./utils");
14
15
  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; }
@@ -26,6 +27,10 @@ var handleDocChanged = function handleDocChanged(tr, prevPluginState) {
26
27
  * We clear bookmark on the following conditions:
27
28
  * 1. if current selection is an empty selection, or
28
29
  * 2. if the current selection and bookmark selection are different
30
+ * @param tr
31
+ * @param editorState
32
+ * @param bookmark
33
+ * @example
29
34
  */
30
35
  var shouldClearBookMarkCheck = exports.shouldClearBookMarkCheck = function shouldClearBookMarkCheck(tr, editorState, bookmark) {
31
36
  if (editorState.selection.empty || !bookmark) {
@@ -53,7 +58,7 @@ var shouldClearBookMarkCheck = exports.shouldClearBookMarkCheck = function shoul
53
58
  // by default we discard bookmark
54
59
  return true;
55
60
  };
56
- var getSelectionChangedHandler = function getSelectionChangedHandler(reopenCommentView) {
61
+ var getSelectionChangeHandlerOld = function getSelectionChangeHandlerOld(reopenCommentView) {
57
62
  return function (tr, pluginState) {
58
63
  if (pluginState.skipSelectionHandling) {
59
64
  return _objectSpread(_objectSpread({}, pluginState), {}, {
@@ -93,6 +98,51 @@ var getSelectionChangedHandler = function getSelectionChangedHandler(reopenComme
93
98
  });
94
99
  };
95
100
  };
101
+ var getSelectionChangeHandlerNew = function getSelectionChangeHandlerNew(reopenCommentView) {
102
+ return function (tr, pluginState) {
103
+ if (pluginState.skipSelectionHandling) {
104
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
105
+ skipSelectionHandling: false
106
+ }, reopenCommentView && {
107
+ isInlineCommentViewClosed: false
108
+ });
109
+ }
110
+ var selectedAnnotations = (0, _utils2.findAnnotationsInSelection)(tr.selection, tr.doc);
111
+
112
+ // NOTE: I've left this commented code here as a reference that the previous old code would reset the selected annotations
113
+ // if the selection is empty. If this is no longer needed, we can remove this code.
114
+ // clean up with platform_editor_comments_api_manager_select
115
+ // if (selectedAnnotations.length === 0) {
116
+ // return {
117
+ // ...pluginState,
118
+ // pendingSelectedAnnotations: selectedAnnotations,
119
+ // pendingSelectedAnnotationsUpdateCount:
120
+ // pluginState.pendingSelectedAnnotationsUpdateCount + 1,
121
+ // isInlineCommentViewClosed: true,
122
+ // selectAnnotationMethod: undefined,
123
+ // };
124
+ // }
125
+
126
+ if ((0, _utils2.isSelectedAnnotationsChanged)(selectedAnnotations, pluginState.pendingSelectedAnnotations)) {
127
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
128
+ pendingSelectedAnnotations: selectedAnnotations,
129
+ pendingSelectedAnnotationsUpdateCount: pluginState.pendingSelectedAnnotationsUpdateCount + 1
130
+ }, reopenCommentView && {
131
+ isInlineCommentViewClosed: false
132
+ });
133
+ }
134
+ return _objectSpread(_objectSpread(_objectSpread({}, pluginState), reopenCommentView && {
135
+ isInlineCommentViewClosed: false
136
+ }), {}, {
137
+ selectAnnotationMethod: undefined
138
+ });
139
+ };
140
+ };
141
+ var getSelectionChangedHandler = function getSelectionChangedHandler(reopenCommentView) {
142
+ return function (tr, pluginState) {
143
+ return (0, _platformFeatureFlags.fg)('platform_editor_comments_api_manager_select') ? getSelectionChangeHandlerNew(reopenCommentView)(tr, pluginState) : getSelectionChangeHandlerOld(reopenCommentView)(tr, pluginState);
144
+ };
145
+ };
96
146
  var _pluginFactory = (0, _utils.pluginFactory)(_utils2.inlineCommentPluginKey, _reducer.default, {
97
147
  onSelectionChanged: getSelectionChangedHandler(true),
98
148
  onDocChanged: handleDocChanged,
@@ -32,12 +32,14 @@ var _default = exports.default = function _default(pluginState, action) {
32
32
  annotations: {}
33
33
  });
34
34
  case _types.ACTIONS.CLOSE_COMPONENT:
35
- return _objectSpread(_objectSpread({}, pluginState), {}, {
35
+ return _objectSpread(_objectSpread(_objectSpread({}, pluginState), {}, {
36
36
  isInlineCommentViewClosed: true,
37
37
  isDrafting: false,
38
38
  isOpeningMediaCommentFromToolbar: false
39
39
  }, (0, _platformFeatureFlags.fg)('platform_editor_annotation_selected_annotation') && {
40
40
  selectedAnnotations: []
41
+ }), (0, _platformFeatureFlags.fg)('platform_editor_comments_api_manager_select') && {
42
+ selectedAnnotations: []
41
43
  });
42
44
  case _types.ACTIONS.ADD_INLINE_COMMENT:
43
45
  var updatedPluginState = getNewDraftState(pluginState, action.data.drafting, action.data.editorState);
@@ -46,6 +48,8 @@ var _default = exports.default = function _default(pluginState, action) {
46
48
  annotations: _objectSpread(_objectSpread({}, pluginState.annotations), action.data.inlineComments),
47
49
  isInlineCommentViewClosed: false,
48
50
  selectAnnotationMethod: undefined
51
+ }, (0, _platformFeatureFlags.fg)('platform_editor_comments_api_manager_select') && {
52
+ skipSelectionHandling: true
49
53
  });
50
54
  case _types.ACTIONS.INLINE_COMMENT_SET_VISIBLE:
51
55
  var isVisible = action.data.isVisible;
@@ -69,6 +73,19 @@ var _default = exports.default = function _default(pluginState, action) {
69
73
  skipSelectionHandling: true,
70
74
  isInlineCommentViewClosed: false
71
75
  });
76
+ case _types.ACTIONS.FLUSH_PENDING_SELECTIONS:
77
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
78
+ selectedAnnotations: action.data.canSetAsSelectedAnnotations ? (0, _toConsumableArray2.default)(pluginState.pendingSelectedAnnotations) : pluginState.selectedAnnotations,
79
+ pendingSelectedAnnotations: [],
80
+ isInlineCommentViewClosed: false
81
+ });
82
+ case _types.ACTIONS.SET_PENDING_SELECTIONS:
83
+ return _objectSpread(_objectSpread({}, pluginState), {}, {
84
+ pendingSelectedAnnotations: (0, _toConsumableArray2.default)(action.data.selectedAnnotations),
85
+ pendingSelectedAnnotationsUpdateCount: pluginState.pendingSelectedAnnotationsUpdateCount + 1,
86
+ skipSelectionHandling: true,
87
+ isInlineCommentViewClosed: false
88
+ });
72
89
  default:
73
90
  return pluginState;
74
91
  }
@@ -137,7 +137,7 @@ var buildToolbar = exports.buildToolbar = function buildToolbar(editorAnalyticsA
137
137
  }
138
138
  if ((0, _platformFeatureFlags.fg)('platform_editor_comments_api_manager')) {
139
139
  if (!annotationManager) {
140
- // TODO: EDITOR-188 - If we've reached here and the manager is not initialized, we should
140
+ // TODO: EDITOR-595 - If we've reached here and the manager is not initialized, we should
141
141
  // dispatch an analytics event to indicate that the user has clicked the button but
142
142
  // the action was not completed.
143
143
  return false;
@@ -151,16 +151,19 @@ var buildToolbar = exports.buildToolbar = function buildToolbar(editorAnalyticsA
151
151
  }
152
152
  });
153
153
  createCommentExperience === null || createCommentExperience === void 0 || createCommentExperience.initExperience.start();
154
- var _annotationManager$st = annotationManager.startDraft(),
155
- success = _annotationManager$st.success;
156
- if (!success) {
157
- // TODO: EDITOR-188 - Report start draft attempt failed.
154
+ var result = annotationManager.startDraft();
155
+ if (result.success) {
156
+ // TODO: EDITOR-595 - Ensure and anlytic is fired to indicate that the user has started a draft.
157
+ } else {
158
+ // TODO: EDITOR-595 - Fire an analytics event to indicate that the user has clicked the button
159
+ // but the action was not completed, the result should contain a reason.
158
160
  }
159
161
  } else {
160
- // TODO: EDITOR-188 - Dispatch analytics event
162
+ // TODO: EDITOR-595 - Track the toolbar comment button was clicked but the preemptive gate
163
+ // check returned false and the draft cannot be started.
161
164
  }
162
165
  }).catch(function () {
163
- // TODO: EDITOR-188 - Handle preemptive gate check error and dispatch analytics event
166
+ // TODO: EDITOR-595 - Handle preemptive gate check error. Something went very wrong in the gate.
164
167
  });
165
168
  return true;
166
169
  } else {
@@ -14,5 +14,7 @@ var ACTIONS = exports.ACTIONS = /*#__PURE__*/function (ACTIONS) {
14
14
  ACTIONS[ACTIONS["CLOSE_COMPONENT"] = 6] = "CLOSE_COMPONENT";
15
15
  ACTIONS[ACTIONS["SET_SELECTED_ANNOTATION"] = 7] = "SET_SELECTED_ANNOTATION";
16
16
  ACTIONS[ACTIONS["SET_HOVERED_ANNOTATION"] = 8] = "SET_HOVERED_ANNOTATION";
17
+ ACTIONS[ACTIONS["FLUSH_PENDING_SELECTIONS"] = 9] = "FLUSH_PENDING_SELECTIONS";
18
+ ACTIONS[ACTIONS["SET_PENDING_SELECTIONS"] = 10] = "SET_PENDING_SELECTIONS";
17
19
  return ACTIONS;
18
20
  }({});
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ var _typeof = require("@babel/runtime/helpers/typeof");
4
5
  Object.defineProperty(exports, "__esModule", {
5
6
  value: true
6
7
  });
7
8
  exports.InlineCommentView = InlineCommentView;
8
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
- var _react = _interopRequireDefault(require("react"));
10
+ var _react = _interopRequireWildcard(require("react"));
10
11
  var _adfSchema = require("@atlaskit/adf-schema");
11
12
  var _analytics = require("@atlaskit/editor-common/analytics");
12
13
  var _hooks = require("@atlaskit/editor-common/hooks");
@@ -19,6 +20,8 @@ var _editorCommands = require("../editor-commands");
19
20
  var _utils3 = require("../pm-plugins/utils");
20
21
  var _types = require("../types");
21
22
  var _AnnotationViewWrapper = require("./AnnotationViewWrapper");
23
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
24
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
22
25
  var findPosForDOM = function findPosForDOM(sel) {
23
26
  var $from = sel.$from,
24
27
  from = sel.from;
@@ -79,6 +82,7 @@ function InlineCommentView(_ref3) {
79
82
  var inlineCommentProvider = providers.inlineComment;
80
83
  var state = editorView.state,
81
84
  dispatch = editorView.dispatch;
85
+ var lastSelectedAnnotationId = (0, _react.useRef)();
82
86
  var CreateComponent = inlineCommentProvider.createComponent,
83
87
  ViewComponent = inlineCommentProvider.viewComponent;
84
88
  var _useInlineCommentView = useInlineCommentViewPluginState({
@@ -131,9 +135,17 @@ function InlineCommentView(_ref3) {
131
135
 
132
136
  // Create Component
133
137
  if (bookmark) {
138
+ var _selectedAnnotations$;
134
139
  if (!CreateComponent) {
135
140
  return null;
136
141
  }
142
+ var currentlySelectedAnnotation = selectedAnnotations === null || selectedAnnotations === void 0 || (_selectedAnnotations$ = selectedAnnotations[0]) === null || _selectedAnnotations$ === void 0 ? void 0 : _selectedAnnotations$.id;
143
+ var isAnnotationSelectionChanged = currentlySelectedAnnotation !== lastSelectedAnnotationId.current;
144
+
145
+ // Update the last selected annotation ID if the selection was updated
146
+ if (isAnnotationSelectionChanged) {
147
+ lastSelectedAnnotationId.current = currentlySelectedAnnotation;
148
+ }
137
149
  var inlineNodeTypes = (0, _utils.getRangeInlineNodeNames)({
138
150
  doc: state.doc,
139
151
  pos: selection
@@ -147,6 +159,7 @@ function InlineCommentView(_ref3) {
147
159
  }, /*#__PURE__*/_react.default.createElement(CreateComponent, {
148
160
  dom: dom,
149
161
  textSelection: textSelection,
162
+ wasNewAnnotationSelected: !!currentlySelectedAnnotation && isAnnotationSelectionChanged,
150
163
  onCreate: function onCreate(id) {
151
164
  if (!(0, _platformFeatureFlags.fg)('platform_editor_comments_api_manager')) {
152
165
  var createAnnotationResult = (0, _editorCommands.createAnnotation)(editorAnalyticsAPI, editorAPI)(id, _adfSchema.AnnotationTypes.INLINE_COMMENT, inlineCommentProvider.supportedBlockNodes)(editorView.state, editorView.dispatch);
@@ -31,6 +31,21 @@ export const closeComponent = () => createCommand({
31
31
  export const clearDirtyMark = () => createCommand({
32
32
  type: ACTIONS.INLINE_COMMENT_CLEAR_DIRTY_MARK
33
33
  });
34
+ export const flushPendingSelections = canSetAsSelectedAnnotations => createCommand({
35
+ type: ACTIONS.FLUSH_PENDING_SELECTIONS,
36
+ data: {
37
+ canSetAsSelectedAnnotations
38
+ }
39
+ });
40
+ export const setPendingSelectedAnnotation = id => createCommand({
41
+ type: ACTIONS.SET_PENDING_SELECTIONS,
42
+ data: {
43
+ selectedAnnotations: [{
44
+ id,
45
+ type: AnnotationTypes.INLINE_COMMENT
46
+ }]
47
+ }
48
+ });
34
49
  const removeInlineCommentFromNode = (id, supportedBlockNodes = [], state, dispatch) => {
35
50
  const {
36
51
  tr,
@@ -94,6 +109,31 @@ export const removeInlineCommentNearSelection = (id, supportedNodes = []) => (st
94
109
  }
95
110
  return true;
96
111
  };
112
+ export const removeInlineCommentFromDoc = (id, supportedNodes = []) => (state, dispatch) => {
113
+ const {
114
+ tr
115
+ } = state;
116
+ state.doc.descendants((node, pos) => {
117
+ // Inline comment on mediaInline is not supported as part of comments on media project
118
+ // Thus, we skip the decoration for mediaInline node
119
+ if (node.type.name === 'mediaInline') {
120
+ return false;
121
+ }
122
+ const isSupportedBlockNode = node.isBlock && (supportedNodes === null || supportedNodes === void 0 ? void 0 : supportedNodes.includes(node.type.name));
123
+ node.marks.filter(mark => mark.type === state.schema.marks.annotation && mark.attrs.id === id).forEach(mark => {
124
+ if (isSupportedBlockNode) {
125
+ tr.removeNodeMark(pos, mark);
126
+ } else {
127
+ tr.removeMark(pos, pos + node.nodeSize, mark);
128
+ }
129
+ });
130
+ });
131
+ if (dispatch) {
132
+ dispatch(tr);
133
+ return true;
134
+ }
135
+ return false;
136
+ };
97
137
  const getDraftCommandAction = (drafting, targetType, targetNodeId, supportedBlockNodes, isOpeningMediaCommentFromToolbar) => {
98
138
  return editorState => {
99
139
  // validate selection only when entering draft mode
@@ -117,6 +157,8 @@ const getDraftCommandAction = (drafting, targetType, targetNodeId, supportedBloc
117
157
  /**
118
158
  * Show active inline comments for a given block node, otherwise,
119
159
  * return false if the node has no comments or no unresolved comments.
160
+ * @param supportedBlockNodes
161
+ * @example
120
162
  */
121
163
  export const showInlineCommentForBlockNode = (supportedBlockNodes = []) => (node, viewMethod, isOpeningMediaCommentFromToolbar) => (state, dispatch) => {
122
164
  const pluginState = getPluginState(state);
@@ -1,13 +1,15 @@
1
1
  import { AnnotationTypes } from '@atlaskit/adf-schema';
2
- import { getRangeInlineNodeNames } from '@atlaskit/editor-common/utils';
2
+ import { getAnnotationInlineNodeTypes, getRangeInlineNodeNames } from '@atlaskit/editor-common/utils';
3
3
  import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils';
4
- import { setInlineCommentDraftState, createAnnotation } from '../editor-commands';
4
+ import { fg } from '@atlaskit/platform-feature-flags';
5
+ import { setInlineCommentDraftState, createAnnotation, setSelectedAnnotation, closeComponent, setHoveredAnnotation, removeInlineCommentFromDoc } from '../editor-commands';
5
6
  import { AnnotationSelectionType } from '../types';
6
7
  import { inlineCommentPluginKey, isSelectionValid } from './utils';
7
8
  const ERROR_REASON_DRAFT_NOT_STARTED = 'draft-not-started';
8
9
  const ERROR_REASON_DRAFT_IN_PROGRESS = 'draft-in-progress';
9
10
  const ERROR_REASON_RANGE_MISSING = 'range-no-longer-exists';
10
11
  const ERROR_REASON_RANGE_INVALID = 'invalid-range';
12
+ const ERROR_REASON_ID_INVALID = 'id-not-valid';
11
13
  const domRefFromPos = (view, position) => {
12
14
  let dom;
13
15
  try {
@@ -38,9 +40,10 @@ export const allowAnnotation = (editorView, options) => () => {
38
40
  return isSelectionValid(editorView.state) === AnnotationSelectionType.VALID;
39
41
  };
40
42
  export const startDraft = (editorView, options) => () => {
41
- var _getRangeInlineNodeNa, _options$annotationMa;
43
+ var _getRangeInlineNodeNa, _options$annotationMa2;
42
44
  const {
43
- isDrafting
45
+ isDrafting,
46
+ selectedAnnotations
44
47
  } = inlineCommentPluginKey.getState(editorView.state) || {};
45
48
  if (isDrafting) {
46
49
  return {
@@ -48,6 +51,24 @@ export const startDraft = (editorView, options) => () => {
48
51
  reason: ERROR_REASON_DRAFT_IN_PROGRESS
49
52
  };
50
53
  }
54
+ if (!!(selectedAnnotations !== null && selectedAnnotations !== void 0 && selectedAnnotations.length) && fg('platform_editor_comments_api_manager_select')) {
55
+ // if there are selected annotations when starting a draft, we need to clear the selected annotations
56
+ // before we start the draft.
57
+ closeComponent()(editorView.state, editorView.dispatch);
58
+
59
+ // not only that but we need to also deselect any other annotations that are currently selected.
60
+ selectedAnnotations === null || selectedAnnotations === void 0 ? void 0 : selectedAnnotations.forEach(annotation => {
61
+ var _options$annotationMa, _getAnnotationInlineN;
62
+ (_options$annotationMa = options.annotationManager) === null || _options$annotationMa === void 0 ? void 0 : _options$annotationMa.emit({
63
+ name: 'annotationSelectionChanged',
64
+ data: {
65
+ annotationId: annotation.id,
66
+ isSelected: false,
67
+ inlineNodeTypes: (_getAnnotationInlineN = getAnnotationInlineNodeTypes(editorView.state, annotation.id)) !== null && _getAnnotationInlineN !== void 0 ? _getAnnotationInlineN : []
68
+ }
69
+ });
70
+ });
71
+ }
51
72
  setInlineCommentDraftState(options.editorAnalyticsAPI)(true)(editorView.state, editorView.dispatch);
52
73
  const {
53
74
  draftDecorationSet
@@ -70,7 +91,7 @@ export const startDraft = (editorView, options) => () => {
70
91
  to: decorations[decorations.length - 1].to
71
92
  }
72
93
  })) !== null && _getRangeInlineNodeNa !== void 0 ? _getRangeInlineNodeNa : [];
73
- (_options$annotationMa = options.annotationManager) === null || _options$annotationMa === void 0 ? void 0 : _options$annotationMa.emit({
94
+ (_options$annotationMa2 = options.annotationManager) === null || _options$annotationMa2 === void 0 ? void 0 : _options$annotationMa2.emit({
74
95
  name: 'draftAnnotationStarted',
75
96
  data: {
76
97
  targetElement,
@@ -111,6 +132,7 @@ export const clearDraft = (editorView, options) => () => {
111
132
  };
112
133
  };
113
134
  export const applyDraft = (editorView, options) => id => {
135
+ var _options$annotationMa3, _getAnnotationInlineN2;
114
136
  const {
115
137
  isDrafting,
116
138
  draftDecorationSet,
@@ -136,6 +158,17 @@ export const applyDraft = (editorView, options) => id => {
136
158
  // Using the original decoration from position we should be able to locate the new target element.
137
159
  // This is because the new annotation will be created at the same position as the draft decoration.
138
160
  const targetElement = domRefFromPos(editorView, from);
161
+
162
+ // When a draft is applied it is automatically selected, so we need to set the selected annotation.
163
+ // emit the event for the selected annotation.
164
+ (_options$annotationMa3 = options.annotationManager) === null || _options$annotationMa3 === void 0 ? void 0 : _options$annotationMa3.emit({
165
+ name: 'annotationSelectionChanged',
166
+ data: {
167
+ annotationId: id,
168
+ isSelected: true,
169
+ inlineNodeTypes: (_getAnnotationInlineN2 = getAnnotationInlineNodeTypes(editorView.state, id)) !== null && _getAnnotationInlineN2 !== void 0 ? _getAnnotationInlineN2 : []
170
+ }
171
+ });
139
172
  return {
140
173
  success: true,
141
174
  // Get the dom element from the newly created annotation and return it here.
@@ -178,4 +211,126 @@ export const getDraft = (editorView, options) => () => {
178
211
  targetElement,
179
212
  actionResult: undefined
180
213
  };
214
+ };
215
+ export const setIsAnnotationSelected = (editorView, options) => (id, isSelected) => {
216
+ var _selectedAnnotations$;
217
+ const {
218
+ annotations,
219
+ isDrafting,
220
+ selectedAnnotations
221
+ } = inlineCommentPluginKey.getState(editorView.state) || {};
222
+ if (isDrafting) {
223
+ return {
224
+ success: false,
225
+ reason: ERROR_REASON_DRAFT_IN_PROGRESS
226
+ };
227
+ }
228
+
229
+ // If there is no annotation state with this id then we can assume the annotation is invalid.
230
+ if (!(annotations !== null && annotations !== void 0 && annotations.hasOwnProperty(id))) {
231
+ return {
232
+ success: false,
233
+ reason: ERROR_REASON_ID_INVALID
234
+ };
235
+ }
236
+ const isCurrentlySelectedIndex = (_selectedAnnotations$ = selectedAnnotations === null || selectedAnnotations === void 0 ? void 0 : selectedAnnotations.findIndex(annotation => annotation.id === id)) !== null && _selectedAnnotations$ !== void 0 ? _selectedAnnotations$ : -1;
237
+ const isCurrentlySelected = isCurrentlySelectedIndex !== -1;
238
+ if (isSelected !== isCurrentlySelected) {
239
+ // the annotation is selection is changing.
240
+ if (isCurrentlySelected && !isSelected) {
241
+ var _options$annotationMa4, _getAnnotationInlineN3;
242
+ // the selected annotaion is being unselected, so we need to close the view.
243
+ closeComponent()(editorView.state, editorView.dispatch);
244
+ (_options$annotationMa4 = options.annotationManager) === null || _options$annotationMa4 === void 0 ? void 0 : _options$annotationMa4.emit({
245
+ name: 'annotationSelectionChanged',
246
+ data: {
247
+ annotationId: id,
248
+ isSelected: false,
249
+ inlineNodeTypes: (_getAnnotationInlineN3 = getAnnotationInlineNodeTypes(editorView.state, id)) !== null && _getAnnotationInlineN3 !== void 0 ? _getAnnotationInlineN3 : []
250
+ }
251
+ });
252
+ } else if (!isCurrentlySelected && isSelected) {
253
+ var _options$annotationMa6, _getAnnotationInlineN5;
254
+ // the annotation is currently not selected and is being selected, so we need to open the view.
255
+ setSelectedAnnotation(id)(editorView.state, editorView.dispatch);
256
+
257
+ // the current annotations are going to be unselected. So we need to notify listeners of this change also.
258
+ selectedAnnotations === null || selectedAnnotations === void 0 ? void 0 : selectedAnnotations.forEach(annotation => {
259
+ if (annotation.id !== id) {
260
+ var _options$annotationMa5, _getAnnotationInlineN4;
261
+ (_options$annotationMa5 = options.annotationManager) === null || _options$annotationMa5 === void 0 ? void 0 : _options$annotationMa5.emit({
262
+ name: 'annotationSelectionChanged',
263
+ data: {
264
+ annotationId: annotation.id,
265
+ isSelected: false,
266
+ inlineNodeTypes: (_getAnnotationInlineN4 = getAnnotationInlineNodeTypes(editorView.state, annotation.id)) !== null && _getAnnotationInlineN4 !== void 0 ? _getAnnotationInlineN4 : []
267
+ }
268
+ });
269
+ }
270
+ });
271
+
272
+ // Lastly we need to emit the event for the selected annotation.
273
+ (_options$annotationMa6 = options.annotationManager) === null || _options$annotationMa6 === void 0 ? void 0 : _options$annotationMa6.emit({
274
+ name: 'annotationSelectionChanged',
275
+ data: {
276
+ annotationId: id,
277
+ isSelected: true,
278
+ inlineNodeTypes: (_getAnnotationInlineN5 = getAnnotationInlineNodeTypes(editorView.state, id)) !== null && _getAnnotationInlineN5 !== void 0 ? _getAnnotationInlineN5 : []
279
+ }
280
+ });
281
+ }
282
+ }
283
+ return {
284
+ success: true,
285
+ isSelected
286
+ };
287
+ };
288
+ export const setIsAnnotationHovered = (editorView, options) => (id, isHovered) => {
289
+ var _hoveredAnnotations$f;
290
+ const {
291
+ annotations,
292
+ hoveredAnnotations
293
+ } = inlineCommentPluginKey.getState(editorView.state) || {};
294
+
295
+ // If there is no annotation state with this id then we can assume the annotation is invalid.
296
+ if (!(annotations !== null && annotations !== void 0 && annotations.hasOwnProperty(id))) {
297
+ return {
298
+ success: false,
299
+ reason: ERROR_REASON_ID_INVALID
300
+ };
301
+ }
302
+ const isCurrentlyHoveredIndex = (_hoveredAnnotations$f = hoveredAnnotations === null || hoveredAnnotations === void 0 ? void 0 : hoveredAnnotations.findIndex(annotation => annotation.id === id)) !== null && _hoveredAnnotations$f !== void 0 ? _hoveredAnnotations$f : -1;
303
+ const isCurrentlyHovered = isCurrentlyHoveredIndex !== -1;
304
+ if (isHovered !== isCurrentlyHovered) {
305
+ // the annotation in hovered is changing.
306
+ if (isCurrentlyHovered && !isHovered) {
307
+ // the hovered annotaion is being unhovered, so we should remove the hover state.
308
+ setHoveredAnnotation('')(editorView.state, editorView.dispatch);
309
+ } else if (!isCurrentlyHovered && isHovered) {
310
+ // the annotation is currently not hovered and is being hovered.
311
+ setHoveredAnnotation(id)(editorView.state, editorView.dispatch);
312
+ }
313
+ }
314
+ return {
315
+ success: true,
316
+ isHovered
317
+ };
318
+ };
319
+ export const clearAnnotation = (editorView, options) => id => {
320
+ const {
321
+ annotations
322
+ } = inlineCommentPluginKey.getState(editorView.state) || {};
323
+
324
+ // If there is no annotation state with this id then we can assume the annotation is invalid.
325
+ if (!(annotations !== null && annotations !== void 0 && annotations.hasOwnProperty(id))) {
326
+ return {
327
+ success: false,
328
+ reason: ERROR_REASON_ID_INVALID
329
+ };
330
+ }
331
+ removeInlineCommentFromDoc(id, options.provider.supportedBlockNodes)(editorView.state, editorView.dispatch);
332
+ return {
333
+ success: true,
334
+ actionResult: undefined
335
+ };
181
336
  };