Update config editor (#37)
* Update config-editor.tsx * Fix minor grammar error * Fix minor grammar error --------- Co-authored-by: Kas-tle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
1 parent
6926df9337
commit
97dd0dab0a
1 file changed
+64
-25
@@ -3,7 +3,7 @@ import HeroBanner from '@site/src/components/HeroBanner';
|
|||||||
import HeroBackground from '@site/static/img/site/split-background.webp';
|
import HeroBackground from '@site/static/img/site/split-background.webp';
|
||||||
import Layout from '@theme/Layout';
|
import Layout from '@theme/Layout';
|
||||||
import styles from './utilities.module.scss';
|
import styles from './utilities.module.scss';
|
||||||
import { useRef, useState } from 'react';
|
import {useRef, useState} from 'react';
|
||||||
|
|
||||||
const ConfigEditorPage: React.FC = () => {
|
const ConfigEditorPage: React.FC = () => {
|
||||||
const [config, setConfig] = useState('');
|
const [config, setConfig] = useState('');
|
||||||
@@ -53,7 +53,9 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
let currentSection = '';
|
let currentSection = '';
|
||||||
let currentComment = '';
|
let currentComment = '';
|
||||||
|
|
||||||
const newConfig = {};
|
const newConfig = {"": {}};
|
||||||
|
|
||||||
|
let ignore = false;
|
||||||
|
|
||||||
// Parse each line of the config
|
// Parse each line of the config
|
||||||
for (let i = 0; i < lines.length; i++) {
|
for (let i = 0; i < lines.length; i++) {
|
||||||
@@ -72,7 +74,7 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
|
|
||||||
const commented = line.startsWith('#');
|
const commented = line.startsWith('#');
|
||||||
|
|
||||||
const match = line.match(keyReg)
|
const match = line.match(keyReg);
|
||||||
if (match === null) {
|
if (match === null) {
|
||||||
// Check if we are in a comment
|
// Check if we are in a comment
|
||||||
if (commented) {
|
if (commented) {
|
||||||
@@ -84,12 +86,26 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
// Check if we are in a new section
|
// Check if we are in a new section
|
||||||
if (line.endsWith(':')) {
|
if (line.endsWith(':')) {
|
||||||
currentSection = line.replace(':', '');
|
currentSection = line.replace(':', '');
|
||||||
|
|
||||||
|
// Ignore user auths section
|
||||||
|
if (currentSection === 'saved-user-logins') {
|
||||||
|
ignore = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
ignore = false;
|
||||||
|
if (commented) {
|
||||||
|
currentComment += '<strong>Note: This option is commented out by default. It will be automatically uncommented if modified.</strong>' + '<br>';
|
||||||
|
}
|
||||||
|
|
||||||
|
currentComment = currentComment.replace(URL_REGEX, function ($0) {
|
||||||
|
return '<a href="' + $0 + '">' + $0 + '</a>';
|
||||||
|
})
|
||||||
|
newConfig[currentSection] = {sectionDesc0: currentComment};
|
||||||
currentComment = '';
|
currentComment = '';
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore user auths section
|
if (ignore) {
|
||||||
if (currentSection === 'saved-user-logins') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +113,7 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
const splitLine = line.split(':');
|
const splitLine = line.split(':');
|
||||||
splitLine[0] = splitLine[0].trim();
|
splitLine[0] = splitLine[0].trim();
|
||||||
let val = '';
|
let val = '';
|
||||||
for (let j = 1;j<splitLine.length;j++) {
|
for (let j = 1; j < splitLine.length; j++) {
|
||||||
val += splitLine[j];
|
val += splitLine[j];
|
||||||
}
|
}
|
||||||
val = val.trim();
|
val = val.trim();
|
||||||
@@ -108,11 +124,14 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (commented) {
|
if (commented) {
|
||||||
currentComment += '<strong>Note: This option is commented on, and if you change it, it will be automatically uncommented.</strong>' + '<br>';
|
currentComment += '<strong>Note: This option is commented out by default. It will be automatically uncommented if modified.</strong>' + '<br>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentComment = currentComment.replace(URL_REGEX, function ($0) {
|
||||||
|
return '<a href="' + $0 + '">' + $0 + '</a>';
|
||||||
|
})
|
||||||
|
|
||||||
// Add the config option to the config object
|
// Add the config option to the config object
|
||||||
newConfig[currentSection] = newConfig[currentSection] || {};
|
|
||||||
newConfig[currentSection][splitLine[0]] = {
|
newConfig[currentSection][splitLine[0]] = {
|
||||||
desc: currentComment,
|
desc: currentComment,
|
||||||
value: val,
|
value: val,
|
||||||
@@ -132,12 +151,22 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
<div className={styles.cardBody}>
|
<div className={styles.cardBody}>
|
||||||
{Object.keys(config[configKey]).map(configOption => {
|
{Object.keys(config[configKey]).map(configOption => {
|
||||||
const configOptionInfo = config[configKey][configOption];
|
const configOptionInfo = config[configKey][configOption];
|
||||||
|
if (configOption == "sectionDesc0") {
|
||||||
|
if (configOptionInfo === "") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className={styles.configOption}>
|
||||||
|
<p dangerouslySetInnerHTML={{__html: configOptionInfo}}/>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
const configOptionKey = configKey === '' ? configOption : `${configKey}.${configOption}`;
|
const configOptionKey = configKey === '' ? configOption : `${configKey}.${configOption}`;
|
||||||
const showOptionName = configOption.replace('#','').trim()
|
const showOptionName = configOption.replace('#', '').trim()
|
||||||
return (
|
return (
|
||||||
<div key={configOptionKey} className={styles.configOption}>
|
<div key={configOptionKey} className={styles.configOption}>
|
||||||
<h3>{showOptionName}</h3>
|
<h3>{showOptionName}</h3>
|
||||||
<p dangerouslySetInnerHTML={{ __html: configOptionInfo.desc }} />
|
<p dangerouslySetInnerHTML={{__html: configOptionInfo.desc}}/>
|
||||||
{getInput(configOptionKey, configOptionInfo.value)}
|
{getInput(configOptionKey, configOptionInfo.value)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@@ -147,6 +176,8 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const URL_REGEX = /https?:\/\/(([0-9a-z.]+\.[a-z]+)|(([0-9]{1,3}\.){3}[0-9]{1,3}))(:[0-9]+)?(\/[0-9a-z%\/.\-_]*)?(\?[0-9a-z=&%_\-]*)?(#[0-9a-z=&%_\-]*)?/ig;
|
||||||
|
|
||||||
const handleExport = () => {
|
const handleExport = () => {
|
||||||
let newConfig = config;
|
let newConfig = config;
|
||||||
const lines = newConfig.split('\n');
|
const lines = newConfig.split('\n');
|
||||||
@@ -160,8 +191,8 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
const option = currentObj[nestedKey];
|
const option = currentObj[nestedKey];
|
||||||
const oldValue = option.value;
|
const oldValue = option.value;
|
||||||
const index = option.line;
|
const index = option.line;
|
||||||
const newKey = nestedKey.replace('#','').trim();
|
const newKey = nestedKey.replace('#', '').trim();
|
||||||
lines[index] = lines[index].replace(` ${nestedKey}: ${oldValue}`,` ${newKey}: ${value}`);
|
lines[index] = lines[index].replace(` ${nestedKey}: ${oldValue}`, ` ${newKey}: ${value}`);
|
||||||
} else {
|
} else {
|
||||||
currentObj = currentObj[nestedKey];
|
currentObj = currentObj[nestedKey];
|
||||||
}
|
}
|
||||||
@@ -176,7 +207,7 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
|
|
||||||
function getInput(name, value) {
|
function getInput(name, value) {
|
||||||
const handleChange = (event) => {
|
const handleChange = (event) => {
|
||||||
const { id, value, type, checked } = event.target;
|
const {id, value, type, checked} = event.target;
|
||||||
|
|
||||||
editedConfig.current = {
|
editedConfig.current = {
|
||||||
...editedConfig.current,
|
...editedConfig.current,
|
||||||
@@ -188,7 +219,8 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
// Handle all the dropdowns
|
// Handle all the dropdowns
|
||||||
case 'remote.auth-type':
|
case 'remote.auth-type':
|
||||||
return (
|
return (
|
||||||
<select key={name} className={styles.formInput} id={name} defaultValue={value} onChange={handleChange}>
|
<select key={name} className={styles.formInput} id={name} defaultValue={value}
|
||||||
|
onChange={handleChange}>
|
||||||
<option value='offline'>offline</option>
|
<option value='offline'>offline</option>
|
||||||
<option value='online'>online</option>
|
<option value='online'>online</option>
|
||||||
<option value='floodgate'>floodgate</option>
|
<option value='floodgate'>floodgate</option>
|
||||||
@@ -196,7 +228,8 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
);
|
);
|
||||||
case 'show-cooldown':
|
case 'show-cooldown':
|
||||||
return (
|
return (
|
||||||
<select key={name} className={styles.formInput} id={name} defaultValue={value} onChange={handleChange}>
|
<select key={name} className={styles.formInput} id={name} defaultValue={value}
|
||||||
|
onChange={handleChange}>
|
||||||
<option value='title'>title</option>
|
<option value='title'>title</option>
|
||||||
<option value='actionbar'>actionbar</option>
|
<option value='actionbar'>actionbar</option>
|
||||||
<option value='false'>false</option>
|
<option value='false'>false</option>
|
||||||
@@ -204,16 +237,19 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
);
|
);
|
||||||
case 'emote-offhand-workaround':
|
case 'emote-offhand-workaround':
|
||||||
return (
|
return (
|
||||||
<select key={name} className={styles.formInput} id={name} defaultValue={value} onChange={handleChange}>
|
<select key={name} className={styles.formInput} id={name} defaultValue={value}
|
||||||
|
onChange={handleChange}>
|
||||||
<option value='disabled'>disabled</option>
|
<option value='disabled'>disabled</option>
|
||||||
<option value='no-emotes'>no-emotes</option>
|
<option value='no-emotes'>no-emotes</option>
|
||||||
<option value='emotes-and-offhand'>emotes-and-offhand</option>
|
<option value='emotes-and-offhand'>emotes-and-offhand</option>
|
||||||
</select>
|
</select>
|
||||||
);
|
);
|
||||||
case 'config-version':
|
case 'config-version':
|
||||||
return <input key={name} className={styles.formInput} type='text' disabled defaultValue={value.replace(/"/g, '')} />;
|
return <input key={name} className={styles.formInput} type='text' disabled
|
||||||
|
defaultValue={value.replace(/"/g, '')}/>;
|
||||||
case 'metrics.uuid':
|
case 'metrics.uuid':
|
||||||
return <input key={name} className={styles.formInput} id={name} type='text' disabled defaultValue={value === 'generateduuid' ? crypto.randomUUID() : value} />;
|
return <input key={name} className={styles.formInput} id={name} type='text' disabled
|
||||||
|
defaultValue={value === 'generateduuid' ? crypto.randomUUID() : value}/>;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// Handle all the other inputs
|
// Handle all the other inputs
|
||||||
@@ -240,7 +276,7 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
|
|
||||||
// From https://gist.github.com/danallison/3ec9d5314788b337b682
|
// From https://gist.github.com/danallison/3ec9d5314788b337b682
|
||||||
function downloadString(text, fileType, fileName) {
|
function downloadString(text, fileType, fileName) {
|
||||||
const blob = new Blob([text], { type: fileType })
|
const blob = new Blob([text], {type: fileType})
|
||||||
|
|
||||||
const a = document.createElement('a')
|
const a = document.createElement('a')
|
||||||
a.download = fileName
|
a.download = fileName
|
||||||
@@ -251,13 +287,16 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
a.click()
|
a.click()
|
||||||
document.body.removeChild(a)
|
document.body.removeChild(a)
|
||||||
|
|
||||||
setTimeout(() => { URL.revokeObjectURL(a.href) }, 1500)
|
setTimeout(() => {
|
||||||
|
URL.revokeObjectURL(a.href)
|
||||||
|
}, 1500)
|
||||||
}
|
}
|
||||||
|
|
||||||
const NoConfigSection = () => (
|
const NoConfigSection = () => (
|
||||||
<>
|
<>
|
||||||
<h1><Translate id='pages.configeditor.noconfigselected.heading'>No config selected.</Translate></h1>
|
<h1><Translate id='pages.configeditor.noconfigselected.heading'>No config selected.</Translate></h1>
|
||||||
<h3 className={styles.fontNormal}><Translate id='pages.configeditor.noconfigselected.subheading'>Upload a file or get started with the default config!</Translate></h3>
|
<h3 className={styles.fontNormal}><Translate id='pages.configeditor.noconfigselected.subheading'>Upload a
|
||||||
|
file or get started with the default config!</Translate></h3>
|
||||||
|
|
||||||
<div className={styles.buttonsContainer}>
|
<div className={styles.buttonsContainer}>
|
||||||
<button className={styles.button} onClick={loadDefault}>
|
<button className={styles.button} onClick={loadDefault}>
|
||||||
@@ -280,7 +319,7 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
|
|
||||||
const EditorView = () => (
|
const EditorView = () => (
|
||||||
<>
|
<>
|
||||||
<div className={styles.buttonsContainer} style={{ marginBottom: '30px' }}>
|
<div className={styles.buttonsContainer} style={{marginBottom: '30px'}}>
|
||||||
<button className={styles.button} onClick={handleExport}>
|
<button className={styles.button} onClick={handleExport}>
|
||||||
<Translate id='pages.configeditor.button.export'>Export</Translate>
|
<Translate id='pages.configeditor.button.export'>Export</Translate>
|
||||||
</button>
|
</button>
|
||||||
@@ -297,8 +336,8 @@ const ConfigEditorPage: React.FC = () => {
|
|||||||
backgroundImage={HeroBackground}
|
backgroundImage={HeroBackground}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<div className='container' style={{ marginTop: '30px' }}>
|
<div className='container' style={{marginTop: '30px'}}>
|
||||||
{Object.keys(parsedConfig).length === 0 ? <NoConfigSection /> : <EditorView />}
|
{Object.keys(parsedConfig).length === 0 ? <NoConfigSection/> : <EditorView/>}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
@@ -315,7 +354,7 @@ export default function ConfigEditor(): JSX.Element {
|
|||||||
>
|
>
|
||||||
<main>
|
<main>
|
||||||
<div className='container container--fluid margin-vert--lg'>
|
<div className='container container--fluid margin-vert--lg'>
|
||||||
<ConfigEditorPage />
|
<ConfigEditorPage/>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|||||||
Reference in new issue
Block a user