Skip to content

Commit 33f2e9e

Browse files
committed
Add option for including the cleaned rich HTML
1 parent 0aae498 commit 33f2e9e

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

paste-html-subset.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@
7272
margin-bottom: 20px;
7373
}
7474

75+
label {
76+
margin-right: 10px;
77+
margin-bottom: 20px;
78+
}
79+
7580
.button:hover {
7681
background-color: #2a9038;
7782
}
@@ -134,6 +139,10 @@ <h3>Supported HTML elements</h3>
134139
<h2>Clean HTML code</h2>
135140
<textarea id="html-output" readonly></textarea>
136141
<button id="copy-button" class="button">Copy HTML</button>
142+
<label title="Useful if pasting into rich text editor, as also writes the cleaned text/html payload to the clipboard. Doesn't make a difference if pasting into a plain text editor.">
143+
<input type="checkbox" id="include-rich-html" checked >
144+
Include Cleaned Rich HTML
145+
</label>
137146
<button id="clear-button" class="button">Clear all</button>
138147

139148
<div id="preview-container">
@@ -279,6 +288,7 @@ <h2>Preview</h2>
279288
const htmlOutput = document.getElementById('html-output');
280289
const previewContent = document.getElementById('preview-content');
281290
const copyButton = document.getElementById('copy-button');
291+
const includeRichHtmlCheckbox = document.getElementById('include-rich-html');
282292
const clearButton = document.getElementById('clear-button');
283293

284294
// Handle paste event
@@ -353,19 +363,26 @@ <h2>Preview</h2>
353363
pasteArea.innerHTML = '';
354364
}
355365
});
356-
357-
// Copy HTML to clipboard
358-
copyButton.addEventListener('click', function() {
359-
htmlOutput.select();
360-
document.execCommand('copy');
361366

367+
// Copy HTML to clipboard
368+
copyButton.addEventListener('click', async function() {
369+
const content = htmlOutput.value;
370+
const items = {
371+
'text/plain': new Blob([content], { type: 'text/plain' }),
372+
};
373+
if (includeRichHtmlCheckbox.checked) {
374+
items['text/html'] = new Blob([content], { type: 'text/html' });
375+
}
376+
const cpItem = new ClipboardItem(items);
377+
await navigator.clipboard.write([cpItem]);
378+
362379
const originalText = copyButton.textContent;
363380
copyButton.textContent = 'Copied!';
364-
365381
setTimeout(function() {
366382
copyButton.textContent = originalText;
367383
}, 1500);
368384
});
385+
369386

370387
// Clear all content
371388
clearButton.addEventListener('click', function() {

0 commit comments

Comments
 (0)