Skip to content

Commit df64209

Browse files
committed
refactory: browser service
1 parent 11819d0 commit df64209

29 files changed

Lines changed: 207 additions & 323 deletions

.idea/workspace.xml

Lines changed: 57 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/app/app.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { provideAnimations } from '@angular/platform-browser/animations'
55
import { provideRouter, withComponentInputBinding } from '@angular/router'
66
import { routes } from './app.routes'
77
import { authInterceptor } from './interceptors/auth.interceptor'
8+
import { transferInterceptor } from './interceptors/transferInterceptor'
89

910
export const appConfig: ApplicationConfig = {
1011
providers: [
1112
provideZoneChangeDetection({ eventCoalescing: true }),
12-
provideHttpClient(withFetch(), withInterceptors([authInterceptor])),
13+
provideHttpClient(withFetch(), withInterceptors([authInterceptor, transferInterceptor])),
1314
provideRouter(routes, withComponentInputBinding()),
1415
provideAnimations(),
1516
provideClientHydration()

client/app/components/admin-layout/admin-layout.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<div [class]="[
55
'fixed lg:relative lg:flex z-40 transition-transform duration-300 ease-in-out w-full h-full',
66
]">
7-
@if (isSidebarOpen$()) {
7+
@if (layoutService.isSidebarOpen$()) {
88
<app-admin-sidebar />
99
}
1010
</div>
@@ -13,7 +13,7 @@
1313
<app-admin-header />
1414
<main class="flex-1 p-4 lg:p-6">
1515
<div class="max-w-full lg:max-w-80% w-full lg:w-360 mx-auto">
16-
@if (isView) {
16+
@if (browserService.is) {
1717
<router-outlet />
1818
}
1919
</div>

client/app/components/admin-layout/admin-layout.component.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@ import { AdminSidebarComponent } from '../admin-sidebar/admin-sidebar.component'
1313
templateUrl: './admin-layout.component.html'
1414
})
1515
export class AdminLayoutComponent {
16-
public isView = false
17-
public isSidebarOpen$
18-
1916
public constructor(
20-
private readonly browserService: BrowserService,
21-
private readonly layoutService: LayoutService
22-
) {
23-
this.isView = this.browserService.isBrowser
24-
this.isSidebarOpen$ = this.layoutService.isSidebarOpen$
25-
}
17+
public readonly layoutService: LayoutService,
18+
public readonly browserService: BrowserService
19+
) {}
2620

2721
@HostListener('window:resize')
2822
public onResize() {

client/app/components/footer/footer.component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class FooterComponent implements OnInit {
2828
) {}
2929

3030
public ngOnInit() {
31-
if (!this.browserService.isBrowser) return
3231
this.apiService
3332
.getHitokoto()
3433
.pipe(
@@ -41,10 +40,11 @@ export class FooterComponent implements OnInit {
4140
this.data = data
4241
})
4342

44-
if (!this.browserService.isBrowser) return
45-
setInterval(() => {
46-
this.currentTime = this.getTimeString()
47-
}, 1000)
43+
this.browserService.on(() =>
44+
setInterval(() => {
45+
this.currentTime = this.getTimeString()
46+
}, 1000)
47+
)
4848
}
4949

5050
public getTimeString() {

client/app/components/layout/layout.component.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,26 @@ export class LayoutComponent implements OnInit, OnDestroy {
3333

3434
public constructor(
3535
private readonly router: Router,
36-
private readonly browserService: BrowserService,
3736
private readonly storeService: StoreService,
37+
private readonly browserService: BrowserService,
3838
public readonly layoutService: LayoutService
39-
) {
40-
// router.events.pipe(filter((e) => e instanceof NavigationEnd)).subscribe(() => {
41-
// const route = this.getActiveRoute()
42-
// // biome-ignore lint: *
43-
// const layoutData: LayoutConfig = route.snapshot.data['layout']
44-
// if (layoutData) this.layoutService.updateHeader(layoutData)
45-
// })
46-
}
39+
) {}
4740

4841
public get headerImageHeight() {
4942
return this.imageHeight ? this.imageHeight : this.fullBackground ? 'min-h-screen' : 'h-350px'
5043
}
5144

5245
public ngOnInit() {
5346
this.router.events.subscribe((event) => this.handleRouteEvent(event))
54-
if (!this.browserService.isBrowser) return
5547

56-
window.addEventListener('scroll', () => {
57-
this.showBackTop = window.scrollY > 100
48+
this.browserService.on(() => {
49+
window.addEventListener('scroll', () => {
50+
this.showBackTop = window.scrollY > 100
51+
})
52+
this.togglePlayer(true)
5853
})
59-
this.togglePlayer(true)
6054
}
6155

62-
// private getActiveRoute(): ActivatedRoute {
63-
// let route = inject(ActivatedRoute)
64-
// while (route.firstChild) route = route.firstChild
65-
// return route
66-
// }
67-
6856
private handleRouteEvent(event: object) {
6957
if (event instanceof NavigationStart) {
7058
this.isLoading = true
@@ -81,10 +69,6 @@ export class LayoutComponent implements OnInit, OnDestroy {
8169
})
8270
}, 100)
8371
}
84-
85-
// if (event instanceof NavigationEnd || event instanceof NavigationCancel || event instanceof NavigationError) {
86-
// this.isLoading = false
87-
// }
8872
}
8973

9074
public scrollToTop() {

client/app/components/post-content/post-content.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<div class="mt-8 mb-6">
9393
<h3 class="text-2xl font-bold mb-6 flex items-center gap-2">评论区</h3>
9494

95-
@if (browserService.isBrowser) {
95+
@if (browserService.is) {
9696
@if (authService.user$(); as user) {
9797
<div class="mb-8">
9898
@if (replyingTo) {

client/app/components/post-content/post-content.component.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ export class PostContentComponent implements OnInit, OnDestroy {
8888
this.layoutService.setTitle(this.post.title)
8989
this.renderContent().then(() => this.viewPost())
9090

91-
if (!this.browserService.isBrowser || this.hideComments) {
91+
if (this.hideComments) {
9292
this.comments = []
93-
return
93+
} else {
94+
this.apiService
95+
.getCommentsByPost(this.post.id)
96+
.pipe(takeUntil(this.destroy$))
97+
.subscribe((comments) => {
98+
this.comments = this.parseComments(comments)
99+
})
94100
}
95-
this.apiService
96-
.getCommentsByPost(this.post.id)
97-
.pipe(takeUntil(this.destroy$))
98-
.subscribe((comments) => {
99-
this.comments = this.parseComments(comments)
100-
})
101101
}
102102

103103
public ngOnDestroy() {
@@ -268,9 +268,7 @@ export class PostContentComponent implements OnInit, OnDestroy {
268268
if (!this.highlighter) this.highlighter = await this.highlighterService.getHighlighter(this.post.languages)
269269

270270
this.extra = {
271-
url: ((ref) => (ref ? `${ref.location.origin}${this.router.url.split('#')[0]}` : ''))(
272-
this.browserService.windowRef
273-
),
271+
url: this.browserService.on(() => `${location.origin}${this.router.url.split('#')[0]}`) ?? '',
274272
tags: this.post.tags.map((tag) => [tag, randomRTagType()])
275273
}
276274

client/app/components/progress/progress.component.ts

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { BrowserService } from '../../services/browser.service'
1010
export class ProgressComponent implements OnInit, AfterViewInit {
1111
public constructor(
1212
private readonly router: Router,
13-
private readonly browserService: BrowserService
13+
private browserService: BrowserService
1414
) {}
1515

1616
public ngOnInit() {
@@ -29,10 +29,9 @@ export class ProgressComponent implements OnInit, AfterViewInit {
2929
}
3030

3131
private startProgress() {
32-
if (!this.browserService.isBrowser) return
33-
34-
const progressBar = document.getElementById('progress-bar')
35-
if (progressBar) {
32+
this.browserService.on(() => {
33+
const progressBar = document.getElementById('progress-bar')
34+
if (!progressBar) return
3635
progressBar.style.transition = 'none'
3736
progressBar.style.width = '90%'
3837
setTimeout(() => {
@@ -43,14 +42,13 @@ export class ProgressComponent implements OnInit, AfterViewInit {
4342
setTimeout(() => {
4443
progressBar.style.width = '0%'
4544
}, 500)
46-
}
45+
})
4746
}
4847

4948
private endProgress() {
50-
if (!this.browserService.isBrowser) return
51-
52-
const progressBar = window.document.getElementById('progress-bar')
53-
if (progressBar) {
49+
this.browserService.on(() => {
50+
const progressBar = document.getElementById('progress-bar')
51+
if (!progressBar) return
5452
progressBar.style.width = '0%'
5553
setTimeout(() => {
5654
progressBar.style.width = '100%'
@@ -59,21 +57,21 @@ export class ProgressComponent implements OnInit, AfterViewInit {
5957
progressBar.style.transition = ''
6058
progressBar.style.width = '0%'
6159
}, 1100)
62-
}
60+
})
6361
}
6462

6563
private initProgress() {
66-
if (!this.browserService.isBrowser) return
67-
68-
const progressBar = document.createElement('div')
69-
progressBar.id = 'progress-bar'
70-
progressBar.style.position = 'fixed'
71-
progressBar.style.top = '0'
72-
progressBar.style.left = '0'
73-
progressBar.style.height = '4px'
74-
progressBar.style.backgroundColor = 'var(--primary-100)'
75-
progressBar.style.width = '100%'
76-
progressBar.style.zIndex = '9999'
77-
document.body.appendChild(progressBar)
64+
this.browserService.on(() => {
65+
const progressBar = document.createElement('div')
66+
progressBar.id = 'progress-bar'
67+
progressBar.style.position = 'fixed'
68+
progressBar.style.top = '0'
69+
progressBar.style.left = '0'
70+
progressBar.style.height = '4px'
71+
progressBar.style.backgroundColor = 'var(--primary-100)'
72+
progressBar.style.width = '100%'
73+
progressBar.style.zIndex = '9999'
74+
document.body.appendChild(progressBar)
75+
})
7876
}
7977
}

client/app/guards/auth.guard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class AuthGuard implements CanActivate {
1414
) {}
1515

1616
public canActivate() {
17-
if (!this.browserService.isBrowser) return true
17+
if (!this.browserService.is) return true
1818
if (this.authService.isLoggedIn()) return true
1919
if (this.router.url.includes('/admin/login')) return true
2020
this.router.navigate(['/admin/login'])

0 commit comments

Comments
 (0)