summaryrefslogtreecommitdiff
path: root/packages/frontend/src/scripts/collect-page-vars.ts
blob: 5096c0669ee123027e4958ed1578311234be028b (plain)
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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

interface StringPageVar {
	name: string,
	type: 'string',
	value: string
}

interface NumberPageVar {
	name: string,
	type: 'number',
	value: number
}

interface BooleanPageVar {
	name: string,
	type: 'boolean',
	value: boolean
}

type PageVar = StringPageVar | NumberPageVar | BooleanPageVar;

export function collectPageVars(content): PageVar[] {
	const pageVars: PageVar[] = [];
	const collect = (xs: any[]): void => {
		for (const x of xs) {
			if (x.type === 'textInput') {
				pageVars.push({
					name: x.name,
					type: 'string',
					value: x.default || '',
				});
			} else if (x.type === 'textareaInput') {
				pageVars.push({
					name: x.name,
					type: 'string',
					value: x.default || '',
				});
			} else if (x.type === 'numberInput') {
				pageVars.push({
					name: x.name,
					type: 'number',
					value: x.default || 0,
				});
			} else if (x.type === 'switch') {
				pageVars.push({
					name: x.name,
					type: 'boolean',
					value: x.default || false,
				});
			} else if (x.type === 'counter') {
				pageVars.push({
					name: x.name,
					type: 'number',
					value: 0,
				});
			} else if (x.type === 'radioButton') {
				pageVars.push({
					name: x.name,
					type: 'string',
					value: x.default || '',
				});
			} else if (x.children) {
				collect(x.children);
			}
		}
	};
	collect(content);
	return pageVars;
}