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

🐛 fix: dragging text mistakenly as image #2111

Merged
merged 27 commits into from May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
df9db56
fix drag
sxjeru Apr 20, 2024
6b9b4ad
Merge branch 'lobehub:main' into fork
sxjeru Apr 20, 2024
575edf9
fix
sxjeru Apr 21, 2024
2d04df9
fix
sxjeru Apr 21, 2024
9546398
fix image drag
sxjeru Apr 21, 2024
6cdc2db
Merge branch 'lobehub:main' into fork
sxjeru Apr 21, 2024
01da452
simplify
sxjeru Apr 21, 2024
9c77ef5
Merge branch 'fork' of https://github.com/sxjeru/lobe-chat into fork
sxjeru Apr 21, 2024
2a10f68
Merge branch 'lobehub:main' into fork
sxjeru Apr 23, 2024
9ac7da0
Restrict types of support browser
sxjeru Apr 25, 2024
fa1621c
Merge branch 'fork' of https://github.com/sxjeru/lobe-chat into fork
sxjeru Apr 25, 2024
7dfc52b
Merge branch 'main' into fork
sxjeru Apr 25, 2024
63c5d62
Merge branch 'main' into fork
sxjeru Apr 26, 2024
55421e1
update
sxjeru Apr 29, 2024
934febe
fix
sxjeru Apr 29, 2024
1ebc461
everything done
sxjeru Apr 29, 2024
669fc24
Merge branch 'main' into fork
sxjeru Apr 29, 2024
a00d251
Update DragUpload.tsx
sxjeru Apr 29, 2024
7ebecf9
refactor function
sxjeru Apr 29, 2024
995c43c
Merge remote-tracking branch 'upstream/main' into fork
sxjeru Apr 30, 2024
17f136a
Avoid nested if
sxjeru Apr 30, 2024
578e137
Merge branch 'main' into fork
sxjeru May 6, 2024
600655c
support all browser
sxjeru May 6, 2024
9096283
Merge branch 'main' into fork
arvinxx May 8, 2024
fb7cccf
Merge remote-tracking branch 'upstream/main' into fork
sxjeru May 8, 2024
d51bdb5
Merge branch 'fork' of https://github.com/sxjeru/lobe-chat into fork
sxjeru May 8, 2024
cd4bcdb
del getEngine()
sxjeru May 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 40 additions & 20 deletions src/app/chat/(desktop)/features/ChatInput/Footer/DragUpload.tsx
Expand Up @@ -11,6 +11,7 @@ import { agentSelectors } from '@/store/agent/selectors';
import { useFileStore } from '@/store/file';
import { useUserStore } from '@/store/user';
import { modelProviderSelectors } from '@/store/user/selectors';
import { allowTextDrag } from '@/utils/platform';

const useStyles = createStyles(({ css, token, stylish }) => {
return {
Expand Down Expand Up @@ -60,8 +61,15 @@ const useStyles = createStyles(({ css, token, stylish }) => {
};
});

const disallowTextDrag = !allowTextDrag();

const handleDragOver = (e: DragEvent) => {
e.preventDefault();
if (e.dataTransfer?.items && e.dataTransfer.items.length > 0) {
const isFile = e.dataTransfer.types.includes('Files'); // Webpage image drag
if (disallowTextDrag || isFile) {
e.preventDefault();
}
}
};

const DragUpload = memo(() => {
Expand Down Expand Up @@ -92,43 +100,55 @@ const DragUpload = memo(() => {
};

const handleDragEnter = (e: DragEvent) => {
e.preventDefault();

dragCounter.current += 1;
if (e.dataTransfer?.items && e.dataTransfer.items.length > 0) {
setIsDragging(true);
const isFile = e.dataTransfer.types.includes('Files');
if (disallowTextDrag || isFile) {
dragCounter.current += 1;
e.preventDefault();
setIsDragging(true);
}
}
};

const handleDragLeave = (e: DragEvent) => {
e.preventDefault();
if (e.dataTransfer?.items && e.dataTransfer.items.length > 0) {
const isFile = e.dataTransfer.types.includes('Files');
if (disallowTextDrag || isFile) {
e.preventDefault();

// reset counter
dragCounter.current -= 1;
// reset counter
dragCounter.current -= 1;

if (dragCounter.current === 0) {
setIsDragging(false);
if (dragCounter.current === 0) {
setIsDragging(false);
}
}
}
};

const handleDrop = async (e: DragEvent) => {
e.preventDefault();
// reset counter
dragCounter.current = 0;
if (e.dataTransfer?.items && e.dataTransfer.items.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

if 的风格,如果可以的话建议改成如果不匹配就 return 这种,而不是 if 里匹配再执行。

因为 if 里执行会导致括号层级加深一级,对查看代码上会有一定的语法干扰

Copy link
Contributor Author

@sxjeru sxjeru Apr 30, 2024

Choose a reason for hiding this comment

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

if 嵌套处理了一下。 17f136a

本身的判断没有改,因为以后可能计划实现拖拽图片链接也能上传,从而支持 macOS 和 Firefox 。
这样目前的判断条件是可用的,当然还需思考这个条件是否可以忽略,因为似乎不存在拖拽了但 items 为空的情况。

const isFile = e.dataTransfer.types.includes('Files');
if (disallowTextDrag || isFile) {
e.preventDefault();

setIsDragging(false);
// reset counter
dragCounter.current = 0;

// get filesList
// TODO: support folder files upload
const files = e.dataTransfer?.files;
setIsDragging(false);

// upload files
uploadImages(files);
// get filesList
// TODO: support folder files upload
const files = e.dataTransfer?.files;

// upload files
uploadImages(files);
}
}
};

const handlePaste = (event: ClipboardEvent) => {
// get files from clipboard

const files = event.clipboardData?.files;

uploadImages(files);
Expand Down
10 changes: 10 additions & 0 deletions src/utils/platform.ts
Expand Up @@ -15,10 +15,20 @@ export const getBrowser = () => {
return getParser().getResult().browser.name;
};

export const getEngine = () => {
return getParser().getEngine().name;
}

export const browserInfo = {
browser: getBrowser(),
isMobile: getParser().getDevice().type === 'mobile',
os: getParser().getOS().name,
};

export const isMacOS = () => getPlatform() === 'Mac OS';

// 文字拖拽仅支持 Windows/Linux - Chromium 系浏览器 (#2111)
export const allowTextDrag = () => {
sxjeru marked this conversation as resolved.
Show resolved Hide resolved
const platform = getPlatform();
return platform && /Linux|Windows/.test(platform) && getEngine() === 'Blink';
}