Skip to content

Commit 4434fd6

Browse files
committed
feat: 支持动态日期和❤️符号
1 parent d99937c commit 4434fd6

File tree

4 files changed

+14
-6
lines changed

4 files changed

+14
-6
lines changed

src/app/app.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<d-calendar-graph></d-calendar-graph>
2-
<d-calendar-graph [dateRange]="['2019-06-01', '2020-11-01']" text="DevUI is excellent"></d-calendar-graph>
2+
<d-calendar-graph text="❤DevUI"></d-calendar-graph>
3+
<d-calendar-graph text="DevUI is excellent"></d-calendar-graph>
34
<router-outlet></router-outlet>

src/app/calendar-graph/calendar-graph.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@ export class CalendarGraphComponent implements OnInit {
2323

2424
ngOnInit() {
2525
const now = moment().format(DEFAULT_DATE_FORMAT);
26-
const lastYear = moment().subtract(1, "years").format(DEFAULT_DATE_FORMAT);
26+
// 开始日期默认为最近一年
27+
let startDate = moment().subtract(1, 'years').format(DEFAULT_DATE_FORMAT);
28+
29+
if (this.text) {
30+
const symbolArray = textToSymbolArray(this.text);
31+
// 根据文本动态计算开始日期
32+
startDate = moment().subtract(symbolArray.length, 'weeks').format(DEFAULT_DATE_FORMAT);
33+
}
2734

28-
let dateRange = [ lastYear, now ];
35+
let dateRange = [ startDate, now ];
2936
if (this.dateRange) {
3037
dateRange = getCompleteDateRange(this.dateRange);
3138
}

src/app/calendar-graph/calendar.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,5 @@ export const SYMBOL_MAP = {
113113
'8': LETTER_8,
114114
'9': LETTER_9,
115115
' ': EMPTY_WEEK,
116-
'❤️': IMG_LOVE,
116+
'2764': IMG_LOVE, // '❤️'.charCodeAt(0).toString(16) = 2764 使用Unicode编码代替具体的符号
117117
};

src/app/calendar-graph/calendar.util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export function transpose(A) {
4040
let result = [], rowItem = [];
4141
const rows = A.length;
4242
const cols = A[0].length;
43-
for(let i = 0;i < cols;i++){
43+
for(let i = 0;i < cols;i++){
4444
rowItem = [];
4545
for(let j = 0;j < rows;j++){
4646
rowItem.push(A[j][i]);
@@ -53,7 +53,7 @@ export function transpose(A) {
5353
// 文字转字母数组
5454
export function textToSymbolArray(text) {
5555
return text.split('').join(' ').split('')
56-
.map(char => SYMBOL_MAP[char])
56+
.map(char => SYMBOL_MAP[char] || SYMBOL_MAP[char.charCodeAt(0).toString(16)])
5757
.reduce((a, b) => a.concat(b), []);
5858
}
5959

0 commit comments

Comments
 (0)