Skip to content

Commit

Permalink
define getter shouldFocus, prevent focusing in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
zr0w1 committed Jan 9, 2025
1 parent a8c3f51 commit 7b6f3fa
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
25 changes: 16 additions & 9 deletions assets/chat/js/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import makeSafeForRegex, {
nsfwregex,
nsflregex,
linkregex,
ismobile,
} from './regex';

import { HashLinkConverter, MISSING_ARG_ERROR } from './hashlinkconverter';
Expand Down Expand Up @@ -112,9 +111,6 @@ class Chat {
this.replyusername = null;
this.watchingfocus = false;

// The context the chat is rendered in
this.ismobile = ismobile.test(window.navigator.userAgent);

// An interface to tell the chat to do things via chat commands, or via emit
// e.g. control.emit('CONNECT', 'ws://localhost:9001') is essentially chat.cmdCONNECT('ws://localhost:9001')
this.control = new EventEmitter(this);
Expand Down Expand Up @@ -240,6 +236,11 @@ class Chat {
this.control.on('BITLY', () => this.cmdDIE());
}

get shouldFocus() {
// return true when not in a mobile context
return !/\bMobi/.test(window.navigator.userAgent);
}

setUser(user) {
if (!user || user.username === '') {
this.user = this.addUser({
Expand Down Expand Up @@ -486,7 +487,7 @@ class Chat {
const onresize = () => {
// If this is a mobile screen, don't close menus.
// The virtual keyboard triggers a 'resize' event, and menus shouldn't be closed whenever the virtual keyboard is opened
if (this.ismobile) {
if (this.shouldFocus) {
return;
}

Expand All @@ -502,14 +503,18 @@ class Chat {
this.windowselect.on('click', '.tab-close', (e) => {
ChatMenu.closeMenus(this);
this.removeWindow($(e.currentTarget).parent().data('name').toLowerCase());
this.input.focus();
if (this.shouldFocus) {
this.input.focus();
}
return false;
});
this.windowselect.on('click', '.tab', (e) => {
ChatMenu.closeMenus(this);
this.windowToFront($(e.currentTarget).data('name').toLowerCase());
this.menus.get('whisper-users').redraw();
this.input.focus();
if (this.shouldFocus) {
this.input.focus();
}
return false;
});

Expand Down Expand Up @@ -1019,7 +1024,7 @@ class Chat {

focusIfNothingSelected() {
// If this is a mobile screen, return to avoid focusing input and bringing up the virtual keyboard
if (this.ismobile) {
if (this.shouldFocus) {
return;
}

Expand Down Expand Up @@ -2542,7 +2547,9 @@ class Chat {
}
this.windowToFront(normalized);
this.menus.get('whisper-users').redraw();
this.input.focus();
if (this.shouldFocus) {
this.input.focus();
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions assets/chat/js/menus/ChatEmoteMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ChatEmoteMenu extends ChatMenu {

show() {
super.show();
if (!this.chat.ismobile) {
if (this.chat.shouldFocus) {
this.searchinput.focus();
}
this.buildEmoteMenu();
Expand Down Expand Up @@ -108,8 +108,9 @@ export default class ChatEmoteMenu extends ChatMenu {

selectEmote(emote) {
const value = this.chat.input.val().toString().trim();
this.chat.input
.val(`${value + (value === '' ? '' : ' ') + emote} `)
.focus();
this.chat.input.val(`${value + (value === '' ? '' : ' ') + emote} `);
if (this.chat.shouldFocus) {
this.chat.input.focus();
}
}
}
7 changes: 4 additions & 3 deletions assets/chat/js/menus/ChatEmoteTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export default class ChatEmoteTooltip extends ChatMenuFloating {

this.ui.emote.on('click', '.emote', () => {
const value = this.chat.input.val().toString().trim();
this.chat.input
.val(`${value + (value === '' ? '' : ' ') + this.emote} `)
.focus();
this.chat.input.val(`${value + (value === '' ? '' : ' ') + this.emote} `);
if (this.chat.shouldFocus) {
this.chat.input.focus();
}
});

this.ui.favorite.on('click', () => {
Expand Down
2 changes: 1 addition & 1 deletion assets/chat/js/menus/ChatMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class ChatMenu extends EventEmitter {
});
this.ui.on('click', '.close,.chat-menu-close', this.hide.bind(this));
this.btn.on('click', (e) => {
if (this.visible && !this.chat.ismobile) {
if (this.visible && this.chat.shouldFocus) {
chat.input.focus();
}
this.toggle(e);
Expand Down
2 changes: 1 addition & 1 deletion assets/chat/js/menus/ChatUserMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default class ChatUserMenu extends ChatMenu {

show() {
super.show();
if (!this.chat.ismobile) {
if (this.chat.shouldFocus) {
this.searchinput.focus();
}
}
Expand Down
4 changes: 0 additions & 4 deletions assets/chat/js/regex.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7b6f3fa

Please sign in to comment.