add python
This commit is contained in:
@@ -85,7 +85,12 @@
|
|||||||
>C</button>
|
>C</button>
|
||||||
<button
|
<button
|
||||||
class="lang-tab"
|
class="lang-tab"
|
||||||
:class="{ active: language === 'python' }"
|
:class="{
|
||||||
|
active: language === 'python',
|
||||||
|
disabled: !hasPythonVersion
|
||||||
|
}"
|
||||||
|
:disabled="!hasPythonVersion"
|
||||||
|
:title="hasPythonVersion ? '切换到 Python 版本' : '该文件暂无 Python 版本'"
|
||||||
@click="switchLanguage('python')"
|
@click="switchLanguage('python')"
|
||||||
>Python</button>
|
>Python</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -129,11 +134,16 @@ const chapterId = computed(() => props.id || route.params.id)
|
|||||||
const currentFile = ref(null)
|
const currentFile = ref(null)
|
||||||
const language = ref('c')
|
const language = ref('c')
|
||||||
|
|
||||||
|
// 判断当前文件是否有 Python 版本
|
||||||
|
const hasPythonVersion = computed(() => {
|
||||||
|
return currentFile.value && !!currentFile.value.pyPath
|
||||||
|
})
|
||||||
|
|
||||||
// 根据语言生成文件路径
|
// 根据语言生成文件路径
|
||||||
const currentFilePath = computed(() => {
|
const currentFilePath = computed(() => {
|
||||||
if (!currentFile.value) return null
|
if (!currentFile.value) return null
|
||||||
if (language.value === 'python') {
|
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
|
return currentFile.value.path
|
||||||
})
|
})
|
||||||
@@ -143,11 +153,17 @@ const currentFileName = computed(() => {
|
|||||||
if (language.value === 'python') {
|
if (language.value === 'python') {
|
||||||
return currentFile.value.pyPath
|
return currentFile.value.pyPath
|
||||||
? currentFile.value.pyPath.split('/').pop()
|
? currentFile.value.pyPath.split('/').pop()
|
||||||
: currentFile.value.name.replace(/\.c$/, '.py')
|
: currentFile.value.name
|
||||||
}
|
}
|
||||||
return currentFile.value.name
|
return currentFile.value.name
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 切换语言时,如果目标语言没有对应文件则保持当前语言
|
||||||
|
function switchLanguage(lang) {
|
||||||
|
if (lang === 'python' && !hasPythonVersion.value) return
|
||||||
|
language.value = lang
|
||||||
|
}
|
||||||
|
|
||||||
function switchLanguage(lang) {
|
function switchLanguage(lang) {
|
||||||
language.value = lang
|
language.value = lang
|
||||||
}
|
}
|
||||||
@@ -458,7 +474,7 @@ function getFolderDescription(folderName) {
|
|||||||
font-family: var(--mono);
|
font-family: var(--mono);
|
||||||
}
|
}
|
||||||
|
|
||||||
.lang-tab:hover {
|
.lang-tab:hover:not(:disabled) {
|
||||||
color: var(--text-primary);
|
color: var(--text-primary);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -468,6 +484,11 @@ function getFolderDescription(folderName) {
|
|||||||
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.lang-tab.disabled {
|
||||||
|
opacity: 0.4;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
.drawer-close-btn {
|
.drawer-close-btn {
|
||||||
background: none;
|
background: none;
|
||||||
border: none;
|
border: none;
|
||||||
|
|||||||
+29
-1
@@ -1,9 +1,37 @@
|
|||||||
import { defineConfig } from 'vite'
|
import { defineConfig } from 'vite'
|
||||||
import vue from '@vitejs/plugin-vue'
|
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/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
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: {
|
server: {
|
||||||
port: 1025,
|
port: 1025,
|
||||||
proxy: {
|
proxy: {
|
||||||
|
|||||||
Reference in New Issue
Block a user