feat(bookmark): implement bookmark management with CRUD operations (#1795)

* feat(bookmark): implement bookmark management with CRUD operations and UI integration

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* feat(bookmark): add uniqueness constraint for bookmarks and check for existing bookmarks before creation

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2025-12-10 12:51:51 -07:00
committed by GitHub
parent 3bfece030b
commit f97b23ea14
16 changed files with 618 additions and 14 deletions
@@ -12,17 +12,49 @@
<div class="progress-info">
<span class="progress-percentage"><span class="progress-label">Progress: </span>{{ progressPercentage }}%</span>
</div>
<p-drawer [(visible)]="isDrawerVisible" [modal]="true" [position]="'left'" header="Chapters">
<ul class="chapter-list">
@for (chapter of chapters; track chapter) {
<li (click)="navigateToChapter(chapter); $event.stopPropagation()"
class="chapter-item"
[class.current-chapter]="chapter.href === currentChapterHref"
[style.padding-left.rem]="chapter.level * 1.5 + 0.75">
{{ chapter.label }}
</li>
<p-drawer [(visible)]="isDrawerVisible" [modal]="true" [position]="'left'" header="Table of Contents">
<div class="mb-3">
<h3 class="text-base font-semibold mb-2">Chapters</h3>
<ul class="chapter-list">
@for (chapter of chapters; track chapter) {
<li (click)="navigateToChapter(chapter); $event.stopPropagation()"
class="chapter-item"
[class.current-chapter]="chapter.href === currentChapterHref"
[style.padding-left.rem]="chapter.level * 1.5 + 0.75">
{{ chapter.label }}
</li>
}
</ul>
</div>
<p-divider></p-divider>
<div class="mt-3">
<h3 class="text-base font-semibold mb-2">Bookmarks</h3>
@if (bookmarks.length === 0) {
<p class="text-sm text-gray-400 italic">No bookmarks yet</p>
} @else {
<ul class="bookmark-list">
@for (bookmark of bookmarks; track bookmark.id) {
<li class="bookmark-item">
<div class="bookmark-content" (click)="navigateToBookmark(bookmark); $event.stopPropagation()">
<i class="pi pi-bookmark-fill mr-2"></i>
<span>{{ bookmark.title }}</span>
</div>
<p-button
icon="pi pi-trash"
(click)="deleteBookmark(bookmark.id); $event.stopPropagation()"
[rounded]="true"
[text]="true"
severity="danger"
size="small"
pTooltip="Delete bookmark">
</p-button>
</li>
}
</ul>
}
</ul>
</div>
</p-drawer>
</div>
@@ -32,6 +64,14 @@
<div class="header-right">
<div class="flex gap-4">
<p-button
size="small"
[icon]="isBookmarked ? 'pi pi-bookmark-fill' : 'pi pi-bookmark'"
(click)="addBookmark()"
severity="secondary"
[disabled]="!currentCfi || isAddingBookmark"
pTooltip="Add bookmark">
</p-button>
@if (!locationsReady) {
<div class="location-indicator" pTooltip="Saving progress not ready yet">
<span class="dot"></span>
@@ -163,6 +163,46 @@
border-radius: 0.45rem;
}
.bookmark-list {
list-style: none;
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
gap: 8px;
}
.bookmark-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 0.5rem 0.75rem;
border-radius: 0.35rem;
background-color: color-mix(in srgb, var(--primary-color) 10%, transparent);
transition: background-color 0.3s ease;
&:hover {
background-color: color-mix(in srgb, var(--primary-color) 20%, transparent);
}
}
.bookmark-content {
display: flex;
align-items: center;
cursor: pointer;
flex: 1;
color: var(--text-color);
font-size: 14px;
i {
color: var(--primary-color);
}
&:hover {
color: var(--primary-color);
}
}
.location-indicator {
display: flex;
align-items: center;
@@ -1,6 +1,7 @@
import {Component, ElementRef, inject, OnDestroy, OnInit, ViewChild, NgZone} from '@angular/core';
import ePub from 'epubjs';
import {Drawer} from 'primeng/drawer';
import {Subscription} from 'rxjs';
import {Button} from 'primeng/button';
import {FormsModule} from '@angular/forms';
@@ -12,6 +13,7 @@ import {Select} from 'primeng/select';
import {UserService} from '../../../settings/user-management/user.service';
import {ProgressSpinner} from 'primeng/progressspinner';
import {MessageService} from 'primeng/api';
import {BookMarkService, BookMark} from '../../../../shared/service/book-mark.service';
import {Tooltip} from 'primeng/tooltip';
import {Slider} from 'primeng/slider';
import {FALLBACK_EPUB_SETTINGS, getChapter} from '../epub-reader-helper';
@@ -36,6 +38,12 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
currentChapterHref: string | null = null;
isDrawerVisible = false;
isSettingsDrawerVisible = false;
bookmarks: BookMark[] = [];
currentCfi: string | null = null;
isBookmarked = false;
isAddingBookmark = false;
isDeletingBookmark = false;
private routeSubscription?: Subscription;
public locationsReady = false;
public approxProgress = 0;
@@ -80,11 +88,12 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
private messageService = inject(MessageService);
private ngZone = inject(NgZone);
private pageTitle = inject(PageTitleService);
private bookMarkService = inject(BookMarkService);
epub!: Book;
ngOnInit(): void {
this.route.paramMap.subscribe((params) => {
this.routeSubscription = this.route.paramMap.subscribe((params) => {
this.isLoading = true;
const bookId = +params.get('bookId')!;
@@ -92,10 +101,13 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
const epub$ = this.bookService.getBookByIdFromAPI(bookId, false);
const epubData$ = this.bookService.getFileContent(bookId);
const bookSetting$ = this.bookService.getBookSetting(bookId);
const bookmarks$ = this.bookMarkService.getBookmarksForBook(bookId);
forkJoin([myself$, epub$, epubData$, bookSetting$]).subscribe({
next: ([myself, epub, epubData, bookSetting]) => {
forkJoin([myself$, epub$, epubData$, bookSetting$, bookmarks$]).subscribe({
next: ([myself, epub, epubData, bookSetting, bookmarks]) => {
this.epub = epub;
this.bookmarks = bookmarks;
this.updateBookmarkStatus();
const individualSetting = bookSetting?.epubSettings;
const fileReader = new FileReader();
@@ -363,6 +375,8 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
this.rendition.on('relocated', (location: any) => {
this.updateCurrentChapter(location);
const cfi = location.end.cfi;
this.currentCfi = location.start.cfi;
this.updateBookmarkStatus();
const currentIndex = location.start.index;
const totalSpineItems = this.book.spine.items.length;
let percentage: number;
@@ -400,6 +414,8 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
}
ngOnDestroy(): void {
this.routeSubscription?.unsubscribe();
if (this.rendition) {
this.rendition.off('keyup', this.keyListener);
}
@@ -472,4 +488,92 @@ export class EpubReaderComponent implements OnInit, OnDestroy {
return chapters;
}
addBookmark(): void {
if (!this.currentCfi || this.isAddingBookmark) {
return;
}
if (this.isCurrentLocationBookmarked()) {
this.messageService.add({
severity: 'info',
summary: 'Info',
detail: 'Bookmark already exists at this location',
});
return;
}
this.isAddingBookmark = true;
const title = this.currentChapter || 'Untitled Bookmark';
const request = {
bookId: this.epub.id,
cfi: this.currentCfi,
title: title
};
this.bookMarkService.createBookmark(request).subscribe({
next: (bookmark) => {
this.bookmarks.push(bookmark);
this.updateBookmarkStatus();
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Bookmark created successfully',
});
this.isAddingBookmark = false;
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to create bookmark',
});
this.isAddingBookmark = false;
},
});
}
deleteBookmark(bookmarkId: number): void {
if (this.isDeletingBookmark) {
return;
}
this.isDeletingBookmark = true;
this.bookMarkService.deleteBookmark(bookmarkId).subscribe({
next: () => {
this.bookmarks = this.bookmarks.filter(b => b.id !== bookmarkId);
this.updateBookmarkStatus();
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Bookmark deleted successfully',
});
this.isDeletingBookmark = false;
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to delete bookmark',
});
this.isDeletingBookmark = false;
},
});
}
navigateToBookmark(bookmark: BookMark): void {
if (this.rendition && bookmark.cfi) {
this.rendition.display(bookmark.cfi);
}
}
isCurrentLocationBookmarked(): boolean {
if (!this.currentCfi) return false;
return this.bookmarks.some(b => b.cfi === this.currentCfi);
}
private updateBookmarkStatus(): void {
this.isBookmarked = this.currentCfi
? this.bookmarks.some(b => b.cfi === this.currentCfi)
: false;
}
}
@@ -0,0 +1,39 @@
import {Injectable, inject} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {API_CONFIG} from '../../core/config/api-config';
export interface BookMark {
id: number;
bookId: number;
cfi: string;
title: string;
createdAt: string;
}
export interface CreateBookMarkRequest {
bookId: number;
cfi: string;
title?: string;
}
@Injectable({
providedIn: 'root'
})
export class BookMarkService {
private readonly url = `${API_CONFIG.BASE_URL}/api/v1/bookmarks`;
private readonly http = inject(HttpClient);
getBookmarksForBook(bookId: number): Observable<BookMark[]> {
return this.http.get<BookMark[]>(`${this.url}/book/${bookId}`);
}
createBookmark(request: CreateBookMarkRequest): Observable<BookMark> {
return this.http.post<BookMark>(this.url, request);
}
deleteBookmark(bookmarkId: number): Observable<void> {
return this.http.delete<void>(`${this.url}/${bookmarkId}`);
}
}