From 7b556668d2896f6a4dc02d2a752d2311eaa4f866 Mon Sep 17 00:00:00 2001 From: opensnail <598092184@qq.com> Date: Fri, 20 Dec 2024 23:39:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=B7=BB=E5=8A=A0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E7=BC=96=E8=BE=91=E5=99=A8=E4=B8=BB=E9=A2=98=20?= =?UTF-8?q?=E7=BC=A9=E7=95=A5=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/api/file-viewer.ts | 8 + src/service/api/python.ts | 10 +- src/typings/api.d.ts | 5 + src/views/code/index.vue | 289 ++++++++++++++++++++------------- 4 files changed, 197 insertions(+), 115 deletions(-) diff --git a/src/service/api/file-viewer.ts b/src/service/api/file-viewer.ts index 4361251..4745764 100644 --- a/src/service/api/file-viewer.ts +++ b/src/service/api/file-viewer.ts @@ -29,3 +29,11 @@ export function saveFileContent(data: Api.File.SaveFileRequest) { data }); } + +export function fetchInitProject(data: Api.File.InitProjectRequest) { + return request({ + url: '/file/init/project', + method: 'post', + data + }); +} diff --git a/src/service/api/python.ts b/src/service/api/python.ts index 8f052d9..0f757f6 100644 --- a/src/service/api/python.ts +++ b/src/service/api/python.ts @@ -8,18 +8,16 @@ export function runPythonCommand(data: Api.Python.SaveFileRequest) { }); } -export function pythonCommand3(data: Api.Python.SaveFileRequest) { +export function pythonCommand3() { return request({ url: '/python/stop', - method: 'post', - data + method: 'post' }); } -export function fetchPythonStatus(data: Api.Python.SaveFileRequest) { +export function fetchPythonStatus() { return request({ url: '/python/status', - method: 'get', - data + method: 'get' }); } diff --git a/src/typings/api.d.ts b/src/typings/api.d.ts index 29e0658..c1308c4 100644 --- a/src/typings/api.d.ts +++ b/src/typings/api.d.ts @@ -1260,6 +1260,11 @@ declare namespace Api { filePath: string; content: string; }>; + + type InitProjectRequest = Common.CommonRecord<{ + projectName: string; + projectUrl: string; + }>; } namespace Python { diff --git a/src/views/code/index.vue b/src/views/code/index.vue index ee2d088..8fe3625 100644 --- a/src/views/code/index.vue +++ b/src/views/code/index.vue @@ -5,6 +5,7 @@ import { Folder, FolderOpenOutline } from '@vicons/ionicons5'; import { NIcon } from 'naive-ui'; import MonacoEditor from '@/components/common/monaco-editor.vue'; import { + fetchInitProject, fetchListFiles, fetchPythonStatus, fetchViewFile, @@ -14,26 +15,66 @@ import { } from '@/service/api'; import { $t } from '@/locales'; import { fetchQuickPublish } from '@/service/api/docker'; +import { useFormRules, useNaiveForm } from '@/hooks/common/form'; const code = ref(`Hello SnailJob`); -const dataRef = ref(createData()); +const dataRef = ref(); const curFile = ref(); -const pyStatus = ref(false); +const theme = ref('vs'); +const eol = ref(0); +const wordWrap = ref('on'); +const minimap = ref('true'); +const themes = [ + { + label: 'Visual Studio', + value: 'vs' + }, + { + label: 'Visual Studio Dark', + value: 'vs-dark' + }, + { + label: 'High Contrast Dark', + value: 'hc-black' + } +]; -function createData() { - return [ - { - label: 'snail-job-python', - key: 'snail-job-python', - isLeaf: false, - prefix: () => { - return h(NIcon, null, { - default: () => h(Folder) - }); - } - } - ]; -} +const eols = [ + { + label: 'LF (Linux)', + value: 0 + }, + { + label: 'CRLF (Windows)', + value: 1 + } +]; + +const wordWraps = [ + { + label: '启用', + value: 'on' + }, + { + label: '关闭', + value: 'off' + } +]; + +const minimaps = [ + { + label: '启用', + value: 'true' + }, + { + label: '关闭', + value: 'false' + } +]; + +const pyStatus = defineModel('pyStatus', { + default: false +}); const updatePrefixWithExpaned = ( _keys: Array, @@ -60,6 +101,22 @@ const updatePrefixWithExpaned = ( break; } }; + +const { formRef, validate } = useNaiveForm(); +const { defaultRequiredRule } = useFormRules(); + +type Model = Pick; +type RuleKey = Extract; +const rules: Record = { + projectName: defaultRequiredRule, + projectUrl: defaultRequiredRule +}; + +const model: Model = reactive({ + projectName: 'project', + projectUrl: 'https://github.com/open-snail/python-client/archive/refs/tags/v0.0.1.zip' +}); + const nodeProps = ({ option }: { option: TreeOption }) => { return { onClick() { @@ -71,6 +128,14 @@ const nodeProps = ({ option }: { option: TreeOption }) => { }; }; +async function handleSubmit() { + await validate(); + const { projectName, projectUrl } = model; + const { error } = await fetchInitProject({ projectName, projectUrl }); + if (error) return; + window.$message?.success($t('common.addSuccess')); +} + async function getViewFile(option: TreeOption) { if (option.isLeaf) { const res = await fetchViewFile(option.key as string); @@ -141,65 +206,18 @@ async function onLoad(node: TreeOption) { }); } -type Model = Pick; - -const model: Model = reactive(createDefaultModel()); - -function createDefaultModel(): Model { - return { - fileName: curFile.value?.key as string, - filePath: curFile.value?.label as string, - content: code.value - }; -} - -async function saveFile() { - model.fileName = curFile.value?.key as string; - model.filePath = curFile.value?.label as string; - model.content = code.value; - - const { fileName, filePath, content } = model; - const { error } = await saveFileContent({ fileName, filePath, content }); - if (error) { - window.$message?.success($t('common.updateFailed')); - } else { - window.$message?.success($t('common.updateSuccess')); - } -} - -type PythonModel = Pick; - -const pythonModel: PythonModel = reactive(createPythonModelModel()); - -function createPythonModelModel(): PythonModel { - return { - pythonPath: '', - command: 'pip3 install -r && python3 ' /// ToDo 这里需要做配置 - }; -} - function codeUpdate(content: string) { code.value = content; } -function saveFileClick() { - saveFile(); -} - -async function runPython() { - pythonModel.pythonPath = curFile.value?.key as string; - - const { pythonPath, command } = pythonModel; - const { error } = await runPythonCommand({ pythonPath, command }); - if (error) { - window.$message?.success($t('common.updateFailed')); - } else { - window.$message?.success($t('common.updateSuccess')); - } -} - -async function stopPython() { - const { error } = await pythonCommand3(pythonModel); +async function saveFileClick() { + const { error } = await saveFileContent( + reactive({ + fileName: curFile.value?.key as string, + filePath: curFile.value?.label as string, + content: code.value + }) + ); if (error) { window.$message?.success($t('common.updateFailed')); } else { @@ -208,7 +226,7 @@ async function stopPython() { } async function pythonStatus() { - const { error, data } = await fetchPythonStatus(pythonModel); + const { error, data } = await fetchPythonStatus(); if (error) { window.$message?.success($t('common.updateFailed')); } else { @@ -216,20 +234,40 @@ async function pythonStatus() { } } -type ContainerRequestModel = Pick; - -const containerRequestModel: ContainerRequestModel = reactive(createContainerRequestModelModel()); - -function createContainerRequestModelModel(): ContainerRequestModel { - return { - groupName: 'snail_job_demo_group', - namespaceId: '764d604ec6fc45f68cd92514c40e9e1a', /// ToDo 这里需要做配置 - imageName: '' - }; +async function runPythonClick() { + const { error } = await runPythonCommand( + reactive({ + pythonPath: curFile.value?.key as string, + command: 'pip3 install -r && python3 ' /// ToDo 这里需要做配置 + }) + ); + if (error) { + window.$message?.success($t('common.updateFailed')); + } else { + window.$message?.success($t('common.updateSuccess')); + pyStatus.value = true; + } + // pythonStatus(); } -async function quickPublish() { - const { error } = await fetchQuickPublish(containerRequestModel); +async function stopPythonClick() { + const { error } = await pythonCommand3(); + if (error) { + window.$message?.success($t('common.updateFailed')); + } else { + window.$message?.success($t('common.updateSuccess')); + } + pythonStatus(); +} + +async function quickPublishClick() { + const { error } = await fetchQuickPublish( + reactive({ + groupName: 'snail_job_demo_group', + namespaceId: '764d604ec6fc45f68cd92514c40e9e1a', /// ToDo 这里需要做配置 + imageName: '' + }) + ); if (error) { window.$message?.success($t('common.updateFailed')); } else { @@ -237,41 +275,57 @@ async function quickPublish() { } } -function runPythonClick() { - runPython(); - pythonStatus(); -} - -function stopPythonClick() { - stopPython(); - pythonStatus(); -} - -function quickPublishClick() { - quickPublish(); -} - -onMounted(() => { - pythonStatus(); +onMounted(async () => { + const res = await fetchListFiles(''); + const files = res.data as Api.File.FileInfo[]; + dataRef.value = [ + { + label: files[0].fileName, + key: files[0].fileName, + isLeaf: false, + prefix: () => { + return h(NIcon, null, { + default: () => h(Folder) + }); + } + } + ]; + await pythonStatus(); });