|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref, computed } from "vue"; |
| 3 | +import { Camera, X } from "lucide-vue-next"; |
| 4 | +import { Button } from "@/components/ui/button"; |
| 5 | +import { Label } from "@/components/ui/label"; |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardHeader, |
| 10 | + CardTitle, |
| 11 | +} from "@/components/ui/card"; |
| 12 | +import html2canvas from "html2canvas"; |
| 13 | +import type { NoteModel } from "./note-model"; |
| 14 | +
|
| 15 | +const props = defineProps<{ |
| 16 | + open: boolean; |
| 17 | + canvasElement: HTMLElement | null; |
| 18 | + selectedNoteIds: string[]; |
| 19 | + notes: { id: string; model: NoteModel }[]; |
| 20 | + zoom: number; |
| 21 | + camX: number; |
| 22 | + camY: number; |
| 23 | +}>(); |
| 24 | +
|
| 25 | +const emit = defineEmits<{ |
| 26 | + close: []; |
| 27 | +}>(); |
| 28 | +
|
| 29 | +const margin = ref(100); |
| 30 | +const scale = ref(100); |
| 31 | +
|
| 32 | +function handleClose() { |
| 33 | + emit("close"); |
| 34 | +} |
| 35 | +
|
| 36 | +const selectedNotes = computed(() => |
| 37 | + props.notes.filter((n) => props.selectedNoteIds.includes(n.id)), |
| 38 | +); |
| 39 | +
|
| 40 | +function getNoteBounds() { |
| 41 | + if (selectedNotes.value.length === 0) return null; |
| 42 | +
|
| 43 | + let minX = Infinity; |
| 44 | + let minY = Infinity; |
| 45 | + let maxX = -Infinity; |
| 46 | + let maxY = -Infinity; |
| 47 | +
|
| 48 | + for (const note of selectedNotes.value) { |
| 49 | + const x = note.model.pos.value.x; |
| 50 | + const y = note.model.pos.value.y; |
| 51 | +
|
| 52 | + let w = 200; |
| 53 | + const wStr = note.model.width.value.expanded; |
| 54 | + if (wStr !== "Auto" && wStr.endsWith("px")) { |
| 55 | + w = parseInt(wStr, 10); |
| 56 | + } |
| 57 | +
|
| 58 | + let h = 80; |
| 59 | + if (note.model.head.enabled.value) { |
| 60 | + const hStr = note.model.head.height.value.expanded; |
| 61 | + if (hStr !== "Auto" && hStr.endsWith("px")) { |
| 62 | + h = parseInt(hStr, 10); |
| 63 | + } else { |
| 64 | + h = 40; |
| 65 | + } |
| 66 | + } |
| 67 | + if (note.model.body.enabled.value) { |
| 68 | + const bStr = note.model.body.height.value.expanded; |
| 69 | + if (bStr !== "Auto" && bStr.endsWith("px")) { |
| 70 | + h += parseInt(bStr, 10); |
| 71 | + } else { |
| 72 | + h += 40; |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | + minX = Math.min(minX, x); |
| 77 | + minY = Math.min(minY, y); |
| 78 | + maxX = Math.max(maxX, x + w); |
| 79 | + maxY = Math.max(maxY, y + h); |
| 80 | + } |
| 81 | +
|
| 82 | + return { minX, minY, maxX, maxY }; |
| 83 | +} |
| 84 | +
|
| 85 | +async function takeScreenshot() { |
| 86 | + if (!props.canvasElement) return; |
| 87 | +
|
| 88 | + const bounds = getNoteBounds(); |
| 89 | + const rect = props.canvasElement.getBoundingClientRect(); |
| 90 | + const cx = rect.left + rect.width / 2; |
| 91 | + const cy = rect.top + rect.height / 2; |
| 92 | +
|
| 93 | + let worldMinX: number; |
| 94 | + let worldMinY: number; |
| 95 | + let worldMaxX: number; |
| 96 | + let worldMaxY: number; |
| 97 | +
|
| 98 | + if (bounds) { |
| 99 | + worldMinX = bounds.minX; |
| 100 | + worldMinY = bounds.minY; |
| 101 | + worldMaxX = bounds.maxX; |
| 102 | + worldMaxY = bounds.maxY; |
| 103 | + } else { |
| 104 | + // Capture visible viewport in world coordinates |
| 105 | + worldMinX = props.camX - rect.width / 2 / props.zoom; |
| 106 | + worldMinY = props.camY - rect.height / 2 / props.zoom; |
| 107 | + worldMaxX = props.camX + rect.width / 2 / props.zoom; |
| 108 | + worldMaxY = props.camY + rect.height / 2 / props.zoom; |
| 109 | + } |
| 110 | +
|
| 111 | + const m = margin.value; |
| 112 | + const z = props.zoom; |
| 113 | +
|
| 114 | + // Convert world bounds to screen coordinates relative to canvas element |
| 115 | + const screenMinX = (worldMinX - props.camX) * z + cx - rect.left; |
| 116 | + const screenMinY = (worldMinY - props.camY) * z + cy - rect.top; |
| 117 | + const screenMaxX = (worldMaxX - props.camX) * z + cx - rect.left; |
| 118 | + const screenMaxY = (worldMaxY - props.camY) * z + cy - rect.top; |
| 119 | +
|
| 120 | + const captureX = screenMinX - m * z; |
| 121 | + const captureY = screenMinY - m * z; |
| 122 | + const captureWidth = screenMaxX - screenMinX + m * z * 2; |
| 123 | + const captureHeight = screenMaxY - screenMinY + m * z * 2; |
| 124 | +
|
| 125 | + const canvas = await html2canvas(props.canvasElement, { |
| 126 | + scale: (1 / z) * (scale.value / 100), |
| 127 | + x: captureX, |
| 128 | + y: captureY, |
| 129 | + width: Math.round(captureWidth), |
| 130 | + height: Math.round(captureHeight), |
| 131 | + useCORS: true, |
| 132 | + allowTaint: true, |
| 133 | + backgroundColor: null, |
| 134 | + }); |
| 135 | +
|
| 136 | + const dataUrl = canvas.toDataURL("image/png"); |
| 137 | +
|
| 138 | + const link = document.createElement("a"); |
| 139 | + link.href = dataUrl; |
| 140 | + link.download = "DeepNotes-screenshot.png"; |
| 141 | + document.body.appendChild(link); |
| 142 | + link.click(); |
| 143 | + document.body.removeChild(link); |
| 144 | +
|
| 145 | + handleClose(); |
| 146 | +} |
| 147 | +</script> |
| 148 | + |
| 149 | +<template> |
| 150 | + <Teleport to="body"> |
| 151 | + <div |
| 152 | + v-if="open" |
| 153 | + class="fixed inset-0 z-50 flex items-center justify-center bg-black/50" |
| 154 | + @click.self="handleClose" |
| 155 | + > |
| 156 | + <Card class="w-full max-w-sm"> |
| 157 | + <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-4"> |
| 158 | + <CardTitle>Take Screenshot</CardTitle> |
| 159 | + <Button variant="ghost" size="icon" @click="handleClose"> |
| 160 | + <X class="h-4 w-4" /> |
| 161 | + </Button> |
| 162 | + </CardHeader> |
| 163 | + |
| 164 | + <CardContent class="space-y-4"> |
| 165 | + <div class="space-y-2"> |
| 166 | + <Label>Margin (px)</Label> |
| 167 | + <input |
| 168 | + v-model.number="margin" |
| 169 | + type="number" |
| 170 | + min="0" |
| 171 | + data-testid="screenshot-margin" |
| 172 | + class="border-input focus-visible:border-ring focus-visible:ring-ring/50 h-8 w-full rounded-lg border bg-transparent px-2.5 py-1 text-base transition-colors focus-visible:ring-3 md:text-sm min-w-0 outline-none" |
| 173 | + /> |
| 174 | + </div> |
| 175 | + |
| 176 | + <div class="space-y-2"> |
| 177 | + <Label>Scale (%)</Label> |
| 178 | + <input |
| 179 | + v-model.number="scale" |
| 180 | + type="number" |
| 181 | + min="10" |
| 182 | + max="500" |
| 183 | + data-testid="screenshot-scale" |
| 184 | + class="border-input focus-visible:border-ring focus-visible:ring-ring/50 h-8 w-full rounded-lg border bg-transparent px-2.5 py-1 text-base transition-colors focus-visible:ring-3 md:text-sm min-w-0 outline-none" |
| 185 | + /> |
| 186 | + </div> |
| 187 | + |
| 188 | + <div class="flex justify-end gap-2"> |
| 189 | + <Button variant="outline" @click="handleClose">Cancel</Button> |
| 190 | + <Button @click="takeScreenshot"> |
| 191 | + <Camera class="mr-2 h-4 w-4" /> |
| 192 | + Download |
| 193 | + </Button> |
| 194 | + </div> |
| 195 | + </CardContent> |
| 196 | + </Card> |
| 197 | + </div> |
| 198 | + </Teleport> |
| 199 | +</template> |
0 commit comments