From b582b92414d620058704d57b596385325bcabee1 Mon Sep 17 00:00:00 2001 From: qscbm187531 <1309635304@qq.com> Date: Sun, 18 Aug 2024 20:47:49 +0800 Subject: [PATCH] Revise utilities (#28) * Overhaul of config-editor * Update default card header * Update config-editor.tsx * Update config-editor.tsx * Update config-editor.tsx * Revert "Merge branch 'patch5-temp' into patch-5" This reverts commit 538eab6ac64cb2e633891ecb284732ed9ea3e98d, reversing changes made to b0538f6e3c40d5dfc1946c93e11577af7814aefd. * Update config-editor.tsx * Revert "Update config-editor.tsx" This reverts commit 68dc70f216bb99359f2999548d6cf940675a2657. * Update utilities.module.scss * Revert "Update utilities.module.scss" This reverts commit d9d14756d655310196dfb07d9fab1e783a961273. * Update dump-viewer.tsx --- src/pages/utilities/config-editor.tsx | 92 +++++++++++++-------------- src/pages/utilities/dump-viewer.tsx | 4 +- 2 files changed, 46 insertions(+), 50 deletions(-) diff --git a/src/pages/utilities/config-editor.tsx b/src/pages/utilities/config-editor.tsx index 1f1f090..31d83e4 100644 --- a/src/pages/utilities/config-editor.tsx +++ b/src/pages/utilities/config-editor.tsx @@ -8,7 +8,8 @@ import { useRef, useState } from 'react'; const ConfigEditorPage: React.FC = () => { const [config, setConfig] = useState(''); const [parsedConfig, setParsedConfig] = useState({}); - + // match optional key + const keyReg = /^#? ?[^:\sA-Z]+:(?!\S)/ const editedConfig = useRef({}); const uploadRef = useRef(null); @@ -55,7 +56,8 @@ const ConfigEditorPage: React.FC = () => { const newConfig = {}; // Parse each line of the config - lines.forEach(line => { + for (let i = 0; i < lines.length; i++) { + let line = lines[i]; // Reset the section if we are no indented if (!line.startsWith(' ')) { currentSection = ''; @@ -65,80 +67,73 @@ const ConfigEditorPage: React.FC = () => { line = line.trim() if (line === '') { currentComment = ''; - return; + continue; } - // Check if we are in a comment - if (line.startsWith('#')) { - if (!line.includes('# ') && line.includes(': ')) { - const splitLine = line.split(':'); - splitLine[0] = splitLine[0].trim(); - splitLine[1] = splitLine[1].trim(); - if (splitLine[0] === '#proxy-protocol-whitelisted-ips') { - currentComment = ''; - return; - } - currentComment += 'Note: This option is commented on, and if you change it, it will be automatically uncommented.' + '
'; - // Add the config option to the config object - newConfig[currentSection] = newConfig[currentSection] || {}; - newConfig[currentSection][splitLine[0]] = { - desc: currentComment, - value: splitLine[1] - }; + const commented = line.startsWith('#'); - // Reset the comment - currentComment = ''; - return; + const match = line.match(keyReg) + if (match === null) { + // Check if we are in a comment + if (commented) { + currentComment += line.replace(/^# ?/i, '') + '
'; } - currentComment += line.replace(/^# ?/i, '') + '
'; - return; + continue; } - // Ignore these lines since they will fail to parse - if (!line.includes(':')) { - return; + // Check if we are in a new section + if (line.endsWith(':')) { + currentSection = line.replace(':', ''); + currentComment = ''; + continue; + } + + // Ignore user auths section + if (currentSection === 'saved-user-logins') { + continue; } // Get the key and value const splitLine = line.split(':'); splitLine[0] = splitLine[0].trim(); - splitLine[1] = splitLine[1].trim(); + let val = ''; + for (let j = 1;j'; } // Add the config option to the config object newConfig[currentSection] = newConfig[currentSection] || {}; newConfig[currentSection][splitLine[0]] = { desc: currentComment, - value: splitLine[1] + value: val, + line: i }; // Reset the comment currentComment = ''; - }) - + } return newConfig; } const generateHTML = (config: any) => { return Object.keys(config).map(configKey => (
-
{configKey === '' ? '.' : configKey}
+
{configKey === '' ? 'Root' : configKey}
{Object.keys(config[configKey]).map(configOption => { const configOptionInfo = config[configKey][configOption]; const configOptionKey = configKey === '' ? configOption : `${configKey}.${configOption}`; - const showOptionName = configOption.replace('#','') + const showOptionName = configOption.replace('#','').trim() return (

{showOptionName}

@@ -154,7 +149,7 @@ const ConfigEditorPage: React.FC = () => { const handleExport = () => { let newConfig = config; - + const lines = newConfig.split('\n'); Object.entries(editedConfig.current).forEach(([key, value]) => { const keys = key.split('.'); let currentObj = parsedConfig; @@ -162,19 +157,18 @@ const ConfigEditorPage: React.FC = () => { keys.forEach((nestedKey, index) => { if (currentObj.hasOwnProperty(nestedKey)) { if (index === keys.length - 1) { - const oldValue = currentObj[nestedKey].value; - if (nestedKey.startsWith('#')) { - const newKey = nestedKey.replace('#',''); - newConfig = newConfig.replace(` ${nestedKey}: ${oldValue}`, ` ${newKey}: ${value}`); - } else { - newConfig = newConfig.replace(` ${nestedKey}: ${oldValue}`, ` ${nestedKey}: ${value}`); - } + const option = currentObj[nestedKey]; + const oldValue = option.value; + const index = option.line; + const newKey = nestedKey.replace('#','').trim(); + lines[index] = lines[index].replace(` ${nestedKey}: ${oldValue}`,` ${newKey}: ${value}`); } else { currentObj = currentObj[nestedKey]; } } }); }); + newConfig = lines.join('\n'); // Trigger a download of the final config downloadString(newConfig, 'text/plain', 'config.yml'); diff --git a/src/pages/utilities/dump-viewer.tsx b/src/pages/utilities/dump-viewer.tsx index 341d785..c749c65 100644 --- a/src/pages/utilities/dump-viewer.tsx +++ b/src/pages/utilities/dump-viewer.tsx @@ -16,6 +16,8 @@ const DumpViewerPage: React.FC = () => { const pluginPopoverRef = useRef(null); + const urlReg = /^(https?:\/\/)?dump\.geysermc\.org/; + useEffect(() => { const handleClickOutside = (event) => setActivePlugin({}); document.addEventListener("mousedown", handleClickOutside); @@ -44,7 +46,7 @@ const DumpViewerPage: React.FC = () => { } let id = dumpId; - if (dumpId.startsWith('https://dump.geysermc.org/')) { + if (dumpId.match(urlReg)) { id = dumpId.split('/').pop(); } setDumpId(id);