46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import { EventSlot } from '../library-ts/browser/events/EventSlot.js';
|
|
|
|
export type ConsoleMessageType = 'info' | 'error' | 'hint';
|
|
|
|
export interface ConsoleMessage
|
|
{
|
|
text: string;
|
|
type: ConsoleMessageType;
|
|
timestamp: Date;
|
|
}
|
|
|
|
export class EditorConsole
|
|
{
|
|
static _instance: EditorConsole | null = null;
|
|
|
|
static get(): EditorConsole
|
|
{
|
|
if ( !this._instance ) this._instance = new EditorConsole();
|
|
return this._instance;
|
|
}
|
|
|
|
static readonly MAX_MESSAGES = 500;
|
|
|
|
messages: ConsoleMessage[] = [];
|
|
readonly onMessage: EventSlot<ConsoleMessage> = new EventSlot();
|
|
readonly onHover: EventSlot<string | null> = new EventSlot();
|
|
|
|
log( text: string, type: ConsoleMessageType = 'info' ): void
|
|
{
|
|
const msg: ConsoleMessage = { text, type, timestamp: new Date() };
|
|
this.messages.push( msg );
|
|
if ( this.messages.length > EditorConsole.MAX_MESSAGES ) this.messages.shift();
|
|
this.onMessage.dispatch( msg );
|
|
}
|
|
|
|
showHover( text: string ): void
|
|
{
|
|
this.onHover.dispatch( text );
|
|
}
|
|
|
|
hideHover(): void
|
|
{
|
|
this.onHover.dispatch( null );
|
|
}
|
|
}
|