forked from EpicGames/PixelStreamingInfrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingText.ts
82 lines (74 loc) · 2.5 KB
/
SettingText.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright Epic Games, Inc. All Rights Reserved.
import type { TextParametersIds } from './Config';
import { SettingBase } from './SettingBase';
/**
* A text setting object with a text label.
*/
export class SettingText<
CustomIds extends string = TextParametersIds
> extends SettingBase {
id: TextParametersIds | CustomIds;
onChangeEmit: (changedValue: string) => void;
useUrlParams: boolean;
constructor(
id: TextParametersIds | CustomIds,
label: string,
description: string,
defaultTextValue: string,
useUrlParams: boolean,
// eslint-disable-next-line @typescript-eslint/no-empty-function
defaultOnChangeListener: (changedValue: unknown, setting: SettingBase) => void = () => { /* Do nothing, to be overridden. */ }
) {
super(id, label, description, defaultTextValue, defaultOnChangeListener);
const urlParams = new URLSearchParams(window.location.search);
if (!useUrlParams || !urlParams.has(this.id)) {
this.text = defaultTextValue;
} else {
// parse flag from url parameters
const urlParamFlag = this.getUrlParamText();
this.text = urlParamFlag;
}
this.useUrlParams = useUrlParams;
}
/**
* Parse the text value from the url parameters.
* @returns The text value parsed from the url if the url parameters contains /?id=value, but empty string if just /?id or no url param found.
*/
getUrlParamText(): string {
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has(this.id)) {
return urlParams.get(this.id) ?? '';
}
return '';
}
/**
* Persist the setting value in URL.
*/
public updateURLParams() {
if (this.useUrlParams) {
// set url params
const urlParams = new URLSearchParams(window.location.search);
urlParams.set(this.id, this.text);
window.history.replaceState(
{},
'',
urlParams.toString() !== ''
? `${location.pathname}?${urlParams}`
: `${location.pathname}`
);
}
}
/**
* @return The setting's value.
*/
public get text(): string {
return this.value as string;
}
/**
* Update the setting's stored value.
* @param inValue The new value for the setting.
*/
public set text(inValue: string) {
this.value = inValue;
}
}