fix(core): resolve links at boundary positions in editLink and deleteLink - #3081
fix(core): resolve links at boundary positions in editLink and deleteLink#3081Ishkirat-Singh wants to merge 1 commit into
Conversation
…Link Both methods looked the link up at `position + 1` unconditionally. With the caret at the end of a link that lookup lands outside the mark, so editLink inserted the new text next to the old one and deleteLink removed nothing; with the position at the very end of the document it threw a RangeError from doc.resolve. Look inside the link first and then at either boundary, skipping positions outside the document, and only accept a range that actually contains the requested position. Fixes TypeCellOS#3073
|
@Ishkirat-Singh is attempting to deploy a commit to the TypeCell Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthrough
ChangesLink boundary handling
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Link editing and deletion now handle boundary and document-end cursor positions without the prior crashes or incorrect link handling. No actionable merge risk remains. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. A rabbit found a boundary bright Comment |
Summary
Fixes #3073.
StyleManager.editLinkanddeleteLinklooked up the link mark atposition + 1unconditionally. At the end of the document that position does not exist andtr.doc.resolvethrows aRangeError; at the end of a link (the usual place for the caret after clicking the link text) the lookup misses the link, falls back to the collapsed selection, andeditLinkinserts the new text next to the old link instead of replacing it.Rationale
The link mark is not inclusive, so a resolved position at either boundary does not carry the mark. Probing one position inside the link on both sides, and only accepting a link whose range actually contains the caret, finds the link at its start, inside it and at its end, without changing behaviour for a caret that is not on a link at all.
Changes
packages/core/src/editor/managers/StyleManager.ts: new privategetLinkMarkAround(position)that probesposition + 1,positionandposition - 1, skips positions outside the document, and returns the first link whose[from, to]range containsposition.editLinkanddeleteLinkuse it instead ofgetLinkMarkAtPos(position + 1).Impact
No public API change. Callers that pass a position outside the document no longer crash, and callers with the caret at a link boundary now edit or delete that link, which is the documented intent of both methods.
Testing
New
packages/core/src/editor/managers/StyleManager.test.ts(jsdom): edit and delete with the caret at the end of the link, edit with the caret at the start, and no throw for a position at the very end of the document. Ran it withvp test --runtogether withvp lint src.Checklist
Summary by CodeRabbit
Bug Fixes
Tests