Skip to content

Commit

Permalink
feat: autoplay videos when previewing (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara authored May 23, 2022
1 parent 6401da2 commit ce39fe5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
65 changes: 36 additions & 29 deletions app/assets/javascripts/Components/Files/FilePreviewModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DialogContent, DialogOverlay } from '@reach/dialog'
import { addToast, ToastType } from '@standardnotes/stylekit'
import { NoPreviewIllustration } from '@standardnotes/icons'
import { FunctionComponent } from 'preact'
import { useCallback, useEffect, useRef, useState } from 'preact/hooks'
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks'
import { getFileIconComponent } from '@/Components/AttachedFilesPopover/PopoverFileItem'
import { Button } from '@/Components/Button/Button'
import { Icon } from '@/Components/Icon/Icon'
Expand Down Expand Up @@ -87,34 +87,46 @@ export const FilePreviewModal: FunctionComponent<Props> = observer(({ applicatio
}
}, [currentFile, getObjectUrl, objectUrl])

const keyDownHandler = (event: KeyboardEvent) => {
if (event.key !== KeyboardKey.Left && event.key !== KeyboardKey.Right) {
return
}
const keyDownHandler = useCallback(
(event: KeyboardEvent) => {
if (event.key !== KeyboardKey.Left && event.key !== KeyboardKey.Right) {
return
}

event.preventDefault()
event.preventDefault()

const currentFileIndex = otherFiles.findIndex((fileFromArray) => fileFromArray.uuid === currentFile.uuid)
const currentFileIndex = otherFiles.findIndex((fileFromArray) => fileFromArray.uuid === currentFile.uuid)

switch (event.key) {
case KeyboardKey.Left: {
const previousFileIndex = currentFileIndex - 1 >= 0 ? currentFileIndex - 1 : otherFiles.length - 1
const previousFile = otherFiles[previousFileIndex]
if (previousFile) {
setCurrentFile(previousFile)
switch (event.key) {
case KeyboardKey.Left: {
const previousFileIndex = currentFileIndex - 1 >= 0 ? currentFileIndex - 1 : otherFiles.length - 1
const previousFile = otherFiles[previousFileIndex]
if (previousFile) {
setCurrentFile(previousFile)
}
break
}
break
}
case KeyboardKey.Right: {
const nextFileIndex = currentFileIndex + 1 < otherFiles.length ? currentFileIndex + 1 : 0
const nextFile = otherFiles[nextFileIndex]
if (nextFile) {
setCurrentFile(nextFile)
case KeyboardKey.Right: {
const nextFileIndex = currentFileIndex + 1 < otherFiles.length ? currentFileIndex + 1 : 0
const nextFile = otherFiles[nextFileIndex]
if (nextFile) {
setCurrentFile(nextFile)
}
break
}
break
}
}
}
},
[currentFile.uuid, otherFiles, setCurrentFile],
)

const IconComponent = useMemo(
() =>
getFileIconComponent(
application.iconsController.getIconForFileType(currentFile.mimeType),
'w-6 h-6 flex-shrink-0',
),
[application.iconsController, currentFile.mimeType],
)

return (
<DialogOverlay
Expand All @@ -139,12 +151,7 @@ export const FilePreviewModal: FunctionComponent<Props> = observer(({ applicatio
onKeyDown={keyDownHandler}
>
<div className="flex items-center">
<div className="w-6 h-6">
{getFileIconComponent(
application.iconsController.getIconForFileType(currentFile.mimeType),
'w-6 h-6 flex-shrink-0',
)}
</div>
<div className="w-6 h-6">{IconComponent}</div>
<span className="ml-3 font-medium">{currentFile.name}</span>
</div>
<div className="flex items-center">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const PreviewComponent: FunctionComponent<Props> = ({ file, objectUrl })
}

if (file.mimeType.startsWith('video/')) {
return <video className="w-full h-full" src={objectUrl} controls />
return <video className="w-full h-full" src={objectUrl} controls autoPlay />
}

if (file.mimeType.startsWith('audio/')) {
Expand Down

0 comments on commit ce39fe5

Please sign in to comment.