-
-
Notifications
You must be signed in to change notification settings - Fork 336
feat: 增加已读历史记录能力,并支持区分已读文本,允许设置跳过是仅已读文本还是全部文本 #930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChangeSuger
wants to merge
6
commits into
OpenWebGAL:dev
Choose a base branch
from
ChangeSuger:feat/read-history
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ebcfd26
feat: 增加已读历史记录能力,并支持区分已读文本
ChangeSuger 916b57d
feat: 增加已读快进与全文快进能力
ChangeSuger 527a810
fix: typo
ChangeSuger cf7dcb9
feat: update
ChangeSuger 3a6cce8
feat: 使用 backlog 与 load 时能正确展示文本为已读状态
ChangeSuger 355e6bd
Merge branch 'dev' into pr/930
MakinoharaShoko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -176,3 +176,7 @@ | |
| .text { | ||
| overflow: hidden; | ||
| } | ||
|
|
||
| .read { | ||
| color: rgb(237, 162, 162); | ||
| } | ||
This file contains hidden or 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,118 @@ | ||
| /** | ||
| * 已读历史记录 | ||
| */ | ||
|
|
||
| import { webgalStore } from "@/store/store"; | ||
| import { SceneManager } from "./scene"; | ||
| import { setReadHistory } from "@/store/userDataReducer"; | ||
| import { setStage } from "@/store/stageReducer"; | ||
| import { setStorage } from "../controller/storage/storageController"; | ||
|
|
||
| export class ReadHistoryManager { | ||
| private history: Map<string, Uint8Array> = new Map(); | ||
|
|
||
| private load: boolean = false; | ||
|
|
||
| private readonly sceneManager: SceneManager; | ||
|
|
||
| public constructor(sceneManager: SceneManager) { | ||
| this.sceneManager = sceneManager; | ||
| } | ||
|
|
||
| private loadReadHistory() { | ||
| const readHistory = webgalStore.getState().userData.readHistory; | ||
|
|
||
| Object.entries(readHistory).forEach(([key, value]) => { | ||
| try { | ||
| const uint8 = Uint8Array.from(Buffer.from(value, 'base64')); | ||
| this.history.set(key, uint8); | ||
| } catch { | ||
| // 浏览器环境下没有 Buffer 时的兜底逻辑 | ||
| const binary = atob(value); | ||
| const uint8 = new Uint8Array(binary.length); | ||
| for (let i = 0; i < binary.length; i++) { | ||
| uint8[i] = binary.charCodeAt(i); | ||
| } | ||
| this.history.set(key, uint8); | ||
| } | ||
| }); | ||
|
|
||
| this.load = true; | ||
| } | ||
|
|
||
| private checkLoad() { | ||
| if (!this.load) { | ||
| this.loadReadHistory(); | ||
| } | ||
| } | ||
|
|
||
| private saveReadHistory(key: string) { | ||
| const bitset = this.history.get(key)!; | ||
|
|
||
| try { | ||
| const base64 = Buffer.from(bitset).toString('base64'); | ||
| webgalStore.dispatch(setReadHistory({ | ||
| key, | ||
| value: base64, | ||
| })); | ||
| } catch { | ||
| // 浏览器环境下没有 Buffer 时的兜底逻辑 | ||
| let binary = ''; | ||
| const len = bitset.length; | ||
| for (let i = 0; i < len; i++) { | ||
| binary += String.fromCharCode(bitset[i]); | ||
| } | ||
| const base64 = btoa(binary); | ||
| webgalStore.dispatch(setReadHistory({ | ||
| key, | ||
| value: base64, | ||
| })); | ||
| } | ||
| setStorage(); | ||
| } | ||
|
|
||
| private addReadHistory() { | ||
| const scenarioName = this.sceneManager.sceneData.currentScene.sceneName; | ||
| const index = this.sceneManager.sceneData.currentSentenceId; | ||
|
|
||
| if (!this.history.has(scenarioName)) { | ||
| const length = this.sceneManager.sceneData.currentScene.sentenceList.length; | ||
| this.history.set(scenarioName, new Uint8Array(Math.ceil(length / 8))); | ||
| } | ||
| let bitset = this.history.get(scenarioName)!; | ||
|
|
||
| // 处理因剧本更新可能导致的 index 溢出问题 | ||
| const requiredIndex = index >> 3; | ||
| if (requiredIndex >= bitset.length) { | ||
| const length = this.sceneManager.sceneData.currentScene.sentenceList.length; | ||
| const newBitset = new Uint8Array(Math.ceil(length / 8)); | ||
| newBitset.set(bitset); | ||
| bitset = newBitset; | ||
| this.history.set(scenarioName, bitset); | ||
| } | ||
|
|
||
| bitset[requiredIndex] |= (1 << (index & 7)); | ||
|
|
||
| this.saveReadHistory(scenarioName); | ||
| } | ||
|
|
||
| public checkIsRead() { | ||
| this.checkLoad(); | ||
|
|
||
| const scenarioName = this.sceneManager.sceneData.currentScene.sceneName; | ||
| const index = this.sceneManager.sceneData.currentSentenceId; | ||
|
|
||
| let isRead = false; | ||
| if (this.history.has(scenarioName)) { | ||
| const bitset = this.history.get(scenarioName)!; | ||
| isRead = (bitset[index >> 3] & (1 << (index & 7))) !== 0; | ||
| } | ||
| webgalStore.dispatch(setStage({ | ||
| key: 'isRead', | ||
| value: isRead, | ||
| })); | ||
| if (!isRead) { | ||
| this.addReadHistory(); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -234,3 +234,7 @@ $height: 330px; | |
| //line-height: 2em; | ||
| overflow: hidden; | ||
| } | ||
|
|
||
| .read { | ||
| color: rgb(237, 162, 162); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
scenarioNameis an empty string (which can happen during initialization or transition), the manager will attempt to record history for an invalid key. It's better to add a guard clause.