Skip to content

Commit

Permalink
feat(fileuploads): download files when element clicked
Browse files Browse the repository at this point in the history
Signed-off-by: grnd-alt <[email protected]>
  • Loading branch information
grnd-alt committed Dec 2, 2024
1 parent ed4f629 commit c6db63d
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/files/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ExcalidrawImperativeAPI,
} from '@excalidraw/excalidraw/types/types'
import { Collab } from '../collaboration/collab'
import type { FileId } from '@excalidraw/excalidraw/types/element/types'
import type { ExcalidrawElement, FileId } from '@excalidraw/excalidraw/types/element/types'

export class FileHandle {

Check failure on line 10 in src/files/files.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Block must be padded by blank lines
private collab: Collab
Expand Down Expand Up @@ -34,7 +34,33 @@ export class FileHandle {
this.filesDragEventListener(ev, excalidrawApi),
)
}
this.excalidrawApi.onPointerDown((tool, state, event) => {
this.excalidrawApi.onPointerDown((_, state, __) => {

Check failure on line 37 in src/files/files.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'__' is defined but never used
const clickedElement = this.getElementAt(state.lastCoords.x, state.lastCoords.y)
if (!clickedElement) {
return
}
this.downloadFile(clickedElement.customData?.meta)
})
}

private downloadFile(meta: any) {

Check failure on line 46 in src/files/files.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Unexpected any. Specify a different type
const blob = new Blob([meta.dataurl], { type: meta.type })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = meta.name
a.click()
URL.revokeObjectURL(url)
}

private getElementAt(px: number, py: number): ExcalidrawElement | undefined {
const elements = this.excalidrawApi.getSceneElements()
return elements.find((element) => {
const { x, y, width, height } = element
return (
px >= x && px <= x + width &&

Check failure on line 61 in src/files/files.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'&&' should be placed at the beginning of the line
py >= y && py <= y + height
)
})
}

Expand Down Expand Up @@ -62,27 +88,31 @@ export class FileHandle {
id: (Math.random() + 1).toString(36).substring(7) as FileId,
dataURL: fr.result as DataURL,
}
this.addCustomFileElement(constructedFile, file.name)
let meta = {

Check failure on line 91 in src/files/files.ts

View workflow job for this annotation

GitHub Actions / NPM lint

'meta' is never reassigned. Use 'const' instead
name: file.name, type: file.type, lastModified: file.lastModified, dataurl: fr.result

Check warning on line 92 in src/files/files.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
}
this.addCustomFileElement(constructedFile, meta)
}
}

private addCustomFileElement(constructedFile: BinaryFileData, filename: string) {
private addCustomFileElement(constructedFile: BinaryFileData, meta: any) {
this.collab.addFile(constructedFile)
const elements = this.excalidrawApi
.getSceneElementsIncludingDeleted()
.slice()
const newElements = convertToExcalidrawElements([
{
type: 'text',
text: filename,
customData: { filedata: { constructedFile } },
text: meta.name,
customData: { meta: meta },
groupIds: ['1'],
y: 0,
x: 0,
},
{
type: 'image',
fileId: 'placeholder_image' as FileId,
customData: { meta: meta },
groupIds: ['1'],
y: 0,
x: 0,
Expand Down

0 comments on commit c6db63d

Please sign in to comment.