-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
130 lines (120 loc) · 3.7 KB
/
App.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
import React from 'react';
import { Container, Textarea } from '@mantine/core';
import { Prism } from '@mantine/prism';
import json5 from "json5";
import { Formatter, FracturedJsonOptions } from 'fracturedjsonjs';
import './App.css';
function App() {
const [originalJSON, setOriginalJSON] = React.useState('');
const [kleUpdate, setKLEUpdate] = React.useState('');
const [outputJSON, setOutputJSON] = React.useState('');
const [originalKLE, setOriginalKLE] = React.useState('');
const [isValidJSON, setIsValidJSON] = React.useState(false);
const [isValidKLE, setIsValidKLE] = React.useState(false);
const formatterOptions = new FracturedJsonOptions();
formatterOptions.IndentSpaces = 2;
formatterOptions.MaxTableRowComplexity = 0;
const formatter = new Formatter();
formatter.Options = formatterOptions;
const handleJSONChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const newJSON = event.target.value
setOriginalJSON(newJSON);
console.log(newJSON);
updateOutput(newJSON, kleUpdate);
updateOriginalKLE(newJSON);
};
const handleKLEChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
const newKLE = event.target.value
setKLEUpdate(newKLE);
console.log(newKLE);
updateOutput(originalJSON, newKLE);
};
const parseInputJSON = (input: string): object | null => {
try {
const output = json5.parse(input);
setIsValidJSON(true);
return output;
} catch {
setIsValidJSON(false);
return null;
}
}
const parseInputKLE = (input: string): object | null => {
if (input === '') {
setIsValidKLE(false);
return null;
}
try {
const output = json5.parse('[' + input + ']');
setIsValidKLE(true);
return output;
} catch {
setIsValidKLE(false);
return null;
}
}
const updateOutput = (inputJSON: string, inputKLE: string) => {
setOutputJSON('');
const newJSON = parseInputJSON(inputJSON);
const keymap = parseInputKLE(inputKLE);
if (newJSON != null && keymap != null) {
var output = newJSON as any;
output.layouts.keymap = keymap;
console.log(output);
setOutputJSON(formatter.Reformat(JSON.stringify(output, null, 2)));
}
}
const updateOriginalKLE = (inputJSON: string) => {
setOriginalKLE('');
const newJSON = parseInputJSON(inputJSON);
if (newJSON != null) {
var output = newJSON as any;
console.log('keymap: ' + output.layouts.keymap);
setOriginalKLE(JSON.stringify(output.layouts.keymap).replace(/^\[|]$/g, ''));
}
}
return (
<div className="App">
<React.Fragment>
<Container>
<h1>VIA JSON Updater Tool</h1>
<h3>Paste original VIA JSON here:</h3>
<Textarea
value={originalJSON}
onChange={handleJSONChange}
placeholder="Original VIA JSON..."
error={!isValidJSON}
autosize
minRows={8}
maxRows={12}
/>
<h3>Original KLE from VIA JSON (to paste into Raw data in KLE):</h3>
<Prism
colorScheme="dark"
language="json"
>
{originalKLE}
</Prism>
<h3>Paste updated Raw data from KLE here:</h3>
<Textarea
value={kleUpdate}
onChange={handleKLEChange}
placeholder="New Raw KLE JSON..."
error={!isValidKLE}
autosize
minRows={8}
maxRows={12}
/>
<h3>Updated VIA JSON:</h3>
<Prism
colorScheme="dark"
language="json"
>
{outputJSON}
</Prism>
</Container>
</React.Fragment>
</div>
);
}
export default App;