2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
import * as React from 'react';
|
2020-02-07 19:37:04 +00:00
|
|
|
import PQueue from 'p-queue';
|
2019-12-17 20:25:57 +00:00
|
|
|
import {
|
|
|
|
SortableContainer,
|
|
|
|
SortableElement,
|
|
|
|
SortEndHandler,
|
|
|
|
} from 'react-sortable-hoc';
|
|
|
|
import * as styles from './StickerGrid.scss';
|
|
|
|
import { Props as StickerFrameProps, StickerFrame } from './StickerFrame';
|
|
|
|
import { stickersDuck } from '../store';
|
|
|
|
import { DropZone, Props as DropZoneProps } from '../elements/DropZone';
|
2020-09-28 18:40:26 +00:00
|
|
|
import { processStickerImage } from '../util/preload';
|
2021-10-13 17:05:18 +00:00
|
|
|
import { useI18n } from '../util/i18n';
|
2019-12-17 20:25:57 +00:00
|
|
|
|
2020-09-18 20:40:41 +00:00
|
|
|
const queue = new PQueue({ concurrency: 3, timeout: 1000 * 60 * 2 });
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
const SmartStickerFrame = SortableElement(
|
|
|
|
({ id, showGuide, mode }: StickerFrameProps) => {
|
|
|
|
const data = stickersDuck.useStickerData(id);
|
|
|
|
const actions = stickersDuck.useStickerActions();
|
2020-09-28 18:40:26 +00:00
|
|
|
const image = data.imageData ? data.imageData.src : undefined;
|
2019-12-17 20:25:57 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<StickerFrame
|
|
|
|
id={id}
|
|
|
|
showGuide={showGuide}
|
|
|
|
mode={mode}
|
|
|
|
image={image}
|
|
|
|
onRemove={actions.removeSticker}
|
|
|
|
onPickEmoji={actions.setEmoji}
|
|
|
|
emojiData={data.emoji}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export type Props = Pick<StickerFrameProps, 'showGuide' | 'mode'>;
|
|
|
|
|
|
|
|
export type InnerGridProps = Props & {
|
|
|
|
ids: Array<string>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const InnerGrid = SortableContainer(
|
|
|
|
({ ids, mode, showGuide }: InnerGridProps) => {
|
2021-10-13 17:05:18 +00:00
|
|
|
const i18n = useI18n();
|
2019-12-17 20:25:57 +00:00
|
|
|
const containerClassName = ids.length > 0 ? styles.grid : styles.drop;
|
|
|
|
const frameMode = mode === 'add' ? 'removable' : 'pick-emoji';
|
|
|
|
|
|
|
|
const actions = stickersDuck.useStickerActions();
|
|
|
|
|
|
|
|
const handleDrop = React.useCallback<DropZoneProps['onDrop']>(
|
|
|
|
async paths => {
|
|
|
|
actions.initializeStickers(paths);
|
|
|
|
paths.forEach(path => {
|
|
|
|
queue.add(async () => {
|
2020-01-07 02:20:16 +00:00
|
|
|
try {
|
2020-09-28 18:40:26 +00:00
|
|
|
const stickerImage = await processStickerImage(path);
|
|
|
|
actions.addImageData(stickerImage);
|
2020-01-07 02:20:16 +00:00
|
|
|
} catch (e) {
|
2021-10-07 23:28:47 +00:00
|
|
|
window.SignalContext.log.error('Error processing image:', e);
|
2020-01-07 02:20:16 +00:00
|
|
|
actions.removeSticker(path);
|
2020-04-29 21:58:05 +00:00
|
|
|
actions.addToast({
|
2020-09-28 18:40:26 +00:00
|
|
|
key:
|
|
|
|
(e || {}).errorMessageI18nKey ||
|
|
|
|
'StickerCreator--Toasts--errorProcessing',
|
2020-04-29 21:58:05 +00:00
|
|
|
});
|
2020-01-07 02:20:16 +00:00
|
|
|
}
|
2019-12-17 20:25:57 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[actions]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={containerClassName}>
|
|
|
|
{ids.length > 0 ? (
|
|
|
|
<>
|
|
|
|
{ids.map((p, i) => (
|
|
|
|
<SmartStickerFrame
|
|
|
|
key={p}
|
|
|
|
index={i}
|
|
|
|
id={p}
|
|
|
|
showGuide={showGuide}
|
|
|
|
mode={frameMode}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
{mode === 'add' && ids.length < stickersDuck.maxStickers ? (
|
|
|
|
<StickerFrame
|
|
|
|
showGuide={showGuide}
|
|
|
|
mode="add"
|
|
|
|
onDrop={handleDrop}
|
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
) : (
|
2021-10-13 17:05:18 +00:00
|
|
|
<DropZone
|
|
|
|
label={i18n('StickerCreator--DropStage--dragDrop')}
|
|
|
|
onDrop={handleDrop}
|
|
|
|
/>
|
2019-12-17 20:25:57 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const StickerGrid = SortableContainer((props: Props) => {
|
|
|
|
const ids = stickersDuck.useStickerOrder();
|
|
|
|
const actions = stickersDuck.useStickerActions();
|
|
|
|
const handleSortEnd = React.useCallback<SortEndHandler>(
|
|
|
|
sortEnd => {
|
|
|
|
actions.moveSticker(sortEnd);
|
|
|
|
},
|
|
|
|
[actions]
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<InnerGrid
|
|
|
|
{...props}
|
|
|
|
ids={ids}
|
|
|
|
axis="xy"
|
|
|
|
onSortEnd={handleSortEnd}
|
2020-09-14 21:56:35 +00:00
|
|
|
useDragHandle
|
2019-12-17 20:25:57 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|