Skip to content

Conversation

@kimdiego2098
Copy link
Contributor

@kimdiego2098 kimdiego2098 commented Jan 13, 2026

feat: 增加代码编辑器插入文本方法

Summary by Sourcery

Add a new API to insert custom text into the code editor at the current selection via JS interop.

New Features:

  • Expose an InsertText method on the CodeEditor component to insert text programmatically.
  • Implement a corresponding insertText JavaScript function that injects text into the Monaco editor at the current selection and refocuses the editor.

@bb-auto
Copy link

bb-auto bot commented Jan 13, 2026

Thanks for your PR, @kimdiego2098. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 13, 2026

Reviewer's Guide

Adds a new text insertion API to the CodeEditor component and wires it to a corresponding JavaScript function that inserts text at the current Monaco editor selection and refocuses the editor.

Sequence diagram for CodeEditor.InsertText interaction

sequenceDiagram
    actor User
    participant BlazorCodeEditor as CodeEditorComponent
    participant JSRuntime as BlazorJSInterop
    participant CodeEditorJs as CodeEditor_razor_js
    participant Monaco as MonacoEditor

    User->>BlazorCodeEditor: Invoke InsertText(data)
    BlazorCodeEditor->>JSRuntime: InvokeVoidAsync insertText Id, data
    JSRuntime->>CodeEditorJs: insertText id, insertData
    CodeEditorJs->>CodeEditorJs: wrapper = Data.get id
    CodeEditorJs->>Monaco: editor.getSelection()
    CodeEditorJs->>Monaco: executeEdits insert-custom-text, range, text
    CodeEditorJs->>Monaco: focus()
Loading

Class diagram for updated CodeEditor text insertion API

classDiagram
    class CodeEditorComponent {
        +string Id
        +Task Resize()
        +Task InsertText(string data)
    }

    class CodeEditor_razor_js {
        +init(id, interop, options)
        +insertText(id, insertData)
        +monacoSetOptions(id, options)
    }

    class MonacoEditor {
        +getSelection()
        +executeEdits(source, edits)
        +focus()
    }

    class DataStore {
        +get(id)
    }

    CodeEditorComponent --> CodeEditor_razor_js : uses_via_JSInterop
    CodeEditor_razor_js --> DataStore : uses
    CodeEditor_razor_js --> MonacoEditor : manipulates
Loading

File-Level Changes

Change Details Files
Add a JavaScript helper to insert arbitrary text into the Monaco editor at the current selection and refocus the editor.
  • Retrieve the editor wrapper by id and bail out if not found
  • Call editor.getSelection() to get the current selection range
  • Use editor.executeEdits with a custom source id to insert the provided text at the selection and move markers
  • Refocus the editor after insertion to keep keyboard interaction consistent
  • Leave a comment noting a cursor position bug observed on subsequent insertions
src/components/BootstrapBlazor.CodeEditor/Components/CodeEditor/CodeEditor.razor.js
Expose a new InsertText C# API on the CodeEditor component that forwards to the JavaScript insertText function.
  • Add an async InsertText(string data) method that calls InvokeVoidAsync with the new insertText JS function, passing the component Id and text data
  • Preserve existing component initialization and resize behavior unchanged
  • Normalize the file header BOM, updating the copyright line formatting minimally
src/components/BootstrapBlazor.CodeEditor/Components/CodeEditor/CodeEditor.razor.cs

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto bot requested a review from ArgoZhang January 13, 2026 03:27
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In insertText, consider guarding against missing wrapper.editor or editor.getSelection() returning null, and explicitly updating the selection after executeEdits to address the cursor-position bug noted in the comment.
  • In the C# InsertText method, you may want to skip invoking JS when data is null or empty to avoid unnecessary interop calls and potential JS-side edge cases.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `insertText`, consider guarding against missing `wrapper.editor` or `editor.getSelection()` returning null, and explicitly updating the selection after `executeEdits` to address the cursor-position bug noted in the comment.
- In the C# `InsertText` method, you may want to skip invoking JS when `data` is null or empty to avoid unnecessary interop calls and potential JS-side edge cases.

## Individual Comments

### Comment 1
<location> `src/components/BootstrapBlazor.CodeEditor/Components/CodeEditor/CodeEditor.razor.cs:125` </location>
<code_context>
+    /// </summary>
+    /// <param name="data"></param>
+    /// <returns></returns>
+    public async Task InsertText(string data) => await InvokeVoidAsync("insertText",Id,data);
+    
     /// <summary>
</code_context>

<issue_to_address>
**suggestion:** Consider null/empty input handling for `InsertText` to avoid unnecessary JS interop calls

If `data` can be `null` or empty, consider short-circuiting with `string.IsNullOrEmpty(data)` so you avoid a no-op interop call or having to add extra defensive checks on the JS side (unless the JS layer is explicitly expected to validate this).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public async Task InsertText(string data) => await InvokeVoidAsync("insertText",Id,data);
Copy link

Choose a reason for hiding this comment

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

suggestion: Consider null/empty input handling for InsertText to avoid unnecessary JS interop calls

If data can be null or empty, consider short-circuiting with string.IsNullOrEmpty(data) so you avoid a no-op interop call or having to add extra defensive checks on the JS side (unless the JS layer is explicitly expected to validate this).

@Vision-Zhang
Copy link
Contributor

@kimdiego2098 测试没有发现问题

@kimdiego2098
Copy link
Contributor Author

@Vision-Zhang 已添加复现demo

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.

3 participants