Skip to content

Commit cbf9e81

Browse files
authored
Merge pull request #12 from ppp-one/copy-to-clipboard
Copy to clipboard
2 parents 8e493b4 + c707322 commit cbf9e81

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to the "simple-fits-viewer" extension will be documented in
44

55
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
66

7+
## [0.1.1] - 2025-07-28
8+
9+
### Added
10+
11+
- Added copy image to clipboard functionality
12+
713
## [0.1.0] - 2025-03-23
814

915
### Added

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "simple-fits-viewer",
33
"displayName": "Simple FITS viewer",
44
"description": "Preview FITS images in VSCode.",
5-
"version": "0.1.0",
5+
"version": "0.1.1",
66
"publisher": "ppp-one",
77
"author": "@ppp-one",
88
"homepage": "https://github.com/ppp-one/simple-fits-viewer",

webview.html

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -200,23 +200,58 @@
200200
contextMenu.style.borderRadius = "5px";
201201
contextMenu.style.zIndex = "1000";
202202

203-
// on hover
204-
contextMenu.addEventListener("mouseenter", () => {
205-
contextMenu.style.backgroundColor = "#ededed";
206-
});
203+
// Helper to add hover effect to menu options
204+
function addHoverEffect(optionDiv) {
205+
optionDiv.addEventListener("mouseenter", () => {
206+
optionDiv.style.backgroundColor = "#ededed";
207+
});
208+
optionDiv.addEventListener("mouseleave", () => {
209+
optionDiv.style.backgroundColor = "#fff";
210+
});
211+
}
207212

208-
contextMenu.addEventListener("mouseover", () => {
209-
contextMenu.style.backgroundColor = "#ededed";
213+
// Copy image to clipboard option
214+
const copyOption = document.createElement("div");
215+
copyOption.textContent = "Copy image to clipboard";
216+
copyOption.style.cursor = "pointer";
217+
copyOption.style.padding = "3px";
218+
addHoverEffect(copyOption);
219+
220+
copyOption.addEventListener("click", async () => {
221+
copyOption.textContent = "Copying...";
222+
try {
223+
// Use toBlob to get the image as a Blob
224+
offscreenCanvas.toBlob(async (blob) => {
225+
try {
226+
await navigator.clipboard.write([
227+
new ClipboardItem({
228+
"image/png": blob,
229+
}),
230+
]);
231+
copyOption.textContent = "Copied!";
232+
} catch (err) {
233+
copyOption.textContent = "Failed to copy";
234+
}
235+
setTimeout(() => {
236+
copyOption.textContent = "Copy image to clipboard";
237+
}, 1000);
238+
}, "image/png");
239+
} catch (err) {
240+
copyOption.textContent = "Failed to copy";
241+
setTimeout(() => {
242+
copyOption.textContent = "Copy image to clipboard";
243+
}, 1000);
244+
}
210245
});
211246

212-
contextMenu.addEventListener("mouseout", () => {
213-
contextMenu.style.backgroundColor = "#fff";
214-
});
247+
contextMenu.appendChild(copyOption);
215248

249+
// Save image as png option
216250
const saveOption = document.createElement("div");
217251
saveOption.textContent = "Save image as png";
218252
saveOption.style.cursor = "pointer";
219253
saveOption.style.padding = "3px";
254+
addHoverEffect(saveOption);
220255

221256
saveOption.addEventListener("click", () => {
222257
console.time("saveOption");

0 commit comments

Comments
 (0)