Skip to content
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

fix: navigator clipboard is not support in devtool panel #3600

Open
wants to merge 6 commits into
base: canary
Choose a base branch
from

Conversation

komagic
Copy link

@komagic komagic commented Aug 4, 2024

Closes #

📝 Description

Add a brief description
navigator clipboard is not support in some envs, like in extension devtools panel.
document.execCommand is a better choice for compatibility

⛳️ Current behavior (updates)

Please describe the current behavior that you are modifying

🚀 New behavior

Please describe the behavior or changes this PR adds

💣 Is this a breaking change (Yes/No):

📝 Additional Information

Summary by CodeRabbit

  • New Features
    • Introduced a compatibility function for copying text to the clipboard, enhancing support for older browsers.
  • Improvements
    • Enhanced robustness of the clipboard copying functionality by ensuring fallback mechanisms are in place for environments lacking modern clipboard API support.

Copy link

changeset-bot bot commented Aug 4, 2024

🦋 Changeset detected

Latest commit: 165d24c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@nextui-org/use-clipboard Patch
@nextui-org/snippet Patch
@nextui-org/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Aug 4, 2024

@komagic is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Aug 4, 2024

Walkthrough

The changes introduce a new function, compatibilityCopy, to enhance the useClipboard hook by providing a fallback mechanism for copying text when the navigator.clipboard API is unavailable. This ensures broader compatibility with older browsers by using a temporary input element and the document.execCommand('copy') method. The operation logs the result and updates the state based on success or failure, improving usability across various environments.

Changes

Files Change Summary
packages/hooks/use-clipboard/src/index.ts Added compatibilityCopy function for fallback clipboard copying; modified copy function to utilize it when navigator.clipboard is unsupported.
.changeset/plenty-ties-yell.md Introduced compatibility fix for useClipboard, enhancing usability and consistency across environments.
.changeset/nervous-students-carry.md Added new method compatibilityCopy to improve clipboard compatibility in the @nextui-org/use-clipboard package.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ClipboardAPI
    participant CompatibilityLayer

    User->>ClipboardAPI: Trigger copy action
    alt Modern API available
        ClipboardAPI-->>User: Copy text using navigator.clipboard
    else Fallback needed
        CompatibilityLayer-->>User: Create temporary input element
        CompatibilityLayer->>CompatibilityLayer: Set input value to text
        CompatibilityLayer->>CompatibilityLayer: Execute document.execCommand('copy')
        alt Copy successful
            CompatibilityLayer-->>User: Update state to copied
        else Copy failed
            CompatibilityLayer-->>User: Log error and update state to not copied
        end
    end
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6d9995b and 9c7410c.

Files selected for processing (1)
  • packages/hooks/use-clipboard/src/index.ts (2 hunks)
Additional comments not posted (1)
packages/hooks/use-clipboard/src/index.ts (1)

39-39: Check the result of execCommand.

The execCommand('copy') method returns a boolean indicating whether the command was successful. Ensure the result is checked correctly.

  let result = document.execCommand('copy'); // 尝试复制
- setCopied(result);
+ handleCopyResult(result);

packages/hooks/use-clipboard/src/index.ts Show resolved Hide resolved
packages/hooks/use-clipboard/src/index.ts Outdated Show resolved Hide resolved
packages/hooks/use-clipboard/src/index.ts Outdated Show resolved Hide resolved
@komagic komagic force-pushed the feat-useclipboard-compatibility branch from 9c7410c to e1d2515 Compare August 4, 2024 02:37
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9c7410c and e1d2515.

Files selected for processing (1)
  • packages/hooks/use-clipboard/src/index.ts (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/hooks/use-clipboard/src/index.ts

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

  1. please add a changeset
  2. please lint your code
  3. please share how to reproduce the original issue
  4. please share the screen record to show the issue is solved with the PR change

@komagic
Copy link
Author

komagic commented Aug 6, 2024

please add a changeset (done)
please lint your code (done)
please share how to reproduce the original issue
original issue:
当独立使用snippet,在chrome extension devtool panel里copy没效果 ,未能提示授权或无权限,无权限error只在console中显示了。
改前:
image

please share the screen record to show the issue is solved with the PR change (done)
改后:

20240806_235632.mp4

@wingkwong 年轻的大佬帮忙看下有没有其他问题

@komagic komagic requested a review from wingkwong August 6, 2024 16:00
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e1d2515 and 5509df0.

Files selected for processing (1)
  • packages/hooks/use-clipboard/src/index.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/hooks/use-clipboard/src/index.ts

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
.changeset/plenty-ties-yell.md (1)

5-5: Translate the changeset description to English for broader understanding.

The description is in Chinese: "fix: useClipboard 兼容性修复 ,新增 compabilityCopy". It should be translated to ensure all contributors understand the changes.

- fix: useClipboard 兼容性修复 ,新增 compabilityCopy
+ fix: useClipboard compatibility fix, added compatibilityCopy
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5509df0 and e643515.

Files selected for processing (1)
  • .changeset/plenty-ties-yell.md (1 hunks)
Additional comments not posted (1)
.changeset/plenty-ties-yell.md (1)

1-3: Ensure the changeset header is correct.

The changeset header seems appropriate for a major change. It specifies the package affected by the change.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e643515 and df29993.

Files selected for processing (1)
  • .changeset/nervous-students-carry.md (1 hunks)
Additional comments not posted (1)
.changeset/nervous-students-carry.md (1)

1-5: Changeset looks good.

The changeset file is correctly formatted and provides the necessary information about the major change to the @nextui-org/use-clipboard package.

.changeset/nervous-students-carry.md Outdated Show resolved Hide resolved
.changeset/plenty-ties-yell.md Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between df29993 and 165d24c.

Files selected for processing (1)
  • .changeset/nervous-students-carry.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • .changeset/nervous-students-carry.md

@komagic
Copy link
Author

komagic commented Aug 12, 2024

what will be next

@komagic
Copy link
Author

komagic commented Sep 16, 2024

@wingkwong brother, i mean this pr

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

  1. please add test
  2. please share how to set up chrome extension devtool panel
  3. please resolve conflict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants