|
@@ -105,15 +105,15 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
? [sharedExample, ...customExamples, ...this.snippets]
|
|
|
: [...customExamples, ...this.snippets];
|
|
|
|
|
|
- const selected = options.find(option => option.value === localData.selectedValue);
|
|
|
+ const selected = options.find((option): boolean => option.value === localData.selectedValue);
|
|
|
|
|
|
- this.setState((prevState: State): Pick<State, never> => ({
|
|
|
+ this.setState({
|
|
|
customExamples,
|
|
|
isCustomExample: (selected && selected.type === 'custom') || false,
|
|
|
options,
|
|
|
selected: sharedExample || selected || this.snippets[0],
|
|
|
sharedExample
|
|
|
- }));
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
public render (): React.ReactNode {
|
|
@@ -194,9 +194,10 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
// as we have in the editor view (TODO: Make the console.error here actually return the full stack)
|
|
|
const exec = `(async ({${Object.keys(this.injected).join(',')}}) => { try { ${code} \n } catch (error) { console.error(error); } })(injected);`;
|
|
|
|
|
|
+ // eslint-disable-next-line no-new-func
|
|
|
new Function('injected', exec)(this.injected);
|
|
|
|
|
|
- this.setState({ isRunning: true } as State);
|
|
|
+ this.setState({ isRunning: true });
|
|
|
}
|
|
|
|
|
|
private stopJs = (): void => {
|
|
@@ -207,13 +208,13 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
this.injected.api.disconnect();
|
|
|
this.injected = null;
|
|
|
|
|
|
- this.setState({ isRunning: false } as State);
|
|
|
+ this.setState({ isRunning: false });
|
|
|
}
|
|
|
|
|
|
- private selectExample = (value: string) => {
|
|
|
+ private selectExample = (value: string): void => {
|
|
|
if (value.length) {
|
|
|
const { options } = this.state;
|
|
|
- const option = options.find(option => option.value === value);
|
|
|
+ const option = options.find((option): boolean => option.value === value);
|
|
|
|
|
|
if (option) {
|
|
|
localStorage.setItem(STORE_SELECTED, value);
|
|
@@ -247,27 +248,27 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
|
|
|
localStorage.setItem(STORE_EXAMPLES, JSON.stringify([snapshot, ...customExamples]));
|
|
|
|
|
|
- this.setState((prevState: State): State => ({
|
|
|
+ this.setState((prevState: State): Pick<State, never> => ({
|
|
|
...prevState,
|
|
|
customExamples: [snapshot, ...prevState.customExamples],
|
|
|
isCustomExample: true,
|
|
|
options,
|
|
|
selected: snapshot,
|
|
|
sharedExample: type === 'shared' ? undefined : prevState.sharedExample
|
|
|
- }) as State);
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
private removeSnippet = (): void => {
|
|
|
const { customExamples, selected } = this.state;
|
|
|
- const filtered = customExamples.filter((value) => value.value !== selected.value);
|
|
|
+ const filtered = customExamples.filter((value): boolean => value.value !== selected.value);
|
|
|
const nextOptions = [...filtered, ...this.snippets];
|
|
|
|
|
|
- this.setState((prevState: State): State => ({
|
|
|
+ this.setState((prevState: State): Pick<State, never> => ({
|
|
|
...prevState,
|
|
|
customExamples: filtered,
|
|
|
isCustomExample: nextOptions[0].type === 'custom' || false,
|
|
|
options: prevState.sharedExample ? [prevState.sharedExample, ...nextOptions] : nextOptions
|
|
|
- }) as State);
|
|
|
+ }));
|
|
|
|
|
|
this.selectExample(nextOptions[0].value);
|
|
|
localStorage.setItem(STORE_EXAMPLES, JSON.stringify(filtered));
|
|
@@ -275,10 +276,10 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
|
|
|
private onEdit = (code: string): void => {
|
|
|
if (code !== this.state.selected.code) {
|
|
|
- this.setState((prevState: State): State => ({
|
|
|
+ this.setState((prevState: State): Pick<State, never> => ({
|
|
|
selected: { ...prevState.selected, code },
|
|
|
isCustomExample: false
|
|
|
- } as State));
|
|
|
+ }));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -286,9 +287,9 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
this.setState({ logs: [] });
|
|
|
}
|
|
|
|
|
|
- private hookConsole = (type: LogType) => {
|
|
|
+ private hookConsole = (type: LogType): (...args: any[]) => void => {
|
|
|
return (...args: any[]): void => {
|
|
|
- this.setState(({ logs }: State) => {
|
|
|
+ this.setState(({ logs }: State): Pick<State, never> => {
|
|
|
logs.push({ args, type });
|
|
|
|
|
|
return { logs: logs.slice(0) };
|
|
@@ -296,20 +297,20 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
};
|
|
|
}
|
|
|
|
|
|
- private decodeBase64 = (base64: string) => {
|
|
|
- const sharedExample = {
|
|
|
+ private decodeBase64 = (base64: string): Snippet => {
|
|
|
+ const sharedExample: Snippet = {
|
|
|
code: '',
|
|
|
label: { basic: true, children: 'URL', size: 'tiny' },
|
|
|
text: 'Shared code example (unsaved)',
|
|
|
type: 'shared',
|
|
|
value: `custom-${Date.now()}`
|
|
|
- } as Snippet;
|
|
|
+ };
|
|
|
|
|
|
try {
|
|
|
const compStr = atob(base64);
|
|
|
const compU8a = new Uint8Array(compStr.length);
|
|
|
|
|
|
- compU8a.forEach((_, i) => {
|
|
|
+ compU8a.forEach((_, i): void => {
|
|
|
compU8a[i] = compStr.charCodeAt(i);
|
|
|
});
|
|
|
|
|
@@ -335,7 +336,7 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
|
|
|
const u8a = util.stringToU8a(code);
|
|
|
const compU8a = snappy.compress(u8a);
|
|
|
- const compStr = compU8a.reduce((str: string, ch: number) => {
|
|
|
+ const compStr = compU8a.reduce((str: string, ch: number): string => {
|
|
|
return str + String.fromCharCode(ch);
|
|
|
}, '');
|
|
|
|
|
@@ -356,14 +357,14 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
private copyToClipboard = (link: string): void => {
|
|
|
// See https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f
|
|
|
const el = document.createElement('textarea');
|
|
|
+
|
|
|
el.value = link;
|
|
|
el.setAttribute('readonly', '');
|
|
|
el.style.position = 'absolute';
|
|
|
el.style.left = '-9999px';
|
|
|
document.body.appendChild(el);
|
|
|
|
|
|
- const existingSelection = document.getSelection()!;
|
|
|
-
|
|
|
+ const existingSelection = document.getSelection();
|
|
|
const selected = existingSelection && existingSelection.rangeCount > 0
|
|
|
? existingSelection.getRangeAt(0)
|
|
|
: undefined;
|
|
@@ -372,7 +373,7 @@ class Playground extends React.PureComponent<Props, State> {
|
|
|
document.execCommand('copy');
|
|
|
document.body.removeChild(el);
|
|
|
|
|
|
- if (selected) {
|
|
|
+ if (existingSelection && selected) {
|
|
|
existingSelection.removeAllRanges();
|
|
|
existingSelection.addRange(selected);
|
|
|
}
|