Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd1d5384

Browse files
committed
setup position/progress/tutorial context
1 parentb1eb6b1 commitd1d5384

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

‎src/channel/state/Position.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import*asCRfrom'typings'
2+
import*asGfrom'typings/graphql'
3+
4+
// position
5+
classPosition{
6+
privatevalue:CR.Position
7+
constructor(){
8+
this.value={
9+
levelId:'',
10+
stageId:'',
11+
stepId:'',
12+
}
13+
}
14+
publicget=()=>{
15+
returnthis.value
16+
}
17+
publicset=(value:CR.Position)=>{
18+
this.value=value
19+
}
20+
// calculate the current position based on the saved progress
21+
publicsetPositionFromProgress=(tutorial:G.Tutorial,progress:CR.Progress):CR.Position=>{
22+
23+
// tutorial already completed
24+
// TODO: handle start again?
25+
if(progress.complete){
26+
returnthis.value
27+
}
28+
29+
const{levels}=tutorial.version
30+
31+
constlastLevelIndex:number|undefined=levels.findIndex((l:G.Level)=>progress.levels[l.id])
32+
// TODO: consider all levels complete as progress.complete
33+
if(lastLevelIndex<0&&lastLevelIndex<levels.length){
34+
thrownewError('Error setting progress level')
35+
}
36+
constcurrentLevel:G.Level=levels[lastLevelIndex+1]
37+
38+
const{stages}=currentLevel
39+
40+
constlastStageIndex:number|undefined=stages.findIndex((s:G.Stage)=>progress.stages[s.id])
41+
if(lastStageIndex<0&&lastStageIndex<stages.length){
42+
thrownewError('Error setting progress stage')
43+
}
44+
constcurrentStage:G.Stage=stages[lastStageIndex+1]
45+
46+
const{steps}=currentStage
47+
48+
constlastStepIndex:number|undefined=steps.findIndex((s:G.Step)=>progress.steps[s.id])
49+
if(lastStepIndex<0&&lastStepIndex<steps.length){
50+
thrownewError('Error setting progress step')
51+
}
52+
constcurrentStep:G.Step=steps[lastStepIndex+1]
53+
54+
this.value={
55+
levelId:currentLevel.id,
56+
stageId:currentStage.id,
57+
stepId:currentStep.id,
58+
}
59+
returnthis.value
60+
}
61+
}
62+
63+
exportdefaultPosition

‎src/channel/state/Progress.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import*asCRfrom'typings'
2+
import*asGfrom'typings/graphql'
3+
import*asvscodefrom'vscode'
4+
5+
importStoragefrom'../../services/storage'
6+
7+
constdefaultValue={
8+
levels:{},
9+
stages:{},
10+
steps:{},
11+
complete:false
12+
}
13+
14+
// hold current progress and sync to storage based on tutorial.id/version
15+
classProgress{
16+
privatevalue:CR.Progress
17+
privatestorage:Storage<CR.Progress>|undefined
18+
constructor(){
19+
this.value=defaultValue
20+
}
21+
publicsetTutorial=(workspaceState:vscode.Memento,tutorial:G.Tutorial)=>{
22+
this.storage=newStorage<CR.Progress>({
23+
key:`coderoad:progress:${tutorial.id}:${tutorial.version}`,
24+
storage:workspaceState,
25+
defaultValue,
26+
})// set value from storage
27+
this.storage.get().then((value:CR.Progress)=>{
28+
this.value=value
29+
})
30+
}
31+
publicget=()=>{
32+
returnthis.value
33+
}
34+
publicset=(value:CR.Progress)=>{
35+
this.value=value
36+
if(this.storage){
37+
this.storage.set(value)
38+
}
39+
}
40+
}
41+
42+
exportdefaultProgress

‎src/channel/state/Tutorial.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import*asGfrom'typings/graphql'
2+
import*asvscodefrom'vscode'
3+
4+
importStoragefrom'../../services/storage'
5+
6+
// Tutorial
7+
classTutorial{
8+
privatestorage:Storage<G.Tutorial|null>
9+
privatevalue:G.Tutorial|null
10+
constructor(workspaceState:vscode.Memento){
11+
this.storage=newStorage<G.Tutorial|null>({
12+
key:'coderoad:currentTutorial',
13+
storage:workspaceState,
14+
defaultValue:null
15+
})
16+
this.value=null
17+
// set value from storage
18+
this.storage.get().then((value:G.Tutorial|null)=>{
19+
this.value=value
20+
})
21+
}
22+
publicget=()=>{
23+
returnthis.value
24+
}
25+
publicset=(value:G.Tutorial)=>{
26+
this.value=value
27+
}
28+
}
29+
30+
exportdefaultTutorial

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp