Files
grimmory/frontend/src/app/features/readers/ebook-reader/shared/selection-popup.component.ts
T
brios 85e8d235b6 fix(dashboard): improve performance with OnPush change detection (#440)
* fix(dashboard): improve performance with OnPush change detection

* fix(dashboard): refactor book card component for improved readability and performance

* fix(tests): mock matchMedia for improved test compatibility

* fix(tests): matchMedia mock for better CI compatibility and support

* fix(dashboard): add clearAll method to BookBrowserScrollService and update tests

* fix(dashboard): improve performance with content visibility and refactor styles

* fix(dashboard): improve scroll performance with estimated total height and refactor layout

* fix(tests): add mock for getCurrentUser in userService for improved test coverage

* fix(dashboard): implement infinite scroll for book loading and scroll restoration

* fix(tests): fix lint error

* fix(tests): mock ResizeObserver and IntersectionObserver

* fix(tests): mock ResizeObserver and IntersectionObserver (again)

* fix(dashboard): improve accessibility and performance by updating aria attributes and optimizing animations

* fix(dashboard): improve submenu accessibility and optimize virtual scroller performance

* fix(dashboard): clean up imports and improve component structure

* fix(dashboard): correct mobile check in author browser component

* fix(dashboard): add OnDestroy lifecycle hook to BookBrowserComponent
2026-04-22 17:27:11 -04:00

155 lines
4.1 KiB
TypeScript

import {Component, EventEmitter, Input, Output} from '@angular/core';
import {TranslocoDirective} from '@jsverse/transloco';
import {ReaderIconComponent} from './icon.component';
export type AnnotationStyle = 'highlight' | 'underline' | 'strikethrough' | 'squiggly';
export interface TextSelectionAction {
type: 'select' | 'annotate' | 'delete' | 'dismiss' | 'preview' | 'search' | 'note' | 'go-to-link';
color?: string;
style?: AnnotationStyle;
annotationId?: number;
searchText?: string;
}
@Component({
selector: 'app-text-selection-popup',
standalone: true,
imports: [TranslocoDirective, ReaderIconComponent],
templateUrl: './selection-popup.component.html',
styleUrls: ['./selection-popup.component.scss']
})
export class TextSelectionPopupComponent {
@Input() set visible(value: boolean) {
this._visible = value;
if (value) {
this.showAnnotationOptions = false;
this.hasPreview = false;
}
}
get visible(): boolean {
return this._visible;
}
private _visible = false;
@Input() position = {x: 0, y: 0};
@Input() showBelow = false;
@Input() overlappingAnnotationId: number | null = null;
@Input() selectedText = '';
@Input() linkUrl?: string;
@Output() action = new EventEmitter<TextSelectionAction>();
showAnnotationOptions = false;
selectedColor = '#FACC15';
selectedStyle: AnnotationStyle = 'highlight';
private hasPreview = false;
highlightColors = [
{value: '#FACC15', label: 'Yellow'},
{value: '#4ADE80', label: 'Green'},
{value: '#38BDF8', label: 'Blue'},
{value: '#F472B6', label: 'Pink'},
{value: '#FB923C', label: 'Orange'}
];
lineColors = [
{value: '#B8860B', label: 'Dark Gold'},
{value: '#228B22', label: 'Forest Green'},
{value: '#1E90FF', label: 'Dodger Blue'},
{value: '#DC143C', label: 'Crimson'},
{value: '#FF8C00', label: 'Dark Orange'}
];
get colors() {
return this.selectedStyle === 'highlight' ? this.highlightColors : this.lineColors;
}
styles: { value: AnnotationStyle; label: string; icon: string }[] = [
{value: 'highlight', label: 'Highlight', icon: 'H'},
{value: 'underline', label: 'Underline', icon: 'U'},
{value: 'squiggly', label: 'Squiggly', icon: '~'},
{value: 'strikethrough', label: 'Strikethrough', icon: 'S'}
];
onSelect(): void {
this.action.emit({type: 'select'});
this.showAnnotationOptions = false;
this.hasPreview = false;
}
toggleAnnotationOptions(): void {
this.showAnnotationOptions = !this.showAnnotationOptions;
if (this.showAnnotationOptions) {
this.emitPreview();
}
}
selectColor(color: string): void {
this.selectedColor = color;
this.emitPreview();
}
selectStyle(style: AnnotationStyle): void {
const wasHighlight = this.selectedStyle === 'highlight';
const isHighlight = style === 'highlight';
this.selectedStyle = style;
if (wasHighlight !== isHighlight) {
this.selectedColor = isHighlight ? this.highlightColors[0].value : this.lineColors[0].value;
}
this.emitPreview();
}
private emitPreview(): void {
this.hasPreview = true;
this.action.emit({
type: 'preview',
color: this.selectedColor,
style: this.selectedStyle
});
}
onDelete(): void {
if (this.overlappingAnnotationId) {
this.action.emit({type: 'delete', annotationId: this.overlappingAnnotationId});
}
}
onSearch(): void {
this.action.emit({type: 'search', searchText: this.selectedText});
this.showAnnotationOptions = false;
this.hasPreview = false;
}
onGoToLink(): void {
if (this.linkUrl) {
this.action.emit({type: 'go-to-link'});
}
}
onNote(): void {
this.action.emit({type: 'note'});
this.showAnnotationOptions = false;
this.hasPreview = false;
}
onDismiss(event: Event): void {
event.stopPropagation();
event.preventDefault();
if (this.hasPreview) {
this.action.emit({
type: 'annotate',
color: this.selectedColor,
style: this.selectedStyle
});
} else {
this.action.emit({type: 'dismiss'});
}
this.showAnnotationOptions = false;
this.hasPreview = false;
}
}