Skip to content

feat(API): add insertAt method#1140

Open
ReFFaT wants to merge 4 commits into
mainfrom
feat-add-api
Open

feat(API): add insertAt method#1140
ReFFaT wants to merge 4 commits into
mainfrom
feat-add-api

Conversation

@ReFFaT

@ReFFaT ReFFaT commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

I added the isertAt method, which adds to the cursor if the parameter is not passed, and to the index if it is passed. If the index is out of range, it uses the standard isert and prepend methods.

Summary by Sourcery

Add support for inserting markup at a specific position or at the current cursor across editor implementations.

New Features:

  • Expose an insertAt(markup, index?) API on the public Markdown editor and underlying editors to insert markup at the cursor or at a given absolute position.

Documentation:

  • Document the new insertAt method on the public editor and content handler interfaces.

Tests:

  • Add unit tests covering insertAt behavior for negative indices (prepend), in-range indices, out-of-range indices (append), and default cursor-based insertion in both Wysiwyg and markup editors.

@ReFFaT ReFFaT requested review from d3m1d0v and makhnatkin as code owners June 8, 2026 17:10
@sourcery-ai

sourcery-ai Bot commented Jun 8, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a new insertAt(markup, index?) API across editor implementations that inserts at the current cursor by default, or at an absolute position with bounds mapped to prepend/append, and wires it through public types plus tests for both Wysiwyg and markup editors.

File-Level Changes

Change Details Files
Introduce insertAt(markup, index?) behavior for WysiwygContentHandler, including cursor-based insertion and clamped index handling.
  • Compute document size and treat negative indices as prepend and indices greater than doc size as append
  • When index is undefined, insert parsed inline content at the current selection position
  • When index is within bounds, insert parsed inline content at the given position
packages/editor/src/core/ContentHandler.ts
Expose insertAt on the shared ContentHandler interface and the public MarkdownEditorInstance/EditorImpl/WysiwygEditor APIs.
  • Extend ContentHandler interface with insertAt(markup, index?) and documentation
  • Add insertAt to MarkdownEditorInstance public type with JSDoc
  • Implement insertAt on EditorImpl delegating to currentEditor
  • Implement insertAt on WysiwygEditor delegating to its content handler
packages/editor/src/common/index.ts
packages/editor/src/bundle/editor-public-types.ts
packages/editor/src/bundle/Editor.ts
packages/editor/src/core/Editor.ts
Implement insertAt(markup, index?) for the CodeMirror-based markup Editor with cursor-based insertion and bounds-mapped index handling.
  • Calculate document length and treat negative index as prepend and out-of-range high index as append
  • When index is undefined, insert at current selection head and move cursor after inserted markup
  • When index is within range, insert at that position and set selection after the inserted text
packages/editor/src/markup/editor.ts
Add tests covering insertAt behavior for both WysiwygContentHandler and markup Editor across negative, omitted, and large index cases.
  • Add WysiwygContentHandler tests for prepend behavior on negative index, inline insertion at cursor with no index, and append behavior on large index
  • Add markup Editor tests for prepend behavior on negative index, insertion at cursor with no index, and append behavior on large index
packages/editor/src/core/ContentHandler.test.ts
packages/editor/src/markup/editor.test.ts

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • In WysiwygContentHandler.insertAt you compute docSize as state.doc.nodeSize - 2, which is a bit opaque and tied to ProseMirror internals; consider using a more direct/semantic measure (e.g. state.doc.content.size) so the meaning and bounds are clearer and less brittle.
  • Both branches of WysiwygContentHandler.insertAt parse the markup and then conditionally use firstChild?.content; you could parse once at the top of the method and reuse the resulting fragment to avoid duplicate work and keep the code more linear.
  • The CodeMirror insertAt updates the selection to the end of the inserted markup while the Wysiwyg insertAt leaves the selection at the original cursor, which may surprise callers of the shared insertAt API—consider aligning the selection behavior across implementations or documenting the difference explicitly.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `WysiwygContentHandler.insertAt` you compute `docSize` as `state.doc.nodeSize - 2`, which is a bit opaque and tied to ProseMirror internals; consider using a more direct/semantic measure (e.g. `state.doc.content.size`) so the meaning and bounds are clearer and less brittle.
- Both branches of `WysiwygContentHandler.insertAt` parse the markup and then conditionally use `firstChild?.content`; you could parse once at the top of the method and reuse the resulting fragment to avoid duplicate work and keep the code more linear.
- The CodeMirror `insertAt` updates the selection to the end of the inserted markup while the Wysiwyg `insertAt` leaves the selection at the original cursor, which may surprise callers of the shared `insertAt` API—consider aligning the selection behavior across implementations or documenting the difference explicitly.

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.

@gravity-ui

gravity-ui Bot commented Jun 8, 2026

Copy link
Copy Markdown

Storybook Deployed

@gravity-ui

gravity-ui Bot commented Jun 8, 2026

Copy link
Copy Markdown

🎭 Playwright Report

@d3m1d0v d3m1d0v left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And fix issues from sourcery

Comment thread packages/editor/src/common/index.ts Outdated
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