Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/web/public/r/forme/utils.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
{
"path": "registry/lib/pdf-renderer.ts",
"content": "import {\n DOCUMENT,\n ElementRef,\n RendererFactory2,\n createComponent,\n createEnvironmentInjector,\n inject,\n platformCore,\n} from \"@angular/core\";\nimport type {\n EnvironmentInjector,\n Provider,\n Renderer2,\n Type,\n} from \"@angular/core\";\n\n/** An element in the tree an Angular component renders into. */\nexport class PdfElement {\n readonly name: string;\n readonly namespace: string | null;\n parent: PdfElement | null = null;\n readonly children: PdfNode[] = [];\n readonly attributes: Record<string, string> = {};\n /** The primitive directive on this element. Elements without one are transparent. */\n primitive: PdfPrimitive | null = null;\n\n constructor(name: string, namespace: string | null) {\n this.name = name;\n this.namespace = namespace;\n }\n\n /** Angular reads hydration markers through this in apps with client hydration. */\n getAttribute(name: string): string | null {\n return this.attributes[name] ?? null;\n }\n}\n\nexport class PdfTextNode {\n parent: PdfElement | null = null;\n\n value: string;\n\n constructor(value: string) {\n this.value = value;\n }\n}\n\n/** Angular's anchor for control flow and projection. Serializers skip it. */\nexport class PdfComment {\n parent: PdfElement | null = null;\n}\n\nexport type PdfNode = PdfElement | PdfTextNode | PdfComment;\n\n/** Base class for the directives that map to engine nodes. */\nexport abstract class PdfPrimitive {\n readonly element: PdfElement =\n inject<ElementRef<PdfElement>>(ElementRef).nativeElement;\n\n constructor() {\n this.element.primitive = this;\n }\n}\n\nconst detach = (node: PdfNode) => {\n if (node.parent) {\n node.parent.children.splice(node.parent.children.indexOf(node), 1);\n node.parent = null;\n }\n};\n\n// oxlint-disable-next-line no-empty-function -- Renderer2 hooks the PDF tree ignores\nconst noop = () => {};\n\nconst renderer: Renderer2 = {\n addClass: noop,\n appendChild(parent: PdfElement, child: PdfNode) {\n detach(child);\n child.parent = parent;\n parent.children.push(child);\n },\n createComment: () => new PdfComment(),\n createElement: (name: string, namespace?: string | null) =>\n new PdfElement(name, namespace ?? null),\n createText: (value: string) => new PdfTextNode(value),\n data: {},\n destroy: noop,\n destroyNode: null,\n insertBefore(parent: PdfElement, child: PdfNode, ref: PdfNode | null) {\n detach(child);\n child.parent = parent;\n const index = ref ? parent.children.indexOf(ref) : -1;\n parent.children.splice(\n index === -1 ? parent.children.length : index,\n 0,\n child\n );\n },\n listen: () => noop,\n nextSibling(node: PdfNode) {\n const siblings = node.parent?.children;\n return siblings?.[siblings.indexOf(node) + 1] ?? null;\n },\n parentNode: (node: PdfNode) => node.parent,\n removeAttribute(element: PdfElement, name: string) {\n Reflect.deleteProperty(element.attributes, name);\n },\n removeChild(_parent: PdfElement | null, child: PdfNode) {\n detach(child);\n },\n removeClass: noop,\n removeStyle: noop,\n selectRootElement: (node: PdfElement) => node,\n setAttribute(element: PdfElement, name: string, value: string) {\n element.attributes[name] = value;\n },\n setProperty: noop,\n setStyle: noop,\n setValue(node: PdfTextNode, value: string) {\n node.value = value;\n },\n};\n\nconst rendererFactory: RendererFactory2 = {\n createRenderer: () => renderer,\n};\n\nexport interface RenderOptions<T> {\n /** Values for the component's inputs. */\n inputs?: Partial<Record<keyof T & string, unknown>>;\n providers?: Provider[];\n}\n\n/** Renders a component once and reads its element tree before tearing it down. */\nexport const renderTree = <T, R>(\n component: Type<T>,\n { inputs = {}, providers = [] }: RenderOptions<T>,\n read: (root: PdfElement) => R\n): R => {\n const environmentInjector = createEnvironmentInjector(\n [\n { provide: RendererFactory2, useValue: rendererFactory },\n // Angular looks up a style host on creation. PDF components have no styles.\n { provide: DOCUMENT, useValue: { head: null } },\n ...providers,\n ],\n platformCore().injector as EnvironmentInjector\n );\n const ref = createComponent(component, { environmentInjector });\n try {\n for (const [name, value] of Object.entries(inputs)) {\n ref.setInput(name, value);\n }\n ref.changeDetectorRef.detectChanges();\n return read(ref.location.nativeElement as PdfElement);\n } finally {\n ref.destroy();\n environmentInjector.destroy();\n }\n};\n\n/** Elements without a primitive pass their children through, like component hosts. */\nexport const contentOf = (element: PdfElement): (PdfElement | PdfTextNode)[] =>\n element.children.flatMap((child) => {\n if (child instanceof PdfComment) {\n return [];\n }\n if (child instanceof PdfElement && !child.primitive && !child.namespace) {\n return contentOf(child);\n }\n return [child];\n });\n\n/** All text under a node, in document order. */\nexport const textOf = (node: PdfNode): string => {\n if (node instanceof PdfTextNode) {\n return node.value;\n }\n if (node instanceof PdfElement) {\n return node.children.map(textOf).join(\"\");\n }\n return \"\";\n};\n",
"content": "import {\n DOCUMENT,\n ElementRef,\n RendererFactory2,\n createComponent,\n createEnvironmentInjector,\n getPlatform,\n inject,\n platformCore,\n} from \"@angular/core\";\nimport type {\n EnvironmentInjector,\n Provider,\n Renderer2,\n Type,\n} from \"@angular/core\";\n\n/** An element in the tree an Angular component renders into. */\nexport class PdfElement {\n readonly name: string;\n readonly namespace: string | null;\n parent: PdfElement | null = null;\n readonly children: PdfNode[] = [];\n readonly attributes: Record<string, string> = {};\n /** The primitive directive on this element. Elements without one are transparent. */\n primitive: PdfPrimitive | null = null;\n\n constructor(name: string, namespace: string | null) {\n this.name = name;\n this.namespace = namespace;\n }\n\n /** Angular reads hydration markers through this in apps with client hydration. */\n getAttribute(name: string): string | null {\n return this.attributes[name] ?? null;\n }\n}\n\nexport class PdfTextNode {\n parent: PdfElement | null = null;\n\n value: string;\n\n constructor(value: string) {\n this.value = value;\n }\n}\n\n/** Angular's anchor for control flow and projection. Serializers skip it. */\nexport class PdfComment {\n parent: PdfElement | null = null;\n}\n\nexport type PdfNode = PdfElement | PdfTextNode | PdfComment;\n\n/** Base class for the directives that map to engine nodes. */\nexport abstract class PdfPrimitive {\n readonly element: PdfElement =\n inject<ElementRef<PdfElement>>(ElementRef).nativeElement;\n\n constructor() {\n this.element.primitive = this;\n }\n}\n\nconst detach = (node: PdfNode) => {\n if (node.parent) {\n node.parent.children.splice(node.parent.children.indexOf(node), 1);\n node.parent = null;\n }\n};\n\n// oxlint-disable-next-line no-empty-function -- Renderer2 hooks the PDF tree ignores\nconst noop = () => {};\n\nconst renderer: Renderer2 = {\n addClass: noop,\n appendChild(parent: PdfElement, child: PdfNode) {\n detach(child);\n child.parent = parent;\n parent.children.push(child);\n },\n createComment: () => new PdfComment(),\n createElement: (name: string, namespace?: string | null) =>\n new PdfElement(name, namespace ?? null),\n createText: (value: string) => new PdfTextNode(value),\n data: {},\n destroy: noop,\n destroyNode: null,\n insertBefore(parent: PdfElement, child: PdfNode, ref: PdfNode | null) {\n detach(child);\n child.parent = parent;\n const index = ref ? parent.children.indexOf(ref) : -1;\n parent.children.splice(\n index === -1 ? parent.children.length : index,\n 0,\n child\n );\n },\n listen: () => noop,\n nextSibling(node: PdfNode) {\n const siblings = node.parent?.children;\n return siblings?.[siblings.indexOf(node) + 1] ?? null;\n },\n parentNode: (node: PdfNode) => node.parent,\n removeAttribute(element: PdfElement, name: string) {\n Reflect.deleteProperty(element.attributes, name);\n },\n removeChild(_parent: PdfElement | null, child: PdfNode) {\n detach(child);\n },\n removeClass: noop,\n removeStyle: noop,\n selectRootElement: (node: PdfElement) => node,\n setAttribute(element: PdfElement, name: string, value: string) {\n element.attributes[name] = value;\n },\n setProperty: noop,\n setStyle: noop,\n setValue(node: PdfTextNode, value: string) {\n node.value = value;\n },\n};\n\nconst rendererFactory: RendererFactory2 = {\n createRenderer: () => renderer,\n};\n\nexport interface RenderOptions<T> {\n /** Values for the component's inputs. */\n inputs?: Partial<Record<keyof T & string, unknown>>;\n providers?: Provider[];\n}\n\n/** Renders a component once and reads its element tree before tearing it down. */\nexport const renderTree = <T, R>(\n component: Type<T>,\n { inputs = {}, providers = [] }: RenderOptions<T>,\n read: (root: PdfElement) => R\n): R => {\n const environmentInjector = createEnvironmentInjector(\n [\n { provide: RendererFactory2, useValue: rendererFactory },\n // Angular looks up a style host on creation. PDF components have no styles.\n { provide: DOCUMENT, useValue: { head: null } },\n ...providers,\n ],\n // bootstrapApplication's platform lacks the marker platformCore() asserts in dev mode.\n (getPlatform() ?? platformCore()).injector as EnvironmentInjector\n );\n const ref = createComponent(component, { environmentInjector });\n try {\n for (const [name, value] of Object.entries(inputs)) {\n ref.setInput(name, value);\n }\n ref.changeDetectorRef.detectChanges();\n return read(ref.location.nativeElement as PdfElement);\n } finally {\n ref.destroy();\n environmentInjector.destroy();\n }\n};\n\n/** Elements without a primitive pass their children through, like component hosts. */\nexport const contentOf = (element: PdfElement): (PdfElement | PdfTextNode)[] =>\n element.children.flatMap((child) => {\n if (child instanceof PdfComment) {\n return [];\n }\n if (child instanceof PdfElement && !child.primitive && !child.namespace) {\n return contentOf(child);\n }\n return [child];\n });\n\n/** All text under a node, in document order. */\nexport const textOf = (node: PdfNode): string => {\n if (node instanceof PdfTextNode) {\n return node.value;\n }\n if (node instanceof PdfElement) {\n return node.children.map(textOf).join(\"\");\n }\n return \"\";\n};\n",
"type": "registry:lib",
"target": "lib/pdf-renderer.ts"
},
Expand Down
Loading
Loading