6a3848ecb5
Co-authored-by: Cursor <cursoragent@cursor.com>
527 lines
12 KiB
Vue
527 lines
12 KiB
Vue
<template>
|
|
<div class="variable-name-converter">
|
|
<!-- 浮层提示 -->
|
|
<Transition name="toast">
|
|
<div v-if="toastMessage" class="toast-notification" :class="toastType">
|
|
<div class="toast-content">
|
|
<i v-if="toastType === 'error'" class="fas fa-circle-exclamation"></i>
|
|
<i v-else class="fas fa-circle-check"></i>
|
|
<span>{{ toastMessage }}</span>
|
|
</div>
|
|
<button @click="closeToast" class="toast-close-btn" title="关闭">
|
|
<i class="fas fa-xmark"></i>
|
|
</button>
|
|
</div>
|
|
</Transition>
|
|
|
|
<div class="container">
|
|
<div class="conversion-card">
|
|
<!-- 输入区域 -->
|
|
<div class="input-section">
|
|
<div class="input-wrapper">
|
|
<input
|
|
v-model="inputText"
|
|
@input="convertVariableName"
|
|
type="text"
|
|
placeholder="请输入变量名(支持任意格式)"
|
|
class="input-field"
|
|
/>
|
|
<button @click="clearInput" class="clear-btn" title="清空">
|
|
<i class="fas fa-xmark"></i>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 输出区域 -->
|
|
<div class="output-section">
|
|
<div class="output-row">
|
|
<div
|
|
v-for="format in formats.slice(0, 3)"
|
|
:key="format.key"
|
|
class="output-item"
|
|
>
|
|
<div class="output-header">
|
|
<span class="output-label">{{ format.label }}</span>
|
|
<button
|
|
@click="copyToClipboard(format.value, format.label)"
|
|
class="copy-btn"
|
|
:title="`复制${format.label}`"
|
|
>
|
|
<i class="far fa-copy"></i>
|
|
</button>
|
|
</div>
|
|
<div class="output-value" :class="{ empty: !format.value }">
|
|
{{ format.value || '—' }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="output-row">
|
|
<div
|
|
v-for="format in formats.slice(3)"
|
|
:key="format.key"
|
|
class="output-item"
|
|
>
|
|
<div class="output-header">
|
|
<span class="output-label">{{ format.label }}</span>
|
|
<button
|
|
@click="copyToClipboard(format.value, format.label)"
|
|
class="copy-btn"
|
|
:title="`复制${format.label}`"
|
|
>
|
|
<i class="far fa-copy"></i>
|
|
</button>
|
|
</div>
|
|
<div class="output-value" :class="{ empty: !format.value }">
|
|
{{ format.value || '—' }}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ref, computed } from 'vue'
|
|
|
|
const inputText = ref('')
|
|
const toastMessage = ref('')
|
|
const toastType = ref('success')
|
|
let toastTimer = null
|
|
|
|
// 变量名格式定义
|
|
const formats = ref([
|
|
{ key: 'camelCase', label: '小驼峰 (camelCase)', value: '' },
|
|
{ key: 'PascalCase', label: '大驼峰 (PascalCase)', value: '' },
|
|
{ key: 'snake_case', label: '下划线 (snake_case)', value: '' },
|
|
{ key: 'kebab-case', label: '横线 (kebab-case)', value: '' },
|
|
{ key: 'CONSTANT_CASE', label: '常量 (CONSTANT_CASE)', value: '' }
|
|
])
|
|
|
|
// 显示提示
|
|
const showToast = (message, type = 'success', duration = 3000) => {
|
|
toastMessage.value = message
|
|
toastType.value = type
|
|
|
|
if (toastTimer) {
|
|
clearTimeout(toastTimer)
|
|
}
|
|
|
|
toastTimer = setTimeout(() => {
|
|
toastMessage.value = ''
|
|
}, duration)
|
|
}
|
|
|
|
// 关闭提示
|
|
const closeToast = () => {
|
|
if (toastTimer) {
|
|
clearTimeout(toastTimer)
|
|
toastTimer = null
|
|
}
|
|
toastMessage.value = ''
|
|
}
|
|
|
|
// 将输入文本解析为单词数组
|
|
const parseToWords = (text) => {
|
|
if (!text || !text.trim()) {
|
|
return []
|
|
}
|
|
|
|
let processed = text.trim()
|
|
|
|
// 处理各种分隔符:空格、下划线、横线、驼峰
|
|
// 1. 先处理连续大写字母的情况:XMLHttpRequest -> XML Http Request
|
|
processed = processed.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
|
|
|
|
// 2. 先处理数字和字母的边界(必须在驼峰处理之前)
|
|
// 2.1 字母+数字+字母:temp2Detail -> temp 2 Detail
|
|
processed = processed.replace(/([a-zA-Z])(\d+)([a-zA-Z])/g, '$1 $2 $3')
|
|
// 2.2 字母+数字(后面跟着分隔符或结尾,但不是字母):item2 -> item 2
|
|
// 注意:这里不匹配后面跟着字母的情况(已由2.1处理)
|
|
processed = processed.replace(/([a-zA-Z])(\d+)(?=[_\-\s]|$)/g, '$1 $2')
|
|
// 2.3 数字+字母(在单词开头或前面是分隔符):2item -> 2 item
|
|
processed = processed.replace(/(\d+)([a-zA-Z])/g, '$1 $2')
|
|
|
|
// 3. 处理驼峰:camelCase -> camel Case(在数字处理之后)
|
|
processed = processed.replace(/([a-z])([A-Z])/g, '$1 $2')
|
|
|
|
// 4. 统一分隔符:下划线、横线、空格统一为空格
|
|
processed = processed.replace(/[_\-\s]+/g, ' ')
|
|
|
|
// 5. 分割并处理
|
|
let words = processed
|
|
.split(' ')
|
|
.filter(word => word.length > 0)
|
|
.map(word => {
|
|
// 转换为小写,保留字母和数字
|
|
return word.toLowerCase()
|
|
})
|
|
.filter(word => word.length > 0) // 允许纯数字
|
|
|
|
return words
|
|
}
|
|
|
|
// 转换单词首字母为大写(处理数字情况)
|
|
const capitalizeWord = (word) => {
|
|
if (!word) return ''
|
|
// 如果单词是纯数字,直接返回
|
|
if (/^\d+$/.test(word)) return word
|
|
// 否则首字母大写
|
|
return word.charAt(0).toUpperCase() + word.slice(1)
|
|
}
|
|
|
|
// 转换为小驼峰 (camelCase)
|
|
const toCamelCase = (words) => {
|
|
if (words.length === 0) return ''
|
|
|
|
const firstWord = words[0]
|
|
const restWords = words.slice(1).map(word => capitalizeWord(word))
|
|
|
|
return firstWord + restWords.join('')
|
|
}
|
|
|
|
// 转换为大驼峰 (PascalCase)
|
|
const toPascalCase = (words) => {
|
|
if (words.length === 0) return ''
|
|
|
|
return words.map(word => capitalizeWord(word)).join('')
|
|
}
|
|
|
|
// 转换为下划线 (snake_case)
|
|
const toSnakeCase = (words) => {
|
|
if (words.length === 0) return ''
|
|
|
|
return words.join('_')
|
|
}
|
|
|
|
// 转换为横线 (kebab-case)
|
|
const toKebabCase = (words) => {
|
|
if (words.length === 0) return ''
|
|
|
|
return words.join('-')
|
|
}
|
|
|
|
// 转换为常量 (CONSTANT_CASE)
|
|
const toConstantCase = (words) => {
|
|
if (words.length === 0) return ''
|
|
|
|
return words.map(word => word.toUpperCase()).join('_')
|
|
}
|
|
|
|
// 转换变量名
|
|
const convertVariableName = () => {
|
|
const words = parseToWords(inputText.value)
|
|
|
|
if (words.length === 0) {
|
|
formats.value.forEach(format => {
|
|
format.value = ''
|
|
})
|
|
return
|
|
}
|
|
|
|
formats.value.forEach(format => {
|
|
switch (format.key) {
|
|
case 'camelCase':
|
|
format.value = toCamelCase(words)
|
|
break
|
|
case 'PascalCase':
|
|
format.value = toPascalCase(words)
|
|
break
|
|
case 'snake_case':
|
|
format.value = toSnakeCase(words)
|
|
break
|
|
case 'kebab-case':
|
|
format.value = toKebabCase(words)
|
|
break
|
|
case 'CONSTANT_CASE':
|
|
format.value = toConstantCase(words)
|
|
break
|
|
}
|
|
})
|
|
}
|
|
|
|
// 清空输入
|
|
const clearInput = () => {
|
|
inputText.value = ''
|
|
convertVariableName()
|
|
}
|
|
|
|
// 复制到剪贴板
|
|
const copyToClipboard = async (text, label) => {
|
|
if (!text || text === '—') {
|
|
showToast('没有可复制的内容', 'error')
|
|
return
|
|
}
|
|
|
|
try {
|
|
await navigator.clipboard.writeText(text)
|
|
showToast(`${label}已复制到剪贴板`, 'success', 2000)
|
|
} catch (error) {
|
|
showToast('复制失败:' + error.message, 'error')
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<style scoped>
|
|
.variable-name-converter {
|
|
width: 100%;
|
|
min-height: 100vh;
|
|
background: #f5f5f5;
|
|
padding: 2rem 1rem;
|
|
}
|
|
|
|
.container {
|
|
max-width: 900px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
.conversion-card {
|
|
background: #ffffff;
|
|
border-radius: 8px;
|
|
padding: 2rem;
|
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
|
|
border: 1px solid #e5e5e5;
|
|
}
|
|
|
|
.input-section {
|
|
margin-bottom: 2rem;
|
|
}
|
|
|
|
.input-label {
|
|
display: block;
|
|
font-size: 0.875rem;
|
|
font-weight: 500;
|
|
color: #333333;
|
|
margin-bottom: 0.5rem;
|
|
}
|
|
|
|
.input-wrapper {
|
|
position: relative;
|
|
display: flex;
|
|
align-items: center;
|
|
}
|
|
|
|
.input-field {
|
|
flex: 1;
|
|
padding: 0.75rem;
|
|
padding-right: 2.5rem;
|
|
border: 1px solid #d0d0d0;
|
|
border-radius: 6px;
|
|
font-size: 0.9375rem;
|
|
font-family: 'Courier New', monospace;
|
|
transition: all 0.2s;
|
|
}
|
|
|
|
.input-field:focus {
|
|
outline: none;
|
|
border-color: #1a1a1a;
|
|
box-shadow: 0 0 0 3px rgba(26, 26, 26, 0.1);
|
|
}
|
|
|
|
.clear-btn {
|
|
position: absolute;
|
|
right: 0.5rem;
|
|
padding: 0.375rem;
|
|
background: transparent;
|
|
border: none;
|
|
border-radius: 4px;
|
|
color: #666666;
|
|
cursor: pointer;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
transition: all 0.2s;
|
|
width: 24px;
|
|
height: 24px;
|
|
}
|
|
|
|
.clear-btn:hover {
|
|
background: #f5f5f5;
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.output-section {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 1rem;
|
|
}
|
|
|
|
.output-row {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
gap: 1rem;
|
|
}
|
|
|
|
.output-item {
|
|
border: 1px solid #e5e5e5;
|
|
border-radius: 6px;
|
|
padding: 1rem;
|
|
background: #fafafa;
|
|
transition: all 0.2s;
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
|
|
.output-item:hover {
|
|
border-color: #d0d0d0;
|
|
background: #ffffff;
|
|
}
|
|
|
|
.output-header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 0.5rem;
|
|
}
|
|
|
|
.output-label {
|
|
font-size: 0.875rem;
|
|
font-weight: 500;
|
|
color: #333333;
|
|
}
|
|
|
|
.copy-btn {
|
|
padding: 0.375rem;
|
|
background: transparent;
|
|
border: 1px solid #d0d0d0;
|
|
border-radius: 4px;
|
|
color: #666666;
|
|
cursor: pointer;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
transition: all 0.2s;
|
|
width: 28px;
|
|
height: 28px;
|
|
}
|
|
|
|
.copy-btn:hover {
|
|
background: #f5f5f5;
|
|
border-color: #1a1a1a;
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.copy-btn:active {
|
|
transform: scale(0.98);
|
|
}
|
|
|
|
.copy-btn i {
|
|
font-size: 0.875rem;
|
|
}
|
|
|
|
.output-value {
|
|
font-family: 'Courier New', monospace;
|
|
font-size: 1rem;
|
|
color: #1a1a1a;
|
|
word-break: break-all;
|
|
min-height: 1.5rem;
|
|
padding: 0.5rem 0;
|
|
}
|
|
|
|
.output-value.empty {
|
|
color: #999999;
|
|
}
|
|
|
|
/* Toast通知样式 */
|
|
.toast-notification {
|
|
position: fixed;
|
|
top: 80px;
|
|
right: 20px;
|
|
background: #ffffff;
|
|
border: 1px solid #e5e5e5;
|
|
border-radius: 8px;
|
|
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
|
padding: 1rem 1.25rem;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.75rem;
|
|
z-index: 1000;
|
|
min-width: 280px;
|
|
max-width: 400px;
|
|
}
|
|
|
|
.toast-notification.success {
|
|
border-left: 4px solid #10b981;
|
|
}
|
|
|
|
.toast-notification.error {
|
|
border-left: 4px solid #ef4444;
|
|
}
|
|
|
|
.toast-content {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 0.5rem;
|
|
flex: 1;
|
|
font-size: 0.875rem;
|
|
color: #333333;
|
|
}
|
|
|
|
.toast-content i {
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.toast-notification.success .toast-content i {
|
|
color: #10b981;
|
|
}
|
|
|
|
.toast-notification.error .toast-content i {
|
|
color: #ef4444;
|
|
}
|
|
|
|
.toast-close-btn {
|
|
background: transparent;
|
|
border: none;
|
|
color: #666666;
|
|
cursor: pointer;
|
|
padding: 0.25rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
transition: color 0.2s;
|
|
flex-shrink: 0;
|
|
}
|
|
|
|
.toast-close-btn:hover {
|
|
color: #1a1a1a;
|
|
}
|
|
|
|
.toast-enter-active,
|
|
.toast-leave-active {
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
.toast-enter-from {
|
|
opacity: 0;
|
|
transform: translateX(100%);
|
|
}
|
|
|
|
.toast-leave-to {
|
|
opacity: 0;
|
|
transform: translateX(100%);
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.variable-name-converter {
|
|
padding: 1rem 0.5rem;
|
|
}
|
|
|
|
.conversion-card {
|
|
padding: 1.5rem;
|
|
}
|
|
|
|
.output-row {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
|
|
.toast-notification {
|
|
right: 10px;
|
|
left: 10px;
|
|
max-width: none;
|
|
top: 60px;
|
|
}
|
|
}
|
|
</style>
|
|
|