forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMentions.tsx
305 lines (284 loc) · 8.92 KB
/
Mentions.tsx
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
import type { CSSProperties, ExtractPropTypes } from 'vue';
import {
toRef,
watchEffect,
defineComponent,
provide,
withDirectives,
ref,
reactive,
onUpdated,
nextTick,
computed,
} from 'vue';
import classNames from '../../_util/classNames';
import KeyCode from '../../_util/KeyCode';
import { initDefaultProps } from '../../_util/props-util';
import {
getBeforeSelectionText,
getLastMeasureIndex,
replaceWithMeasure,
setInputSelection,
} from './util';
import KeywordTrigger from './KeywordTrigger';
import { vcMentionsProps, defaultProps } from './mentionsProps';
import type { OptionProps } from './Option';
import MentionsContextKey from './MentionsContext';
import antInputDirective from '../../_util/antInputDirective';
import omit from '../../_util/omit';
import type { EventHandler } from '../../_util/EventInterface';
export type MentionsProps = Partial<ExtractPropTypes<typeof vcMentionsProps>>;
function noop() {}
export default defineComponent({
compatConfig: { MODE: 3 },
name: 'Mentions',
inheritAttrs: false,
props: initDefaultProps(vcMentionsProps, defaultProps),
emits: ['change', 'select', 'search', 'focus', 'blur', 'pressenter'],
setup(props, { emit, attrs, expose, slots }) {
const measure = ref(null);
const textarea = ref(null);
const focusId = ref();
const state = reactive({
value: props.value || '',
measuring: false,
measureLocation: 0,
measureText: null,
measurePrefix: '',
activeIndex: 0,
isFocus: false,
});
watchEffect(() => {
state.value = props.value;
});
const triggerChange = (val: string) => {
emit('change', val);
};
const onChange: EventHandler = ({ target: { value } }) => {
triggerChange(value);
};
const startMeasure = (measureText: string, measurePrefix: string, measureLocation: number) => {
Object.assign(state, {
measuring: true,
measureText,
measurePrefix,
measureLocation,
activeIndex: 0,
});
};
const stopMeasure = (callback?: () => void) => {
Object.assign(state, {
measuring: false,
measureLocation: 0,
measureText: null,
});
callback?.();
};
const onKeyDown = (event: KeyboardEvent) => {
const { which } = event;
// Skip if not measuring
if (!state.measuring) {
return;
}
if (which === KeyCode.UP || which === KeyCode.DOWN) {
// Control arrow function
const optionLen = options.value.length;
const offset = which === KeyCode.UP ? -1 : 1;
const newActiveIndex = (state.activeIndex + offset + optionLen) % optionLen;
state.activeIndex = newActiveIndex;
event.preventDefault();
} else if (which === KeyCode.ESC) {
stopMeasure();
} else if (which === KeyCode.ENTER) {
// Measure hit
event.preventDefault();
if (!options.value.length) {
stopMeasure();
return;
}
const option = options.value[state.activeIndex];
selectOption(option);
}
};
const onKeyUp = (event: KeyboardEvent) => {
const { key, which } = event;
const { measureText: prevMeasureText, measuring } = state;
const { prefix, validateSearch } = props;
const target = event.target as HTMLTextAreaElement;
if ((target as any).composing) {
return;
}
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, props);
const matchOption = !!getOptions(measureText).length;
if (validateMeasure) {
if (
key === measurePrefix ||
key === 'Shift' ||
measuring ||
(measureText !== prevMeasureText && matchOption)
) {
startMeasure(measureText, measurePrefix, measureIndex);
}
} else if (measuring) {
// Stop if measureText is invalidate
stopMeasure();
}
/**
* We will trigger `onSearch` to developer since they may use for async update.
* If met `space` means user finished searching.
*/
if (validateMeasure) {
emit('search', measureText, measurePrefix);
}
} else if (measuring) {
stopMeasure();
}
};
const onPressEnter = event => {
if (!state.measuring) {
emit('pressenter', event);
}
};
const onInputFocus = (event: Event) => {
onFocus(event);
};
const onInputBlur = (event: Event) => {
onBlur(event);
};
const onFocus = (event: Event) => {
clearTimeout(focusId.value);
const { isFocus } = state;
if (!isFocus && event) {
emit('focus', event);
}
state.isFocus = true;
};
const onBlur = (event: Event) => {
focusId.value = setTimeout(() => {
state.isFocus = false;
stopMeasure();
emit('blur', event);
}, 100);
};
const selectOption = (option: OptionProps) => {
const { split } = props;
const { value: mentionValue = '' } = option;
const { text, selectionLocation } = replaceWithMeasure(state.value, {
measureLocation: state.measureLocation,
targetText: mentionValue,
prefix: state.measurePrefix,
selectionStart: textarea.value.selectionStart,
split,
});
triggerChange(text);
stopMeasure(() => {
// We need restore the selection position
setInputSelection(textarea.value, selectionLocation);
});
emit('select', option, state.measurePrefix);
};
const setActiveIndex = (activeIndex: number) => {
state.activeIndex = activeIndex;
};
const getOptions = (measureText?: string) => {
const targetMeasureText = measureText || state.measureText || '';
const { filterOption } = props;
const list = props.options.filter((option: OptionProps) => {
/** Return all result if `filterOption` is false. */
if (!!filterOption === false) {
return true;
}
return (filterOption as Function)(targetMeasureText, option);
});
return list;
};
const options = computed(() => {
return getOptions();
});
const focus = () => {
textarea.value.focus();
};
const blur = () => {
textarea.value.blur();
};
expose({ blur, focus });
provide(MentionsContextKey, {
activeIndex: toRef(state, 'activeIndex'),
setActiveIndex,
selectOption,
onFocus,
onBlur,
loading: toRef(props, 'loading'),
});
onUpdated(() => {
nextTick(() => {
if (state.measuring) {
measure.value.scrollTop = textarea.value.scrollTop;
}
});
});
return () => {
const { measureLocation, measurePrefix, measuring } = state;
const { prefixCls, placement, transitionName, getPopupContainer, direction, ...restProps } =
props;
const { class: className, style, ...otherAttrs } = attrs;
const inputProps = omit(restProps, [
'value',
'prefix',
'split',
'validateSearch',
'filterOption',
'options',
'loading',
]);
const textareaProps = {
...inputProps,
...otherAttrs,
onChange: noop,
onSelect: noop,
value: state.value,
onInput: onChange,
onBlur: onInputBlur,
onKeydown: onKeyDown,
onKeyup: onKeyUp,
onFocus: onInputFocus,
onPressenter: onPressEnter,
};
return (
<div class={classNames(prefixCls, className)} style={style as CSSProperties}>
{withDirectives(<textarea ref={textarea} {...textareaProps} />, [[antInputDirective]])}
{measuring && (
<div ref={measure} class={`${prefixCls}-measure`}>
{state.value.slice(0, measureLocation)}
<KeywordTrigger
prefixCls={prefixCls}
transitionName={transitionName}
dropdownClassName={props.dropdownClassName}
placement={placement}
options={measuring ? options.value : []}
visible
direction={direction}
getPopupContainer={getPopupContainer}
v-slots={{ notFoundContent: slots.notFoundContent, option: slots.option }}
>
<span>{measurePrefix}</span>
</KeywordTrigger>
{state.value.slice(measureLocation + measurePrefix.length)}
</div>
)}
</div>
);
};
},
});