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

代码折叠 #5235

Merged
merged 6 commits into from
Aug 16, 2024
Merged

代码折叠 #5235

merged 6 commits into from
Aug 16, 2024

Conversation

mayfwl
Copy link
Contributor

@mayfwl mayfwl commented Aug 8, 2024

💻 变更类型 | Change Type

  • feat
  • fix
  • refactor
  • perf
  • style
  • test
  • docs
  • ci
  • chore
  • build

🔀 变更说明 | Description of Change

📝 补充信息 | Additional Information

#5216

Summary by CodeRabbit

  • New Features

    • Enhanced code block functionality allowing users to expand or collapse lengthy code snippets for better visibility.
    • Added a toggle button for managing the visibility of code blocks based on their height.
  • Style

    • Introduced a new button styling for the toggle feature to improve user interface and accessibility.
    • Maintained styling for existing copy code button functionality.

Copy link

vercel bot commented Aug 8, 2024

@mayfwl is attempting to deploy a commit to the NextChat Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Aug 8, 2024

Walkthrough

The recent updates enhance the PreCode component by introducing collapsible functionality for code blocks. Users can now toggle the visibility of lengthy code snippets, significantly improving usability. Additionally, styling improvements in the global CSS ensure that the toggle button is intuitive and user-friendly, promoting better interaction with code displays.

Changes

Files Change Summary
app/components/markdown.tsx Enhanced PreCode component with collapsible functionality; added CustomCode for state management and visibility toggling.
app/styles/globals.scss Introduced .show-hide-button class for toggle button styling; added .expanded and .collapsed classes for state-based styling.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CustomCode
    participant StateManager

    User->>CustomCode: Click toggle button
    CustomCode->>StateManager: Request to toggle collapsed state
    StateManager-->>CustomCode: Collapsed state updated
    CustomCode->>User: Display updated code block
Loading

🐰🌟 In the code, I found a way,
To toggle blocks and play all day!
With a click, the code unfolds,
A joy for all, or so I'm told!
Hop in, explore each snippet with glee,
Usability blooms, come code with me! 🌼✨


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

github-actions bot commented Aug 8, 2024

Your build has completed!

Preview deployment

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between cf1c8e8 and 4478763.

Files selected for processing (2)
  • app/components/markdown.tsx (2 hunks)
  • app/styles/globals.scss (2 hunks)
Additional comments not posted (5)
app/styles/globals.scss (1)

304-314: Styling for .show-hide-button looks good!

The .show-hide-button class is well-styled for its intended purpose of enhancing code block usability. Ensure that it integrates seamlessly with existing styles and does not overlap with other elements.

However, verify that the positioning and visibility are consistent across different screen sizes and themes.

app/components/markdown.tsx (4)

137-139: Toggle logic in toggleCollapsed is correct!

The toggleCollapsed function correctly toggles the collapsed state.

However, verify that it is used correctly within the component to ensure proper functionality.

Verification successful

toggleCollapsed is used correctly within the component!

The toggleCollapsed function is used as an onClick handler, which is an appropriate usage for toggling UI state.

  • File: app/components/markdown.tsx
    • Usage: onClick={toggleCollapsed}
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the correct usage of `toggleCollapsed` within the component.

# Test: Search for `toggleCollapsed` usage in the codebase. Expect: Correct usage ensuring proper functionality.
rg --type tsx 'toggleCollapsed'

Length of output: 59


Script:

#!/bin/bash
# Description: Verify the correct usage of `toggleCollapsed` within the component.

# Test: Search for `toggleCollapsed` usage in the codebase. Expect: Correct usage ensuring proper functionality.
rg --type ts 'toggleCollapsed'

Length of output: 158


Line range hint 144-165:
Rendering logic for collapsibility is well-implemented!

The conditional rendering of the toggle button enhances user interaction with code blocks.

However, verify that the user experience is smooth and visually consistent across different scenarios.


128-135: Height calculation logic in useEffect looks good!

The use of useEffect to calculate the code block's height and determine the toggle visibility is appropriate.

However, verify that the calculations are accurate and do not cause unnecessary re-renders.

Verification successful

Height calculation logic in useEffect is accurate and efficient.

The useEffect correctly calculates the height of the code block and determines the visibility of the toggle without causing unnecessary re-renders. The dependency array is appropriately set to [props.children], ensuring updates only when necessary.

  • The height calculation logic is correctly implemented.
  • No unnecessary re-renders are caused by this useEffect.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the accuracy of height calculations in `useEffect`.

# Test: Search for `useEffect` usage related to height calculation. Expect: Accurate calculations without unnecessary re-renders.
rg --type tsx 'useEffect' -A 5

Length of output: 58


Script:

#!/bin/bash
# Description: Verify the accuracy of height calculations in `useEffect`.

# Find all `.tsx` files and search for `useEffect` usage related to height calculation.
fd --extension tsx --exec rg 'useEffect' -A 5

Length of output: 9296


125-126: State management for collapsibility looks good!

The introduction of collapsed and showToggle state variables is appropriate for managing the collapsible functionality of code blocks.

However, verify that the state updates do not introduce performance bottlenecks, especially with large code blocks.

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4478763 and 8f759d1.

Files selected for processing (2)
  • app/components/markdown.tsx (1 hunks)
  • app/styles/globals.scss (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • app/components/markdown.tsx
  • app/styles/globals.scss

{showToggle && collapsed && (
<div
className="show-hide-button"
style={{
Copy link
Member

Choose a reason for hiding this comment

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

这里不要一会儿使用className 一会儿又是inline style把 都可以使用className来控制

Copy link
Contributor Author

Choose a reason for hiding this comment

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

好的

const code = ref.current.innerText;
copyToClipboard(code);
}
<div style={{ position: "relative" }}>
Copy link
Member

Choose a reason for hiding this comment

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

基于pre定位可以吗,减少不必要的div

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我调整一下

}, [props.children]);

const toggleCollapsed = () => {
setCollapsed(!collapsed);
Copy link
Member

Choose a reason for hiding this comment

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

setCollapsed(collapsed=>!collapsed)

if (ref.current) {
const codeHeight = ref.current.scrollHeight;
setShowToggle(codeHeight > 400);
ref.current.scrollTop = ref.current.scrollHeight;
Copy link
Member

Choose a reason for hiding this comment

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

这里的意义是?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

设置pre框高度>400px,就会显示‘展示全部的按钮’,ref.current.scrollTop = ref.current.scrollHeight;设置pre高度固定后里面内容自动向下滚动

app/styles/globals.scss Show resolved Hide resolved
@Dogtiti
Copy link
Member

Dogtiti commented Aug 9, 2024

另外我觉得是不是可以对markdown加一个custom code 组件?
不要去改pre组件的一些东西?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


In addition, I think it is possible to add a custom code component to markdown?
Don't you want to change some things in the pre component?

@mayfwl
Copy link
Contributor Author

mayfwl commented Aug 9, 2024

另外我觉得是不是可以对markdown加一个custom code 组件? 不要去改pre组件的一些东西?

我去改改

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


In addition, I think it is possible to add a custom code component to markdown? Don't you want to change some things in the pre component?

I'll change it

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 (5)
app/components/markdown.tsx (5)

125-138: Remove commented-out code.

The commented-out code related to collapsible functionality is no longer needed and should be removed for cleaner code.

-  // const [collapsed, setCollapsed] = useState(true);
-  // const [showToggle, setShowToggle] = useState(false);
-  // useEffect(() => {
-  //   if (ref.current) {
-  //     const codeHeight = ref.current.scrollHeight;
-  //     setShowToggle(codeHeight > 400);
-  //     ref.current.scrollTop = ref.current.scrollHeight;
-  //   }
-  // }, [props.children]);
-  // const toggleCollapsed = () => {
-  //   setCollapsed(collapsed=>!collapsed);
-  // };

142-147: Use consistent styling methods.

Consider using className instead of inline styles for consistency, as previously suggested in the comments.

-      <div style={{ position: "relative" }}>
+      <div className="relative-position">

Ensure that the corresponding CSS class is defined.


207-213: Use className for styling.

Consider using className instead of inline styles for better maintainability and consistency.

-      <code
+      <code className={`code-block ${collapsed ? 'collapsed' : 'expanded'}`}

Ensure that the corresponding CSS classes are defined.


207-224: Use <pre> instead of <code> for semantic correctness.

The <pre> tag is more appropriate for block-level code snippets, while <code> is typically used for inline code.

-      <code
+      <pre

Ensure that the corresponding CSS is updated to reflect this change.


269-269: Remove console log statement.

The console log statement should be removed to clean up the production code.

-  console.log(escapedContent, 11233);
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8f759d1 and 6f75ef8.

Files selected for processing (2)
  • app/components/markdown.tsx (4 hunks)
  • app/styles/globals.scss (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/styles/globals.scss

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6f75ef8 and 356155f.

Files selected for processing (1)
  • app/components/markdown.tsx (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app/components/markdown.tsx

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 UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 356155f and c3c3dd5.

Files selected for processing (2)
  • app/components/markdown.tsx (2 hunks)
  • app/styles/globals.scss (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • app/components/markdown.tsx
  • app/styles/globals.scss

position: absolute;
top: 0;
left: 0;
right: 0;
Copy link
Member

Choose a reason for hiding this comment

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

better to use inset:0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里不能全为0,因为要设置顶部居中,下面加了margin:auto

@lloydzhou lloydzhou merged commit e210db7 into ChatGPTNextWeb:main Aug 16, 2024
1 of 2 checks passed
@lloydzhou lloydzhou deleted the feature/access branch September 26, 2024 13:35
@coderabbitai coderabbitai bot mentioned this pull request Oct 11, 2024
10 tasks
@coderabbitai coderabbitai bot mentioned this pull request Nov 6, 2024
10 tasks
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.

4 participants