Skip to content

Commit

Permalink
fix: support meta+enter send key in macos platform (#160)
Browse files Browse the repository at this point in the history
Co-Authored-By: Minghan Zhang <[email protected]>
  • Loading branch information
Sh1n3zZ and zmh-program committed Jun 21, 2024
1 parent b07d421 commit 0ddd2d2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
9 changes: 4 additions & 5 deletions app/src/components/home/assemblies/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useSelector } from "react-redux";
import { senderSelector } from "@/store/settings.ts";
import { blobEvent } from "@/events/blob.ts";
import { cn } from "@/components/ui/lib/utils.ts";
import { isEnter } from "@/utils/base.ts";
import { isEnter, withCtrl, withShift } from "@/utils/base.ts";

type ChatInputProps = {
className?: string;
Expand Down Expand Up @@ -47,7 +47,7 @@ function ChatInput({
// on Enter, clear the input
// on Ctrl + Enter or Shift + Enter, keep the input

if (!e.ctrlKey && !e.shiftKey) {
if (!withCtrl(e) && !withShift(e)) {
e.preventDefault();
onEnterPressed();
} else {
Expand All @@ -67,9 +67,8 @@ function ChatInput({
onValueChange(input.value);
}
} else {
// on Enter, keep the input
// on Ctrl + Enter, clear the input
if (e.ctrlKey) {
// on Enter, keep the input & on Ctrl + Enter, send the input
if (withCtrl(e)) {
e.preventDefault();
onEnterPressed();
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ export function isEnter<T extends HTMLElement>(
return e.key === "Enter" && e.keyCode != 229;
}

export function withCtrl<T extends HTMLElement>(
e: React.KeyboardEvent<T> | KeyboardEvent,
): boolean {
// if platform is Mac, use Command instead of Ctrl
return e.ctrlKey || e.metaKey;
}

export function withShift<T extends HTMLElement>(
e: React.KeyboardEvent<T> | KeyboardEvent,
): boolean {
return e.shiftKey;
}

export function resetJsArray<T>(arr: T[], target: T[]): T[] {
/**
* this function is used to reset an array to another array without changing the *pointer
Expand Down

0 comments on commit 0ddd2d2

Please sign in to comment.