refactor(frontend): rename booklore-ui to frontend
- Frontend path: - rename the tracked Angular app tree from `booklore-ui/` to `frontend/` - preserve file contents during the move so this commit stays focused on the path change itself - Frontend docs: - update the moved frontend README and contribution guide where they refer to the renamed folder - keep those edits limited to path references introduced by the directory rename
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {Subject} from 'rxjs';
|
||||
import {TranslocoService} from '@jsverse/transloco';
|
||||
import {BookPatchService} from '../../../book/service/book-patch.service';
|
||||
import {ReadingSessionService} from '../../../../shared/service/reading-session.service';
|
||||
import {PageInfo, ThemeInfo} from '../core/view-manager.service';
|
||||
import {ReaderViewManagerService} from '../core/view-manager.service';
|
||||
import {ReaderStateService} from './reader-state.service';
|
||||
import {ReaderAnnotationHttpService} from '../features/annotations/annotation.service';
|
||||
import {ReaderBookmarkService} from '../features/bookmarks/bookmark.service';
|
||||
import {BookType} from '../../../book/model/book.model';
|
||||
|
||||
export interface ProgressState {
|
||||
cfi: string | null;
|
||||
href: string | null;
|
||||
chapterName: string | null;
|
||||
chapterHref: string | null;
|
||||
fraction: number;
|
||||
pageInfo: PageInfo | undefined;
|
||||
progressData: any;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class ReaderProgressService {
|
||||
private bookPatchService = inject(BookPatchService);
|
||||
private readingSessionService = inject(ReadingSessionService);
|
||||
private readonly t = inject(TranslocoService);
|
||||
private viewManager = inject(ReaderViewManagerService);
|
||||
private stateService = inject(ReaderStateService);
|
||||
private annotationService = inject(ReaderAnnotationHttpService);
|
||||
private bookmarkService = inject(ReaderBookmarkService);
|
||||
|
||||
private bookId!: number;
|
||||
private bookType!: BookType;
|
||||
private bookFileId?: number;
|
||||
private hasStartedSession = false;
|
||||
|
||||
private _currentCfi: string | null = null;
|
||||
private _currentChapterName: string | null = null;
|
||||
private _currentChapterHref: string | null = null;
|
||||
private _currentPageInfo: PageInfo | undefined;
|
||||
private _currentProgressData: any = null;
|
||||
|
||||
private progressSubject = new Subject<ProgressState>();
|
||||
public progress$ = this.progressSubject.asObservable();
|
||||
|
||||
get currentCfi(): string | null {
|
||||
return this._currentCfi;
|
||||
}
|
||||
|
||||
get currentChapterName(): string | null {
|
||||
return this._currentChapterName;
|
||||
}
|
||||
|
||||
get currentChapterHref(): string | null {
|
||||
return this._currentChapterHref;
|
||||
}
|
||||
|
||||
get currentProgressData(): any {
|
||||
return this._currentProgressData;
|
||||
}
|
||||
|
||||
get currentPageInfo(): PageInfo | undefined {
|
||||
return this._currentPageInfo;
|
||||
}
|
||||
|
||||
initialize(bookId: number, bookType: BookType, bookFileId?: number): void {
|
||||
this.bookId = bookId;
|
||||
this.bookType = bookType;
|
||||
this.bookFileId = bookFileId;
|
||||
this.hasStartedSession = false;
|
||||
}
|
||||
|
||||
handleRelocateEvent(detail: any): void {
|
||||
this._currentProgressData = detail;
|
||||
|
||||
const cfi = detail?.cfi ?? null;
|
||||
const href = detail?.pageItem?.href ?? detail?.tocItem?.href ?? null;
|
||||
const percentage = typeof detail?.fraction === 'number' ? detail.fraction * 100 : null;
|
||||
|
||||
if (!this.hasStartedSession && cfi && percentage !== null) {
|
||||
this.hasStartedSession = true;
|
||||
this.readingSessionService.startSession(this.bookId, this.bookType, cfi, percentage);
|
||||
}
|
||||
|
||||
if (cfi && percentage !== null) {
|
||||
this.bookPatchService.saveEpubProgress(this.bookId, cfi, href, percentage, this.bookFileId);
|
||||
this.readingSessionService.updateProgress(cfi, percentage);
|
||||
}
|
||||
|
||||
const chapterLabel = detail?.tocItem?.label;
|
||||
if (chapterLabel && chapterLabel !== this._currentChapterName) {
|
||||
this._currentChapterName = chapterLabel;
|
||||
this.annotationService.updateCurrentChapter(chapterLabel);
|
||||
}
|
||||
|
||||
if (href && href !== this._currentChapterHref) {
|
||||
this._currentChapterHref = href;
|
||||
}
|
||||
|
||||
if (detail?.section) {
|
||||
const percentCompleted = Math.round((detail.fraction * 100) * 10) / 10;
|
||||
const totalMinutes = detail.time?.section ?? 0;
|
||||
|
||||
const hours = Math.floor(totalMinutes / 60);
|
||||
const minutes = Math.floor(totalMinutes % 60);
|
||||
const seconds = Math.round((totalMinutes - Math.floor(totalMinutes)) * 60);
|
||||
|
||||
const parts: string[] = [];
|
||||
if (hours) parts.push(`${hours}h`);
|
||||
if (minutes) parts.push(`${minutes}m`);
|
||||
if (seconds || parts.length === 0) parts.push(`${seconds}s`);
|
||||
|
||||
this._currentPageInfo = {
|
||||
percentCompleted,
|
||||
sectionTimeText: parts.join(' ')
|
||||
};
|
||||
}
|
||||
|
||||
if (this.stateService.state().flow === 'paginated') {
|
||||
this.updateHeadersAndFooters();
|
||||
}
|
||||
|
||||
if (cfi) {
|
||||
this._currentCfi = cfi;
|
||||
this.bookmarkService.updateCurrentPosition(cfi, chapterLabel);
|
||||
}
|
||||
|
||||
this.progressSubject.next({
|
||||
cfi: this._currentCfi,
|
||||
href,
|
||||
chapterName: this._currentChapterName,
|
||||
chapterHref: this._currentChapterHref,
|
||||
fraction: detail?.fraction ?? 0,
|
||||
pageInfo: this._currentPageInfo,
|
||||
progressData: this._currentProgressData
|
||||
});
|
||||
}
|
||||
|
||||
updateHeadersAndFooters(): void {
|
||||
const renderer = this.viewManager.getRenderer();
|
||||
if (!renderer) return;
|
||||
|
||||
const readerState = this.stateService.state();
|
||||
const theme: ThemeInfo = {
|
||||
fg: readerState.theme.fg || readerState.theme.light.fg,
|
||||
bg: readerState.theme.bg || readerState.theme.light.bg
|
||||
};
|
||||
|
||||
const timeLabel = this.t.translate('readerEbook.headerFooterUtil.timeRemainingInSection', {
|
||||
time: this._currentPageInfo?.sectionTimeText ?? '0s'
|
||||
});
|
||||
|
||||
this.viewManager.updateHeadersAndFooters(
|
||||
this._currentChapterName || '',
|
||||
this._currentPageInfo,
|
||||
theme,
|
||||
timeLabel
|
||||
);
|
||||
}
|
||||
|
||||
endSession(): void {
|
||||
if (this.readingSessionService.isSessionActive()) {
|
||||
const progress = typeof this._currentProgressData?.fraction === 'number'
|
||||
? Math.round(this._currentProgressData.fraction * 100 * 100) / 100
|
||||
: undefined;
|
||||
this.readingSessionService.endSession(this._currentCfi || undefined, progress);
|
||||
}
|
||||
}
|
||||
|
||||
reset(): void {
|
||||
this._currentCfi = null;
|
||||
this._currentChapterName = null;
|
||||
this._currentChapterHref = null;
|
||||
this._currentPageInfo = undefined;
|
||||
this._currentProgressData = null;
|
||||
this.hasStartedSession = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
import {inject, Injectable, signal} from '@angular/core';
|
||||
import {forkJoin, Observable} from 'rxjs';
|
||||
import {map, tap} from 'rxjs/operators';
|
||||
import {Theme, themes} from './themes.constant';
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {EpubCustomFontService} from '../features/fonts/custom-font.service';
|
||||
|
||||
export interface ReaderState {
|
||||
lineHeight: number;
|
||||
justify: boolean;
|
||||
hyphenate: boolean;
|
||||
maxColumnCount: number;
|
||||
gap: number;
|
||||
fontSize: number;
|
||||
theme: Theme;
|
||||
maxInlineSize: number;
|
||||
maxBlockSize: number;
|
||||
fontFamily: string | null;
|
||||
isDark: boolean;
|
||||
flow: 'paginated' | 'scrolled';
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ReaderStateService {
|
||||
private epubCustomFontService = inject(EpubCustomFontService);
|
||||
|
||||
private readonly BASE_FONTS = [
|
||||
{name: 'Publisher\'s', value: null},
|
||||
{name: 'Serif', value: 'serif'},
|
||||
{name: 'Sans-Serif', value: 'sans-serif'},
|
||||
{name: 'Monospace', value: 'monospace'},
|
||||
{name: 'Cursive', value: 'cursive'},
|
||||
];
|
||||
|
||||
private readonly defaultState: ReaderState = {
|
||||
lineHeight: 1.5,
|
||||
justify: true,
|
||||
hyphenate: true,
|
||||
maxColumnCount: 2,
|
||||
gap: 0.05,
|
||||
fontSize: 16,
|
||||
theme: {
|
||||
...themes[0],
|
||||
fg: themes[0].dark.fg,
|
||||
bg: themes[0].dark.bg,
|
||||
link: themes[0].dark.link,
|
||||
},
|
||||
maxInlineSize: 720,
|
||||
maxBlockSize: 1440,
|
||||
fontFamily: null,
|
||||
isDark: true,
|
||||
flow: 'paginated',
|
||||
};
|
||||
|
||||
private readonly _state = signal<ReaderState>(this.defaultState);
|
||||
readonly state = this._state.asReadonly();
|
||||
|
||||
readonly themes = themes;
|
||||
private readonly _fonts = signal<Array<{ name: string; value: string | null }>>(this.BASE_FONTS);
|
||||
readonly fonts = this._fonts.asReadonly();
|
||||
|
||||
constructor(private bookService: BookService, private userService: UserService) {
|
||||
this.loadCustomFontsIntoList();
|
||||
}
|
||||
|
||||
private loadCustomFontsIntoList(): void {
|
||||
const customFonts = this.epubCustomFontService.getCustomFonts();
|
||||
const customFontOptions = customFonts.map(font => ({
|
||||
name: font.fontName.replace(/\.(ttf|otf|woff|woff2)$/i, ''),
|
||||
value: `custom:${font.id}`
|
||||
}));
|
||||
|
||||
const updatedFonts = [
|
||||
...this._fonts(),
|
||||
...customFontOptions
|
||||
];
|
||||
|
||||
this._fonts.set(updatedFonts);
|
||||
}
|
||||
|
||||
refreshCustomFonts(): void {
|
||||
this._fonts.set([...this.BASE_FONTS]);
|
||||
this.loadCustomFontsIntoList();
|
||||
}
|
||||
|
||||
initializeState(bookId: number, bookFileId: number): Observable<void> {
|
||||
return forkJoin([
|
||||
this.userService.getMyself(),
|
||||
this.bookService.getBookSetting(bookId, bookFileId)
|
||||
]).pipe(
|
||||
tap(([myself, bookSetting]) => {
|
||||
const settingScope = myself.userSettings.perBookSetting.epub;
|
||||
const globalSettings = myself.userSettings.ebookReaderSetting;
|
||||
const individualSetting = bookSetting?.ebookSettings;
|
||||
const settings = settingScope === 'Global' ? globalSettings : (individualSetting || globalSettings);
|
||||
const newState: Partial<ReaderState> = {};
|
||||
if (settings.fontSize != null) newState.fontSize = settings.fontSize;
|
||||
if (settings.lineHeight != null) newState.lineHeight = settings.lineHeight;
|
||||
|
||||
if (settings.fontFamily != null) {
|
||||
if (settings.fontFamily.startsWith('custom:')) {
|
||||
newState.fontFamily = settings.fontFamily;
|
||||
} else {
|
||||
const numericId = parseInt(settings.fontFamily, 10);
|
||||
if (!isNaN(numericId) && numericId.toString() === settings.fontFamily) {
|
||||
newState.fontFamily = `custom:${numericId}`;
|
||||
} else {
|
||||
newState.fontFamily = settings.fontFamily;
|
||||
}
|
||||
}
|
||||
} else if ((settings as any).customFontId != null) {
|
||||
newState.fontFamily = `custom:${(settings as any).customFontId}`;
|
||||
}
|
||||
|
||||
if (settings.gap != null) newState.gap = Math.min(settings.gap, 0.5);
|
||||
if (settings.hyphenate != null) newState.hyphenate = settings.hyphenate;
|
||||
if (settings.justify != null) newState.justify = settings.justify;
|
||||
if (settings.maxColumnCount != null) newState.maxColumnCount = settings.maxColumnCount;
|
||||
if (settings.maxInlineSize != null) newState.maxInlineSize = settings.maxInlineSize;
|
||||
if (settings.maxBlockSize != null) newState.maxBlockSize = settings.maxBlockSize;
|
||||
if (settings.isDark != null) newState.isDark = settings.isDark;
|
||||
if (settings.flow) newState.flow = settings.flow;
|
||||
if (settings.theme) {
|
||||
const theme = this.themes.find(t => t.name === settings.theme);
|
||||
if (theme) {
|
||||
newState.theme = {
|
||||
...theme,
|
||||
fg: settings.isDark ? theme.dark.fg : theme.light.fg,
|
||||
bg: settings.isDark ? theme.dark.bg : theme.light.bg,
|
||||
link: settings.isDark ? theme.dark.link : theme.light.link,
|
||||
};
|
||||
}
|
||||
}
|
||||
if (Object.keys(newState).length > 0) {
|
||||
this.updateState(newState);
|
||||
}
|
||||
}),
|
||||
map(() => void 0)
|
||||
);
|
||||
}
|
||||
|
||||
updateLineHeight(delta: number): void {
|
||||
const current = this._state().lineHeight;
|
||||
const newValue = Math.max(0.8, Math.min(3, current + delta));
|
||||
this.updateState({lineHeight: newValue});
|
||||
}
|
||||
|
||||
updateMaxColumnCount(delta: number): void {
|
||||
const current = this._state().maxColumnCount;
|
||||
const newValue = Math.max(1, Math.min(10, current + delta));
|
||||
this.updateState({maxColumnCount: newValue});
|
||||
}
|
||||
|
||||
updateGap(delta: number): void {
|
||||
const current = this._state().gap;
|
||||
const newValue = Math.max(0, Math.min(0.5, current + delta));
|
||||
this.updateState({gap: newValue});
|
||||
}
|
||||
|
||||
toggleJustify(): void {
|
||||
this.updateState({justify: !this._state().justify});
|
||||
}
|
||||
|
||||
toggleHyphenate(): void {
|
||||
this.updateState({hyphenate: !this._state().hyphenate});
|
||||
}
|
||||
|
||||
updateFontSize(delta: number): void {
|
||||
const newFontSize = Math.max(10, Math.min(32, this._state().fontSize + delta));
|
||||
this.updateState({fontSize: newFontSize});
|
||||
}
|
||||
|
||||
setTheme(theme: Theme): void {
|
||||
this.updateState({theme});
|
||||
}
|
||||
|
||||
setFontFamily(font: string | null): void {
|
||||
if (font === null) {
|
||||
this.updateState({fontFamily: null});
|
||||
} else if (!font.includes(':') && !isNaN(parseInt(font, 10)) && parseInt(font, 10).toString() === font) {
|
||||
this.updateState({fontFamily: `custom:${font}`});
|
||||
} else {
|
||||
this.updateState({fontFamily: font});
|
||||
}
|
||||
}
|
||||
|
||||
updateMaxInlineSize(delta: number): void {
|
||||
const newValue = Math.max(400, Math.min(1600, this._state().maxInlineSize + delta));
|
||||
this.updateState({maxInlineSize: newValue});
|
||||
}
|
||||
|
||||
updateMaxBlockSize(delta: number): void {
|
||||
const newValue = Math.max(600, Math.min(2400, this._state().maxBlockSize + delta));
|
||||
this.updateState({maxBlockSize: newValue});
|
||||
}
|
||||
|
||||
toggleDarkMode() {
|
||||
const currentState = this._state();
|
||||
const currentTheme = currentState.theme;
|
||||
const newIsDark = !currentState.isDark;
|
||||
const newTheme = {
|
||||
...currentTheme,
|
||||
fg: newIsDark ? currentTheme.dark.fg : currentTheme.light.fg,
|
||||
bg: newIsDark ? currentTheme.dark.bg : currentTheme.light.bg,
|
||||
link: newIsDark ? currentTheme.dark.link : currentTheme.light.link,
|
||||
};
|
||||
this.updateState({theme: newTheme, isDark: newIsDark});
|
||||
}
|
||||
|
||||
setThemeByName(themeName: string) {
|
||||
const theme = this.themes.find(t => t.name === themeName);
|
||||
if (theme) {
|
||||
const currentState = this._state();
|
||||
const newTheme = {
|
||||
...theme,
|
||||
fg: currentState.isDark ? theme.dark.fg : theme.light.fg,
|
||||
bg: currentState.isDark ? theme.dark.bg : theme.light.bg,
|
||||
link: currentState.isDark ? theme.dark.link : theme.light.link,
|
||||
};
|
||||
this.setTheme(newTheme);
|
||||
}
|
||||
}
|
||||
|
||||
setFlow(flow: 'paginated' | 'scrolled'): void {
|
||||
this.updateState({flow});
|
||||
}
|
||||
|
||||
private updateState(partial: Partial<ReaderState>): void {
|
||||
this._state.update(current => ({...current, ...partial}));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
export interface ThemeMode {
|
||||
fg: string;
|
||||
bg: string;
|
||||
link: string;
|
||||
}
|
||||
|
||||
export interface Theme {
|
||||
name: string;
|
||||
label: string;
|
||||
light: ThemeMode;
|
||||
dark: ThemeMode;
|
||||
fg?: string;
|
||||
bg?: string;
|
||||
link?: string;
|
||||
}
|
||||
|
||||
export const themes: Theme[] = [
|
||||
{
|
||||
name: 'default', label: 'Default',
|
||||
light: {fg: '#000000', bg: '#ffffff', link: '#0066cc'},
|
||||
dark: {fg: '#e0e0e0', bg: '#222222', link: '#77bbee'},
|
||||
},
|
||||
{
|
||||
name: 'gray', label: 'Gray',
|
||||
light: {fg: '#222222', bg: '#e0e0e0', link: '#4488cc'},
|
||||
dark: {fg: '#c6c6c6', bg: '#444444', link: '#88ccee'},
|
||||
},
|
||||
{
|
||||
name: 'sepia', label: 'Sepia',
|
||||
light: {fg: '#5b4636', bg: '#f1e8d0', link: '#008b8b'},
|
||||
dark: {fg: '#ffd595', bg: '#342e25', link: '#48d1cc'},
|
||||
},
|
||||
{
|
||||
name: 'crimson', label: 'Crimson',
|
||||
light: {fg: '#212529', bg: '#ffffff', link: '#dd0031'},
|
||||
dark: {fg: '#dee2e6', bg: '#343a40', link: '#ff4081'},
|
||||
},
|
||||
{
|
||||
name: 'meadow', label: 'Meadow',
|
||||
light: {fg: '#232c16', bg: '#d7dbbd', link: '#177b4d'},
|
||||
dark: {fg: '#d8deba', bg: '#333627', link: '#a6d608'},
|
||||
},
|
||||
{
|
||||
name: 'rosewood', label: 'Rosewood',
|
||||
light: {fg: '#4e1609', bg: '#f0d1d5', link: '#de3838'},
|
||||
dark: {fg: '#e5c4c8', bg: '#462f32', link: '#ff646e'},
|
||||
},
|
||||
{
|
||||
name: 'azure', label: 'Azure',
|
||||
light: {fg: '#262d48', bg: '#cedef5', link: '#2d53e5'},
|
||||
dark: {fg: '#babee1', bg: '#282e47', link: '#ff646e'},
|
||||
},
|
||||
{
|
||||
name: 'dawnlight', label: 'Dawnlight',
|
||||
light: {fg: '#586e75', bg: '#fdf6e3', link: '#268bd2'},
|
||||
dark: {fg: '#93a1a1', bg: '#002b36', link: '#268bd2'},
|
||||
},
|
||||
{
|
||||
name: 'ember', label: 'Ember',
|
||||
light: {fg: '#3c3836', bg: '#fbf1c7', link: '#076678'},
|
||||
dark: {fg: '#ebdbb2', bg: '#282828', link: '#83a598'},
|
||||
},
|
||||
{
|
||||
name: 'aurora', label: 'Aurora',
|
||||
light: {fg: '#2e3440', bg: '#eceff4', link: '#5e81ac'},
|
||||
dark: {fg: '#d8dee9', bg: '#2e3440', link: '#88c0d0'},
|
||||
},
|
||||
{
|
||||
name: 'ocean', label: 'Ocean',
|
||||
light: {fg: '#0a4d4d', bg: '#e0f7fa', link: '#00838f'},
|
||||
dark: {fg: '#b2dfdb', bg: '#263238', link: '#4dd0e1'},
|
||||
},
|
||||
{
|
||||
name: 'mist', label: 'Mist',
|
||||
light: {fg: '#4a148c', bg: '#f3e5f5', link: '#7b1fa2'},
|
||||
dark: {fg: '#c7b6dd', bg: '#3a3150', link: '#b39ddb'},
|
||||
},
|
||||
{
|
||||
name: 'amoled', label: 'AMOLED',
|
||||
light: {fg: '#000000', bg: '#ffffff', link: '#0066cc'},
|
||||
dark: {fg: '#ffffff', bg: '#000000', link: '#77bbee'},
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user