This commit is contained in:
94
src/oj/store/code.ts
Normal file
94
src/oj/store/code.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { defineStore } from "pinia"
|
||||
import { STORAGE_KEY } from "utils/constants"
|
||||
import storage from "utils/storage"
|
||||
import { Code, LANGUAGE } from "utils/types"
|
||||
|
||||
/**
|
||||
* 代码编辑器状态管理 Store
|
||||
* 管理全局的代码、输入、输出状态
|
||||
*/
|
||||
export const useCodeStore = defineStore("code", () => {
|
||||
// ==================== 状态 ====================
|
||||
const code = reactive<Code>({
|
||||
value: "",
|
||||
language: storage.get(STORAGE_KEY.LANGUAGE) || "Python3",
|
||||
})
|
||||
|
||||
const input = ref("")
|
||||
const output = ref("")
|
||||
|
||||
// ==================== 计算属性 ====================
|
||||
const isEmpty = computed(() => code.value.trim() === "")
|
||||
|
||||
// ==================== 操作 ====================
|
||||
/**
|
||||
* 设置代码内容
|
||||
*/
|
||||
function setCode(value: string) {
|
||||
code.value = value
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置编程语言
|
||||
*/
|
||||
function setLanguage(language: LANGUAGE) {
|
||||
code.language = language
|
||||
storage.set(STORAGE_KEY.LANGUAGE, language)
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置输入
|
||||
*/
|
||||
function setInput(value: string) {
|
||||
input.value = value
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置输出
|
||||
*/
|
||||
function setOutput(value: string) {
|
||||
output.value = value
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置所有状态
|
||||
*/
|
||||
function reset() {
|
||||
code.value = ""
|
||||
input.value = ""
|
||||
output.value = ""
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空输出
|
||||
*/
|
||||
function clearOutput() {
|
||||
output.value = ""
|
||||
}
|
||||
|
||||
// 监听语言变化,保存到本地存储
|
||||
watch(
|
||||
() => code.language,
|
||||
(newLanguage) => {
|
||||
storage.set(STORAGE_KEY.LANGUAGE, newLanguage)
|
||||
},
|
||||
)
|
||||
|
||||
return {
|
||||
// 状态
|
||||
code,
|
||||
input,
|
||||
output,
|
||||
|
||||
// 计算属性
|
||||
isEmpty,
|
||||
|
||||
// 操作
|
||||
setCode,
|
||||
setLanguage,
|
||||
setInput,
|
||||
setOutput,
|
||||
reset,
|
||||
clearOutput,
|
||||
}
|
||||
})
|
||||
66
src/oj/store/problem.ts
Normal file
66
src/oj/store/problem.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { defineStore } from "pinia"
|
||||
import { Problem } from "utils/types"
|
||||
|
||||
/**
|
||||
* 题目状态管理 Store
|
||||
* 管理当前题目的信息
|
||||
*/
|
||||
export const useProblemStore = defineStore("problem", () => {
|
||||
// ==================== 状态 ====================
|
||||
const problem = ref<Problem | null>(null)
|
||||
|
||||
// ==================== 计算属性 ====================
|
||||
const hasProblem = computed(() => problem.value !== null)
|
||||
|
||||
const problemId = computed(() => problem.value?._id ?? null)
|
||||
|
||||
const problemTitle = computed(() => problem.value?.title ?? "")
|
||||
|
||||
const difficulty = computed(() => problem.value?.difficulty ?? "")
|
||||
|
||||
const languages = computed(() => problem.value?.languages ?? [])
|
||||
|
||||
const isACed = computed(() => problem.value?.my_status === 0)
|
||||
|
||||
// ==================== 操作 ====================
|
||||
/**
|
||||
* 设置当前题目
|
||||
*/
|
||||
function setProblem(newProblem: Problem | null) {
|
||||
problem.value = newProblem
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空当前题目
|
||||
*/
|
||||
function clearProblem() {
|
||||
problem.value = null
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新题目的部分字段
|
||||
*/
|
||||
function updateProblem(updates: Partial<Problem>) {
|
||||
if (problem.value) {
|
||||
problem.value = { ...problem.value, ...updates }
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// 状态
|
||||
problem,
|
||||
|
||||
// 计算属性
|
||||
hasProblem,
|
||||
problemId,
|
||||
problemTitle,
|
||||
difficulty,
|
||||
languages,
|
||||
isACed,
|
||||
|
||||
// 操作
|
||||
setProblem,
|
||||
clearProblem,
|
||||
updateProblem,
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user