All files / app/codeCharta/ui/screenshotButton screenshotButton.component.ts

97% Statements 97/100
56.52% Branches 13/23
80% Functions 12/15
96.87% Lines 93/96

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 1953x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x   3x 3x               3x       5x 5x 5x     5x 5x 5x 5x 5x       5x 5x 5x   5x     5x           1x 1x 1x   1x 1x       2x 1x   1x 1x 1x   1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x       2x 2x 2x 2x       2x 2x 2x 2x       2x 2x 2x   2x                   2x   2x 2x 2x 2x 2x   2x                           2x       2x 2x 2x   2x 2x   2x 2x 2x 2x   2x 600x 90000x 90000x 10000x 10000x 10000x 10000x         2x 2x   2x 2x   2x                       2x       5x           5x      
import { Component } from "@angular/core"
import { State, Store } from "@ngrx/store"
import hotkeys from "hotkeys-js"
import html2canvas from "html2canvas"
import { Color, WebGLRenderer } from "three"
import { checkWriteToClipboardAllowed, setToClipboard } from "../../../../app/codeCharta/util/clipboard/clipboardWriter"
import { CcState } from "../../codeCharta.model"
import { screenshotToClipboardEnabledSelector } from "../../state/store/appSettings/enableClipboard/screenshotToClipboardEnabled.selector"
import { ThreeCameraService } from "../codeMap/threeViewer/threeCamera.service"
import { ThreeRendererService } from "../codeMap/threeViewer/threeRenderer.service"
import { ThreeSceneService } from "../codeMap/threeViewer/threeSceneService"
import { createPNGFileName } from "../../model/files/files.helper"
import { FileState } from "../../model/files/files"
import { ActionIconComponent } from "../actionIcon/actionIcon.component"
import { AsyncPipe } from "@angular/common"
 
@Component({
    selector: "cc-screenshot-button",
    templateUrl: "./screenshotButton.component.html",
    standalone: true,
    imports: [ActionIconComponent, AsyncPipe]
})
export class ScreenshotButtonComponent {
    TITLE_FILE_BUTTON: string
    TITLE_CLIPBOARD_BUTTON: string
    isWriteToClipboardAllowed: boolean
    SCREENSHOT_HOTKEY_TO_FILE = "Ctrl+Alt+S"
    SCREENSHOT_HOTKEY_TO_CLIPBOARD = "Ctrl+Alt+F"
    isScreenshotToClipboardEnabled$ = this.store.select(screenshotToClipboardEnabledSelector)
 
    constructor(
        private threeCameraService: ThreeCameraService,
        private threeSceneService: ThreeSceneService,
        private threeRendererService: ThreeRendererService,
        private store: Store<CcState>,
        private state: State<CcState>
    ) {}
 
    ngOnInit() {
        this.isWriteToClipboardAllowed = checkWriteToClipboardAllowed()
        this.TITLE_CLIPBOARD_BUTTON = this.createTitleClipboardButton()
        this.TITLE_FILE_BUTTON = this.createTitleFileButton()
 
        hotkeys(this.SCREENSHOT_HOTKEY_TO_FILE, () => {
            this.makeScreenshotToFile()
        })
        hotkeys(this.SCREENSHOT_HOTKEY_TO_CLIPBOARD, () => {
            this.makeScreenshotToClipboard()
        })
    }
 
    async makeScreenshotToFile() {
        const renderer = this.threeRendererService.renderer
        const renderSettings = this.saveRenderSettings(renderer)
        const canvas = await this.buildScreenShotCanvas(renderer)
 
        this.downloadScreenshot(canvas, this.state.getValue().files)
        this.applyRenderSettings(renderer, renderSettings)
    }
 
    async makeScreenshotToClipboard() {
        if (!this.isWriteToClipboardAllowed) {
            return
        }
        const renderer = this.threeRendererService.renderer
        const renderSettings = this.saveRenderSettings(renderer)
        const canvas = await this.buildScreenShotCanvas(renderer)
 
        const canvasToBlobPromise: Promise<Blob> = new Promise(resolve => canvas.toBlob(resolve))
        this.applyRenderSettings(renderer, renderSettings)
        const blob = await canvasToBlobPromise
        await setToClipboard(blob)
    }
 
    private downloadScreenshot(canvas: HTMLCanvasElement, files: FileState[]) {
        const dataUrl = canvas.toDataURL("image/png")
        const downloadLink = document.createElement("a")
        downloadLink.download = createPNGFileName(files, "map")
        downloadLink.href = dataUrl
        document.body.appendChild(downloadLink)
        downloadLink.click()
        downloadLink.remove()
    }
 
    private saveRenderSettings(renderer: WebGLRenderer) {
        const pixelRatio = renderer.getPixelRatio()
        const clearColor = new Color()
        renderer.getClearColor(clearColor)
        return { pixelRatio, clearColor }
    }
 
    private applyRenderSettings(renderer: WebGLRenderer, settings: { pixelRatio: number; clearColor: Color }) {
        const { pixelRatio, clearColor } = settings
        renderer.setPixelRatio(pixelRatio)
        renderer.setClearColor(clearColor)
        renderer.render(this.threeSceneService.scene, this.threeCameraService.camera)
    }
 
    private async buildScreenShotCanvas(renderer: WebGLRenderer): Promise<HTMLCanvasElement> {
        renderer.setPixelRatio(window.devicePixelRatio)
        renderer.setClearColor(new Color(0, 0, 0), 0)
        renderer.render(this.threeSceneService.scene, this.threeCameraService.camera)
 
        const tagsNamesToIgnore = new Set([
            "cc-logo",
            "cc-tool-bar",
            "cc-view-cube",
            "cc-ribbon-bar",
            "cc-file-extension-bar",
            "cc-attribute-side-bar",
            "cc-loading-file-progess-spinner"
        ])
 
        const idsToIgnore = new Set(["legend-panel-button"])
 
        const bodyHeight = document.querySelector("body")?.offsetHeight
        const ribbonBarHeight = (document.querySelector("cc-ribbon-bar") as HTMLElement)?.offsetHeight
        const toolBarHeight = (document.querySelector("cc-tool-bar") as HTMLElement)?.offsetHeight
        const fileExtensionBarHeight = (document.querySelector("cc-file-extension-bar") as HTMLElement)?.offsetHeight
        const offsetMenuBar = ribbonBarHeight + toolBarHeight + fileExtensionBarHeight
 
        const canvas = await html2canvas(document.querySelector("body"), {
            removeContainer: true,
            backgroundColor: "#00",
            scrollY: -offsetMenuBar,
            height: bodyHeight - offsetMenuBar,
            ignoreElements(element) {
                return (
                    tagsNamesToIgnore.has(element.tagName.toLowerCase()) ||
                    idsToIgnore.has(element.id) ||
                    (element as HTMLElement).style.zIndex === "10000"
                )
            }
        })
 
        return this.getCroppedCanvas(canvas)
    }
 
    private getCroppedCanvas(canvas: HTMLCanvasElement) {
        const context = canvas.getContext("2d")
        const width = canvas.width
        const height = canvas.height
 
        const imageData = context.getImageData(0, 0, width, height)
        const data = imageData.data
 
        let minX = width,
            minY = height,
            maxX = 0,
            maxY = 0
 
        for (let x = 0; x < width; x++) {
            for (let y = 0; y < height; y++) {
                const alpha = data[(width * y + x) * 4 + 3]
                if (alpha > 0) {
                    minX = Math.min(minX, x)
                    maxX = Math.max(maxX, x)
                    minY = Math.min(minY, y)
                    maxY = Math.max(maxY, y)
                }
            }
        }
 
        const croppedCanvas = document.createElement("canvas")
        const croppedContext = croppedCanvas.getContext("2d")
 
        croppedCanvas.width = maxX - minX + 1
        croppedCanvas.height = maxY - minY + 1
 
        croppedContext.drawImage(
            canvas,
            minX,
            minY,
            croppedCanvas.width,
            croppedCanvas.height,
            0,
            0,
            croppedCanvas.width,
            croppedCanvas.height
        )
 
        return croppedCanvas
    }
 
    private createTitleClipboardButton() {
        return this.isWriteToClipboardAllowed
            ? `Take a screenshot of the map with ${this.SCREENSHOT_HOTKEY_TO_CLIPBOARD} (copy to clipboard) or ${this.SCREENSHOT_HOTKEY_TO_FILE} (save as file)`
            : "Firefox does not support copying to clipboard"
    }
 
    private createTitleFileButton() {
        return `Take a screenshot of the map with ${this.SCREENSHOT_HOTKEY_TO_FILE} (save as file) or ${this.SCREENSHOT_HOTKEY_TO_CLIPBOARD} (copy to clipboard)`
    }
}