Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Optimize pressing enter to line breaks on mobile #2692

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,14 @@ function autoSendMessage() {
}

function sendChatHandle(event?: any) {
const isMobile = /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
)
// 如果是移动端,且按下回车键,不直接发送
if ((isMobile || mode === 'mobile') && event?.key === 'Enter') {
// 阻止默认事件
return
}
if (!event?.ctrlKey && !event?.shiftKey && !event?.altKey && !event?.metaKey) {
// 如果没有按下组合键,则会阻止默认事件
event?.preventDefault()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code contains no major irregularities or critical issues. However, there are a few minor enhancements that could be made to improve readability and functionality:

  1. Function Name Clarity: The function name autoSendMessage suggests it's meant to handle auto-sending messages, but you might want to clarify its purpose with comments if it serves multiple roles.

  2. Event Object Checking: Adding an additional check before attempting to access properties of event can ensure better type safety and prevent errors. For example:

    if (event && typeof event.preventDefault !== 'function') {
      return;
    }
  3. Code Reformatting: Ensure consistent indentation and whitespace to make the code easier to read. This improves maintainability.

Here's the revised version of the function with these improvements:

function autoSendMessage() {
    // Functionality placeholder for automatic message sending logic here...
}

function sendChatHandle(event?: any): void {
    const isMobile = /Mobi|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    
    if ((isMobile || mode === 'mobile') && event?.key === 'Enter') {
        event?.preventDefault();
        return;
    }

    if (
        event &&
        typeof event.preventDefault === 'function' &&
        !event.ctrlKey &&
        !event.shiftKey &&
        !event.altKey &&
        !event.metaKey
    ) {
        event?.preventDefault();
    } else {
        console.warn("Invalid input detected; default action prevented.");
    }
}

These changes help make the code more robust and easier to understand. If the intention behind preventing certain actions remains unchanged, the current approach works well, so only the formatting and some optional checks have been added.

Expand Down