-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change design of edit media modal in web UI (#33516)
- Loading branch information
Showing
42 changed files
with
919 additions
and
900 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { List as ImmutableList, Map as ImmutableMap } from 'immutable'; | ||
|
||
import { apiUpdateMedia } from 'mastodon/api/compose'; | ||
import type { ApiMediaAttachmentJSON } from 'mastodon/api_types/media_attachments'; | ||
import type { MediaAttachment } from 'mastodon/models/media_attachment'; | ||
import { createDataLoadingThunk } from 'mastodon/store/typed_functions'; | ||
|
||
type SimulatedMediaAttachmentJSON = ApiMediaAttachmentJSON & { | ||
unattached?: boolean; | ||
}; | ||
|
||
const simulateModifiedApiResponse = ( | ||
media: MediaAttachment, | ||
params: { description?: string; focus?: string }, | ||
): SimulatedMediaAttachmentJSON => { | ||
const [x, y] = (params.focus ?? '').split(','); | ||
|
||
const data = { | ||
...media.toJS(), | ||
...params, | ||
meta: { | ||
focus: { | ||
x: parseFloat(x ?? '0'), | ||
y: parseFloat(y ?? '0'), | ||
}, | ||
}, | ||
} as unknown as SimulatedMediaAttachmentJSON; | ||
|
||
return data; | ||
}; | ||
|
||
export const changeUploadCompose = createDataLoadingThunk( | ||
'compose/changeUpload', | ||
async ( | ||
{ | ||
id, | ||
...params | ||
}: { | ||
id: string; | ||
description: string; | ||
focus: string; | ||
}, | ||
{ getState }, | ||
) => { | ||
const media = ( | ||
(getState().compose as ImmutableMap<string, unknown>).get( | ||
'media_attachments', | ||
) as ImmutableList<MediaAttachment> | ||
).find((item) => item.get('id') === id); | ||
|
||
// Editing already-attached media is deferred to editing the post itself. | ||
// For simplicity's sake, fake an API reply. | ||
if (media && !media.get('unattached')) { | ||
return new Promise<SimulatedMediaAttachmentJSON>((resolve) => { | ||
resolve(simulateModifiedApiResponse(media, params)); | ||
}); | ||
} | ||
|
||
return apiUpdateMedia(id, params); | ||
}, | ||
(media: SimulatedMediaAttachmentJSON) => { | ||
return { | ||
media, | ||
attached: typeof media.unattached !== 'undefined' && !media.unattached, | ||
}; | ||
}, | ||
{ | ||
useLoadingBar: false, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { apiRequestPut } from 'mastodon/api'; | ||
import type { ApiMediaAttachmentJSON } from 'mastodon/api_types/media_attachments'; | ||
|
||
export const apiUpdateMedia = ( | ||
id: string, | ||
params?: { description?: string; focus?: string }, | ||
) => apiRequestPut<ApiMediaAttachmentJSON>(`v1/media/${id}`, params); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,70 @@ | ||
import { useCallback, useState } from 'react'; | ||
import { useCallback, useState, forwardRef } from 'react'; | ||
|
||
interface Props { | ||
src: string; | ||
key: string; | ||
alt?: string; | ||
lang?: string; | ||
width: number; | ||
height: number; | ||
onClick?: () => void; | ||
width?: number; | ||
height?: number; | ||
onClick?: React.MouseEventHandler; | ||
onMouseDown?: React.MouseEventHandler; | ||
onTouchStart?: React.TouchEventHandler; | ||
} | ||
|
||
export const GIFV: React.FC<Props> = ({ | ||
src, | ||
alt, | ||
lang, | ||
width, | ||
height, | ||
onClick, | ||
}) => { | ||
const [loading, setLoading] = useState(true); | ||
export const GIFV = forwardRef<HTMLVideoElement, Props>( | ||
( | ||
{ src, alt, lang, width, height, onClick, onMouseDown, onTouchStart }, | ||
ref, | ||
) => { | ||
const [loading, setLoading] = useState(true); | ||
|
||
const handleLoadedData: React.ReactEventHandler<HTMLVideoElement> = | ||
useCallback(() => { | ||
const handleLoadedData = useCallback(() => { | ||
setLoading(false); | ||
}, [setLoading]); | ||
|
||
const handleClick: React.MouseEventHandler = useCallback( | ||
(e) => { | ||
if (onClick) { | ||
const handleClick = useCallback( | ||
(e: React.MouseEvent) => { | ||
e.stopPropagation(); | ||
onClick(); | ||
} | ||
}, | ||
[onClick], | ||
); | ||
onClick?.(e); | ||
}, | ||
[onClick], | ||
); | ||
|
||
return ( | ||
<div className='gifv' style={{ position: 'relative' }}> | ||
{loading && ( | ||
<canvas | ||
width={width} | ||
height={height} | ||
return ( | ||
<div className='gifv'> | ||
{loading && ( | ||
<canvas | ||
role='button' | ||
tabIndex={0} | ||
aria-label={alt} | ||
title={alt} | ||
lang={lang} | ||
onClick={handleClick} | ||
/> | ||
)} | ||
|
||
<video | ||
ref={ref} | ||
src={src} | ||
role='button' | ||
tabIndex={0} | ||
aria-label={alt} | ||
title={alt} | ||
lang={lang} | ||
width={width} | ||
height={height} | ||
muted | ||
loop | ||
autoPlay | ||
playsInline | ||
onClick={handleClick} | ||
onLoadedData={handleLoadedData} | ||
onMouseDown={onMouseDown} | ||
onTouchStart={onTouchStart} | ||
/> | ||
)} | ||
</div> | ||
); | ||
}, | ||
); | ||
|
||
<video | ||
src={src} | ||
role='button' | ||
tabIndex={0} | ||
aria-label={alt} | ||
title={alt} | ||
lang={lang} | ||
muted | ||
loop | ||
autoPlay | ||
playsInline | ||
onClick={handleClick} | ||
onLoadedData={handleLoadedData} | ||
style={{ position: loading ? 'absolute' : 'static', top: 0, left: 0 }} | ||
/> | ||
</div> | ||
); | ||
}; | ||
GIFV.displayName = 'GIFV'; |
Oops, something went wrong.