1- import { BasePlugin , createEmitter , PluginRegistry } from '@embedpdf/core' ;
1+ import { BasePlugin , PluginRegistry } from '@embedpdf/core' ;
22
3- import { AttachmentCapability , AttachmentPluginConfig } from './types' ;
3+ import { AttachmentCapability , AttachmentPluginConfig , AttachmentScope } from './types' ;
44import {
55 PdfAttachmentObject ,
66 PdfErrorCode ,
@@ -18,30 +18,64 @@ export class AttachmentPlugin extends BasePlugin<AttachmentPluginConfig, Attachm
1818
1919 async initialize ( _ : AttachmentPluginConfig ) : Promise < void > { }
2020
21+ // ─────────────────────────────────────────────────────────
22+ // Capability
23+ // ─────────────────────────────────────────────────────────
24+
2125 protected buildCapability ( ) : AttachmentCapability {
2226 return {
23- getAttachments : this . getAttachments . bind ( this ) ,
24- downloadAttachment : this . downloadAttachment . bind ( this ) ,
27+ // Active document operations
28+ getAttachments : ( ) => this . getAttachments ( ) ,
29+ downloadAttachment : ( attachment ) => this . downloadAttachment ( attachment ) ,
30+
31+ // Document-scoped operations
32+ forDocument : ( documentId : string ) => this . createAttachmentScope ( documentId ) ,
33+ } ;
34+ }
35+
36+ // ─────────────────────────────────────────────────────────
37+ // Document Scoping
38+ // ─────────────────────────────────────────────────────────
39+
40+ private createAttachmentScope ( documentId : string ) : AttachmentScope {
41+ return {
42+ getAttachments : ( ) => this . getAttachments ( documentId ) ,
43+ downloadAttachment : ( attachment ) => this . downloadAttachment ( attachment , documentId ) ,
2544 } ;
2645 }
2746
28- private downloadAttachment ( attachment : PdfAttachmentObject ) : Task < ArrayBuffer , PdfErrorReason > {
29- const doc = this . coreState . core . document ;
47+ // ─────────────────────────────────────────────────────────
48+ // Core Operations
49+ // ─────────────────────────────────────────────────────────
50+
51+ private downloadAttachment (
52+ attachment : PdfAttachmentObject ,
53+ documentId ?: string ,
54+ ) : Task < ArrayBuffer , PdfErrorReason > {
55+ const id = documentId ?? this . getActiveDocumentId ( ) ;
56+ const coreDoc = this . coreState . core . documents [ id ] ;
3057
31- if ( ! doc ) {
32- return PdfTaskHelper . reject ( { code : PdfErrorCode . NotFound , message : 'Document not found' } ) ;
58+ if ( ! coreDoc ?. document ) {
59+ return PdfTaskHelper . reject ( {
60+ code : PdfErrorCode . NotFound ,
61+ message : `Document ${ id } not found` ,
62+ } ) ;
3363 }
3464
35- return this . engine . readAttachmentContent ( doc , attachment ) ;
65+ return this . engine . readAttachmentContent ( coreDoc . document , attachment ) ;
3666 }
3767
38- private getAttachments ( ) : Task < PdfAttachmentObject [ ] , PdfErrorReason > {
39- const doc = this . coreState . core . document ;
68+ private getAttachments ( documentId ?: string ) : Task < PdfAttachmentObject [ ] , PdfErrorReason > {
69+ const id = documentId ?? this . getActiveDocumentId ( ) ;
70+ const coreDoc = this . coreState . core . documents [ id ] ;
4071
41- if ( ! doc ) {
42- return PdfTaskHelper . reject ( { code : PdfErrorCode . NotFound , message : 'Document not found' } ) ;
72+ if ( ! coreDoc ?. document ) {
73+ return PdfTaskHelper . reject ( {
74+ code : PdfErrorCode . NotFound ,
75+ message : `Document ${ id } not found` ,
76+ } ) ;
4377 }
4478
45- return this . engine . getAttachments ( doc ) ;
79+ return this . engine . getAttachments ( coreDoc . document ) ;
4680 }
4781}
0 commit comments