forked from microbit-foundation/python-editor-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.tsx
199 lines (185 loc) · 4.7 KB
/
settings.tsx
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
import { createContext, ReactNode, useContext } from "react";
import { useStorage } from "../common/use-storage";
import { defaultCodeFontSizePt } from "../deployment/misc";
import { stage } from "../environment";
import { flags } from "../flags";
export interface Language {
id: string;
name: string;
enName: string;
preview?: boolean;
}
// When we add languages we need to update the toolkit search indexing,
// which will require the dynamic import of a new language plugin for lunr.
// See search.ts.
//
// Tag new languages with `preview: true` to enable for beta only.
const allLanguages: Language[] = [
{
id: "en",
name: "English",
enName: "English",
},
{
id: "ca",
name: "Català",
enName: "Catalan",
},
{
id: "zh-cn",
name: "简体中文",
enName: "Chinese (Simplified)",
},
{
id: "zh-tw",
name: "繁體中文",
enName: "Chinese (Traditional)",
},
{
id: "nl",
name: "Nederlands",
enName: "Dutch",
},
{
id: "fr",
name: "Français",
enName: "French",
},
{
id: "de",
name: "Deutsch",
enName: "German",
preview: true,
},
{
id: "ja",
name: "日本語",
enName: "Japanese",
},
{
id: "ko",
name: "한국어",
enName: "Korean",
},
{
id: "es-es",
name: "Español",
enName: "Spanish",
},
];
export const supportedLanguages: Language[] = allLanguages.filter(
(l) => stage !== "PRODUCTION" || !l.preview
);
export const minimumFontSize = 4;
export const maximumFontSize = 154;
export const fontSizeStep = 3;
export type ParameterHelpOption = "automatic" | "manual";
export const parameterHelpOptions: ParameterHelpOption[] = [
"automatic",
"manual",
];
export const getLanguageFromQuery = (): string => {
const searchParams = new URLSearchParams(window.location.search);
const l = searchParams.get("l");
const supportedLanguage = supportedLanguages.find((x) => x.id === l);
return supportedLanguage?.id || supportedLanguages[0].id;
};
export const defaultSettings: Settings = {
languageId: getLanguageFromQuery(),
fontSize: defaultCodeFontSizePt,
codeStructureHighlight: "full",
parameterHelp: "automatic",
showConnectHelp: true,
showTransferHexHelp: true,
showPostSaveHelp: true,
showMultipleFilesHelp: true,
allowEditingThirdPartyModules: false,
};
const inContextTranslationLangId = "lol";
export const isValidSettingsObject = (value: unknown): value is Settings => {
if (typeof value !== "object") {
return false;
}
const object = value as any;
if (
object.languageId &&
object.languageId !== inContextTranslationLangId &&
!supportedLanguages.find((x) => x.id === object.languageId)
) {
return false;
}
if (codeStructureOptions.indexOf(object.codeStructureHighlight) === -1) {
return false;
}
if (parameterHelpOptions.indexOf(object.parameterHelp) === -1) {
return false;
}
if (typeof object.showConnectHelp !== "boolean") {
return false;
}
if (typeof object.showTransferHexHelp !== "boolean") {
return false;
}
if (typeof object.showPostSaveHelp !== "boolean") {
return false;
}
if (typeof object.showMultipleFilesHelp !== "boolean") {
return false;
}
if (typeof object.allowEditingThirdPartyModules !== "boolean") {
return false;
}
return true;
};
export type CodeStructureOption = "none" | "full" | "simple";
export const codeStructureOptions: CodeStructureOption[] = [
"none",
"full",
"simple",
];
export interface Settings {
languageId: string;
fontSize: number;
codeStructureHighlight: CodeStructureOption;
parameterHelp: ParameterHelpOption;
allowEditingThirdPartyModules: boolean;
showConnectHelp: boolean;
showTransferHexHelp: boolean;
showPostSaveHelp: boolean;
showMultipleFilesHelp: boolean;
}
type SettingsContextValue = [Settings, (settings: Settings) => void];
const SettingsContext = createContext<SettingsContextValue | undefined>(
undefined
);
export const useSettings = (): SettingsContextValue => {
const settings = useContext(SettingsContext);
if (!settings) {
throw new Error("Missing provider");
}
return settings;
};
const SettingsProvider = ({ children }: { children: ReactNode }) => {
const [settings, setSettings] = useStorage<Settings>(
"local",
"settings",
defaultSettings,
isValidSettingsObject,
flags.translate
? { languageId: inContextTranslationLangId }
: flags.noLang
? { languageId: getLanguageFromQuery() }
: {}
);
return (
<SettingsContext.Provider value={[settings, setSettings]}>
{children}
</SettingsContext.Provider>
);
};
export default SettingsProvider;