feat(core): add SystemFontFamilyService

This commit is contained in:
Jimmfly
2024-08-19 22:28:05 +08:00
committed by EYHN
parent 9a4cb5d69a
commit d28a6300d9
4 changed files with 97 additions and 0 deletions
@@ -16,6 +16,7 @@ import { configurePermissionsModule } from './permissions';
import { configureWorkspacePropertiesModule } from './properties';
import { configureQuickSearchModule } from './quicksearch';
import { configureShareDocsModule } from './share-doc';
import { configureSystemFontFamilyModule } from './system-font-family';
import { configureTagModule } from './tag';
import { configureTelemetryModule } from './telemetry';
import { configureThemeEditorModule } from './theme-editor';
@@ -41,4 +42,5 @@ export function configureCommonModules(framework: Framework) {
configureExplorerModule(framework);
configureThemeEditorModule(framework);
configureEditorModule(framework);
configureSystemFontFamilyModule(framework);
}
@@ -0,0 +1,79 @@
import { DebugLogger } from '@affine/debug';
import { apis } from '@affine/electron-api';
import { Entity, LiveData } from '@toeverything/infra';
import {
debounceTime,
distinctUntilChanged,
of,
shareReplay,
switchMap,
tap,
} from 'rxjs';
const logger = new DebugLogger('affine:system-font-family');
export class SystemFontFamily extends Entity {
constructor() {
super();
this.loadFontList().catch(error => {
logger.error('Failed to load system font list', error);
});
}
readonly searchText$ = new LiveData<string | null>(null);
readonly isLoading$ = new LiveData<boolean>(false);
readonly fontList$ = new LiveData<string[]>([]);
readonly result$ = LiveData.from(
this.searchText$.pipe(
distinctUntilChanged(),
debounceTime(500),
switchMap(searchText => {
if (!searchText) {
return of([]);
}
return this.fontList$.pipe(
tap(() => {
this.isLoading$.next(true);
}),
switchMap(fontList => {
const filteredFonts = fontList.filter(font =>
font.toLowerCase().includes(searchText.toLowerCase())
);
this.isLoading$.next(false);
return of(filteredFonts);
})
);
}),
shareReplay({
bufferSize: 1,
refCount: true,
})
),
[]
);
async loadFontList() {
if (!apis?.fontList) {
return;
}
try {
this.isLoading$.next(true);
const fontList = await apis.fontList.getSystemFonts();
this.fontList$.next(fontList);
} catch (error) {
logger.error('Failed to load system font list', error);
} finally {
this.isLoading$.next(false);
}
}
search(searchText: string) {
if (!this.searchText$.value) return;
this.searchText$.next(searchText);
}
clearSearch() {
if (!this.searchText$.value) return;
this.searchText$.next(null);
}
}
@@ -0,0 +1,8 @@
import type { Framework } from '@toeverything/infra';
import { SystemFontFamily } from './entities/system-font-family';
import { SystemFontFamilyService } from './services/system-font-family';
export function configureSystemFontFamilyModule(framework: Framework) {
framework.service(SystemFontFamilyService).entity(SystemFontFamily);
}
@@ -0,0 +1,8 @@
import { Service } from '@toeverything/infra';
import { SystemFontFamily } from '../entities/system-font-family';
export class SystemFontFamilyService extends Service {
public readonly systemFontFamily =
this.framework.createEntity(SystemFontFamily);
}