@@ -1,44 +1,28 @@
|
||||
import { useConfigStore } from "shared/store/config"
|
||||
import { useConfigWebSocket, type ConfigUpdate } from "shared/composables/websocket"
|
||||
import {
|
||||
useConfigWebSocket,
|
||||
type ConfigUpdate,
|
||||
} from "shared/composables/websocket"
|
||||
|
||||
export function useConfigUpdate() {
|
||||
const configStore = useConfigStore()
|
||||
|
||||
|
||||
// 处理 WebSocket 配置更新
|
||||
const handleConfigUpdate = (data: ConfigUpdate) => {
|
||||
console.log("收到配置更新:", data)
|
||||
|
||||
// 更新全局配置 - 使用响应式方式
|
||||
if (data.key in configStore.config) {
|
||||
// 直接修改 ref 的值来触发响应式更新
|
||||
(configStore.config as any)[data.key] = data.value
|
||||
console.log(`配置 ${data.key} 已更新为:`, data.value)
|
||||
;(configStore.config as any)[data.key] = data.value
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化 WebSocket - handler 会在 onMounted 时自动添加
|
||||
const { connect, status } = useConfigWebSocket(handleConfigUpdate)
|
||||
|
||||
// 连接 WebSocket
|
||||
onMounted(() => {
|
||||
console.log("配置更新 WebSocket 正在连接...")
|
||||
connect()
|
||||
|
||||
// 测试连接 - 发送心跳包
|
||||
setTimeout(() => {
|
||||
if (status.value === "connected") {
|
||||
console.log("配置更新 WebSocket 连接成功,发送测试消息...")
|
||||
// 这里可以发送一个测试消息
|
||||
}
|
||||
}, 2000)
|
||||
})
|
||||
|
||||
// 监听连接状态
|
||||
watch(status, (newStatus) => {
|
||||
console.log("配置更新 WebSocket 状态:", newStatus)
|
||||
})
|
||||
|
||||
// 初始化 WebSocket - handler 会在 onMounted 时自动添加
|
||||
const { connect } = useConfigWebSocket(handleConfigUpdate)
|
||||
|
||||
onMounted(() => {
|
||||
connect()
|
||||
})
|
||||
return {
|
||||
connect
|
||||
connect,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { useConfigStore } from "shared/store/config"
|
||||
import { useConfigWebSocket, type ConfigUpdate } from "shared/composables/websocket"
|
||||
import {
|
||||
useConfigWebSocket,
|
||||
type ConfigUpdate,
|
||||
} from "shared/composables/websocket"
|
||||
|
||||
export function useMaxKB() {
|
||||
const configStore = useConfigStore()
|
||||
const isLoaded = ref(false)
|
||||
|
||||
|
||||
// 处理 WebSocket 配置更新 - 只处理 MaxKB 相关
|
||||
const handleConfigUpdate = (data: ConfigUpdate) => {
|
||||
console.log("MaxKB 收到配置更新:", data)
|
||||
if (data.key === 'enable_maxkb') {
|
||||
if (data.key === "enable_maxkb") {
|
||||
if (data.value) {
|
||||
loadMaxKBScript()
|
||||
} else {
|
||||
@@ -16,7 +18,7 @@ export function useMaxKB() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 初始化 WebSocket
|
||||
const { connect } = useConfigWebSocket(handleConfigUpdate)
|
||||
|
||||
@@ -27,7 +29,9 @@ export function useMaxKB() {
|
||||
return
|
||||
}
|
||||
|
||||
const existingScript = document.querySelector(`script[src="${import.meta.env.PUBLIC_MAXKB_URL}"]`)
|
||||
const existingScript = document.querySelector(
|
||||
`script[src="${import.meta.env.PUBLIC_MAXKB_URL}"]`,
|
||||
)
|
||||
if (existingScript) {
|
||||
isLoaded.value = true
|
||||
return
|
||||
@@ -38,12 +42,11 @@ export function useMaxKB() {
|
||||
script.src = import.meta.env.PUBLIC_MAXKB_URL
|
||||
script.async = true
|
||||
script.defer = true
|
||||
|
||||
|
||||
script.onload = () => {
|
||||
isLoaded.value = true
|
||||
console.log("MaxKB script loaded successfully")
|
||||
}
|
||||
|
||||
|
||||
script.onerror = () => {
|
||||
console.error("Failed to load MaxKB script")
|
||||
}
|
||||
@@ -52,15 +55,41 @@ export function useMaxKB() {
|
||||
}
|
||||
|
||||
const removeMaxKBScript = () => {
|
||||
// 把 script 也删除
|
||||
const script = document.querySelector(
|
||||
`script[src="${import.meta.env.PUBLIC_MAXKB_URL}"]`,
|
||||
)
|
||||
if (script) {
|
||||
script.remove()
|
||||
}
|
||||
// 等待DOM加载完成后删除所有id以"maxkb-"开头的元素
|
||||
const removeMaxKBElements = () => {
|
||||
// 查找所有id以"maxkb-"开头的元素
|
||||
const elements = document.querySelectorAll('[id^="maxkb-"]')
|
||||
|
||||
elements.forEach((element) => {
|
||||
element.remove()
|
||||
})
|
||||
}
|
||||
|
||||
// 如果DOM已经加载完成,直接执行删除
|
||||
if (document.readyState === "complete") {
|
||||
removeMaxKBElements()
|
||||
} else {
|
||||
// 等待DOM加载完成
|
||||
window.addEventListener("load", removeMaxKBElements, { once: true })
|
||||
}
|
||||
|
||||
// 移除MaxKB脚本标签
|
||||
const existingScript = document.querySelector(
|
||||
`script[src="${import.meta.env.PUBLIC_MAXKB_URL}"]`,
|
||||
)
|
||||
if (existingScript) {
|
||||
existingScript.remove()
|
||||
}
|
||||
|
||||
// 重置加载状态
|
||||
isLoaded.value = false
|
||||
const maxkbChatButton = document.getElementById("maxkb-chat-button")
|
||||
const maxkbChatContainer = document.getElementById("maxkb-chat-container")
|
||||
if (maxkbChatButton) {
|
||||
maxkbChatButton.remove()
|
||||
}
|
||||
if (maxkbChatContainer) {
|
||||
maxkbChatContainer.remove()
|
||||
}
|
||||
}
|
||||
|
||||
// 连接 WebSocket
|
||||
@@ -70,20 +99,19 @@ export function useMaxKB() {
|
||||
|
||||
watch(
|
||||
() => configStore.config.enable_maxkb,
|
||||
(newValue) => {
|
||||
if (newValue) {
|
||||
(enabled) => {
|
||||
if (enabled) {
|
||||
loadMaxKBScript()
|
||||
} else {
|
||||
console.log("removeMaxKBScript")
|
||||
removeMaxKBScript()
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
{ immediate: true },
|
||||
)
|
||||
|
||||
return {
|
||||
loadMaxKBScript,
|
||||
removeMaxKBScript,
|
||||
isLoaded: readonly(isLoaded)
|
||||
isLoaded: readonly(isLoaded),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ export const SYNC_MESSAGES = {
|
||||
SYNC_ON: "断开同步",
|
||||
SYNC_OFF: "开启同步",
|
||||
SYNCING_WITH: (name: string) => `🔗 与 ${name} 同步中`,
|
||||
STUDENT_LEFT: (name?: string) => name ? `💡 ${name}已离开` : "💡 可以关闭同步",
|
||||
STUDENT_LEFT: (name?: string) =>
|
||||
name ? `💡 ${name}已离开` : "💡 可以关闭同步",
|
||||
} as const
|
||||
|
||||
// 类型定义
|
||||
|
||||
@@ -313,7 +313,6 @@ class SubmissionWebSocket extends BaseWebSocket<SubmissionUpdate> {
|
||||
* 订阅特定提交的更新
|
||||
*/
|
||||
subscribe(submissionId: string) {
|
||||
console.log(`[WebSocket] 发送订阅请求: submission_id=${submissionId}`)
|
||||
const success = this.send({
|
||||
type: "subscribe",
|
||||
submission_id: submissionId,
|
||||
|
||||
Reference in New Issue
Block a user