Skip to content

Commit 0655760

Browse files
committed
Refactor navigation handling and settings access across components
- Updated SideComponent to use `navs()` instead of `websiteList` for navigation items. - Modified SimComponent to access settings via `settings()` for dynamic updates. - Refactored SuperComponent to utilize `navs()` for navigation and settings retrieval. - Adjusted SystemBookmarkExportComponent and SystemBookmarkComponent to replace `websiteList` with `navs()`. - Changed CollectComponent to use `navs()` and updated tagMap initialization. - Updated SystemComponent to access settings and components dynamically. - Refactored SystemSearchComponent to use `search()` for search list retrieval. - Modified SystemSettingComponent to access settings and components dynamically. - Updated SystemTagComponent to use `tagList()` for tag management. - Refactored WebComponent to utilize `navs()` for navigation and settings handling. - Ensured consistent use of dynamic settings retrieval across all components.
1 parent 4f427d6 commit 0655760

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+523
-511
lines changed

scripts/start.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dayjs.extend(utc)
3636
dayjs.extend(timezone)
3737
dayjs.tz.setDefault('Asia/Shanghai')
3838

39-
const getWebs = (): INavProps[] => {
39+
const getNavs = (): INavProps[] => {
4040
try {
4141
const strings = fs.readFileSync(PATHS.db).toString().trim()
4242
if (!strings) throw new Error('empty')
@@ -59,7 +59,7 @@ const main = async () => {
5959
const configJson = getConfig()
6060
fs.writeFileSync(PATHS.configJson, JSON.stringify(configJson))
6161

62-
const db = getWebs()
62+
const db = getNavs()
6363
let internal = {} as InternalProps
6464
let settings = {} as ISettings
6565
let tags: ITagPropValues[] = []

scripts/utils.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ interface WebCountResult {
9090
}
9191

9292
// 统计网站数量
93-
export function getWebCount(websiteList: INavProps[]): WebCountResult {
93+
export function getWebCount(navs: INavProps[]): WebCountResult {
9494
// 用户查看所有数量
9595
let userViewCount = 0
9696
// 登陆者统计所有数量
@@ -130,8 +130,8 @@ export function getWebCount(websiteList: INavProps[]): WebCountResult {
130130
}
131131
}
132132

133-
r(websiteList)
134-
r2(websiteList)
133+
r(navs)
134+
r2(navs)
135135

136136
return {
137137
userViewCount: userViewCount - diffCount,
@@ -434,13 +434,10 @@ export function writeTemplate({
434434
/(<!-- nav\.config-start -->)(.|\s)*?(<!-- nav.config-end -->)/i,
435435
`$1${htmlTemplate}$3`,
436436
)
437-
if (settings.headerContent) {
438-
t = t.replace(
439-
/(<!-- nav.headerContent-start -->)(.|\s)*?(<!-- nav.headerContent-end -->)/i,
440-
`$1${settings.headerContent}$3`,
441-
)
442-
}
443-
437+
t = t.replace(
438+
/(<!-- nav.headerContent-start -->)(.|\s)*?(<!-- nav.headerContent-end -->)/i,
439+
`$1${settings.headerContent}$3`,
440+
)
444441
t = t.replace(
445442
/(<!-- nav.seo-start -->)(.|\s)*?(<!-- nav.seo-end -->)/i,
446443
`$1${seoTemplate}$3`,

src/api/index.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,7 @@ import http, {
1212
} from '../utils/http'
1313
import qs from 'qs'
1414
import { encode } from 'js-base64'
15-
import {
16-
settings,
17-
websiteList,
18-
tagList,
19-
getTagMap,
20-
search,
21-
internal,
22-
component,
23-
} from 'src/store'
24-
import type { ISettings } from 'src/types'
15+
import { settings, navs, tagList, search, internal, component } from 'src/store'
2516
import { isSelfDevelop } from 'src/utils/utils'
2617
import { isLogin, getImageToken } from 'src/utils/user'
2718
import { DB_PATH } from 'src/constants'
@@ -102,27 +93,12 @@ export function getContentes() {
10293
return http
10394
.post('/api/contents/get', getDefaultRequestData())
10495
.then((res: any) => {
105-
websiteList.splice(0, websiteList.length)
106-
tagList.splice(0, tagList.length)
107-
108-
internal.loginViewCount = res.data.internal.loginViewCount
109-
internal.userViewCount = res.data.internal.userViewCount
110-
websiteList.push(...res.data.webs)
111-
tagList.push(...res.data.tags)
112-
const resSettings = res.data.settings as ISettings
113-
for (const k in resSettings) {
114-
// @ts-ignore
115-
settings[k] = resSettings[k]
116-
}
117-
for (const k in res.data.component) {
118-
// @ts-ignore
119-
component[k] = res.data.component[k]
120-
}
121-
for (const k in res.data.search) {
122-
// @ts-ignore
123-
search[k] = res.data.search[k]
124-
}
125-
getTagMap()
96+
internal.set(res.data.internal)
97+
navs.set(res.data.webs)
98+
tagList.set(res.data.tags)
99+
settings.set(res.data.settings)
100+
component.set(res.data.component)
101+
search.set(res.data.search)
126102
event.emit('WEB_REFRESH')
127103
return res
128104
})
@@ -429,11 +405,11 @@ export function getCDN(path: string) {
429405
} else if (_isGItLab) {
430406
return `https://gitlab.com/${owner}/${repo}/-/raw/${branch}/${path}?ref_type=heads`
431407
}
432-
return `https://${settings.gitHubCDN}/gh/${owner}/${repo}@${branch}/${path}`
408+
return `https://${settings().gitHubCDN}/gh/${owner}/${repo}@${branch}/${path}`
433409
}
434410

435411
function requestActionUrl() {
436-
const url = settings.actionUrl
412+
const url = settings().actionUrl
437413
if (url) {
438414
const img = document.createElement('img')
439415
img.src = url

src/app/app.component.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { NzMessageService } from 'ng-zorro-antd/message'
2525
import { NzNotificationService } from 'ng-zorro-antd/notification'
2626
import { NzSpinModule } from 'ng-zorro-antd/spin'
2727
import { NzModalService } from 'ng-zorro-antd/modal'
28-
import { getWebs } from 'src/utils/web'
28+
import { getNavs } from 'src/utils/web'
2929
import { isSelfDevelop } from 'src/utils/utils'
3030
import { routes } from './app.routes'
3131
import { MoveWebComponent } from 'src/components/move-web/index.component'
@@ -96,21 +96,22 @@ export class AppComponent {
9696

9797
private updateDocumentTitle() {
9898
const url = this.router.url.split('?')[0].slice(1)
99-
const theme = (url === '' ? settings.theme : url).toLowerCase()
100-
const title = settings[`${theme}DocTitle`]
101-
document.title = title || window.__TITLE__ || settings.title
99+
const theme = (url === '' ? settings().theme : url).toLowerCase()
100+
const title = settings()[`${theme}DocTitle`]
101+
document.title = title || window.__TITLE__ || settings().title
102102
}
103103

104104
ngOnInit() {
105105
this.goRoute()
106106
this.activatedRoute.queryParams.subscribe(setLocation)
107107
this.setLocale()
108108
this.verifyToken()
109-
this.getWebs()
109+
this.getNavs()
110110
this.getCollectCount()
111111
}
112112

113-
private getWebs() {
113+
private getNavs() {
114+
const { href } = location
114115
if (isSelfDevelop) {
115116
getContentes().then(() => {
116117
setTimeout(() => {
@@ -129,7 +130,7 @@ export class AppComponent {
129130
},
130131
])
131132
}
132-
if (isHome && !location.href.includes('system')) {
133+
if (isHome && !href.includes('/system')) {
133134
this.router.navigate([defaultTheme])
134135
}
135136
this.updateDocumentTitle()
@@ -139,7 +140,7 @@ export class AppComponent {
139140
}, 100)
140141
})
141142
} else {
142-
getWebs().finally(() => {
143+
getNavs().finally(() => {
143144
this.fetchIng = false
144145
})
145146
}
@@ -160,15 +161,19 @@ export class AppComponent {
160161
.then((res) => {
161162
const data = res.data || {}
162163
if (!isSelfDevelop) {
163-
if (data.login && data.login !== authorName) {
164+
if ((data.login || data.username) !== authorName) {
164165
throw new Error('Bad credentials')
165166
}
166-
if (!settings.email && data.email) {
167-
settings.email = data.email
167+
if (!settings().email && data.email) {
168+
settings.update((prev) => {
169+
return {
170+
...prev,
171+
email: data.email,
172+
}
173+
})
174+
event.emit('GITHUB_USER_INFO', data)
168175
}
169176
}
170-
171-
event.emit('GITHUB_USER_INFO', data)
172177
})
173178
.catch((e: any) => {
174179
if (e.code !== 'ERR_NETWORK') {
@@ -182,7 +187,7 @@ export class AppComponent {
182187
}
183188

184189
private getCollectCount() {
185-
if (isLogin && getAuthCode() && getPermissions(settings).ok) {
190+
if (isLogin && getAuthCode() && getPermissions(settings()).ok) {
186191
getUserCollectCount().then((res) => {
187192
const count = res.data.count
188193
if (count > 0) {
@@ -200,15 +205,16 @@ export class AppComponent {
200205

201206
private goRoute() {
202207
// is App
203-
if (settings.appTheme !== 'Current' && isMobile()) {
208+
const { appTheme } = settings()
209+
if (appTheme !== 'Current' && isMobile()) {
204210
if (location.href.includes('system')) {
205211
return
206212
}
207213

208214
const url = (this.router.url.split('?')[0] || '').toLowerCase()
209215
const { id, q } = queryString()
210216
const queryParams = { id, q }
211-
const path = '/' + String(settings.appTheme).toLowerCase()
217+
const path = '/' + String(appTheme).toLowerCase()
212218

213219
if (!url.includes(path)) {
214220
this.router.navigate([path], { queryParams })
@@ -218,7 +224,7 @@ export class AppComponent {
218224

219225
private registerKeyboard() {
220226
document.addEventListener('keyup', (e) => {
221-
const createWebKey = settings.createWebKey.toLowerCase()
227+
const createWebKey = settings().createWebKey.toLowerCase()
222228
if (!createWebKey) {
223229
return
224230
}

src/components/calendar/index.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { component } from 'src/store'
1717
export class CalendarComponent {
1818
@Input() data!: IComponentItemProps
1919

20-
readonly component = component
20+
readonly component = component()
2121
date = ''
2222
day = ''
2323
week = ''

src/components/card/index.component.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import { CommonModule } from '@angular/common'
88
import { isLogin, getPermissions } from 'src/utils/user'
99
import { copyText, getTextContent, randomColor, randomInt } from 'src/utils'
1010
import { parseHtmlWithContent, parseLoadingWithContent } from 'src/utils/utils'
11-
import { setWebsiteList } from 'src/utils/web'
12-
import type { INavProps, IWebProps, ICardType } from 'src/types'
11+
import { setNavs } from 'src/utils/web'
12+
import type { IWebProps, ICardType } from 'src/types'
1313
import { ActionType } from 'src/types'
1414
import { SearchType } from 'src/components/search/index'
1515
import { $t, isZhCN } from 'src/locale'
16-
import { settings, websiteList } from 'src/store'
16+
import { settings, navs } from 'src/store'
1717
import { JumpService } from 'src/services/jump'
1818
import { NzRateModule } from 'ng-zorro-antd/rate'
1919
import { LogoComponent } from 'src/components/logo/logo.component'
@@ -56,10 +56,9 @@ export class CardComponent {
5656
@ViewChild('root', { static: false }) root!: ElementRef
5757

5858
readonly $t = $t
59-
readonly settings = settings
60-
readonly websiteList: INavProps[] = websiteList
59+
readonly settings = settings()
6160
readonly isLogin = isLogin
62-
readonly permissions = getPermissions(settings)
61+
readonly permissions = getPermissions(settings())
6362
copyUrlDone = false
6463
copyPathDone = false
6564
description = ''
@@ -125,7 +124,7 @@ export class CardComponent {
125124

126125
onRateChange(rate: number): void {
127126
this.dataSource.rate = rate
128-
setWebsiteList(this.websiteList)
127+
setNavs(navs())
129128
}
130129

131130
async confirmDel(): Promise<void> {

src/components/component-group/index.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class ComponentGroupComponent {
4747
readonly isMobile = isMobile()
4848
ComponentType = ComponentType
4949
components: IComponentItemProps[] = []
50-
componentsLength: number = settings.components.length
50+
componentsLength: number = settings().components.length
5151
widths: number[] = []
5252
isShowAll = !!Number(
5353
localStorage.getItem(STORAGE_KEY_MAP.COMPONENT_COLLAPSED),
@@ -61,8 +61,8 @@ export class ComponentGroupComponent {
6161

6262
const c: IComponentItemProps[] = []
6363
// 按照系统设置顺序排序显示
64-
component.components.forEach((item) => {
65-
const has = settings.components.find(
64+
component().components.forEach((item) => {
65+
const has = settings().components.find(
6666
(c) => c.type === item.type && c.id === item.id,
6767
)
6868
if (has) {

src/components/countdown/index.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IProps {
2121
export class CountdownComponent {
2222
@Input() data!: IComponentItemProps
2323

24-
readonly component = component
24+
readonly component = component()
2525
countdownData = {} as IProps
2626

2727
constructor() {}

src/components/create-web/index.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204
(ngModelChange)="onSelectChange(idx)"
205205
>
206206
<nz-option
207-
*ngFor="let tag of tagList"
207+
*ngFor="let tag of tagList()"
208208
[nzLabel]="tag.name"
209209
[nzValue]="tag.id"
210210
>
@@ -225,7 +225,7 @@
225225
</nz-form-item>
226226
</div>
227227

228-
<nz-form-item *ngIf="tagList.length > 0">
228+
<nz-form-item *ngIf="tagList().length > 0">
229229
<nz-form-label [nzSpan]="4" [nzNoColon]="true"></nz-form-label>
230230
<nz-form-control [nzSpan]="20">
231231
<button nz-button nzType="default" nzBlock (click)="addMoreUrl()">

0 commit comments

Comments
 (0)