@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,38 @@
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 const useOnForceSelect = ({
5
+ triggerHandler,
6
+ items,
7
+ query,
8
+ editorView,
9
+ closePopup
10
+ }) => {
11
+ const editorViewRef = useRef(editorView);
12
+ useLayoutEffect(() => {
13
+ if (!query || typeof triggerHandler.forceSelect !== 'function') {
14
+ return;
15
+ }
16
+ const item = triggerHandler.forceSelect({
17
+ items,
18
+ query,
19
+ editorState: editorViewRef.current.state
20
+ });
21
+ if (!item) {
22
+ return;
23
+ }
24
+ const {
25
+ current: view
26
+ } = editorViewRef;
27
+ closePopup();
28
+ queueMicrotask(() => {
29
+ insertTypeAheadItem(view)({
30
+ item,
31
+ mode: SelectItemMode.SPACE,
32
+ query,
33
+ handler: triggerHandler,
34
+ sourceListItem: items
35
+ });
36
+ });
37
+ }, [triggerHandler, closePopup, query, items]);
38
+ };
@@ -0,0 +1,126 @@
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 const 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 const isTypeAheadOpen = editorState => {
12
+ var _typeAheadPluginKey$g, _typeAheadPluginKey$g2;
13
+ return !!(typeAheadPluginKey !== null && typeAheadPluginKey !== void 0 && (_typeAheadPluginKey$g = typeAheadPluginKey.getState(editorState)) !== null && _typeAheadPluginKey$g !== void 0 && (_typeAheadPluginKey$g2 = _typeAheadPluginKey$g.decorationSet) !== null && _typeAheadPluginKey$g2 !== void 0 && _typeAheadPluginKey$g2.find().length);
14
+ };
15
+ export const getPluginState = editorState => {
16
+ return typeAheadPluginKey.getState(editorState);
17
+ };
18
+ export const getTypeAheadHandler = editorState => {
19
+ var _typeAheadPluginKey$g3;
20
+ return (_typeAheadPluginKey$g3 = typeAheadPluginKey.getState(editorState)) === null || _typeAheadPluginKey$g3 === void 0 ? void 0 : _typeAheadPluginKey$g3.triggerHandler;
21
+ };
22
+ export const getTypeAheadQuery = editorState => {
23
+ var _typeAheadPluginKey$g4;
24
+ return (_typeAheadPluginKey$g4 = typeAheadPluginKey.getState(editorState)) === null || _typeAheadPluginKey$g4 === void 0 ? void 0 : _typeAheadPluginKey$g4.query;
25
+ };
26
+ export const isTypeAheadAllowed = state => {
27
+ const isOpen = isTypeAheadOpen(state);
28
+ // if the TypeAhead is open
29
+ // we should not allow it
30
+ return !isOpen;
31
+ };
32
+ export const findHandler = (id, state) => {
33
+ const pluginState = typeAheadPluginKey.getState(state);
34
+ if (!pluginState || !pluginState.typeAheadHandlers || pluginState.typeAheadHandlers.length === 0) {
35
+ return null;
36
+ }
37
+ const {
38
+ typeAheadHandlers
39
+ } = pluginState;
40
+ return typeAheadHandlers.find(h => h.id === id) || null;
41
+ };
42
+ export const findHandlerByTrigger = ({
43
+ trigger,
44
+ editorState
45
+ }) => {
46
+ const pluginState = typeAheadPluginKey.getState(editorState);
47
+ if (!pluginState || !pluginState.typeAheadHandlers || pluginState.typeAheadHandlers.length === 0) {
48
+ return null;
49
+ }
50
+ const {
51
+ typeAheadHandlers
52
+ } = pluginState;
53
+ return typeAheadHandlers.find(h => h.trigger === trigger) || null;
54
+ };
55
+ export const moveSelectedIndex = ({
56
+ editorView,
57
+ direction
58
+ }) => () => {
59
+ const typeAheadState = getPluginState(editorView.state);
60
+ if (!typeAheadState) {
61
+ return;
62
+ }
63
+ const {
64
+ selectedIndex,
65
+ items
66
+ } = typeAheadState;
67
+ const stats = typeAheadState.stats instanceof StatsModifier ? typeAheadState.stats : new StatsModifier();
68
+ let nextIndex;
69
+ if (direction === 'next') {
70
+ stats.increaseArrowDown();
71
+
72
+ /**
73
+ * See: https://product-fabric.atlassian.net/browse/ED-17200
74
+ * `selectedIndex` is forced to -1 now to not immediately focus the typeahead
75
+ * and only do so when there is explicit logic to focus into the typeahead
76
+ * options.
77
+ *
78
+ * This check for "set index to 1 when -1"
79
+ * - is a temporary workaround to get back the previous behaviour without
80
+ * entirely reverting the a11y improvements
81
+ *
82
+ */
83
+ if (selectedIndex === -1 && items.length > 1) {
84
+ nextIndex = 1;
85
+ } else {
86
+ nextIndex = selectedIndex >= items.length - 1 ? 0 : selectedIndex + 1;
87
+ }
88
+ } else {
89
+ stats.increaseArrowUp();
90
+ nextIndex = selectedIndex <= 0 ? items.length - 1 : selectedIndex - 1;
91
+ }
92
+ updateSelectedIndex(nextIndex)(editorView.state, editorView.dispatch);
93
+ };
94
+ export const getTypeAheadListAriaLabels = (trigger, intl, item) => {
95
+ var _item$mention, _item$mention2, _item$emoji, _item$emoji2, _item$emoji3;
96
+ switch (trigger) {
97
+ case '@':
98
+ return {
99
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.mentionPopupLabel),
100
+ listItemAriaLabel: intl.formatMessage(typeAheadListMessages.metionListItemLabel, {
101
+ name: (item === null || item === void 0 ? void 0 : (_item$mention = item.mention) === null || _item$mention === void 0 ? void 0 : _item$mention.name) || '',
102
+ shortName: (item === null || item === void 0 ? void 0 : (_item$mention2 = item.mention) === null || _item$mention2 === void 0 ? void 0 : _item$mention2.mentionName) || ''
103
+ })
104
+ };
105
+ case '/':
106
+ return {
107
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.quickInsertPopupLabel),
108
+ listItemAriaLabel: intl.formatMessage(typeAheadListMessages.emojiListItemLabel, {
109
+ name: (item === null || item === void 0 ? void 0 : item.title) || '',
110
+ shortcut: (item === null || item === void 0 ? void 0 : (_item$emoji = item.emoji) === null || _item$emoji === void 0 ? void 0 : _item$emoji.shortName) || ''
111
+ })
112
+ };
113
+ case ':':
114
+ return {
115
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.emojiPopupLabel),
116
+ listItemAriaLabel: intl.formatMessage(typeAheadListMessages.emojiListItemLabel, {
117
+ name: (item === null || item === void 0 ? void 0 : (_item$emoji2 = item.emoji) === null || _item$emoji2 === void 0 ? void 0 : _item$emoji2.name) || '',
118
+ shortcut: (item === null || item === void 0 ? void 0 : (_item$emoji3 = item.emoji) === null || _item$emoji3 === void 0 ? void 0 : _item$emoji3.shortName) || ''
119
+ })
120
+ };
121
+ default:
122
+ return {
123
+ popupAriaLabel: intl.formatMessage(typeAheadListMessages.typeAheadPopupLabel)
124
+ };
125
+ }
126
+ };
@@ -0,0 +1,209 @@
1
+ import { INPUT_METHOD } from '@atlaskit/editor-common/analytics';
2
+ import { SelectItemMode, TypeAheadAvailableNodes } from '@atlaskit/editor-common/type-ahead';
3
+ import { insertTypeAheadItem } from './commands/insert-type-ahead-item';
4
+ import { updateQuery } from './commands/update-query';
5
+ import { closeTypeAhead } from './transforms/close-type-ahead';
6
+ import { openTypeAheadAtCursor } from './transforms/open-typeahead-at-cursor';
7
+ import { findHandler, getTypeAheadHandler, getTypeAheadQuery, isTypeAheadOpen } from './utils';
8
+ var open = function open(_ref) {
9
+ var editorView = _ref.editorView;
10
+ return function (itemType) {
11
+ return function (inputMethod) {
12
+ var state = editorView.state;
13
+ var handler = findHandler(itemType, state);
14
+ if (!handler) {
15
+ return false;
16
+ }
17
+ var tr = state.tr;
18
+ openTypeAheadAtCursor({
19
+ triggerHandler: handler,
20
+ inputMethod: inputMethod
21
+ })({
22
+ tr: tr
23
+ });
24
+ editorView.dispatch(tr);
25
+ return true;
26
+ };
27
+ };
28
+ };
29
+ var defaultCloseOptions = {
30
+ insertCurrentQueryAsRawText: false
31
+ };
32
+ var close = function close(_ref2) {
33
+ var editorView = _ref2.editorView;
34
+ return function () {
35
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultCloseOptions;
36
+ var state = editorView.state;
37
+ var currentQuery = getTypeAheadQuery(editorView.state);
38
+ var tr = state.tr;
39
+ if (options.attachCommand) {
40
+ var fakeDispatch = function fakeDispatch(customTr) {
41
+ tr = customTr;
42
+ };
43
+ options.attachCommand(state, fakeDispatch);
44
+ }
45
+ closeTypeAhead(tr);
46
+ if (options.insertCurrentQueryAsRawText && currentQuery && currentQuery.length > 0) {
47
+ var handler = getTypeAheadHandler(state);
48
+ var text = handler.trigger.concat(currentQuery);
49
+ tr.replaceSelectionWith(state.schema.text(text));
50
+ }
51
+ editorView.dispatch(tr);
52
+ if (!editorView.hasFocus()) {
53
+ editorView.focus();
54
+ }
55
+ return true;
56
+ };
57
+ };
58
+ var search = function search(_ref3) {
59
+ var editorView = _ref3.editorView;
60
+ return function (itemType) {
61
+ return function () {
62
+ var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
63
+ var state = editorView.state;
64
+ var handler = findHandler(itemType, state);
65
+ if (!handler) {
66
+ throw new Error("Handler not found, did you load the ".concat(itemType, " plugin properly"));
67
+ }
68
+ open({
69
+ editorView: editorView
70
+ })(itemType)(INPUT_METHOD.KEYBOARD);
71
+ updateQuery(query)(editorView.state, editorView.dispatch);
72
+ var lastQuery = {
73
+ current: query
74
+ };
75
+ var last = handler.getItems({
76
+ query: query,
77
+ editorState: state
78
+ }).then(function (items) {
79
+ if (!handler.forceSelect) {
80
+ return items;
81
+ }
82
+ var forceSelectedItem = handler.forceSelect({
83
+ items: items,
84
+ query: query,
85
+ editorState: state
86
+ });
87
+ if (!forceSelectedItem) {
88
+ return items;
89
+ }
90
+ insertTypeAheadItem(editorView)({
91
+ handler: handler,
92
+ item: forceSelectedItem,
93
+ query: query,
94
+ mode: SelectItemMode.SELECTED,
95
+ sourceListItem: items
96
+ });
97
+ });
98
+ var results = {
99
+ last: last
100
+ };
101
+ return {
102
+ type: function type(appendValue) {
103
+ if (!appendValue) {
104
+ return;
105
+ }
106
+ lastQuery.current += appendValue;
107
+ updateQuery(lastQuery.current)(editorView.state, editorView.dispatch);
108
+ var promise = handler.getItems({
109
+ query: lastQuery.current,
110
+ editorState: state
111
+ });
112
+ results.last = promise;
113
+ return promise;
114
+ },
115
+ result: function result() {
116
+ return results.last;
117
+ },
118
+ close: close({
119
+ editorView: editorView
120
+ }),
121
+ insert: function insert(_ref4) {
122
+ var index = _ref4.index,
123
+ mode = _ref4.mode;
124
+ return results.last.then(function (result) {
125
+ var item = result ? result[index] : null;
126
+ if (result && item) {
127
+ insertTypeAheadItem(editorView)({
128
+ handler: handler,
129
+ item: item,
130
+ query: query,
131
+ mode: mode || SelectItemMode.SELECTED,
132
+ sourceListItem: result
133
+ });
134
+ }
135
+ });
136
+ }
137
+ };
138
+ };
139
+ };
140
+ };
141
+ var insertItem = function insertItem(_ref5) {
142
+ var editorView = _ref5.editorView;
143
+ return function (itemType) {
144
+ return function (_ref6) {
145
+ var contentItem = _ref6.contentItem,
146
+ query = _ref6.query,
147
+ sourceListItem = _ref6.sourceListItem;
148
+ var state = editorView.state;
149
+ var handler = findHandler(itemType, state);
150
+ if (!handler) {
151
+ return false;
152
+ }
153
+ insertTypeAheadItem(editorView)({
154
+ handler: handler,
155
+ item: contentItem,
156
+ mode: SelectItemMode.SELECTED,
157
+ query: query,
158
+ sourceListItem: sourceListItem
159
+ });
160
+ return true;
161
+ };
162
+ };
163
+ };
164
+ var isOpen = function isOpen(_ref7) {
165
+ var editorView = _ref7.editorView;
166
+ return function () {
167
+ if (!isTypeAheadOpen(editorView.state)) {
168
+ return false;
169
+ }
170
+ var handler = getTypeAheadHandler(editorView.state);
171
+ if (!handler) {
172
+ return false;
173
+ }
174
+ return handler;
175
+ };
176
+ };
177
+ var currentQuery = function currentQuery(_ref8) {
178
+ var editorView = _ref8.editorView;
179
+ return function () {
180
+ return getTypeAheadQuery(editorView.state);
181
+ };
182
+ };
183
+
184
+ /**
185
+ * Please do not use it. Ping #help-editor and talk to the Lego team before use it.
186
+ *
187
+ * @private
188
+ * @deprecated
189
+ *
190
+ */
191
+ export var createTypeAheadTools = function createTypeAheadTools(editorView) {
192
+ var props = {
193
+ editorView: editorView
194
+ };
195
+ return {
196
+ isOpen: isOpen(props),
197
+ currentQuery: currentQuery(props),
198
+ close: close(props),
199
+ openMention: open(props)(TypeAheadAvailableNodes.MENTION),
200
+ searchMention: search(props)(TypeAheadAvailableNodes.MENTION),
201
+ openQuickInsert: open(props)(TypeAheadAvailableNodes.QUICK_INSERT),
202
+ searchQuickInsert: search(props)(TypeAheadAvailableNodes.QUICK_INSERT),
203
+ openEmoji: open(props)(TypeAheadAvailableNodes.EMOJI),
204
+ searchEmoji: search(props)(TypeAheadAvailableNodes.EMOJI),
205
+ insertItemMention: insertItem(props)(TypeAheadAvailableNodes.MENTION),
206
+ insertItemEmoji: insertItem(props)(TypeAheadAvailableNodes.EMOJI),
207
+ insertItemQuickInsert: insertItem(props)(TypeAheadAvailableNodes.QUICK_INSERT)
208
+ };
209
+ };
@@ -0,0 +1,198 @@
1
+ import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2
+ import { InsertTypeAheadStages, InsertTypeAheadStep } from '@atlaskit/adf-schema/steps';
3
+ import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
4
+ import { closeHistory } from '@atlaskit/editor-prosemirror/history';
5
+ import { Fragment, Node as PMNode } from '@atlaskit/editor-prosemirror/model';
6
+ import { insertBlockNode, insertInlineNodeOrFragment } from '../insert-utils';
7
+ import { ACTIONS } from '../pm-plugins/actions';
8
+ import { pluginKey } from '../pm-plugins/key';
9
+ import { StatsModifier } from '../stats-modifier';
10
+ import { closeTypeAhead } from '../transforms/close-type-ahead';
11
+ import { getPluginState } from '../utils';
12
+ var validateNode = function validateNode(_ref) {
13
+ var schema = _ref.schema,
14
+ maybeNode = _ref.maybeNode;
15
+ if (!maybeNode) {
16
+ return null;
17
+ }
18
+ if (maybeNode instanceof PMNode || maybeNode instanceof Fragment) {
19
+ return maybeNode;
20
+ }
21
+ if (typeof maybeNode === 'string') {
22
+ return schema.text(maybeNode);
23
+ }
24
+ try {
25
+ return PMNode.fromJSON(schema, maybeNode);
26
+ } catch (e) {
27
+ // eslint-disable-next-line no-console
28
+ console.error(e);
29
+ return null;
30
+ }
31
+ };
32
+ var createInsertCallback = function createInsertCallback(_ref2) {
33
+ var state = _ref2.editorState,
34
+ handler = _ref2.handler,
35
+ query = _ref2.query,
36
+ wasInsertedBySpace = _ref2.wasInsertedBySpace,
37
+ selectedIndex = _ref2.selectedIndex,
38
+ textStartPosition = _ref2.textStartPosition,
39
+ textInserted = _ref2.textInserted;
40
+ return function (maybeNode) {
41
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
42
+ var tr = state.tr;
43
+ var position = {
44
+ start: textStartPosition,
45
+ end: textStartPosition + (wasInsertedBySpace ? textInserted.length : 0)
46
+ };
47
+ var node = validateNode({
48
+ schema: state.schema,
49
+ maybeNode: maybeNode
50
+ });
51
+ if (!node) {
52
+ closeTypeAhead(tr);
53
+ // In this kind of situation we need to
54
+ // delete the raw text query
55
+ tr.delete(position.start, position.end);
56
+ return tr;
57
+ }
58
+ node instanceof PMNode && node.isBlock ? insertBlockNode({
59
+ node: node,
60
+ tr: tr,
61
+ position: position
62
+ }) : insertInlineNodeOrFragment({
63
+ maybeFragment: node,
64
+ tr: tr,
65
+ position: position,
66
+ selectInlineNode: Boolean(opts.selectInlineNode)
67
+ });
68
+ closeHistory(tr);
69
+ if (wasInsertedBySpace) {
70
+ return tr;
71
+ }
72
+ var config = {
73
+ stage: InsertTypeAheadStages.INSERTING_ITEM,
74
+ query: query,
75
+ selectedIndex: selectedIndex,
76
+ trigger: handler.trigger
77
+ };
78
+ tr.step(new InsertTypeAheadStep(config));
79
+ return tr;
80
+ };
81
+ };
82
+ var createDeleteRawTextCallback = function createDeleteRawTextCallback(_ref3) {
83
+ var trigger = _ref3.trigger,
84
+ selectedIndex = _ref3.selectedIndex,
85
+ position = _ref3.position,
86
+ query = _ref3.query,
87
+ wasInsertedBySpace = _ref3.wasInsertedBySpace,
88
+ insertItem = _ref3.insertItem;
89
+ return function (newState) {
90
+ var tr = newState.tr;
91
+ closeHistory(tr);
92
+ if (!wasInsertedBySpace) {
93
+ tr.delete(position.start, position.end);
94
+ var config = {
95
+ stage: InsertTypeAheadStages.DELETING_RAW_QUERY,
96
+ selectedIndex: selectedIndex,
97
+ query: query,
98
+ trigger: trigger
99
+ };
100
+ tr.step(new InsertTypeAheadStep(config));
101
+ }
102
+ tr.setMeta(pluginKey, {
103
+ action: ACTIONS.INSERT_ITEM,
104
+ params: insertItem
105
+ });
106
+ return tr;
107
+ };
108
+ };
109
+ export var insertTypeAheadItem = function insertTypeAheadItem(view) {
110
+ return function (_ref4) {
111
+ var item = _ref4.item,
112
+ handler = _ref4.handler,
113
+ mode = _ref4.mode,
114
+ query = _ref4.query,
115
+ sourceListItem = _ref4.sourceListItem;
116
+ var pluginState = getPluginState(view.state);
117
+ if (!pluginState) {
118
+ return;
119
+ }
120
+ var stats = (pluginState.stats || new StatsModifier()).serialize();
121
+ var meta = {
122
+ mode: mode,
123
+ query: query,
124
+ stats: stats,
125
+ sourceListItem: sourceListItem
126
+ };
127
+ var tr = view.state.tr;
128
+ var trigger = handler.trigger;
129
+ var text = "".concat(trigger).concat(query);
130
+ if (mode === SelectItemMode.SPACE) {
131
+ text = text.trim().concat(' ');
132
+ }
133
+
134
+ // We want to always persist selectedIndex as -1 upon selecting an item from typeahead
135
+ // in order to retain exist focus behaviour when undoing
136
+ var selectedIndex = -1;
137
+ var wasInsertedBySpace = mode === SelectItemMode.SPACE;
138
+ var textStartPosition = tr.selection.from;
139
+ var insertItem = function insertItem(newEditorSate) {
140
+ var insertCallback = createInsertCallback({
141
+ editorState: newEditorSate,
142
+ query: query,
143
+ mode: mode,
144
+ handler: handler,
145
+ wasInsertedBySpace: wasInsertedBySpace,
146
+ selectedIndex: selectedIndex,
147
+ textInserted: text,
148
+ textStartPosition: textStartPosition
149
+ });
150
+ var wasInsertCallbackCalled = false;
151
+ // Some wierd plugins doesn't call the insert item callback
152
+ // For example, the link quick insert item
153
+ // For those cases we need to make sure we are closing the typeahead
154
+ var proxyHandler = {
155
+ apply: function apply(target, _thisContext, argumentsList) {
156
+ wasInsertCallbackCalled = true;
157
+ return target.apply(void 0, _toConsumableArray(argumentsList));
158
+ }
159
+ };
160
+ var insertCallbackProxy = new Proxy(insertCallback, proxyHandler);
161
+ var nextTr = handler.selectItem(newEditorSate, item, insertCallbackProxy, meta);
162
+ if (!wasInsertCallbackCalled && nextTr) {
163
+ closeHistory(nextTr);
164
+
165
+ // In some cases we need to re-open the typeahead
166
+ // e.g.: addign mentions from the quick insert
167
+ //
168
+ // Today, the QuickInsert API doesn't have a tool
169
+ // to help on this. So the code below will close the typeahead
170
+ // only if there is no previous metadata about typeahead in the
171
+ // next transaction
172
+ if (!nextTr.getMeta(pluginKey)) {
173
+ closeTypeAhead(nextTr);
174
+ }
175
+ }
176
+ return nextTr;
177
+ };
178
+ var position = {
179
+ start: tr.selection.from,
180
+ end: tr.selection.from + text.length
181
+ };
182
+ tr.setMeta(pluginKey, {
183
+ action: ACTIONS.INSERT_RAW_QUERY,
184
+ params: createDeleteRawTextCallback({
185
+ wasInsertedBySpace: wasInsertedBySpace,
186
+ selectedIndex: selectedIndex,
187
+ insertItem: insertItem,
188
+ position: position,
189
+ query: query,
190
+ trigger: trigger
191
+ })
192
+ });
193
+ tr.insertText(text);
194
+ closeHistory(tr);
195
+ view.dispatch(tr);
196
+ view.focus();
197
+ };
198
+ };
@@ -0,0 +1,17 @@
1
+ import { ACTIONS } from '../pm-plugins/actions';
2
+ import { pluginKey as typeAheadPluginKey } from '../pm-plugins/key';
3
+ export var updateListItem = function updateListItem(items) {
4
+ return function (state, dispatch) {
5
+ var tr = state.tr;
6
+ tr.setMeta(typeAheadPluginKey, {
7
+ action: ACTIONS.UPDATE_LIST_ITEMS,
8
+ params: {
9
+ items: items
10
+ }
11
+ });
12
+ if (dispatch) {
13
+ dispatch(tr);
14
+ }
15
+ return true;
16
+ };
17
+ };
@@ -0,0 +1,21 @@
1
+ import { ACTIONS } from '../pm-plugins/actions';
2
+ import { pluginKey as typeAheadPluginKey } from '../pm-plugins/key';
3
+ export var updateQuery = function updateQuery(query) {
4
+ return function (state, dispatch) {
5
+ var pluginState = typeAheadPluginKey.getState(state);
6
+ if ((pluginState === null || pluginState === void 0 ? void 0 : pluginState.query) === query) {
7
+ return false;
8
+ }
9
+ var tr = state.tr;
10
+ tr.setMeta(typeAheadPluginKey, {
11
+ action: ACTIONS.CHANGE_QUERY,
12
+ params: {
13
+ query: query
14
+ }
15
+ });
16
+ if (dispatch) {
17
+ dispatch(tr);
18
+ }
19
+ return true;
20
+ };
21
+ };
@@ -0,0 +1,21 @@
1
+ import { ACTIONS } from '../pm-plugins/actions';
2
+ import { pluginKey as typeAheadPluginKey } from '../pm-plugins/key';
3
+ export var updateSelectedIndex = function updateSelectedIndex(selectedIndex) {
4
+ return function (state, dispatch) {
5
+ var pluginState = typeAheadPluginKey.getState(state);
6
+ if ((pluginState === null || pluginState === void 0 ? void 0 : pluginState.selectedIndex) === selectedIndex) {
7
+ return false;
8
+ }
9
+ var tr = state.tr;
10
+ tr.setMeta(typeAheadPluginKey, {
11
+ action: ACTIONS.UPDATE_SELECTED_INDEX,
12
+ params: {
13
+ selectedIndex: selectedIndex
14
+ }
15
+ });
16
+ if (dispatch) {
17
+ dispatch(tr);
18
+ }
19
+ return true;
20
+ };
21
+ };
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- export {};
1
+ export { typeAheadPlugin } from './plugin';