1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

Audio title editing

This commit is contained in:
Fedor Katurov 2020-03-16 16:52:16 +07:00
parent da47da9d6a
commit fff3770b1e
7 changed files with 304 additions and 242 deletions

View file

@ -28,9 +28,21 @@ const AudioGrid: FC<IProps> = ({ files, setFiles, locked }) => {
[setFiles, files] [setFiles, files]
); );
const onTitleChange = useCallback(
(changeId: IFile['id'], title: IFile['metadata']['title']) => {
setFiles(
files.map(file =>
file && file.id === changeId ? { ...file, metadata: { ...file.metadata, title } } : file
)
);
},
[setFiles, files]
);
return ( return (
<SortableAudioGrid <SortableAudioGrid
onDrop={onDrop} onDrop={onDrop}
onTitleChange={onTitleChange}
onSortEnd={onMove} onSortEnd={onMove}
axis="xy" axis="xy"
items={files} items={files}

View file

@ -12,17 +12,19 @@ const SortableAudioGrid = SortableContainer(
items, items,
locked, locked,
onDrop, onDrop,
onTitleChange,
}: { }: {
items: IFile[]; items: IFile[];
locked: IUploadStatus[]; locked: IUploadStatus[];
onDrop: (file_id: IFile['id']) => void; onDrop: (file_id: IFile['id']) => void;
onTitleChange: (file_id: IFile['id'], title: IFile['metadata']['title']) => void;
}) => ( }) => (
<div className={styles.grid}> <div className={styles.grid}>
{items {items
.filter(file => file && file.id) .filter(file => file && file.id)
.map((file, index) => ( .map((file, index) => (
<SortableImageGridItem key={file.id} index={index} collection={0}> <SortableImageGridItem key={file.id} index={index} collection={0}>
<AudioPlayer file={file} onDrop={onDrop} nonInteractive /> <AudioPlayer file={file} onDrop={onDrop} onTitleChange={onTitleChange} isEditing />
</SortableImageGridItem> </SortableImageGridItem>
))} ))}

View file

@ -1,4 +1,4 @@
import React, { useCallback, useState, useEffect, memo } from 'react'; import React, { useCallback, useState, useEffect, memo, useMemo } from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { selectPlayer } from '~/redux/player/selectors'; import { selectPlayer } from '~/redux/player/selectors';
import * as PLAYER_ACTIONS from '~/redux/player/actions'; import * as PLAYER_ACTIONS from '~/redux/player/actions';
@ -8,6 +8,7 @@ import { Player, IPlayerProgress } from '~/utils/player';
import classNames from 'classnames'; import classNames from 'classnames';
import * as styles from './styles.scss'; import * as styles from './styles.scss';
import { Icon } from '~/components/input/Icon'; import { Icon } from '~/components/input/Icon';
import { InputText } from '~/components/input/InputText';
const mapStateToProps = state => ({ const mapStateToProps = state => ({
player: selectPlayer(state), player: selectPlayer(state),
@ -23,15 +24,17 @@ const mapDispatchToProps = {
type Props = ReturnType<typeof mapStateToProps> & type Props = ReturnType<typeof mapStateToProps> &
typeof mapDispatchToProps & { typeof mapDispatchToProps & {
file: IFile; file: IFile;
nonInteractive?: boolean; isEditing?: boolean;
onDrop?: (id: IFile['id']) => void; onDrop?: (id: IFile['id']) => void;
onTitleChange?: (file_id: IFile['id'], title: IFile['metadata']['title']) => void;
}; };
const AudioPlayerUnconnected = memo( const AudioPlayerUnconnected = memo(
({ ({
file, file,
onDrop, onDrop,
nonInteractive, isEditing,
onTitleChange,
player: { file: current, status }, player: { file: current, status },
playerSetFileAndPlay, playerSetFileAndPlay,
playerPlay, playerPlay,
@ -46,7 +49,7 @@ const AudioPlayerUnconnected = memo(
}); });
const onPlay = useCallback(() => { const onPlay = useCallback(() => {
if (nonInteractive) return; if (isEditing) return;
if (current && current.id === file.id) { if (current && current.id === file.id) {
if (status === PLAYER_STATES.PLAYING) return playerPause(); if (status === PLAYER_STATES.PLAYING) return playerPause();
@ -54,7 +57,7 @@ const AudioPlayerUnconnected = memo(
} }
playerSetFileAndPlay(file); playerSetFileAndPlay(file);
}, [file, current, status, playerPlay, playerPause, playerSetFileAndPlay, nonInteractive]); }, [file, current, status, playerPlay, playerPause, playerSetFileAndPlay, isEditing]);
const onProgress = useCallback( const onProgress = useCallback(
({ detail }: { detail: IPlayerProgress }) => { ({ detail }: { detail: IPlayerProgress }) => {
@ -80,6 +83,21 @@ const AudioPlayerUnconnected = memo(
onDrop(file.id); onDrop(file.id);
}, [file, onDrop]); }, [file, onDrop]);
const title = useMemo(
() =>
(file.metadata &&
(file.metadata.title ||
[file.metadata.id3artist, file.metadata.id3title].filter(el => el).join(' - '))) ||
file.orig_name ||
'',
[file.metadata]
);
const onRename = useCallback((val: string) => onTitleChange(file.id, val), [
onTitleChange,
file.id,
]);
useEffect(() => { useEffect(() => {
const active = current && current.id === file.id; const active = current && current.id === file.id;
setPlaying(current && current.id === file.id); setPlaying(current && current.id === file.id);
@ -91,11 +109,6 @@ const AudioPlayerUnconnected = memo(
}; };
}, [file, current, setPlaying, onProgress]); }, [file, current, setPlaying, onProgress]);
const title =
file.metadata &&
(file.metadata.title ||
[file.metadata.id3artist, file.metadata.id3title].filter(el => !!el).join(' - '));
return ( return (
<div onClick={onPlay} className={classNames(styles.wrap, { playing })}> <div onClick={onPlay} className={classNames(styles.wrap, { playing })}>
{onDrop && ( {onDrop && (
@ -112,13 +125,21 @@ const AudioPlayerUnconnected = memo(
)} )}
</div> </div>
<div className={styles.content}> {isEditing ? (
<div className={styles.title}>{title || 'Unknown'}</div> <div className={styles.input}>
<InputText value={title} handler={onRename} />
<div className={styles.progress} onClick={onSeek}>
<div className={styles.bar} style={{ width: `${progress.progress}%` }} />
</div> </div>
</div> ) : (
<div className={styles.content}>
<div className={styles.title}>
<div className={styles.title}>{title || 'Unknown'}</div>
</div>
<div className={styles.progress} onClick={onSeek}>
<div className={styles.bar} style={{ width: `${progress.progress}%` }} />
</div>
</div>
)}
</div> </div>
); );
} }

View file

@ -130,3 +130,9 @@
height: 20px; height: 20px;
} }
} }
.input {
flex: 1;
box-sizing: border-box;
padding: 0 48px 0 0;
}

View file

@ -1,4 +1,4 @@
import React, { FC, useCallback, KeyboardEventHandler, useEffect, useMemo } from 'react'; import React, { FC, useCallback, KeyboardEventHandler, useEffect, useMemo, memo } from 'react';
import { Textarea } from '~/components/input/Textarea'; import { Textarea } from '~/components/input/Textarea';
import * as styles from './styles.scss'; import * as styles from './styles.scss';
import { Filler } from '~/components/containers/Filler'; import { Filler } from '~/components/containers/Filler';
@ -41,244 +41,263 @@ type IProps = ReturnType<typeof mapStateToProps> &
is_before?: boolean; is_before?: boolean;
}; };
const CommentFormUnconnected: FC<IProps> = ({ const CommentFormUnconnected: FC<IProps> = memo(
node: { comment_data, is_sending_comment }, ({
uploads: { statuses, files }, node: { comment_data, is_sending_comment },
id, uploads: { statuses, files },
is_before = false, id,
nodePostComment, is_before = false,
nodeSetCommentData, nodePostComment,
uploadUploadFiles, nodeSetCommentData,
nodeCancelCommentEdit, uploadUploadFiles,
}) => { nodeCancelCommentEdit,
const onInputChange = useCallback( }) => {
event => { const onInputChange = useCallback(
event.preventDefault(); event => {
event.preventDefault();
if (!event.target.files || !event.target.files.length) return; if (!event.target.files || !event.target.files.length) return;
const items: IFileWithUUID[] = Array.from(event.target.files).map( const items: IFileWithUUID[] = Array.from(event.target.files).map(
(file: File): IFileWithUUID => ({ (file: File): IFileWithUUID => ({
file, file,
temp_id: uuid(), temp_id: uuid(),
subject: UPLOAD_SUBJECTS.COMMENT, subject: UPLOAD_SUBJECTS.COMMENT,
target: UPLOAD_TARGETS.COMMENTS, target: UPLOAD_TARGETS.COMMENTS,
type: getFileType(file), type: getFileType(file),
}) })
); );
const temps = items.map(file => file.temp_id); const temps = items.map(file => file.temp_id);
nodeSetCommentData( nodeSetCommentData(
id, id,
assocPath(['temp_ids'], [...comment_data[id].temp_ids, ...temps], comment_data[id]) assocPath(['temp_ids'], [...comment_data[id].temp_ids, ...temps], comment_data[id])
); );
uploadUploadFiles(items); uploadUploadFiles(items);
}, },
[uploadUploadFiles, comment_data, id, nodeSetCommentData] [uploadUploadFiles, comment_data, id, nodeSetCommentData]
);
const onInput = useCallback<InputHandler>(
text => {
nodeSetCommentData(id, assocPath(['text'], text, comment_data[id]));
},
[nodeSetCommentData, comment_data, id]
);
useEffect(() => {
const temp_ids = (comment_data && comment_data[id] && comment_data[id].temp_ids) || [];
const added_files = temp_ids
.map(temp_uuid => statuses[temp_uuid] && statuses[temp_uuid].uuid)
.map(el => !!el && files[el])
.filter(el => !!el && !comment_data[id].files.some(file => file && file.id === el.id));
const filtered_temps = temp_ids.filter(
temp_id =>
statuses[temp_id] &&
(!statuses[temp_id].uuid || !added_files.some(file => file.id === statuses[temp_id].uuid))
); );
if (added_files.length) { const onInput = useCallback<InputHandler>(
nodeSetCommentData(id, { text => {
...comment_data[id], nodeSetCommentData(id, assocPath(['text'], text, comment_data[id]));
temp_ids: filtered_temps, },
files: [...comment_data[id].files, ...added_files], [nodeSetCommentData, comment_data, id]
}); );
}
}, [statuses, files]);
const comment = comment_data[id]; useEffect(() => {
const temp_ids = (comment_data && comment_data[id] && comment_data[id].temp_ids) || [];
const added_files = temp_ids
.map(temp_uuid => statuses[temp_uuid] && statuses[temp_uuid].uuid)
.map(el => !!el && files[el])
.filter(el => !!el && !comment_data[id].files.some(file => file && file.id === el.id));
const is_uploading_files = useMemo(() => comment.temp_ids.length > 0, [comment.temp_ids]); const filtered_temps = temp_ids.filter(
temp_id =>
const onSubmit = useCallback( statuses[temp_id] &&
event => { (!statuses[temp_id].uuid || !added_files.some(file => file.id === statuses[temp_id].uuid))
if (event) event.preventDefault();
if (is_uploading_files || is_sending_comment) return;
nodePostComment(id, is_before);
},
[nodePostComment, id, is_before, is_uploading_files, is_sending_comment]
);
const onKeyDown = useCallback<KeyboardEventHandler<HTMLTextAreaElement>>(
({ ctrlKey, key }) => {
if (!!ctrlKey && key === 'Enter') onSubmit(null);
},
[onSubmit]
);
const images = useMemo(
() => comment.files.filter(file => file && file.type === UPLOAD_TYPES.IMAGE),
[comment.files]
);
const locked_images = useMemo(
() =>
comment.temp_ids
.filter(temp => statuses[temp] && statuses[temp].type === UPLOAD_TYPES.IMAGE)
.map(temp_id => statuses[temp_id]),
[statuses, comment.temp_ids]
);
const audios = useMemo(
() => comment.files.filter(file => file && file.type === UPLOAD_TYPES.AUDIO),
[comment.files]
);
const locked_audios = useMemo(
() =>
comment.temp_ids
.filter(temp => statuses[temp] && statuses[temp].type === UPLOAD_TYPES.AUDIO)
.map(temp_id => statuses[temp_id]),
[statuses, comment.temp_ids]
);
const onFileDrop = useCallback(
(file_id: IFile['id']) => {
nodeSetCommentData(
id,
assocPath(['files'], comment.files.filter(file => file.id != file_id), comment_data[id])
); );
},
[comment_data, id, nodeSetCommentData]
);
const onImageMove = useCallback( if (added_files.length) {
({ oldIndex, newIndex }: SortEnd) => { nodeSetCommentData(id, {
nodeSetCommentData( ...comment_data[id],
id, temp_ids: filtered_temps,
assocPath( files: [...comment_data[id].files, ...added_files],
['files'], });
[ }
...audios, }, [statuses, files]);
...(moveArrItem(oldIndex, newIndex, images.filter(file => !!file)) as IFile[]),
],
comment_data[id]
)
);
},
[images, audios]
);
const onAudioMove = useCallback( const comment = comment_data[id];
({ oldIndex, newIndex }: SortEnd) => {
nodeSetCommentData(
id,
assocPath(
['files'],
[
...images,
...(moveArrItem(oldIndex, newIndex, audios.filter(file => !!file)) as IFile[]),
],
comment_data[id]
)
);
},
[images, audios]
);
const onCancelEdit = useCallback(() => { const is_uploading_files = useMemo(() => comment.temp_ids.length > 0, [comment.temp_ids]);
nodeCancelCommentEdit(id);
}, [nodeCancelCommentEdit, comment.id]);
const placeholder = getRandomPhrase('SIMPLE'); const onSubmit = useCallback(
event => {
if (event) event.preventDefault();
if (is_uploading_files || is_sending_comment) return;
return ( nodePostComment(id, is_before);
<form onSubmit={onSubmit} className={styles.wrap}> },
<div className={styles.input}> [nodePostComment, id, is_before, is_uploading_files, is_sending_comment]
<Textarea );
value={comment.text}
handler={onInput}
onKeyDown={onKeyDown}
disabled={is_sending_comment}
placeholder={placeholder}
minRows={2}
/>
</div>
{(!!images.length || !!audios.length) && ( const onKeyDown = useCallback<KeyboardEventHandler<HTMLTextAreaElement>>(
<div className={styles.attaches}> ({ ctrlKey, key }) => {
{!!images.length && ( if (!!ctrlKey && key === 'Enter') onSubmit(null);
<SortableImageGrid },
onDrop={onFileDrop} [onSubmit]
onSortEnd={onImageMove} );
axis="xy"
items={images}
locked={locked_images}
pressDelay={50}
helperClass={styles.helper}
size={120}
/>
)}
{(!!audios.length || !!locked_audios.length) && ( const images = useMemo(
<SortableAudioGrid () => comment.files.filter(file => file && file.type === UPLOAD_TYPES.IMAGE),
items={audios} [comment.files]
onDrop={onFileDrop} );
onSortEnd={onAudioMove}
axis="y" const locked_images = useMemo(
locked={locked_audios} () =>
pressDelay={50} comment.temp_ids
helperClass={styles.helper} .filter(temp => statuses[temp] && statuses[temp].type === UPLOAD_TYPES.IMAGE)
/> .map(temp_id => statuses[temp_id]),
)} [statuses, comment.temp_ids]
);
const audios = useMemo(
() => comment.files.filter(file => file && file.type === UPLOAD_TYPES.AUDIO),
[comment.files]
);
const locked_audios = useMemo(
() =>
comment.temp_ids
.filter(temp => statuses[temp] && statuses[temp].type === UPLOAD_TYPES.AUDIO)
.map(temp_id => statuses[temp_id]),
[statuses, comment.temp_ids]
);
const onFileDrop = useCallback(
(fileId: IFile['id']) => {
nodeSetCommentData(
id,
assocPath(['files'], comment.files.filter(file => file.id != fileId), comment_data[id])
);
},
[comment_data, id, nodeSetCommentData]
);
const onTitleChange = useCallback(
(fileId: IFile['id'], title: IFile['metadata']['title']) => {
nodeSetCommentData(
id,
assocPath(
['files'],
comment.files.map(file =>
file.id === fileId ? { ...file, metadata: { ...file.metadata, title } } : file
),
comment_data[id]
)
);
},
[comment_data, id, nodeSetCommentData]
);
const onImageMove = useCallback(
({ oldIndex, newIndex }: SortEnd) => {
nodeSetCommentData(
id,
assocPath(
['files'],
[
...audios,
...(moveArrItem(oldIndex, newIndex, images.filter(file => !!file)) as IFile[]),
],
comment_data[id]
)
);
},
[images, audios]
);
const onAudioMove = useCallback(
({ oldIndex, newIndex }: SortEnd) => {
nodeSetCommentData(
id,
assocPath(
['files'],
[
...images,
...(moveArrItem(oldIndex, newIndex, audios.filter(file => !!file)) as IFile[]),
],
comment_data[id]
)
);
},
[images, audios]
);
const onCancelEdit = useCallback(() => {
nodeCancelCommentEdit(id);
}, [nodeCancelCommentEdit, comment.id]);
const placeholder = getRandomPhrase('SIMPLE');
return (
<form onSubmit={onSubmit} className={styles.wrap}>
<div className={styles.input}>
<Textarea
value={comment.text}
handler={onInput}
onKeyDown={onKeyDown}
disabled={is_sending_comment}
placeholder={placeholder}
minRows={2}
/>
</div> </div>
)}
<Group horizontal className={styles.buttons}> {(!!images.length || !!audios.length) && (
<ButtonGroup> <div className={styles.attaches}>
<Button iconLeft="photo" size="small" color="gray" iconOnly> {!!images.length && (
<input type="file" onInput={onInputChange} multiple accept="image/*" /> <SortableImageGrid
</Button> onDrop={onFileDrop}
onSortEnd={onImageMove}
axis="xy"
items={images}
locked={locked_images}
pressDelay={50}
helperClass={styles.helper}
size={120}
/>
)}
<Button iconRight="audio" size="small" color="gray" iconOnly> {(!!audios.length || !!locked_audios.length) && (
<input type="file" onInput={onInputChange} multiple accept="audio/*" /> <SortableAudioGrid
</Button> items={audios}
</ButtonGroup> onDrop={onFileDrop}
onTitleChange={onTitleChange}
<Filler /> onSortEnd={onAudioMove}
axis="y"
{(is_sending_comment || is_uploading_files) && <LoaderCircle size={20} />} locked={locked_audios}
pressDelay={50}
{id !== 0 && ( helperClass={styles.helper}
<Button size="small" color="link" type="button" onClick={onCancelEdit}> />
Отмена )}
</Button> </div>
)} )}
<Button <Group horizontal className={styles.buttons}>
size="small" <ButtonGroup>
color="gray" <Button iconLeft="photo" size="small" color="gray" iconOnly>
iconRight={id === 0 ? 'enter' : 'check'} <input type="file" onInput={onInputChange} multiple accept="image/*" />
disabled={is_sending_comment || is_uploading_files} </Button>
>
{id === 0 ? 'Сказать' : 'Сохранить'} <Button iconRight="audio" size="small" color="gray" iconOnly>
</Button> <input type="file" onInput={onInputChange} multiple accept="audio/*" />
</Group> </Button>
</form> </ButtonGroup>
);
}; <Filler />
{(is_sending_comment || is_uploading_files) && <LoaderCircle size={20} />}
{id !== 0 && (
<Button size="small" color="link" type="button" onClick={onCancelEdit}>
Отмена
</Button>
)}
<Button
size="small"
color="gray"
iconRight={id === 0 ? 'enter' : 'check'}
disabled={is_sending_comment || is_uploading_files}
>
{id === 0 ? 'Сказать' : 'Сохранить'}
</Button>
</Group>
</form>
);
}
);
const CommentForm = connect( const CommentForm = connect(
mapStateToProps, mapStateToProps,

View file

@ -64,6 +64,7 @@ export interface IFile {
node_id?: UUID; node_id?: UUID;
name: string; name: string;
orig_name: string;
path: string; path: string;
full_path: string; full_path: string;
url: string; url: string;

View file

@ -19,13 +19,14 @@ export const EMPTY_FILE: IFile = {
user_id: null, user_id: null,
node_id: null, node_id: null,
name: 'mario-collage-800x450.jpg', name: '',
path: '/wp-content/uploads/2017/09/', orig_name: '',
full_path: '/wp-content/uploads/2017/09/mario-collage-800x450.jpg', path: '',
url: 'https://cdn.arstechnica.net/wp-content/uploads/2017/09/mario-collage-800x450.jpg', full_path: '',
size: 2400000, url: '',
type: 'image', size: 0,
mime: 'image/jpeg', type: null,
mime: '',
}; };
export const EMPTY_UPLOAD_STATUS: IUploadStatus = { export const EMPTY_UPLOAD_STATUS: IUploadStatus = {