Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All-round TGChat improvements #2151

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions tgui/packages/tgui-panel/chat/ChatPageSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export const ChatPageSettings = (props, context) => {
return (
<Section>
<Stack align="center">
<Stack.Item grow={1}>
<Stack.Item grow>
<Input
fluid
width="100%"
value={page.name}
onChange={(e, value) =>
dispatch(
Expand All @@ -30,8 +30,25 @@ export const ChatPageSettings = (props, context) => {
}
/>
</Stack.Item>
<Stack.Item>
<Button.Checkbox
content="Mute"
checked={page.hideUnreadCount}
icon={page.hideUnreadCount ? 'bell-slash' : 'bell'}
tooltip="Disables unread counter"
onClick={() =>
dispatch(
updateChatPage({
pageId: page.id,
hideUnreadCount: !page.hideUnreadCount,
})
)
}
/>
</Stack.Item>
<Stack.Item>
<Button
content="Remove"
icon="times"
color="red"
onClick={() =>
Expand All @@ -40,9 +57,8 @@ export const ChatPageSettings = (props, context) => {
pageId: page.id,
})
)
}>
Remove
</Button>
}
/>
</Stack.Item>
</Stack>
<Divider />
Expand Down
1 change: 1 addition & 0 deletions tgui/packages/tgui-panel/chat/ChatTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const ChatTabs = (props, context) => {
key={page.id}
selected={page === currentPage}
rightSlot={
!page.hideUnreadCount &&
page.unreadCount > 0 && (
<UnreadCountWidget value={page.unreadCount} />
)
Expand Down
1 change: 1 addition & 0 deletions tgui/packages/tgui-panel/chat/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { createPage } from './model';

export const loadChat = createAction('chat/load');
export const rebuildChat = createAction('chat/rebuild');
export const clearChat = createAction('chat/clear');
export const updateMessageCount = createAction('chat/updateMessageCount');
export const addChatPage = createAction('chat/addPage', () => ({
payload: createPage(),
Expand Down
6 changes: 5 additions & 1 deletion tgui/packages/tgui-panel/chat/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import DOMPurify from 'dompurify';
import { storage } from 'common/storage';
import { loadSettings, updateSettings, addHighlightSetting, removeHighlightSetting, updateHighlightSetting } from '../settings/actions';
import { selectSettings } from '../settings/selectors';
import { addChatPage, changeChatPage, changeScrollTracking, loadChat, rebuildChat, removeChatPage, saveChatToDisk, toggleAcceptedType, updateMessageCount } from './actions';
import { addChatPage, changeChatPage, changeScrollTracking, clearChat, loadChat, rebuildChat, removeChatPage, saveChatToDisk, toggleAcceptedType, updateMessageCount } from './actions';
import { MAX_PERSISTED_MESSAGES, MESSAGE_SAVE_INTERVAL } from './constants';
import { createMessage, serializeMessage } from './model';
import { chatRenderer } from './renderer';
Expand Down Expand Up @@ -173,6 +173,10 @@ export const chatMiddleware = (store) => {
chatRenderer.saveToDisk();
return;
}
if (type === clearChat.type) {
chatRenderer.clearChat();
return;
}
return next(action);
};
};
1 change: 1 addition & 0 deletions tgui/packages/tgui-panel/chat/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const createPage = (obj) => {
name: 'New Tab',
acceptedTypes: acceptedTypes,
unreadCount: 0,
hideUnreadCount: false,
createdAt: Date.now(),
...obj,
};
Expand Down
51 changes: 39 additions & 12 deletions tgui/packages/tgui-panel/chat/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class ChatRenderer {
const highlightWholeMessage = setting.highlightWholeMessage;
const matchWord = setting.matchWord;
const matchCase = setting.matchCase;
const allowedRegex = /^[a-z0-9_\-$/^[\s\]\\]+$/gi;
const enabled = setting.enabled;
const allowedRegex = /^[a-zа-яё0-9_\-$/^[\s\]\\]+$/gi;
const lines = String(text)
.split(',')
.map((str) => str.trim())
Expand Down Expand Up @@ -247,6 +248,7 @@ class ChatRenderer {
this.highlightParsers = [];
}
this.highlightParsers.push({
enabled,
highlightWords,
highlightRegex,
highlightColor,
Expand Down Expand Up @@ -402,17 +404,19 @@ class ChatRenderer {

// Highlight text
if (!message.avoidHighlighting && this.highlightParsers) {
this.highlightParsers.map((parser) => {
const highlighted = highlightNode(
node,
parser.highlightRegex,
parser.highlightWords,
(text) => createHighlightNode(text, parser.highlightColor)
);
if (highlighted && parser.highlightWholeMessage) {
node.className += ' ChatMessage--highlighted';
}
});
this.highlightParsers
.filter((parser) => parser.enabled)
.map((parser) => {
const highlighted = highlightNode(
node,
parser.highlightRegex,
parser.highlightWords,
(text) => createHighlightNode(text, parser.highlightColor)
);
if (highlighted && parser.highlightWholeMessage) {
node.className += ' ChatMessage--highlighted';
}
});
}
// Linkify text
const linkifyNodes = node.querySelectorAll('.linkify');
Expand Down Expand Up @@ -537,6 +541,29 @@ class ChatRenderer {
});
}

/**
* @clearChat
* @copyright 2023
* @author Cheffie
* @link https://github.com/CheffieGithub
* @license MIT
*/
clearChat() {
const messages = this.visibleMessages;
this.visibleMessages = [];
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
this.rootNode.removeChild(message.node);
// Mark this message as pruned
message.node = 'pruned';
}
// Remove pruned messages from the message array
this.messages = this.messages.filter(
(message) => message.node !== 'pruned'
);
logger.log(`Cleared chat`);
}

saveToDisk() {
// Allow only on IE11
if (Byond.IS_LTE_IE10) {
Expand Down
2 changes: 1 addition & 1 deletion tgui/packages/tgui-panel/chat/replaceInTextNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const replaceInTextNode = (regex, words, createNode) => (node) => {
for (let word of words) {
// Capture if the word is at the beginning, end, middle,
// or by itself in a message
wordRegexStr += `^${word}\\W|\\W${word}\\W|\\W${word}$|^${word}$`;
wordRegexStr += `^${word}\\s\\W|\\s\\W${word}\\s\\W|\\s\\W${word}$|^${word}\\s\\W$`;
// Make sure the last character for the expression is NOT '|'
if (++i !== words.length) {
wordRegexStr += '|';
Expand Down
Loading
Loading