-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathMentions.jsx
323 lines (309 loc) · 8.7 KB
/
Mentions.jsx
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
import omit from 'omit.js';
import KeyCode from '../../_util/KeyCode';
import BaseMixin from '../../_util/BaseMixin';
import {
getSlots,
hasProp,
getOptionProps,
getListeners,
initDefaultProps,
} from '../../_util/props-util';
import warning from 'warning';
import {
getBeforeSelectionText,
getLastMeasureIndex,
replaceWithMeasure,
setInputSelection,
} from './util';
import KeywordTrigger from './KeywordTrigger';
import { vcMentionsProps, defaultProps } from './mentionsProps';
function noop() {}
const Mentions = {
name: 'Mentions',
mixins: [BaseMixin],
inheritAttrs: false,
model: {
prop: 'value',
event: 'change',
},
props: initDefaultProps(vcMentionsProps, defaultProps),
provide() {
return {
mentionsContext: this,
};
},
data() {
const { value = '', defaultValue = '' } = this.$props;
warning(this.$props.children, 'please children prop replace slots.default');
return {
_value: !hasProp(this, 'value') ? defaultValue : value,
measuring: false,
measureLocation: 0,
measureText: null,
measurePrefix: '',
activeIndex: 0,
isFocus: false,
};
},
watch: {
value(val) {
this.$data._value = val;
},
},
updated() {
this.$nextTick(() => {
const { measuring } = this.$data;
// Sync measure div top with textarea for rc-trigger usage
if (measuring) {
this.$refs.measure.scrollTop = this.$refs.textarea.scrollTop;
}
});
},
methods: {
triggerChange(value) {
const props = getOptionProps(this);
if (!('value' in props)) {
this.setState({ _value: value });
} else {
this.$forceUpdate();
}
this.$emit('change', value);
},
onChange({ target: { value, composing }, isComposing }) {
if (isComposing || composing) return;
this.triggerChange(value);
},
onKeyDown(event) {
const { which } = event;
const { activeIndex, measuring } = this.$data;
// Skip if not measuring
if (!measuring) {
return;
}
if (which === KeyCode.UP || which === KeyCode.DOWN) {
// Control arrow function
const optionLen = this.getOptions().length;
const offset = which === KeyCode.UP ? -1 : 1;
const newActiveIndex = (activeIndex + offset + optionLen) % optionLen;
this.setState({
activeIndex: newActiveIndex,
});
event.preventDefault();
} else if (which === KeyCode.ESC) {
this.stopMeasure();
} else if (which === KeyCode.ENTER) {
// Measure hit
const option = this.getOptions()[activeIndex];
this.selectOption(option);
event.preventDefault();
}
},
/**
* 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
*/
onKeyUp(event) {
const { key, which } = event;
const { measureText: prevMeasureText, measuring } = this.$data;
const { prefix = '', validateSearch } = this.$props;
const target = event.target;
const selectionStartText = getBeforeSelectionText(target);
const { location: measureIndex, prefix: measurePrefix } = getLastMeasureIndex(
selectionStartText,
prefix,
);
// Skip if match the white key list
if ([KeyCode.ESC, KeyCode.UP, KeyCode.DOWN, KeyCode.ENTER].indexOf(which) !== -1) {
return;
}
if (measureIndex !== -1) {
const measureText = selectionStartText.slice(measureIndex + measurePrefix.length);
const validateMeasure = validateSearch(measureText, this.$props);
const matchOption = !!this.getOptions(measureText).length;
if (validateMeasure) {
if (
key === measurePrefix ||
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 (validateMeasure) {
this.$emit('search', measureText, measurePrefix);
}
} else if (measuring) {
this.stopMeasure();
}
},
onInputFocus(event) {
this.onFocus(event);
},
onInputBlur(event) {
this.onBlur(event);
},
onDropdownFocus() {
this.onFocus();
},
onDropdownBlur() {
this.onBlur();
},
onFocus(event) {
window.clearTimeout(this.focusId);
const { isFocus } = this.$data;
if (!isFocus && event) {
this.$emit('focus', event);
}
this.setState({ isFocus: true });
},
onBlur(event) {
this.focusId = window.setTimeout(() => {
this.setState({ isFocus: false });
this.stopMeasure();
this.$emit('blur', event);
}, 0);
},
selectOption(option) {
const { _value: value, measureLocation, measurePrefix } = this.$data;
const { split } = this.$props;
const { value: mentionValue = '' } = option;
const { text, selectionLocation } = replaceWithMeasure(value, {
measureLocation,
targetText: mentionValue,
prefix: measurePrefix,
selectionStart: this.$refs.textarea.selectionStart,
split,
});
this.triggerChange(text);
this.stopMeasure(() => {
// We need restore the selection position
setInputSelection(this.$refs.textarea, selectionLocation);
});
this.$emit('select', option, measurePrefix);
},
setActiveIndex(activeIndex) {
this.setState({
activeIndex,
});
},
getOptions(measureText) {
const targetMeasureText = measureText || this.$data.measureText || '';
const { filterOption, children = [] } = this.$props;
const list = (Array.isArray(children) ? children : [children])
.map(item => {
const children = getSlots(item).default;
return { ...getOptionProps(item), children };
})
.filter(option => {
/** Return all result if `filterOption` is false. */
if (filterOption === false) {
return true;
}
return filterOption(targetMeasureText, option);
});
return list;
},
startMeasure(measureText, measurePrefix, measureLocation) {
this.setState({
measuring: true,
measureText,
measurePrefix,
measureLocation,
activeIndex: 0,
});
},
stopMeasure(callback) {
this.setState(
{
measuring: false,
measureLocation: 0,
measureText: null,
},
callback,
);
},
focus() {
this.$refs.textarea.focus();
},
blur() {
this.$refs.textarea.blur();
},
},
render() {
const { _value: value, measureLocation, measurePrefix, measuring } = this.$data;
const {
prefixCls,
placement,
transitionName,
autoFocus,
notFoundContent,
getPopupContainer,
...restProps
} = getOptionProps(this);
const inputProps = omit(restProps, [
'value',
'defaultValue',
'prefix',
'split',
'children',
'validateSearch',
'filterOption',
]);
const options = measuring ? this.getOptions() : [];
return (
<div class={prefixCls}>
<textarea
ref="textarea"
{...{
directives: [{ name: 'ant-input' }],
attrs: { ...inputProps, ...this.$attrs },
domProps: {
value,
},
on: {
...getListeners(this),
select: noop,
change: noop,
input: this.onChange,
keydown: this.onKeyDown,
keyup: this.onKeyUp,
blur: this.onInputBlur,
},
}}
/>
{measuring && (
<div ref="measure" class={`${prefixCls}-measure`}>
{value.slice(0, measureLocation)}
<KeywordTrigger
prefixCls={prefixCls}
transitionName={transitionName}
placement={placement}
options={options}
visible
getPopupContainer={getPopupContainer}
>
<span>{measurePrefix}</span>
</KeywordTrigger>
{value.slice(measureLocation + measurePrefix.length)}
</div>
)}
</div>
);
},
};
export default Mentions;