wujingjing
2025-04-10 8aa7ffddc511138d61d64029157c11cfccc5431d
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
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,
    };
};