@atlaskit/editor-core 188.7.5 → 188.8.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 +16 -0
  2. package/dist/cjs/plugins/date/actions.js +8 -114
  3. package/dist/cjs/plugins/date/commands.js +118 -0
  4. package/dist/cjs/plugins/date/plugin.js +13 -7
  5. package/dist/cjs/plugins/insert-block/ui/ToolbarInsertBlock/index.js +34 -33
  6. package/dist/cjs/plugins/type-ahead/index.js +4 -372
  7. package/dist/cjs/plugins/type-ahead/plugin.js +382 -0
  8. package/dist/cjs/plugins/view-update-subscription/subscribe/toolbarAndPickerUpdates.js +17 -8
  9. package/dist/cjs/version-wrapper.js +1 -1
  10. package/dist/es2019/plugins/date/actions.js +3 -109
  11. package/dist/es2019/plugins/date/commands.js +113 -0
  12. package/dist/es2019/plugins/date/plugin.js +14 -8
  13. package/dist/es2019/plugins/insert-block/ui/ToolbarInsertBlock/index.js +5 -3
  14. package/dist/es2019/plugins/type-ahead/index.js +2 -375
  15. package/dist/es2019/plugins/type-ahead/plugin.js +381 -0
  16. package/dist/es2019/plugins/view-update-subscription/subscribe/toolbarAndPickerUpdates.js +17 -8
  17. package/dist/es2019/version-wrapper.js +1 -1
  18. package/dist/esm/plugins/date/actions.js +8 -114
  19. package/dist/esm/plugins/date/commands.js +112 -0
  20. package/dist/esm/plugins/date/plugin.js +14 -8
  21. package/dist/esm/plugins/insert-block/ui/ToolbarInsertBlock/index.js +34 -33
  22. package/dist/esm/plugins/type-ahead/index.js +2 -368
  23. package/dist/esm/plugins/type-ahead/plugin.js +374 -0
  24. package/dist/esm/plugins/view-update-subscription/subscribe/toolbarAndPickerUpdates.js +17 -8
  25. package/dist/esm/version-wrapper.js +1 -1
  26. package/dist/types/labs/next/presets/default.d.ts +30 -12
  27. package/dist/types/plugins/date/actions.d.ts +4 -10
  28. package/dist/types/plugins/date/commands.d.ts +20 -0
  29. package/dist/types/plugins/date/types.d.ts +4 -4
  30. package/dist/types/plugins/type-ahead/index.d.ts +3 -14
  31. package/dist/types/plugins/type-ahead/plugin.d.ts +10 -0
  32. package/dist/types/plugins/type-ahead/types.d.ts +51 -6
  33. package/dist/types-ts4.5/labs/next/presets/default.d.ts +30 -12
  34. package/dist/types-ts4.5/plugins/date/actions.d.ts +4 -10
  35. package/dist/types-ts4.5/plugins/date/commands.d.ts +22 -0
  36. package/dist/types-ts4.5/plugins/date/types.d.ts +4 -4
  37. package/dist/types-ts4.5/plugins/type-ahead/index.d.ts +3 -14
  38. package/dist/types-ts4.5/plugins/type-ahead/plugin.d.ts +10 -0
  39. package/dist/types-ts4.5/plugins/type-ahead/types.d.ts +53 -6
  40. package/package.json +2 -2
  41. package/dist/cjs/plugins/view-update-subscription/subscribe/type-ahead-updates.js +0 -33
  42. package/dist/es2019/plugins/view-update-subscription/subscribe/type-ahead-updates.js +0 -28
  43. package/dist/esm/plugins/view-update-subscription/subscribe/type-ahead-updates.js +0 -27
  44. package/dist/types/plugins/view-update-subscription/subscribe/type-ahead-updates.d.ts +0 -9
  45. package/dist/types-ts4.5/plugins/view-update-subscription/subscribe/type-ahead-updates.d.ts +0 -9
@@ -0,0 +1,112 @@
1
+ import { Fragment } from '@atlaskit/editor-prosemirror/model';
2
+ import { NodeSelection, Selection } from '@atlaskit/editor-prosemirror/state';
3
+ import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE } from '@atlaskit/editor-common/analytics';
4
+ import { canInsert } from '@atlaskit/editor-prosemirror/utils';
5
+ import { todayTimestampInUTC } from '@atlaskit/editor-common/utils';
6
+ import { pluginKey } from './pm-plugins/plugin-key';
7
+ import { isToday } from './utils/internal';
8
+ /** Delete the date and close the datepicker */
9
+ export var deleteDateCommand = function deleteDateCommand(pluginInjectionApi) {
10
+ return function (_ref) {
11
+ var _pluginInjectionApi$d, _pluginInjectionApi$d2;
12
+ var tr = _ref.tr;
13
+ var _ref2 = (_pluginInjectionApi$d = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$d2 = pluginInjectionApi.date) === null || _pluginInjectionApi$d2 === void 0 ? void 0 : _pluginInjectionApi$d2.sharedState.currentState()) !== null && _pluginInjectionApi$d !== void 0 ? _pluginInjectionApi$d : {},
14
+ showDatePickerAt = _ref2.showDatePickerAt;
15
+ if (!showDatePickerAt) {
16
+ return tr;
17
+ }
18
+ tr.delete(showDatePickerAt, showDatePickerAt + 1).setMeta(pluginKey, {
19
+ showDatePickerAt: null,
20
+ isNew: false
21
+ });
22
+ return tr;
23
+ };
24
+ };
25
+ export var insertDateCommand = function insertDateCommand(pluginInjectionApi) {
26
+ return function (_ref3) {
27
+ var date = _ref3.date,
28
+ inputMethod = _ref3.inputMethod,
29
+ commitMethod = _ref3.commitMethod,
30
+ _ref3$enterPressed = _ref3.enterPressed,
31
+ enterPressed = _ref3$enterPressed === void 0 ? true : _ref3$enterPressed;
32
+ return function (_ref4) {
33
+ var _pluginInjectionApi$d3, _pluginInjectionApi$d4;
34
+ var tr = _ref4.tr;
35
+ var schema = tr.doc.type.schema;
36
+ var timestamp;
37
+ if (date) {
38
+ timestamp = Date.UTC(date.year, date.month - 1, date.day).toString();
39
+ } else {
40
+ timestamp = todayTimestampInUTC();
41
+ }
42
+ if (inputMethod) {
43
+ var _pluginInjectionApi$a;
44
+ pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a === void 0 || (_pluginInjectionApi$a = _pluginInjectionApi$a.actions) === null || _pluginInjectionApi$a === void 0 || _pluginInjectionApi$a.attachAnalyticsEvent({
45
+ action: ACTION.INSERTED,
46
+ actionSubject: ACTION_SUBJECT.DOCUMENT,
47
+ actionSubjectId: ACTION_SUBJECT_ID.DATE,
48
+ eventType: EVENT_TYPE.TRACK,
49
+ attributes: {
50
+ inputMethod: inputMethod
51
+ }
52
+ })(tr);
53
+ }
54
+ if (commitMethod) {
55
+ var _pluginInjectionApi$a2;
56
+ pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a2 = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a2 === void 0 || (_pluginInjectionApi$a2 = _pluginInjectionApi$a2.actions) === null || _pluginInjectionApi$a2 === void 0 || _pluginInjectionApi$a2.attachAnalyticsEvent({
57
+ eventType: EVENT_TYPE.TRACK,
58
+ action: ACTION.COMMITTED,
59
+ actionSubject: ACTION_SUBJECT.DATE,
60
+ attributes: {
61
+ commitMethod: commitMethod,
62
+ isValid: date !== undefined,
63
+ isToday: isToday(date)
64
+ }
65
+ })(tr);
66
+ }
67
+ var _ref5 = (_pluginInjectionApi$d3 = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$d4 = pluginInjectionApi.date) === null || _pluginInjectionApi$d4 === void 0 ? void 0 : _pluginInjectionApi$d4.sharedState.currentState()) !== null && _pluginInjectionApi$d3 !== void 0 ? _pluginInjectionApi$d3 : {},
68
+ showDatePickerAt = _ref5.showDatePickerAt;
69
+ if (!showDatePickerAt) {
70
+ var dateNode = schema.nodes.date.createChecked({
71
+ timestamp: timestamp
72
+ });
73
+ var textNode = schema.text(' ');
74
+ var fragment = Fragment.fromArray([dateNode, textNode]);
75
+ var _tr$selection = tr.selection,
76
+ from = _tr$selection.from,
77
+ to = _tr$selection.to;
78
+ var insertable = canInsert(tr.selection.$from, fragment);
79
+ if (!insertable) {
80
+ var parentSelection = NodeSelection.create(tr.doc, tr.selection.from - tr.selection.$anchor.parentOffset - 1);
81
+ tr.insert(parentSelection.to, fragment).setSelection(NodeSelection.create(tr.doc, parentSelection.to + 1));
82
+ } else {
83
+ tr.replaceWith(from, to, fragment).setSelection(NodeSelection.create(tr.doc, from));
84
+ }
85
+ if (tr.docChanged) {
86
+ tr.scrollIntoView().setMeta(pluginKey, {
87
+ isNew: true
88
+ });
89
+ }
90
+ return tr;
91
+ }
92
+ if (tr.doc.nodeAt(showDatePickerAt)) {
93
+ if (enterPressed) {
94
+ // Setting selection to outside the date node causes showDatePickerAt
95
+ // to be set to null by the PM plugin (onSelectionChanged), which will
96
+ // immediately close the datepicker once a valid date is typed in.
97
+ // Adding this check forces it to stay open until the user presses Enter.
98
+ tr = tr.setSelection(Selection.near(tr.doc.resolve(showDatePickerAt + 2)));
99
+ }
100
+ tr = tr.setNodeMarkup(showDatePickerAt, schema.nodes.date, {
101
+ timestamp: timestamp
102
+ }).setMeta(pluginKey, {
103
+ isNew: false
104
+ }).scrollIntoView();
105
+ if (!enterPressed) {
106
+ tr = tr.setSelection(NodeSelection.create(tr.doc, showDatePickerAt));
107
+ }
108
+ }
109
+ return tr;
110
+ };
111
+ };
112
+ };
@@ -2,7 +2,8 @@ import React from 'react';
2
2
  import { findDomRefAtPos } from '@atlaskit/editor-prosemirror/utils';
3
3
  import Loadable from 'react-loadable';
4
4
  import { date } from '@atlaskit/adf-schema';
5
- import { insertDate, closeDatePicker as _closeDatePicker, closeDatePickerWithAnalytics as _closeDatePickerWithAnalytics, createDate, deleteDate } from './actions';
5
+ import { closeDatePicker as _closeDatePicker, closeDatePickerWithAnalytics as _closeDatePickerWithAnalytics, createDate } from './actions';
6
+ import { insertDateCommand, deleteDateCommand } from './commands';
6
7
  import createDatePlugin from './pm-plugins/main';
7
8
  import keymap from './pm-plugins/keymap';
8
9
  import { IconDate } from '@atlaskit/editor-common/quick-insert';
@@ -49,20 +50,25 @@ function ContentComponent(_ref) {
49
50
  isNew: isNew,
50
51
  autoFocus: focusDateInput,
51
52
  onDelete: function onDelete() {
52
- deleteDate()(editorView.state, dispatch);
53
+ dependencyApi === null || dependencyApi === void 0 || dependencyApi.core.actions.execute(deleteDateCommand(dependencyApi));
53
54
  editorView.focus();
54
- return;
55
55
  },
56
56
  onSelect: function onSelect(date, commitMethod) {
57
57
  // Undefined means couldn't parse date, null means invalid (out of bounds) date
58
58
  if (date === undefined || date === null) {
59
59
  return;
60
60
  }
61
- insertDate(date, undefined, commitMethod)(editorView.state, dispatch);
61
+ dependencyApi === null || dependencyApi === void 0 || dependencyApi.core.actions.execute(insertDateCommand(dependencyApi)({
62
+ date: date,
63
+ commitMethod: commitMethod
64
+ }));
62
65
  editorView.focus();
63
66
  },
64
67
  onTextChanged: function onTextChanged(date) {
65
- insertDate(date, undefined, undefined, false)(editorView.state, dispatch);
68
+ dependencyApi === null || dependencyApi === void 0 || dependencyApi.core.actions.execute(insertDateCommand(dependencyApi)({
69
+ date: date,
70
+ enterPressed: false
71
+ }));
66
72
  },
67
73
  closeDatePicker: function closeDatePicker() {
68
74
  _closeDatePicker()(editorView.state, dispatch);
@@ -103,9 +109,9 @@ var datePlugin = function datePlugin(_ref3) {
103
109
  focusDateInput: !!focusDateInput
104
110
  };
105
111
  },
106
- actions: {
107
- insertDate: insertDate,
108
- deleteDate: deleteDate
112
+ commands: {
113
+ insertDate: insertDateCommand(api),
114
+ deleteDate: deleteDateCommand(api)
109
115
  },
110
116
  nodes: function nodes() {
111
117
  return [{
@@ -197,11 +197,12 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
197
197
  })(state, dispatch)) !== null && _pluginInjectionApi$t !== void 0 ? _pluginInjectionApi$t : false;
198
198
  });
199
199
  _defineProperty(_assertThisInitialized(_this), "createDate", function (inputMethod) {
200
- var _pluginInjectionApi$d, _pluginInjectionApi$d2;
201
- var _this$props3 = _this.props,
202
- pluginInjectionApi = _this$props3.pluginInjectionApi,
203
- editorView = _this$props3.editorView;
204
- return (_pluginInjectionApi$d = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$d2 = pluginInjectionApi.date) === null || _pluginInjectionApi$d2 === void 0 ? void 0 : _pluginInjectionApi$d2.actions.insertDate(undefined, inputMethod)(editorView.state, editorView.dispatch)) !== null && _pluginInjectionApi$d !== void 0 ? _pluginInjectionApi$d : false;
200
+ var _pluginInjectionApi$d;
201
+ var pluginInjectionApi = _this.props.pluginInjectionApi;
202
+ pluginInjectionApi === null || pluginInjectionApi === void 0 || pluginInjectionApi.core.actions.execute(pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$d = pluginInjectionApi.date) === null || _pluginInjectionApi$d === void 0 || (_pluginInjectionApi$d = _pluginInjectionApi$d.commands) === null || _pluginInjectionApi$d === void 0 ? void 0 : _pluginInjectionApi$d.insertDate({
203
+ inputMethod: inputMethod
204
+ }));
205
+ return true;
205
206
  });
206
207
  _defineProperty(_assertThisInitialized(_this), "createPlaceholderText", function () {
207
208
  var editorView = _this.props.editorView;
@@ -215,15 +216,15 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
215
216
  });
216
217
  _defineProperty(_assertThisInitialized(_this), "createStatus", function (inputMethod) {
217
218
  var _pluginInjectionApi$s, _pluginInjectionApi$s2;
218
- var _this$props4 = _this.props,
219
- pluginInjectionApi = _this$props4.pluginInjectionApi,
220
- editorView = _this$props4.editorView;
219
+ var _this$props3 = _this.props,
220
+ pluginInjectionApi = _this$props3.pluginInjectionApi,
221
+ editorView = _this$props3.editorView;
221
222
  return (_pluginInjectionApi$s = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$s2 = pluginInjectionApi.status) === null || _pluginInjectionApi$s2 === void 0 || (_pluginInjectionApi$s2 = _pluginInjectionApi$s2.actions) === null || _pluginInjectionApi$s2 === void 0 ? void 0 : _pluginInjectionApi$s2.updateStatus(inputMethod)(editorView.state, editorView.dispatch)) !== null && _pluginInjectionApi$s !== void 0 ? _pluginInjectionApi$s : false;
222
223
  });
223
224
  _defineProperty(_assertThisInitialized(_this), "openMediaPicker", function (inputMethod) {
224
- var _this$props5 = _this.props,
225
- onShowMediaPicker = _this$props5.onShowMediaPicker,
226
- dispatchAnalyticsEvent = _this$props5.dispatchAnalyticsEvent;
225
+ var _this$props4 = _this.props,
226
+ onShowMediaPicker = _this$props4.onShowMediaPicker,
227
+ dispatchAnalyticsEvent = _this$props4.dispatchAnalyticsEvent;
227
228
  if (onShowMediaPicker) {
228
229
  onShowMediaPicker();
229
230
  if (dispatchAnalyticsEvent) {
@@ -251,11 +252,11 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
251
252
  });
252
253
  _defineProperty(_assertThisInitialized(_this), "insertHorizontalRule", function (inputMethod) {
253
254
  var _pluginInjectionApi$r, _pluginInjectionApi$r2;
254
- var _this$props6 = _this.props,
255
- _this$props6$editorVi = _this$props6.editorView,
256
- state = _this$props6$editorVi.state,
257
- dispatch = _this$props6$editorVi.dispatch,
258
- pluginInjectionApi = _this$props6.pluginInjectionApi;
255
+ var _this$props5 = _this.props,
256
+ _this$props5$editorVi = _this$props5.editorView,
257
+ state = _this$props5$editorVi.state,
258
+ dispatch = _this$props5$editorVi.dispatch,
259
+ pluginInjectionApi = _this$props5.pluginInjectionApi;
259
260
  return (_pluginInjectionApi$r = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$r2 = pluginInjectionApi.rule) === null || _pluginInjectionApi$r2 === void 0 ? void 0 : _pluginInjectionApi$r2.actions.insertHorizontalRule(inputMethod)(state, dispatch)) !== null && _pluginInjectionApi$r !== void 0 ? _pluginInjectionApi$r : false;
260
261
  });
261
262
  _defineProperty(_assertThisInitialized(_this), "insertExpand", function () {
@@ -266,9 +267,9 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
266
267
  });
267
268
  _defineProperty(_assertThisInitialized(_this), "insertBlockType", function (itemName) {
268
269
  return function () {
269
- var _this$props7 = _this.props,
270
- editorView = _this$props7.editorView,
271
- onInsertBlockType = _this$props7.onInsertBlockType;
270
+ var _this$props6 = _this.props,
271
+ editorView = _this$props6.editorView,
272
+ onInsertBlockType = _this$props6.onInsertBlockType;
272
273
  var state = editorView.state,
273
274
  dispatch = editorView.dispatch;
274
275
  onInsertBlockType(itemName)(state, dispatch);
@@ -291,11 +292,11 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
291
292
  _defineProperty(_assertThisInitialized(_this), "onItemActivated", function (_ref2) {
292
293
  var item = _ref2.item,
293
294
  inputMethod = _ref2.inputMethod;
294
- var _this$props8 = _this.props,
295
- editorView = _this$props8.editorView,
296
- editorActions = _this$props8.editorActions,
297
- handleImageUpload = _this$props8.handleImageUpload,
298
- expandEnabled = _this$props8.expandEnabled;
295
+ var _this$props7 = _this.props,
296
+ editorView = _this$props7.editorView,
297
+ editorActions = _this$props7.editorActions,
298
+ handleImageUpload = _this$props7.handleImageUpload,
299
+ expandEnabled = _this$props7.expandEnabled;
299
300
 
300
301
  // need to do this before inserting nodes so scrollIntoView works properly
301
302
  if (!editorView.hasFocus()) {
@@ -411,12 +412,12 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
411
412
  key: "renderPopup",
412
413
  value: function renderPopup() {
413
414
  var emojiPickerOpen = this.state.emojiPickerOpen;
414
- var _this$props9 = this.props,
415
- popupsMountPoint = _this$props9.popupsMountPoint,
416
- popupsBoundariesElement = _this$props9.popupsBoundariesElement,
417
- popupsScrollableElement = _this$props9.popupsScrollableElement,
418
- emojiProvider = _this$props9.emojiProvider,
419
- replacePlusMenuWithElementBrowser = _this$props9.replacePlusMenuWithElementBrowser;
415
+ var _this$props8 = this.props,
416
+ popupsMountPoint = _this$props8.popupsMountPoint,
417
+ popupsBoundariesElement = _this$props8.popupsBoundariesElement,
418
+ popupsScrollableElement = _this$props8.popupsScrollableElement,
419
+ emojiProvider = _this$props8.emojiProvider,
420
+ replacePlusMenuWithElementBrowser = _this$props8.replacePlusMenuWithElementBrowser;
420
421
  var dropdownEmoji = this.state.dropdownItems.some(function (_ref4) {
421
422
  var name = _ref4.value.name;
422
423
  return name === 'emoji';
@@ -453,9 +454,9 @@ export var ToolbarInsertBlock = /*#__PURE__*/function (_React$PureComponent) {
453
454
  buttons = _this$state.buttons,
454
455
  dropdownItems = _this$state.dropdownItems,
455
456
  emojiPickerOpen = _this$state.emojiPickerOpen;
456
- var _this$props10 = this.props,
457
- isDisabled = _this$props10.isDisabled,
458
- isReducedSpacing = _this$props10.isReducedSpacing;
457
+ var _this$props9 = this.props,
458
+ isDisabled = _this$props9.isDisabled,
459
+ isReducedSpacing = _this$props9.isReducedSpacing;
459
460
  if (buttons.length === 0 && dropdownItems.length === 0) {
460
461
  return null;
461
462
  }
@@ -1,369 +1,3 @@
1
- import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
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
- import React from 'react';
11
- import { SelectItemMode } from '@atlaskit/editor-common/type-ahead';
12
- import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
13
- import { createPlugin } from './pm-plugins/main';
14
- import { createPlugin as createInsertItemPlugin } from './pm-plugins/insert-item-plugin';
15
- import { WithPluginState } from '@atlaskit/editor-common/with-plugin-state';
16
- import { typeAheadQuery } from '@atlaskit/adf-schema';
17
- import { pluginKey as typeAheadPluginKey } from './pm-plugins/key';
18
- import { inputRulePlugin } from './pm-plugins/input-rules';
19
- import { TypeAheadPopup } from './ui/TypeAheadPopup';
20
- import { findHandler, getPluginState, isTypeAheadOpen, isTypeAheadAllowed, getTypeAheadQuery, getTypeAheadHandler } from './utils';
21
- import { useItemInsert } from './ui/hooks/use-item-insert';
22
- import { updateSelectedIndex } from './commands/update-selected-index';
23
- import { StatsModifier } from './stats-modifier';
24
- import { ACTION, ACTION_SUBJECT, INPUT_METHOD, EVENT_TYPE, fireAnalyticsEvent } from '@atlaskit/editor-common/analytics';
25
- import { openTypeAheadAtCursor } from './transforms/open-typeahead-at-cursor';
26
- import { closeTypeAhead } from './transforms/close-type-ahead';
27
- import { insertTypeAheadItem } from './commands/insert-type-ahead-item';
28
- var TypeAheadMenu = /*#__PURE__*/React.memo(function (_ref) {
29
- var _popupMountRef$curren, _popupMountRef$curren2, _popupMountRef$curren3;
30
- var editorView = _ref.editorView,
31
- popupMountRef = _ref.popupMountRef,
32
- typeAheadState = _ref.typeAheadState,
33
- fireAnalyticsCallback = _ref.fireAnalyticsCallback;
34
- var isOpen = typeAheadState.decorationSet.find().length > 0;
35
- var triggerHandler = typeAheadState.triggerHandler,
36
- items = typeAheadState.items,
37
- selectedIndex = typeAheadState.selectedIndex,
38
- decorationElement = typeAheadState.decorationElement,
39
- decorationSet = typeAheadState.decorationSet,
40
- query = typeAheadState.query;
41
- var _useItemInsert = useItemInsert(triggerHandler, editorView, items),
42
- _useItemInsert2 = _slicedToArray(_useItemInsert, 3),
43
- onItemInsert = _useItemInsert2[0],
44
- onTextInsert = _useItemInsert2[1],
45
- onItemMatch = _useItemInsert2[2];
46
- var setSelectedItem = React.useCallback(function (_ref2) {
47
- var nextIndex = _ref2.index;
48
- queueMicrotask(function () {
49
- updateSelectedIndex(nextIndex)(editorView.state, editorView.dispatch);
50
- });
51
- }, [editorView]);
52
- var insertItem = React.useCallback(function () {
53
- var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : SelectItemMode.SELECTED;
54
- var index = arguments.length > 1 ? arguments[1] : undefined;
55
- queueMicrotask(function () {
56
- onItemInsert({
57
- mode: mode,
58
- index: index,
59
- query: query
60
- });
61
- });
62
- }, [onItemInsert, query]);
63
- var cancel = React.useCallback(function (_ref3) {
64
- var setSelectionAt = _ref3.setSelectionAt,
65
- addPrefixTrigger = _ref3.addPrefixTrigger,
66
- forceFocusOnEditor = _ref3.forceFocusOnEditor;
67
- var fullQuery = addPrefixTrigger ? "".concat(triggerHandler === null || triggerHandler === void 0 ? void 0 : triggerHandler.trigger).concat(query) : query;
68
- onTextInsert({
69
- forceFocusOnEditor: forceFocusOnEditor,
70
- setSelectionAt: setSelectionAt,
71
- text: fullQuery
72
- });
73
- }, [triggerHandler, onTextInsert, query]);
74
- React.useEffect(function () {
75
- if (!isOpen || !query) {
76
- return;
77
- }
78
- var isLastCharSpace = query[query.length - 1] === ' ';
79
- if (!isLastCharSpace) {
80
- return;
81
- }
82
- var result = onItemMatch({
83
- mode: SelectItemMode.SPACE,
84
- query: query.trim()
85
- });
86
- if (!result) {
87
- return;
88
- }
89
- }, [isOpen, query, onItemMatch]);
90
- if (!isOpen || !triggerHandler || !(decorationElement instanceof HTMLElement) || items.length === 0) {
91
- return null;
92
- }
93
- return /*#__PURE__*/React.createElement(TypeAheadPopup, {
94
- editorView: editorView,
95
- popupsMountPoint: (_popupMountRef$curren = popupMountRef.current) === null || _popupMountRef$curren === void 0 ? void 0 : _popupMountRef$curren.popupsMountPoint,
96
- popupsBoundariesElement: (_popupMountRef$curren2 = popupMountRef.current) === null || _popupMountRef$curren2 === void 0 ? void 0 : _popupMountRef$curren2.popupsBoundariesElement,
97
- popupsScrollableElement: (_popupMountRef$curren3 = popupMountRef.current) === null || _popupMountRef$curren3 === void 0 ? void 0 : _popupMountRef$curren3.popupsScrollableElement,
98
- anchorElement: decorationElement,
99
- triggerHandler: triggerHandler,
100
- fireAnalyticsCallback: fireAnalyticsCallback,
101
- items: items,
102
- selectedIndex: selectedIndex,
103
- setSelectedItem: setSelectedItem,
104
- onItemInsert: insertItem,
105
- decorationSet: decorationSet,
106
- isEmptyQuery: !query,
107
- cancel: cancel
108
- });
109
- });
110
- var createOpenAtTransaction = function createOpenAtTransaction(editorAnalyticsAPI) {
111
- return function (props) {
112
- return function (tr) {
113
- var triggerHandler = props.triggerHandler,
114
- inputMethod = props.inputMethod;
115
- openTypeAheadAtCursor({
116
- triggerHandler: triggerHandler,
117
- inputMethod: inputMethod
118
- })({
119
- tr: tr
120
- });
121
- editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
122
- action: ACTION.INVOKED,
123
- actionSubject: ACTION_SUBJECT.TYPEAHEAD,
124
- actionSubjectId: triggerHandler.id,
125
- attributes: {
126
- inputMethod: inputMethod
127
- },
128
- eventType: EVENT_TYPE.UI
129
- })(tr);
130
- return true;
131
- };
132
- };
133
- };
134
- var createOpenTypeAhead = function createOpenTypeAhead(editorViewRef, editorAnalyticsAPI) {
135
- return function (props) {
136
- if (!editorViewRef.current) {
137
- return false;
138
- }
139
- var view = editorViewRef.current;
140
- var tr = view.state.tr;
141
- createOpenAtTransaction(editorAnalyticsAPI)(props)(tr);
142
- view.dispatch(tr);
143
- return true;
144
- };
145
- };
146
- var createInsertTypeAheadItem = function createInsertTypeAheadItem(editorViewRef) {
147
- return function (props) {
148
- if (!editorViewRef.current) {
149
- return false;
150
- }
151
- var view = editorViewRef.current;
152
- var triggerHandler = props.triggerHandler,
153
- contentItem = props.contentItem,
154
- query = props.query,
155
- sourceListItem = props.sourceListItem,
156
- mode = props.mode;
157
- insertTypeAheadItem(view)({
158
- handler: triggerHandler,
159
- item: contentItem,
160
- mode: mode || SelectItemMode.SELECTED,
161
- query: query,
162
- sourceListItem: sourceListItem
163
- });
164
- return true;
165
- };
166
- };
167
- var createFindHandlerByTrigger = function createFindHandlerByTrigger(editorViewRef) {
168
- return function (trigger) {
169
- if (!editorViewRef.current) {
170
- return null;
171
- }
172
- var view = editorViewRef.current;
173
- return findHandler(trigger, view.state);
174
- };
175
- };
176
- var createCloseTypeAhead = function createCloseTypeAhead(editorViewRef) {
177
- return function (options) {
178
- if (!editorViewRef.current) {
179
- return false;
180
- }
181
- var view = editorViewRef.current;
182
- var currentQuery = getTypeAheadQuery(view.state) || '';
183
- var state = view.state;
184
- var tr = state.tr;
185
- if (options.attachCommand) {
186
- var fakeDispatch = function fakeDispatch(customTr) {
187
- tr = customTr;
188
- };
189
- options.attachCommand(state, fakeDispatch);
190
- }
191
- closeTypeAhead(tr);
192
- if (options.insertCurrentQueryAsRawText && currentQuery && currentQuery.length > 0) {
193
- var handler = getTypeAheadHandler(state);
194
- if (!handler) {
195
- return false;
196
- }
197
- var text = handler.trigger.concat(currentQuery);
198
- tr.replaceSelectionWith(state.schema.text(text));
199
- }
200
- view.dispatch(tr);
201
- if (!view.hasFocus()) {
202
- view.focus();
203
- }
204
- return true;
205
- };
206
- };
207
-
208
- /**
209
- *
210
- * Revamped typeahead using decorations instead of the `typeAheadQuery` mark
211
- *
212
- * https://product-fabric.atlassian.net/wiki/spaces/E/pages/2992177582/Technical+TypeAhead+Data+Flow
213
- *
214
- *
215
- */
216
- var typeAheadPlugin = function typeAheadPlugin(_ref4) {
217
- var _api$analytics, _api$analytics2;
218
- var options = _ref4.config,
219
- api = _ref4.api;
220
- var fireAnalyticsCallback = fireAnalyticsEvent(options === null || options === void 0 ? void 0 : options.createAnalyticsEvent);
221
- var popupMountRef = {
222
- current: null
223
- };
224
- var editorViewRef = {
225
- current: null
226
- };
227
- return {
228
- name: 'typeAhead',
229
- marks: function marks() {
230
- // We need to keep this to make sure
231
- // All documents with typeahead marks will be loaded normally
232
- return [{
233
- name: 'typeAheadQuery',
234
- mark: typeAheadQuery
235
- }];
236
- },
237
- pmPlugins: function pmPlugins() {
238
- var typeAhead = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
239
- return [{
240
- name: 'typeAhead',
241
- plugin: function plugin(_ref5) {
242
- var dispatch = _ref5.dispatch,
243
- getIntl = _ref5.getIntl;
244
- return createPlugin({
245
- getIntl: getIntl,
246
- popupMountRef: popupMountRef,
247
- reactDispatch: dispatch,
248
- typeAheadHandlers: typeAhead,
249
- createAnalyticsEvent: options === null || options === void 0 ? void 0 : options.createAnalyticsEvent
250
- });
251
- }
252
- }, {
253
- name: 'typeAheadEditorViewRef',
254
- plugin: function plugin() {
255
- return new SafePlugin({
256
- view: function view(_view) {
257
- editorViewRef.current = _view;
258
- return {
259
- destroy: function destroy() {
260
- editorViewRef.current = null;
261
- }
262
- };
263
- }
264
- });
265
- }
266
- }, {
267
- name: 'typeAheadInsertItem',
268
- plugin: createInsertItemPlugin
269
- }, {
270
- name: 'typeAheadInputRule',
271
- plugin: function plugin(_ref6) {
272
- var schema = _ref6.schema,
273
- featureFlags = _ref6.featureFlags;
274
- return inputRulePlugin(schema, typeAhead, featureFlags);
275
- }
276
- }];
277
- },
278
- getSharedState: function getSharedState(editorState) {
279
- if (!editorState) {
280
- return {
281
- query: ''
282
- };
283
- }
284
- return {
285
- query: getTypeAheadQuery(editorState) || ''
286
- };
287
- },
288
- actions: {
289
- isOpen: isTypeAheadOpen,
290
- isAllowed: isTypeAheadAllowed,
291
- open: createOpenTypeAhead(editorViewRef, api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions),
292
- openAtTransaction: createOpenAtTransaction(api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions),
293
- findHandlerByTrigger: createFindHandlerByTrigger(editorViewRef),
294
- insert: createInsertTypeAheadItem(editorViewRef),
295
- close: createCloseTypeAhead(editorViewRef)
296
- },
297
- contentComponent: function contentComponent(_ref7) {
298
- var editorView = _ref7.editorView,
299
- containerElement = _ref7.containerElement,
300
- popupsMountPoint = _ref7.popupsMountPoint,
301
- popupsBoundariesElement = _ref7.popupsBoundariesElement,
302
- popupsScrollableElement = _ref7.popupsScrollableElement,
303
- wrapperElement = _ref7.wrapperElement;
304
- popupMountRef.current = {
305
- popupsMountPoint: popupsMountPoint || wrapperElement || undefined,
306
- popupsBoundariesElement: popupsBoundariesElement,
307
- popupsScrollableElement: popupsScrollableElement || containerElement || undefined
308
- };
309
- return /*#__PURE__*/React.createElement(WithPluginState, {
310
- plugins: {
311
- typeAheadState: typeAheadPluginKey
312
- },
313
- render: function render(_ref8) {
314
- var typeAheadState = _ref8.typeAheadState;
315
- if (!typeAheadState) {
316
- return null;
317
- }
318
- return /*#__PURE__*/React.createElement(TypeAheadMenu, {
319
- editorView: editorView,
320
- popupMountRef: popupMountRef,
321
- typeAheadState: typeAheadState,
322
- fireAnalyticsCallback: fireAnalyticsCallback
323
- });
324
- }
325
- });
326
- },
327
- onEditorViewStateUpdated: function onEditorViewStateUpdated(_ref9) {
328
- var originalTransaction = _ref9.originalTransaction,
329
- oldEditorState = _ref9.oldEditorState,
330
- newEditorState = _ref9.newEditorState;
331
- var oldPluginState = getPluginState(oldEditorState);
332
- var newPluginState = getPluginState(newEditorState);
333
- if (!oldPluginState || !newPluginState) {
334
- return;
335
- }
336
- var oldTriggerHandler = oldPluginState.triggerHandler;
337
- var newTriggerHandler = newPluginState.triggerHandler;
338
- var isANewHandler = oldTriggerHandler !== newTriggerHandler;
339
- if (oldTriggerHandler !== null && oldTriggerHandler !== void 0 && oldTriggerHandler.dismiss && isANewHandler) {
340
- var typeAheadMessage = originalTransaction.getMeta(typeAheadPluginKey);
341
- var wasItemInserted = typeAheadMessage && typeAheadMessage.action === 'INSERT_RAW_QUERY';
342
- oldTriggerHandler.dismiss({
343
- editorState: newEditorState,
344
- query: oldPluginState.query,
345
- stats: (oldPluginState.stats || new StatsModifier()).serialize(),
346
- wasItemInserted: wasItemInserted
347
- });
348
- }
349
- if (newTriggerHandler !== null && newTriggerHandler !== void 0 && newTriggerHandler.onOpen && isANewHandler) {
350
- newTriggerHandler.onOpen(newEditorState);
351
- }
352
- if (newTriggerHandler && isANewHandler && options !== null && options !== void 0 && options.createAnalyticsEvent) {
353
- fireAnalyticsCallback({
354
- payload: {
355
- action: ACTION.INVOKED,
356
- actionSubject: ACTION_SUBJECT.TYPEAHEAD,
357
- actionSubjectId: newTriggerHandler.id || 'not_set',
358
- attributes: {
359
- inputMethod: newPluginState.inputMethod || INPUT_METHOD.KEYBOARD
360
- },
361
- eventType: EVENT_TYPE.UI
362
- }
363
- });
364
- }
365
- }
366
- };
367
- };
1
+ import { typeAheadPlugin } from './plugin';
368
2
  export default typeAheadPlugin;
369
- export { typeAheadPluginKey };
3
+ export { typeAheadPlugin };