@lobehub/chat 0.117.4 → 0.117.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ### [Version 0.117.5](https://github.com/lobehub/lobe-chat/compare/v0.117.4...v0.117.5)
6
+
7
+ <sup>Released on **2023-12-29**</sup>
8
+
9
+ #### 🐛 Bug Fixes
10
+
11
+ - **misc**: The input box is prone to losing focus.
12
+
13
+ <br/>
14
+
15
+ <details>
16
+ <summary><kbd>Improvements and Fixes</kbd></summary>
17
+
18
+ #### What's fixed
19
+
20
+ - **misc**: The input box is prone to losing focus, closes [#834](https://github.com/lobehub/lobe-chat/issues/834) ([26a42f6](https://github.com/lobehub/lobe-chat/commit/26a42f6))
21
+
22
+ </details>
23
+
24
+ <div align="right">
25
+
26
+ [![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)
27
+
28
+ </div>
29
+
5
30
  ### [Version 0.117.4](https://github.com/lobehub/lobe-chat/compare/v0.117.3...v0.117.4)
6
31
 
7
32
  <sup>Released on **2023-12-28**</sup>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/chat",
3
- "version": "0.117.4",
3
+ "version": "0.117.5",
4
4
  "description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
5
5
  "keywords": [
6
6
  "framework",
@@ -0,0 +1,85 @@
1
+ import { act, renderHook } from '@testing-library/react';
2
+ import { RefObject } from 'react';
3
+
4
+ import { useAutoFocus } from '../useAutoFocus';
5
+
6
+ enum ElType {
7
+ div,
8
+ input,
9
+ markdown,
10
+ }
11
+
12
+ // 模拟elementFromPoint方法
13
+ document.elementFromPoint = function (x) {
14
+ if (x === ElType.div) {
15
+ return document.createElement('div');
16
+ }
17
+
18
+ if (x === ElType.input) {
19
+ return document.createElement('input');
20
+ }
21
+
22
+ if (x === ElType.markdown) {
23
+ const markdownEl = document.createElement('article');
24
+ const markdownChildEl = document.createElement('p');
25
+ markdownEl.appendChild(markdownChildEl);
26
+ return markdownChildEl;
27
+ }
28
+
29
+ return null;
30
+ };
31
+
32
+ describe('useAutoFocus', () => {
33
+ it('should focus inputRef when mouseup event happens outside of input or markdown element', () => {
34
+ const inputRef = { current: { focus: vi.fn() } } as RefObject<any>;
35
+ renderHook(() => useAutoFocus(inputRef));
36
+
37
+ // Simulate a mousedown event on an element outside of input or markdown element
38
+ act(() => {
39
+ document.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: ElType.div }));
40
+ });
41
+
42
+ // Simulate a mouseup event
43
+ act(() => {
44
+ document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
45
+ });
46
+
47
+ expect(inputRef.current?.focus).toHaveBeenCalledTimes(1);
48
+ });
49
+
50
+ it('should not focus inputRef when mouseup event happens inside of input element', () => {
51
+ const inputRef = { current: { focus: vi.fn() } } as RefObject<any>;
52
+ renderHook(() => useAutoFocus(inputRef));
53
+
54
+ // Simulate a mousedown event on an input element
55
+ act(() => {
56
+ document.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, clientX: ElType.input }));
57
+ });
58
+
59
+ // Simulate a mouseup event
60
+ act(() => {
61
+ document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
62
+ });
63
+
64
+ expect(inputRef.current?.focus).not.toHaveBeenCalled();
65
+ });
66
+
67
+ it('should not focus inputRef when mouseup event happens inside of markdown element', () => {
68
+ const inputRef = { current: { focus: vi.fn() } } as RefObject<any>;
69
+ renderHook(() => useAutoFocus(inputRef));
70
+
71
+ // Simulate a mousedown event on an input element
72
+ act(() => {
73
+ document.dispatchEvent(
74
+ new MouseEvent('mousedown', { bubbles: true, clientX: ElType.markdown }),
75
+ );
76
+ });
77
+
78
+ // Simulate a mouseup event
79
+ act(() => {
80
+ document.dispatchEvent(new MouseEvent('mouseup', { bubbles: true }));
81
+ });
82
+
83
+ expect(inputRef.current?.focus).not.toHaveBeenCalled();
84
+ });
85
+ });
@@ -14,6 +14,7 @@ import { useChatInput } from '@/features/ChatInput/useChatInput';
14
14
 
15
15
  import DragUpload from './DragUpload';
16
16
  import { LocalFiles } from './LocalFiles';
17
+ import { useAutoFocus } from './useAutoFocus';
17
18
 
18
19
  const ChatInputDesktopLayout = memo(() => {
19
20
  const { t } = useTranslation('chat');
@@ -31,6 +32,8 @@ const ChatInputDesktopLayout = memo(() => {
31
32
  canUpload,
32
33
  } = useChatInput();
33
34
 
35
+ useAutoFocus(ref);
36
+
34
37
  const handleSizeChange = useCallback(
35
38
  (_: any, size: any) => {
36
39
  if (!size) return;
@@ -79,6 +82,7 @@ const ChatInputDesktopLayout = memo(() => {
79
82
  <>
80
83
  {canUpload && <DragUpload />}
81
84
  <ChatInputArea
85
+ autoFocus
82
86
  bottomAddons={buttonAddons}
83
87
  expand={expand}
84
88
  heights={{
@@ -0,0 +1,36 @@
1
+ import { TextAreaRef } from 'antd/es/input/TextArea';
2
+ import { RefObject, useEffect } from 'react';
3
+
4
+ export const useAutoFocus = (inputRef: RefObject<TextAreaRef>) => {
5
+ useEffect(() => {
6
+ let isInputOrMarkdown = false;
7
+
8
+ const onMousedown = (e: MouseEvent) => {
9
+ isInputOrMarkdown = false;
10
+ const element = document.elementFromPoint(e.clientX, e.clientY);
11
+ if (!element) return;
12
+ let currentElement: Element | null = element;
13
+ // 因为点击 Markdown 元素时,element 会是 article 标签的子元素
14
+ // 所以向上查找全局点击对象是否是 Markdown 或者 Input 元素
15
+ while (currentElement && !isInputOrMarkdown) {
16
+ isInputOrMarkdown = ['TEXTAREA', 'INPUT', 'ARTICLE'].includes(currentElement.tagName);
17
+ currentElement = currentElement.parentElement;
18
+ }
19
+ };
20
+
21
+ const onMouseup = () => {
22
+ // 因为有时候要复制 Markdown 里生成的内容,或者点击别的 Input
23
+ // 所以全局点击元素不是 Markdown 或者 Input 元素的话就聚焦
24
+ if (!isInputOrMarkdown) {
25
+ inputRef.current?.focus();
26
+ }
27
+ };
28
+
29
+ document.addEventListener('mousedown', onMousedown);
30
+ document.addEventListener('mouseup', onMouseup);
31
+ return () => {
32
+ document.removeEventListener('mousedown', onMousedown);
33
+ document.removeEventListener('mouseup', onMouseup);
34
+ };
35
+ }, []);
36
+ };