@atlaskit/link-picker 1.4.0 → 1.5.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 (28) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/services/use-plugins/index.js +201 -0
  3. package/dist/cjs/ui/link-picker/index.js +308 -599
  4. package/dist/cjs/ui/link-picker/link-search-list/index.js +54 -82
  5. package/dist/cjs/ui/link-picker/list-item/index.js +5 -3
  6. package/dist/cjs/ui/link-picker/styled.js +5 -3
  7. package/dist/cjs/ui/link-picker/text-input/index.js +6 -4
  8. package/dist/cjs/version.json +1 -1
  9. package/dist/es2019/services/use-plugins/index.js +122 -0
  10. package/dist/es2019/ui/link-picker/index.js +288 -467
  11. package/dist/es2019/ui/link-picker/link-search-list/index.js +49 -50
  12. package/dist/es2019/ui/link-picker/list-item/index.js +4 -3
  13. package/dist/es2019/ui/link-picker/styled.js +4 -0
  14. package/dist/es2019/ui/link-picker/text-input/index.js +5 -4
  15. package/dist/es2019/version.json +1 -1
  16. package/dist/esm/services/use-plugins/index.js +187 -0
  17. package/dist/esm/ui/link-picker/index.js +298 -601
  18. package/dist/esm/ui/link-picker/link-search-list/index.js +51 -79
  19. package/dist/esm/ui/link-picker/list-item/index.js +4 -3
  20. package/dist/esm/ui/link-picker/styled.js +3 -2
  21. package/dist/esm/ui/link-picker/text-input/index.js +5 -4
  22. package/dist/esm/version.json +1 -1
  23. package/dist/types/services/use-plugins/index.d.ts +12 -0
  24. package/dist/types/ui/link-picker/index.d.ts +24 -13
  25. package/dist/types/ui/link-picker/link-search-list/index.d.ts +3 -5
  26. package/dist/types/ui/link-picker/styled.d.ts +1 -0
  27. package/dist/types/ui/link-picker/text-input/index.d.ts +1 -1
  28. package/package.json +4 -3
@@ -1,637 +1,340 @@
1
- import _typeof from "@babel/runtime/helpers/typeof";
2
- import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
- import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4
- import _createClass from "@babel/runtime/helpers/createClass";
5
- import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
6
- import _inherits from "@babel/runtime/helpers/inherits";
7
- import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
8
- import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
9
3
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
10
4
 
11
5
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
12
6
 
13
7
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
14
8
 
15
- import _regeneratorRuntime from "@babel/runtime/regenerator";
16
-
17
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
18
-
19
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
20
-
21
9
  /** @jsx jsx */
22
- import { PureComponent } from 'react';
10
+ import { useCallback, useReducer } from 'react';
23
11
  import { jsx } from '@emotion/react';
24
- import { injectIntl } from 'react-intl-next';
12
+ import { useIntl, FormattedMessage } from 'react-intl-next';
13
+ import Button, { ButtonGroup } from '@atlaskit/button';
14
+ import { FormFooter } from '@atlaskit/form';
15
+ import EditorSearchIcon from '@atlaskit/icon/glyph/editor/search';
25
16
  import Tabs, { Tab, TabList } from '@atlaskit/tabs';
17
+ import VisuallyHidden from '@atlaskit/visually-hidden';
26
18
  import { messages } from './messages';
27
19
  import PanelTextInput from './text-input';
28
20
  import LinkSearchList from './link-search-list';
29
21
  import { normalizeUrl, isSafeUrl } from './url';
30
- import { rootContainerStyles, searchIconStyles, listTitleStyles } from './styled';
22
+ import { rootContainerStyles, searchIconStyles, listTitleStyles, tabsWrapperStyles } from './styled';
31
23
  import { browser } from './browser';
32
24
  import { transformTimeStamp } from './transformTimeStamp';
33
- import Announcer from './/announcer';
34
- import Button, { ButtonGroup } from '@atlaskit/button';
35
- import { FormFooter } from '@atlaskit/form';
36
- import EditorSearchIcon from '@atlaskit/icon/glyph/editor/search';
37
- import VisuallyHidden from '@atlaskit/visually-hidden';
25
+ import Announcer from './announcer';
38
26
  import LinkSearchNoResults from './link-search-no-results';
27
+ import { usePlugins } from '../../services/use-plugins';
39
28
  export var RECENT_SEARCH_LIST_SIZE = 5;
29
+ export var testIds = {
30
+ linkPicker: 'link-picker',
31
+ urlInputField: 'link-url',
32
+ textInputField: 'link-text',
33
+ searchIcon: 'link-picker-search-icon',
34
+ insertButton: 'link-picker-insert-button',
35
+ cancelButton: 'link-picker-cancel-button',
36
+ clearUrlButton: 'clear-text',
37
+ resultListTitle: 'link-picker-list-title',
38
+ emptyResultPage: 'link-search-no-results',
39
+ searchResultList: 'link-search-list',
40
+ searchResultItem: 'link-search-list-item',
41
+ searchResultLoadingIndicator: 'link-picker.results-loading-indicator',
42
+ urlError: 'link-error',
43
+ tabList: 'link-picker-tabs',
44
+ tabItem: 'link-picker-tab'
45
+ };
46
+ var initState = {
47
+ url: '',
48
+ displayText: '',
49
+ activeIndex: -1,
50
+ selectedIndex: -1,
51
+ invalidUrl: false,
52
+ activeTab: 0
53
+ };
54
+
55
+ function reducer(state, payload) {
56
+ if (payload.url && state.url !== payload.url) {
57
+ return _objectSpread(_objectSpread({}, state), {}, {
58
+ invalidUrl: false,
59
+ selectedIndex: isSafeUrl(payload.url) && payload.url.length ? -1 : state.selectedIndex
60
+ }, payload);
61
+ }
40
62
 
41
- var LinkPicker = /*#__PURE__*/function (_PureComponent) {
42
- _inherits(LinkPicker, _PureComponent);
43
-
44
- var _super = _createSuper(LinkPicker);
45
-
46
- function LinkPicker(props) {
47
- var _this;
48
-
49
- _classCallCheck(this, LinkPicker);
50
-
51
- _this = _super.call(this, props);
52
-
53
- _defineProperty(_assertThisInitialized(_this), "queryVersion", 0);
54
-
55
- _defineProperty(_assertThisInitialized(_this), "updateInput", /*#__PURE__*/function () {
56
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(input) {
57
- return _regeneratorRuntime.wrap(function _callee$(_context) {
58
- while (1) {
59
- switch (_context.prev = _context.next) {
60
- case 0:
61
- if (!(input.length && isSafeUrl(input))) {
62
- _context.next = 2;
63
- break;
64
- }
65
-
66
- return _context.abrupt("return", _this.setState({
67
- url: input,
68
- selectedIndex: -1,
69
- items: [],
70
- isLoading: false,
71
- invalidUrl: null
72
- }));
73
-
74
- case 2:
75
- return _context.abrupt("return", _this.setState({
76
- url: input,
77
- // Unset selection on any input change
78
- selectedIndex: -1,
79
- invalidUrl: null
80
- }, function () {
81
- _this.updateResults();
82
- }));
83
-
84
- case 3:
85
- case "end":
86
- return _context.stop();
87
- }
88
- }
89
- }, _callee);
90
- }));
91
-
92
- return function (_x) {
93
- return _ref.apply(this, arguments);
94
- };
95
- }());
96
-
97
- _defineProperty(_assertThisInitialized(_this), "createClearHandler", function (field) {
98
- return function () {
99
- switch (field) {
100
- case 'url':
101
- {
102
- _this.updateInput('');
103
-
104
- break;
105
- }
106
-
107
- case 'displayText':
108
- {
109
- _this.setState(_defineProperty({}, field, ''));
110
- }
111
- }
112
- };
113
- });
114
-
115
- _defineProperty(_assertThisInitialized(_this), "getScreenReaderText", function () {
116
- var intl = _this.props.intl;
117
- var _this$state = _this.state,
118
- items = _this$state.items,
119
- selectedIndex = _this$state.selectedIndex;
120
-
121
- if (items.length && selectedIndex > -1) {
122
- var _items$selectedIndex = items[selectedIndex],
123
- name = _items$selectedIndex.name,
124
- container = _items$selectedIndex.container,
125
- lastUpdatedDate = _items$selectedIndex.lastUpdatedDate,
126
- lastViewedDate = _items$selectedIndex.lastViewedDate;
127
- var date = transformTimeStamp(intl, lastViewedDate, lastUpdatedDate);
128
- var formattedDate = [date === null || date === void 0 ? void 0 : date.pageAction, date === null || date === void 0 ? void 0 : date.dateString, date === null || date === void 0 ? void 0 : date.timeSince].filter(Boolean).join(' ');
129
- return [name, container, formattedDate].filter(Boolean).join(', ');
130
- }
131
- });
132
-
133
- _defineProperty(_assertThisInitialized(_this), "getTabs", function () {
134
- if (_this.props.plugins && _this.props.plugins.length > 1) {
135
- return _this.props.plugins.filter(function (plugin) {
136
- return !!plugin.tabTitle;
137
- }).map(function (plugin) {
138
- return {
139
- tabTitle: plugin.tabTitle
140
- };
141
- });
142
- }
143
-
144
- return [];
145
- });
146
-
147
- _defineProperty(_assertThisInitialized(_this), "isUrlPopulatedWithSelectedItem", function () {
148
- /**
149
- * When we use ArrowKey to navigate through result items,
150
- * the URL field will be populated with the content of
151
- * selected item.
152
- * This function will check if the URL field is populated
153
- * with selected item.
154
- * It can be useful to detect whether we want to insert a
155
- * smartlink or a hyperlink with customized title
156
- */
157
- var _this$state2 = _this.state,
158
- items = _this$state2.items,
159
- selectedIndex = _this$state2.selectedIndex,
160
- url = _this$state2.url;
161
- var selectedItem = items[selectedIndex];
162
-
163
- if (selectedItem && selectedItem.url === url) {
164
- return true;
165
- }
63
+ return _objectSpread(_objectSpread({}, state), payload);
64
+ }
166
65
 
167
- return false;
66
+ function LinkPicker(_ref) {
67
+ var onSubmit = _ref.onSubmit,
68
+ onCancel = _ref.onCancel,
69
+ plugins = _ref.plugins,
70
+ initUrl = _ref.url,
71
+ initDisplayText = _ref.displayText;
72
+
73
+ var _useReducer = useReducer(reducer, _objectSpread(_objectSpread({}, initState), {}, {
74
+ url: normalizeUrl(initUrl) || '',
75
+ displayText: initDisplayText || ''
76
+ })),
77
+ _useReducer2 = _slicedToArray(_useReducer, 2),
78
+ state = _useReducer2[0],
79
+ dispatch = _useReducer2[1];
80
+
81
+ var activeIndex = state.activeIndex,
82
+ selectedIndex = state.selectedIndex,
83
+ url = state.url,
84
+ displayText = state.displayText,
85
+ invalidUrl = state.invalidUrl;
86
+ var isEditing = !!initUrl;
87
+ var intl = useIntl();
88
+
89
+ var _usePlugins = usePlugins(state, plugins),
90
+ items = _usePlugins.items,
91
+ isLoading = _usePlugins.isLoading,
92
+ isSelectedItem = _usePlugins.isSelectedItem,
93
+ isActivePlugin = _usePlugins.isActivePlugin,
94
+ tabs = _usePlugins.tabs;
95
+
96
+ var handleChangeUrl = useCallback(function (url) {
97
+ dispatch({
98
+ url: url
168
99
  });
169
-
170
- _defineProperty(_assertThisInitialized(_this), "handleSelected", function (objectId) {
171
- var items = _this.state.items;
172
- var selectedItem = items.find(function (item) {
173
- return item.objectId === objectId;
174
- });
175
-
176
- if (selectedItem) {
177
- var url = selectedItem.url,
178
- name = selectedItem.name;
179
-
180
- _this.handleInsert(url, name, 'typeAhead');
181
- }
100
+ }, []);
101
+ var handleChangeText = useCallback(function (displayText) {
102
+ dispatch({
103
+ displayText: displayText
182
104
  });
105
+ }, []);
106
+ var handleKeyDown = useCallback(function (event) {
107
+ var keyCode = event.keyCode;
108
+ var KEY_CODE_ARROW_DOWN = 40;
109
+ var KEY_CODE_ARROW_UP = 38;
110
+
111
+ if (!items || !items.length) {
112
+ return;
113
+ }
183
114
 
184
- _defineProperty(_assertThisInitialized(_this), "handleInsert", function (url, title, inputType) {
185
- var onSubmit = _this.props.onSubmit;
186
- var _this$state3 = _this.state,
187
- rawUrl = _this$state3.url,
188
- displayText = _this$state3.displayText;
115
+ var updatedIndex = activeIndex;
189
116
 
190
- if (onSubmit) {
191
- onSubmit(_objectSpread({
192
- url: url,
193
- displayText: displayText || null,
194
- title: title || null,
195
- meta: {
196
- inputMethod: inputType
197
- }
198
- }, inputType === 'manual' ? {
199
- rawUrl: rawUrl
200
- } : {}));
201
- }
202
- });
117
+ switch (keyCode) {
118
+ case KEY_CODE_ARROW_DOWN:
119
+ // down
120
+ event.preventDefault();
121
+ updatedIndex = (activeIndex + 1) % items.length;
122
+ break;
203
123
 
204
- _defineProperty(_assertThisInitialized(_this), "handleMouseEnterResultItem", function (objectId) {
205
- var items = _this.state.items;
206
- var index = findIndex(items, function (item) {
207
- return item.objectId === objectId;
208
- });
124
+ case KEY_CODE_ARROW_UP:
125
+ // up
126
+ event.preventDefault();
127
+ updatedIndex = activeIndex > 0 ? activeIndex - 1 : items.length - 1;
128
+ break;
129
+ }
209
130
 
210
- _this.setState({
211
- activeIndex: index
131
+ if ([KEY_CODE_ARROW_DOWN, KEY_CODE_ARROW_UP].includes(keyCode) && items[updatedIndex]) {
132
+ dispatch({
133
+ activeIndex: updatedIndex,
134
+ selectedIndex: updatedIndex,
135
+ url: items[updatedIndex].url,
136
+ invalidUrl: false
212
137
  });
138
+ }
139
+ }, [activeIndex, items]);
140
+ var handleClear = useCallback(function (field) {
141
+ dispatch(_defineProperty({}, field, ''));
142
+ }, []);
143
+ var handleMouseEnterResultItem = useCallback(function (objectId) {
144
+ var index = findIndex(items, function (item) {
145
+ return item.objectId === objectId;
213
146
  });
214
-
215
- _defineProperty(_assertThisInitialized(_this), "handleMouseLeaveResultItem", function (objectId) {
216
- var _this$state4 = _this.state,
217
- items = _this$state4.items,
218
- activeIndex = _this$state4.activeIndex;
219
- var index = findIndex(items, function (item) {
220
- return item.objectId === objectId;
221
- }); // This is to avoid updating index that was set by other mouseenter event
222
-
223
- if (activeIndex === index) {
224
- _this.setState({
225
- activeIndex: -1
226
- });
227
- }
147
+ dispatch({
148
+ activeIndex: index
228
149
  });
229
-
230
- _defineProperty(_assertThisInitialized(_this), "handleSubmit", function () {
231
- var _this$state5 = _this.state,
232
- url = _this$state5.url,
233
- selectedIndex = _this$state5.selectedIndex,
234
- items = _this$state5.items;
235
- var intl = _this.props.intl;
236
- var selectedItem = items[selectedIndex];
237
-
238
- if (_this.isUrlPopulatedWithSelectedItem()) {
239
- return _this.handleInsert(selectedItem.url, selectedItem.name, 'typeAhead');
240
- }
241
-
242
- var normalized = normalizeUrl(url);
243
-
244
- if (normalized) {
245
- return _this.handleInsert(normalized, null, 'manual');
246
- }
247
-
248
- return _this.setState({
249
- invalidUrl: intl.formatMessage(messages.linkInvalid)
150
+ }, [items]);
151
+ var handleMouseLeaveResultItem = useCallback(function (objectId) {
152
+ var index = findIndex(items, function (item) {
153
+ return item.objectId === objectId;
154
+ }); // This is to avoid updating index that was set by other mouseenter event
155
+
156
+ if (activeIndex === index) {
157
+ dispatch({
158
+ activeIndex: -1
250
159
  });
251
- });
252
-
253
- _defineProperty(_assertThisInitialized(_this), "handleKeyDown", function (event) {
254
- var _this$state6 = _this.state,
255
- items = _this$state6.items,
256
- activeIndex = _this$state6.activeIndex;
257
- var keyCode = event.keyCode;
258
- var KEY_CODE_ARROW_DOWN = 40;
259
- var KEY_CODE_ARROW_UP = 38;
260
-
261
- if (!items || !items.length) {
262
- return;
263
- }
264
-
265
- var updatedIndex = activeIndex;
266
-
267
- switch (keyCode) {
268
- case KEY_CODE_ARROW_DOWN:
269
- // down
270
- event.preventDefault();
271
- updatedIndex = (activeIndex + 1) % items.length;
272
- break;
273
-
274
- case KEY_CODE_ARROW_UP:
275
- // up
276
- event.preventDefault();
277
- updatedIndex = activeIndex > 0 ? activeIndex - 1 : items.length - 1;
278
- break;
279
- }
280
-
281
- if ([KEY_CODE_ARROW_DOWN, KEY_CODE_ARROW_UP].includes(keyCode) && items[updatedIndex]) {
282
- _this.setState({
283
- activeIndex: updatedIndex,
284
- selectedIndex: updatedIndex,
285
- url: items[updatedIndex].url,
286
- invalidUrl: null
287
- });
288
- }
289
- });
290
-
291
- _defineProperty(_assertThisInitialized(_this), "handleCancel", function () {
292
- var onCancel = _this.props.onCancel;
293
-
294
- if (onCancel) {
295
- onCancel();
160
+ }
161
+ }, [activeIndex, items]);
162
+ var handleInsert = useCallback(function (url, title, inputType) {
163
+ onSubmit(_objectSpread({
164
+ url: url,
165
+ displayText: displayText || null,
166
+ title: title || null,
167
+ meta: {
168
+ inputMethod: inputType
296
169
  }
170
+ }, inputType === 'manual' ? {
171
+ rawUrl: state.url
172
+ } : {}));
173
+ }, [displayText, onSubmit, state.url]);
174
+ var handleSelected = useCallback(function (objectId) {
175
+ var selectedItem = items.find(function (item) {
176
+ return item.objectId === objectId;
297
177
  });
298
178
 
299
- _defineProperty(_assertThisInitialized(_this), "updateTextInput", function (displayText) {
300
- _this.setState({
301
- displayText: displayText
302
- });
303
- });
304
-
305
- _this.state = {
306
- selectedIndex: -1,
307
- activeIndex: -1,
308
- isLoading: false,
309
- isEditing: !!props.url,
310
- url: normalizeUrl(props.url) || '',
311
- displayText: props.displayText || '',
312
- items: [],
313
- invalidUrl: null,
314
- activePlugin: 0
315
- };
316
- /* Cache functions */
317
-
318
- _this.handleClearUrl = _this.createClearHandler('url');
319
- _this.handleClearDisplayText = _this.createClearHandler('displayText');
320
- return _this;
321
- }
322
-
323
- _createClass(LinkPicker, [{
324
- key: "componentDidMount",
325
- value: function componentDidMount() {
326
- this.loadInitialLinkSearchResult();
327
- }
328
- }, {
329
- key: "loadInitialLinkSearchResult",
330
- value: function loadInitialLinkSearchResult() {
331
- var url = this.state.url;
332
-
333
- if (!url) {
334
- this.updateResults();
335
- }
179
+ if (selectedItem) {
180
+ var _url = selectedItem.url,
181
+ name = selectedItem.name;
182
+ handleInsert(_url, name, 'typeAhead');
336
183
  }
337
- }, {
338
- key: "getActivePlugin",
339
- value: function getActivePlugin() {
340
- if (!this.props.plugins || this.props.plugins.length === 0) {
341
- return null;
342
- }
184
+ }, [handleInsert, items]);
185
+ var handleSubmit = useCallback(function () {
186
+ var selectedItem = items[selectedIndex];
343
187
 
344
- return this.props.plugins[this.state.activePlugin];
188
+ if (isSelectedItem) {
189
+ return handleInsert(selectedItem.url, selectedItem.name, 'typeAhead');
345
190
  }
346
- /**
347
- * Interface between link picker and plugins to update results
348
- */
349
-
350
- }, {
351
- key: "updateResults",
352
- value: function () {
353
- var _updateResults = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
354
- var _this2 = this;
355
-
356
- var queryVersion, activePlugin, updates, _loop, _ret;
357
-
358
- return _regeneratorRuntime.wrap(function _callee2$(_context3) {
359
- while (1) {
360
- switch (_context3.prev = _context3.next) {
361
- case 0:
362
- queryVersion = ++this.queryVersion;
363
- activePlugin = this.getActivePlugin();
364
-
365
- if (activePlugin) {
366
- _context3.next = 4;
367
- break;
368
- }
369
-
370
- return _context3.abrupt("return");
371
-
372
- case 4:
373
- updates = activePlugin.resolve({
374
- query: this.state.url
375
- });
376
- this.setState({
377
- isLoading: true,
378
- items: []
379
- });
380
- _context3.prev = 6;
381
- _loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() {
382
- var done, value, _result$done, result;
383
-
384
- return _regeneratorRuntime.wrap(function _loop$(_context2) {
385
- while (1) {
386
- switch (_context2.prev = _context2.next) {
387
- case 0:
388
- done = false;
389
- value = void 0;
390
-
391
- if (!(updates instanceof Promise)) {
392
- _context2.next = 9;
393
- break;
394
- }
395
-
396
- done = true;
397
- _context2.next = 6;
398
- return updates;
399
191
 
400
- case 6:
401
- value = _context2.sent;
402
- _context2.next = 14;
403
- break;
192
+ var normalized = normalizeUrl(url);
404
193
 
405
- case 9:
406
- _context2.next = 11;
407
- return updates.next();
408
-
409
- case 11:
410
- result = _context2.sent;
411
- done = (_result$done = result.done) !== null && _result$done !== void 0 ? _result$done : false;
412
- value = result.value;
413
-
414
- case 14:
415
- if (!(queryVersion !== _this2.queryVersion)) {
416
- _context2.next = 16;
417
- break;
418
- }
419
-
420
- return _context2.abrupt("return", {
421
- v: void 0
422
- });
423
-
424
- case 16:
425
- _this2.setState(function (prev) {
426
- return {
427
- items: limit(value.data),
428
- isLoading: !done
429
- };
430
- });
431
-
432
- if (!done) {
433
- _context2.next = 19;
434
- break;
435
- }
436
-
437
- return _context2.abrupt("return", {
438
- v: void 0
439
- });
440
-
441
- case 19:
442
- case "end":
443
- return _context2.stop();
444
- }
445
- }
446
- }, _loop);
447
- });
448
-
449
- case 8:
450
- if (!(queryVersion === this.queryVersion)) {
451
- _context3.next = 15;
452
- break;
453
- }
454
-
455
- return _context3.delegateYield(_loop(), "t0", 10);
456
-
457
- case 10:
458
- _ret = _context3.t0;
459
-
460
- if (!(_typeof(_ret) === "object")) {
461
- _context3.next = 13;
462
- break;
463
- }
464
-
465
- return _context3.abrupt("return", _ret.v);
466
-
467
- case 13:
468
- _context3.next = 8;
469
- break;
470
-
471
- case 15:
472
- _context3.next = 20;
473
- break;
474
-
475
- case 17:
476
- _context3.prev = 17;
477
- _context3.t1 = _context3["catch"](6);
478
- this.setState({
479
- isLoading: false
480
- });
481
-
482
- case 20:
483
- case "end":
484
- return _context3.stop();
485
- }
486
- }
487
- }, _callee2, this, [[6, 17]]);
488
- }));
489
-
490
- function updateResults() {
491
- return _updateResults.apply(this, arguments);
492
- }
493
-
494
- return updateResults;
495
- }()
496
- }, {
497
- key: "render",
498
- value: function render() {
499
- var _this3 = this;
500
-
501
- var _this$state7 = this.state,
502
- items = _this$state7.items,
503
- isLoading = _this$state7.isLoading,
504
- isEditing = _this$state7.isEditing,
505
- activeIndex = _this$state7.activeIndex,
506
- selectedIndex = _this$state7.selectedIndex,
507
- url = _this$state7.url,
508
- displayText = _this$state7.displayText;
509
- var formatMessage = this.props.intl.formatMessage;
510
- var activePlugin = this.getActivePlugin();
511
- var tabs = this.getTabs();
512
- var linkPlaceHolder = formatMessage(activePlugin ? messages.placeholder : messages.linkPlaceholder);
513
- var linkLabel = formatMessage(messages.linkLabel);
514
- var linkTextLabel = formatMessage(messages.linkTextLabel);
515
- var linkTextPlaceHolder = formatMessage(messages.linkTextPlaceholder);
516
- var screenReaderDescriptionId = 'search-recent-links-field-description';
517
- var linkSearchListId = 'link-picker-search-list';
518
- var ariaActiveDescendant = selectedIndex > -1 ? "link-search-list-item-".concat(selectedIndex) : ''; // Added workaround with a screen reader Announcer specifically for VoiceOver + Safari
519
- // as the Aria design pattern for combobox does not work in this case
520
- // for details: https://a11y-internal.atlassian.net/browse/AK-740
194
+ if (normalized) {
195
+ return handleInsert(normalized, null, 'manual');
196
+ }
521
197
 
522
- var screenReaderText = browser.safari && this.getScreenReaderText();
523
- var searchIcon = activePlugin && jsx("span", {
524
- css: searchIconStyles,
525
- "data-testid": "link-picker-search-icon"
526
- }, jsx(EditorSearchIcon, {
527
- size: "medium",
528
- label: ''
529
- }));
530
- var noResults = activePlugin && !isLoading && !items.length && !isSafeUrl(url);
531
- return jsx("div", {
532
- "data-testid": "link-picker",
533
- css: rootContainerStyles
534
- }, tabs.length > 0 && jsx(Tabs, {
535
- id: "link-picker-tabs",
536
- testId: "link-picker-tabs",
537
- onChange: function onChange(activePlugin) {
538
- return _this3.setState({
539
- activePlugin: activePlugin
540
- }, function () {
541
- _this3.updateResults();
542
- });
543
- }
544
- }, jsx(TabList, null, tabs.map(function (tab) {
545
- var label = tab.tabTitle;
546
- return jsx(Tab, {
547
- key: label,
548
- testId: "link-picker-tab"
549
- }, label);
550
- }))), screenReaderText && jsx(Announcer, {
551
- ariaLive: "assertive",
552
- text: screenReaderText,
553
- ariaRelevant: "additions",
554
- delay: 250
555
- }), jsx(VisuallyHidden, null, jsx("span", {
556
- "aria-hidden": "true",
557
- id: screenReaderDescriptionId
558
- }, formatMessage(messages.searchLinkAriaDescription))), jsx(PanelTextInput, {
559
- name: "link",
560
- testId: "link-url",
561
- role: "combobox",
562
- label: linkLabel,
563
- placeholder: linkPlaceHolder,
564
- value: url,
565
- autoFocus: true,
566
- autoComplete: "off",
567
- elemBeforeInput: searchIcon,
568
- onSubmit: this.handleSubmit,
569
- onChange: this.updateInput,
570
- onKeyDown: this.handleKeyDown,
571
- onClear: this.handleClearUrl,
572
- error: this.state.invalidUrl,
573
- clearLabel: formatMessage(messages.clearLink),
574
- "aria-controls": linkSearchListId,
575
- "aria-autocomplete": "list",
576
- "aria-activedescendant": ariaActiveDescendant,
577
- "aria-describedby": screenReaderDescriptionId,
578
- "aria-expanded": true
579
- }), jsx(PanelTextInput, {
580
- name: "link-text",
581
- testId: "link-text",
582
- autoComplete: "off",
583
- label: linkTextLabel,
584
- placeholder: linkTextPlaceHolder,
585
- "aria-label": formatMessage(messages.linkAriaLabel),
586
- value: displayText,
587
- onSubmit: this.handleSubmit,
588
- onKeyDown: this.handleKeyDown,
589
- onChange: this.updateTextInput,
590
- onClear: this.handleClearDisplayText,
591
- clearLabel: formatMessage(messages.clearText)
592
- }), !!items.length && jsx("div", {
593
- css: listTitleStyles,
594
- id: "link-picker-list-title",
595
- "data-testid": "link-picker-list-title"
596
- }, formatMessage(this.state.url ? messages.titleResults : messages.titleRecentlyViewed)), jsx(VisuallyHidden, null, jsx("span", {
597
- "aria-live": "polite",
598
- "aria-atomic": "true",
599
- id: "fabric.smartcard.linkpicker.suggested.results"
600
- }, url && formatMessage(messages.searchLinkResults, {
601
- count: items.length
602
- }))), jsx(LinkSearchList, {
603
- ariaLabelledBy: "link-picker-list-title",
604
- ariaControls: "fabric.smartcard.linkpicker.suggested.results",
605
- id: linkSearchListId,
606
- role: "listbox",
607
- items: items,
608
- isLoading: isLoading,
609
- selectedIndex: selectedIndex,
610
- activeIndex: activeIndex,
611
- onSelect: this.handleSelected,
612
- onMouseEnter: this.handleMouseEnterResultItem,
613
- onMouseLeave: this.handleMouseLeaveResultItem
614
- }), noResults && jsx(LinkSearchNoResults, {
615
- testId: "link-search-no-results",
616
- header: formatMessage(messages.noResults),
617
- description: formatMessage(messages.noResultsDescription)
618
- }), jsx(FormFooter, {
619
- align: "end"
620
- }, jsx(ButtonGroup, null, jsx(Button, {
621
- appearance: "default",
622
- onClick: this.handleCancel,
623
- testId: "link-picker-cancel-button"
624
- }, formatMessage(messages.cancelButton)), jsx(Button, {
625
- type: "submit",
626
- appearance: "primary",
627
- onClick: this.handleSubmit,
628
- testId: "link-picker-insert-button"
629
- }, formatMessage(isEditing ? messages.saveButton : messages.insertButton)))));
198
+ return dispatch({
199
+ invalidUrl: true
200
+ });
201
+ }, [handleInsert, isSelectedItem, items, selectedIndex, url]);
202
+ var linkPlaceHolder = isActivePlugin ? messages.placeholder : messages.linkPlaceholder;
203
+ var linkListTitle = url ? messages.titleResults : messages.titleRecentlyViewed;
204
+ var insertButtonMsg = isEditing ? messages.saveButton : messages.insertButton;
205
+ var noResults = isActivePlugin && !isLoading && !items.length && !isSafeUrl(url);
206
+ var screenReaderDescriptionId = 'search-recent-links-field-description';
207
+ var linkSearchListId = 'link-picker-search-list';
208
+ var ariaActiveDescendant = selectedIndex > -1 ? "link-search-list-item-".concat(selectedIndex) : ''; // Added workaround with a screen reader Announcer specifically for VoiceOver + Safari
209
+ // as the Aria design pattern for combobox does not work in this case
210
+ // for details: https://a11y-internal.atlassian.net/browse/AK-740
211
+
212
+ var screenReaderText = browser.safari && getScreenReaderText(items, selectedIndex, intl);
213
+ var searchIcon = isActivePlugin && jsx("span", {
214
+ css: searchIconStyles,
215
+ "data-testid": testIds.searchIcon
216
+ }, jsx(EditorSearchIcon, {
217
+ size: "medium",
218
+ label: ''
219
+ }));
220
+ return jsx("div", {
221
+ "data-testid": testIds.linkPicker,
222
+ css: rootContainerStyles
223
+ }, screenReaderText && jsx(Announcer, {
224
+ ariaLive: "assertive",
225
+ text: screenReaderText,
226
+ ariaRelevant: "additions",
227
+ delay: 250
228
+ }), jsx(VisuallyHidden, {
229
+ id: screenReaderDescriptionId
230
+ }, jsx(FormattedMessage, messages.searchLinkAriaDescription)), jsx(PanelTextInput, {
231
+ role: "combobox",
232
+ autoComplete: "off",
233
+ name: "url",
234
+ testId: testIds.urlInputField,
235
+ label: intl.formatMessage(messages.linkLabel),
236
+ placeholder: intl.formatMessage(linkPlaceHolder),
237
+ value: url,
238
+ autoFocus: true,
239
+ elemBeforeInput: searchIcon,
240
+ clearLabel: intl.formatMessage(messages.clearLink),
241
+ "aria-expanded": true,
242
+ "aria-autocomplete": "list",
243
+ "aria-controls": linkSearchListId,
244
+ "aria-activedescendant": ariaActiveDescendant,
245
+ "aria-describedby": screenReaderDescriptionId,
246
+ error: invalidUrl ? intl.formatMessage(messages.linkInvalid) : null,
247
+ onClear: handleClear,
248
+ onSubmit: handleSubmit,
249
+ onKeyDown: handleKeyDown,
250
+ onChange: handleChangeUrl
251
+ }), jsx(PanelTextInput, {
252
+ autoComplete: "off",
253
+ name: "displayText",
254
+ testId: testIds.textInputField,
255
+ value: displayText,
256
+ label: intl.formatMessage(messages.linkTextLabel),
257
+ placeholder: intl.formatMessage(messages.linkTextPlaceholder),
258
+ clearLabel: intl.formatMessage(messages.clearText),
259
+ "aria-label": intl.formatMessage(messages.linkAriaLabel),
260
+ onClear: handleClear,
261
+ onSubmit: handleSubmit,
262
+ onKeyDown: handleKeyDown,
263
+ onChange: handleChangeText
264
+ }), tabs.length > 0 && jsx("div", {
265
+ css: tabsWrapperStyles
266
+ }, jsx(Tabs, {
267
+ id: testIds.tabList,
268
+ testId: testIds.tabList,
269
+ onChange: function onChange(activeTab) {
270
+ return dispatch({
271
+ selectedIndex: -1,
272
+ activeIndex: -1,
273
+ invalidUrl: false,
274
+ activeTab: activeTab
275
+ });
630
276
  }
631
- }]);
277
+ }, jsx(TabList, null, tabs.map(function (tab) {
278
+ return jsx(Tab, {
279
+ key: tab.tabTitle,
280
+ testId: testIds.tabItem
281
+ }, tab.tabTitle);
282
+ })))), !!items.length && jsx("div", {
283
+ css: listTitleStyles,
284
+ id: testIds.resultListTitle,
285
+ "data-testid": testIds.resultListTitle
286
+ }, jsx(FormattedMessage, linkListTitle)), jsx(VisuallyHidden, {
287
+ id: "fabric.smartcard.linkpicker.suggested.results"
288
+ }, url && jsx(FormattedMessage, _extends({}, messages.searchLinkResults, {
289
+ values: {
290
+ count: items.length
291
+ },
292
+ "aria-live": "polite",
293
+ "aria-atomic": "true"
294
+ }))), jsx(LinkSearchList, {
295
+ ariaLabelledBy: testIds.resultListTitle,
296
+ ariaControls: "fabric.smartcard.linkpicker.suggested.results",
297
+ id: linkSearchListId,
298
+ role: "listbox",
299
+ items: items,
300
+ isLoading: isLoading,
301
+ selectedIndex: selectedIndex,
302
+ activeIndex: activeIndex,
303
+ onSelect: handleSelected,
304
+ onMouseEnter: handleMouseEnterResultItem,
305
+ onMouseLeave: handleMouseLeaveResultItem
306
+ }), noResults && jsx(LinkSearchNoResults, {
307
+ testId: testIds.emptyResultPage,
308
+ header: intl.formatMessage(messages.noResults),
309
+ description: intl.formatMessage(messages.noResultsDescription)
310
+ }), jsx(FormFooter, {
311
+ align: "end"
312
+ }, jsx(ButtonGroup, null, jsx(Button, {
313
+ appearance: "default",
314
+ onClick: onCancel,
315
+ testId: testIds.cancelButton
316
+ }, intl.formatMessage(messages.cancelButton)), jsx(Button, {
317
+ type: "submit",
318
+ appearance: "primary",
319
+ onClick: handleSubmit,
320
+ testId: testIds.insertButton
321
+ }, intl.formatMessage(insertButtonMsg)))));
322
+ }
632
323
 
633
- return LinkPicker;
634
- }(PureComponent);
324
+ export default LinkPicker;
325
+
326
+ function getScreenReaderText(items, selectedIndex, intl) {
327
+ if (items.length && selectedIndex > -1) {
328
+ var _items$selectedIndex = items[selectedIndex],
329
+ name = _items$selectedIndex.name,
330
+ container = _items$selectedIndex.container,
331
+ lastUpdatedDate = _items$selectedIndex.lastUpdatedDate,
332
+ lastViewedDate = _items$selectedIndex.lastViewedDate;
333
+ var date = transformTimeStamp(intl, lastViewedDate, lastUpdatedDate);
334
+ var formattedDate = [date === null || date === void 0 ? void 0 : date.pageAction, date === null || date === void 0 ? void 0 : date.dateString, date === null || date === void 0 ? void 0 : date.timeSince].filter(Boolean).join(' ');
335
+ return [name, container, formattedDate].filter(Boolean).join(', ');
336
+ }
337
+ }
635
338
 
636
339
  function findIndex(array, predicate) {
637
340
  var index = -1;
@@ -644,10 +347,4 @@ function findIndex(array, predicate) {
644
347
  return false;
645
348
  });
646
349
  return index;
647
- }
648
-
649
- function limit(items) {
650
- return items.slice(0, RECENT_SEARCH_LIST_SIZE);
651
- }
652
-
653
- export default injectIntl(LinkPicker);
350
+ }