Mentions.js
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
"use strict";
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _createSuper2 = _interopRequireDefault(require("@babel/runtime/helpers/createSuper"));
var _classnames = _interopRequireDefault(require("classnames"));
var _toArray = _interopRequireDefault(require("rc-util/lib/Children/toArray"));
var _KeyCode = _interopRequireDefault(require("rc-util/lib/KeyCode"));
var React = _interopRequireWildcard(require("react"));
var _rcTextarea = _interopRequireDefault(require("rc-textarea"));
var _KeywordTrigger = _interopRequireDefault(require("./KeywordTrigger"));
var _MentionsContext = require("./MentionsContext");
var _Option = _interopRequireDefault(require("./Option"));
var _util = require("./util");
var Mentions = /*#__PURE__*/function (_React$Component) {
(0, _inherits2.default)(Mentions, _React$Component);
var _super = (0, _createSuper2.default)(Mentions);
function Mentions(props) {
var _this;
(0, _classCallCheck2.default)(this, Mentions);
_this = _super.call(this, props);
_this.focusId = undefined;
_this.triggerChange = function (value) {
var onChange = _this.props.onChange;
if (!('value' in _this.props)) {
_this.setState({
value: value
});
}
if (onChange) {
onChange(value);
}
};
_this.onChange = function (_ref) {
var value = _ref.target.value;
_this.triggerChange(value);
}; // Check if hit the measure keyword
_this.onKeyDown = function (event) {
var which = event.which;
var _this$state = _this.state,
activeIndex = _this$state.activeIndex,
measuring = _this$state.measuring; // Skip if not measuring
if (!measuring) {
return;
}
if (which === _KeyCode.default.UP || which === _KeyCode.default.DOWN) {
// Control arrow function
var optionLen = _this.getOptions().length;
var offset = which === _KeyCode.default.UP ? -1 : 1;
var newActiveIndex = (activeIndex + offset + optionLen) % optionLen;
_this.setState({
activeIndex: newActiveIndex
});
event.preventDefault();
} else if (which === _KeyCode.default.ESC) {
_this.stopMeasure();
} else if (which === _KeyCode.default.ENTER) {
// Measure hit
event.preventDefault();
var options = _this.getOptions();
if (!options.length) {
_this.stopMeasure();
return;
}
var option = options[activeIndex];
_this.selectOption(option);
}
};
/**
* When to start measure:
* 1. When user press `prefix`
* 2. When measureText !== prevMeasureText
* - If measure hit
* - If measuring
*
* When to stop measure:
* 1. Selection is out of range
* 2. Contains `space`
* 3. ESC or select one
*/
_this.onKeyUp = function (event) {
var key = event.key,
which = event.which;
var _this$state2 = _this.state,
prevMeasureText = _this$state2.measureText,
measuring = _this$state2.measuring;
var _this$props = _this.props,
_this$props$prefix = _this$props.prefix,
prefix = _this$props$prefix === void 0 ? '' : _this$props$prefix,
onSearch = _this$props.onSearch,
validateSearch = _this$props.validateSearch;
var target = event.target;
var selectionStartText = (0, _util.getBeforeSelectionText)(target);
var _getLastMeasureIndex = (0, _util.getLastMeasureIndex)(selectionStartText, prefix),
measureIndex = _getLastMeasureIndex.location,
measurePrefix = _getLastMeasureIndex.prefix; // Skip if match the white key list
if ([_KeyCode.default.ESC, _KeyCode.default.UP, _KeyCode.default.DOWN, _KeyCode.default.ENTER].indexOf(which) !== -1) {
return;
}
if (measureIndex !== -1) {
var measureText = selectionStartText.slice(measureIndex + measurePrefix.length);
var validateMeasure = validateSearch(measureText, _this.props);
var matchOption = !!_this.getOptions(measureText).length;
if (validateMeasure) {
if (key === measurePrefix || key === 'Shift' || measuring || measureText !== prevMeasureText && matchOption) {
_this.startMeasure(measureText, measurePrefix, measureIndex);
}
} else if (measuring) {
// Stop if measureText is invalidate
_this.stopMeasure();
}
/**
* We will trigger `onSearch` to developer since they may use for async update.
* If met `space` means user finished searching.
*/
if (onSearch && validateMeasure) {
onSearch(measureText, measurePrefix);
}
} else if (measuring) {
_this.stopMeasure();
}
};
_this.onPressEnter = function (event) {
var measuring = _this.state.measuring;
var onPressEnter = _this.props.onPressEnter;
if (!measuring && onPressEnter) {
onPressEnter(event);
}
};
_this.onInputFocus = function (event) {
_this.onFocus(event);
};
_this.onInputBlur = function (event) {
_this.onBlur(event);
};
_this.onDropdownFocus = function () {
_this.onFocus();
};
_this.onDropdownBlur = function () {
_this.onBlur();
};
_this.onFocus = function (event) {
window.clearTimeout(_this.focusId);
var isFocus = _this.state.isFocus;
var onFocus = _this.props.onFocus;
if (!isFocus && event && onFocus) {
onFocus(event);
}
_this.setState({
isFocus: true
});
};
_this.onBlur = function (event) {
_this.focusId = window.setTimeout(function () {
var onBlur = _this.props.onBlur;
_this.setState({
isFocus: false
});
_this.stopMeasure();
if (onBlur) {
onBlur(event);
}
}, 0);
};
_this.selectOption = function (option) {
var _this$state3 = _this.state,
value = _this$state3.value,
measureLocation = _this$state3.measureLocation,
measurePrefix = _this$state3.measurePrefix;
var _this$props2 = _this.props,
split = _this$props2.split,
onSelect = _this$props2.onSelect;
var _option$value = option.value,
mentionValue = _option$value === void 0 ? '' : _option$value;
var _replaceWithMeasure = (0, _util.replaceWithMeasure)(value, {
measureLocation: measureLocation,
targetText: mentionValue,
prefix: measurePrefix,
selectionStart: _this.textarea.selectionStart,
split: split
}),
text = _replaceWithMeasure.text,
selectionLocation = _replaceWithMeasure.selectionLocation;
_this.triggerChange(text);
_this.stopMeasure(function () {
// We need restore the selection position
(0, _util.setInputSelection)(_this.textarea, selectionLocation);
});
if (onSelect) {
onSelect(option, measurePrefix);
}
};
_this.setActiveIndex = function (activeIndex) {
_this.setState({
activeIndex: activeIndex
});
};
_this.setTextAreaRef = function (element) {
var _element$resizableTex;
_this.textarea = element === null || element === void 0 ? void 0 : (_element$resizableTex = element.resizableTextArea) === null || _element$resizableTex === void 0 ? void 0 : _element$resizableTex.textArea;
};
_this.setMeasureRef = function (element) {
_this.measure = element;
};
_this.getOptions = function (measureText) {
var targetMeasureText = measureText || _this.state.measureText || '';
var _this$props3 = _this.props,
children = _this$props3.children,
filterOption = _this$props3.filterOption;
var list = (0, _toArray.default)(children).map(function (_ref2) {
var props = _ref2.props,
key = _ref2.key;
return (0, _objectSpread2.default)((0, _objectSpread2.default)({}, props), {}, {
key: key || props.value
});
}).filter(function (option) {
/** Return all result if `filterOption` is false. */
if (filterOption === false) {
return true;
}
return filterOption(targetMeasureText, option);
});
return list;
};
_this.state = {
value: props.defaultValue || props.value || '',
measuring: false,
measureLocation: 0,
measureText: null,
measurePrefix: '',
activeIndex: 0,
isFocus: false
};
return _this;
}
(0, _createClass2.default)(Mentions, [{
key: "componentDidUpdate",
value: function componentDidUpdate() {
var measuring = this.state.measuring; // Sync measure div top with textarea for rc-trigger usage
if (measuring) {
this.measure.scrollTop = this.textarea.scrollTop;
}
}
}, {
key: "startMeasure",
value: function startMeasure(measureText, measurePrefix, measureLocation) {
this.setState({
measuring: true,
measureText: measureText,
measurePrefix: measurePrefix,
measureLocation: measureLocation,
activeIndex: 0
});
}
}, {
key: "stopMeasure",
value: function stopMeasure(callback) {
this.setState({
measuring: false,
measureLocation: 0,
measureText: null
}, callback);
}
}, {
key: "focus",
value: function focus() {
this.textarea.focus();
}
}, {
key: "blur",
value: function blur() {
this.textarea.blur();
}
}, {
key: "render",
value: function render() {
var _this$state4 = this.state,
value = _this$state4.value,
measureLocation = _this$state4.measureLocation,
measurePrefix = _this$state4.measurePrefix,
measuring = _this$state4.measuring,
activeIndex = _this$state4.activeIndex;
var _this$props4 = this.props,
prefixCls = _this$props4.prefixCls,
placement = _this$props4.placement,
direction = _this$props4.direction,
transitionName = _this$props4.transitionName,
className = _this$props4.className,
style = _this$props4.style,
autoFocus = _this$props4.autoFocus,
notFoundContent = _this$props4.notFoundContent,
getPopupContainer = _this$props4.getPopupContainer,
restProps = (0, _objectWithoutProperties2.default)(_this$props4, ["prefixCls", "placement", "direction", "transitionName", "className", "style", "autoFocus", "notFoundContent", "getPopupContainer"]);
var inputProps = (0, _util.omit)(restProps, 'value', 'defaultValue', 'prefix', 'split', 'children', 'validateSearch', 'filterOption', 'onSelect', 'onSearch');
var options = measuring ? this.getOptions() : [];
return React.createElement("div", {
className: (0, _classnames.default)(prefixCls, className),
style: style
}, React.createElement(_rcTextarea.default, Object.assign({
autoFocus: autoFocus,
ref: this.setTextAreaRef,
value: value
}, inputProps, {
onChange: this.onChange,
onKeyDown: this.onKeyDown,
onKeyUp: this.onKeyUp,
onPressEnter: this.onPressEnter,
onFocus: this.onInputFocus,
onBlur: this.onInputBlur
})), measuring && React.createElement("div", {
ref: this.setMeasureRef,
className: "".concat(prefixCls, "-measure")
}, value.slice(0, measureLocation), React.createElement(_MentionsContext.MentionsContextProvider, {
value: {
notFoundContent: notFoundContent,
activeIndex: activeIndex,
setActiveIndex: this.setActiveIndex,
selectOption: this.selectOption,
onFocus: this.onDropdownFocus,
onBlur: this.onDropdownBlur
}
}, React.createElement(_KeywordTrigger.default, {
prefixCls: prefixCls,
transitionName: transitionName,
placement: placement,
direction: direction,
options: options,
visible: true,
getPopupContainer: getPopupContainer
}, React.createElement("span", null, measurePrefix))), value.slice(measureLocation + measurePrefix.length)));
}
}], [{
key: "getDerivedStateFromProps",
value: function getDerivedStateFromProps(props, prevState) {
var newState = {};
if ('value' in props && props.value !== prevState.value) {
newState.value = props.value || '';
}
return newState;
}
}]);
return Mentions;
}(React.Component);
Mentions.Option = _Option.default;
Mentions.defaultProps = {
prefixCls: 'rc-mentions',
prefix: '@',
split: ' ',
validateSearch: _util.validateSearch,
filterOption: _util.filterOption,
notFoundContent: 'Not Found',
rows: 1
};
var _default = Mentions;
exports.default = _default;