fix
This commit is contained in:
57
src/oj/problem/components/ContestEditor.vue
Normal file
57
src/oj/problem/components/ContestEditor.vue
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import { code } from "oj/composables/code"
|
||||||
|
import { problem } from "oj/composables/problem"
|
||||||
|
import { SOURCES } from "utils/constants"
|
||||||
|
import CodeEditor from "~/shared/components/CodeEditor.vue"
|
||||||
|
import { isDesktop } from "~/shared/composables/breakpoints"
|
||||||
|
import storage from "~/utils/storage"
|
||||||
|
import { LANGUAGE } from "~/utils/types"
|
||||||
|
import Form from "./Form.vue"
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
|
||||||
|
const contestID = route.params.contestID || null
|
||||||
|
const storageKey = computed(
|
||||||
|
() =>
|
||||||
|
`problem_${problem.value!._id}_contest_${contestID}_lang_${code.language}`,
|
||||||
|
)
|
||||||
|
|
||||||
|
const editorHeight = computed(() =>
|
||||||
|
isDesktop.value ? "calc(100vh - 133px)" : "calc(100vh - 172px)",
|
||||||
|
)
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
const savedCode = storage.get(storageKey.value)
|
||||||
|
code.value =
|
||||||
|
savedCode ||
|
||||||
|
problem.value!.template[code.language] ||
|
||||||
|
SOURCES[code.language]
|
||||||
|
})
|
||||||
|
|
||||||
|
const changeCode = (v: string) => {
|
||||||
|
storage.set(storageKey.value, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
const changeLanguage = (v: LANGUAGE) => {
|
||||||
|
const savedCode = storage.get(storageKey.value)
|
||||||
|
code.value =
|
||||||
|
savedCode && storageKey.value.split("_").pop() === v
|
||||||
|
? savedCode
|
||||||
|
: problem.value!.template[code.language] || SOURCES[code.language]
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<n-flex vertical>
|
||||||
|
<Form
|
||||||
|
:storage-key="storageKey"
|
||||||
|
@change-language="changeLanguage"
|
||||||
|
/>
|
||||||
|
<CodeEditor
|
||||||
|
v-model:value="code.value"
|
||||||
|
:language="code.language"
|
||||||
|
:height="editorHeight"
|
||||||
|
@update:model-value="changeCode"
|
||||||
|
/>
|
||||||
|
</n-flex>
|
||||||
|
</template>
|
||||||
@@ -16,13 +16,13 @@ interface Props {
|
|||||||
storageKey: string
|
storageKey: string
|
||||||
withTest?: boolean
|
withTest?: boolean
|
||||||
otherUserInfo?: { name: string; isSuperAdmin: boolean }
|
otherUserInfo?: { name: string; isSuperAdmin: boolean }
|
||||||
isSynced?: boolean
|
isConnected?: boolean // WebSocket 实际的连接状态(已建立/未建立)
|
||||||
hadConnection?: boolean
|
hadConnection?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
withTest: false,
|
withTest: false,
|
||||||
isSynced: false,
|
isConnected: false,
|
||||||
hadConnection: false,
|
hadConnection: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -36,34 +36,40 @@ const route = useRoute()
|
|||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
|
|
||||||
const isSynced = ref(false)
|
const syncEnabled = ref(false) // 用户点击按钮后的意图状态(想要开启/关闭)
|
||||||
const statisticPanel = ref(false)
|
const statisticPanel = ref(false)
|
||||||
|
|
||||||
|
// 计算属性
|
||||||
|
const isContestMode = computed(() => route.name === "contest problem")
|
||||||
|
const buttonSize = computed(() => (isDesktop.value ? "medium" : "small"))
|
||||||
|
const showSyncFeature = computed(
|
||||||
|
() => isDesktop.value && userStore.isAuthed && !isContestMode.value,
|
||||||
|
)
|
||||||
|
|
||||||
const menu = computed<DropdownOption[]>(() => [
|
const menu = computed<DropdownOption[]>(() => [
|
||||||
{ label: "去自测猫", key: "test", show: isMobile.value },
|
{ label: "去自测猫", key: "goTestCat", show: isMobile.value },
|
||||||
{ label: "复制代码", key: "copy" },
|
{ label: "复制代码", key: "copy" },
|
||||||
{ label: "重置代码", key: "reset" },
|
{ label: "重置代码", key: "reset" },
|
||||||
])
|
])
|
||||||
|
|
||||||
const languageOptions: DropdownOption[] = problem.value!.languages.map((it) => ({
|
const languageOptions: DropdownOption[] = problem.value!.languages.map(
|
||||||
label: () =>
|
(it) => ({
|
||||||
h("div", { style: "display: flex; align-items: center;" }, [
|
label: () =>
|
||||||
h("img", {
|
h("div", { style: "display: flex; align-items: center;" }, [
|
||||||
src: `/${it}.svg`,
|
h("img", {
|
||||||
style: { width: "16px", height: "16px", marginRight: "8px" },
|
src: `/${it}.svg`,
|
||||||
}),
|
style: { width: "16px", height: "16px", marginRight: "8px" },
|
||||||
LANGUAGE_SHOW_VALUE[it],
|
}),
|
||||||
]),
|
LANGUAGE_SHOW_VALUE[it],
|
||||||
value: it,
|
]),
|
||||||
}))
|
value: it,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
// 代码操作相关
|
||||||
const copy = async () => {
|
const copy = async () => {
|
||||||
const success = await copyToClipboard(code.value)
|
const success = await copyToClipboard(code.value)
|
||||||
if (success) {
|
message[success ? "success" : "error"](`代码复制${success ? "成功" : "失败"}`)
|
||||||
message.success("代码复制成功")
|
|
||||||
} else {
|
|
||||||
message.error("代码复制失败")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const reset = () => {
|
const reset = () => {
|
||||||
@@ -72,67 +78,70 @@ const reset = () => {
|
|||||||
message.success("代码重置成功")
|
message.success("代码重置成功")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const changeLanguage = (v: LANGUAGE) => {
|
||||||
|
storage.set(STORAGE_KEY.LANGUAGE, v)
|
||||||
|
emit("changeLanguage", v)
|
||||||
|
}
|
||||||
|
|
||||||
|
const runCode = async () => {
|
||||||
|
const res = await createTestSubmission(code, input.value)
|
||||||
|
output.value = res.output
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导航相关
|
||||||
|
const goTestCat = () => {
|
||||||
|
window.open(import.meta.env.PUBLIC_CODE_URL, "_blank")
|
||||||
|
}
|
||||||
|
|
||||||
const goSubmissions = () => {
|
const goSubmissions = () => {
|
||||||
const name = route.params.contestID ? "contest submissions" : "submissions"
|
const name = route.params.contestID ? "contest submissions" : "submissions"
|
||||||
router.push({ name, query: { problem: problem.value!._id } })
|
router.push({ name, query: { problem: problem.value!._id } })
|
||||||
}
|
}
|
||||||
|
|
||||||
const goEdit = () => {
|
const goEdit = () => {
|
||||||
const baseUrl = "/admin/problem/edit/" + problem.value!.id
|
|
||||||
const url = problem.value!.contest
|
const url = problem.value!.contest
|
||||||
? `/admin/contest/${problem.value!.contest}/problem/edit/${problem.value!.id}`
|
? `/admin/contest/${problem.value!.contest}/problem/edit/${problem.value!.id}`
|
||||||
: baseUrl
|
: `/admin/problem/edit/${problem.value!.id}`
|
||||||
window.open(router.resolve(url).href, "_blank")
|
window.open(router.resolve(url).href, "_blank")
|
||||||
}
|
}
|
||||||
|
|
||||||
const test = async () => {
|
// 菜单处理
|
||||||
const res = await createTestSubmission(code, input.value)
|
|
||||||
output.value = res.output
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleMenuSelect = (key: string) => {
|
const handleMenuSelect = (key: string) => {
|
||||||
const actions: Record<string, () => void> = {
|
const actions: Record<string, () => void> = {
|
||||||
reset,
|
reset,
|
||||||
copy,
|
copy,
|
||||||
test: () => window.open(import.meta.env.PUBLIC_CODE_URL, "_blank"),
|
goTestCat,
|
||||||
}
|
}
|
||||||
actions[key]?.()
|
actions[key]?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
const changeLanguage = (v: LANGUAGE) => {
|
// 协同编辑相关
|
||||||
storage.set(STORAGE_KEY.LANGUAGE, v)
|
|
||||||
emit("changeLanguage", v)
|
|
||||||
}
|
|
||||||
|
|
||||||
const toggleSync = () => {
|
const toggleSync = () => {
|
||||||
isSynced.value = !isSynced.value
|
syncEnabled.value = !syncEnabled.value
|
||||||
emit("toggleSync", isSynced.value)
|
emit("toggleSync", syncEnabled.value)
|
||||||
}
|
|
||||||
|
|
||||||
const gotoTestCat = () => {
|
|
||||||
window.open(import.meta.env.PUBLIC_CODE_URL, "_blank")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
resetSyncStatus: () => {
|
resetSyncStatus: () => {
|
||||||
isSynced.value = false
|
syncEnabled.value = false
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<n-flex align="center">
|
<n-flex align="center">
|
||||||
|
<!-- 语言选择器 -->
|
||||||
<n-select
|
<n-select
|
||||||
v-model:value="code.language"
|
v-model:value="code.language"
|
||||||
class="language"
|
class="language"
|
||||||
:size="isDesktop ? 'medium' : 'small'"
|
:size="buttonSize"
|
||||||
:options="languageOptions"
|
:options="languageOptions"
|
||||||
@update:value="changeLanguage"
|
@update:value="changeLanguage"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<template v-if="withTest">
|
<template v-if="withTest">
|
||||||
<n-button @click="reset">重置代码</n-button>
|
<n-button @click="reset">重置代码</n-button>
|
||||||
<n-button type="primary" secondary @click="test">运行代码</n-button>
|
<n-button type="primary" secondary @click="runCode">运行代码</n-button>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<n-flex v-else align="center">
|
<n-flex v-else align="center">
|
||||||
@@ -140,7 +149,7 @@ defineExpose({
|
|||||||
|
|
||||||
<n-button
|
<n-button
|
||||||
v-if="!userStore.isSuperAdmin && userStore.showSubmissions"
|
v-if="!userStore.isSuperAdmin && userStore.showSubmissions"
|
||||||
:size="isDesktop ? 'medium' : 'small'"
|
:size="buttonSize"
|
||||||
@click="goSubmissions"
|
@click="goSubmissions"
|
||||||
>
|
>
|
||||||
提交信息
|
提交信息
|
||||||
@@ -148,16 +157,16 @@ defineExpose({
|
|||||||
|
|
||||||
<n-button
|
<n-button
|
||||||
v-if="userStore.isSuperAdmin"
|
v-if="userStore.isSuperAdmin"
|
||||||
:size="isDesktop ? 'medium' : 'small'"
|
:size="buttonSize"
|
||||||
@click="statisticPanel = true"
|
@click="statisticPanel = true"
|
||||||
>
|
>
|
||||||
{{ isDesktop ? "统计信息" : "统计" }}
|
{{ isDesktop ? "统计信息" : "统计" }}
|
||||||
</n-button>
|
</n-button>
|
||||||
|
|
||||||
<n-button v-if="isDesktop" @click="gotoTestCat">自测猫</n-button>
|
<n-button v-if="isDesktop" @click="goTestCat">自测猫</n-button>
|
||||||
|
|
||||||
<n-dropdown size="large" :options="menu" @select="handleMenuSelect">
|
<n-dropdown size="large" :options="menu" @select="handleMenuSelect">
|
||||||
<n-button :size="isDesktop ? 'medium' : 'small'">操作</n-button>
|
<n-button :size="buttonSize">操作</n-button>
|
||||||
</n-dropdown>
|
</n-dropdown>
|
||||||
|
|
||||||
<IconButton
|
<IconButton
|
||||||
@@ -167,28 +176,31 @@ defineExpose({
|
|||||||
@click="goEdit"
|
@click="goEdit"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<IconButton
|
<!-- 协同编辑功能(仅在非比赛模式) -->
|
||||||
v-if="isDesktop && userStore.isAuthed"
|
<template v-if="showSyncFeature">
|
||||||
:icon="
|
<IconButton
|
||||||
isSynced
|
:icon="
|
||||||
? 'streamline-stickies-color:earpod-connected'
|
syncEnabled
|
||||||
: 'streamline-stickies-color:earpod-connected-duo'
|
? 'streamline-stickies-color:earpod-connected'
|
||||||
"
|
: 'streamline-stickies-color:earpod-connected-duo'
|
||||||
:tip="isSynced ? '断开同步' : '打开同步'"
|
"
|
||||||
:type="isSynced ? 'info' : 'default'"
|
:tip="syncEnabled ? '断开同步' : '打开同步'"
|
||||||
@click="toggleSync"
|
:type="syncEnabled ? 'info' : 'default'"
|
||||||
/>
|
@click="toggleSync"
|
||||||
|
/>
|
||||||
|
|
||||||
<template v-if="isDesktop && props.isSynced">
|
<!-- 同步状态标签 -->
|
||||||
<n-tag v-if="otherUserInfo" type="info">
|
<template v-if="props.isConnected">
|
||||||
与 {{ otherUserInfo.name }} 同步中
|
<n-tag v-if="otherUserInfo" type="info">
|
||||||
</n-tag>
|
与 {{ otherUserInfo.name }} 同步中
|
||||||
<n-tag
|
</n-tag>
|
||||||
v-if="userStore.isSuperAdmin && !otherUserInfo && hadConnection"
|
<n-tag
|
||||||
type="warning"
|
v-if="userStore.isSuperAdmin && !otherUserInfo && hadConnection"
|
||||||
>
|
type="warning"
|
||||||
学生已退出,可以关闭同步
|
>
|
||||||
</n-tag>
|
学生已退出,可以关闭同步
|
||||||
|
</n-tag>
|
||||||
|
</template>
|
||||||
</template>
|
</template>
|
||||||
</n-flex>
|
</n-flex>
|
||||||
</n-flex>
|
</n-flex>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
import { code } from "oj/composables/code"
|
import { code } from "oj/composables/code"
|
||||||
import { problem } from "oj/composables/problem"
|
import { problem } from "oj/composables/problem"
|
||||||
import { SOURCES } from "utils/constants"
|
import { SOURCES } from "utils/constants"
|
||||||
import CodeEditor from "~/shared/components/CodeEditor.vue"
|
import SyncCodeEditor from "~/shared/components/SyncCodeEditor.vue"
|
||||||
import { isDesktop } from "~/shared/composables/breakpoints"
|
import { isDesktop } from "~/shared/composables/breakpoints"
|
||||||
import storage from "~/utils/storage"
|
import storage from "~/utils/storage"
|
||||||
import { LANGUAGE } from "~/utils/types"
|
import { LANGUAGE } from "~/utils/types"
|
||||||
@@ -75,12 +75,12 @@ const handleSyncStatusChange = (status: {
|
|||||||
ref="formRef"
|
ref="formRef"
|
||||||
:storage-key="storageKey"
|
:storage-key="storageKey"
|
||||||
:other-user-info="otherUserInfo"
|
:other-user-info="otherUserInfo"
|
||||||
:is-synced="sync"
|
:is-connected="sync"
|
||||||
:had-connection="hadConnection"
|
:had-connection="hadConnection"
|
||||||
@change-language="changeLanguage"
|
@change-language="changeLanguage"
|
||||||
@toggle-sync="toggleSync"
|
@toggle-sync="toggleSync"
|
||||||
/>
|
/>
|
||||||
<CodeEditor
|
<SyncCodeEditor
|
||||||
v-model:value="code.value"
|
v-model:value="code.value"
|
||||||
:sync="sync"
|
:sync="sync"
|
||||||
:problem="problem!._id"
|
:problem="problem!._id"
|
||||||
@@ -9,7 +9,8 @@ import {
|
|||||||
} from "~/shared/composables/switchScreen"
|
} from "~/shared/composables/switchScreen"
|
||||||
import { problem } from "../composables/problem"
|
import { problem } from "../composables/problem"
|
||||||
|
|
||||||
const Editor = defineAsyncComponent(() => import("./components/Editor.vue"))
|
const ProblemEditor = defineAsyncComponent(() => import("./components/ProblemEditor.vue"))
|
||||||
|
const ContestEditor = defineAsyncComponent(() => import("./components/ContestEditor.vue"))
|
||||||
const EditorWithTest = defineAsyncComponent(
|
const EditorWithTest = defineAsyncComponent(
|
||||||
() => import("./components/EditorWithTest.vue"),
|
() => import("./components/EditorWithTest.vue"),
|
||||||
)
|
)
|
||||||
@@ -54,6 +55,8 @@ const tabOptions = computed(() => {
|
|||||||
|
|
||||||
const currentTab = ref("content")
|
const currentTab = ref("content")
|
||||||
|
|
||||||
|
const shouldUseProblemEditor = computed(() => route.name === "problem")
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
[() => route.query.tab, () => tabOptions.value],
|
[() => route.query.tab, () => tabOptions.value],
|
||||||
([rawTab]) => {
|
([rawTab]) => {
|
||||||
@@ -125,7 +128,8 @@ watch(isMobile, (value) => {
|
|||||||
<ProblemContent />
|
<ProblemContent />
|
||||||
</n-tab-pane>
|
</n-tab-pane>
|
||||||
<n-tab-pane name="editor" tab="编辑">
|
<n-tab-pane name="editor" tab="编辑">
|
||||||
<Editor />
|
<ProblemEditor v-if="shouldUseProblemEditor" />
|
||||||
|
<ContestEditor v-else />
|
||||||
</n-tab-pane>
|
</n-tab-pane>
|
||||||
<n-tab-pane name="info" tab="统计">
|
<n-tab-pane name="info" tab="统计">
|
||||||
<ProblemInfo />
|
<ProblemInfo />
|
||||||
@@ -139,7 +143,8 @@ watch(isMobile, (value) => {
|
|||||||
</n-tabs>
|
</n-tabs>
|
||||||
</n-gi>
|
</n-gi>
|
||||||
<n-gi v-if="isDesktop && screenMode === ScreenMode.both">
|
<n-gi v-if="isDesktop && screenMode === ScreenMode.both">
|
||||||
<Editor />
|
<ProblemEditor v-if="shouldUseProblemEditor" />
|
||||||
|
<ContestEditor v-else />
|
||||||
</n-gi>
|
</n-gi>
|
||||||
<n-gi v-if="isDesktop && screenMode === ScreenMode.code">
|
<n-gi v-if="isDesktop && screenMode === ScreenMode.code">
|
||||||
<EditorWithTest />
|
<EditorWithTest />
|
||||||
|
|||||||
@@ -7,18 +7,8 @@ import type { Extension } from "@codemirror/state"
|
|||||||
import { LANGUAGE } from "~/utils/types"
|
import { LANGUAGE } from "~/utils/types"
|
||||||
import { oneDark } from "../themes/oneDark"
|
import { oneDark } from "../themes/oneDark"
|
||||||
import { smoothy } from "../themes/smoothy"
|
import { smoothy } from "../themes/smoothy"
|
||||||
import { useCodeSync } from "../composables/sync"
|
|
||||||
import { isDesktop } from "../composables/breakpoints"
|
|
||||||
|
|
||||||
interface EditorReadyPayload {
|
|
||||||
view: EditorView
|
|
||||||
state: any
|
|
||||||
container: HTMLElement
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
sync?: boolean
|
|
||||||
problem?: string
|
|
||||||
language?: LANGUAGE
|
language?: LANGUAGE
|
||||||
fontSize?: number
|
fontSize?: number
|
||||||
height?: string
|
height?: string
|
||||||
@@ -27,8 +17,6 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const props = withDefaults(defineProps<Props>(), {
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
sync: false,
|
|
||||||
problem: "",
|
|
||||||
language: "Python3",
|
language: "Python3",
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
height: "100%",
|
height: "100%",
|
||||||
@@ -39,13 +27,6 @@ const props = withDefaults(defineProps<Props>(), {
|
|||||||
const { readonly, placeholder, height, fontSize } = toRefs(props)
|
const { readonly, placeholder, height, fontSize } = toRefs(props)
|
||||||
const code = defineModel<string>("value")
|
const code = defineModel<string>("value")
|
||||||
|
|
||||||
const emit = defineEmits<{
|
|
||||||
syncClosed: []
|
|
||||||
syncStatusChange: [
|
|
||||||
status: { otherUser?: { name: string; isSuperAdmin: boolean } },
|
|
||||||
]
|
|
||||||
}>()
|
|
||||||
|
|
||||||
const isDark = useDark()
|
const isDark = useDark()
|
||||||
|
|
||||||
const styleTheme = EditorView.baseTheme({
|
const styleTheme = EditorView.baseTheme({
|
||||||
@@ -64,66 +45,7 @@ const extensions = computed(() => [
|
|||||||
styleTheme,
|
styleTheme,
|
||||||
lang.value,
|
lang.value,
|
||||||
isDark.value ? oneDark : smoothy,
|
isDark.value ? oneDark : smoothy,
|
||||||
getInitialExtension(),
|
|
||||||
])
|
])
|
||||||
|
|
||||||
const { startSync, stopSync, getInitialExtension } = useCodeSync()
|
|
||||||
const editorView = ref<EditorView | null>(null)
|
|
||||||
let cleanupSync: (() => void) | null = null
|
|
||||||
|
|
||||||
const cleanupSyncResources = () => {
|
|
||||||
if (cleanupSync) {
|
|
||||||
cleanupSync()
|
|
||||||
cleanupSync = null
|
|
||||||
}
|
|
||||||
stopSync()
|
|
||||||
}
|
|
||||||
|
|
||||||
const initSync = async () => {
|
|
||||||
if (!editorView.value || !props.problem || !isDesktop.value) return
|
|
||||||
|
|
||||||
cleanupSyncResources()
|
|
||||||
|
|
||||||
cleanupSync = await startSync({
|
|
||||||
problemId: props.problem,
|
|
||||||
editorView: editorView.value as EditorView,
|
|
||||||
onStatusChange: (status) => {
|
|
||||||
if (status.error === "超管已离开" && !status.connected) {
|
|
||||||
emit("syncClosed")
|
|
||||||
}
|
|
||||||
emit("syncStatusChange", { otherUser: status.otherUser })
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleEditorReady = (payload: EditorReadyPayload) => {
|
|
||||||
editorView.value = payload.view as EditorView
|
|
||||||
if (props.sync) {
|
|
||||||
initSync()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => props.sync,
|
|
||||||
(shouldSync) => {
|
|
||||||
if (shouldSync) {
|
|
||||||
initSync()
|
|
||||||
} else {
|
|
||||||
cleanupSyncResources()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => props.problem,
|
|
||||||
(newProblem, oldProblem) => {
|
|
||||||
if (newProblem !== oldProblem && props.sync) {
|
|
||||||
initSync()
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
onUnmounted(cleanupSyncResources)
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -135,6 +57,5 @@ onUnmounted(cleanupSyncResources)
|
|||||||
:tab-size="4"
|
:tab-size="4"
|
||||||
:placeholder="placeholder"
|
:placeholder="placeholder"
|
||||||
:style="{ height, fontSize: `${fontSize}px` }"
|
:style="{ height, fontSize: `${fontSize}px` }"
|
||||||
@ready="handleEditorReady"
|
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
138
src/shared/components/SyncCodeEditor.vue
Normal file
138
src/shared/components/SyncCodeEditor.vue
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import { cpp } from "@codemirror/lang-cpp"
|
||||||
|
import { python } from "@codemirror/lang-python"
|
||||||
|
import { EditorView } from "@codemirror/view"
|
||||||
|
import { Codemirror } from "vue-codemirror"
|
||||||
|
import type { Extension } from "@codemirror/state"
|
||||||
|
import { LANGUAGE } from "~/utils/types"
|
||||||
|
import { oneDark } from "../themes/oneDark"
|
||||||
|
import { smoothy } from "../themes/smoothy"
|
||||||
|
import { useCodeSync } from "../composables/sync"
|
||||||
|
import { isDesktop } from "../composables/breakpoints"
|
||||||
|
|
||||||
|
interface EditorReadyPayload {
|
||||||
|
view: EditorView
|
||||||
|
state: any
|
||||||
|
container: HTMLElement
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
sync: boolean
|
||||||
|
problem: string
|
||||||
|
language?: LANGUAGE
|
||||||
|
fontSize?: number
|
||||||
|
height?: string
|
||||||
|
readonly?: boolean
|
||||||
|
placeholder?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = withDefaults(defineProps<Props>(), {
|
||||||
|
language: "Python3",
|
||||||
|
fontSize: 20,
|
||||||
|
height: "100%",
|
||||||
|
readonly: false,
|
||||||
|
placeholder: "",
|
||||||
|
})
|
||||||
|
|
||||||
|
const { readonly, placeholder, height, fontSize } = toRefs(props)
|
||||||
|
const code = defineModel<string>("value")
|
||||||
|
|
||||||
|
const emit = defineEmits<{
|
||||||
|
syncClosed: []
|
||||||
|
syncStatusChange: [
|
||||||
|
status: { otherUser?: { name: string; isSuperAdmin: boolean } },
|
||||||
|
]
|
||||||
|
}>()
|
||||||
|
|
||||||
|
const isDark = useDark()
|
||||||
|
|
||||||
|
const styleTheme = EditorView.baseTheme({
|
||||||
|
"& .cm-scroller": { "font-family": "Monaco" },
|
||||||
|
"&.cm-editor.cm-focused": { outline: "none" },
|
||||||
|
"&.cm-editor .cm-tooltip.cm-tooltip-autocomplete ul": {
|
||||||
|
"font-family": "Monaco",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const lang = computed((): Extension => {
|
||||||
|
return ["Python2", "Python3"].includes(props.language) ? python() : cpp()
|
||||||
|
})
|
||||||
|
|
||||||
|
const extensions = computed(() => [
|
||||||
|
styleTheme,
|
||||||
|
lang.value,
|
||||||
|
isDark.value ? oneDark : smoothy,
|
||||||
|
getInitialExtension(),
|
||||||
|
])
|
||||||
|
|
||||||
|
const { startSync, stopSync, getInitialExtension } = useCodeSync()
|
||||||
|
const editorView = ref<EditorView | null>(null)
|
||||||
|
let cleanupSync: (() => void) | null = null
|
||||||
|
|
||||||
|
const cleanupSyncResources = () => {
|
||||||
|
if (cleanupSync) {
|
||||||
|
cleanupSync()
|
||||||
|
cleanupSync = null
|
||||||
|
}
|
||||||
|
stopSync()
|
||||||
|
}
|
||||||
|
|
||||||
|
const initSync = async () => {
|
||||||
|
if (!editorView.value || !props.problem || !isDesktop.value) return
|
||||||
|
|
||||||
|
cleanupSyncResources()
|
||||||
|
|
||||||
|
cleanupSync = await startSync({
|
||||||
|
problemId: props.problem,
|
||||||
|
editorView: editorView.value as EditorView,
|
||||||
|
onStatusChange: (status) => {
|
||||||
|
if (status.error === "超管已离开" && !status.connected) {
|
||||||
|
emit("syncClosed")
|
||||||
|
}
|
||||||
|
emit("syncStatusChange", { otherUser: status.otherUser })
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleEditorReady = (payload: EditorReadyPayload) => {
|
||||||
|
editorView.value = payload.view as EditorView
|
||||||
|
if (props.sync) {
|
||||||
|
initSync()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.sync,
|
||||||
|
(shouldSync) => {
|
||||||
|
if (shouldSync) {
|
||||||
|
initSync()
|
||||||
|
} else {
|
||||||
|
cleanupSyncResources()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.problem,
|
||||||
|
(newProblem, oldProblem) => {
|
||||||
|
if (newProblem !== oldProblem && props.sync) {
|
||||||
|
initSync()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
onUnmounted(cleanupSyncResources)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<Codemirror
|
||||||
|
v-model="code"
|
||||||
|
indentWithTab
|
||||||
|
:extensions="extensions"
|
||||||
|
:disabled="readonly"
|
||||||
|
:tab-size="4"
|
||||||
|
:placeholder="placeholder"
|
||||||
|
:style="{ height, fontSize: `${fontSize}px` }"
|
||||||
|
@ready="handleEditorReady"
|
||||||
|
/>
|
||||||
|
</template>
|
||||||
@@ -121,7 +121,7 @@ export function useCodeSync() {
|
|||||||
},
|
},
|
||||||
onStatusChange,
|
onStatusChange,
|
||||||
)
|
)
|
||||||
message.warning(`超管 ${superAdminInfo.name} 已退出`)
|
message.warning(`🎈 超管 ${superAdminInfo.name} 溜了,协同编辑已断开连接`)
|
||||||
stopSync()
|
stopSync()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -150,7 +150,7 @@ export function useCodeSync() {
|
|||||||
onStatusChange,
|
onStatusChange,
|
||||||
)
|
)
|
||||||
if (lastSyncState !== "error") {
|
if (lastSyncState !== "error") {
|
||||||
message.warning("协同编辑需要至少一个超级管理员")
|
message.warning("🎓 协同编辑需要一位超级管理员坐镇哦")
|
||||||
lastSyncState = "error"
|
lastSyncState = "error"
|
||||||
}
|
}
|
||||||
} else if (canSync) {
|
} else if (canSync) {
|
||||||
@@ -159,13 +159,13 @@ export function useCodeSync() {
|
|||||||
connected: true,
|
connected: true,
|
||||||
roomUsers,
|
roomUsers,
|
||||||
canSync: true,
|
canSync: true,
|
||||||
message: "协同编辑已激活,可以开始协作!",
|
message: "🎉 协同编辑已激活,开始愉快的代码之旅吧!",
|
||||||
otherUser,
|
otherUser,
|
||||||
},
|
},
|
||||||
onStatusChange,
|
onStatusChange,
|
||||||
)
|
)
|
||||||
if (lastSyncState !== "active") {
|
if (lastSyncState !== "active") {
|
||||||
message.success("协同编辑已激活,可以开始协作!")
|
message.success("🎉 协同编辑已激活,开始愉快的代码之旅吧!")
|
||||||
lastSyncState = "active"
|
lastSyncState = "active"
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -174,7 +174,7 @@ export function useCodeSync() {
|
|||||||
connected: true,
|
connected: true,
|
||||||
roomUsers,
|
roomUsers,
|
||||||
canSync: false,
|
canSync: false,
|
||||||
message: roomUsers === 1 ? "正在等待加入" : "等待超级管理员加入...",
|
message: roomUsers === 1 ? "👋 正在等待小伙伴加入..." : "🎓 等待超级管理员加入...",
|
||||||
otherUser,
|
otherUser,
|
||||||
},
|
},
|
||||||
onStatusChange,
|
onStatusChange,
|
||||||
@@ -213,17 +213,6 @@ export function useCodeSync() {
|
|||||||
const { problemId, editorView, onStatusChange } = options
|
const { problemId, editorView, onStatusChange } = options
|
||||||
|
|
||||||
if (!userStore.isAuthed) {
|
if (!userStore.isAuthed) {
|
||||||
updateStatus(
|
|
||||||
{
|
|
||||||
connected: false,
|
|
||||||
roomUsers: 0,
|
|
||||||
canSync: false,
|
|
||||||
message: "请先登录后再使用同步功能",
|
|
||||||
error: "用户未登录",
|
|
||||||
},
|
|
||||||
onStatusChange,
|
|
||||||
)
|
|
||||||
message.error("请先登录后再使用同步功能")
|
|
||||||
return () => {}
|
return () => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +249,7 @@ export function useCodeSync() {
|
|||||||
},
|
},
|
||||||
onStatusChange,
|
onStatusChange,
|
||||||
)
|
)
|
||||||
message.warning("协同编辑连接已断开")
|
message.warning("📡 协同编辑连接断开了,可能网络不太稳定呢")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -280,7 +269,7 @@ export function useCodeSync() {
|
|||||||
onStatusChange,
|
onStatusChange,
|
||||||
)
|
)
|
||||||
message.warning(
|
message.warning(
|
||||||
`房间人数已满(最多${SYNC_CONSTANTS.MAX_ROOM_USERS}人),已自动断开连接`,
|
`🚪 哎呀,房间已经坐满了(最多${SYNC_CONSTANTS.MAX_ROOM_USERS}人),已自动断开连接`,
|
||||||
)
|
)
|
||||||
stopSync()
|
stopSync()
|
||||||
return
|
return
|
||||||
@@ -349,13 +338,13 @@ export function useCodeSync() {
|
|||||||
connected: true,
|
connected: true,
|
||||||
roomUsers: 1,
|
roomUsers: 1,
|
||||||
canSync: false,
|
canSync: false,
|
||||||
message: "正在等待加入",
|
message: "🚀 协同编辑已准备就绪,等待伙伴加入...",
|
||||||
},
|
},
|
||||||
onStatusChange,
|
onStatusChange,
|
||||||
)
|
)
|
||||||
|
|
||||||
message.info(
|
message.info(
|
||||||
userStore.isSuperAdmin ? "正在等待学生加入..." : "正在等待超管加入...",
|
userStore.isSuperAdmin ? "👀 正在等待学生加入房间..." : "🎯 正在等待超管加入房间...",
|
||||||
)
|
)
|
||||||
lastSyncState = "waiting"
|
lastSyncState = "waiting"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user