Text Formatter: Add button and hotkey for spoiler

This commit is contained in:
Alexander Zinchuk 2022-01-27 06:08:49 +01:00
parent 22401f5490
commit 76c98116b8
4 changed files with 24 additions and 3 deletions

View File

@ -294,6 +294,8 @@ export function buildMtpMessageEntity(entity: ApiMessageEntity): GramJs.TypeMess
length,
userId: new GramJs.InputUser({ userId: BigInt(userId!), accessHash: user!.accessHash! }),
});
case ApiMessageEntityTypes.Spoiler:
return new GramJs.MessageEntitySpoiler({ offset, length });
default:
return new GramJs.MessageEntityUnknown({ offset, length });
}

View File

@ -499,6 +499,8 @@ function processEntityAsHtml(
data-entity-type="${entity.type}"
dir="auto"
>${renderedContent}</a>`;
case ApiMessageEntityTypes.Spoiler:
return `||${renderedContent}||`;
default:
return renderedContent;
}

View File

@ -253,6 +253,12 @@ const TextFormatter: FC<OwnProps> = ({
selectedRange, selectedTextFormats.strikethrough,
]);
const handleSpoilerText = useCallback(() => {
const text = getSelectedText();
document.execCommand('insertHTML', false, `||${text}||`);
onClose();
}, [getSelectedText, onClose]);
const handleMonospaceText = useCallback(() => {
if (selectedTextFormats.monospace) {
const element = getSelectedElement();
@ -314,6 +320,7 @@ const TextFormatter: FC<OwnProps> = ({
i: handleItalicText,
m: handleMonospaceText,
s: handleStrikethroughText,
p: handleSpoilerText,
};
const handler = HANDLERS_BY_KEY[getKeyFromEvent(e)];
@ -330,9 +337,8 @@ const TextFormatter: FC<OwnProps> = ({
e.stopPropagation();
handler();
}, [
handleBoldText, handleItalicText, handleUnderlineText,
handleMonospaceText, handleStrikethroughText,
openLinkControl,
openLinkControl, handleBoldText, handleUnderlineText, handleItalicText, handleMonospaceText,
handleStrikethroughText, handleSpoilerText,
]);
useEffect(() => {
@ -380,6 +386,13 @@ const TextFormatter: FC<OwnProps> = ({
onKeyDown={handleContainerKeyDown}
>
<div className="TextFormatter-buttons">
<Button
color="translucent"
ariaLabel="Spoiler text"
onClick={handleSpoilerText}
>
<i className="icon-eye-closed" />
</Button>
<Button
color="translucent"
ariaLabel="Bold text"

View File

@ -96,6 +96,10 @@ function parseMarkdown(html: string) {
/(^|\s)(?!<code[^<]*|<\/)[~]{2}([^~\n]+)[~]{2}(?![^<]*<\/code>)(\s|$)/g,
'$1<s>$2</s>$3',
);
parsedHtml = parsedHtml.replace(
/(^|\s)(?!<code[^<]*|<\/)[|]{2}([^|\n]+)[|]{2}(?![^<]*<\/code>)(\s|$)/g,
`$1<span data-entity-type="${ApiMessageEntityTypes.Spoiler}">$2</span>$3`,
);
return parsedHtml;
}