From 994648780fc62a2a59fe17d3baa4b0083c9d2d51 Mon Sep 17 00:00:00 2001 From: xtyuns Date: Tue, 17 Sep 2024 12:49:16 +0800 Subject: [PATCH] feature: var name transform (#936) --- src-tauri/src/main.rs | 2 +- .../Translate/components/SourceArea/index.jsx | 120 ++++++++++++++++-- 2 files changed, 109 insertions(+), 13 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 62534b76ef..61804c5b97 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -101,7 +101,7 @@ fn main() { } match get("proxy_enable") { Some(v) => { - if v.as_bool().unwrap() { + if v.as_bool().unwrap() && get("proxy_host").map_or(false, |host| !host.as_str().unwrap().is_empty()) { let _ = set_proxy(); } } diff --git a/src/window/Translate/components/SourceArea/index.jsx b/src/window/Translate/components/SourceArea/index.jsx index 4c4f1c7d26..878a101ba6 100644 --- a/src/window/Translate/components/SourceArea/index.jsx +++ b/src/window/Translate/components/SourceArea/index.jsx @@ -20,6 +20,8 @@ import * as recognizeServices from '../../../../services/recognize'; import * as builtinTtsServices from '../../../../services/tts'; import detect from '../../../../utils/lang_detect'; import { store } from '../../../../utils/store'; +import { info } from 'tauri-plugin-log-api'; +import { debug } from 'tauri-plugin-log-api'; export const sourceTextAtom = atom(''); export const detectLanguageAtom = atom(''); @@ -256,6 +258,111 @@ export default function SourceArea(props) { setDetectLanguage(await detect(text)); }; + let sourceTextChangeTimer = null; + const changeSourceText = async (text) => { + setDetectLanguage(''); + await setSourceText(text); + if (dynamicTranslate) { + if (sourceTextChangeTimer) { + clearTimeout(sourceTextChangeTimer); + } + sourceTextChangeTimer = setTimeout(() => { + detect_language(text).then(() => { + syncSourceText(); + }); + }, 1000); + } + } + + const transformVarName = function (str) { + let str2 = str; + + // snake_case to SNAKE_CASE + if (/_[a-z]/.test(str2)) { + str2 = str2.split('_').map(it => it.toLocaleUpperCase()).join('_'); + } + if (str2 !== str) { + return str2; + } + + // SNAKE_CASE to kebab-case + if (/^[A-Z]+(_[A-Z]+)*$/.test(str2)) { + str2 = str2.split('_').map(it => it.toLocaleLowerCase()).join('-'); + } + if (str2 !== str) { + return str2; + } + + // kebab-case to dot.notation + if (/-/.test(str2)) { + str2 = str2.split('-').map(it => it.toLocaleLowerCase()).join('.'); + } + if (str2 !== str) { + return str2; + } + + // dot.notation to space separated + if (/\.[a-z]/.test(str2)) { + str2 = str2.replaceAll(/(\.)([a-z])/g, (_, _2, it) => ' ' + it); + } + if (str2 !== str) { + return str2; + } + + // space separated to Title Case + if (/\s[a-z]/.test(str2)) { + str2 = str2.replaceAll(/\s([a-z])/g, (_, it) => ' ' + it.toLocaleUpperCase()); + str2 = str2.substring(0, 1).toLocaleUpperCase() + str2.substring(1); + } + if (str2 !== str) { + return str2; + } + + // Title Case to CamelCase + if (/\s[A-Z]/.test(str2)) { + str2 = str2.replaceAll(/\s([A-Z])/g, (_, it) => it); + str2 = str2.substring(0, 1).toLocaleLowerCase() + str2.substring(1); + } + if (str2 !== str) { + return str2; + } + + // CamelCase to PascalCase + if (/^[a-z]+[A-Z]+/.test(str2)) { + str2 = str2.substring(0, 1).toLocaleUpperCase() + str2.substring(1); + } + if (str2 !== str) { + return str2; + } + + // PascalCase to snake_case + if (/[^\s][A-Z]/.test(str2)) { + str2 = str2.replaceAll(/[A-Z]/g, (it, offset) => { + return (offset == 0 ? '' : '_') + it.toLocaleLowerCase(); + }); + } + + return str2; + } + useEffect(() => { + textAreaRef.current.addEventListener("keydown", async (event) => { + if (event.altKey && event.shiftKey && event.code === 'KeyU') { + const originText = textAreaRef.current.value; + const selectionStart = textAreaRef.current.selectionStart; + const selectionEnd = textAreaRef.current.selectionEnd; + const selectionText = originText.substring(selectionStart, selectionEnd); + + const convertedText = transformVarName(selectionText); + const targetText = originText.substring(0, selectionStart) + convertedText + originText.substring(selectionEnd); + + await changeSourceText(targetText); + textAreaRef.current.selectionStart = selectionStart; + textAreaRef.current.selectionEnd = selectionStart + convertedText.length; + } + }); + }, [textAreaRef]); + + return (
{ const v = e.target.value; - setDetectLanguage(''); - setSourceText(v); - if (dynamicTranslate) { - if (timer) { - clearTimeout(timer); - } - timer = setTimeout(() => { - detect_language(v).then(() => { - syncSourceText(); - }); - }, 1000); - } + changeSourceText(v); }} />