Improved code

This commit is contained in:
2019-09-08 18:57:30 +02:00
parent df3f1f409d
commit 3c801987bb
10 changed files with 452 additions and 270 deletions
+25 -3
View File
@@ -9,10 +9,11 @@ import { faComment, faAngleDoubleRight } from '../../../client/icons';
import { isInParty } from '../../../client/partyUtils';
import { handleActionCommand } from '../../../client/playerActions';
import { hasHeadAnimation } from '../../../common/pony';
import { AutocompleteState, autocompleteMesssage, replaceEmojis } from '../../../client/emoji';
import { AutocompleteState, autocompleteMesssage, replaceEmojis, emojis } from '../../../client/emoji';
import { replaceNodes } from '../../../client/htmlUtils';
import { invalidEnumReturn } from '../../../common/utils';
import { findMatchingEntityNames, findEntityOrMockByAnyMeans, findBestEntityByName } from '../../../client/handlers';
import * as _ from 'lodash';
const chatTypeNames: string[] = [];
const chatTypeClasses: string[] = [];
@@ -33,7 +34,7 @@ setupChatType(ChatType.Think, 'think');
setupChatType(ChatType.PartyThink, 'party think');
function isActionCommand(message: string) {
return /^\/(yawn|sneeze|achoo|laugh|lol|haha|хаха|jaja)/i.test(message);
return /^\/(yawn|sneeze|excite|tada|wink|achoo|laugh|lol|haha|хаха|jaja|hug)/i.test(message);
}
@Component({
@@ -45,6 +46,8 @@ export class ChatBox implements AfterViewInit, OnDestroy {
readonly maxSayLength = SAY_MAX_LENGTH;
readonly commentIcon = faComment;
readonly sendIcon = faAngleDoubleRight;
readonly emotes = emojis;
emojiBoxState = 'none';
@ViewChild('inputElement', { static: true }) inputElement!: ElementRef;
@ViewChild('typeBox', { static: true }) typeBox!: ElementRef;
@ViewChild('typePrefix', { static: true }) typePrefix!: ElementRef;
@@ -54,12 +57,14 @@ export class ChatBox implements AfterViewInit, OnDestroy {
isOpen = false;
message: string | undefined = '';
chatType = ChatType.Say;
btnEmoji = emojis[0].names[0];
private pasted = false;
private lastMessages: string[] = [];
private state: AutocompleteState = {};
private subscriptions: Subscription[];
private _disabled = false;
constructor(private game: PonyTownGame, zone: NgZone) {
this.subscriptions = [
this.game.onChat.subscribe(() => zone.run(() => this.chat(undefined))),
this.game.onToggleChat.subscribe(() => zone.run(() => this.toggle())),
@@ -92,6 +97,18 @@ export class ChatBox implements AfterViewInit, OnDestroy {
ngOnDestroy() {
this.subscriptions.forEach(s => s.unsubscribe());
}
addEmoji(emoji: string) {
this.toggleEmojiBox();
if (!this.message) this.message = emoji;
else if (this.input.maxLength > this.message.length) {
this.message += emoji;
}
}
toggleEmojiBox() {
if (this.emojiBoxState === 'none')
this.emojiBoxState = 'inline-block';
else this.emojiBoxState = 'none';
}
send(_event: Event | undefined) {
let chatType = this.chatType;
let message = replaceEmojis(cleanMessage(this.message || '')).substr(0, SAY_MAX_LENGTH);
@@ -136,7 +153,6 @@ export class ChatBox implements AfterViewInit, OnDestroy {
this.lastMessages.shift();
}
}
this.close();
}
}
@@ -259,6 +275,7 @@ export class ChatBox implements AfterViewInit, OnDestroy {
}
private close() {
if (this.isOpen) {
this.emojiBoxState = 'none';
this.input.blur();
this.isOpen = false;
this.chatBox.nativeElement.hidden = true;
@@ -273,6 +290,11 @@ export class ChatBox implements AfterViewInit, OnDestroy {
this.open();
}
}
onMouseEnter(event: MouseEvent) {
if (!event.target) return;
const emoji = _.sample(emojis) || emojis[0];
this.btnEmoji = emoji.names[0];
}
toggleChatType() {
const chatTypes = getChatTypes(this.game);
this.chatType = chatTypes[(chatTypes.indexOf(this.chatType) + 1) % chatTypes.length];