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 21 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
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 { getPlatform, getEngine } from '@/utils/platform';

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

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

const handleDragOver = (e: DragEvent) => {
e.preventDefault();
if (!e.dataTransfer?.items || e.dataTransfer.items.length === 0) return;

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

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

const handleDragEnter = (e: DragEvent) => {
e.preventDefault();
if (!e.dataTransfer?.items || e.dataTransfer.items.length === 0) return;

dragCounter.current += 1;
if (e.dataTransfer?.items && e.dataTransfer.items.length > 0) {
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) return;

// reset counter
dragCounter.current -= 1;
const isFile = e.dataTransfer.types.includes('Files');
if (disallowTextDrag || isFile) {
e.preventDefault();

if (dragCounter.current === 0) {
setIsDragging(false);
// reset counter
dragCounter.current -= 1;

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) return;

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

// get filesList
// TODO: support folder files upload
const files = e.dataTransfer?.files;
// reset counter
dragCounter.current = 0;

// upload files
uploadImages(files);
setIsDragging(false);

// 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
4 changes: 4 additions & 0 deletions src/utils/platform.ts
Expand Up @@ -15,6 +15,10 @@
return getParser().getResult().browser.name;
};

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

Check warning on line 20 in src/utils/platform.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/platform.ts#L19-L20

Added lines #L19 - L20 were not covered by tests

export const browserInfo = {
browser: getBrowser(),
isMobile: getParser().getDevice().type === 'mobile',
Expand Down