Skip to content
This repository was archived by the owner on May 12, 2026. It is now read-only.
Open
Show file tree
Hide file tree
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
63 changes: 63 additions & 0 deletions src/cloud/lib/hooks/sidebar/useCloudDnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { SerializedDocWithSupplemental } from '../../../interfaces/db/doc'
import { SidebarDragState } from '../../../../design/lib/dnd'
import { useToast } from '../../../../design/lib/stores/toast'
import { getMapFromEntityArray } from '../../../../design/lib/utils/array'
import { SerializedTeam } from '../../../interfaces/db/team'

const SUPPORTED_FILE_EXTENSIONS = ['.md', '.txt', '.html']
const FILE_ENCODING = 'utf-8'

export function useCloudDnd() {
const {
Expand All @@ -47,11 +51,17 @@ export function useCloudDnd() {
body: UpdateDocRequestBody
) => Promise<void>
) => {
const files = event.dataTransfer?.files
if (files != null && files.length > 0) {
return 'files'
}

const draggedResource = getDraggedResource(event)
if (draggedResource === null) {
return
}


if (draggedResource.type === 'folder') {
const folder = draggedResource.resource
await updateFolder(folder, {
Expand All @@ -72,6 +82,58 @@ export function useCloudDnd() {
[]
)

const dropFilesInWorkspace = useCallback(
async (
event: any,
workspaceId: string,
team: SerializedTeam,
createDoc: (
team: SerializedTeam,
body: { workspaceId: string; title: string; content?: string }
) => Promise<{ id: string }>
) => {
const files = event.dataTransfer?.files
if (files == null || files.length === 0) {
return
}

for (let i = 0; i < files.length; i++) {
const file = files[i]
const ext = getFileExtension(file.name)
if (!SUPPORTED_FILE_EXTENSIONS.includes(ext)) {
continue
}

try {
const content = await readFileAsText(file)
const title = file.name.replace(ext, '')
await createDoc(team, {
workspaceId,
title,
content,
})
} catch (err) {
console.warn('Failed to create doc from file:', file.name, err)
}
}
},
[]
)

function getFileExtension(filename: string): string {
const lastDot = filename.lastIndexOf('.')
return lastDot >= 0 ? filename.slice(lastDot).toLowerCase() : ''
}

async function readFileAsText(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = () => resolve(reader.result as string)
reader.onerror = () => reject(reader.error)
reader.readAsText(file, FILE_ENCODING)
})
}

const dropInDocOrFolder = useCallback(
async (
event: any,
Expand Down Expand Up @@ -173,6 +235,7 @@ export function useCloudDnd() {
return {
dropInWorkspace,
dropInDocOrFolder,
dropFilesInWorkspace,
saveFolderTransferData,
saveDocTransferData,
clearDragTransferData,
Expand Down
12 changes: 10 additions & 2 deletions src/cloud/lib/hooks/sidebar/useCloudSidebarTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function useCloudSidebarTree() {
const {
dropInDocOrFolder,
dropInWorkspace,
dropFilesInWorkspace,
saveFolderTransferData,
saveDocTransferData,
clearDragTransferData,
Expand Down Expand Up @@ -262,8 +263,14 @@ export function useCloudSidebarTree() {
currentUserIsCoreMember
? {
dropIn: true,
onDrop: (event: any) =>
dropInWorkspace(event, wp.id, updateFolder, updateDoc),
onDrop: (event: any) => {
const files = event.dataTransfer?.files
if (files != null && files.length > 0) {
dropFilesInWorkspace(event, wp.id, team, createDoc)
} else {
dropInWorkspace(event, wp.id, updateFolder, updateDoc)
}
},
controls: [
{
icon: mdiTextBoxPlus,
Expand Down Expand Up @@ -1117,4 +1124,5 @@ type CloudTreeItem = {
onDragStart?: (event: any) => void
onDrop?: (event: any, position?: SidebarDragState) => void
onDragEnd?: (event: any) => void
onDropFiles?: (event: any) => void
}