@atlaskit/editor-common 134.10.1 → 134.11.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 (45) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/credit-guard/package.json +10 -0
  3. package/dist/cjs/credit-guard/CreditCheckedPopup.compiled.css +3 -0
  4. package/dist/cjs/credit-guard/CreditCheckedPopup.js +182 -0
  5. package/dist/cjs/credit-guard/checkAICreditStatus.js +25 -0
  6. package/dist/cjs/credit-guard/index.js +26 -0
  7. package/dist/cjs/credit-guard/types.js +5 -0
  8. package/dist/cjs/credit-guard/useAICreditGuard.js +45 -0
  9. package/dist/cjs/element-browser/components/ElementList/ElementList.js +80 -28
  10. package/dist/cjs/element-browser/components/StatelessElementBrowser.js +17 -5
  11. package/dist/cjs/monitoring/error.js +1 -1
  12. package/dist/cjs/quick-insert/insert-menu-check.js +11 -0
  13. package/dist/cjs/ui/DropList/index.js +1 -1
  14. package/dist/es2019/credit-guard/CreditCheckedPopup.compiled.css +3 -0
  15. package/dist/es2019/credit-guard/CreditCheckedPopup.js +161 -0
  16. package/dist/es2019/credit-guard/checkAICreditStatus.js +19 -0
  17. package/dist/es2019/credit-guard/index.js +5 -0
  18. package/dist/es2019/credit-guard/types.js +1 -0
  19. package/dist/es2019/credit-guard/useAICreditGuard.js +27 -0
  20. package/dist/es2019/element-browser/components/ElementList/ElementList.js +71 -20
  21. package/dist/es2019/element-browser/components/StatelessElementBrowser.js +13 -5
  22. package/dist/es2019/monitoring/error.js +1 -1
  23. package/dist/es2019/quick-insert/insert-menu-check.js +5 -0
  24. package/dist/es2019/ui/DropList/index.js +1 -1
  25. package/dist/esm/credit-guard/CreditCheckedPopup.compiled.css +3 -0
  26. package/dist/esm/credit-guard/CreditCheckedPopup.js +173 -0
  27. package/dist/esm/credit-guard/checkAICreditStatus.js +19 -0
  28. package/dist/esm/credit-guard/index.js +5 -0
  29. package/dist/esm/credit-guard/types.js +1 -0
  30. package/dist/esm/credit-guard/useAICreditGuard.js +38 -0
  31. package/dist/esm/element-browser/components/ElementList/ElementList.js +80 -28
  32. package/dist/esm/element-browser/components/StatelessElementBrowser.js +17 -5
  33. package/dist/esm/monitoring/error.js +1 -1
  34. package/dist/esm/quick-insert/insert-menu-check.js +5 -0
  35. package/dist/esm/ui/DropList/index.js +1 -1
  36. package/dist/types/credit-guard/CreditCheckedPopup.d.ts +34 -0
  37. package/dist/types/credit-guard/checkAICreditStatus.d.ts +3 -0
  38. package/dist/types/credit-guard/index.d.ts +6 -0
  39. package/dist/types/credit-guard/types.d.ts +13 -0
  40. package/dist/types/credit-guard/useAICreditGuard.d.ts +16 -0
  41. package/dist/types/element-browser/components/ElementList/ElementList.d.ts +1 -0
  42. package/dist/types/provider-factory/quick-insert-provider.d.ts +26 -0
  43. package/dist/types/quick-insert/insert-menu-check.d.ts +3 -0
  44. package/package.json +2 -1
  45. package/quick-insert/insert-menu-check/package.json +10 -0
@@ -0,0 +1,161 @@
1
+ /* CreditCheckedPopup.tsx generated by @compiled/babel-plugin v3.0.2 */
2
+ import "./CreditCheckedPopup.compiled.css";
3
+ import { ax, ix } from "@compiled/react/runtime";
4
+ import React, { useCallback, useEffect, useId, useRef, useState } from 'react';
5
+ import { bind } from 'bind-event-listener';
6
+ import { akEditorFloatingDialogZIndex } from '@atlaskit/editor-shared-styles/constants';
7
+ import { Popup } from '@atlaskit/popup/popup';
8
+ import { Box } from '@atlaskit/primitives/compiled/box';
9
+ import { useAICreditGuard } from './useAICreditGuard';
10
+ const CreditPopupTrigger = ({
11
+ props,
12
+ id,
13
+ renderTrigger,
14
+ onElement
15
+ }) => {
16
+ const popupRef = props.ref;
17
+ const ref = useCallback(element => {
18
+ onElement(element);
19
+ if (typeof popupRef === 'function') popupRef(element);else if (popupRef) Object.assign(popupRef, {
20
+ current: element
21
+ });
22
+ }, [onElement, popupRef]);
23
+ return renderTrigger({
24
+ ...props,
25
+ ref,
26
+ id
27
+ });
28
+ };
29
+ const styles = {
30
+ content: "_1bsb5jys _p12f1osq _vchhusvi"
31
+ };
32
+ const stopPropagation = event => event.stopPropagation();
33
+
34
+ /** Editor owns popup behavior; products supply content through getAICreditStatus. */
35
+ const CreditPopup = ({
36
+ guard: {
37
+ feedback,
38
+ close
39
+ },
40
+ renderTrigger,
41
+ placement = 'right-start',
42
+ isNestedMenu = false,
43
+ shouldRenderToParent
44
+ }) => {
45
+ const id = useId();
46
+ const [contentElement, setContentElement] = useState(null);
47
+ const [triggerElement, setTriggerElement] = useState(null);
48
+ const returnFocus = useRef(null);
49
+ const wasOpen = useRef(false);
50
+ useEffect(() => {
51
+ if (feedback && !wasOpen.current) {
52
+ const active = triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.ownerDocument.activeElement;
53
+ returnFocus.current = active instanceof HTMLElement ? active : null;
54
+ }
55
+ wasOpen.current = feedback !== null;
56
+ }, [feedback, triggerElement]);
57
+ useEffect(() => {
58
+ if (!feedback || !contentElement || !isNestedMenu) return;
59
+ return bind(contentElement.ownerDocument, {
60
+ type: 'scroll',
61
+ options: {
62
+ capture: true
63
+ },
64
+ listener: event => {
65
+ if (event.target instanceof Node && contentElement.contains(event.target)) return;
66
+ close();
67
+ }
68
+ });
69
+ }, [feedback, contentElement, isNestedMenu, close]);
70
+ const onKeyDown = useCallback(event => {
71
+ event.stopPropagation();
72
+ if (event.key === 'Escape') {
73
+ var _returnFocus$current;
74
+ event.preventDefault();
75
+ close();
76
+ if (isNestedMenu) (_returnFocus$current = returnFocus.current) === null || _returnFocus$current === void 0 ? void 0 : _returnFocus$current.focus();
77
+ }
78
+ }, [close, isNestedMenu]);
79
+ const content = useCallback(() => /*#__PURE__*/React.createElement(Box, {
80
+ ref: setContentElement,
81
+ padding: "space.200",
82
+ xcss: styles.content,
83
+ role: "none",
84
+ onClick: stopPropagation,
85
+ onKeyDown: onKeyDown
86
+ }, feedback === null || feedback === void 0 ? void 0 : feedback.renderCustomContent()), [feedback, onKeyDown]);
87
+ const trigger = useCallback(props => /*#__PURE__*/React.createElement(CreditPopupTrigger, {
88
+ props: props,
89
+ id: id,
90
+ renderTrigger: renderTrigger,
91
+ onElement: setTriggerElement
92
+ }), [renderTrigger, id]);
93
+ return /*#__PURE__*/React.createElement(Popup, {
94
+ isOpen: feedback !== null,
95
+ onClose: close,
96
+ role: "dialog",
97
+ titleId: (triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.id) || id,
98
+ placement: placement,
99
+ shouldFitViewport: true,
100
+ shouldRenderToParent: shouldRenderToParent !== null && shouldRenderToParent !== void 0 ? shouldRenderToParent : false,
101
+ zIndex: isNestedMenu ? akEditorFloatingDialogZIndex : undefined,
102
+ strategy: isNestedMenu ? 'fixed' : undefined,
103
+ shouldReturnFocus: !isNestedMenu,
104
+ shouldUseCaptureOnOutsideClick: true,
105
+ content: content,
106
+ trigger: trigger
107
+ });
108
+ };
109
+
110
+ /** Props the trigger applies to anchor the popup and guard its native action. */
111
+
112
+ const CreditCheckedActionPopup = ({
113
+ creditCheck,
114
+ getAICreditStatus,
115
+ onAllowed,
116
+ placement,
117
+ renderTrigger
118
+ }) => {
119
+ const guard = useAICreditGuard(getAICreditStatus);
120
+ const {
121
+ check,
122
+ close,
123
+ feedback
124
+ } = guard;
125
+ const activate = useCallback(event => {
126
+ if (feedback) {
127
+ close();
128
+ return;
129
+ }
130
+ if (check(creditCheck)) onAllowed(event);
131
+ }, [feedback, close, check, creditCheck, onAllowed]);
132
+ const trigger = useCallback(props => renderTrigger({
133
+ ...props,
134
+ 'aria-haspopup': 'dialog',
135
+ isSelected: feedback !== null,
136
+ onClick: activate
137
+ }), [renderTrigger, feedback, activate]);
138
+ return /*#__PURE__*/React.createElement(CreditPopup, {
139
+ guard: guard,
140
+ placement: placement,
141
+ renderTrigger: trigger
142
+ });
143
+ };
144
+
145
+ /** Reuses the toolbar action contract; insert-menu adapters can supply an externally triggered guard. */
146
+ export const CreditCheckedPopup = props => {
147
+ if ('guard' in props) return /*#__PURE__*/React.createElement(CreditPopup, {
148
+ guard: props.guard,
149
+ placement: props.placement,
150
+ renderTrigger: props.renderTrigger,
151
+ isNestedMenu: props.isNestedMenu,
152
+ shouldRenderToParent: props.shouldRenderToParent
153
+ });
154
+ return /*#__PURE__*/React.createElement(CreditCheckedActionPopup, {
155
+ creditCheck: props.creditCheck,
156
+ getAICreditStatus: props.getAICreditStatus,
157
+ onAllowed: props.onAllowed,
158
+ placement: props.placement,
159
+ renderTrigger: props.renderTrigger
160
+ });
161
+ };
@@ -0,0 +1,19 @@
1
+ /** Editor consumes cached decisions synchronously; legacy async providers fail open. */
2
+ export const checkAICreditStatus = (getAICreditStatus, creditCheck) => {
3
+ try {
4
+ const result = getAICreditStatus(creditCheck);
5
+ if ('then' in result) {
6
+ // Temporary compatibility: never defer an Editor action or apply a late decision.
7
+ void result.catch(() => {});
8
+ return {
9
+ isBlocked: false
10
+ };
11
+ }
12
+ return result;
13
+ } catch {
14
+ // Advisory checks fail open; action backends remain authoritative.
15
+ return {
16
+ isBlocked: false
17
+ };
18
+ }
19
+ };
@@ -0,0 +1,5 @@
1
+ /* eslint-disable @atlaskit/editor/no-re-export */
2
+
3
+ export { CreditCheckedPopup } from './CreditCheckedPopup';
4
+ export { useAICreditGuard } from './useAICreditGuard';
5
+ export { checkAICreditStatus } from './checkAICreditStatus';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { useCallback, useLayoutEffect, useState } from 'react';
2
+ import { checkAICreditStatus } from './checkAICreditStatus';
3
+ /** Shares advisory checks and feedback state without owning action execution or selection. */
4
+ export const useAICreditGuard = (getAICreditStatus, isActive = true) => {
5
+ const [feedback, setFeedback] = useState(null);
6
+ const [isBlocked, setIsBlocked] = useState(false);
7
+ const close = useCallback(() => setFeedback(null), []);
8
+ useLayoutEffect(() => {
9
+ close();
10
+ setIsBlocked(false);
11
+ }, [isActive, close, getAICreditStatus]);
12
+ const check = useCallback((creditCheck, {
13
+ showFeedback = true
14
+ } = {}) => {
15
+ if (!isActive) return false;
16
+ const result = checkAICreditStatus(getAICreditStatus, creditCheck);
17
+ setIsBlocked(result.isBlocked);
18
+ if (showFeedback) setFeedback(result.isBlocked ? result : null);
19
+ return !result.isBlocked;
20
+ }, [getAICreditStatus, isActive]);
21
+ return {
22
+ check,
23
+ close,
24
+ feedback,
25
+ isBlocked
26
+ };
27
+ };
@@ -23,6 +23,7 @@ import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
23
23
  import Tooltip from '@atlaskit/tooltip/Tooltip';
24
24
  import { ACTION, ACTION_SUBJECT, EVENT_TYPE, fireAnalyticsEvent } from '../../../analytics';
25
25
  import { IconFallback } from '../../../quick-insert';
26
+ import { runInsertMenuItemCheck } from '../../../quick-insert/insert-menu-check';
26
27
  import { ELEMENT_ITEM_HEIGHT, ELEMENT_ITEM_PADDING, ELEMENT_LIST_PADDING, SCROLLBAR_WIDTH } from '../../constants';
27
28
  import useContainerWidth from '../../hooks/use-container-width';
28
29
  import useFocus from '../../hooks/use-focus';
@@ -365,6 +366,7 @@ const ElementListMultipleColumns = props => {
365
366
  const MemoizedElementItem = /*#__PURE__*/memo(ElementItem);
366
367
  MemoizedElementItem.displayName = 'MemoizedElementItem';
367
368
 
369
+ /** Render an insert item, optionally wrapped by its interaction policy. */
368
370
  // eslint-disable-next-line @atlaskit/volt-strict-mode/no-multiple-exports
369
371
  export function ElementItem({
370
372
  inlineMode,
@@ -377,6 +379,7 @@ export function ElementItem({
377
379
  role
378
380
  }) {
379
381
  const ref = useFocus(focus);
382
+ const surface = inlineMode ? 'toolbar' : 'element-browser';
380
383
 
381
384
  /**
382
385
  * Note: props.onSelectItem(item) is not called here as the StatelessElementBrowser's
@@ -387,24 +390,69 @@ export function ElementItem({
387
390
  e.preventDefault();
388
391
  e.stopPropagation();
389
392
  setFocusedItemIndex(index);
390
- switch (e.nativeEvent.detail) {
391
- case 0:
392
- onInsertItem(item);
393
- break;
394
- case 1:
395
- if (inlineMode) {
393
+ const detail = e.nativeEvent.detail;
394
+ runInsertMenuItemCheck(item, {
395
+ surface
396
+ }, () => {
397
+ switch (detail) {
398
+ case 0:
396
399
  onInsertItem(item);
397
- }
398
- break;
399
- case 2:
400
- if (!inlineMode) {
401
- onInsertItem(item);
402
- }
403
- break;
404
- default:
405
- return;
400
+ break;
401
+ case 1:
402
+ if (inlineMode) {
403
+ onInsertItem(item);
404
+ }
405
+ break;
406
+ case 2:
407
+ if (!inlineMode) {
408
+ onInsertItem(item);
409
+ }
410
+ break;
411
+ default:
412
+ return;
413
+ }
414
+ });
415
+ }, [index, inlineMode, item, onInsertItem, setFocusedItemIndex, surface]);
416
+ const renderItem = useCallback(triggerProps => jsx(ElementItemButton, {
417
+ item: item,
418
+ index: index,
419
+ inlineMode: inlineMode,
420
+ selected: selected,
421
+ role: role,
422
+ onClick: onClick,
423
+ focusRef: ref,
424
+ triggerProps: triggerProps
425
+ }), [item, index, inlineMode, selected, role, onClick, ref]);
426
+ return item.renderMenuItemWrapper ? item.renderMenuItemWrapper({
427
+ isActive: focus || selected,
428
+ surface,
429
+ renderItem
430
+ }) : renderItem();
431
+ }
432
+ const ElementItemButton = ({
433
+ item,
434
+ index,
435
+ inlineMode,
436
+ selected,
437
+ role,
438
+ onClick,
439
+ focusRef,
440
+ triggerProps
441
+ }) => {
442
+ var _triggerProps$isSelec;
443
+ const popupRef = triggerProps === null || triggerProps === void 0 ? void 0 : triggerProps.ref;
444
+ const setTriggerRef = useCallback(element => {
445
+ Object.assign(focusRef, {
446
+ current: element
447
+ });
448
+ if (typeof popupRef === 'function') {
449
+ popupRef(element);
450
+ } else if (popupRef) {
451
+ Object.assign(popupRef, {
452
+ current: element
453
+ });
406
454
  }
407
- }, [index, inlineMode, item, onInsertItem, setFocusedItemIndex]);
455
+ }, [focusRef, popupRef]);
408
456
  const {
409
457
  icon,
410
458
  title,
@@ -422,10 +470,13 @@ export function ElementItem({
422
470
  icon: icon,
423
471
  title: title
424
472
  }),
425
- isSelected: selected,
473
+ isSelected: (_triggerProps$isSelec = triggerProps === null || triggerProps === void 0 ? void 0 : triggerProps.isSelected) !== null && _triggerProps$isSelec !== void 0 ? _triggerProps$isSelec : selected,
474
+ "aria-controls": triggerProps === null || triggerProps === void 0 ? void 0 : triggerProps['aria-controls'],
426
475
  "aria-label": title,
427
- ref: ref,
428
- testId: `element-item-${index}`,
476
+ ref: setTriggerRef,
477
+ testId: `element-item-${index}`
478
+ // Keep the search combobox's aria-activedescendant target stable.
479
+ ,
429
480
  id: `searched-item-${index}`,
430
481
  isDisabled: isDisabled,
431
482
  role: role
@@ -442,7 +493,7 @@ export function ElementItem({
442
493
  isDisabled: isDisabled,
443
494
  lozenge: lozenge
444
495
  })));
445
- }
496
+ };
446
497
 
447
498
  /**
448
499
  * Inline mode should use the existing Align-items:center value.
@@ -12,6 +12,7 @@ import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext'
12
12
  import withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
13
13
  import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
14
14
  import { ACTION, ACTION_SUBJECT, EVENT_TYPE, fireAnalyticsEvent } from '../../analytics';
15
+ import { runInsertMenuItemCheck } from '../../quick-insert/insert-menu-check';
15
16
  import editorUGCToken from '../../ugc-tokens/get-editor-ugc-token';
16
17
  import { ViewMore } from '../components/ViewMore';
17
18
  import { DEVICE_BREAKPOINT_NUMBERS, ELEMENT_BROWSER_ID, ELEMENT_BROWSER_LIST_ID, GRID_SIZE, INLINE_SIDEBAR_HEIGHT, SIDEBAR_HEADING_WRAPPER_HEIGHT, SIDEBAR_WIDTH } from '../constants';
@@ -192,6 +193,7 @@ function StatelessElementBrowser(props) {
192
193
  /* Only for hitting enter to select item when focused on search bar,
193
194
  * The actual enter key press is handled on individual items level.
194
195
  */
196
+ const surface = props.mode === 'inline' ? 'toolbar' : 'element-browser';
195
197
  const selectedItem = selectedItemIndex !== undefined ? items[selectedItemIndex] : null;
196
198
  const onItemsEnterTabKeyPress = useCallback(e => {
197
199
  var _selectedItem$isDisab;
@@ -204,11 +206,13 @@ function StatelessElementBrowser(props) {
204
206
  e.preventDefault();
205
207
  return;
206
208
  }
209
+ e.preventDefault();
207
210
  if (onInsertItem && selectedItem != null && !((_selectedItem$isDisab = selectedItem.isDisabled) !== null && _selectedItem$isDisab !== void 0 ? _selectedItem$isDisab : false)) {
208
- onInsertItem(selectedItem);
211
+ runInsertMenuItemCheck(selectedItem, {
212
+ surface
213
+ }, () => onInsertItem(selectedItem));
209
214
  }
210
- e.preventDefault();
211
- }, [onInsertItem, selectedItem, setFocusedCategoryIndex, showCategories, selectedCategoryIndex]);
215
+ }, [onInsertItem, selectedItem, setFocusedCategoryIndex, showCategories, selectedCategoryIndex, surface]);
212
216
 
213
217
  /**
214
218
  * On arrow key selection and clicks the selectedItemIndex will change.
@@ -216,9 +220,13 @@ function StatelessElementBrowser(props) {
216
220
  */
217
221
  useEffect(() => {
218
222
  if (onSelectItem && selectedItem != null) {
219
- onSelectItem(selectedItem);
223
+ // Highlighting is eligibility-only: arrows must never open a popup or take focus.
224
+ runInsertMenuItemCheck(selectedItem, {
225
+ surface,
226
+ showFeedback: false
227
+ }, () => onSelectItem(selectedItem));
220
228
  }
221
- }, [onSelectItem, selectedItem]);
229
+ }, [onSelectItem, selectedItem, surface, focusedItemIndex, selectedItemIndex]);
222
230
  const onSelectCategoryCB = useCallback(category => {
223
231
  onSelectCategory(category);
224
232
  categoryBeenChosen.current = true;
@@ -13,7 +13,7 @@ const NETWORK_FAILURE_REGEX = /^network failure/i;
13
13
  const RESIZE_OBSERVER_LOOP_REGEX = /ResizeObserver loop completed with undelivered notifications/;
14
14
  const SENTRY_DSN = 'https://0b10c8e02fb44d8796c047b102c9bee8@o55978.ingest.sentry.io/4505129224110080';
15
15
  const packageName = 'editor-common'; // Sentry doesn't accept '/' in its releases https://docs.sentry.io/platforms/javascript/configuration/releases/
16
- const packageVersion = "134.10.0";
16
+ const packageVersion = "134.10.1";
17
17
  const sanitiseSentryEvents = (data, _hint) => {
18
18
  // Remove URL as it has UGC
19
19
  // Ignored via go/ees007
@@ -0,0 +1,5 @@
1
+ /** Check eligibility before selecting or inserting in the original event handler. */
2
+ export const runInsertMenuItemCheck = (item, options, onAllowed) => {
3
+ var _item$onBeforeSelect;
4
+ if (((_item$onBeforeSelect = item.onBeforeSelect) === null || _item$onBeforeSelect === void 0 ? void 0 : _item$onBeforeSelect.call(item, options)) !== false) onAllowed();
5
+ };
@@ -13,7 +13,7 @@ import withAnalyticsContext from '@atlaskit/analytics-next/withAnalyticsContext'
13
13
  import withAnalyticsEvents from '@atlaskit/analytics-next/withAnalyticsEvents';
14
14
  import Layer from '../Layer';
15
15
  const packageName = "@atlaskit/editor-common";
16
- const packageVersion = "134.10.0";
16
+ const packageVersion = "134.10.1";
17
17
  const halfFocusRing = 1;
18
18
  const dropOffset = '0, 8';
19
19
  // Ignored via go/ees005
@@ -0,0 +1,3 @@
1
+ ._1bsb5jys{width:11pc}
2
+ ._p12f1osq{max-width:100%}
3
+ ._vchhusvi{box-sizing:border-box}
@@ -0,0 +1,173 @@
1
+ /* CreditCheckedPopup.tsx generated by @compiled/babel-plugin v3.0.2 */
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
+ import "./CreditCheckedPopup.compiled.css";
5
+ import { ax, ix } from "@compiled/react/runtime";
6
+ 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; }
7
+ 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; }
8
+ import React, { useCallback, useEffect, useId, useRef, useState } from 'react';
9
+ import { bind } from 'bind-event-listener';
10
+ import { akEditorFloatingDialogZIndex } from '@atlaskit/editor-shared-styles/constants';
11
+ import { Popup } from '@atlaskit/popup/popup';
12
+ import { Box } from '@atlaskit/primitives/compiled/box';
13
+ import { useAICreditGuard } from './useAICreditGuard';
14
+ var CreditPopupTrigger = function CreditPopupTrigger(_ref) {
15
+ var props = _ref.props,
16
+ id = _ref.id,
17
+ renderTrigger = _ref.renderTrigger,
18
+ onElement = _ref.onElement;
19
+ var popupRef = props.ref;
20
+ var ref = useCallback(function (element) {
21
+ onElement(element);
22
+ if (typeof popupRef === 'function') popupRef(element);else if (popupRef) Object.assign(popupRef, {
23
+ current: element
24
+ });
25
+ }, [onElement, popupRef]);
26
+ return renderTrigger(_objectSpread(_objectSpread({}, props), {}, {
27
+ ref: ref,
28
+ id: id
29
+ }));
30
+ };
31
+ var styles = {
32
+ content: "_1bsb5jys _p12f1osq _vchhusvi"
33
+ };
34
+ var stopPropagation = function stopPropagation(event) {
35
+ return event.stopPropagation();
36
+ };
37
+
38
+ /** Editor owns popup behavior; products supply content through getAICreditStatus. */
39
+ var CreditPopup = function CreditPopup(_ref2) {
40
+ var _ref2$guard = _ref2.guard,
41
+ feedback = _ref2$guard.feedback,
42
+ close = _ref2$guard.close,
43
+ renderTrigger = _ref2.renderTrigger,
44
+ _ref2$placement = _ref2.placement,
45
+ placement = _ref2$placement === void 0 ? 'right-start' : _ref2$placement,
46
+ _ref2$isNestedMenu = _ref2.isNestedMenu,
47
+ isNestedMenu = _ref2$isNestedMenu === void 0 ? false : _ref2$isNestedMenu,
48
+ shouldRenderToParent = _ref2.shouldRenderToParent;
49
+ var id = useId();
50
+ var _useState = useState(null),
51
+ _useState2 = _slicedToArray(_useState, 2),
52
+ contentElement = _useState2[0],
53
+ setContentElement = _useState2[1];
54
+ var _useState3 = useState(null),
55
+ _useState4 = _slicedToArray(_useState3, 2),
56
+ triggerElement = _useState4[0],
57
+ setTriggerElement = _useState4[1];
58
+ var returnFocus = useRef(null);
59
+ var wasOpen = useRef(false);
60
+ useEffect(function () {
61
+ if (feedback && !wasOpen.current) {
62
+ var active = triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.ownerDocument.activeElement;
63
+ returnFocus.current = active instanceof HTMLElement ? active : null;
64
+ }
65
+ wasOpen.current = feedback !== null;
66
+ }, [feedback, triggerElement]);
67
+ useEffect(function () {
68
+ if (!feedback || !contentElement || !isNestedMenu) return;
69
+ return bind(contentElement.ownerDocument, {
70
+ type: 'scroll',
71
+ options: {
72
+ capture: true
73
+ },
74
+ listener: function listener(event) {
75
+ if (event.target instanceof Node && contentElement.contains(event.target)) return;
76
+ close();
77
+ }
78
+ });
79
+ }, [feedback, contentElement, isNestedMenu, close]);
80
+ var onKeyDown = useCallback(function (event) {
81
+ event.stopPropagation();
82
+ if (event.key === 'Escape') {
83
+ var _returnFocus$current;
84
+ event.preventDefault();
85
+ close();
86
+ if (isNestedMenu) (_returnFocus$current = returnFocus.current) === null || _returnFocus$current === void 0 || _returnFocus$current.focus();
87
+ }
88
+ }, [close, isNestedMenu]);
89
+ var content = useCallback(function () {
90
+ return /*#__PURE__*/React.createElement(Box, {
91
+ ref: setContentElement,
92
+ padding: "space.200",
93
+ xcss: styles.content,
94
+ role: "none",
95
+ onClick: stopPropagation,
96
+ onKeyDown: onKeyDown
97
+ }, feedback === null || feedback === void 0 ? void 0 : feedback.renderCustomContent());
98
+ }, [feedback, onKeyDown]);
99
+ var trigger = useCallback(function (props) {
100
+ return /*#__PURE__*/React.createElement(CreditPopupTrigger, {
101
+ props: props,
102
+ id: id,
103
+ renderTrigger: renderTrigger,
104
+ onElement: setTriggerElement
105
+ });
106
+ }, [renderTrigger, id]);
107
+ return /*#__PURE__*/React.createElement(Popup, {
108
+ isOpen: feedback !== null,
109
+ onClose: close,
110
+ role: "dialog",
111
+ titleId: (triggerElement === null || triggerElement === void 0 ? void 0 : triggerElement.id) || id,
112
+ placement: placement,
113
+ shouldFitViewport: true,
114
+ shouldRenderToParent: shouldRenderToParent !== null && shouldRenderToParent !== void 0 ? shouldRenderToParent : false,
115
+ zIndex: isNestedMenu ? akEditorFloatingDialogZIndex : undefined,
116
+ strategy: isNestedMenu ? 'fixed' : undefined,
117
+ shouldReturnFocus: !isNestedMenu,
118
+ shouldUseCaptureOnOutsideClick: true,
119
+ content: content,
120
+ trigger: trigger
121
+ });
122
+ };
123
+
124
+ /** Props the trigger applies to anchor the popup and guard its native action. */
125
+
126
+ var CreditCheckedActionPopup = function CreditCheckedActionPopup(_ref3) {
127
+ var creditCheck = _ref3.creditCheck,
128
+ getAICreditStatus = _ref3.getAICreditStatus,
129
+ onAllowed = _ref3.onAllowed,
130
+ placement = _ref3.placement,
131
+ renderTrigger = _ref3.renderTrigger;
132
+ var guard = useAICreditGuard(getAICreditStatus);
133
+ var check = guard.check,
134
+ close = guard.close,
135
+ feedback = guard.feedback;
136
+ var activate = useCallback(function (event) {
137
+ if (feedback) {
138
+ close();
139
+ return;
140
+ }
141
+ if (check(creditCheck)) onAllowed(event);
142
+ }, [feedback, close, check, creditCheck, onAllowed]);
143
+ var trigger = useCallback(function (props) {
144
+ return renderTrigger(_objectSpread(_objectSpread({}, props), {}, {
145
+ 'aria-haspopup': 'dialog',
146
+ isSelected: feedback !== null,
147
+ onClick: activate
148
+ }));
149
+ }, [renderTrigger, feedback, activate]);
150
+ return /*#__PURE__*/React.createElement(CreditPopup, {
151
+ guard: guard,
152
+ placement: placement,
153
+ renderTrigger: trigger
154
+ });
155
+ };
156
+
157
+ /** Reuses the toolbar action contract; insert-menu adapters can supply an externally triggered guard. */
158
+ export var CreditCheckedPopup = function CreditCheckedPopup(props) {
159
+ if ('guard' in props) return /*#__PURE__*/React.createElement(CreditPopup, {
160
+ guard: props.guard,
161
+ placement: props.placement,
162
+ renderTrigger: props.renderTrigger,
163
+ isNestedMenu: props.isNestedMenu,
164
+ shouldRenderToParent: props.shouldRenderToParent
165
+ });
166
+ return /*#__PURE__*/React.createElement(CreditCheckedActionPopup, {
167
+ creditCheck: props.creditCheck,
168
+ getAICreditStatus: props.getAICreditStatus,
169
+ onAllowed: props.onAllowed,
170
+ placement: props.placement,
171
+ renderTrigger: props.renderTrigger
172
+ });
173
+ };
@@ -0,0 +1,19 @@
1
+ /** Editor consumes cached decisions synchronously; legacy async providers fail open. */
2
+ export var checkAICreditStatus = function checkAICreditStatus(getAICreditStatus, creditCheck) {
3
+ try {
4
+ var result = getAICreditStatus(creditCheck);
5
+ if ('then' in result) {
6
+ // Temporary compatibility: never defer an Editor action or apply a late decision.
7
+ void result.catch(function () {});
8
+ return {
9
+ isBlocked: false
10
+ };
11
+ }
12
+ return result;
13
+ } catch (_unused) {
14
+ // Advisory checks fail open; action backends remain authoritative.
15
+ return {
16
+ isBlocked: false
17
+ };
18
+ }
19
+ };
@@ -0,0 +1,5 @@
1
+ /* eslint-disable @atlaskit/editor/no-re-export */
2
+
3
+ export { CreditCheckedPopup } from './CreditCheckedPopup';
4
+ export { useAICreditGuard } from './useAICreditGuard';
5
+ export { checkAICreditStatus } from './checkAICreditStatus';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useCallback, useLayoutEffect, useState } from 'react';
3
+ import { checkAICreditStatus } from './checkAICreditStatus';
4
+ /** Shares advisory checks and feedback state without owning action execution or selection. */
5
+ export var useAICreditGuard = function useAICreditGuard(getAICreditStatus) {
6
+ var isActive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
7
+ var _useState = useState(null),
8
+ _useState2 = _slicedToArray(_useState, 2),
9
+ feedback = _useState2[0],
10
+ setFeedback = _useState2[1];
11
+ var _useState3 = useState(false),
12
+ _useState4 = _slicedToArray(_useState3, 2),
13
+ isBlocked = _useState4[0],
14
+ setIsBlocked = _useState4[1];
15
+ var close = useCallback(function () {
16
+ return setFeedback(null);
17
+ }, []);
18
+ useLayoutEffect(function () {
19
+ close();
20
+ setIsBlocked(false);
21
+ }, [isActive, close, getAICreditStatus]);
22
+ var check = useCallback(function (creditCheck) {
23
+ var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
24
+ _ref$showFeedback = _ref.showFeedback,
25
+ showFeedback = _ref$showFeedback === void 0 ? true : _ref$showFeedback;
26
+ if (!isActive) return false;
27
+ var result = checkAICreditStatus(getAICreditStatus, creditCheck);
28
+ setIsBlocked(result.isBlocked);
29
+ if (showFeedback) setFeedback(result.isBlocked ? result : null);
30
+ return !result.isBlocked;
31
+ }, [getAICreditStatus, isActive]);
32
+ return {
33
+ check: check,
34
+ close: close,
35
+ feedback: feedback,
36
+ isBlocked: isBlocked
37
+ };
38
+ };