import type { InputInstance } from 'element-plus';
|
import getCaretCoordinates from 'textarea-caret';
|
import type { Ref } from 'vue';
|
import { computed, nextTick, ref } from 'vue';
|
import InputTip from '../inputTip/index.vue';
|
import { usePickHistory } from './usePickHistory';
|
import { useCompRef } from '/@/utils/types';
|
|
type useInputEventOption = {
|
props: any;
|
emit: any;
|
inputValue: Ref<string>;
|
inputRef: Ref<InputInstance>;
|
};
|
|
export const useInputEvent = (option: useInputEventOption) => {
|
const { props, emit, inputValue, inputRef } = option;
|
const inputTipRef = useCompRef(InputTip);
|
const { setUpQuestion, setDownQuestion } = usePickHistory({
|
msgList: computed(() => props.msgList),
|
inputValue: inputValue,
|
});
|
const keydownInput = (e) => {
|
if (props.isTalking) return;
|
const isEnterInput = !e.shiftKey && e.key == 'Enter';
|
const isDigitalInput = e.ctrlKey && e.code.startsWith('Digit');
|
const arrowUp = e.key === 'ArrowUp';
|
const arrowDown = e.key === 'ArrowDown';
|
if (isEnterInput || isDigitalInput || arrowUp || arrowDown) {
|
e.cancelBubble = true; //ie阻止冒泡行为
|
e.stopPropagation(); //Firefox阻止冒泡行为
|
e.preventDefault(); //取消事件的默认动作*换行
|
if (isEnterInput) {
|
//以下处理发送消息代码
|
emit('sendClick');
|
} else if (isDigitalInput) {
|
const num = Number(e.code.replace('Digit', ''));
|
inputTipRef.value.applyIndexItem(num - 1);
|
} else if (arrowUp) {
|
setUpQuestion();
|
} else if (arrowDown) {
|
setDownQuestion();
|
}
|
}
|
};
|
|
const inputText = (text) => {
|
nextTick(() => {
|
setTimeout(() => {
|
const container = inputRef.value.$el;
|
const textAreaEl = inputRef.value.$el.firstElementChild;
|
const caret = getCaretCoordinates(textAreaEl, textAreaEl.selectionEnd);
|
inputTipRef.value.triggerInputTip(text, {
|
left: caret.left,
|
bottom: container.offsetHeight,
|
});
|
}, 0);
|
});
|
};
|
|
return {
|
keydownInput,
|
inputText,
|
inputTipRef,
|
};
|
};
|