Skip to content

feat: add button to show full code example #2334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 5, 2025
Merged

Conversation

harsha509
Copy link
Member

@harsha509 harsha509 commented Jun 5, 2025

User description

Description

add show example, inspired from PR #2276

Screenshot 2025-06-05 at 10 43 18 AM

Motivation and Context

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Enhancement


Description

  • Add "Show full example" button for code snippets with line fragments

    • Opens modal displaying the complete code file
    • Includes copy and close functionality in modal
    • Handles modal display, clipboard copy, and keyboard events
  • Improve user experience for viewing and copying full code examples


Changes walkthrough 📝

Relevant files
Enhancement
gh-codeblock.html
Add modal to display and copy full code example                   

website_and_docs/layouts/shortcodes/gh-codeblock.html

  • Add logic to detect line fragment in code path
  • Render "Show full example" button and modal for partial code blocks
  • Implement modal with copy and close buttons, styled overlay
  • Add JavaScript for modal control, clipboard copy, and keyboard
    shortcuts
  • +126/-29

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    qodo-merge-pro bot commented Jun 5, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Event Handling

    The click event listener on line 130 might not work as expected for all modal elements. It only checks if the clicked element's ID starts with 'codeModal_', but this would only close the modal if the background overlay itself is clicked, not its children.

    document.addEventListener('click', function(e) {
        if (e.target.id && e.target.id.startsWith('codeModal_')) {
            const id = e.target.id.replace('codeModal_', '');
            closeCodeModal(id);
        }
    });
    Missing Event Parameter

    The copyCode function references 'event' on line 98 but doesn't declare it as a parameter, which could cause issues in strict mode or when called programmatically without an event context.

    const btn = event.target;
    const originalText = btn.textContent;

    Copy link

    netlify bot commented Jun 5, 2025

    Deploy Preview for selenium-dev ready!

    Name Link
    🔨 Latest commit 9e65921
    🔍 Latest deploy log https://app.netlify.com/projects/selenium-dev/deploys/6841c616787c7f0008d5ee72
    😎 Deploy Preview https://deploy-preview-2334--selenium-dev.netlify.app
    📱 Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify project configuration.

    Copy link
    Contributor

    qodo-merge-pro bot commented Jun 5, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix event handling
    Suggestion Impact:The commit implemented the suggestion by modifying both the function definition to accept the event parameter and updating the onclick handler to pass the event

    code diff:

    -                <button onclick="copyCode('{{ $uniqueId }}')" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
    +                <button onclick="copyCode('{{ $uniqueId }}', event)" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
                         Copy
                     </button>
                     <button onclick="closeCodeModal('{{ $uniqueId }}')" style="padding: 5px 10px; border: 1px solid #6c757d; background: #6c757d; color: white; border-radius: 4px; cursor: pointer;">
    @@ -89,7 +89,7 @@
         document.body.style.overflow = '';
     };
     
    -window.copyCode = window.copyCode || function(id) {
    +window.copyCode = window.copyCode || function(id, event) {

    The copyCode function uses the global event object which is deprecated and may
    not be available in all browsers. Pass the event as a parameter to ensure
    reliable access to the clicked button.

    website_and_docs/layouts/shortcodes/gh-codeblock.html [92-128]

    -window.copyCode = window.copyCode || function(id) {
    +window.copyCode = window.copyCode || function(id, event) {
         const codeElement = document.getElementById('codeContent_' + id);
         const code = codeElement.textContent;
    +    const btn = event.target;
    +    const originalText = btn.textContent;
         
         if (navigator.clipboard) {
             navigator.clipboard.writeText(code).then(() => {
    -            const btn = event.target;
    -            const originalText = btn.textContent;
                 btn.textContent = 'Copied!';
                 btn.style.background = '#28a745';
                 btn.style.borderColor = '#28a745';
                 setTimeout(() => {
                     btn.textContent = originalText;
                     btn.style.background = '#007bff';
                     btn.style.borderColor = '#007bff';
                 }, 2000);
             });
         } else {
             const textArea = document.createElement('textarea');
             textArea.value = code;
             document.body.appendChild(textArea);
             textArea.select();
             document.execCommand('copy');
             document.body.removeChild(textArea);
             
    -        const btn = event.target;
    -        const originalText = btn.textContent;
             btn.textContent = 'Copied!';
             btn.style.background = '#28a745';
             btn.style.borderColor = '#28a745';
             setTimeout(() => {
                 btn.textContent = originalText;
                 btn.style.background = '#007bff';
                 btn.style.borderColor = '#007bff';
             }, 2000);
         }
     };

    [Suggestion processed]

    Suggestion importance[1-10]: 7

    __

    Why: Valid improvement addressing the deprecated global event object by accepting an event parameter, enhancing browser compatibility and code reliability.

    Medium
    Pass event to function
    Suggestion Impact:The commit directly implemented the suggestion by updating both the onclick handler to pass the event parameter and the copyCode function definition to accept this parameter.

    code diff:

    -                <button onclick="copyCode('{{ $uniqueId }}')" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
    +                <button onclick="copyCode('{{ $uniqueId }}', event)" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
                         Copy
                     </button>
                     <button onclick="closeCodeModal('{{ $uniqueId }}')" style="padding: 5px 10px; border: 1px solid #6c757d; background: #6c757d; color: white; border-radius: 4px; cursor: pointer;">
    @@ -89,7 +89,7 @@
         document.body.style.overflow = '';
     };
     
    -window.copyCode = window.copyCode || function(id) {
    +window.copyCode = window.copyCode || function(id, event) {

    Update the onclick handler to pass the event object to the copyCode function to
    fix the event reference issue in the function implementation.

    website_and_docs/layouts/shortcodes/gh-codeblock.html [67-69]

    -<button onclick="copyCode('{{ $uniqueId }}')" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
    +<button onclick="copyCode('{{ $uniqueId }}', event)" style="margin-right: 10px; padding: 5px 10px; border: 1px solid #007bff; background: #007bff; color: white; border-radius: 4px; cursor: pointer;">
         Copy
     </button>

    [Suggestion processed]

    Suggestion importance[1-10]: 7

    __

    Why: Necessary complementary change to suggestion 2, properly passing the event object to the copyCode function for reliable event handling.

    Medium
    Possible issue
    Fix modal click behavior

    The click event listener will only close the modal when clicking directly on the
    modal element, not its children. Modify the event listener to check if the
    clicked element is the modal background (the outer div) to ensure users can
    click anywhere outside the content area to dismiss the modal.

    website_and_docs/layouts/shortcodes/gh-codeblock.html [130-135]

     document.addEventListener('click', function(e) {
         if (e.target.id && e.target.id.startsWith('codeModal_')) {
    -        const id = e.target.id.replace('codeModal_', '');
    -        closeCodeModal(id);
    +        // Only close if clicking the background (not the modal content)
    +        if (e.target === document.getElementById(e.target.id)) {
    +            const id = e.target.id.replace('codeModal_', '');
    +            closeCodeModal(id);
    +        }
         }
     });
    • Apply / Chat
    Suggestion importance[1-10]: 6

    __

    Why: The suggestion correctly identifies a UX issue where clicking anywhere on a modal element closes it, rather than only closing when clicking the background. This could lead to unexpected modal closures when users interact with modal content.

    Low
    Prevent unwanted modal closing

    The modal content doesn't prevent event propagation, which means clicks inside
    the modal will bubble up to the background and close the modal. Add a click
    handler to the modal content div to stop event propagation.

    website_and_docs/layouts/shortcodes/gh-codeblock.html [62-63]

     <div id="codeModal_{{ $uniqueId }}" style="display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1050;">
    -    <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; border-radius: 8px; max-width: 90%; max-height: 90%; overflow: auto; box-shadow: 0 4px 20px rgba(0,0,0,0.3);">
    +    <div style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; border-radius: 8px; max-width: 90%; max-height: 90%; overflow: auto; box-shadow: 0 4px 20px rgba(0,0,0,0.3);" onclick="event.stopPropagation();">
    • Apply / Chat
    Suggestion importance[1-10]: 6

    __

    Why: This suggestion complements the first one by preventing event propagation from modal content to the background. It addresses the same modal interaction issue and provides a valid solution to prevent unintended modal closures.

    Low
    General
    Add clipboard error handling

    The document.execCommand('copy') method is deprecated. While you have a fallback
    mechanism, it's better to provide user feedback if copying fails. Add error
    handling to inform users when the clipboard operation fails.

    website_and_docs/layouts/shortcodes/gh-codeblock.html [114]

    -document.execCommand('copy');
    +try {
    +    document.execCommand('copy');
    +} catch (err) {
    +    alert('Copy failed. Please try again or copy the code manually.');
    +    console.error('Copy failed:', err);
    +    return;
    +}
    • Apply / Chat
    Suggestion importance[1-10]: 4

    __

    Why: While document.execCommand('copy') is deprecated, this suggestion adds error handling for the fallback scenario when navigator.clipboard is unavailable. This is a minor improvement that enhances robustness but doesn't address a critical issue.

    Low
    • Update

    @harsha509 harsha509 requested a review from titusfortner June 5, 2025 15:16
    Copy link
    Member

    @diemol diemol left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Thank you!

    @diemol diemol merged commit 2fc8f70 into trunk Jun 5, 2025
    5 of 6 checks passed
    @diemol diemol deleted the feat/show-code-sample branch June 5, 2025 16:30
    selenium-ci added a commit that referenced this pull request Jun 5, 2025
    * feat: add button to show full code example
    
    * fix: apply bot  code suggestions
    
    * set border style for buttons
    
    ---------
    
    [deploy site]
    
    Co-authored-by: Diego Molina <diemol@users.noreply.github.com> 2fc8f70
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants