# @embedpdf/angular — ./selection ArmedSelectionHandle [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface ArmedSelectionHandle{ base: Point; drag: SelectionHandleDragSession; } EpdfSelection [ClassDeclaration] (from packages/framework/angular/selection/src/selection.ts): @Injectable({ providedIn: 'root'} ) export class EpdfSelection extends pluginService({ name: 'EpdfSelection', feature: 'withSelection()', token: SelectionToken, state: selectionState, methods: [ 'select', 'selectWordAt', 'selectLineAt', 'selectPage', 'selectAll', 'extendTo', 'clear', 'readText', 'readTextInRange', 'listSegments', 'listRects', 'getAnchor', 'getSnapshot', 'getRange', 'listSelectedPages', 'canSelect', 'canCopy', ], events: ['onChanged', 'onCommitted', 'onCleared'], } ){ } EpdfSelectionClipboard [ClassDeclaration] (from packages/framework/angular/selection/src/selection-clipboard.ts): @Directive({ selector: 'epdf-selection-clipboard, [epdfSelectionClipboard]'} ) export class EpdfSelectionClipboard{ readonly prefetch = input(undefined); readonly refused = output(); private readonly selection = new CapabilityBinding( injectKernelHost(''), () => SelectionToken, injectDocumentScope(), ).capability; constructor(){ const host = inject>(ElementRef).nativeElement; if (!isPlatformBrowser(inject(PLATFORM_ID))) return; const target = host.localName === 'epdf-selection-clipboard' ? undefined : host; effect((onCleanup) =>{ const selection = this.selection(); const prefetch = this.prefetch(); if (!selection) return; onCleanup( wireSelectionClipboard(selection, { ...(prefetch === undefined ?{ } :{ prefetch} ), ...(target ?{ target} :{ } ), onRefused: () => this.refused.emit(), } ), ); } ); } } EpdfSelectionHandles [ClassDeclaration] (from packages/framework/angular/selection/src/selection-handles.ts): @Component({ selector: 'epdf-selection-handles', imports: [EpdfSelectionGrip], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @for (handle of handles(); track handle.role){
} `, } ) export class EpdfSelectionHandles{ readonly token = input | undefined>(undefined); protected readonly pad = HANDLE_PAD; protected readonly bar = HANDLE_BAR; protected readonly head = HANDLE_HEAD; protected readonly gripWidth = HANDLE_BAR + 2 * HANDLE_PAD; private readonly host = injectKernelHost(''); private readonly enclosingStage = inject(EpdfStage, { optional: true} ); private readonly projection = inject(EPDF_PROJECTOR, { optional: true} ); private readonly documentId = injectDocumentScope(); private readonly selection = new CapabilityBinding( this.host, () => SelectionHostToken, this.documentId, ); private readonly stage = new CapabilityBinding( this.host, () => this.token() ?? this.enclosingStage?.token() ?? StageToken, this.documentId, ).capability; private readonly selecting = this.selection.select((selection) => selection.isSelecting(), false); private readonly visible = this.selection.select( (selection) => selection.isHighlightVisible(), true, ); private readonly settings = this.selection.select( (selection) => selection.getSettings().handles, SELECTION_DEFAULTS.handles, ); private readonly endpoints = this.selection.select( (selection) => selectionHandleEndpointsOf(selection.getSnapshot()), null, sameSelectionEndpoints, ); private readonly dragging = signal(null); protected readonly color = computed(() => paint('text-selection-handle', this.settings().color ?? this.host.viewerSettings().accent), ); protected readonly shadow = computed(() => paint('text-selection-handle-shadow', this.settings().shadow), ); private readonly overStage = computed(() => this.projection?.projector()?.space === 'overlay'); protected readonly handles = computed((): readonly HandleLayout[] =>{ const stage = this.stage(); const endpoints = this.endpoints(); if (!this.overStage() || !stage || !endpoints || !this.visible()) return NO_HANDLES; if (this.selecting() && !this.dragging()) return NO_HANDLES; this.projection?.revision(); const view = selectionHandleViewOf(stage); return (['start', 'end'] as const).flatMap((role) =>{ const layout = layoutOf(view, endpoints[role], role); return layout ? [layout] : []; } ); } ); protected readonly armStart = () => this.arm('start'); protected readonly armEnd = () => this.arm('end'); constructor(){ if (!this.projection) warnOutsideStage(); effect(() =>{ if (this.projection?.projector()?.space === 'client') warnOutsideStage(); } ); } private arm(role: Role): SelectionGripDrag{ const stage = untracked(this.stage); const endpoints = untracked(this.endpoints); const selection = untracked(this.selection.capability); if (!stage || !endpoints || !selection) return null; const armed = armSelectionHandle(selection, selectionHandleViewOf(stage), endpoints, role); if (!armed) return null; this.dragging.set(role); return{ base: armed.base, session:{ move: armed.drag.move, end: () =>{ armed.drag.end(); this.dragging.set(null); } , } , } ; } } EpdfSelectionLayer [ClassDeclaration] (from packages/framework/angular/selection/src/selection-layer.ts): @Component({ selector: 'epdf-selection-layer', changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (visible() && polygons().length > 0){ @for (points of polygons(); track $index){ } } `, } ) export class EpdfSelectionLayer{ private readonly page = injectPage(''); private readonly host = injectKernelHost(''); private readonly selection = new CapabilityBinding( this.host, () => SelectionHostToken, () => this.page.documentId, ); private readonly segments = this.selection.select( (selection) => selection.listSegments(this.page.ref), NO_SEGMENTS, ); protected readonly visible = this.selection.select( (selection) => selection.isHighlightVisible(), true, ); private readonly color = this.selection.select( (selection) => selection.getSettings().color, null, ); protected readonly polygons = computed(() =>{ const transform = this.page.transform(); return this.segments().map((segment) => svgPoints(quadInPixels(segment.quad, transform))); } ); protected readonly fill = computed(() => paint( 'text-selection', this.color() ?? mixAccent('text-selection', this.host.viewerSettings().accent), ), ); constructor(){ effect(() =>{ const selection = this.selection.capability(); const page = this.page.ref; if (selection) untracked(() => void selection.ensureLoaded(page)); } ); } } EpdfSelectionMenu [ClassDeclaration] (from packages/framework/angular/selection/src/selection-menu.ts): @Component({ selector: 'epdf-selection-menu', imports: [EpdfAnchored], changeDetection: ChangeDetectionStrategy.OnPush, template: ` `, } ) export class EpdfSelectionMenu{ readonly placement = input('top'); readonly gap = input(8); private readonly selection = new CapabilityBinding( injectKernelHost(''), () => SelectionToken, injectDocumentScope(), ); private readonly selecting = this.selection.select((selection) => selection.isSelecting(), false); private readonly settled = this.selection.select( (selection) => selection.getAnchor(), null, samePageBounds, ); protected readonly anchor = computed(() => (this.selecting() ? null : this.settled())); } HANDLE_BAR [VariableDeclaration] (from packages/plugin/selection/src/handles.ts): HANDLE_BAR = 2 HANDLE_HEAD [VariableDeclaration] (from packages/plugin/selection/src/handles.ts): HANDLE_HEAD = 12 HANDLE_PAD [VariableDeclaration] (from packages/plugin/selection/src/handles.ts): HANDLE_PAD = 14 SELECTION_DEFAULTS [VariableDeclaration] (from packages/plugin/selection/src/contract.ts): SELECTION_DEFAULTS: SelectionSettings ={ dragThresholdPx: 4, color: null, handles:{ color: null, shadow: '0 1px 4px rgb(0 0 0 / 0.35)'} , } SelectionAnchor [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionAnchor{ readonly page: PageRef; readonly bounds: Rect; } SelectionCapability [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionCapability extends SettingsApi{ canSelect(): boolean; canCopy(): boolean; select(range: SelectionRangeInput): void; selectAll(): void; selectPage(page: PageRef | number): void; selectWordAt(page: PageRef | number, point: Point): boolean; selectLineAt(page: PageRef | number, point: Point): boolean; extendTo(page: PageRef | number, point: Point): void; clear(): void; hasSelection(): boolean; isSelecting(): boolean; getSnapshot(): SelectionSnapshot; getRange(): TextRange | null; listSelectedPages(): readonly PageRef[]; listSegments(page: PageRef | number): readonly SelectionSegment[]; listRects(page: PageRef | number): readonly Rect[]; getAnchor(): SelectionAnchor | null; readText(options?: OperationOptions): Promise; readTextInRange(range: TextRange, options?: OperationOptions): Promise; readonly onChanged: EventHook; readonly onCommitted: EventHook; readonly onCleared: EventHook; } SelectionChangedEvent [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionChangedEvent{ readonly range: TextRange | null; readonly pages: readonly PageRef[]; } SelectionClearedEvent [TypeAliasDeclaration] (from packages/plugin/selection/src/contract.ts): export type SelectionClearedEvent = Record; SelectionCommittedEvent [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionCommittedEvent{ readonly range: TextRange; } SelectionConfig [TypeAliasDeclaration] (from packages/plugin/selection/src/contract.ts): export type SelectionConfig = DeepPartial; SelectionEndpoint [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionEndpoint{ readonly page: PageRef; readonly glyphQuad: Quad; readonly advance: 1 | -1; readonly rect: Rect; } SelectionHandleDragSession [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleDragSession{ move(overlay: Point): void; end(): void; } SelectionHandleEndpoint [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleEndpoint{ page: PageRef; glyphQuad: Quad; advance: 1 | -1; } SelectionHandleEndpoints [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleEndpoints{ start: SelectionHandleEndpoint; end: SelectionHandleEndpoint; } SelectionHandleGeometry [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleGeometry{ bar:{ from: Point; to: Point} ; length: number; rotation: number; upright: boolean; head: Point; } SelectionHandleStage [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleStage{ pageToViewport(page: PageRef, point: Point): Point | null; getPageAt(point: Point):{ ref: PageRef; point: Point} | null; viewportToPage(page: PageRef, point: Point): Point | null; } SelectionHandleTarget [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleTarget{ beginGestureAt(page: PageRef, point: Point): boolean; extendTo(page: PageRef, point: Point): void; endGesture(): void; } SelectionHandleView [InterfaceDeclaration] (from packages/plugin/selection/src/handles.ts): export interface SelectionHandleView{ toOverlay(page: PageRef, point: Point): Point | null; pageAt(overlay: Point):{ ref: PageRef; point: Point} | null; pointOnPage(page: PageRef, overlay: Point): Point | null; } SelectionRangeInput [TypeAliasDeclaration] (from packages/plugin/selection/src/contract.ts): export type SelectionRangeInput = TextRange |{ page: PageRef; start: number; count: number} ; SelectionSegment [TypeAliasDeclaration] (from packages/plugin/selection/src/geometry.ts): export type SelectionSegment = PdfTextSegment; SelectionSettings [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionSettings{ readonly dragThresholdPx: number; readonly color: string | null; readonly handles:{ readonly color: string | null; readonly shadow: string; } ; } SelectionSnapshot [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface SelectionSnapshot{ readonly pages: readonly{ page: PageRef; segments: readonly SelectionSegment[]} []; readonly start: SelectionEndpoint | null; readonly end: SelectionEndpoint | null; readonly direction: 'forward' | 'backward'; readonly range: TextRange | null; } SelectionState [InterfaceDeclaration] (from packages/plugin/selection/src/state.ts): export interface SelectionState{ readonly hasSelection: boolean; readonly isSelecting: boolean; readonly range: TextRange | null; readonly pages: readonly PageRef[]; } SelectionToken [VariableDeclaration] (from packages/plugin/selection/src/token.ts): SelectionToken = createCapabilityToken('selection', { hint: `add selectionPlugin() from '@embedpdf/plugin-selection' to your plugins list`, promises:{ readText: true, readTextInRange: true, } , } ) TextPosition [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface TextPosition{ readonly page: PageRef; readonly index: number; } TextRange [InterfaceDeclaration] (from packages/plugin/selection/src/contract.ts): export interface TextRange{ readonly start: TextPosition; readonly end: TextPosition; } armSelectionHandle [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function armSelectionHandle( selection: SelectionHandleTarget, view: SelectionHandleView, endpoints: SelectionHandleEndpoints, role: 'start' | 'end', ): ArmedSelectionHandle | null{ const geometry = selectionHandleGeometry(view, endpoints[role], role); if (!geometry) return null; const opposite = endpoints[role === 'start' ? 'end' : 'start']; return{ base: midpoint(geometry.bar.from, geometry.bar.to), drag: createSelectionHandleDrag(selection, view, opposite, endpoints[role].page), } ; } copySelection [FunctionDeclaration] (from packages/framework/web/src/clipboard.ts): export async function copySelection( selection: Pick, ): Promise{ const text = await selection.readText(); if (text !== '') await navigator.clipboard.writeText(text); return text; } createSelectionHandleDrag [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function createSelectionHandleDrag( selection: SelectionHandleTarget, view: SelectionHandleView, opposite: SelectionHandleEndpoint, draggedPage: PageRef, ): SelectionHandleDragSession{ const quad = opposite.glyphQuad; const anchorPoint = midpoint( midpoint(quad.upperLeft, quad.lowerRight), midpoint(quad.upperRight, quad.lowerLeft), ); let begun = false; let lastPage = draggedPage; return{ move: (overlay) =>{ if (!begun){ if (!selection.beginGestureAt(opposite.page, anchorPoint)) return; begun = true; } const hit = view.pageAt(overlay); if (hit){ lastPage = hit.ref; selection.extendTo(hit.ref, hit.point); } else{ const point = view.pointOnPage(lastPage, overlay); if (point) selection.extendTo(lastPage, point); } } , end: () =>{ if (begun) selection.endGesture(); } , } ; } selectionCommands [VariableDeclaration] (from packages/framework/angular/selection/src/commands.ts): selectionCommands = createSelectionCommands(standardCommandsBrowser) selectionHandleEndpointsOf [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function selectionHandleEndpointsOf( snapshot: Pick, ): SelectionHandleEndpoints | null{ const{ start, end} = snapshot; if (!start || !end) return null; return{ start:{ page: start.page, glyphQuad: start.glyphQuad, advance: start.advance} , end:{ page: end.page, glyphQuad: end.glyphQuad, advance: end.advance} , } ; } selectionHandleGeometry [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function selectionHandleGeometry( view: SelectionHandleView, endpoint: SelectionHandleEndpoint, role: 'start' | 'end', ): SelectionHandleGeometry | null{ const leading = role === 'start' ? endpoint.advance > 0 : endpoint.advance < 0; const [upperPage, lowerPage] = quadEdge(endpoint.glyphQuad, leading ? 'left' : 'right'); const upper = view.toOverlay(endpoint.page, upperPage); const lower = view.toOverlay(endpoint.page, lowerPage); if (!upper || !lower) return null; const dx = lower.x - upper.x; const dy = lower.y - upper.y; const length = Math.hypot(dx, dy); if (length === 0) return null; const rotation = (Math.atan2(dy, dx) * 180) / Math.PI - 90; const upright = Math.abs(rotation) < HANDLE_ROT_EPSILON; const ux = dx / length; const uy = dy / length; const head = role === 'start' ?{ x: upper.x - (ux * HANDLE_HEAD) / 2, y: upper.y - (uy * HANDLE_HEAD) / 2} :{ x: lower.x + (ux * HANDLE_HEAD) / 2, y: lower.y + (uy * HANDLE_HEAD) / 2} ; return{ bar:{ from: upper, to: lower} , length, rotation, upright, head} ; } selectionHandleViewOf [FunctionDeclaration] (from packages/plugin/selection/src/handles.ts): export function selectionHandleViewOf(stage: SelectionHandleStage): SelectionHandleView{ return{ toOverlay: (page, point) => stage.pageToViewport(page, point), pageAt: (overlay) => stage.getPageAt(overlay), pointOnPage: (page, overlay) => stage.viewportToPage(page, overlay), } ; } selectionPlugin [VariableDeclaration] (from packages/plugin/selection/src/selection.plugin.ts): selectionPlugin = (config?: SelectionConfig) => definePlugin({ id: 'selection', token: SelectionToken, scope: 'document', requires: [InteractionToken], optional: [FeedbackToken], state: initialSelectionModel, settings:{ defaults: SELECTION_DEFAULTS, registered: config} , create: createSelectionController, } ) selectionState [VariableDeclaration] (from packages/plugin/selection/src/state.ts): selectionState = defineState(SelectionToken, { read: (selection): SelectionState => ({ hasSelection: selection.hasSelection(), isSelecting: selection.isSelecting(), range: selection.getRange(), pages: selection.listSelectedPages(), } ), empty:{ hasSelection: false, isSelecting: false, range: null, pages: [], } , } ) withSelection [FunctionDeclaration] (from packages/framework/angular/selection/src/selection.ts): export function withSelection(options?: SelectionConfig): EmbedPdfFeature{ return{ plugins: [selectionPlugin(options)], services: [EpdfSelection]} ; }