@@ -18,12 +18,17 @@ function generateGuid(): string {
18
18
19
19
20
20
21
+ // 函数matchTimeSpan用于匹配字符串中的时间跨度
21
22
function matchTimeSpan ( s : string ) : TimeSpan {
23
+ // 定义正则表达式,匹配字符串中的数字
22
24
const regex = / \d + / gi;
25
+ // 获取字符串中匹配到的数字
23
26
const nums = s . match ( regex ) ;
27
+ // 判断匹配到的数字是否为4个,不是则抛出错误
24
28
if ( nums == null || nums . length != 4 ) {
25
29
throw new Error ( "无效的时间格式:" + s ) ;
26
30
}
31
+ // 定义开始时间和结束时间
27
32
const start = new Date ( ) ;
28
33
start . setUTCHours ( + nums [ 0 ] ) ;
29
34
start . setUTCMinutes ( + nums [ 1 ] ) ;
@@ -34,20 +39,27 @@ function matchTimeSpan(s: string): TimeSpan {
34
39
end . setUTCMinutes ( + nums [ 3 ] ) ;
35
40
end . setUTCSeconds ( 0 ) ;
36
41
end . setUTCMilliseconds ( 0 ) ;
42
+ // 返回开始时间和结束时间
37
43
return {
38
44
start : start ,
39
45
end : end
40
46
}
41
47
}
42
48
43
49
export function convertEcsToClassIsland ( profile : Schedule ) : Profile {
50
+ // 创建一个Profile实例
44
51
const classIsland = new Profile ( ) ;
52
+ // 创建一个Map,用于存储学科的guid
45
53
const subjectMapping = new Map < string , string > ( ) ;
54
+ // 创建一个Map,用于存储时间表的guid
46
55
const timeTableMapping = new Map < string , string > ( ) ;
47
56
// 处理科目
48
57
profile . subject_name . forEach ( ( v , k ) => {
58
+ // 生成一个guid
49
59
const guid = generateGuid ( ) ;
60
+ // 将subject_name和guid存入subjectMapping
50
61
subjectMapping . set ( k , guid ) ;
62
+ // 将guid和subject信息存入classIsland.Subjects
51
63
classIsland . Subjects . set ( guid , {
52
64
Name : v ,
53
65
Initial : k ,
@@ -56,30 +68,48 @@ export function convertEcsToClassIsland(profile: Schedule): Profile {
56
68
} ) ;
57
69
} )
58
70
// 处理时间表
59
- profile . timetable . forEach ( ( v , k ) => {
71
+ // 为profile的timetable循环
72
+ profile . timetable . forEach ( ( v , k ) => {
73
+ // 生成GUID
60
74
const guid = generateGuid ( ) ;
75
+ // 存储GUID到timeTableMapping
61
76
timeTableMapping . set ( k , guid ) ;
77
+ // 创建TimeLayout实例
62
78
const tl = new TimeLayout ( ) ;
79
+ // 如果v.size小于等于0,返回
63
80
if ( v . size <= 0 ) {
64
81
return ;
65
82
}
83
+ // 设置TimeLayout的name
66
84
tl . Name = k ;
85
+ // 初始化last为undefined
67
86
let last : TimeLayoutItem | undefined ;
87
+ // 获取dividers
68
88
const dividers = profile . divider . get ( k ) ;
89
+ // 遍历v
69
90
v . forEach ( ( v , k ) => {
91
+ // 创建TimeLayoutItem实例
70
92
const tp = new TimeLayoutItem ( ) ;
93
+ // 获取时间段
71
94
const ts = matchTimeSpan ( k ) ;
72
95
console . debug ( "匹配时间段:" , ts ) ;
96
+ // 设置TimeLayoutItem的startSecond
73
97
tp . StartSecond = ts . start ;
98
+ // 设置TimeLayoutItem的endSecond
74
99
tp . EndSecond = ts . end ;
100
+ // 如果last不为undefined,设置last的endSecond为tp的startSecond
75
101
if ( last != undefined ) {
76
102
last . EndSecond = tp . StartSecond ;
77
103
}
104
+ // 如果v的类型为string,设置tp的timetype为1
78
105
if ( typeof ( v ) == "string" ) {
79
106
tp . TimeType = 1 ;
80
107
}
108
+ // 设置last为tp
81
109
last = tp
110
+ // 将tp添加到tl的layouts数组中
82
111
tl . Layouts . push ( tp ) ;
112
+ // 如果v的类型为number,并且dividers包含v,创建TimeLayoutItem实例tpd,设置tpd的startSecond和endSecond,将tpd添加到tl的layouts数组中
83
113
if ( typeof ( v ) == "number" && dividers != undefined && dividers . includes ( v as number ) ) {
84
114
const startDivider = new Date ( ts . end . getTime ( ) + 120 ) ;
85
115
const tpd = new TimeLayoutItem ( ) ;
@@ -88,17 +118,23 @@ export function convertEcsToClassIsland(profile: Schedule): Profile {
88
118
tl . Layouts . push ( tpd )
89
119
}
90
120
} ) ;
121
+ // 获取tl的layouts数组中的第一个元素
91
122
const first = tl . Layouts [ 0 ] ;
123
+ // 获取tl的layouts数组中的最后一个元素
92
124
const end = tl . Layouts [ tl . Layouts . length - 1 ] ;
125
+ // 如果first的startSecond的时为0,且分为为0,将first从tl的layouts数组中移除
93
126
if ( first . StartSecond . getUTCHours ( ) == 0 && first . StartSecond . getUTCMinutes ( ) == 0 ) {
94
127
tl . Layouts . shift ( ) ;
95
128
}
129
+ // 如果end的endSecond的时为23,且分为59,将end从tl的layouts数组中移除
96
130
if ( end != undefined && end . EndSecond . getUTCHours ( ) == 23 && end . EndSecond . getUTCMinutes ( ) == 59 ) {
97
131
tl . Layouts . pop ( ) ;
98
132
}
133
+ // 对tl的layouts数组排序,根据startSecond
99
134
tl . Layouts . sort ( ( x , y ) => {
100
135
return x . StartSecond . getUTCSeconds ( ) - y . EndSecond . getUTCSeconds ( ) ;
101
136
} ) ;
137
+ // 将tl添加到classIsland的TimeLayouts中
102
138
classIsland . TimeLayouts . set ( guid , tl ) ;
103
139
} )
104
140
0 commit comments