Preact: Add togglemessages command and handle 'hidelines' properly (#2553)
Some checks are pending
Node.js CI / build (22.x) (push) Waiting to run

This commit is contained in:
Aurastic 2026-03-17 13:45:11 +05:30 committed by GitHub
parent c46bac0471
commit a7f3c522e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 4 deletions

View File

@ -1015,6 +1015,7 @@ export class BattleLog {
hideChatFrom(userid: ID, showRevealButton = true, lineCount = 0) {
const classStart = 'chat chatmessage-' + userid + ' ';
let nodes: HTMLElement[] = [];
const lastChild = this.innerElem.lastChild;
for (const node of this.innerElem.childNodes as any as HTMLElement[]) {
if (node.className && (node.className + ' ').startsWith(classStart)) {
nodes.push(node);
@ -1033,15 +1034,15 @@ export class BattleLog {
node.style.display = 'none';
node.className = 'revealed ' + node.className;
}
if (!nodes.length || !showRevealButton) return;
if (!nodes.length || !showRevealButton || !lastChild) return;
const button = document.createElement('button');
button.name = 'toggleMessages';
button.setAttribute('data-cmd', '/togglemessages ' + userid);
button.value = userid;
button.className = 'subtle';
button.innerHTML = `<small>(${nodes.length} line${nodes.length > 1 ? 's' : ''} from ${userid} hidden)</small>`;
const lastNode = nodes[nodes.length - 1];
lastNode.appendChild(document.createTextNode(' '));
lastNode.appendChild(button);
lastChild.appendChild(document.createTextNode(' '));
lastChild.appendChild(button);
}
static unlinkNodeList(nodeList: ArrayLike<HTMLElement>, classStart: string) {

View File

@ -349,6 +349,40 @@ export class ChatRoom extends PSRoom {
this.log?.reset();
this.update(null);
},
'togglemessages'(target) {
if (this.pmTarget ||
this.type !== 'chat') return this.errorReply('This command can only be used in proper chat rooms.');
if (this.log) {
const userid = toID(target);
const classStart = 'revealed chat chatmessage-' + userid;
const nodes: HTMLElement[] = [];
let isHidden = true;
for (const node of this.log.innerElem.childNodes as any as HTMLElement[]) {
if (node.className && (node.className + ' ').startsWith(classStart)) {
nodes.push(node);
}
}
if (this.log.preemptElem) {
for (const node of this.log.preemptElem.childNodes as any as HTMLElement[]) {
if (node.className && (node.className + ' ').startsWith(classStart)) {
nodes.push(node);
}
}
}
isHidden = nodes[0].style.display === 'none';
nodes.every(node => {
node.style.display = isHidden ? '' : 'none';
return true;
});
isHidden = !isHidden;
const toggleButtons = this.log.innerElem.querySelectorAll(`button[name="toggleMessages"][value="${userid}"]`);
for (const button of toggleButtons) {
button.innerHTML = isHidden ?
`<small>(${nodes.length} line${nodes.length > 1 ? 's' : ''} from ${userid} hidden)</small>` :
`<small>(Hide ${nodes.length} line${nodes.length > 1 ? 's' : ''} from ${userid})</small>`;
}
}
},
'rank,ranking,rating,ladder'(target) {
let arg = target;
if (!arg) {