File size: 1,618 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * PS custom HTML elements and Preact handling.
 * By Mia and Zarel
 */
import preact from 'preact';
import render from 'preact-render-to-string';
import { Utils } from '../lib';

/** For easy concenation of Preact nodes with strings */
export function html(
	strings: TemplateStringsArray, ...args: (preact.VNode | string | number | null | undefined)[]
) {
	let buf = strings[0];
	let i = 0;
	while (i < args.length) {
		buf += typeof args[i] === 'string' || typeof args[i] === 'number' ?
			Utils.escapeHTML(args[i] as string | number) :
			render(args[i] as preact.VNode);
		buf += strings[++i];
	}
	return buf;
}

/** client-side custom elements */
export interface PSElements extends preact.JSX.IntrinsicElements {
	youtube: { src: string };
	twitch: { src: string, width?: number, height?: number };
	spotify: { src: string };
	username: { name?: string, class?: string, children?: preact.VNode | string };
	psicon: { pokemon: string } | { item: string } | { type: string } | { category: string };
	center: { class?: string };
}

export { render };

export type VNode = preact.VNode;

export const h = preact.h;
export const Fragment = preact.Fragment;
export const Component = preact.Component;

export class FormatText extends preact.Component<{ isTrusted?: boolean, replaceLinebreaks?: boolean }> {
	render() {
		const child = this.props.children;
		if (typeof child !== 'string') throw new Error(`Invalid props.children type: ${!child ? child : typeof child}`);
		return <span
			dangerouslySetInnerHTML={{ __html: Chat.formatText(child, this.props.isTrusted, this.props.replaceLinebreaks) }}
		/>;
	}
}