Skip to content

Commit 5c7a358

Browse files
Merge pull request #39 from MicrosoftEdge/svg-clipboard
New SVG clipboard demo
2 parents 1a5c5b3 + 262c2e7 commit 5c7a358

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ last sync'd April 16, 2024
8686
| Reader app | An article reader app used to demonstrate how to use various web APIs such as CSS Custom Highlight, `<selectlist>`, EyeDropper, CSS and JSON modules, Scroll animation timeline, and Async Clipboard. | [/reader/](https://github.com/MicrosoftEdge/Demos/tree/main/reader) | [Reader](https://microsoftedge.github.io/Demos/reader/) |
8787
| Selectlist demos | Demo page showing how the Open UI's `<selectlist>` element can be used. | [/selectlist/](https://github.com/MicrosoftEdge/Demos/tree/main/selectlist) | [Open UI's \<selectlist\> demos](https://microsoftedge.github.io/Demos/selectlist/) |
8888
| EditContext API demo | Demo page showing how the EditContext API can be used to build an advanced text editor. | [/edit-context/](https://github.com/MicrosoftEdge/Demos/tree/main/edit-context) | [HTML editor demo](https://microsoftedge.github.io/Demos/edit-context/) |
89+
| SVG support in the Async Clipboard API | Demo page showing how the Async Clipboard API supports SVG format. | [/svg-clipboard/](https://github.com/MicrosoftEdge/Demos/tree/main/svg-clipboard) | [SVG clipbard support demo](https://microsoftedge.github.io/Demos/svg-clipboard/) |
8990

9091

9192
## Adding a new demo

svg-clipboard/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SVG support in the Async Clipboard API
2+
3+
➡️ **[Open the demo](https://microsoftedge.github.io/Demos/svg-clipboard/)** ⬅️
4+
5+
This demo showcases the support for SVG format in the Async Clipboard API.

svg-clipboard/edge.svg

+1
Loading

svg-clipboard/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<title>SVG clipbard support demo</title>
8+
<link rel="icon" type="image/png" href="https://edgestatic.azureedge.net/welcome/static/favicon.png">
9+
<link rel="stylesheet" href="style.css" />
10+
<script src="script.js" type="module" defer></script>
11+
</head>
12+
13+
<body>
14+
<h1>Support for the SVG format in the Async Clipboard API</h1>
15+
<p>This page demos support for the SVG format in the Async Clipboard API. Support for SVG makes it possible for web
16+
apps to participate in SVG file copy/paste operations with other web and native apps, such as:</p>
17+
<ul>
18+
<li>Copying SVG content from a webpage to a native app like PowerPoint.</li>
19+
<li>Copying SVG content from a native app like the Windows Explorer to a web app.</li>
20+
<li>Copying SVG content between two web apps.</li>
21+
</ul>
22+
23+
<p class="support-notice">Your browser supports SVG format in the Async Clipboard API.</p>
24+
25+
<div class="test copy">
26+
<h2>Copy</h2>
27+
<p>Click the <strong>Copy</strong> button below to write the SVG content to the system clipboard.</p>
28+
<button type="button">Copy ✂️</button>
29+
<div class="svg-area"></div>
30+
</div>
31+
32+
<div class="test paste">
33+
<h2>Paste</h2>
34+
<p>Click the <strong>Paste</strong> button below to paste SVG content you copied from another app into the system clipboard, and display the image below.</p>
35+
<button type="button">Paste 📋</button>
36+
<div class="svg-area"></div>
37+
</div>
38+
</body>
39+
40+
</html>

svg-clipboard/script.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const copyEl = document.querySelector(".copy");
2+
const copyButton = copyEl.querySelector("button");
3+
const copySvgArea = copyEl.querySelector(".svg-area");
4+
5+
const pasteEl = document.querySelector(".paste");
6+
const pasteButton = pasteEl.querySelector("button");
7+
const pasteSvgArea = pasteEl.querySelector(".svg-area");
8+
9+
const supportNotice = document.querySelector(".support-notice");
10+
11+
// Start by loading the edge.svg image and displaying it in the copy area.
12+
fetch("edge.svg")
13+
.then((response) => response.text())
14+
.then((text) => (copySvgArea.innerHTML = text));
15+
16+
// Update the support notice based on the browser's capabilities.
17+
const supportsSVG = ("supports" in window.ClipboardItem) && window.ClipboardItem.supports("image/svg+xml");
18+
if (!supportsSVG) {
19+
supportNotice.classList.add("no-support");
20+
supportNotice.textContent = "Your browser does not support reading or writing SVG to the clipboard.";
21+
}
22+
23+
copyButton.addEventListener("click", async () => {
24+
if (!supportsSVG) {
25+
return;
26+
}
27+
28+
// Get the SVG image as a Blob.
29+
const response = await fetch("edge.svg");
30+
const blob = await response.blob();
31+
32+
// Store the SVG content in the clipboard.
33+
await navigator.clipboard.write([
34+
new window.ClipboardItem({
35+
[blob.type]: blob
36+
}),
37+
]);
38+
});
39+
40+
pasteButton.addEventListener("click", async () => {
41+
if (!supportsSVG) {
42+
return;
43+
}
44+
45+
// On paste, read the system clipboard.
46+
const [clipboardItem] = await navigator.clipboard.read();
47+
const svgBlob = await clipboardItem.getType("image/svg+xml");
48+
if (!svgBlob) {
49+
alert("No SVG in the clipboard.");
50+
return;
51+
}
52+
53+
// Getting the source code here, since :hover effects don't
54+
// work when the SVG is referenced as `<img src>`.
55+
const svgCode = await svgBlob.text();
56+
pasteSvgArea.innerHTML = svgCode;
57+
});

svg-clipboard/style.css

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
:root {
2+
color-scheme: dark light;
3+
}
4+
5+
body {
6+
margin: 2rem;
7+
font-family: system-ui, sans-serif;
8+
font-size: 1rem;
9+
}
10+
11+
h1, h2, p, ul {
12+
margin: 1rem 0;
13+
}
14+
15+
.test .svg-area {
16+
height: 20vh;
17+
}
18+
19+
.test .svg-area svg {
20+
height: 100%;
21+
}
22+
23+
.test button {
24+
display: block;
25+
padding: .5rem;
26+
margin: 1rem 0;
27+
}
28+
29+
.test .svg-area {
30+
border: 1rem dashed #eee;
31+
border-radius: .5rem;
32+
}
33+
34+
.support-notice {
35+
font-weight: bold;
36+
background: rgb(223, 247, 223);
37+
padding: .5rem;
38+
}
39+
40+
.support-notice.no-support {
41+
background: rgb(247, 223, 223);
42+
}

0 commit comments

Comments
 (0)