Add legacy color code support in NameFormatService and implement unit tests for colored name parsing.
This commit is contained in:
parent
5be3072b26
commit
819739ff60
|
|
@ -0,0 +1,37 @@
|
|||
import {TestBed} from '@angular/core/testing';
|
||||
import {NameFormatService} from './name-format.service';
|
||||
import {ChatService} from './chat.service';
|
||||
|
||||
describe('NameFormatService', () => {
|
||||
let service: NameFormatService;
|
||||
let chatServiceSpy: jasmine.SpyObj<ChatService>;
|
||||
|
||||
beforeEach(() => {
|
||||
chatServiceSpy = jasmine.createSpyObj('ChatService', ['partieMap', 'userNameMap']);
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
NameFormatService,
|
||||
{provide: ChatService, useValue: chatServiceSpy}
|
||||
]
|
||||
});
|
||||
service = TestBed.inject(NameFormatService);
|
||||
});
|
||||
|
||||
it('should parse legacy color codes', () => {
|
||||
const input = '&4Hello &cWorld';
|
||||
const result = (service as any).parseColoredName(input);
|
||||
expect(result).toEqual([
|
||||
{text: 'Hello ', color: '#AA0000'},
|
||||
{text: 'World', color: '#FF5555'}
|
||||
]);
|
||||
});
|
||||
|
||||
it('should parse mixed color codes', () => {
|
||||
const input = '{#AA0000<>}Hello &4World';
|
||||
const result = (service as any).parseColoredName(input);
|
||||
expect(result).toEqual([
|
||||
{text: 'Hello ', color: '#AA0000'},
|
||||
{text: 'World', color: '#AA0000'}
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
|
@ -12,6 +12,25 @@ export class NameFormatService {
|
|||
return this.parseColoredName(this.getNameFromType(channel));
|
||||
}
|
||||
|
||||
private readonly LEGACY_COLOR_MAP: Record<string, string> = {
|
||||
'4': '#AA0000',
|
||||
'c': '#FF5555',
|
||||
'6': '#FFAA00',
|
||||
'e': '#FFFF55',
|
||||
'2': '#00AA00',
|
||||
'a': '#55FF55',
|
||||
'b': '#55FFFF',
|
||||
'3': '#00AAAA',
|
||||
'1': '#0000AA',
|
||||
'9': '#5555FF',
|
||||
'd': '#FF55FF',
|
||||
'5': '#AA00AA',
|
||||
'f': '#FFFFFF',
|
||||
'7': '#AAAAAA',
|
||||
'8': '#555555',
|
||||
'0': '#000000'
|
||||
};
|
||||
|
||||
private getNameFromType(channel: ChatChannel): string {
|
||||
if (channel.type === 'PARTY') {
|
||||
return this.chatService.partieMap().get(channel.name) ?? channel.name;
|
||||
|
|
@ -23,7 +42,7 @@ export class NameFormatService {
|
|||
}
|
||||
|
||||
private parseColoredName(name: string): ColoredNameSegment[] {
|
||||
const markerRegex = /\{#([0-9a-fA-F]{6})(<>|[<>]?)}/g;
|
||||
const markerRegex = /\{#([0-9a-fA-F]{6})(<>|[<>]?)\}|&([0-9a-fA-F])/g;
|
||||
const segments: ColoredNameSegment[] = [];
|
||||
let currentColor: string | undefined;
|
||||
let gradientStartColor: string | undefined;
|
||||
|
|
@ -34,8 +53,9 @@ export class NameFormatService {
|
|||
while ((match = markerRegex.exec(name)) !== null) {
|
||||
this.addColoredTextSegment(segments, name.slice(lastIndex, match.index), currentColor);
|
||||
|
||||
const markerColor = `#${match[1]}`;
|
||||
const markerType = match[2];
|
||||
const isLegacy = !!match[3];
|
||||
const markerColor = isLegacy ? this.LEGACY_COLOR_MAP[match[3]] : `#${match[1]}`;
|
||||
const markerType = isLegacy ? undefined : match[2];
|
||||
|
||||
if (markerType === '>') {
|
||||
currentColor = markerColor;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ export function getServerColor(server: string) {
|
|||
} else if (server === 'creative') {
|
||||
return "#FFA500";
|
||||
}
|
||||
return "#0290e8";
|
||||
return "#8a9197";
|
||||
}
|
||||
|
||||
export function getServerTextColor(server: string) {
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import {ComponentFixture, TestBed} from '@angular/core/testing';
|
||||
|
||||
import { LoginComponent } from './login.component';
|
||||
import {LoginDialogComponent} from './login.component';
|
||||
|
||||
describe('LoginComponent', () => {
|
||||
let component: LoginComponent;
|
||||
let fixture: ComponentFixture<LoginComponent>;
|
||||
let component: LoginDialogComponent;
|
||||
let fixture: ComponentFixture<LoginDialogComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [LoginComponent]
|
||||
imports: [LoginDialogComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(LoginComponent);
|
||||
fixture = TestBed.createComponent(LoginDialogComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user