Performance: Optimize TreeWalker usage in selection handling #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Performance Improvements and onBeforeInput Edge Case Implementation
Summary
This PR implements two key improvements to the edytor text editor:
TreeWalker Optimization: Added WeakMap-based caching for TreeWalker instances in selection handling to reduce DOM API overhead during frequent selection operations.
onBeforeInput Edge Case: Completed the missing implementation for block-spanning backward deletion scenarios, adding proper post-deletion handling for nested blocks and empty block cleanup.
Both changes follow existing code patterns and are designed to improve performance and feature completeness without breaking existing functionality.
Review & Testing Checklist for Human
Note: CI failures are deployment infrastructure-related (Cloudflare Pages, Netlify), not code issues. Local build and tests pass except for a pre-existing setBlock test failure that exists on master branch.
Diagram
%%{ init : { "theme" : "default" }}%% graph TD A["src/lib/selection/selection.svelte.ts"]:::major-edit --> B["TreeWalker Cache"] A --> C["findTextNode()"] A --> D["setAtTextRange()"] E["src/lib/events/onBeforeInput.ts"]:::major-edit --> F["deleteContentBackward case"] E --> G["Block-spanning deletion"] H["src/lib/edytor.utils.ts"]:::context --> I["deleteContentWithinSelection()"] I --> F J["src/lib/block/block.svelte.ts"]:::context --> K["unNestBlock()"] J --> L["mergeBlockBackward()"] K --> F L --> F M["PERFORMANCE_ANALYSIS.md"]:::minor-edit --> N["Performance Report"] subgraph Legend L1["Major Edit"]:::major-edit L2["Minor Edit"]:::minor-edit L3["Context/No Edit"]:::context end classDef major-edit fill:#90EE90 classDef minor-edit fill:#87CEEB classDef context fill:#FFFFFFNotes
Pre-existing Issue: There's a failing test in
setBlock.test.tsxshowing text duplication ("Hello world!Hello world!") that exists on both this branch and master - this appears unrelated to the current changes and should be addressed separately.