mirror of
https://github.com/dreamstarsky/runbin.git
synced 2026-05-15 22:33:09 +00:00
fixed issue#3 & issue#4
This commit is contained in:
@@ -39,7 +39,7 @@
|
|||||||
<div v-show="time !== 0">
|
<div v-show="time !== 0">
|
||||||
运行时间:{{ time }} ms
|
运行时间:{{ time }} ms
|
||||||
</div>
|
</div>
|
||||||
{{ status !== 'completed' ? status : (stdout === '' ? 'Empty' : stdout) }}
|
<pre>{{ status !== 'completed' ? status : (stdout === '' ? 'Empty' : stdout) }}</pre>
|
||||||
<div v-show="stderr !== ''" class="text-red-500">
|
<div v-show="stderr !== ''" class="text-red-500">
|
||||||
{{ stderr }}
|
{{ stderr }}
|
||||||
</div>
|
</div>
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref, onMounted, defineProps } from 'vue'
|
import { ref, onMounted, defineProps, onUnmounted } from 'vue'
|
||||||
import { EditorView } from '@codemirror/view'
|
import { EditorView } from '@codemirror/view'
|
||||||
import { EditorState } from '@codemirror/state'
|
import { EditorState } from '@codemirror/state'
|
||||||
import { cpp } from '@codemirror/lang-cpp'
|
import { cpp } from '@codemirror/lang-cpp'
|
||||||
@@ -85,6 +85,7 @@ const stderr = ref('')
|
|||||||
const time = ref(0)
|
const time = ref(0)
|
||||||
const log = ref('')
|
const log = ref('')
|
||||||
const isLoading = ref(false)
|
const isLoading = ref(false)
|
||||||
|
const editorView = ref<EditorView | null>(null)
|
||||||
|
|
||||||
const serverUri = window.CONFIG.LSP_SERVER !== '__LSP_SERVER_URL_PLACEHOLDER__' ? window.CONFIG.LSP_SERVER : import.meta.env.VITE_LSP_SERVER;
|
const serverUri = window.CONFIG.LSP_SERVER !== '__LSP_SERVER_URL_PLACEHOLDER__' ? window.CONFIG.LSP_SERVER : import.meta.env.VITE_LSP_SERVER;
|
||||||
const backend = window.CONFIG.BACKEND !== '__BACKEND_URL_PLACEHOLDER__' ? window.CONFIG.BACKEND : import.meta.env.VITE_BACKEND;
|
const backend = window.CONFIG.BACKEND !== '__BACKEND_URL_PLACEHOLDER__' ? window.CONFIG.BACKEND : import.meta.env.VITE_BACKEND;
|
||||||
@@ -93,11 +94,9 @@ const ls = languageServer({
|
|||||||
rootUri: 'file:///main.cpp',
|
rootUri: 'file:///main.cpp',
|
||||||
workspaceFolders: [],
|
workspaceFolders: [],
|
||||||
documentUri: `file:///main.cpp`,
|
documentUri: `file:///main.cpp`,
|
||||||
languageId: 'cpp'
|
languageId: 'cpp',
|
||||||
});
|
});
|
||||||
|
|
||||||
const editorContainer = ref<HTMLElement | null>(null)
|
const editorContainer = ref<HTMLElement | null>(null)
|
||||||
let view: EditorView
|
|
||||||
|
|
||||||
function getStatus(id: string) {
|
function getStatus(id: string) {
|
||||||
fetch(backend + `/api/pastes/${props.id}`)
|
fetch(backend + `/api/pastes/${props.id}`)
|
||||||
@@ -116,7 +115,7 @@ function getStatus(id: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
|
||||||
const oldCode = localStorage.getItem('code')
|
const oldCode = localStorage.getItem('code')
|
||||||
stdin.value = localStorage.getItem('stdin') || ''
|
stdin.value = localStorage.getItem('stdin') || ''
|
||||||
const state = EditorState.create({
|
const state = EditorState.create({
|
||||||
@@ -127,7 +126,7 @@ onMounted(() => {
|
|||||||
ls,
|
ls,
|
||||||
indentUnit.of(" "),
|
indentUnit.of(" "),
|
||||||
keymap.of([
|
keymap.of([
|
||||||
{ key: "Tab", run: indentMore},
|
{ key: "Tab", run: indentMore },
|
||||||
{ key: "Shift-Tab", run: indentLess },
|
{ key: "Shift-Tab", run: indentLess },
|
||||||
// 如果需要在行中插入真实 Tab:
|
// 如果需要在行中插入真实 Tab:
|
||||||
{ key: "Mod-Tab", run: insertTab },
|
{ key: "Mod-Tab", run: insertTab },
|
||||||
@@ -135,8 +134,7 @@ onMounted(() => {
|
|||||||
oneDark,
|
oneDark,
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
editorView.value = new EditorView({
|
||||||
view = new EditorView({
|
|
||||||
state,
|
state,
|
||||||
parent: editorContainer.value as HTMLElement
|
parent: editorContainer.value as HTMLElement
|
||||||
})
|
})
|
||||||
@@ -146,10 +144,13 @@ onMounted(() => {
|
|||||||
fetch(backend + `/api/pastes/${props.id}`)
|
fetch(backend + `/api/pastes/${props.id}`)
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
view.dispatch({
|
if (!editorView.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
editorView.value.dispatch({
|
||||||
changes: {
|
changes: {
|
||||||
from: 0,
|
from: 0,
|
||||||
to: view.state.doc.length,
|
to: editorView.value.state.doc.length,
|
||||||
insert: res.code
|
insert: res.code
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -167,7 +168,7 @@ onMounted(() => {
|
|||||||
isLoading.value = false
|
isLoading.value = false
|
||||||
}
|
}
|
||||||
}, 1000)
|
}, 1000)
|
||||||
}else {
|
} else {
|
||||||
isLoading.value = false
|
isLoading.value = false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -178,11 +179,23 @@ onMounted(() => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
onUnmounted(() => {
|
||||||
|
if (editorView.value) {
|
||||||
|
console.log("Destroying CodeMirror EditorView and closing LSP connection...");
|
||||||
|
editorView.value.destroy();
|
||||||
|
editorView.value = null;
|
||||||
|
console.log("EditorView destroyed.");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const handleRun = (isRun: boolean) => {
|
const handleRun = (isRun: boolean) => {
|
||||||
if (isLoading.value) {
|
if (isLoading.value) {
|
||||||
console.log("别急")
|
console.log("别急")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (!editorView.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
console.log('run')
|
console.log('run')
|
||||||
fetch(backend + '/api/pastes', {
|
fetch(backend + '/api/pastes', {
|
||||||
@@ -191,7 +204,7 @@ const handleRun = (isRun: boolean) => {
|
|||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
code: view.state.doc.toString(),
|
code: editorView.value.state.doc.toString() ?? '',
|
||||||
stdin: isRun ? stdin.value : '',
|
stdin: isRun ? stdin.value : '',
|
||||||
language: 'c++20',
|
language: 'c++20',
|
||||||
run: isRun
|
run: isRun
|
||||||
@@ -215,8 +228,10 @@ const handleRun = (isRun: boolean) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
localStorage.setItem('code', view.state.doc.toString())
|
if (editorView.value) {
|
||||||
localStorage.setItem('stdin', stdin.value)
|
localStorage.setItem('code', editorView.value.state.doc.toString())
|
||||||
|
localStorage.setItem('stdin', stdin.value)
|
||||||
|
}
|
||||||
}, 1000)
|
}, 1000)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user