Skip to content

Commit 16ec0c7

Browse files
committed
Updated.
1 parent dd8c450 commit 16ec0c7

10 files changed

Lines changed: 138 additions & 69 deletions

src/app/home/components/book-list.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</mat-card-subtitle>
2727
</mat-card-header>
2828

29-
<mat-card-content *ngIf="book.desc.length > 0" fxLayout="row" fxLayoutGap="5px" class="cate-list">
29+
<mat-card-content *ngIf="book.desc.length > 0">
3030
<mat-label><b></b>{{book.desc}}<b></b></mat-label>
3131
</mat-card-content>
3232

src/app/home/components/reading-record-dialog.component.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import {
66
ElementRef,
77
} from '@angular/core';
88

9-
import {
10-
MatDialogRef,
11-
MAT_DIALOG_DATA
12-
} from '@angular/material/dialog';
13-
149
import {
1510
differenceInYears,
1611
differenceInMonths,
@@ -20,13 +15,18 @@ import {
2015
differenceInMinutes
2116
} from 'date-fns';
2217

18+
import {
19+
MatDialogRef,
20+
MAT_DIALOG_DATA
21+
} from '@angular/material/dialog';
22+
2323
import { Book, Record } from '../../models';
2424
import { CrudService } from '../../services/crud.service';
2525
import { MessageService } from '../../services/message.service';
2626
import {
2727
IBookWithPath,
2828
sortBy,
29-
IMessage
29+
IMessage,
3030
} from '../../vendor';
3131

3232
@Component({
@@ -60,6 +60,22 @@ export class ReadingRecordDialog implements OnInit{
6060
return sortBy(this._recordList, 'dateCreated');
6161
}
6262

63+
getReadableDate = (date: Date): string => {
64+
const now = new Date();
65+
const years = differenceInYears(now, date);
66+
const months = differenceInMonths(now, date);
67+
const weeks = differenceInWeeks(now, date);
68+
const days = differenceInDays(now, date);
69+
const hours = differenceInHours(now, date);
70+
const minutes = differenceInMinutes(now, date);
71+
72+
if(years > 0) return `${years} 年前`;
73+
if(months > 0) return `${months} 个月前`;
74+
if(weeks > 0) return `${weeks} 周前`;
75+
if(days > 0) return `${days} 天前`;
76+
if(hours > 0) return `${hours} 小时前`;
77+
return `${minutes} 分钟前`;
78+
}
6379
ngOnInit() {
6480
this.message.getMessage().subscribe((msg: IMessage) => {
6581
if(msg.event === 'book-list-updated'){
@@ -80,23 +96,6 @@ export class ReadingRecordDialog implements OnInit{
8096
this.crud.ipcRenderer.send('open-book-with-path', msg);
8197
}
8298

83-
getReadableDate = (date: Date): string => {
84-
const now = new Date();
85-
const years = differenceInYears(now, date);
86-
const months = differenceInMonths(now, date);
87-
const weeks = differenceInWeeks(now, date);
88-
const days = differenceInDays(now, date);
89-
const hours = differenceInHours(now, date);
90-
const minutes = differenceInMinutes(now, date);
91-
92-
if(years > 0) return `${years} 年前`;
93-
if(months > 0) return `${months} 个月前`;
94-
if(weeks > 0) return `${weeks} 周前`;
95-
if(days > 0) return `${days} 天前`;
96-
if(hours > 0) return `${hours} 小时前`;
97-
return `${minutes} 分钟前`;
98-
}
99-
10099
onHeaderClick(event) {
101100
if (!this.expandEnabled) {
102101
event.stopPropagation();

src/app/home/components/search.component.html

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
<div fxLayout="column" fxLayoutAlign="start stretch" style="background-color: #424242;">
2-
<form fxLayout="row" fxLayoutAlign="center center" fxLayoutGap="10px">
1+
<div fxLayout="column" fxLayoutAlign="start stretch">
2+
<form>
3+
<div fxLayout="row"
4+
fxLayoutAlign="center center"
5+
class="search-form"
6+
slot="fixed"
7+
fxLayoutGap="10px"
8+
style="background-color: #424242;">
39
<mat-form-field class="search-input-form-field" fxFlex="25">
410
<mat-label>关键字</mat-label>
511
<input type="text" matInput [formControl]="keywordsFormControl"
@@ -26,17 +32,49 @@
2632
</mat-radio-group>
2733

2834
<button mat-stroked-button color="primary"
29-
(click)="search()"
30-
[disabled]="!keywordsFormControl.valid">
35+
(click)="search(1)"
36+
[disabled]="!keywordsFormControl.valid">
3137
<ion-text color="light"><ion-icon name="search-outline"></ion-icon>搜索</ion-text>
3238
</button>
39+
</div>
3340
</form>
3441

35-
<div fxLayout="column" fxLayoutAlign="space-evenly stretch">
42+
<div fxLayout="row" fxLayoutAlign="none stretch" style="background-color: white;">
3643
<!--搜索结果-->
37-
<div fxFlex="75"></div>
44+
<div fxFlex="60" fxLayout="column"
45+
fxLayoutAlign="start stretch"
46+
fxLayoutGap="10px"
47+
fxFlexOffset="10px"
48+
style="top: 10px;">
49+
<mat-card class="search-result" *ngFor="let book of bookList">
50+
<mat-card-header>
51+
<img mat-card-avatar
52+
uiImageLoader
53+
onErrorSrc="assets/images/avatar.png"
54+
[src]="book.writerAvatarUrl" />
55+
<mat-card-title>
56+
{{book.fullName}}
57+
</mat-card-title>
58+
<mat-card-subtitle>
59+
{{book.writerName}}
60+
</mat-card-subtitle>
61+
</mat-card-header>
62+
63+
<mat-card-content *ngIf="book.desc.length > 0">
64+
<mat-label><b></b>{{book.desc.length > 256 ? book.desc.slice(0,256)+'...' : book.desc}}<b></b></mat-label>
65+
</mat-card-content>
66+
67+
<mat-card-footer fxLayout="row" fxLayoutAlign="none stretch" fxLayoutGap="50px">
68+
<ion-label>
69+
获星<ion-icon name="star"></ion-icon><i>{{book.stars}}</i>
70+
</ion-label>
71+
<ion-label>
72+
更新时间:<i>{{book.dateUpdated | date : 'yyyy-M-d H:mm:ss' : 'GMT+8' }}</i>
73+
</ion-label>
74+
</mat-card-footer>
75+
</mat-card>
76+
</div>
3877
<!--最近搜索条目-->
3978
<div></div>
4079
</div>
4180
</div>
42-
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.mat-input-element {
2+
color: white;
3+
}

src/app/home/components/search.component.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {FormControl, FormGroupDirective, NgForm, Validators} from '@angular/form
44
import {ErrorStateMatcher} from '@angular/material/core';
55

66
import { FetchService } from '../services/fetch.service';
7-
import { ICloudBook } from '../../vendor';
7+
import { MessageService } from '../../services/message.service';
8+
9+
import {
10+
ICloudBook,
11+
IMessage
12+
} from '../../vendor';
813

914
/** Error when invalid control is dirty, touched, or submitted. */
1015
export class MyErrorStateMatcher implements ErrorStateMatcher {
@@ -37,24 +42,35 @@ export class SearchComponent implements OnInit {
3742
icon: 'assets/images/gitlab-seeklogo.com.svg',
3843
}];
3944

45+
matcher = new MyErrorStateMatcher();
4046
keywords: string = '';
4147
platformSelected: string = 'github.com';
4248
bookListCount: number;
4349

4450
constructor(
45-
private fetchService: FetchService
51+
private fetchService: FetchService,
52+
private message: MessageService
4653
) { }
4754

48-
ngOnInit() {}
55+
ngOnInit() {
56+
this.message.getMessage().subscribe(async (msg: IMessage) => {
57+
if(msg.event === 'scrolled-to-end'){
58+
if(this.bookList.length%20 === 0 && !(/gitlab/.test(this.platformSelected))){
59+
this.search(this.bookList.length/20 + 1);
60+
}
61+
}
62+
});
63+
}
4964

5065
clearKeywords = () => {
5166
this.keywords = '';
52-
this.bookList = [].slice();
5367
}
54-
55-
search = async () => {
56-
const res = await this.fetchService.searchBooks(this.platformSelected, this.keywords, 1) as object[] | object;
57-
console.log(res)
68+
/*
69+
*
70+
* @page, 传入 1 表示首次搜索,需要清空原有 bookList
71+
*/
72+
search = async (page: number) => {
73+
const res = await this.fetchService.searchBooks(this.platformSelected, this.keywords, page) as object[] | object;
5874

5975
let _bookList: object[]
6076
if(/github/.test(this.platformSelected)){
@@ -64,9 +80,10 @@ export class SearchComponent implements OnInit {
6480
_bookList = (res as object[]).slice();
6581
}
6682

67-
this.bookList = [].slice();
83+
if(page === 1) this.bookList = [].slice();
6884
_bookList.map((bookRaw: object) => {
6985
const book: ICloudBook = {
86+
fullName: '',
7087
url: '',
7188
desc: '',
7289
writerName: '',
@@ -76,26 +93,25 @@ export class SearchComponent implements OnInit {
7693
};
7794

7895
book.desc = bookRaw['description'] ? bookRaw['description'] : '';
96+
7997
if(/gitlab/.test(this.platformSelected)){
8098
book.dateUpdated = bookRaw['last_activity_at'];
8199
book.writerName = bookRaw['namespace']['name'];
82100
book.writerAvatarUrl = bookRaw['namespace']['avatar_url'];
83101
book.url = bookRaw['http_url_to_repo'];
84102
book.stars = bookRaw['star_count'];
103+
book.fullName = bookRaw['path_with_namespace'];
85104
}
86105
if(!(/gitlab/.test(this.platformSelected))){
87106
book.dateUpdated = bookRaw['updated_at'];
88107
book.writerName = /github/.test(this.platformSelected) ? bookRaw['owner']['login'] : bookRaw['owner']['name'];
89108
book.writerAvatarUrl = bookRaw['owner']['avatar_url'];
90109
book.url = /github/.test(this.platformSelected) ? bookRaw['clone_url'] : bookRaw['html_url'];
91110
book.stars = bookRaw['stargazers_count'];
111+
book.fullName = bookRaw['full_name'];
92112
}
93113

94114
this.bookList.push(book);
95115
});
96-
97-
console.log(this.bookList);
98116
}
99-
100-
matcher = new MyErrorStateMatcher();
101117
}

src/app/home/home.page.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@
6666
</ion-fab>
6767
</ion-toolbar>
6868
</ion-header>
69-
<ion-content padding>
69+
<ion-content
70+
[scrollEvents]="true"
71+
(ionScrollEnd)="onScrollEnd()"
72+
padding>
7073
<section class="book-list">
7174
<app-book-list [_bookList]="bookList"
7275
*ngIf="!search"

src/app/home/home.page.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,11 @@ export class HomePage implements OnInit, AfterViewInit {
197197
if(res) this.book.save(res);
198198
});
199199
}
200+
201+
onScrollEnd = () => {
202+
const msg: IMessage = {
203+
event: 'scrolled-to-end'
204+
}
205+
if(this.search) this.message.sendMessage(msg);
206+
}
200207
}

src/app/home/services/fetch.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,19 @@ export class FetchService {
9696
let header: string;
9797

9898
if(/github/.test(websiteUri)) {
99-
const q = encodeURIComponent(keywords + ' gitbook');
99+
const q = encodeURIComponent(keywords);
100100
url = `https://api.github.com/search/repositories?q=${q}&sort=stars&order=desc&per_page=20&page=${page}`;
101101
header = "Accept: application/vnd.github.v3+json";
102102
}
103103

104104
if(/gitee/.test(websiteUri)) {
105-
const q = encodeURIComponent(keywords+' gitbook');
105+
const q = encodeURIComponent(keywords);
106106
url = `https://gitee.com/api/v5/search/repositories?access_token=${this.tokens.giteeToken}&q=${q}&page=${page}&per_page=20&order=desc&sort=stars_count`;
107107
header = "Content-Type: application/json;charset=UTF-8";
108108
}
109109

110110
if(/gitlab/.test(websiteUri)){
111-
const q = encodeURIComponent(keywords+' gitbook');
111+
const q = encodeURIComponent(keywords);
112112
url = `https://gitlab.com/api/v4/search?scope=projects&search=${q}`;
113113
header = `PRIVATE-TOKEN: ${this.tokens.gitlabToken}`;
114114
}

src/app/material.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {MatChipsModule} from '@angular/material/chips';
1717
import {MatAutocompleteModule} from '@angular/material/autocomplete';
1818
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
1919
import {MatSnackBarModule} from '@angular/material/snack-bar';
20+
import {MatBadgeModule} from '@angular/material/badge';
2021

2122
@NgModule({
2223
declarations: [],
@@ -26,6 +27,7 @@ import {MatSnackBarModule} from '@angular/material/snack-bar';
2627
MatProgressBarModule,
2728
MatInputModule,
2829
MatTooltipModule,
30+
MatBadgeModule,
2931
MatSnackBarModule,
3032
MatDialogModule,
3133
MatCardModule,
@@ -56,6 +58,7 @@ import {MatSnackBarModule} from '@angular/material/snack-bar';
5658
MatDividerModule,
5759
MatTooltipModule,
5860
MatToolbarModule,
61+
MatBadgeModule,
5962
FlexLayoutModule,
6063
MatFormFieldModule,
6164
]

src/app/vendor.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Book, Category, Writer, Website, Record } from './models';
22
import { ValidatorFn, AbstractControl } from '@angular/forms';
33
// typeorm parts
4-
54
export const sortBy = (list: Array<any>, prop: string, subProp?: string) => {
65
return subProp ? list.sort((a, b) => a[prop][subProp] > b[prop][subProp] ? -1 : a[prop][subProp] === b[prop][subProp] ? 0 : 1)
76
: list.sort((a, b) => a[prop] > b[prop] ? -1 : a[prop] === b[prop] ? 0 : 1);
87
}
98

109
export interface ICloudBook {
10+
fullName: string;
1111
url: string;
1212
desc: string;
1313
writerName: string;
@@ -204,26 +204,26 @@ export interface IMessage {
204204
}
205205

206206
export const join = (start: string, end: string): string => {
207-
if (start.length === 0) {
208-
return end;
209-
}
210-
if (end.length === 0) {
211-
return start;
212-
}
213-
let slashes = 0;
214-
if (start.endsWith('/')) {
215-
slashes++;
216-
}
217-
if (end.startsWith('/')) {
218-
slashes++;
219-
}
220-
if (slashes === 2) {
221-
return start + end.substring(1);
222-
}
223-
if (slashes === 1) {
224-
return start + end;
225-
}
226-
return start + '/' + end;
207+
if (start.length === 0) {
208+
return end;
209+
}
210+
if (end.length === 0) {
211+
return start;
212+
}
213+
let slashes = 0;
214+
if (start.endsWith('/')) {
215+
slashes++;
216+
}
217+
if (end.startsWith('/')) {
218+
slashes++;
219+
}
220+
if (slashes === 2) {
221+
return start + end.substring(1);
222+
}
223+
if (slashes === 1) {
224+
return start + end;
225+
}
226+
return start + '/' + end;
227227
}
228228

229229

0 commit comments

Comments
 (0)