@atlaskit/renderer 115.0.2 → 115.0.4

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 (37) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/cjs/ui/Renderer/index.js +1 -1
  3. package/dist/cjs/ui/annotations/contexts/AnnotationManagerContext.js +421 -0
  4. package/dist/cjs/ui/annotations/draft/component.js +13 -1
  5. package/dist/cjs/ui/annotations/element/index.js +38 -6
  6. package/dist/cjs/ui/annotations/element/mark.js +16 -0
  7. package/dist/cjs/ui/annotations/hooks/use-events.js +77 -27
  8. package/dist/cjs/ui/annotations/hooks/use-load-annotations.js +18 -2
  9. package/dist/cjs/ui/annotations/index.js +49 -12
  10. package/dist/cjs/ui/annotations/selection/mounter.js +177 -4
  11. package/dist/es2019/ui/Renderer/index.js +1 -1
  12. package/dist/es2019/ui/annotations/contexts/AnnotationManagerContext.js +416 -0
  13. package/dist/es2019/ui/annotations/draft/component.js +15 -3
  14. package/dist/es2019/ui/annotations/element/index.js +39 -6
  15. package/dist/es2019/ui/annotations/element/mark.js +18 -0
  16. package/dist/es2019/ui/annotations/hooks/use-events.js +85 -28
  17. package/dist/es2019/ui/annotations/hooks/use-load-annotations.js +19 -2
  18. package/dist/es2019/ui/annotations/index.js +47 -10
  19. package/dist/es2019/ui/annotations/selection/mounter.js +177 -6
  20. package/dist/esm/ui/Renderer/index.js +1 -1
  21. package/dist/esm/ui/annotations/contexts/AnnotationManagerContext.js +411 -0
  22. package/dist/esm/ui/annotations/draft/component.js +14 -3
  23. package/dist/esm/ui/annotations/element/index.js +38 -6
  24. package/dist/esm/ui/annotations/element/mark.js +16 -0
  25. package/dist/esm/ui/annotations/hooks/use-events.js +78 -28
  26. package/dist/esm/ui/annotations/hooks/use-load-annotations.js +18 -2
  27. package/dist/esm/ui/annotations/index.js +45 -10
  28. package/dist/esm/ui/annotations/selection/mounter.js +179 -6
  29. package/dist/types/ui/annotations/contexts/AnnotationManagerContext.d.ts +71 -0
  30. package/dist/types/ui/annotations/draft/component.d.ts +5 -1
  31. package/dist/types/ui/annotations/element/mark.d.ts +6 -0
  32. package/dist/types/ui/annotations/index.d.ts +1 -0
  33. package/dist/types-ts4.5/ui/annotations/contexts/AnnotationManagerContext.d.ts +71 -0
  34. package/dist/types-ts4.5/ui/annotations/draft/component.d.ts +5 -1
  35. package/dist/types-ts4.5/ui/annotations/element/mark.d.ts +6 -0
  36. package/dist/types-ts4.5/ui/annotations/index.d.ts +1 -0
  37. package/package.json +13 -5
@@ -0,0 +1,411 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
3
+ 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; }
4
+ 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; }
5
+ import React, { createContext, useContext, useEffect, useMemo, useReducer } from 'react';
6
+ import { AnnotationMarkStates, AnnotationTypes } from '@atlaskit/adf-schema';
7
+ import { AnnotationUpdateEvent } from '@atlaskit/editor-common/types';
8
+ var initState = {
9
+ isDrafting: false,
10
+ draftId: undefined,
11
+ draftMarkRef: undefined,
12
+ draftActionResult: undefined,
13
+ annotations: {},
14
+ currentSelectedAnnotationId: undefined,
15
+ currentSelectedMarkRef: undefined,
16
+ currentHoveredAnnotationId: undefined
17
+ };
18
+ var AnnotationManagerStateContext = /*#__PURE__*/createContext(initState);
19
+ var AnnotationManagerDispatchContext = /*#__PURE__*/createContext({
20
+ annotationManager: undefined,
21
+ dispatch: function dispatch() {}
22
+ });
23
+ function reducer(state, action) {
24
+ switch (action.type) {
25
+ case 'reset':
26
+ {
27
+ return _objectSpread(_objectSpread({}, state), initState);
28
+ }
29
+ case 'loadAnnotation':
30
+ {
31
+ var currentIds = Object.keys(state.annotations);
32
+ var uids = Array.from(new Set(currentIds.concat(action.data.map(function (a) {
33
+ return a.id;
34
+ }))));
35
+ var updates = [];
36
+ var _loop = function _loop() {
37
+ var _state$annotations$id, _state$annotations$id2;
38
+ var id = _uids[_i];
39
+ var loadedAnnotation = action.data.find(function (a) {
40
+ return a.id === id;
41
+ });
42
+ if (!loadedAnnotation && ((_state$annotations$id = state.annotations[id]) === null || _state$annotations$id === void 0 ? void 0 : _state$annotations$id.markState) === AnnotationMarkStates.ACTIVE) {
43
+ // If the annotation is not in the loaded data, we need to remove it from the state. However,
44
+ // rather then removing it, we will set the mark state to resolved. This is to align it better with
45
+ // how the editor works.
46
+ updates.push({
47
+ id: id,
48
+ markState: AnnotationMarkStates.RESOLVED
49
+ });
50
+ return 0; // continue
51
+ }
52
+ if (!!(loadedAnnotation !== null && loadedAnnotation !== void 0 && loadedAnnotation.markState) && ((_state$annotations$id2 = state.annotations[id]) === null || _state$annotations$id2 === void 0 ? void 0 : _state$annotations$id2.markState) !== loadedAnnotation.markState) {
53
+ updates.push({
54
+ id: id,
55
+ markState: loadedAnnotation.markState
56
+ });
57
+ return 0; // continue
58
+ }
59
+ },
60
+ _ret;
61
+ for (var _i = 0, _uids = uids; _i < _uids.length; _i++) {
62
+ _ret = _loop();
63
+ if (_ret === 0) continue;
64
+ }
65
+ if (updates.length > 0) {
66
+ return _objectSpread(_objectSpread({}, state), {}, {
67
+ annotations: updates.reduce(function (nextAnnotations, update) {
68
+ return _objectSpread(_objectSpread({}, nextAnnotations), {}, _defineProperty({}, update.id, _objectSpread(_objectSpread({}, nextAnnotations[update.id]), update)));
69
+ }, state.annotations)
70
+ });
71
+ }
72
+ return state;
73
+ }
74
+ case 'updateAnnotation':
75
+ {
76
+ var current = state.annotations[action.data.id];
77
+ var _action$data = action.data,
78
+ id = _action$data.id,
79
+ selected = _action$data.selected,
80
+ hovered = _action$data.hovered,
81
+ _action$data$markStat = _action$data.markState,
82
+ markState = _action$data$markStat === void 0 ? current === null || current === void 0 ? void 0 : current.markState : _action$data$markStat;
83
+ var _updates = [];
84
+
85
+ // If the annotation is not currently in the state, we need to add it to the state.
86
+ if (!current) {
87
+ _updates.push({
88
+ id: id,
89
+ markState: markState !== null && markState !== void 0 ? markState : AnnotationMarkStates.ACTIVE
90
+ });
91
+ }
92
+
93
+ // The goal of the following is to enforce a single selection and a single hover state across all annotations.
94
+ var nextSelectedId = state.currentSelectedAnnotationId;
95
+ if (selected && nextSelectedId !== id) {
96
+ nextSelectedId = id;
97
+ }
98
+ // If the annotation is currently selected and it's being unselected, we need to remove it from the
99
+ // current selected annotation id.
100
+ if (selected === false && nextSelectedId === id) {
101
+ nextSelectedId = undefined;
102
+ }
103
+ var nextHoveredId = state.currentHoveredAnnotationId;
104
+ if (hovered && nextHoveredId !== id) {
105
+ nextHoveredId = id;
106
+ }
107
+ // If the annotation is currently hovered and it's being unhovered, we need to remove it from the
108
+ // current hovered annotation id.
109
+ if (hovered === false && nextHoveredId === id) {
110
+ nextHoveredId = undefined;
111
+ }
112
+
113
+ // If the annotations mark state is not the same as the current mark state, we need to update it.
114
+ if ((current === null || current === void 0 ? void 0 : current.markState) !== markState) {
115
+ _updates.push({
116
+ id: id,
117
+ markState: markState !== null && markState !== void 0 ? markState : AnnotationMarkStates.ACTIVE
118
+ });
119
+
120
+ // If the annotation is currently selected and it's being resolved, then we need to remove it from the
121
+ // current selected annotation id also.
122
+ if (markState === AnnotationMarkStates.RESOLVED && nextSelectedId === id) {
123
+ nextSelectedId = undefined;
124
+ }
125
+ }
126
+ if (_updates.length > 0 || nextSelectedId !== state.currentSelectedAnnotationId || nextHoveredId !== state.currentHoveredAnnotationId) {
127
+ return _objectSpread(_objectSpread({}, state), {}, {
128
+ currentSelectedAnnotationId: nextSelectedId,
129
+ currentHoveredAnnotationId: nextHoveredId,
130
+ annotations: _updates.reduce(function (nextAnnotations, update) {
131
+ return _objectSpread(_objectSpread({}, nextAnnotations), {}, _defineProperty({}, update.id, _objectSpread(_objectSpread({}, nextAnnotations[update.id]), update)));
132
+ }, state.annotations)
133
+ });
134
+ }
135
+ return state;
136
+ }
137
+ case 'resetSelectedAnnotation':
138
+ {
139
+ if (state.currentSelectedAnnotationId !== undefined) {
140
+ return _objectSpread(_objectSpread({}, state), {}, {
141
+ currentSelectedAnnotationId: undefined,
142
+ currentSelectedMarkRef: undefined
143
+ });
144
+ }
145
+ return state;
146
+ }
147
+ case 'resetHoveredAnnotation':
148
+ {
149
+ if (state.currentHoveredAnnotationId !== undefined) {
150
+ return _objectSpread(_objectSpread({}, state), {}, {
151
+ currentHoveredAnnotationId: undefined
152
+ });
153
+ }
154
+ return state;
155
+ }
156
+ case 'setDrafting':
157
+ {
158
+ if (state.isDrafting !== action.data.isDrafting || state.draftId !== action.data.draftId || state.draftActionResult !== action.data.draftActionResult) {
159
+ // XXX: When a draft is open the current selected annotation should no longer be selected. We need
160
+ // to decide what is better UX,
161
+ // 1 - do we want to deselct the selected annotation on draft open, if so then when draft is closed the s
162
+ // selected annotation will not come back
163
+ // 2 - do we want to still allow the selected annotation to be selected when draft is open, however the underlying
164
+ // mark style just shows the annotation as not selected when a draft is active. Then when a draft closes
165
+ // we can reopen the previous selected annotation.
166
+ return _objectSpread(_objectSpread({}, state), {}, {
167
+ isDrafting: action.data.isDrafting,
168
+ draftId: action.data.draftId,
169
+ draftActionResult: action.data.draftActionResult
170
+ });
171
+ }
172
+ return state;
173
+ }
174
+ case 'setDraftMarkRef':
175
+ {
176
+ if (state.draftMarkRef !== action.data.draftMarkRef) {
177
+ return _objectSpread(_objectSpread({}, state), {}, {
178
+ draftMarkRef: action.data.draftMarkRef
179
+ });
180
+ }
181
+ return state;
182
+ }
183
+ case 'setSelectedMarkRef':
184
+ {
185
+ if (state.currentSelectedMarkRef !== action.data.markRef) {
186
+ return _objectSpread(_objectSpread({}, state), {}, {
187
+ currentSelectedMarkRef: action.data.markRef
188
+ });
189
+ }
190
+ return state;
191
+ }
192
+ }
193
+ }
194
+ export var AnnotationManagerProvider = function AnnotationManagerProvider(_ref) {
195
+ var children = _ref.children,
196
+ annotationManager = _ref.annotationManager,
197
+ updateSubscriber = _ref.updateSubscriber;
198
+ var _useReducer = useReducer(reducer, initState),
199
+ _useReducer2 = _slicedToArray(_useReducer, 2),
200
+ state = _useReducer2[0],
201
+ dispatch = _useReducer2[1];
202
+ useEffect(function () {
203
+ var getDraft = function getDraft() {
204
+ var _state$draftActionRes;
205
+ if (!state.isDrafting || !state.draftActionResult || !state.draftMarkRef || !state.draftId) {
206
+ return {
207
+ success: false,
208
+ reason: 'draft-not-started'
209
+ };
210
+ }
211
+ return {
212
+ success: true,
213
+ inlineNodeTypes: (_state$draftActionRes = state.draftActionResult.inlineNodeTypes) !== null && _state$draftActionRes !== void 0 ? _state$draftActionRes : [],
214
+ targetElement: state.draftMarkRef,
215
+ actionResult: {
216
+ step: state.draftActionResult.step,
217
+ doc: state.draftActionResult.doc,
218
+ inlineNodeTypes: state.draftActionResult.inlineNodeTypes,
219
+ targetNodeType: state.draftActionResult.targetNodeType,
220
+ originalSelection: state.draftActionResult.originalSelection,
221
+ numMatches: state.draftActionResult.numMatches,
222
+ matchIndex: state.draftActionResult.matchIndex,
223
+ pos: state.draftActionResult.pos
224
+ }
225
+ };
226
+ };
227
+ annotationManager === null || annotationManager === void 0 || annotationManager.hook('getDraft', getDraft);
228
+ return function () {
229
+ annotationManager === null || annotationManager === void 0 || annotationManager.unhook('getDraft', getDraft);
230
+ };
231
+ }, [annotationManager, state.draftId, state.isDrafting, state.draftMarkRef, state.draftActionResult]);
232
+
233
+ // We need to watch for the draft mark element to exist, so we can inform any listeners that the draft has been started
234
+ // and give them a reference to the element
235
+ useEffect(function () {
236
+ if (state.isDrafting && state.draftId && state.draftMarkRef && state.draftActionResult) {
237
+ var _state$draftActionRes2;
238
+ annotationManager === null || annotationManager === void 0 || annotationManager.emit({
239
+ name: 'draftAnnotationStarted',
240
+ data: {
241
+ inlineNodeTypes: (_state$draftActionRes2 = state.draftActionResult.inlineNodeTypes) !== null && _state$draftActionRes2 !== void 0 ? _state$draftActionRes2 : [],
242
+ targetElement: state.draftMarkRef,
243
+ actionResult: {
244
+ step: state.draftActionResult.step,
245
+ doc: state.draftActionResult.doc,
246
+ inlineNodeTypes: state.draftActionResult.inlineNodeTypes,
247
+ targetNodeType: state.draftActionResult.targetNodeType,
248
+ originalSelection: state.draftActionResult.originalSelection,
249
+ numMatches: state.draftActionResult.numMatches,
250
+ matchIndex: state.draftActionResult.matchIndex,
251
+ pos: state.draftActionResult.pos
252
+ }
253
+ }
254
+ });
255
+ }
256
+ }, [annotationManager, state.draftId, state.isDrafting, state.draftMarkRef, state.draftActionResult]);
257
+
258
+ // useEffect(() => {
259
+ // const setIsAnnotationSelected = (id: AnnotationId, isSelected: boolean): SelectAnnotationResult => {
260
+ // if (!state.annotations?.[id]) {
261
+ // return {
262
+ // success: false,
263
+ // reason: 'id-not-valid',
264
+ // };
265
+ // }
266
+
267
+ // dispatch({
268
+ // type: 'updateAnnotation',
269
+ // data: {
270
+ // id,
271
+ // selected: isSelected,
272
+ // },
273
+ // })
274
+
275
+ // return {
276
+ // success: true,
277
+ // isSelected,
278
+ // };
279
+ // };
280
+
281
+ // annotationManager?.hook('setIsAnnotationSelected', setIsAnnotationSelected);
282
+ // return () => {
283
+ // annotationManager?.unhook('setIsAnnotationSelected', setIsAnnotationSelected);
284
+ // };
285
+ // }, [state.annotations]);
286
+
287
+ // useEffect(() => {
288
+ // console.log('annotationsSelectedChanged', state.annotationsSelectedChanged);
289
+
290
+ // // if (state.isDrafting && state.currentDraftId && state.draftMarkRef) {
291
+ // // annotationManager?.emit({
292
+ // // name: 'annotationSelectionChanged',
293
+ // // data: {
294
+ // // annotationId: state.currentDraftId,
295
+ // // inlineNodeTypes: [],
296
+ // // isSelected: true,
297
+ // // },
298
+ // // });
299
+ // // }
300
+ // }, [annotationManager, state.annotationsSelectedChanged]);
301
+
302
+ /**
303
+ * This is a temporary solution to ensure that the annotation manager state is in sync with the
304
+ * old updateSubscriber. The updateSubscriber will eventually be deprecated and the state will be managed
305
+ * by the annotation manager itself.
306
+ */
307
+ useEffect(function () {
308
+ var onSetAnnotationState = function onSetAnnotationState(payload) {
309
+ if (!payload) {
310
+ return;
311
+ }
312
+ Object.values(payload).forEach(function (annotation) {
313
+ if (annotation.id && annotation.annotationType === AnnotationTypes.INLINE_COMMENT && !annotation.state) {
314
+ var _annotation$state;
315
+ dispatch({
316
+ type: 'updateAnnotation',
317
+ data: {
318
+ id: annotation.id,
319
+ markState: (_annotation$state = annotation.state) !== null && _annotation$state !== void 0 ? _annotation$state : undefined
320
+ }
321
+ });
322
+ }
323
+ });
324
+ };
325
+ var onAnnotationSelected = function onAnnotationSelected(payload) {
326
+ dispatch({
327
+ type: 'updateAnnotation',
328
+ data: {
329
+ id: payload.annotationId,
330
+ selected: true
331
+ }
332
+ });
333
+ };
334
+ var onAnnotationHovered = function onAnnotationHovered(payload) {
335
+ dispatch({
336
+ type: 'updateAnnotation',
337
+ data: {
338
+ id: payload.annotationId,
339
+ hovered: true
340
+ }
341
+ });
342
+ };
343
+ var onAnnotationSelectedRemoved = function onAnnotationSelectedRemoved() {
344
+ dispatch({
345
+ type: 'resetSelectedAnnotation'
346
+ });
347
+ };
348
+ var onAnnotationHoveredRemoved = function onAnnotationHoveredRemoved() {
349
+ dispatch({
350
+ type: 'resetHoveredAnnotation'
351
+ });
352
+ };
353
+ var onAnnotationClick = function onAnnotationClick(_ref2) {
354
+ var annotationIds = _ref2.annotationIds,
355
+ eventTarget = _ref2.eventTarget,
356
+ eventTargetType = _ref2.eventTargetType,
357
+ viewMethod = _ref2.viewMethod;
358
+ dispatch({
359
+ type: 'updateAnnotation',
360
+ data: {
361
+ id: annotationIds[0],
362
+ selected: true
363
+ }
364
+ });
365
+ dispatch({
366
+ type: 'setSelectedMarkRef',
367
+ data: {
368
+ markRef: eventTarget
369
+ }
370
+ });
371
+ };
372
+ var onAnnotationDeselect = function onAnnotationDeselect() {
373
+ dispatch({
374
+ type: 'resetSelectedAnnotation'
375
+ });
376
+ };
377
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.SET_ANNOTATION_STATE, onSetAnnotationState);
378
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.SET_ANNOTATION_FOCUS, onAnnotationSelected);
379
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.SET_ANNOTATION_HOVERED, onAnnotationHovered);
380
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.REMOVE_ANNOTATION_FOCUS, onAnnotationSelectedRemoved);
381
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.REMOVE_ANNOTATION_HOVERED, onAnnotationHoveredRemoved);
382
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.ON_ANNOTATION_CLICK, onAnnotationClick);
383
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.on(AnnotationUpdateEvent.DESELECT_ANNOTATIONS, onAnnotationDeselect);
384
+ return function () {
385
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.SET_ANNOTATION_STATE, onSetAnnotationState);
386
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.SET_ANNOTATION_FOCUS, onAnnotationSelected);
387
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.SET_ANNOTATION_HOVERED, onAnnotationHovered);
388
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.REMOVE_ANNOTATION_FOCUS, onAnnotationSelectedRemoved);
389
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.REMOVE_ANNOTATION_HOVERED, onAnnotationHoveredRemoved);
390
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.ON_ANNOTATION_CLICK, onAnnotationClick);
391
+ updateSubscriber === null || updateSubscriber === void 0 || updateSubscriber.off(AnnotationUpdateEvent.DESELECT_ANNOTATIONS, onAnnotationDeselect);
392
+ };
393
+ }, [updateSubscriber]);
394
+ var dispatchData = useMemo(function () {
395
+ return {
396
+ annotationManager: annotationManager,
397
+ dispatch: dispatch
398
+ };
399
+ }, [annotationManager, dispatch]);
400
+ return /*#__PURE__*/React.createElement(AnnotationManagerStateContext.Provider, {
401
+ value: state
402
+ }, /*#__PURE__*/React.createElement(AnnotationManagerDispatchContext.Provider, {
403
+ value: dispatchData
404
+ }, children));
405
+ };
406
+ export var useAnnotationManagerState = function useAnnotationManagerState() {
407
+ return useContext(AnnotationManagerStateContext);
408
+ };
409
+ export var useAnnotationManagerDispatch = function useAnnotationManagerDispatch() {
410
+ return useContext(AnnotationManagerDispatchContext);
411
+ };
@@ -3,8 +3,7 @@ import _extends from "@babel/runtime/helpers/extends";
3
3
  * @jsxRuntime classic
4
4
  * @jsx jsx
5
5
  */
6
-
7
- import React, { Fragment } from 'react';
6
+ import React, { Fragment, useCallback } from 'react';
8
7
  // eslint-disable-next-line @atlaskit/ui-styling-standard/use-compiled -- Ignored via go/DSP-18766
9
8
  import { css, jsx } from '@emotion/react';
10
9
  import { InsertDraftPosition } from '../types';
@@ -14,6 +13,7 @@ import { calcInsertDraftPositionOnText } from './position';
14
13
  import { dataAttributes } from './dom';
15
14
  import { segmentText } from '../../../react/utils/segment-text';
16
15
  import { renderTextSegments } from '../../../react/utils/render-text-segments';
16
+ import { useAnnotationManagerDispatch } from '../contexts/AnnotationManagerContext';
17
17
 
18
18
  // Localized AnnotationSharedCSSByState().common and AnnotationSharedCSSByState().focus
19
19
  var markStyles = css({
@@ -38,12 +38,23 @@ var markStyles = css({
38
38
  export var AnnotationDraft = function AnnotationDraft(_ref) {
39
39
  var draftPosition = _ref.draftPosition,
40
40
  children = _ref.children;
41
+ var _useAnnotationManager = useAnnotationManagerDispatch(),
42
+ dispatch = _useAnnotationManager.dispatch;
43
+ var markRef = useCallback(function (node) {
44
+ dispatch({
45
+ type: 'setDraftMarkRef',
46
+ data: {
47
+ draftMarkRef: node !== null && node !== void 0 ? node : undefined
48
+ }
49
+ });
50
+ }, [dispatch]);
41
51
  return jsx("mark", _extends({
42
52
  "data-renderer-mark": true
43
53
  // Ignored via go/ees005
44
54
  // eslint-disable-next-line react/jsx-props-no-spreading
45
55
  }, dataAttributes(draftPosition), {
46
- css: markStyles
56
+ css: markStyles,
57
+ ref: markRef
47
58
  }), children);
48
59
  };
49
60
  export var getAnnotationIndex = function getAnnotationIndex(annotationPosition, fragmentCount) {
@@ -1,11 +1,13 @@
1
1
  import React, { useMemo, useCallback, useContext } from 'react';
2
2
  import { AnnotationMarkStates } from '@atlaskit/adf-schema';
3
+ import { AnnotationUpdateEvent } from '@atlaskit/editor-common/types';
4
+ import { fg } from '@atlaskit/platform-feature-flags';
3
5
  import { MarkComponent } from './mark';
4
6
  import { useInlineCommentsFilter } from '../hooks/use-inline-comments-filter';
5
7
  import { useInlineCommentSubscriberContext } from '../hooks/use-inline-comment-subscriber';
6
8
  import { useHasFocusEvent } from '../hooks/use-events';
7
9
  import { InlineCommentsStateContext } from '../context';
8
- import { AnnotationUpdateEvent } from '@atlaskit/editor-common/types';
10
+ import { useAnnotationManagerDispatch } from '../contexts/AnnotationManagerContext';
9
11
  var MarkElement = function MarkElement(_ref) {
10
12
  var annotationParentIds = _ref.annotationParentIds,
11
13
  children = _ref.children,
@@ -23,6 +25,9 @@ var MarkElement = function MarkElement(_ref) {
23
25
  var dataAttributesMemorized = useMemo(function () {
24
26
  return dataAttributes;
25
27
  }, [dataAttributes]);
28
+ var _useAnnotationManager = useAnnotationManagerDispatch(),
29
+ dispatch = _useAnnotationManager.dispatch,
30
+ annotationManager = _useAnnotationManager.annotationManager;
26
31
  var onClick = useCallback(function (props) {
27
32
  if (!updateSubscriber) {
28
33
  return;
@@ -32,11 +37,38 @@ var MarkElement = function MarkElement(_ref) {
32
37
  }
33
38
  var eventTarget = props.eventTarget,
34
39
  annotationIds = props.annotationIds;
35
- updateSubscriber.emit(AnnotationUpdateEvent.ON_ANNOTATION_CLICK, {
36
- annotationIds: annotationIds,
37
- eventTarget: eventTarget
38
- });
39
- }, [updateSubscriber, useBlockLevel]);
40
+ if (fg('platform_editor_comments_api_manager')) {
41
+ if (hasFocus) {
42
+ return;
43
+ }
44
+ annotationManager === null || annotationManager === void 0 || annotationManager.checkPreemptiveGate().then(function (canSelect) {
45
+ if (canSelect) {
46
+ dispatch({
47
+ type: 'updateAnnotation',
48
+ data: {
49
+ id: annotationIds[0],
50
+ selected: true
51
+ }
52
+ });
53
+ dispatch({
54
+ type: 'setSelectedMarkRef',
55
+ data: {
56
+ markRef: eventTarget
57
+ }
58
+ });
59
+ } else {
60
+ // TODO: EDITOR-595 - If the preemptive gate returns false, should we track the analytics event?
61
+ }
62
+ }).catch(function (error) {
63
+ // TODO: EDITOR-595 - An error occurred while checking the preemptive gate. We should report this error.
64
+ });
65
+ } else {
66
+ updateSubscriber.emit(AnnotationUpdateEvent.ON_ANNOTATION_CLICK, {
67
+ annotationIds: annotationIds,
68
+ eventTarget: eventTarget
69
+ });
70
+ }
71
+ }, [updateSubscriber, useBlockLevel, dispatch, annotationManager, hasFocus]);
40
72
  var activeParentIds = useInlineCommentsFilter({
41
73
  annotationIds: annotationParentIds,
42
74
  filter: {
@@ -14,6 +14,7 @@ import { AnnotationMarkStates } from '@atlaskit/adf-schema';
14
14
  import { fg } from '@atlaskit/platform-feature-flags';
15
15
  import { useIntl } from 'react-intl-next';
16
16
  import { inlineCommentMessages } from '../../../messages';
17
+ import { useAnnotationManagerDispatch, useAnnotationManagerState } from '../contexts/AnnotationManagerContext';
17
18
  var markStyles = css(_defineProperty({
18
19
  color: 'inherit',
19
20
  backgroundColor: 'unset',
@@ -122,6 +123,20 @@ export var MarkComponent = function MarkComponent(_ref) {
122
123
  var annotationIds = useMemo(function () {
123
124
  return _toConsumableArray(new Set([].concat(_toConsumableArray(annotationParentIds), [id])));
124
125
  }, [id, annotationParentIds]);
126
+ var _useAnnotationManager = useAnnotationManagerDispatch(),
127
+ dispatch = _useAnnotationManager.dispatch;
128
+ var _useAnnotationManager2 = useAnnotationManagerState(),
129
+ currentSelectedAnnotationId = _useAnnotationManager2.currentSelectedAnnotationId;
130
+ var markRef = useCallback(function (node) {
131
+ if (id === currentSelectedAnnotationId && node) {
132
+ dispatch({
133
+ type: 'setSelectedMarkRef',
134
+ data: {
135
+ markRef: node
136
+ }
137
+ });
138
+ }
139
+ }, [dispatch, id, currentSelectedAnnotationId]);
125
140
  var onMarkClick = useCallback(function (event) {
126
141
  // prevent inline mark logic for media block marks
127
142
  if (event.currentTarget instanceof HTMLElement && event.currentTarget.getAttribute('data-block-mark')) {
@@ -180,6 +195,7 @@ export var MarkComponent = function MarkComponent(_ref) {
180
195
  'aria-details': annotationIds.join(', ')
181
196
  }, desktopAccessibilityAttributes);
182
197
  return jsx(useBlockLevel ? 'div' : 'mark', _objectSpread(_objectSpread(_objectSpread(_defineProperty({
198
+ ref: id === currentSelectedAnnotationId ? markRef : undefined,
183
199
  id: id
184
200
  }, fg('editor_inline_comments_on_inline_nodes') ? 'onClickCapture' : 'onClick', onMarkClick), accessibility), overriddenData), !useBlockLevel && {
185
201
  css: [markStyles, fg('platform_renderer_nested_annotation_styling') && markStylesLayeringFix, fg('editor_inline_comments_on_inline_nodes') && markStylesWithInlineComments, fg('confluence-frontend-comments-panel') && markStylesWithCommentsPanel, !isMobile() && accessibilityStyles, markStylesWithUpdatedShadow],