From 4a37c27938bedad499f12b2d8b2a3e86fae769dc Mon Sep 17 00:00:00 2001 From: changjunqiang <344656718@qq.com> Date: Tue, 16 Jun 2026 13:18:49 +0800 Subject: [PATCH] add python --- src/views/ChapterView.vue | 29 +++++++++++++++++++++++++---- vite.config.js | 30 +++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/views/ChapterView.vue b/src/views/ChapterView.vue index 6ea6c65..37d525f 100644 --- a/src/views/ChapterView.vue +++ b/src/views/ChapterView.vue @@ -85,7 +85,12 @@ >C @@ -129,11 +134,16 @@ const chapterId = computed(() => props.id || route.params.id) const currentFile = ref(null) const language = ref('c') +// 判断当前文件是否有 Python 版本 +const hasPythonVersion = computed(() => { + return currentFile.value && !!currentFile.value.pyPath +}) + // 根据语言生成文件路径 const currentFilePath = computed(() => { if (!currentFile.value) return null if (language.value === 'python') { - return currentFile.value.pyPath || currentFile.value.path.replace(/^c\//, 'py/').replace(/\.c$/, '.py') + return currentFile.value.pyPath } return currentFile.value.path }) @@ -143,11 +153,17 @@ const currentFileName = computed(() => { if (language.value === 'python') { return currentFile.value.pyPath ? currentFile.value.pyPath.split('/').pop() - : currentFile.value.name.replace(/\.c$/, '.py') + : currentFile.value.name } return currentFile.value.name }) +// 切换语言时,如果目标语言没有对应文件则保持当前语言 +function switchLanguage(lang) { + if (lang === 'python' && !hasPythonVersion.value) return + language.value = lang +} + function switchLanguage(lang) { language.value = lang } @@ -458,7 +474,7 @@ function getFolderDescription(folderName) { font-family: var(--mono); } -.lang-tab:hover { +.lang-tab:hover:not(:disabled) { color: var(--text-primary); } @@ -468,6 +484,11 @@ function getFolderDescription(folderName) { box-shadow: 0 1px 3px rgba(0,0,0,0.1); } +.lang-tab.disabled { + opacity: 0.4; + cursor: not-allowed; +} + .drawer-close-btn { background: none; border: none; diff --git a/vite.config.js b/vite.config.js index d0be6ca..882ecbd 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,9 +1,37 @@ import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' +import fs from 'fs' +import path from 'path' +import { fileURLToPath } from 'url' + +const __dirname = path.dirname(fileURLToPath(import.meta.url)) // https://vite.dev/config/ export default defineConfig({ - plugins: [vue()], + plugins: [ + vue(), + // 让 Vite 开发服务器正确提供 .py 文件的静态服务 + { + name: 'serve-py-files', + configureServer(server) { + server.middlewares.use((req, res, next) => { + const url = req.url + // 只处理 .py 文件请求,且排除 API 路径 + if (url.endsWith('.py') && !url.startsWith('/api/')) { + const filePath = path.join(__dirname, 'public', url.replace(/^\//, '')) + if (fs.existsSync(filePath)) { + const content = fs.readFileSync(filePath, 'utf-8') + res.setHeader('Content-Type', 'text/plain; charset=utf-8') + res.statusCode = 200 + res.end(content) + return + } + } + next() + }) + } + } + ], server: { port: 1025, proxy: {