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: screenshot functional bug #81

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

zzxming
Copy link
Collaborator

@zzxming zzxming commented Sep 25, 2024

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Introduced a new .scroll class for enhanced scrolling behavior in full-screen mode.
    • Added functions for managing scroll behavior and scrollbar width.
  • Improvements

    • Updated screenshot functionality to better manage scroll locking.
    • Adjusted focus behavior in the toolbar to prevent scrolling during screenshot mode.
  • Bug Fixes

    • Ensured consistent background color for full-screen elements across browsers.

Copy link

coderabbitai bot commented Sep 25, 2024

Walkthrough

The changes introduce modifications to the styling and functionality related to full-screen elements and screenshot capabilities. The fullscreen.scss file updates CSS rules for various full-screen selectors and adds new classes to manage scrolling behavior. The screenshot/index.ts file enhances the screenshot process by improving scroll management, while the scroll-lock.ts file introduces utility functions to calculate scrollbar width and lock scrolling. Overall, these changes aim to refine user experience during full-screen and screenshot operations.

Changes

File Path Change Summary
packages/fluent-editor/src/assets/fullscreen.scss Modified CSS rules for full-screen elements, added new .scroll class with &--lock and &__wrap modifiers to manage scrolling behavior.
packages/fluent-editor/src/screenshot/index.ts Updated screenshot functionality to utilize lockScroll for managing scroll during screenshots, modified selection retrieval, and improved cleanup for scroll locking.
packages/fluent-editor/src/toolbar/index.ts Changed the argument for this.quill.focus() to prevent scrolling when the format is 'screenshot'.
packages/fluent-editor/src/utils/scroll-lock.ts Introduced getScrollBarWidth and lockScroll functions for managing scrollbar width and locking scroll behavior on target elements.

Possibly related PRs

  • feat: screenshot #75: The main PR introduces new styles for full-screen elements and scrolling behavior, while this PR adds a screenshot feature that involves managing scroll behavior during the screenshot process, indicating a direct connection in handling scroll functionality.

Suggested labels

bug

Suggested reviewers

  • kagol

Poem

In a world where screens expand,
A rabbit hops with a steady hand.
With styles new and scrolls in check,
Screenshots snap, what the heck!
Lock the scroll, let’s take a peek,
Full-screen fun, so unique! 🐇✨


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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@github-actions github-actions bot added the bug Something isn't working label Sep 25, 2024
Copy link

@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

🧹 Outside diff range and nitpick comments (7)
packages/fluent-editor/src/assets/fullscreen.scss (2)

2-2: Consistent full-screen background color across browsers.

The changes ensure a consistent white background for full-screen elements across different browser implementations. This is good for cross-browser compatibility.

While the use of !important is generally discouraged, it might be necessary in this case to override any potential conflicting styles. However, consider if there's a way to achieve the same result without !important to maintain better CSS specificity control in the future.

Also applies to: 5-5, 8-8, 11-11


13-21: New .scroll class enhances scrolling control.

The addition of the .scroll class with its modifiers provides useful functionality for controlling scroll behavior. The use of BEM naming convention is consistent with best practices.

Consider adding a comment or documentation to explain the purpose and usage of the .scroll class and its modifiers. This will help other developers understand when and how to use these new styles.

Example:

// .scroll - Controls scrolling behavior
//   &--lock: Prevents scrolling (useful for modals or during transitions)
//   &__wrap: Allows scrolling within a full-height container
.scroll {
  // ... (existing code)
}
packages/fluent-editor/src/utils/scroll-lock.ts (1)

2-25: LGTM! Consider adding error handling and performance optimization.

The getScrollBarWidth function is well-implemented and uses a common technique to calculate the scrollbar width. The caching mechanism is a good optimization.

However, consider the following improvements:

  1. Add error handling for DOM operations to make the function more robust.
  2. Consider the performance implications of DOM manipulation, especially if this function is called frequently.

Here's a suggestion to add basic error handling:

 export const getScrollBarWidth = ({ target = document.body } = {}): number => {
   if (scrollBarWidth !== undefined) return scrollBarWidth
 
+  try {
     const outer = document.createElement('div')
     outer.className = 'scroll__wrap'
     outer.style.visibility = 'hidden'
     outer.style.width = '100px'
     outer.style.position = 'absolute'
     outer.style.top = '-9999px'
     target.appendChild(outer)
 
     const widthNoScroll = outer.offsetWidth
     outer.style.overflow = 'scroll'
 
     const inner = document.createElement('div')
     inner.style.width = '100%'
     outer.appendChild(inner)
 
     const widthWithScroll = inner.offsetWidth
     outer.parentNode?.removeChild(outer)
     scrollBarWidth = widthNoScroll - widthWithScroll
 
     return scrollBarWidth
+  } catch (error) {
+    console.error('Error calculating scrollbar width:', error)
+    return 0 // fallback to 0 if calculation fails
+  }
 }

This change adds a try-catch block to handle any potential errors during DOM manipulation and provides a fallback value.

packages/fluent-editor/src/toolbar/index.ts (1)

143-143: Approved: Good fix for screenshot functionality.

The change to prevent scrolling when focusing for screenshots is a good solution to the reported bug. It should improve the user experience without affecting other functionality.

For improved clarity and maintainability, consider extracting the condition into a named constant:

+ const isScreenshotFormat = format === 'screenshot';
- this.quill.focus({ preventScroll: format === 'screenshot' })
+ this.quill.focus({ preventScroll: isScreenshotFormat })

This makes the code more readable and easier to modify if additional formats need similar treatment in the future.

packages/fluent-editor/src/screenshot/index.ts (3)

Line range hint 94-106: LGTM: Improved selection handling and scroll locking.

The changes in this segment improve the screenshot functionality:

  1. Removing the true argument from getSelection allows for more flexible selection handling.
  2. Using lockScroll() and storing its result in cleanLock provides a clear mechanism for managing scroll locking.

These changes enhance the overall robustness of the screenshot process.

Consider renaming cleanLock to unlockScroll or releaseScrollLock for better clarity on its purpose.


119-119: LGTM: Consistent cleanup of scroll lock.

The addition of cleanLock() after removing the wrapper is a good practice. It ensures that the scroll lock is properly released when the screenshot process is completed, maintaining consistent scroll behavior.

For consistency, consider moving the cleanLock() call right after wrapper.remove() in both this function and the removeContextmenu function. This ensures the cleanup order is the same in both cases.


Line range hint 1-194: Overall: Improved screenshot functionality with better scroll management.

The changes in this file significantly enhance the screenshot functionality by introducing a more robust scroll locking mechanism. The use of the lockScroll utility and consistent cleanup improve the overall reliability of the screenshot process. The code is now more organized and follows better practices for resource management.

Key improvements:

  1. Introduction of a dedicated lockScroll utility.
  2. Consistent cleanup of scroll locking across different scenarios.
  3. More flexible selection handling in the Screenshot function.

These changes should result in a more stable and predictable user experience when using the screenshot feature.

Consider adding error handling for the lockScroll and cleanLock operations to ensure robustness in case of unexpected issues with scroll management.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f03dfab and ed78ff4.

📒 Files selected for processing (4)
  • packages/fluent-editor/src/assets/fullscreen.scss (1 hunks)
  • packages/fluent-editor/src/screenshot/index.ts (3 hunks)
  • packages/fluent-editor/src/toolbar/index.ts (1 hunks)
  • packages/fluent-editor/src/utils/scroll-lock.ts (1 hunks)
🧰 Additional context used
Biome
packages/fluent-editor/src/utils/scroll-lock.ts

[error] 34-34: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

🔇 Additional comments not posted (5)
packages/fluent-editor/src/assets/fullscreen.scss (1)

1-21: Request for clarification on bug fix implementation.

The changes made to the full-screen styles and the addition of the .scroll class appear to be related to the screenshot functional bug mentioned in the PR objectives. However, to ensure that these changes fully address the issue:

  1. Could you provide more details about the specific screenshot bug that was being experienced?
  2. How do these CSS changes resolve the issue?
  3. Have you tested the fix across different browsers to ensure consistent behavior?

This information will help in understanding the full impact of these changes and ensure that the bug has been completely resolved.

packages/fluent-editor/src/utils/scroll-lock.ts (1)

1-52: Summary of review for scroll-lock.ts

Overall, this file introduces well-implemented utility functions for managing scroll behavior and scrollbar width. The main points for improvement are:

  1. Add error handling to getScrollBarWidth function.
  2. Fix the typo in the 'clockClass' variable name in lockScroll function.
  3. Refactor the assignment in expression in the cleanLock function.
  4. Consider using data attributes instead of classes for locking scroll.
  5. Optimize performance by using requestAnimationFrame for style changes.

These improvements will enhance the robustness, maintainability, and performance of the code. Great job on implementing these utility functions, and with these minor adjustments, the code will be even better!

🧰 Tools
Biome

[error] 34-34: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

packages/fluent-editor/src/screenshot/index.ts (3)

5-5: LGTM: Good practice for code organization.

The addition of the lockScroll import from a separate utility file is a good practice. It improves code organization and promotes reusability of the scroll locking functionality.


94-94: LGTM: Improved scroll locking mechanism.

The removal of the manual overflow: hidden setting is a good change. It's consistent with the introduction of the lockScroll utility, which likely provides a more robust and reusable solution for managing scroll behavior during the screenshot process.


111-111: LGTM: Proper cleanup of scroll lock.

The addition of cleanLock() before removing the event listener is a good practice. It ensures that the scroll lock is properly released when the screenshot process is cancelled, preventing potential issues with scroll behavior.

@kagol kagol merged commit 01325ee into opentiny:main Sep 26, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants