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

Remove private members of JS class in BitMarkdownEditor (#10273) #10274

Conversation

msynk
Copy link
Member

@msynk msynk commented Mar 18, 2025

closes #10273

Summary by CodeRabbit

  • New Features
    • The Markdown Editor has been updated to deliver a smoother, more responsive text editing experience. These improvements enhance event handling and resource management, ensuring reliable performance during extended editing sessions. Enjoy a more streamlined interface that supports your editing needs while maintaining optimal stability.

Copy link

coderabbitai bot commented Mar 18, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The pull request refactors the Markdown editor implementation in the BitBlazorUI library. The static _editors property and the init method of the MarkdownEditor class are updated to use the newly introduced Editor class instead of the previous self-referential approach. Private fields and methods have been replaced with public ones in the Editor class. Additionally, the Content type has been repositioned to the end of the file, and a dispose() method has been added to manage event listener removal and cleanup.

Changes

File Change Summary
src/BlazorUI/…/BitMarkdownEditor.ts (MarkdownEditor & Content) Updated the static _editors property from type MarkdownEditor to Editor and modified the init method to instantiate an Editor instance. The Content type definition has also been moved to the bottom of the file.
src/BlazorUI/…/BitMarkdownEditor.ts (Editor) Introduced the new Editor class by replacing private (#) members with public fields and methods; moved event listener setup into the constructor; updated selection and caret management methods; and added a dispose() method to remove event listeners and clear dotnetObj.

Sequence Diagram(s)

Loading
sequenceDiagram
    participant M as MarkdownEditor
    participant E as Editor
    participant T as HTMLTextAreaElement
    participant D as DotNetObject

    M->>E: init(id, textArea, dotnetObj, defaultValue)
    E->>T: Setup event listeners in constructor
    T->>E: Trigger input events (update selection/caret)
    E->>E: Process events and manage content insertion
    E->>E: dispose() removes listeners and clears dotnetObj

Assessment against linked issues

Objective Addressed Explanation
The private members of JS classes should not be used until ES2022 (#10273)

Poem

I'm a rabbit hopping through the code so bright,
Where private secrets now stand in the open light.
With Editor fresh and functions in a row,
My carrot's sweet—watch the clean code grow!
Leaping over bugs with joyful cheer,
The rabbit stamps its paws, "New code is here!"


🪧 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 generate docstrings to generate docstrings for this 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.

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: 0

🧹 Nitpick comments (2)
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownEditor/BitMarkdownEditor.ts (2)

32-42: Consider making _opens and _pairs private.
These fields are currently named with underscores but declared without an access modifier, making them publicly accessible. If they are not intended to be part of the external API, marking them as private or protected could reduce public surface area and improve maintainability.

-class Editor {
-    _opens: string[] = [];
-    _pairs: { [key: string]: string } = {
+class Editor {    
+    private _opens: string[] = [];
+    private _pairs: { [key: string]: string } = {

240-359: keydown method is comprehensive but quite large.
While functionally robust, it would benefit from splitting into smaller helper functions for readability (e.g., extracting logic for Backspace, Tab, Enter, and Ctrl/Meta shortcuts). This can simplify maintenance and testing.

 async keydown(e: KeyboardEvent) {
+    if (this.handleBackspace(e)) return;
+    if (this.handleCtrlShortcuts(e)) return;
+    ...
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6897a70 and 8711a10.

📒 Files selected for processing (1)
  • src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownEditor/BitMarkdownEditor.ts (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test
🔇 Additional comments (8)
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownEditor/BitMarkdownEditor.ts (8)

4-4: Transition to using the Editor class looks good.
The updated static _editors map, the instantiation of Editor, and invoking dispose() on teardown align well with the goal of removing private members from MarkdownEditor. No functional issues spotted here.

Also applies to: 7-7, 26-26


54-67: Event listener setup in constructor is solid.
The constructor properly binds and registers event listeners for textArea. Remember that removing them in dispose() is crucial to avoid memory leaks. No issues found in the implementation.


70-75: Getter/setter for value are straightforward and clear.
Storing text via this.textArea.value is idiomatic and avoids duplication. Good job.


77-88: Validate potential off-by-one scenarios in get block.
The loop increments total by b.length + 3 each iteration to account for '```'. If there’s any mismatch between the code blocks and caret position due to additional spacing or newlines, it might cause inaccurate block calculations. Consider adding tests for edge cases (e.g., caret at the start/end of a block).


119-144: Insertion logic is flexible but watch out for mismatched pairs.
For 'wrap' types, the code only pushes to _opens if content.value.length < 2, potentially excluding **. If the user is meant to track bold delimiters in _opens for auto-completion, you may want to handle length ≥ 2 as well. Otherwise, it’s safe to keep as-is if intentional.


228-236: Proper cleanup in dispose().
Removing all event listeners and resetting dotnetObj to undefined is an appropriate memory management pattern.


362-370: Event handlers for dblClick, click, and change are succinct.
They effectively manage selection trimming (dblClick), reset _opens on click, and notify the .NET object of changes. No concerns here.

Also applies to: 373-375, 377-379


382-385: Moved Content type to the end of the file.
This improves organization by keeping utility types near the bottom and relevant class definitions at the top. This is consistent with typical code style guidelines.

@msynk msynk merged commit 709ac05 into bitfoundation:develop Mar 19, 2025
3 checks passed
@msynk msynk deleted the 10273-blazorui-markdowneditor-replace-private-js-members branch March 19, 2025 14:00
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.

The private members of JS classes should not be used until ES2022
1 participant