2021-08-04 12:52:59 +03:00
|
|
|
/* eslint-disable react/prop-types */
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-08-31 16:13:31 +03:00
|
|
|
import './RoomViewCmdBar.scss';
|
2021-08-09 12:13:43 +03:00
|
|
|
import parse from 'html-react-parser';
|
|
|
|
import twemoji from 'twemoji';
|
2021-08-04 12:52:59 +03:00
|
|
|
|
|
|
|
import initMatrix from '../../../client/initMatrix';
|
2021-08-08 19:26:34 +03:00
|
|
|
import { toggleMarkdown } from '../../../client/action/settings';
|
|
|
|
import * as roomActions from '../../../client/action/room';
|
|
|
|
import {
|
2021-08-31 16:13:31 +03:00
|
|
|
openCreateRoom,
|
|
|
|
openPublicRooms,
|
2021-08-08 19:26:34 +03:00
|
|
|
openInviteUser,
|
|
|
|
} from '../../../client/action/navigation';
|
2021-12-28 05:29:39 +02:00
|
|
|
import { getEmojiForCompletion } from '../emoji-board/custom-emoji';
|
2021-08-24 13:01:20 +03:00
|
|
|
import AsyncSearch from '../../../util/AsyncSearch';
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
import Text from '../../atoms/text/Text';
|
|
|
|
import ScrollView from '../../atoms/scroll/ScrollView';
|
2021-12-12 08:01:52 +02:00
|
|
|
import FollowingMembers from '../../molecules/following-members/FollowingMembers';
|
2021-08-04 12:52:59 +03:00
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
const commands = [{
|
|
|
|
name: 'markdown',
|
|
|
|
description: 'Toggle markdown for messages.',
|
|
|
|
exe: () => toggleMarkdown(),
|
|
|
|
}, {
|
|
|
|
name: 'startDM',
|
|
|
|
isOptions: true,
|
|
|
|
description: 'Start direct message with user. Example: /startDM/@johndoe.matrix.org',
|
|
|
|
exe: (roomId, searchTerm) => openInviteUser(undefined, searchTerm),
|
|
|
|
}, {
|
2021-08-31 16:13:31 +03:00
|
|
|
name: 'createRoom',
|
|
|
|
description: 'Create new room',
|
|
|
|
exe: () => openCreateRoom(),
|
2021-08-08 19:26:34 +03:00
|
|
|
}, {
|
|
|
|
name: 'join',
|
|
|
|
isOptions: true,
|
2021-08-31 16:13:31 +03:00
|
|
|
description: 'Join room with alias. Example: /join/#cinny:matrix.org',
|
|
|
|
exe: (roomId, searchTerm) => openPublicRooms(searchTerm),
|
2021-08-08 19:26:34 +03:00
|
|
|
}, {
|
|
|
|
name: 'leave',
|
2021-08-31 16:13:31 +03:00
|
|
|
description: 'Leave current room',
|
2021-08-08 19:26:34 +03:00
|
|
|
exe: (roomId) => roomActions.leave(roomId),
|
|
|
|
}, {
|
|
|
|
name: 'invite',
|
|
|
|
isOptions: true,
|
|
|
|
description: 'Invite user to room. Example: /invite/@johndoe:matrix.org',
|
|
|
|
exe: (roomId, searchTerm) => openInviteUser(roomId, searchTerm),
|
|
|
|
}];
|
|
|
|
|
|
|
|
function CmdItem({ onClick, children }) {
|
2021-08-04 12:52:59 +03:00
|
|
|
return (
|
2021-08-08 19:26:34 +03:00
|
|
|
<button className="cmd-item" onClick={onClick} type="button">
|
|
|
|
{children}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
CmdItem.propTypes = {
|
|
|
|
onClick: PropTypes.func.isRequired,
|
|
|
|
children: PropTypes.node.isRequired,
|
|
|
|
};
|
|
|
|
|
2021-12-12 08:01:52 +02:00
|
|
|
function renderSuggestions({ prefix, option, suggestions }, fireCmd) {
|
|
|
|
function renderCmdSuggestions(cmdPrefix, cmds) {
|
2021-08-24 13:01:20 +03:00
|
|
|
const cmdOptString = (typeof option === 'string') ? `/${option}` : '/?';
|
|
|
|
return cmds.map((cmd) => (
|
2021-08-08 19:26:34 +03:00
|
|
|
<CmdItem
|
2021-08-24 13:01:20 +03:00
|
|
|
key={cmd.name}
|
2021-08-08 19:26:34 +03:00
|
|
|
onClick={() => {
|
|
|
|
fireCmd({
|
|
|
|
prefix: cmdPrefix,
|
2021-08-24 13:01:20 +03:00
|
|
|
option,
|
|
|
|
result: cmd,
|
2021-08-08 19:26:34 +03:00
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
2021-08-24 13:01:20 +03:00
|
|
|
<Text variant="b2">{`${cmd.name}${cmd.isOptions ? cmdOptString : ''}`}</Text>
|
2021-08-08 19:26:34 +03:00
|
|
|
</CmdItem>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-12-12 08:01:52 +02:00
|
|
|
function renderEmojiSuggestion(emPrefix, emos) {
|
2021-12-28 05:29:39 +02:00
|
|
|
const mx = initMatrix.matrixClient;
|
|
|
|
|
|
|
|
// Renders a small Twemoji
|
|
|
|
function renderTwemoji(emoji) {
|
|
|
|
return parse(twemoji.parse(
|
|
|
|
emoji.unicode,
|
|
|
|
{
|
|
|
|
attributes: () => ({
|
|
|
|
unicode: emoji.unicode,
|
|
|
|
shortcodes: emoji.shortcodes?.toString(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render a custom emoji
|
|
|
|
function renderCustomEmoji(emoji) {
|
|
|
|
return (
|
|
|
|
<img
|
|
|
|
className="emoji"
|
|
|
|
src={mx.mxcUrlToHttp(emoji.mxc)}
|
|
|
|
data-mx-emoticon=""
|
|
|
|
alt={`:${emoji.shortcode}:`}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dynamically render either a custom emoji or twemoji based on what the input is
|
|
|
|
function renderEmoji(emoji) {
|
|
|
|
if (emoji.mxc) {
|
|
|
|
return renderCustomEmoji(emoji);
|
|
|
|
}
|
|
|
|
return renderTwemoji(emoji);
|
|
|
|
}
|
|
|
|
|
2021-08-24 13:01:20 +03:00
|
|
|
return emos.map((emoji) => (
|
2021-08-08 19:26:34 +03:00
|
|
|
<CmdItem
|
2021-12-28 05:29:39 +02:00
|
|
|
key={emoji.shortcode}
|
2021-08-08 19:26:34 +03:00
|
|
|
onClick={() => fireCmd({
|
|
|
|
prefix: emPrefix,
|
2021-08-24 13:01:20 +03:00
|
|
|
result: emoji,
|
2021-08-08 19:26:34 +03:00
|
|
|
})}
|
|
|
|
>
|
2021-08-09 12:13:43 +03:00
|
|
|
{
|
2021-12-28 05:29:39 +02:00
|
|
|
renderEmoji(emoji)
|
2021-08-09 12:13:43 +03:00
|
|
|
}
|
2021-08-24 13:01:20 +03:00
|
|
|
<Text variant="b2">{`:${emoji.shortcode}:`}</Text>
|
|
|
|
</CmdItem>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2021-12-12 08:01:52 +02:00
|
|
|
function renderNameSuggestion(namePrefix, members) {
|
2021-08-24 13:01:20 +03:00
|
|
|
return members.map((member) => (
|
|
|
|
<CmdItem
|
|
|
|
key={member.userId}
|
|
|
|
onClick={() => {
|
|
|
|
fireCmd({
|
|
|
|
prefix: namePrefix,
|
|
|
|
result: member,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Text variant="b2">{member.name}</Text>
|
2021-08-08 19:26:34 +03:00
|
|
|
</CmdItem>
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmd = {
|
2021-12-12 08:01:52 +02:00
|
|
|
'/': (cmds) => renderCmdSuggestions(prefix, cmds),
|
|
|
|
':': (emos) => renderEmojiSuggestion(prefix, emos),
|
|
|
|
'@': (members) => renderNameSuggestion(prefix, members),
|
2021-08-08 19:26:34 +03:00
|
|
|
};
|
2021-08-24 13:01:20 +03:00
|
|
|
return cmd[prefix]?.(suggestions);
|
2021-08-08 19:26:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 13:01:20 +03:00
|
|
|
const asyncSearch = new AsyncSearch();
|
|
|
|
let cmdPrefix;
|
|
|
|
let cmdOption;
|
2021-08-31 16:13:31 +03:00
|
|
|
function RoomViewCmdBar({ roomId, roomTimeline, viewEvent }) {
|
2021-08-08 19:26:34 +03:00
|
|
|
const [cmd, setCmd] = useState(null);
|
|
|
|
|
2021-08-24 13:01:20 +03:00
|
|
|
function displaySuggestions(suggestions) {
|
|
|
|
if (suggestions.length === 0) {
|
|
|
|
setCmd({ prefix: cmd?.prefix || cmdPrefix, error: 'No suggestion found.' });
|
|
|
|
viewEvent.emit('cmd_error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setCmd({ prefix: cmd?.prefix || cmdPrefix, suggestions, option: cmdOption });
|
|
|
|
}
|
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
function processCmd(prefix, slug) {
|
2021-08-24 13:01:20 +03:00
|
|
|
let searchTerm = slug;
|
|
|
|
cmdOption = undefined;
|
|
|
|
cmdPrefix = prefix;
|
|
|
|
if (prefix === '/') {
|
|
|
|
const cmdSlugParts = slug.split('/');
|
|
|
|
[searchTerm, cmdOption] = cmdSlugParts;
|
|
|
|
}
|
|
|
|
if (prefix === ':') {
|
|
|
|
if (searchTerm.length <= 3) {
|
|
|
|
if (searchTerm.match(/^[-]?(\))$/)) searchTerm = 'smile';
|
|
|
|
else if (searchTerm.match(/^[-]?(s|S)$/)) searchTerm = 'confused';
|
|
|
|
else if (searchTerm.match(/^[-]?(o|O|0)$/)) searchTerm = 'astonished';
|
|
|
|
else if (searchTerm.match(/^[-]?(\|)$/)) searchTerm = 'neutral_face';
|
|
|
|
else if (searchTerm.match(/^[-]?(d|D)$/)) searchTerm = 'grin';
|
|
|
|
else if (searchTerm.match(/^[-]?(\/)$/)) searchTerm = 'frown';
|
2021-08-26 08:06:41 +03:00
|
|
|
else if (searchTerm.match(/^[-]?(p|P)$/)) searchTerm = 'stuck_out_tongue';
|
2021-08-24 13:01:20 +03:00
|
|
|
else if (searchTerm.match(/^'[-]?(\()$/)) searchTerm = 'cry';
|
|
|
|
else if (searchTerm.match(/^[-]?(x|X)$/)) searchTerm = 'dizzy_face';
|
|
|
|
else if (searchTerm.match(/^[-]?(\()$/)) searchTerm = 'pleading_face';
|
|
|
|
else if (searchTerm.match(/^[-]?(\$)$/)) searchTerm = 'money';
|
|
|
|
else if (searchTerm.match(/^(<3)$/)) searchTerm = 'heart';
|
2021-10-25 14:16:23 +03:00
|
|
|
else if (searchTerm.match(/^(c|ca|cat)$/)) searchTerm = '_cat';
|
2021-08-24 13:01:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncSearch.search(searchTerm);
|
2021-08-08 19:26:34 +03:00
|
|
|
}
|
|
|
|
function activateCmd(prefix) {
|
2021-08-24 13:01:20 +03:00
|
|
|
cmdPrefix = prefix;
|
2021-12-12 08:01:52 +02:00
|
|
|
cmdPrefix = undefined;
|
2021-08-24 13:01:20 +03:00
|
|
|
|
2021-12-12 08:01:52 +02:00
|
|
|
const mx = initMatrix.matrixClient;
|
2021-08-24 13:01:20 +03:00
|
|
|
const setupSearch = {
|
2021-12-12 08:01:52 +02:00
|
|
|
'/': () => {
|
|
|
|
asyncSearch.setup(commands, { keys: ['name'], isContain: true });
|
|
|
|
setCmd({ prefix, suggestions: commands });
|
|
|
|
},
|
|
|
|
':': () => {
|
2021-12-29 06:26:17 +02:00
|
|
|
const emojis = getEmojiForCompletion(mx.getRoom(roomId));
|
2021-12-12 08:01:52 +02:00
|
|
|
asyncSearch.setup(emojis, { keys: ['shortcode'], isContain: true, limit: 20 });
|
|
|
|
setCmd({ prefix, suggestions: emojis.slice(26, 46) });
|
|
|
|
},
|
|
|
|
'@': () => {
|
|
|
|
const members = mx.getRoom(roomId).getJoinedMembers().map((member) => ({
|
|
|
|
name: member.name,
|
|
|
|
userId: member.userId.slice(1),
|
|
|
|
}));
|
|
|
|
asyncSearch.setup(members, { keys: ['name', 'userId'], limit: 20 });
|
|
|
|
const endIndex = members.length > 20 ? 20 : members.length;
|
|
|
|
setCmd({ prefix, suggestions: members.slice(0, endIndex) });
|
|
|
|
},
|
2021-08-24 13:01:20 +03:00
|
|
|
};
|
|
|
|
setupSearch[prefix]?.();
|
2021-08-08 19:26:34 +03:00
|
|
|
}
|
|
|
|
function deactivateCmd() {
|
|
|
|
setCmd(null);
|
2021-08-24 13:01:20 +03:00
|
|
|
cmdOption = undefined;
|
|
|
|
cmdPrefix = undefined;
|
2021-08-08 19:26:34 +03:00
|
|
|
}
|
|
|
|
function fireCmd(myCmd) {
|
|
|
|
if (myCmd.prefix === '/') {
|
|
|
|
myCmd.result.exe(roomId, myCmd.option);
|
|
|
|
viewEvent.emit('cmd_fired');
|
|
|
|
}
|
|
|
|
if (myCmd.prefix === ':') {
|
|
|
|
viewEvent.emit('cmd_fired', {
|
2021-12-28 05:29:39 +02:00
|
|
|
replace: myCmd.result.mxc ? `:${myCmd.result.shortcode}: ` : myCmd.result.unicode,
|
2021-08-08 19:26:34 +03:00
|
|
|
});
|
|
|
|
}
|
2021-08-24 13:01:20 +03:00
|
|
|
if (myCmd.prefix === '@') {
|
|
|
|
viewEvent.emit('cmd_fired', {
|
|
|
|
replace: myCmd.result.name,
|
|
|
|
});
|
|
|
|
}
|
2021-08-08 19:26:34 +03:00
|
|
|
deactivateCmd();
|
|
|
|
}
|
|
|
|
|
2021-08-26 12:13:14 +03:00
|
|
|
function listenKeyboard(event) {
|
|
|
|
const { activeElement } = document;
|
|
|
|
const lastCmdItem = document.activeElement.parentNode.lastElementChild;
|
|
|
|
if (event.keyCode === 27) {
|
|
|
|
if (activeElement.className !== 'cmd-item') return;
|
|
|
|
viewEvent.emit('focus_msg_input');
|
|
|
|
}
|
|
|
|
if (event.keyCode === 9) {
|
|
|
|
if (lastCmdItem.className !== 'cmd-item') return;
|
|
|
|
if (lastCmdItem !== activeElement) return;
|
|
|
|
if (event.shiftKey) return;
|
|
|
|
viewEvent.emit('focus_msg_input');
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 19:26:34 +03:00
|
|
|
useEffect(() => {
|
|
|
|
viewEvent.on('cmd_activate', activateCmd);
|
|
|
|
viewEvent.on('cmd_deactivate', deactivateCmd);
|
|
|
|
return () => {
|
|
|
|
deactivateCmd();
|
|
|
|
viewEvent.removeListener('cmd_activate', activateCmd);
|
|
|
|
viewEvent.removeListener('cmd_deactivate', deactivateCmd);
|
|
|
|
};
|
|
|
|
}, [roomId]);
|
|
|
|
|
2021-08-24 13:01:20 +03:00
|
|
|
useEffect(() => {
|
2021-08-26 12:13:14 +03:00
|
|
|
if (cmd !== null) document.body.addEventListener('keydown', listenKeyboard);
|
2021-08-24 13:01:20 +03:00
|
|
|
viewEvent.on('cmd_process', processCmd);
|
|
|
|
asyncSearch.on(asyncSearch.RESULT_SENT, displaySuggestions);
|
|
|
|
return () => {
|
2021-08-26 12:13:14 +03:00
|
|
|
if (cmd !== null) document.body.removeEventListener('keydown', listenKeyboard);
|
|
|
|
|
2021-08-24 13:01:20 +03:00
|
|
|
viewEvent.removeListener('cmd_process', processCmd);
|
|
|
|
asyncSearch.removeListener(asyncSearch.RESULT_SENT, displaySuggestions);
|
|
|
|
};
|
|
|
|
}, [cmd]);
|
|
|
|
|
2021-12-12 08:01:52 +02:00
|
|
|
const isError = typeof cmd?.error === 'string';
|
|
|
|
if (cmd === null || isError) {
|
2021-08-08 19:26:34 +03:00
|
|
|
return (
|
|
|
|
<div className="cmd-bar">
|
2021-12-12 08:01:52 +02:00
|
|
|
<FollowingMembers roomTimeline={roomTimeline} />
|
2021-08-08 19:26:34 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="cmd-bar">
|
|
|
|
<div className="cmd-bar__info">
|
2021-12-12 08:01:52 +02:00
|
|
|
<Text variant="b3">TAB</Text>
|
2021-08-08 19:26:34 +03:00
|
|
|
</div>
|
|
|
|
<div className="cmd-bar__content">
|
2021-12-12 08:01:52 +02:00
|
|
|
<ScrollView horizontal vertical={false} invisible>
|
|
|
|
<div className="cmd-bar__content-suggestions">
|
|
|
|
{ renderSuggestions(cmd, fireCmd) }
|
|
|
|
</div>
|
|
|
|
</ScrollView>
|
2021-08-08 19:26:34 +03:00
|
|
|
</div>
|
2021-08-04 12:52:59 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-08-31 16:13:31 +03:00
|
|
|
RoomViewCmdBar.propTypes = {
|
2021-08-04 12:52:59 +03:00
|
|
|
roomId: PropTypes.string.isRequired,
|
|
|
|
roomTimeline: PropTypes.shape({}).isRequired,
|
|
|
|
viewEvent: PropTypes.shape({}).isRequired,
|
|
|
|
};
|
|
|
|
|
2021-08-31 16:13:31 +03:00
|
|
|
export default RoomViewCmdBar;
|