@atlaskit/editor-plugin-type-ahead 0.6.0 → 0.7.1

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 (160) hide show
  1. package/.eslintrc.js +5 -0
  2. package/CHANGELOG.md +12 -0
  3. package/dist/cjs/api.js +215 -0
  4. package/dist/cjs/commands/insert-type-ahead-item.js +205 -0
  5. package/dist/cjs/commands/update-list-items.js +23 -0
  6. package/dist/cjs/commands/update-query.js +27 -0
  7. package/dist/cjs/commands/update-selected-index.js +27 -0
  8. package/dist/cjs/index.js +8 -1
  9. package/dist/cjs/insert-utils.js +107 -0
  10. package/dist/cjs/messages.js +79 -0
  11. package/dist/cjs/plugin.js +382 -0
  12. package/dist/cjs/pm-plugins/actions.js +16 -0
  13. package/dist/cjs/pm-plugins/decorations.js +148 -0
  14. package/dist/cjs/pm-plugins/input-rules.js +36 -0
  15. package/dist/cjs/pm-plugins/insert-item-plugin.js +22 -0
  16. package/dist/cjs/pm-plugins/key.js +8 -0
  17. package/dist/cjs/pm-plugins/main.js +110 -0
  18. package/dist/cjs/pm-plugins/reducer.js +158 -0
  19. package/dist/cjs/pm-plugins/utils.js +18 -0
  20. package/dist/cjs/stats-modifier.js +42 -0
  21. package/dist/cjs/transforms/close-type-ahead.js +13 -0
  22. package/dist/cjs/transforms/open-typeahead-at-cursor.js +75 -0
  23. package/dist/cjs/transforms/set-selection-before-query.js +18 -0
  24. package/dist/cjs/ui/AssistiveText.js +120 -0
  25. package/dist/cjs/ui/InputQuery.js +400 -0
  26. package/dist/cjs/ui/TypeAheadList.js +285 -0
  27. package/dist/cjs/ui/TypeAheadListItem.js +175 -0
  28. package/dist/cjs/ui/TypeAheadPopup.js +230 -0
  29. package/dist/cjs/ui/WrapperTypeAhead.js +127 -0
  30. package/dist/cjs/ui/hooks/use-item-insert.js +109 -0
  31. package/dist/cjs/ui/hooks/use-load-items.js +50 -0
  32. package/dist/cjs/ui/hooks/use-on-force-select.js +41 -0
  33. package/dist/cjs/utils.js +130 -0
  34. package/dist/es2019/api.js +205 -0
  35. package/dist/es2019/commands/insert-type-ahead-item.js +204 -0
  36. package/dist/es2019/commands/update-list-items.js +17 -0
  37. package/dist/es2019/commands/update-query.js +21 -0
  38. package/dist/es2019/commands/update-selected-index.js +21 -0
  39. package/dist/es2019/index.js +1 -1
  40. package/dist/es2019/insert-utils.js +106 -0
  41. package/dist/es2019/messages.js +73 -0
  42. package/dist/es2019/plugin.js +381 -0
  43. package/dist/es2019/pm-plugins/actions.js +10 -0
  44. package/dist/es2019/pm-plugins/decorations.js +148 -0
  45. package/dist/es2019/pm-plugins/input-rules.js +29 -0
  46. package/dist/es2019/pm-plugins/insert-item-plugin.js +16 -0
  47. package/dist/es2019/pm-plugins/key.js +2 -0
  48. package/dist/es2019/pm-plugins/main.js +106 -0
  49. package/dist/es2019/pm-plugins/reducer.js +160 -0
  50. package/dist/es2019/pm-plugins/utils.js +12 -0
  51. package/dist/es2019/stats-modifier.js +33 -0
  52. package/dist/es2019/transforms/close-type-ahead.js +7 -0
  53. package/dist/es2019/transforms/open-typeahead-at-cursor.js +71 -0
  54. package/dist/es2019/transforms/set-selection-before-query.js +10 -0
  55. package/dist/es2019/ui/AssistiveText.js +88 -0
  56. package/dist/es2019/ui/InputQuery.js +392 -0
  57. package/dist/es2019/ui/TypeAheadList.js +273 -0
  58. package/dist/es2019/ui/TypeAheadListItem.js +212 -0
  59. package/dist/es2019/ui/TypeAheadPopup.js +233 -0
  60. package/dist/es2019/ui/WrapperTypeAhead.js +109 -0
  61. package/dist/es2019/ui/hooks/use-item-insert.js +112 -0
  62. package/dist/es2019/ui/hooks/use-load-items.js +41 -0
  63. package/dist/es2019/ui/hooks/use-on-force-select.js +38 -0
  64. package/dist/es2019/utils.js +126 -0
  65. package/dist/esm/api.js +209 -0
  66. package/dist/esm/commands/insert-type-ahead-item.js +198 -0
  67. package/dist/esm/commands/update-list-items.js +17 -0
  68. package/dist/esm/commands/update-query.js +21 -0
  69. package/dist/esm/commands/update-selected-index.js +21 -0
  70. package/dist/esm/index.js +1 -1
  71. package/dist/esm/insert-utils.js +101 -0
  72. package/dist/esm/messages.js +73 -0
  73. package/dist/esm/plugin.js +374 -0
  74. package/dist/esm/pm-plugins/actions.js +10 -0
  75. package/dist/esm/pm-plugins/decorations.js +141 -0
  76. package/dist/esm/pm-plugins/input-rules.js +29 -0
  77. package/dist/esm/pm-plugins/insert-item-plugin.js +16 -0
  78. package/dist/esm/pm-plugins/key.js +2 -0
  79. package/dist/esm/pm-plugins/main.js +104 -0
  80. package/dist/esm/pm-plugins/reducer.js +151 -0
  81. package/dist/esm/pm-plugins/utils.js +12 -0
  82. package/dist/esm/stats-modifier.js +35 -0
  83. package/dist/esm/transforms/close-type-ahead.js +7 -0
  84. package/dist/esm/transforms/open-typeahead-at-cursor.js +69 -0
  85. package/dist/esm/transforms/set-selection-before-query.js +12 -0
  86. package/dist/esm/ui/AssistiveText.js +115 -0
  87. package/dist/esm/ui/InputQuery.js +389 -0
  88. package/dist/esm/ui/TypeAheadList.js +276 -0
  89. package/dist/esm/ui/TypeAheadListItem.js +165 -0
  90. package/dist/esm/ui/TypeAheadPopup.js +220 -0
  91. package/dist/esm/ui/WrapperTypeAhead.js +117 -0
  92. package/dist/esm/ui/hooks/use-item-insert.js +103 -0
  93. package/dist/esm/ui/hooks/use-load-items.js +43 -0
  94. package/dist/esm/ui/hooks/use-on-force-select.js +35 -0
  95. package/dist/esm/utils.js +124 -0
  96. package/dist/types/api.d.ts +60 -0
  97. package/dist/types/commands/insert-type-ahead-item.d.ts +12 -0
  98. package/dist/types/commands/update-list-items.d.ts +3 -0
  99. package/dist/types/commands/update-query.d.ts +2 -0
  100. package/dist/types/commands/update-selected-index.d.ts +2 -0
  101. package/dist/types/index.d.ts +2 -1
  102. package/dist/types/insert-utils.d.ts +18 -0
  103. package/dist/types/messages.d.ts +72 -0
  104. package/dist/types/plugin.d.ts +10 -0
  105. package/dist/types/pm-plugins/actions.d.ts +9 -0
  106. package/dist/types/pm-plugins/decorations.d.ts +14 -0
  107. package/dist/types/pm-plugins/input-rules.d.ts +6 -0
  108. package/dist/types/pm-plugins/insert-item-plugin.d.ts +2 -0
  109. package/dist/types/pm-plugins/key.d.ts +3 -0
  110. package/dist/types/pm-plugins/main.d.ts +14 -0
  111. package/dist/types/pm-plugins/reducer.d.ts +10 -0
  112. package/dist/types/pm-plugins/utils.d.ts +4 -0
  113. package/dist/types/stats-modifier.d.ts +20 -0
  114. package/dist/types/transforms/close-type-ahead.d.ts +2 -0
  115. package/dist/types/transforms/open-typeahead-at-cursor.d.ts +11 -0
  116. package/dist/types/transforms/set-selection-before-query.d.ts +2 -0
  117. package/dist/types/ui/AssistiveText.d.ts +33 -0
  118. package/dist/types/ui/InputQuery.d.ts +26 -0
  119. package/dist/types/ui/TypeAheadList.d.ts +25 -0
  120. package/dist/types/ui/TypeAheadListItem.d.ts +18 -0
  121. package/dist/types/ui/TypeAheadPopup.d.ts +29 -0
  122. package/dist/types/ui/WrapperTypeAhead.d.ts +20 -0
  123. package/dist/types/ui/hooks/use-item-insert.d.ts +3 -0
  124. package/dist/types/ui/hooks/use-load-items.d.ts +3 -0
  125. package/dist/types/ui/hooks/use-on-force-select.d.ts +11 -0
  126. package/dist/types/utils.d.ts +27 -0
  127. package/dist/types-ts4.5/api.d.ts +60 -0
  128. package/dist/types-ts4.5/commands/insert-type-ahead-item.d.ts +12 -0
  129. package/dist/types-ts4.5/commands/update-list-items.d.ts +3 -0
  130. package/dist/types-ts4.5/commands/update-query.d.ts +2 -0
  131. package/dist/types-ts4.5/commands/update-selected-index.d.ts +2 -0
  132. package/dist/types-ts4.5/index.d.ts +2 -1
  133. package/dist/types-ts4.5/insert-utils.d.ts +18 -0
  134. package/dist/types-ts4.5/messages.d.ts +72 -0
  135. package/dist/types-ts4.5/plugin.d.ts +10 -0
  136. package/dist/types-ts4.5/pm-plugins/actions.d.ts +9 -0
  137. package/dist/types-ts4.5/pm-plugins/decorations.d.ts +14 -0
  138. package/dist/types-ts4.5/pm-plugins/input-rules.d.ts +6 -0
  139. package/dist/types-ts4.5/pm-plugins/insert-item-plugin.d.ts +2 -0
  140. package/dist/types-ts4.5/pm-plugins/key.d.ts +3 -0
  141. package/dist/types-ts4.5/pm-plugins/main.d.ts +14 -0
  142. package/dist/types-ts4.5/pm-plugins/reducer.d.ts +10 -0
  143. package/dist/types-ts4.5/pm-plugins/utils.d.ts +4 -0
  144. package/dist/types-ts4.5/stats-modifier.d.ts +20 -0
  145. package/dist/types-ts4.5/transforms/close-type-ahead.d.ts +2 -0
  146. package/dist/types-ts4.5/transforms/open-typeahead-at-cursor.d.ts +11 -0
  147. package/dist/types-ts4.5/transforms/set-selection-before-query.d.ts +2 -0
  148. package/dist/types-ts4.5/ui/AssistiveText.d.ts +33 -0
  149. package/dist/types-ts4.5/ui/InputQuery.d.ts +26 -0
  150. package/dist/types-ts4.5/ui/TypeAheadList.d.ts +25 -0
  151. package/dist/types-ts4.5/ui/TypeAheadListItem.d.ts +18 -0
  152. package/dist/types-ts4.5/ui/TypeAheadPopup.d.ts +29 -0
  153. package/dist/types-ts4.5/ui/WrapperTypeAhead.d.ts +20 -0
  154. package/dist/types-ts4.5/ui/hooks/use-item-insert.d.ts +7 -0
  155. package/dist/types-ts4.5/ui/hooks/use-load-items.d.ts +3 -0
  156. package/dist/types-ts4.5/ui/hooks/use-on-force-select.d.ts +11 -0
  157. package/dist/types-ts4.5/utils.d.ts +27 -0
  158. package/package.json +21 -25
  159. package/report.api.md +29 -1
  160. package/tmp/api-report-tmp.d.ts +26 -0
@@ -0,0 +1,117 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import React, { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
3
+ import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
4
+ import { updateQuery } from '../commands/update-query';
5
+ import { getPluginState, moveSelectedIndex } from '../utils';
6
+ import { useItemInsert } from './hooks/use-item-insert';
7
+ import { useLoadItems } from './hooks/use-load-items';
8
+ import { useOnForceSelect } from './hooks/use-on-force-select';
9
+ import { InputQuery } from './InputQuery';
10
+ export var WrapperTypeAhead = /*#__PURE__*/React.memo(function (_ref) {
11
+ var triggerHandler = _ref.triggerHandler,
12
+ editorView = _ref.editorView,
13
+ anchorElement = _ref.anchorElement,
14
+ shouldFocusCursorInsideQuery = _ref.shouldFocusCursorInsideQuery,
15
+ popupsMountPoint = _ref.popupsMountPoint,
16
+ popupsBoundariesElement = _ref.popupsBoundariesElement,
17
+ popupsScrollableElement = _ref.popupsScrollableElement,
18
+ createAnalyticsEvent = _ref.createAnalyticsEvent,
19
+ inputMethod = _ref.inputMethod,
20
+ getDecorationPosition = _ref.getDecorationPosition,
21
+ reopenQuery = _ref.reopenQuery,
22
+ onUndoRedo = _ref.onUndoRedo;
23
+ var _useState = useState(false),
24
+ _useState2 = _slicedToArray(_useState, 2),
25
+ closed = _useState2[0],
26
+ setClosed = _useState2[1];
27
+ var _useState3 = useState(reopenQuery || ''),
28
+ _useState4 = _slicedToArray(_useState3, 2),
29
+ query = _useState4[0],
30
+ setQuery = _useState4[1];
31
+ var queryRef = useRef(query);
32
+ var editorViewRef = useRef(editorView);
33
+ var items = useLoadItems(triggerHandler, editorView, query);
34
+ useLayoutEffect(function () {
35
+ queryRef.current = query;
36
+ }, [query]);
37
+ var _useItemInsert = useItemInsert(triggerHandler, editorView, items),
38
+ _useItemInsert2 = _slicedToArray(_useItemInsert, 2),
39
+ onItemInsert = _useItemInsert2[0],
40
+ onTextInsert = _useItemInsert2[1];
41
+ var selectNextItem = useMemo(function () {
42
+ return moveSelectedIndex({
43
+ editorView: editorView,
44
+ direction: 'next'
45
+ });
46
+ }, [editorView]);
47
+ var selectPreviousItem = useMemo(function () {
48
+ return moveSelectedIndex({
49
+ editorView: editorView,
50
+ direction: 'previous'
51
+ });
52
+ }, [editorView]);
53
+ var cancel = useCallback(function (_ref2) {
54
+ var setSelectionAt = _ref2.setSelectionAt,
55
+ addPrefixTrigger = _ref2.addPrefixTrigger,
56
+ text = _ref2.text,
57
+ forceFocusOnEditor = _ref2.forceFocusOnEditor;
58
+ setClosed(true);
59
+ var fullquery = addPrefixTrigger ? "".concat(triggerHandler.trigger).concat(text) : text;
60
+ onTextInsert({
61
+ forceFocusOnEditor: forceFocusOnEditor,
62
+ setSelectionAt: setSelectionAt,
63
+ text: fullquery
64
+ });
65
+ }, [triggerHandler, onTextInsert]);
66
+ var insertSelectedItem = useCallback(function () {
67
+ var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : SelectItemMode.SELECTED;
68
+ var view = editorViewRef.current;
69
+ var _ref3 = getPluginState(view.state),
70
+ selectedIndex = _ref3.selectedIndex;
71
+ setClosed(true);
72
+ queueMicrotask(function () {
73
+ onItemInsert({
74
+ mode: mode,
75
+ index: selectedIndex,
76
+ query: queryRef.current
77
+ });
78
+ });
79
+ }, [onItemInsert]);
80
+ var showTypeAheadPopupList = useCallback(function () {}, []);
81
+ var closePopup = useCallback(function () {
82
+ setClosed(true);
83
+ }, []);
84
+ useEffect(function () {
85
+ var view = editorViewRef.current;
86
+ var pluginState = getPluginState(view.state);
87
+ if (query.length === 0 || query === (pluginState === null || pluginState === void 0 ? void 0 : pluginState.query) || !(pluginState !== null && pluginState !== void 0 && pluginState.triggerHandler)) {
88
+ return;
89
+ }
90
+ updateQuery(query)(view.state, view.dispatch);
91
+ }, [query, reopenQuery]);
92
+ useOnForceSelect({
93
+ triggerHandler: triggerHandler,
94
+ items: items,
95
+ query: query,
96
+ editorView: editorView,
97
+ closePopup: closePopup
98
+ });
99
+ if (closed) {
100
+ return null;
101
+ }
102
+ return /*#__PURE__*/React.createElement(InputQuery, {
103
+ triggerQueryPrefix: triggerHandler.trigger,
104
+ onQueryChange: setQuery,
105
+ onItemSelect: insertSelectedItem,
106
+ selectNextItem: selectNextItem,
107
+ selectPreviousItem: selectPreviousItem,
108
+ onQueryFocus: showTypeAheadPopupList,
109
+ cancel: cancel,
110
+ forceFocus: shouldFocusCursorInsideQuery,
111
+ onUndoRedo: onUndoRedo,
112
+ reopenQuery: reopenQuery,
113
+ editorView: editorView,
114
+ items: items
115
+ });
116
+ });
117
+ WrapperTypeAhead.displayName = 'WrapperTypeAhead';
@@ -0,0 +1,103 @@
1
+ import { useCallback, useLayoutEffect, useRef } from 'react';
2
+ import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
3
+ import { insertTypeAheadItem } from '../../commands/insert-type-ahead-item';
4
+ import { CloseSelectionOptions } from '../../constants';
5
+ import { closeTypeAhead } from '../../transforms/close-type-ahead';
6
+ import { setSelectionBeforeQuery } from '../../transforms/set-selection-before-query';
7
+ var insertRawQuery = function insertRawQuery(_ref) {
8
+ var view = _ref.view,
9
+ setSelectionAt = _ref.setSelectionAt,
10
+ text = _ref.text,
11
+ forceFocusOnEditor = _ref.forceFocusOnEditor;
12
+ var _view$state = view.state,
13
+ tr = _view$state.tr,
14
+ schema = _view$state.schema;
15
+ closeTypeAhead(tr);
16
+ if (text.length > 0) {
17
+ tr.replaceSelectionWith(schema.text(text));
18
+ if (setSelectionAt === CloseSelectionOptions.BEFORE_TEXT_INSERTED) {
19
+ setSelectionBeforeQuery(text)(tr);
20
+ }
21
+ }
22
+ view.dispatch(tr);
23
+ if (forceFocusOnEditor) {
24
+ view.focus();
25
+ }
26
+ };
27
+ export var useItemInsert = function useItemInsert(triggerHandler, editorView, items) {
28
+ var editorViewRef = useRef(editorView);
29
+ var itemsRef = useRef(items);
30
+ var onTextInsert = useCallback(function (_ref2) {
31
+ var setSelectionAt = _ref2.setSelectionAt,
32
+ text = _ref2.text,
33
+ forceFocusOnEditor = _ref2.forceFocusOnEditor;
34
+ if (!triggerHandler) {
35
+ return;
36
+ }
37
+ var view = editorViewRef.current;
38
+ insertRawQuery({
39
+ view: view,
40
+ setSelectionAt: setSelectionAt,
41
+ text: text,
42
+ forceFocusOnEditor: forceFocusOnEditor
43
+ });
44
+ }, [triggerHandler]);
45
+ var onItemInsert = useCallback(function (_ref3) {
46
+ var mode = _ref3.mode,
47
+ index = _ref3.index,
48
+ query = _ref3.query;
49
+ var sourceListItem = itemsRef.current;
50
+ if (sourceListItem.length === 0 || !triggerHandler) {
51
+ var text = "".concat(triggerHandler.trigger).concat(query);
52
+ onTextInsert({
53
+ forceFocusOnEditor: true,
54
+ setSelectionAt: CloseSelectionOptions.AFTER_TEXT_INSERTED,
55
+ text: text
56
+ });
57
+ return;
58
+ }
59
+ var itemToInsert = sourceListItem[index];
60
+ if (!itemToInsert) {
61
+ return;
62
+ }
63
+ var view = editorViewRef.current;
64
+ insertTypeAheadItem(view)({
65
+ item: itemToInsert,
66
+ handler: triggerHandler,
67
+ mode: mode,
68
+ query: query,
69
+ sourceListItem: sourceListItem
70
+ });
71
+ }, [triggerHandler, onTextInsert]);
72
+ var onItemMatch = useCallback(function (_ref4) {
73
+ var mode = _ref4.mode,
74
+ query = _ref4.query;
75
+ var _items = itemsRef.current;
76
+ if (_items && _items.length > 1) {
77
+ return false;
78
+ }
79
+ if (_items.length === 1) {
80
+ queueMicrotask(function () {
81
+ onItemInsert({
82
+ mode: mode,
83
+ query: query,
84
+ index: 0
85
+ });
86
+ });
87
+ } else {
88
+ var text = "".concat(triggerHandler.trigger).concat(query);
89
+ queueMicrotask(function () {
90
+ onTextInsert({
91
+ forceFocusOnEditor: true,
92
+ setSelectionAt: CloseSelectionOptions.AFTER_TEXT_INSERTED,
93
+ text: mode === SelectItemMode.SPACE ? text.concat(' ') : text
94
+ });
95
+ });
96
+ }
97
+ return true;
98
+ }, [onItemInsert, triggerHandler, onTextInsert]);
99
+ useLayoutEffect(function () {
100
+ itemsRef.current = items;
101
+ }, [items]);
102
+ return [onItemInsert, onTextInsert, onItemMatch];
103
+ };
@@ -0,0 +1,43 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useEffect, useRef, useState } from 'react';
3
+ import { updateListItem } from '../../commands/update-list-items';
4
+ var EMPTY_LIST_ITEM = [];
5
+ export var useLoadItems = function useLoadItems(triggerHandler, editorView, query) {
6
+ var _useState = useState(EMPTY_LIST_ITEM),
7
+ _useState2 = _slicedToArray(_useState, 2),
8
+ items = _useState2[0],
9
+ setItems = _useState2[1];
10
+ var componentIsMounted = useRef(true);
11
+ var editorViewRef = useRef(editorView);
12
+ useEffect(function () {
13
+ var getItems = triggerHandler === null || triggerHandler === void 0 ? void 0 : triggerHandler.getItems;
14
+ if (!getItems) {
15
+ setItems(EMPTY_LIST_ITEM);
16
+ return;
17
+ }
18
+ var options = {
19
+ query: query || '',
20
+ editorState: editorView.state
21
+ };
22
+ var view = editorViewRef.current;
23
+ getItems(options).then(function (result) {
24
+ var list = result.length > 0 ? result : EMPTY_LIST_ITEM;
25
+ if (componentIsMounted.current) {
26
+ setItems(list);
27
+ }
28
+ queueMicrotask(function () {
29
+ updateListItem(list)(view.state, view.dispatch);
30
+ });
31
+ });
32
+
33
+ // ignore because EditorView is mutable but we don't want to
34
+ // call loadItems when it changes, only when the query changes
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ }, [triggerHandler, query]);
37
+ useEffect(function () {
38
+ return function () {
39
+ componentIsMounted.current = false;
40
+ };
41
+ }, []);
42
+ return items;
43
+ };
@@ -0,0 +1,35 @@
1
+ import { useLayoutEffect, useRef } from 'react';
2
+ import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
3
+ import { insertTypeAheadItem } from '../../commands/insert-type-ahead-item';
4
+ export var useOnForceSelect = function useOnForceSelect(_ref) {
5
+ var triggerHandler = _ref.triggerHandler,
6
+ items = _ref.items,
7
+ query = _ref.query,
8
+ editorView = _ref.editorView,
9
+ closePopup = _ref.closePopup;
10
+ var editorViewRef = useRef(editorView);
11
+ useLayoutEffect(function () {
12
+ if (!query || typeof triggerHandler.forceSelect !== 'function') {
13
+ return;
14
+ }
15
+ var item = triggerHandler.forceSelect({
16
+ items: items,
17
+ query: query,
18
+ editorState: editorViewRef.current.state
19
+ });
20
+ if (!item) {
21
+ return;
22
+ }
23
+ var view = editorViewRef.current;
24
+ closePopup();
25
+ queueMicrotask(function () {
26
+ insertTypeAheadItem(view)({
27
+ item: item,
28
+ mode: SelectItemMode.SPACE,
29
+ query: query,
30
+ handler: triggerHandler,
31
+ sourceListItem: items
32
+ });
33
+ });
34
+ }, [triggerHandler, closePopup, query, items]);
35
+ };
@@ -0,0 +1,124 @@
1
+ import { TypeAheadAvailableNodes } from '@atlaskit/editor-common/type-ahead';
2
+ import { updateSelectedIndex } from './commands/update-selected-index';
3
+ import { typeAheadListMessages } from './messages';
4
+ import { pluginKey as typeAheadPluginKey } from './pm-plugins/key';
5
+ import { StatsModifier } from './stats-modifier';
6
+ export var isTypeAheadHandler = function isTypeAheadHandler(handler) {
7
+ return handler && Object.values(TypeAheadAvailableNodes).includes(handler.id) && typeof handler.trigger === 'string' && typeof handler.selectItem === 'function' && typeof handler.getItems === 'function';
8
+ };
9
+
10
+ /** Is a typeahead plugin open? */
11
+ export var isTypeAheadOpen = function isTypeAheadOpen(editorState) {
12
+ var _typeAheadPluginKey$g;
13
+ return !!(typeAheadPluginKey !== null && typeAheadPluginKey !== void 0 && (_typeAheadPluginKey$g = typeAheadPluginKey.getState(editorState)) !== null && _typeAheadPluginKey$g !== void 0 && (_typeAheadPluginKey$g = _typeAheadPluginKey$g.decorationSet) !== null && _typeAheadPluginKey$g !== void 0 && _typeAheadPluginKey$g.find().length);
14
+ };
15
+ export var getPluginState = function getPluginState(editorState) {
16
+ return typeAheadPluginKey.getState(editorState);
17
+ };
18
+ export var getTypeAheadHandler = function getTypeAheadHandler(editorState) {
19
+ var _typeAheadPluginKey$g2;
20
+ return (_typeAheadPluginKey$g2 = typeAheadPluginKey.getState(editorState)) === null || _typeAheadPluginKey$g2 === void 0 ? void 0 : _typeAheadPluginKey$g2.triggerHandler;
21
+ };
22
+ export var getTypeAheadQuery = function getTypeAheadQuery(editorState) {
23
+ var _typeAheadPluginKey$g3;
24
+ return (_typeAheadPluginKey$g3 = typeAheadPluginKey.getState(editorState)) === null || _typeAheadPluginKey$g3 === void 0 ? void 0 : _typeAheadPluginKey$g3.query;
25
+ };
26
+ export var isTypeAheadAllowed = function isTypeAheadAllowed(state) {
27
+ var isOpen = isTypeAheadOpen(state);
28
+ // if the TypeAhead is open
29
+ // we should not allow it
30
+ return !isOpen;
31
+ };
32
+ export var findHandler = function findHandler(id, state) {
33
+ var pluginState = typeAheadPluginKey.getState(state);
34
+ if (!pluginState || !pluginState.typeAheadHandlers || pluginState.typeAheadHandlers.length === 0) {
35
+ return null;
36
+ }
37
+ var typeAheadHandlers = pluginState.typeAheadHandlers;
38
+ return typeAheadHandlers.find(function (h) {
39
+ return h.id === id;
40
+ }) || null;
41
+ };
42
+ export var findHandlerByTrigger = function findHandlerByTrigger(_ref) {
43
+ var trigger = _ref.trigger,
44
+ editorState = _ref.editorState;
45
+ var pluginState = typeAheadPluginKey.getState(editorState);
46
+ if (!pluginState || !pluginState.typeAheadHandlers || pluginState.typeAheadHandlers.length === 0) {
47
+ return null;
48
+ }
49
+ var typeAheadHandlers = pluginState.typeAheadHandlers;
50
+ return typeAheadHandlers.find(function (h) {
51
+ return h.trigger === trigger;
52
+ }) || null;
53
+ };
54
+ export var moveSelectedIndex = function moveSelectedIndex(_ref2) {
55
+ var editorView = _ref2.editorView,
56
+ direction = _ref2.direction;
57
+ return function () {
58
+ var typeAheadState = getPluginState(editorView.state);
59
+ if (!typeAheadState) {
60
+ return;
61
+ }
62
+ var selectedIndex = typeAheadState.selectedIndex,
63
+ items = typeAheadState.items;
64
+ var stats = typeAheadState.stats instanceof StatsModifier ? typeAheadState.stats : new StatsModifier();
65
+ var nextIndex;
66
+ if (direction === 'next') {
67
+ stats.increaseArrowDown();
68
+
69
+ /**
70
+ * See: https://product-fabric.atlassian.net/browse/ED-17200
71
+ * `selectedIndex` is forced to -1 now to not immediately focus the typeahead
72
+ * and only do so when there is explicit logic to focus into the typeahead
73
+ * options.
74
+ *
75
+ * This check for "set index to 1 when -1"
76
+ * - is a temporary workaround to get back the previous behaviour without
77
+ * entirely reverting the a11y improvements
78
+ *
79
+ */
80
+ if (selectedIndex === -1 && items.length > 1) {
81
+ nextIndex = 1;
82
+ } else {
83
+ nextIndex = selectedIndex >= items.length - 1 ? 0 : selectedIndex + 1;
84
+ }
85
+ } else {
86
+ stats.increaseArrowUp();
87
+ nextIndex = selectedIndex <= 0 ? items.length - 1 : selectedIndex - 1;
88
+ }
89
+ updateSelectedIndex(nextIndex)(editorView.state, editorView.dispatch);
90
+ };
91
+ };
92
+ export var getTypeAheadListAriaLabels = function getTypeAheadListAriaLabels(trigger, intl, item) {
93
+ var _item$mention, _item$mention2, _item$emoji, _item$emoji2, _item$emoji3;
94
+ switch (trigger) {
95
+ case '@':
96
+ return {
97
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.mentionPopupLabel),
98
+ listItemAriaLabel: intl.formatMessage(typeAheadListMessages.metionListItemLabel, {
99
+ name: (item === null || item === void 0 || (_item$mention = item.mention) === null || _item$mention === void 0 ? void 0 : _item$mention.name) || '',
100
+ shortName: (item === null || item === void 0 || (_item$mention2 = item.mention) === null || _item$mention2 === void 0 ? void 0 : _item$mention2.mentionName) || ''
101
+ })
102
+ };
103
+ case '/':
104
+ return {
105
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.quickInsertPopupLabel),
106
+ listItemAriaLabel: intl.formatMessage(typeAheadListMessages.emojiListItemLabel, {
107
+ name: (item === null || item === void 0 ? void 0 : item.title) || '',
108
+ shortcut: (item === null || item === void 0 || (_item$emoji = item.emoji) === null || _item$emoji === void 0 ? void 0 : _item$emoji.shortName) || ''
109
+ })
110
+ };
111
+ case ':':
112
+ return {
113
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.emojiPopupLabel),
114
+ listItemAriaLabel: intl.formatMessage(typeAheadListMessages.emojiListItemLabel, {
115
+ name: (item === null || item === void 0 || (_item$emoji2 = item.emoji) === null || _item$emoji2 === void 0 ? void 0 : _item$emoji2.name) || '',
116
+ shortcut: (item === null || item === void 0 || (_item$emoji3 = item.emoji) === null || _item$emoji3 === void 0 ? void 0 : _item$emoji3.shortName) || ''
117
+ })
118
+ };
119
+ default:
120
+ return {
121
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.typeAheadPopupLabel)
122
+ };
123
+ }
124
+ };
@@ -0,0 +1,60 @@
1
+ import type { TypeAheadItem } from '@atlaskit/editor-common/provider-factory';
2
+ import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
3
+ import type { Command } from '@atlaskit/editor-common/types';
4
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
5
+ import type { TypeAheadHandler, TypeAheadInputMethod } from './types';
6
+ type CloseOptions = {
7
+ insertCurrentQueryAsRawText: boolean;
8
+ attachCommand?: Command;
9
+ };
10
+ type InsertItemProps = {
11
+ contentItem: TypeAheadItem;
12
+ query: string;
13
+ sourceListItem: TypeAheadItem[];
14
+ };
15
+ /**
16
+ * Please do not use it. Ping #help-editor and talk to the Lego team before use it.
17
+ *
18
+ * @private
19
+ * @deprecated
20
+ *
21
+ */
22
+ export declare const createTypeAheadTools: (editorView: EditorView) => {
23
+ isOpen: () => TypeAheadHandler | false;
24
+ currentQuery: () => string | undefined;
25
+ close: (options?: CloseOptions) => boolean;
26
+ openMention: (inputMethod: TypeAheadInputMethod) => boolean;
27
+ searchMention: (query?: string) => {
28
+ type: (appendValue: string) => Promise<TypeAheadItem[]> | undefined;
29
+ result: () => Promise<TypeAheadItem[] | undefined>;
30
+ close: (options?: CloseOptions) => boolean;
31
+ insert: ({ index, mode }: {
32
+ index: number;
33
+ mode?: SelectItemMode | undefined;
34
+ }) => Promise<void>;
35
+ };
36
+ openQuickInsert: (inputMethod: TypeAheadInputMethod) => boolean;
37
+ searchQuickInsert: (query?: string) => {
38
+ type: (appendValue: string) => Promise<TypeAheadItem[]> | undefined;
39
+ result: () => Promise<TypeAheadItem[] | undefined>;
40
+ close: (options?: CloseOptions) => boolean;
41
+ insert: ({ index, mode }: {
42
+ index: number;
43
+ mode?: SelectItemMode | undefined;
44
+ }) => Promise<void>;
45
+ };
46
+ openEmoji: (inputMethod: TypeAheadInputMethod) => boolean;
47
+ searchEmoji: (query?: string) => {
48
+ type: (appendValue: string) => Promise<TypeAheadItem[]> | undefined;
49
+ result: () => Promise<TypeAheadItem[] | undefined>;
50
+ close: (options?: CloseOptions) => boolean;
51
+ insert: ({ index, mode }: {
52
+ index: number;
53
+ mode?: SelectItemMode | undefined;
54
+ }) => Promise<void>;
55
+ };
56
+ insertItemMention: ({ contentItem, query, sourceListItem }: InsertItemProps) => boolean;
57
+ insertItemEmoji: ({ contentItem, query, sourceListItem }: InsertItemProps) => boolean;
58
+ insertItemQuickInsert: ({ contentItem, query, sourceListItem }: InsertItemProps) => boolean;
59
+ };
60
+ export {};
@@ -0,0 +1,12 @@
1
+ import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
2
+ import type { EditorView } from '@atlaskit/editor-prosemirror/view';
3
+ import type { TypeAheadHandler, TypeAheadItem } from '../types';
4
+ type Props = {
5
+ item: TypeAheadItem;
6
+ handler: TypeAheadHandler;
7
+ mode: SelectItemMode;
8
+ sourceListItem: Array<TypeAheadItem>;
9
+ query: string;
10
+ };
11
+ export declare const insertTypeAheadItem: (view: EditorView) => ({ item, handler, mode, query, sourceListItem }: Props) => void;
12
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { Command } from '@atlaskit/editor-common/types';
2
+ import type { TypeAheadItem } from '../types';
3
+ export declare const updateListItem: (items: Array<TypeAheadItem>) => Command;
@@ -0,0 +1,2 @@
1
+ import type { Command } from '@atlaskit/editor-common/types';
2
+ export declare const updateQuery: (query: string) => Command;
@@ -0,0 +1,2 @@
1
+ import type { Command } from '@atlaskit/editor-common/types';
2
+ export declare const updateSelectedIndex: (selectedIndex: number) => Command;
@@ -1 +1,2 @@
1
- export type { TypeAheadPlugin, TypeAheadPluginOptions, TypeAheadInputMethod, } from './types';
1
+ export { typeAheadPlugin } from './plugin';
2
+ export type { TypeAheadHandler, TypeAheadInputMethod, TypeAheadPlugin, TypeAheadPluginOptions, TypeAheadPluginState, } from './types';
@@ -0,0 +1,18 @@
1
+ import { Fragment, Node as PMNode } from '@atlaskit/editor-prosemirror/model';
2
+ import type { Transaction } from '@atlaskit/editor-prosemirror/state';
3
+ type Position = {
4
+ start: number;
5
+ end: number;
6
+ };
7
+ export declare const insertBlockNode: ({ node, tr, position, }: {
8
+ node: PMNode;
9
+ tr: Transaction;
10
+ position: Position;
11
+ }) => Transaction;
12
+ export declare const insertInlineNodeOrFragment: ({ maybeFragment, tr, position, selectInlineNode, }: {
13
+ maybeFragment: Fragment | PMNode;
14
+ tr: Transaction;
15
+ position: Position;
16
+ selectInlineNode: boolean;
17
+ }) => Transaction;
18
+ export {};
@@ -0,0 +1,72 @@
1
+ export declare const typeAheadListMessages: {
2
+ typeAheadPopupLabel: {
3
+ id: string;
4
+ defaultMessage: string;
5
+ description: string;
6
+ };
7
+ quickInsertPopupLabel: {
8
+ id: string;
9
+ defaultMessage: string;
10
+ description: string;
11
+ };
12
+ quickInsertInputLabel: {
13
+ id: string;
14
+ defaultMessage: string;
15
+ description: string;
16
+ };
17
+ emojiPopupLabel: {
18
+ id: string;
19
+ defaultMessage: string;
20
+ description: string;
21
+ };
22
+ emojiInputLabel: {
23
+ id: string;
24
+ defaultMessage: string;
25
+ description: string;
26
+ };
27
+ mentionPopupLabel: {
28
+ id: string;
29
+ defaultMessage: string;
30
+ description: string;
31
+ };
32
+ mentionInputLabel: {
33
+ id: string;
34
+ defaultMessage: string;
35
+ description: string;
36
+ };
37
+ metionListItemLabel: {
38
+ id: string;
39
+ defaultMessage: string;
40
+ description: string;
41
+ };
42
+ emojiListItemLabel: {
43
+ id: string;
44
+ defaultMessage: string;
45
+ description: string;
46
+ };
47
+ inputQueryAssistiveLabel: {
48
+ id: string;
49
+ defaultMessage: string;
50
+ description: string;
51
+ };
52
+ searchResultsLabel: {
53
+ id: string;
54
+ defaultMessage: string;
55
+ description: string;
56
+ };
57
+ noSearchResultsLabel: {
58
+ id: string;
59
+ defaultMessage: string;
60
+ description: string;
61
+ };
62
+ descriptionLabel: {
63
+ id: string;
64
+ defaultMessage: string;
65
+ description: string;
66
+ };
67
+ shortcutLabel: {
68
+ id: string;
69
+ defaultMessage: string;
70
+ description: string;
71
+ };
72
+ };
@@ -0,0 +1,10 @@
1
+ import type { TypeAheadPlugin } from './types';
2
+ /**
3
+ *
4
+ * Revamped typeahead using decorations instead of the `typeAheadQuery` mark
5
+ *
6
+ * https://product-fabric.atlassian.net/wiki/spaces/E/pages/2992177582/Technical+TypeAhead+Data+Flow
7
+ *
8
+ *
9
+ */
10
+ export declare const typeAheadPlugin: TypeAheadPlugin;
@@ -0,0 +1,9 @@
1
+ export declare enum ACTIONS {
2
+ OPEN_TYPEAHEAD_AT_CURSOR = "OPEN_TYPEAHEAD_AT_CURSOR",
3
+ CLOSE_TYPE_AHEAD = "CLOSE_TYPE_AHEAD",
4
+ CHANGE_QUERY = "CHANGE_QUERY",
5
+ INSERT_ITEM = "INSERT_ITEM",
6
+ INSERT_RAW_QUERY = "INSERT_RAW_QUERY",
7
+ UPDATE_LIST_ITEMS = "UPDATE_LIST_ITEMS",
8
+ UPDATE_SELECTED_INDEX = "UPDATE_SELECTED_INDEX"
9
+ }
@@ -0,0 +1,14 @@
1
+ import type { IntlShape } from 'react-intl-next';
2
+ import type { CreateUIAnalyticsEvent } from '@atlaskit/analytics-next';
3
+ import type { CreateTypeAheadDecorations, PopupMountPointReference, RemoveTypeAheadDecorations } from '../types';
4
+ type FactoryProps = {
5
+ intl: IntlShape;
6
+ popupMountRef: PopupMountPointReference;
7
+ createAnalyticsEvent?: CreateUIAnalyticsEvent;
8
+ };
9
+ type FactoryReturn = {
10
+ createDecorations: CreateTypeAheadDecorations;
11
+ removeDecorations: RemoveTypeAheadDecorations;
12
+ };
13
+ export declare const factoryDecorations: ({ intl, popupMountRef, createAnalyticsEvent, }: FactoryProps) => FactoryReturn;
14
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ import type { FeatureFlags } from '@atlaskit/editor-common/types';
3
+ import type { Schema } from '@atlaskit/editor-prosemirror/model';
4
+ import type { TypeAheadHandler } from '../types';
5
+ export declare function inputRulePlugin(schema: Schema, typeAheads: TypeAheadHandler[], featureFlags: FeatureFlags): SafePlugin | undefined;
6
+ export default inputRulePlugin;
@@ -0,0 +1,2 @@
1
+ import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
2
+ export declare function createPlugin(): SafePlugin;
@@ -0,0 +1,3 @@
1
+ import { PluginKey } from '@atlaskit/editor-prosemirror/state';
2
+ import type { TypeAheadPluginState } from '../types';
3
+ export declare const pluginKey: PluginKey<TypeAheadPluginState>;