-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: shortcut key settings for submit operations
- Loading branch information
zwd
committed
Feb 22, 2024
1 parent
a070b9f
commit fbbb96c
Showing
4 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { ref } from "vue"; | ||
|
||
type AnswerSubmitKeys = { | ||
label: string; | ||
value: string; | ||
defaultChecked: boolean; | ||
}; | ||
|
||
export const LOCAL_SUBMIT_KEY = "submitKey"; | ||
const answerSubmitKeys = ref<AnswerSubmitKeys[]>([ | ||
{ | ||
label: "回车键", | ||
value: "Enter", | ||
defaultChecked: true, | ||
}, | ||
{ | ||
label: "空格键", | ||
value: "Space", | ||
defaultChecked: false, | ||
}, | ||
{ | ||
label: "都支持", | ||
value: "All", | ||
defaultChecked: false, | ||
}, | ||
]); | ||
|
||
function store(value: string) { | ||
localStorage.setItem(LOCAL_SUBMIT_KEY, value); | ||
} | ||
|
||
function loadCache() { | ||
const submitKey = localStorage.getItem(LOCAL_SUBMIT_KEY); | ||
store(submitKey || "Enter"); | ||
} | ||
|
||
function handleCheckRadio(data: AnswerSubmitKeys) { | ||
answerSubmitKeys.value.forEach((item) => { | ||
item.defaultChecked = false; | ||
}); | ||
data.defaultChecked = true; | ||
store(data.value); | ||
} | ||
|
||
function showSubmitKey() { | ||
return localStorage.getItem(LOCAL_SUBMIT_KEY); | ||
} | ||
|
||
export function useSubmitKey() { | ||
loadCache(); | ||
return { | ||
answerSubmitKeys, | ||
handleCheckRadio, | ||
showSubmitKey, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { it, expect, describe, beforeEach } from "vitest"; | ||
import { LOCAL_SUBMIT_KEY, useSubmitKey } from "../submitKey"; | ||
|
||
describe("set submit key", () => { | ||
beforeEach(() => { | ||
localStorage.removeItem(LOCAL_SUBMIT_KEY); | ||
}); | ||
it("default value should be Enter if no cache", () => { | ||
const { showSubmitKey } = useSubmitKey(); | ||
expect(showSubmitKey()).toBe("Enter"); | ||
}); | ||
|
||
it("should be equal to cache value if it exists", () => { | ||
localStorage.setItem(LOCAL_SUBMIT_KEY, "Space"); | ||
const { showSubmitKey } = useSubmitKey(); | ||
expect(showSubmitKey()).toBe("Space"); | ||
}); | ||
|
||
it("should be change value", () => { | ||
const { showSubmitKey, handleCheckRadio } = useSubmitKey(); | ||
const mockData = { | ||
label: "都支持", | ||
value: "All", | ||
defaultChecked: false, | ||
}; | ||
handleCheckRadio(mockData); | ||
expect(showSubmitKey()).toBe("All"); | ||
}); | ||
}); |