Skip to content

Conversation

@ilimei
Copy link
Collaborator

@ilimei ilimei commented Jan 7, 2026

๐Ÿ’ป ๅ˜ๆ›ด็ฑปๅž‹ | Change Type

  • โœจ feat
  • ๐Ÿ› fix
  • โ™ป๏ธ refactor
  • ๐Ÿ’„ style
  • ๐Ÿ”จ chore
  • ๐Ÿ“ docs

๐Ÿ”€ ๅ˜ๆ›ด่ฏดๆ˜Ž | Description of Change

  1. ไฟฎๅค keepId ๅฏผ่‡ดๅ›ž้€€ๆŠฅ้”™็š„้—ฎ้ข˜

๐Ÿ“ ่กฅๅ……ไฟกๆฏ | Additional Information

@vercel
Copy link

vercel bot commented Jan 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
lobe-editor Ready Ready Preview, Comment Jan 8, 2026 0:51am

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 7, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures that when JSON state is parsed into the editor, the global random key generator is reset based on existing node IDs to prevent future ID collisions.

Sequence diagram for JSON state parsing with random key reset

sequenceDiagram
  participant JSONDataSource
  participant Editor
  participant EditorState
  participant ParseSerializedNodeImpl
  participant RandomKeyGenerator

  JSONDataSource->>Editor: loadJSONState(dataObj)
  Editor->>EditorState: withJsonState(state)
  EditorState->>JSONDataSource: callback(state)

  JSONDataSource->>ParseSerializedNodeImpl: $parseSerializedNodeImpl(dataObj.root, editor, true, state)
  ParseSerializedNodeImpl-->>JSONDataSource: rootNode

  JSONDataSource->>EditorState: access _nodeMap
  JSONDataSource->>EditorState: iterate _nodeMap keys (exclude root)
  JSONDataSource->>JSONDataSource: compute maxId from numeric keys

  JSONDataSource->>RandomKeyGenerator: resetRandomKey(maxId + 1)

  JSONDataSource->>EditorState: _nodeMap.set(rootNode.getKey(), rootNode)
Loading

Updated class diagram for JSONDataSource and random key management

classDiagram
  class DataSource
  class JSONDataSource
  class Editor
  class EditorState {
    Map~string, EditorNode~ _nodeMap
  }
  class EditorNode {
    string getKey()
  }
  class RandomKeyGenerator {
    resetRandomKey(startId number)
  }

  JSONDataSource --|> DataSource
  JSONDataSource ..> Editor
  JSONDataSource ..> EditorState
  JSONDataSource ..> RandomKeyGenerator
  Editor o--> EditorState
  EditorState o--> EditorNode
Loading

File-Level Changes

Change Details Files
After parsing serialized JSON state, recompute the maximum numeric node ID in the editor state and reset the random key generator to start from the next available ID to avoid key conflicts.
  • After calling $parseSerializedNodeImpl, iterate over state._nodeMap keys (excluding 'root') to compute the maximum numeric node key present in the state.
  • Invoke resetRandomKey with maxId + 1 so that future randomly generated node keys do not collide with existing node IDs from the parsed state.
  • Keep the existing behavior of inserting the parsed root node into state._nodeMap after resetting the random key generator.
src/plugins/common/data-source/json-data-source.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

@lobehubbot
Copy link
Member

๐Ÿ‘ @ilimei


Thank you for raising your pull request and contributing to our Community
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
If you encounter any problems, please feel free to connect with us.
้žๅธธๆ„Ÿ่ฐขๆ‚จๆๅ‡บๆ‹‰ๅ–่ฏทๆฑ‚ๅนถไธบๆˆ‘ไปฌ็š„็คพๅŒบๅšๅ‡บ่ดก็Œฎ๏ผŒ่ฏท็กฎไฟๆ‚จๅทฒ็ป้ตๅพชไบ†ๆˆ‘ไปฌ็š„่ดก็ŒฎๆŒ‡ๅ—๏ผŒๆˆ‘ไปฌไผšๅฐฝๅฟซๅฎกๆŸฅๅฎƒใ€‚
ๅฆ‚ๆžœๆ‚จ้‡ๅˆฐไปปไฝ•้—ฎ้ข˜๏ผŒ่ฏท้šๆ—ถไธŽๆˆ‘ไปฌ่”็ณปใ€‚

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:

  • The iteration over state._nodeMap.keys() assumes keys() returns something with .forEach, which is not true for standard Map iterators; consider using for (const key of state._nodeMap.keys()) or Array.from(state._nodeMap.keys()).forEach(...) to avoid runtime errors.
  • When computing maxId, Number(key) can yield NaN for non-numeric keys (beyond 'root'), which will break Math.max; it would be safer to skip keys that don't parse to a finite number before taking the max.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The iteration over `state._nodeMap.keys()` assumes `keys()` returns something with `.forEach`, which is not true for standard `Map` iterators; consider using `for (const key of state._nodeMap.keys())` or `Array.from(state._nodeMap.keys()).forEach(...)` to avoid runtime errors.
- When computing `maxId`, `Number(key)` can yield `NaN` for non-numeric keys (beyond `'root'`), which will break `Math.max`; it would be safer to skip keys that don't parse to a finite number before taking the max.

## Individual Comments

### Comment 1
<location> `src/plugins/common/data-source/json-data-source.ts:78` </location>
<code_context>
           try {
             const root = $parseSerializedNodeImpl(dataObj.root, editor, true, state);
+            let maxId = -1;
+            state._nodeMap.keys().forEach((key) => {
+              if (key === 'root') return;
+              maxId = Math.max(maxId, Number(key));
</code_context>

<issue_to_address>
**issue (bug_risk):** Using `forEach` directly on `state._nodeMap.keys()` is likely incorrect because `Map.prototype.keys()` returns an iterator, not an array.

In JS/TS, `Map#keys()` returns an iterator, which doesnโ€™t have `forEach`, so this will throw at runtime. Use a `for...of` loop over `state._nodeMap.keys()` or wrap with `Array.from(...)` if you specifically need `forEach` behavior.
</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.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 8, 2026

Open in StackBlitz

npm i https://pkg.pr.new/lobehub/lobe-editor/@lobehub/editor@103

commit: 576a2b3

@arvinxx arvinxx merged commit 7119713 into master Jan 8, 2026
7 checks passed
@arvinxx arvinxx deleted the fix/id_will_conflict branch January 8, 2026 16:04
@lobehubbot
Copy link
Member

โค๏ธ Great PR @ilimei โค๏ธ


The growth of project is inseparable from user feedback and contribution, thanks for your contribution!
้กน็›ฎ็š„ๆˆ้•ฟ็ฆปไธๅผ€็”จๆˆทๅ้ฆˆๅ’Œ่ดก็Œฎ๏ผŒๆ„Ÿ่ฐขๆ‚จ็š„่ดก็Œฎ!

github-actions bot pushed a commit that referenced this pull request Jan 8, 2026
## [Version&nbsp;3.9.0](v3.8.0...v3.9.0)
<sup>Released on **2026-01-08**</sup>

#### โœจ Features

- **misc**: Reset random key to avoid ID conflicts during state parsing.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### What's improved

* **misc**: Reset random key to avoid ID conflicts during state parsing, closes [#103](#103) ([7119713](7119713))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>
@lobehubbot
Copy link
Member

๐ŸŽ‰ This PR is included in version 3.9.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants