/** * The shell plugin's service, feature and directives: which panels, dialogs and menus are open. * The app draws them; these bind their open state to the viewer, so a command, a button and * your code open the same panel, and the layout can be stored and restored. * * withShell() the plugin, for provideEmbedPdf() * inject(EpdfShell) `openSurfaces()`, `openMenus()`, `open()`, `toggle()`, … * shell.surface('comments') one panel: `isOpen()`, `props()`, `open()`, `close()` * * * The template reference has `isOpen()`: `#comments="epdfPanelToggle"`. */ @Directive({ selector: '[epdfPanelToggle]', exportAs: 'epdfPanelToggle', host: { '(click)': 'toggle()', '[attr.aria-expanded]': 'isOpen()', }, }) export class EpdfPanelToggle { /** The panel's name. */ readonly id = input.required({ alias: 'epdfPanelToggle' }); /** Its group: opening it closes the other open panel of the same group. */ readonly exclusive = input(undefined, { alias: 'epdfPanelToggleExclusive' }); private readonly panel = inject(EpdfShell).surface(() => this.id()); /** Whether the panel is open. */ readonly isOpen: Signal = this.panel.isOpen; /** Open the panel when it's closed, close it when it's open. */ toggle(): void { const exclusive = this.exclusive(); this.panel.toggle(exclusive === undefined ? undefined : { exclusive }); } } /** * An element of yours that takes the cursor when the panel `epdfSurfaceFocus` names asks for it: * on every `open(id, { focus: true })`, a command whose panel says `focus` among them, and when * the element appears while the request is up (the panel opened, or its code loaded, because of * it). A plain open never moves it. `epdfSurfaceFocusSelect` selects an input's text, so typing * replaces it: * * * * It reads the panel of this part of the template's document. */ @Directive({ selector: '[epdfSurfaceFocus]' }) export class EpdfSurfaceFocus { /** The panel's name. */ readonly id = input.required({ alias: 'epdfSurfaceFocus' }); /** Select the text of an input or a text area as it takes focus. */ readonly select = input(false, { alias: 'epdfSurfaceFocusSelect', transform: booleanAttribute }); constructor() { // The service's own checks: a viewer above, with withShell() in it. inject(EpdfShell); const element = inject>(ElementRef).nativeElement; const shell = new CapabilityBinding( injectKernelHost('epdfSurfaceFocus'), () => ShellToken, injectDocumentScope(), ); const request = shell.select( (current) => current.getSurface(this.id())?.focusRequest ?? 0, 0, Object.is, ); const follower = createSurfaceFocusFollower(); // After rendering, so an element that has just appeared is in place to take focus. afterRenderEffect({ write: () => { const current = shell.capability(); const value = request(); const select = this.select(); untracked(() => follower.follow(element, current, value, { select })); }, }); } }