2021-08-06 00:17:05 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ChangeEvent, ClipboardEvent } from 'react';
|
2021-08-06 00:17:05 +00:00
|
|
|
import React, {
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react';
|
|
|
|
import { noop } from 'lodash';
|
|
|
|
|
|
|
|
import * as grapheme from '../util/grapheme';
|
|
|
|
import { AvatarColorPicker } from './AvatarColorPicker';
|
|
|
|
import { AvatarColors } from '../types/Colors';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { AvatarDataType } from '../types/Avatar';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { AvatarModalButtons } from './AvatarModalButtons';
|
|
|
|
import { BetterAvatarBubble } from './BetterAvatarBubble';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { LocalizerType } from '../types/Util';
|
2021-09-24 00:49:05 +00:00
|
|
|
import { avatarDataToBytes } from '../util/avatarDataToBytes';
|
2021-08-06 00:17:05 +00:00
|
|
|
import { createAvatarData } from '../util/createAvatarData';
|
|
|
|
import {
|
|
|
|
getFittedFontSize,
|
|
|
|
getFontSizes,
|
|
|
|
} from '../util/avatarTextSizeCalculator';
|
|
|
|
|
|
|
|
type DoneHandleType = (
|
2021-09-24 00:49:05 +00:00
|
|
|
avatarBuffer: Uint8Array,
|
2021-08-06 00:17:05 +00:00
|
|
|
avatarData: AvatarDataType
|
|
|
|
) => unknown;
|
|
|
|
|
|
|
|
export type PropsType = {
|
|
|
|
avatarData?: AvatarDataType;
|
|
|
|
i18n: LocalizerType;
|
|
|
|
onCancel: () => unknown;
|
|
|
|
onDone: DoneHandleType;
|
|
|
|
};
|
|
|
|
|
|
|
|
const BUBBLE_SIZE = 120;
|
|
|
|
const MAX_LENGTH = 3;
|
|
|
|
|
2022-11-18 00:45:19 +00:00
|
|
|
export function AvatarTextEditor({
|
2021-08-06 00:17:05 +00:00
|
|
|
avatarData,
|
|
|
|
i18n,
|
|
|
|
onCancel,
|
|
|
|
onDone,
|
2022-11-18 00:45:19 +00:00
|
|
|
}: PropsType): JSX.Element {
|
2021-08-06 00:17:05 +00:00
|
|
|
const initialText = useMemo(() => avatarData?.text || '', [avatarData]);
|
2021-11-11 22:43:05 +00:00
|
|
|
const initialColor = useMemo(
|
|
|
|
() => avatarData?.color || AvatarColors[0],
|
|
|
|
[avatarData]
|
|
|
|
);
|
2021-08-06 00:17:05 +00:00
|
|
|
|
|
|
|
const [inputText, setInputText] = useState(initialText);
|
|
|
|
const [fontSize, setFontSize] = useState(getFontSizes(BUBBLE_SIZE).text);
|
|
|
|
const [selectedColor, setSelectedColor] = useState(initialColor);
|
|
|
|
|
|
|
|
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
|
|
|
|
const focusInput = useCallback(() => {
|
|
|
|
const inputEl = inputRef?.current;
|
|
|
|
if (inputEl) {
|
|
|
|
inputEl.focus();
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const handleChange = useCallback(
|
|
|
|
(ev: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
const { value } = ev.target;
|
|
|
|
if (grapheme.count(value) <= MAX_LENGTH) {
|
|
|
|
setInputText(ev.target.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[setInputText]
|
|
|
|
);
|
|
|
|
|
|
|
|
const handlePaste = useCallback(
|
|
|
|
(ev: ClipboardEvent<HTMLInputElement>) => {
|
|
|
|
const inputEl = ev.currentTarget;
|
|
|
|
|
|
|
|
const selectionStart = inputEl.selectionStart || 0;
|
|
|
|
const selectionEnd = inputEl.selectionEnd || inputEl.selectionStart || 0;
|
|
|
|
const textBeforeSelection = inputText.slice(0, selectionStart);
|
|
|
|
const textAfterSelection = inputText.slice(selectionEnd);
|
|
|
|
|
|
|
|
const pastedText = ev.clipboardData.getData('Text');
|
|
|
|
|
|
|
|
const newGraphemeCount =
|
|
|
|
grapheme.count(textBeforeSelection) +
|
|
|
|
grapheme.count(pastedText) +
|
|
|
|
grapheme.count(textAfterSelection);
|
|
|
|
|
|
|
|
if (newGraphemeCount > MAX_LENGTH) {
|
|
|
|
ev.preventDefault();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[inputText]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onDoneRef = useRef<DoneHandleType>(onDone);
|
|
|
|
|
|
|
|
// Make sure we keep onDoneRef up to date
|
|
|
|
useEffect(() => {
|
|
|
|
onDoneRef.current = onDone;
|
|
|
|
}, [onDone]);
|
|
|
|
|
|
|
|
const handleDone = useCallback(async () => {
|
|
|
|
const newAvatarData = createAvatarData({
|
|
|
|
color: selectedColor,
|
|
|
|
text: inputText,
|
|
|
|
});
|
|
|
|
|
2021-09-24 00:49:05 +00:00
|
|
|
const buffer = await avatarDataToBytes(newAvatarData);
|
2021-08-06 00:17:05 +00:00
|
|
|
|
|
|
|
onDoneRef.current(buffer, newAvatarData);
|
|
|
|
}, [inputText, selectedColor]);
|
|
|
|
|
|
|
|
// In case the component unmounts before we're able to create the avatar data
|
|
|
|
// we set the done handler to a no-op.
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
onDoneRef.current = noop;
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const measureElRef = useRef<null | HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
|
|
const measureEl = measureElRef.current;
|
|
|
|
if (!measureEl) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextFontSize = getFittedFontSize(
|
|
|
|
BUBBLE_SIZE,
|
|
|
|
inputText,
|
|
|
|
candidateFontSize => {
|
|
|
|
measureEl.style.fontSize = `${candidateFontSize}px`;
|
|
|
|
const { width, height } = measureEl.getBoundingClientRect();
|
|
|
|
return { height, width };
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
setFontSize(nextFontSize);
|
|
|
|
}, [inputText]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
focusInput();
|
|
|
|
}, [focusInput]);
|
|
|
|
|
|
|
|
const hasChanges =
|
|
|
|
initialText !== inputText || selectedColor !== initialColor;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="AvatarEditor__preview">
|
|
|
|
<BetterAvatarBubble
|
|
|
|
color={selectedColor}
|
|
|
|
i18n={i18n}
|
|
|
|
onSelect={focusInput}
|
|
|
|
style={{
|
|
|
|
height: BUBBLE_SIZE,
|
|
|
|
width: BUBBLE_SIZE,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
className="AvatarTextEditor__input"
|
|
|
|
onChange={handleChange}
|
|
|
|
onPaste={handlePaste}
|
|
|
|
ref={inputRef}
|
|
|
|
style={{ fontSize }}
|
|
|
|
type="text"
|
|
|
|
value={inputText}
|
|
|
|
/>
|
|
|
|
</BetterAvatarBubble>
|
|
|
|
</div>
|
|
|
|
<hr className="AvatarEditor__divider" />
|
|
|
|
<AvatarColorPicker
|
|
|
|
i18n={i18n}
|
|
|
|
onColorSelected={color => {
|
|
|
|
setSelectedColor(color);
|
|
|
|
focusInput();
|
|
|
|
}}
|
|
|
|
selectedColor={selectedColor}
|
|
|
|
/>
|
|
|
|
<AvatarModalButtons
|
|
|
|
hasChanges={hasChanges}
|
|
|
|
i18n={i18n}
|
|
|
|
onCancel={onCancel}
|
|
|
|
onSave={handleDone}
|
|
|
|
/>
|
|
|
|
<div className="AvatarTextEditor__measure" ref={measureElRef}>
|
|
|
|
{inputText}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
2022-11-18 00:45:19 +00:00
|
|
|
}
|