Skip to content

Commit

Permalink
feat(skymp5-front): revert "add chat history" (#1763)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pospelove authored Dec 11, 2023
1 parent f44353a commit a84adca
Showing 1 changed file with 1 addition and 52 deletions.
53 changes: 1 addition & 52 deletions skymp5-front/src/constructorComponents/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const TIME_LIMIT = 1; // Seconds
const SHOUT_LIMIT = 180; // Seconds
const MAX_LINES = 10;
const MAX_SHOUT_LENGTH = 100;
const MAX_HISTORY_LENGTH = 20;

const SHOUTREGEXP = /(.*?)/gi;

Expand Down Expand Up @@ -47,33 +46,11 @@ const Chat = (props) => {

const shoutReset = useRef(true);

const messagesHistory = useRef([]);

const currentMessageInHistory = useRef(-1);

const writtenMessage = useRef('');

const handleScroll = () => {
if (chatRef.current) {
window.needToScroll = (chatRef.current.scrollTop === chatRef.current.scrollHeight - chatRef.current.offsetHeight);
}
};

const setEndOfContenteditable = (elem) => {
const sel = window.getSelection();
sel.selectAllChildren(elem);
sel.collapseToEnd();
};

const addMessageToHistory = (message) => {
messagesHistory.current = [message, ...messagesHistory.current];
if (messagesHistory.length > MAX_HISTORY_LENGTH) {
messagesHistory.current = messagesHistory.current.slice(0, MAX_HISTORY_LENGTH);
}
currentMessageInHistory.current = -1;
writtenMessage.current = '';
};

const sendMessage = useCallback((text) => {
const shout = text.match(SHOUTREGEXP);
const shoutLen = shout
Expand All @@ -84,9 +61,7 @@ const Chat = (props) => {
: 0;
if (text !== '' && text.length <= MAX_LENGTH && isReset.current && shoutLen <= MAX_SHOUT_LENGTH && (shoutLen === 0 || shoutReset.current)) {
if (send !== undefined) {
const message = replaceIfMoreThan20(text.trim(), '\n', '', MAX_LINES);
send(message);
addMessageToHistory(message);
send(replaceIfMoreThan20(text.trim(), '\n', '', MAX_LINES));
}
isReset.current = false;
updateInput('');
Expand Down Expand Up @@ -119,32 +94,6 @@ const Chat = (props) => {
event.preventDefault();
sendMessage(input);
}
if (event.key === 'ArrowUp') {
if (currentMessageInHistory.current === -1) {
writtenMessage.current = input;
}
if (currentMessageInHistory.current + 1 < messagesHistory.current.length) {
currentMessageInHistory.current = currentMessageInHistory.current + 1;
updateInput(messagesHistory.current[currentMessageInHistory.current]);
inputRef.current.innerHTML = messagesHistory.current[currentMessageInHistory.current];
setEndOfContenteditable(inputRef.current);
}
}
if (event.key === 'ArrowDown') {
if (currentMessageInHistory.current >= 0) {
if (currentMessageInHistory.current === 0) {
updateInput(writtenMessage.current);
inputRef.current.innerHTML = writtenMessage.current;
setEndOfContenteditable(inputRef.current);
currentMessageInHistory.current = -1;
} else {
currentMessageInHistory.current = currentMessageInHistory.current - 1;
updateInput(messagesHistory.current[currentMessageInHistory.current]);
inputRef.current.innerHTML = messagesHistory.current[currentMessageInHistory.current];
setEndOfContenteditable(inputRef.current);
}
}
}
};
node?.addEventListener('keydown', listener);
return () => node?.removeEventListener('keydown', listener);
Expand Down

0 comments on commit a84adca

Please sign in to comment.