( node: AltNode<T>, excludeChildren: boolean, )
| 83 | }; |
| 84 | |
| 85 | export const exportNodeAsBase64PNG = async <T extends ExportableNode>( |
| 86 | node: AltNode<T>, |
| 87 | excludeChildren: boolean, |
| 88 | ) => { |
| 89 | // Shorcut export if the node has already been converted. |
| 90 | if (node.base64 !== undefined && node.base64 !== "") { |
| 91 | return node.base64; |
| 92 | } |
| 93 | |
| 94 | const n: ExportableNode = node; |
| 95 | |
| 96 | const temporarilyHideChildren = |
| 97 | excludeChildren && "children" in n && n.children.length > 0; |
| 98 | const parent = n as ChildrenMixin; |
| 99 | const originalVisibility = new Map<SceneNode, boolean>(); |
| 100 | |
| 101 | if (temporarilyHideChildren) { |
| 102 | // Store the original visible state of children |
| 103 | parent.children.map((child: SceneNode) => |
| 104 | originalVisibility.set(child, child.visible), |
| 105 | ), |
| 106 | // Temporarily hide all children |
| 107 | parent.children.forEach((child) => { |
| 108 | child.visible = false; |
| 109 | }); |
| 110 | } |
| 111 | |
| 112 | // export the image as bytes |
| 113 | const exportSettings: ExportSettingsImage = { |
| 114 | format: "PNG", |
| 115 | constraint: { type: "SCALE", value: 1 }, |
| 116 | }; |
| 117 | const bytes = await exportAsyncProxy(n, exportSettings); |
| 118 | |
| 119 | if (temporarilyHideChildren) { |
| 120 | // After export, restore visibility |
| 121 | parent.children.forEach((child) => { |
| 122 | child.visible = originalVisibility.get(child) ?? false; |
| 123 | }); |
| 124 | } |
| 125 | |
| 126 | addWarning("Some images exported as Base64 PNG"); |
| 127 | |
| 128 | // Encode binary string to base64 |
| 129 | const base64 = imageBytesToBase64(bytes); |
| 130 | // Save the value so it's only calculated once. |
| 131 | node.base64 = base64; |
| 132 | return base64; |
| 133 | }; |
no test coverage detected