init
This commit is contained in:
2457
src/views/Comparator.vue
Normal file
2457
src/views/Comparator.vue
Normal file
File diff suppressed because it is too large
Load Diff
@@ -30,11 +30,26 @@ const tools = ref([
|
||||
title: 'JSON',
|
||||
description: '格式化、验证和美化JSON数据'
|
||||
},
|
||||
{
|
||||
path: '/comparator',
|
||||
title: '对比',
|
||||
description: '文本和JSON对比工具'
|
||||
},
|
||||
{
|
||||
path: '/encoder-decoder',
|
||||
title: '编解码',
|
||||
description: '编码/解码工具'
|
||||
},
|
||||
{
|
||||
path: '/variable-name',
|
||||
title: '变量名',
|
||||
description: '变量名格式转换'
|
||||
},
|
||||
{
|
||||
path: '/qr-code',
|
||||
title: '二维码',
|
||||
description: '生成二维码'
|
||||
},
|
||||
{
|
||||
path: '/timestamp-converter',
|
||||
title: '时间戳',
|
||||
|
||||
700
src/views/QRCodeGenerator.vue
Normal file
700
src/views/QRCodeGenerator.vue
Normal file
@@ -0,0 +1,700 @@
|
||||
<template>
|
||||
<div class="tool-page">
|
||||
<!-- 浮层提示 -->
|
||||
<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="main-container">
|
||||
<!-- 左侧侧栏(历史记录) -->
|
||||
<div class="sidebar" :class="{ 'sidebar-open': sidebarOpen }">
|
||||
<div class="sidebar-header">
|
||||
<h3>历史记录</h3>
|
||||
<button @click="toggleSidebar" class="close-btn">×</button>
|
||||
</div>
|
||||
<div class="sidebar-content">
|
||||
<div v-if="historyList.length === 0" class="empty-history">
|
||||
暂无历史记录
|
||||
</div>
|
||||
<div
|
||||
v-for="(item, index) in historyList"
|
||||
:key="index"
|
||||
class="history-item"
|
||||
@click="loadHistory(item.text)"
|
||||
>
|
||||
<div class="history-time">{{ formatTime(item.time) }}</div>
|
||||
<div class="history-preview">{{ truncateText(item.text, 50) }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 主内容区域 -->
|
||||
<div class="content-wrapper" :class="{ 'sidebar-pushed': sidebarOpen }">
|
||||
<div class="container">
|
||||
<div class="qr-card">
|
||||
<!-- 输入区域 -->
|
||||
<div class="input-section">
|
||||
<div class="input-wrapper">
|
||||
<textarea
|
||||
v-model="inputText"
|
||||
@keydown.enter.prevent="generateQRCode"
|
||||
placeholder="请输入要生成二维码的内容"
|
||||
class="input-textarea"
|
||||
rows="4"
|
||||
></textarea>
|
||||
</div>
|
||||
<button @click="generateQRCode" class="generate-btn">
|
||||
<i class="fas fa-qrcode"></i>
|
||||
生成二维码
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 二维码显示区域 -->
|
||||
<div v-if="qrCodeDataUrl" class="qr-display-section">
|
||||
<div class="qr-code-wrapper">
|
||||
<img :src="qrCodeDataUrl" alt="二维码" class="qr-code-image" />
|
||||
</div>
|
||||
<div class="qr-actions">
|
||||
<button @click="downloadQRCode" class="action-btn">
|
||||
<i class="fas fa-download"></i>
|
||||
下载
|
||||
</button>
|
||||
<button @click="copyQRCodeImage" class="action-btn">
|
||||
<i class="far fa-copy"></i>
|
||||
复制图片
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 侧栏切换按钮 -->
|
||||
<div class="sidebar-toggle">
|
||||
<button @click="toggleSidebar" class="toggle-btn">
|
||||
{{ sidebarOpen ? '◀' : '▶' }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { ref, onMounted } from 'vue'
|
||||
import QRCode from 'qrcode'
|
||||
|
||||
// 输入文本
|
||||
const inputText = ref('')
|
||||
// 二维码数据URL
|
||||
const qrCodeDataUrl = ref('')
|
||||
// 侧栏状态
|
||||
const sidebarOpen = ref(false)
|
||||
|
||||
// 历史记录
|
||||
const historyList = ref([])
|
||||
const STORAGE_KEY = 'qr-code-history'
|
||||
const MAX_HISTORY = 20
|
||||
|
||||
// 提示消息
|
||||
const toastMessage = ref('')
|
||||
const toastType = ref('success')
|
||||
let toastTimer = null
|
||||
|
||||
// 显示提示
|
||||
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 generateQRCode = async () => {
|
||||
if (!inputText.value.trim()) {
|
||||
showToast('请输入要生成二维码的内容', 'error')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 生成二维码
|
||||
const dataUrl = await QRCode.toDataURL(inputText.value.trim(), {
|
||||
width: 300,
|
||||
margin: 2,
|
||||
color: {
|
||||
dark: '#000000',
|
||||
light: '#FFFFFF'
|
||||
}
|
||||
})
|
||||
|
||||
qrCodeDataUrl.value = dataUrl
|
||||
|
||||
// 保存到历史记录
|
||||
saveToHistory(inputText.value.trim())
|
||||
|
||||
showToast('二维码生成成功', 'success', 2000)
|
||||
} catch (error) {
|
||||
showToast('生成二维码失败:' + error.message, 'error')
|
||||
qrCodeDataUrl.value = ''
|
||||
}
|
||||
}
|
||||
|
||||
// 下载二维码
|
||||
const downloadQRCode = () => {
|
||||
if (!qrCodeDataUrl.value) {
|
||||
showToast('没有可下载的二维码', 'error')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const link = document.createElement('a')
|
||||
link.download = `qrcode-${Date.now()}.png`
|
||||
link.href = qrCodeDataUrl.value
|
||||
link.click()
|
||||
showToast('下载成功', 'success', 2000)
|
||||
} catch (error) {
|
||||
showToast('下载失败:' + error.message, 'error')
|
||||
}
|
||||
}
|
||||
|
||||
// 复制二维码图片
|
||||
const copyQRCodeImage = async () => {
|
||||
if (!qrCodeDataUrl.value) {
|
||||
showToast('没有可复制的二维码', 'error')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 将 data URL 转换为 blob
|
||||
const response = await fetch(qrCodeDataUrl.value)
|
||||
const blob = await response.blob()
|
||||
|
||||
// 复制到剪贴板
|
||||
await navigator.clipboard.write([
|
||||
new ClipboardItem({
|
||||
'image/png': blob
|
||||
})
|
||||
])
|
||||
|
||||
showToast('已复制到剪贴板', 'success', 2000)
|
||||
} catch (error) {
|
||||
// 降级方案:提示用户手动保存
|
||||
showToast('复制失败,请使用下载功能', 'error')
|
||||
}
|
||||
}
|
||||
|
||||
// 保存到历史记录
|
||||
const saveToHistory = (text) => {
|
||||
const historyItem = {
|
||||
text: text,
|
||||
time: Date.now()
|
||||
}
|
||||
|
||||
// 从localStorage读取现有历史
|
||||
let history = []
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY)
|
||||
if (stored) {
|
||||
history = JSON.parse(stored)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('读取历史记录失败', e)
|
||||
}
|
||||
|
||||
// 避免重复保存相同的记录
|
||||
const lastHistory = history[0]
|
||||
if (lastHistory && lastHistory.text === historyItem.text) {
|
||||
return
|
||||
}
|
||||
|
||||
// 添加到开头
|
||||
history.unshift(historyItem)
|
||||
|
||||
// 限制最多20条
|
||||
if (history.length > MAX_HISTORY) {
|
||||
history = history.slice(0, MAX_HISTORY)
|
||||
}
|
||||
|
||||
// 保存到localStorage
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(history))
|
||||
loadHistoryList()
|
||||
} catch (e) {
|
||||
console.error('保存历史记录失败', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载历史记录列表
|
||||
const loadHistoryList = () => {
|
||||
try {
|
||||
const stored = localStorage.getItem(STORAGE_KEY)
|
||||
if (stored) {
|
||||
historyList.value = JSON.parse(stored)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('加载历史记录失败', e)
|
||||
historyList.value = []
|
||||
}
|
||||
}
|
||||
|
||||
// 加载历史记录
|
||||
const loadHistory = (text) => {
|
||||
inputText.value = text
|
||||
generateQRCode()
|
||||
}
|
||||
|
||||
// 切换侧栏
|
||||
const toggleSidebar = () => {
|
||||
sidebarOpen.value = !sidebarOpen.value
|
||||
}
|
||||
|
||||
// 格式化时间
|
||||
const formatTime = (timestamp) => {
|
||||
const date = new Date(timestamp)
|
||||
const now = new Date()
|
||||
const diff = now - date
|
||||
|
||||
if (diff < 60000) return '刚刚'
|
||||
if (diff < 3600000) return Math.floor(diff / 60000) + '分钟前'
|
||||
if (diff < 86400000) return Math.floor(diff / 3600000) + '小时前'
|
||||
|
||||
return date.toLocaleString('zh-CN', {
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
})
|
||||
}
|
||||
|
||||
// 截断文本
|
||||
const truncateText = (text, maxLength) => {
|
||||
if (text.length <= maxLength) return text
|
||||
return text.substring(0, maxLength) + '...'
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadHistoryList()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.tool-page {
|
||||
height: calc(100vh - 64px);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
margin: -1rem;
|
||||
padding: 0;
|
||||
background: #ffffff;
|
||||
}
|
||||
|
||||
.main-container {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
background: #ffffff;
|
||||
}
|
||||
|
||||
/* 侧栏样式 */
|
||||
.sidebar {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
width: 300px;
|
||||
background: #ffffff;
|
||||
border-right: 1px solid #e5e5e5;
|
||||
transform: translateX(-100%);
|
||||
transition: transform 0.3s ease;
|
||||
z-index: 10;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
|
||||
}
|
||||
|
||||
.sidebar-open {
|
||||
transform: translateX(0);
|
||||
}
|
||||
|
||||
.content-wrapper.sidebar-pushed {
|
||||
margin-left: 300px;
|
||||
}
|
||||
|
||||
.sidebar-header {
|
||||
padding: 1rem;
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
background: #ffffff;
|
||||
}
|
||||
|
||||
.sidebar-header h3 {
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
color: #1a1a1a;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.close-btn {
|
||||
background: none;
|
||||
border: none;
|
||||
font-size: 1.5rem;
|
||||
cursor: pointer;
|
||||
color: #666666;
|
||||
padding: 0;
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
.close-btn:hover {
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.sidebar-content {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
padding: 0.5rem;
|
||||
background: #ffffff;
|
||||
}
|
||||
|
||||
.empty-history {
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
color: #999999;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.history-item {
|
||||
padding: 0.75rem;
|
||||
margin-bottom: 0.5rem;
|
||||
background: #ffffff;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
border: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
.history-item:hover {
|
||||
background: #f5f5f5;
|
||||
border-color: #d0d0d0;
|
||||
}
|
||||
|
||||
.history-time {
|
||||
font-size: 0.75rem;
|
||||
color: #999999;
|
||||
margin-bottom: 0.25rem;
|
||||
}
|
||||
|
||||
.history-preview {
|
||||
font-size: 0.875rem;
|
||||
color: #1a1a1a;
|
||||
word-break: break-all;
|
||||
}
|
||||
|
||||
.content-wrapper {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
transition: margin-left 0.3s ease;
|
||||
overflow-y: auto;
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.qr-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 {
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
|
||||
.input-textarea {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
border: 1px solid #d0d0d0;
|
||||
border-radius: 6px;
|
||||
font-size: 0.9375rem;
|
||||
font-family: inherit;
|
||||
resize: vertical;
|
||||
transition: all 0.2s;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.input-textarea:focus {
|
||||
outline: none;
|
||||
border-color: #1a1a1a;
|
||||
box-shadow: 0 0 0 3px rgba(26, 26, 26, 0.1);
|
||||
}
|
||||
|
||||
.generate-btn {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: #1a1a1a;
|
||||
color: #ffffff;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
font-size: 0.9375rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.generate-btn:hover {
|
||||
background: #333333;
|
||||
}
|
||||
|
||||
.generate-btn:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
.qr-display-section {
|
||||
margin-top: 2rem;
|
||||
padding-top: 2rem;
|
||||
border-top: 1px solid #e5e5e5;
|
||||
}
|
||||
|
||||
.qr-code-wrapper {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
padding: 1.5rem;
|
||||
background: #fafafa;
|
||||
border-radius: 8px;
|
||||
margin-bottom: 1.5rem;
|
||||
}
|
||||
|
||||
.qr-code-image {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.qr-actions {
|
||||
display: flex;
|
||||
gap: 0.75rem;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
padding: 0.75rem 1.5rem;
|
||||
background: #f5f5f5;
|
||||
color: #333333;
|
||||
border: 1px solid #d0d0d0;
|
||||
border-radius: 6px;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.action-btn:hover {
|
||||
background: #e5e5e5;
|
||||
border-color: #1a1a1a;
|
||||
color: #1a1a1a;
|
||||
}
|
||||
|
||||
.action-btn:active {
|
||||
transform: scale(0.98);
|
||||
}
|
||||
|
||||
/* 侧栏切换按钮 */
|
||||
.sidebar-toggle {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
bottom: 1rem;
|
||||
z-index: 11;
|
||||
}
|
||||
|
||||
.content-wrapper.sidebar-pushed .sidebar-toggle {
|
||||
left: 300px;
|
||||
}
|
||||
|
||||
.toggle-btn {
|
||||
width: 32px;
|
||||
height: 48px;
|
||||
background: #1a1a1a;
|
||||
color: #ffffff;
|
||||
border: none;
|
||||
border-radius: 0 4px 4px 0;
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: all 0.2s;
|
||||
box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.toggle-btn:hover {
|
||||
background: #333333;
|
||||
}
|
||||
|
||||
/* 提示消息样式 */
|
||||
.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 svg,
|
||||
.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) {
|
||||
.qr-card {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
width: 280px;
|
||||
}
|
||||
|
||||
.content-wrapper.sidebar-pushed {
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
.sidebar-toggle {
|
||||
bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.toggle-btn {
|
||||
width: 28px;
|
||||
height: 40px;
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.toast-notification {
|
||||
right: 10px;
|
||||
left: 10px;
|
||||
max-width: none;
|
||||
top: 60px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
526
src/views/VariableNameConverter.vue
Normal file
526
src/views/VariableNameConverter.vue
Normal file
@@ -0,0 +1,526 @@
|
||||
<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>
|
||||
|
||||
Reference in New Issue
Block a user