Skip to content

Instantly share code, notes, and snippets.

@simonw
Created October 21, 2024 04:16
Show Gist options
  • Save simonw/5393dd81fcabc9f854e8bbec205e7e1e to your computer and use it in GitHub Desktop.
Save simonw/5393dd81fcabc9f854e8bbec205e7e1e to your computer and use it in GitHub Desktop.

Clipboard Viewer: Explore Paste Formats

human (Oct 15, 2024, 04:35 PM)

Build an artifact, no React, where I can hit paste (anywhere on the page, or after focusing on a small textarea with a placeholder that says "paste here") and it then shows me a detailed view of ALL available paste formats - the HTML format and the text format and so on, really iterate and go to town on that API

assistant (Oct 15, 2024, 04:35 PM)

Certainly! I'll create an HTML artifact that allows you to paste content anywhere on the page or in a specific textarea, and then displays detailed information about all available paste formats. This will use the Clipboard API to access the clipboard data and show various formats.

Clipboard Format Viewer

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clipboard Format Viewer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f0f0f0;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        #paste-area {
            width: 100%;
            height: 100px;
            margin-bottom: 20px;
            padding: 10px;
            border: 2px dashed #aaa;
            border-radius: 5px;
            resize: vertical;
        }
        #output {
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 20px;
        }
        .format {
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .format h2 {
            color: #0066cc;
            margin-bottom: 10px;
        }
        pre {
            background-color: #f5f5f5;
            border: 1px solid #ddd;
            border-radius: 3px;
            padding: 10px;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
    </style>
</head>
<body>
    <h1>Clipboard Format Viewer</h1>
    <textarea id="paste-area" placeholder="Paste here or anywhere on the page"></textarea>
    <div id="output"></div>

    <script>
        const pasteArea = document.getElementById('paste-area');
        const output = document.getElementById('output');

        function handlePaste(event) {
            event.preventDefault();
            const clipboardData = event.clipboardData || window.clipboardData;
            output.innerHTML = '';

            // Get available formats
            const formats = clipboardData.types;
            
            formats.forEach(format => {
                const formatDiv = document.createElement('div');
                formatDiv.className = 'format';
                
                const formatTitle = document.createElement('h2');
                formatTitle.textContent = format;
                formatDiv.appendChild(formatTitle);

                const formatContent = document.createElement('pre');
                let content = clipboardData.getData(format);
                
                // Special handling for certain formats
                if (format === 'text/html') {
                    const tempElement = document.createElement('div');
                    tempElement.innerHTML = content;
                    content = tempElement.innerHTML;
                } else if (format === 'Files') {
                    const files = clipboardData.files;
                    content = Array.from(files).map(file => `${file.name} (${file.type}, ${file.size} bytes)`).join('\n');
                }

                formatContent.textContent = content;
                formatDiv.appendChild(formatContent);

                output.appendChild(formatDiv);
            });

            // Additional information about the clipboard event
            const eventInfo = document.createElement('div');
            eventInfo.className = 'format';
            eventInfo.innerHTML = `
                <h2>Clipboard Event Information</h2>
                <pre>
Event type: ${event.type}
Formats available: ${formats.join(', ')}
                </pre>
            `;
            output.appendChild(eventInfo);
        }

        // Listen for paste events on the textarea
        pasteArea.addEventListener('paste', handlePaste);

        // Listen for paste events on the entire document
        document.addEventListener('paste', handlePaste);
    </script>
</body>
</html>

I've created an HTML artifact that implements a Clipboard Format Viewer. Here's a breakdown of its features:

  1. You can paste content either in the textarea or anywhere on the page.
  2. The viewer displays detailed information about all available paste formats.
  3. Each format is shown in its own section with a title and pre-formatted content.
  4. Special handling is implemented for HTML content and file lists.
  5. The page also displays general information about the clipboard event.

To use this viewer:

  1. Copy any content to your clipboard.
  2. Open the HTML file in a web browser.
  3. Click anywhere on the page or focus on the textarea.
  4. Paste your content (usually Ctrl+V or Cmd+V).
  5. The page will display detailed information about all available formats in the pasted content.

This implementation uses the Clipboard API to access the clipboard data and display various formats. It's designed to be informative and easy to use, allowing you to explore the different data types available in your clipboard.

Let me know if you'd like any modifications or if you have any questions about the implementation!

human (Oct 15, 2024, 04:36 PM)

make each of the display areas have a fixed height and be scrollable within that height

assistant (Oct 15, 2024, 04:36 PM)

Certainly! I'll modify the HTML artifact to make each format display area have a fixed height and be scrollable within that height. This will make the page more compact and easier to navigate, especially when dealing with large amounts of pasted data.

Clipboard Format Viewer with Scrollable Sections

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clipboard Format Viewer</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            background-color: #f0f0f0;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        #paste-area {
            width: 100%;
            height: 100px;
            margin-bottom: 20px;
            padding: 10px;
            border: 2px dashed #aaa;
            border-radius: 5px;
            resize: vertical;
        }
        #output {
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 20px;
            max-height: 70vh;
            overflow-y: auto;
        }
        .format {
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .format h2 {
            color: #0066cc;
            margin-bottom: 10px;
        }
        .format-content {
            max-height: 200px;
            overflow-y: auto;
            background-color: #f5f5f5;
            border: 1px solid #ddd;
            border-radius: 3px;
            padding: 10px;
        }
        pre {
            margin: 0;
            white-space: pre-wrap;
            word-wrap: break-word;
        }
    </style>
</head>
<body>
    <h1>Clipboard Format Viewer</h1>
    <textarea id="paste-area" placeholder="Paste here or anywhere on the page"></textarea>
    <div id="output"></div>

    <script>
        const pasteArea = document.getElementById('paste-area');
        const output = document.getElementById('output');

        function handlePaste(event) {
            event.preventDefault();
            const clipboardData = event.clipboardData || window.clipboardData;
            output.innerHTML = '';

            // Get available formats
            const formats = clipboardData.types;
            
            formats.forEach(format => {
                const formatDiv = document.createElement('div');
                formatDiv.className = 'format';
                
                const formatTitle = document.createElement('h2');
                formatTitle.textContent = format;
                formatDiv.appendChild(formatTitle);

                const formatContentWrapper = document.createElement('div');
                formatContentWrapper.className = 'format-content';
                
                const formatContent = document.createElement('pre');
                let content = clipboardData.getData(format);
                
                // Special handling for certain formats
                if (format === 'text/html') {
                    const tempElement = document.createElement('div');
                    tempElement.innerHTML = content;
                    content = tempElement.innerHTML;
                } else if (format === 'Files') {
                    const files = clipboardData.files;
                    content = Array.from(files).map(file => `${file.name} (${file.type}, ${file.size} bytes)`).join('\n');
                }

                formatContent.textContent = content;
                formatContentWrapper.appendChild(formatContent);
                formatDiv.appendChild(formatContentWrapper);

                output.appendChild(formatDiv);
            });

            // Additional information about the clipboard event
            const eventInfo = document.createElement('div');
            eventInfo.className = 'format';
            eventInfo.innerHTML = `
                <h2>Clipboard Event Information</h2>
                <div class="format-content">
                    <pre>
Event type: ${event.type}
Formats available: ${formats.join(', ')}
                    </pre>
                </div>
            `;
            output.appendChild(eventInfo);
        }

        // Listen for paste events on the textarea
        pasteArea.addEventListener('paste', handlePaste);

        // Listen for paste events on the entire document
        document.addEventListener('paste', handlePaste);
    </script>
</body>
</html>

I've updated the Clipboard Format Viewer to make each format display area have a fixed height and be scrollable within that height. Here are the key changes:

  1. Added a new format-content class with the following properties:

    • max-height: 200px; to limit the height of each format's content area.
    • overflow-y: auto; to make it scrollable when content exceeds the max height.
  2. Wrapped the <pre> element for each format in a new <div> with the format-content class.

  3. Modified the #output container to have a maximum height of 70% of the viewport height (max-height: 70vh;) and made it scrollable (overflow-y: auto;). This ensures that the entire output area doesn't grow indefinitely on the page.

  4. Adjusted some styles to improve the overall appearance and consistency.

These changes will make the page more compact and easier to navigate, especially when dealing with large amounts of pasted data. Each format's content will now be contained within a scrollable area with a fixed height of 200 pixels. If the content is longer, users can scroll within that specific format's box.

The overall output container is also now limited to 70% of the viewport height, ensuring that it doesn't take up too much space on the screen. Users can scroll through the entire list of formats if there are many of them.

You can use this updated version in the same way as before:

  1. Copy any content to your clipboard.
  2. Open the HTML file in a web browser.
  3. Click anywhere on the page or focus on the textarea.
  4. Paste your content (usually Ctrl+V or Cmd+V).
  5. The page will display detailed information about all available formats in the pasted content, now with scrollable sections for better organization.

Let me know if you'd like any further modifications or if you have any questions about the implementation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment