{"version":3,"file":"ng-doc-app-processors-page-processor.mjs","sources":["../../../../libs/app/processors/page-processor/page-processor.component.ts","../../../../libs/app/processors/page-processor/ng-doc-app-processors-page-processor.ts"],"sourcesContent":["import {\n  ApplicationRef,\n  ChangeDetectionStrategy,\n  Component,\n  ComponentRef,\n  ElementRef,\n  EventEmitter,\n  HostBinding,\n  inject,\n  Injector,\n  Input,\n  OnChanges,\n  Output,\n  Renderer2,\n  SimpleChanges,\n  ViewContainerRef,\n} from '@angular/core';\nimport { SafeHtml } from '@angular/platform-browser';\nimport { NgDocPageProcessor, NgDocProcessorOptions } from '@ng-doc/app/interfaces';\nimport { NG_DOC_PAGE_CUSTOM_PROCESSOR, NG_DOC_PAGE_PROCESSOR } from '@ng-doc/app/tokens';\nimport { asArray } from '@ng-doc/core/helpers/as-array';\nimport { objectKeys } from '@ng-doc/core/helpers/object-keys';\n\n/**\n * Base processor class to create a processor directive that will be used to replace\n * html nodes with an Angular component.\n */\n@Component({\n  selector: '[ngDocPageProcessor]',\n  standalone: true,\n  template: '<ng-content></ng-content>',\n  changeDetection: ChangeDetectionStrategy.OnPush,\n})\nexport class NgDocPageProcessorComponent implements OnChanges {\n  @Input({ required: true, alias: 'ngDocPageProcessor' })\n  @HostBinding('innerHTML')\n  html: SafeHtml = '';\n\n  @Output()\n  afterRender: EventEmitter<void> = new EventEmitter<void>();\n\n  processors: Array<NgDocPageProcessor<unknown>> =\n    inject<Array<NgDocPageProcessor<unknown>>>(NG_DOC_PAGE_PROCESSOR, { optional: true }) ?? [];\n  customProcessors: Array<NgDocPageProcessor<unknown>> =\n    inject<Array<NgDocPageProcessor<unknown>>>(NG_DOC_PAGE_CUSTOM_PROCESSOR, { optional: true }) ??\n    [];\n\n  protected readonly elementRef: ElementRef<HTMLElement> = inject(ElementRef);\n  protected readonly viewContainerRef: ViewContainerRef = inject(ViewContainerRef);\n  protected readonly applicationRef = inject(ApplicationRef);\n  protected readonly injector: Injector = inject(Injector);\n  protected readonly renderer: Renderer2 = inject(Renderer2);\n\n  ngOnChanges({ html }: SimpleChanges): void {\n    if (html) {\n      Promise.resolve().then(() => {\n        asArray(this.processors, this.customProcessors).forEach(this.process.bind(this));\n        this.applicationRef.tick();\n        this.afterRender.emit();\n      });\n    }\n  }\n\n  private process<T>(processor: NgDocPageProcessor<T>): void {\n    Array.from(this.elementRef.nativeElement.querySelectorAll(processor.selector)).forEach(\n      (elementNode: Element) => {\n        // check if element node has a parent node because it can be removed by another processor\n        if (elementNode.parentNode) {\n          const replaceElement: Element =\n            (processor.nodeToReplace && processor.nodeToReplace(elementNode, this.injector)) ??\n            elementNode;\n          const options: NgDocProcessorOptions<T> = processor.extractOptions(\n            elementNode,\n            this.elementRef.nativeElement,\n          );\n\n          // create component\n          const componentRef: ComponentRef<T> = this.viewContainerRef.createComponent(\n            processor.component,\n            {\n              projectableNodes: options.content,\n              injector: this.injector,\n            },\n          );\n\n          // set component options\n          if (options.inputs) {\n            objectKeys(options.inputs).forEach(\n              (key: keyof T) =>\n                options.inputs && componentRef.setInput(key as string, options.inputs[key]),\n            );\n          }\n\n          // replace element node with component node\n          replaceElement.parentNode?.replaceChild(\n            componentRef.location.nativeElement,\n            replaceElement,\n          );\n\n          componentRef.changeDetectorRef.markForCheck();\n        }\n      },\n    );\n  }\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AAuBA;;;AAGG;MAOU,2BAA2B,CAAA;AANxC,IAAA,WAAA,GAAA;QASE,IAAA,CAAA,IAAI,GAAa,EAAE;AAGnB,QAAA,IAAA,CAAA,WAAW,GAAuB,IAAI,YAAY,EAAQ;AAE1D,QAAA,IAAA,CAAA,UAAU,GACR,MAAM,CAAqC,qBAAqB,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE;QAC7F,IAAA,CAAA,gBAAgB,GACd,MAAM,CAAqC,4BAA4B,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;AAC5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