Fix and refactor Sorter.ts
Question
- Fix the Sorter usage in StyleManager (regression with sorting property layers)
- Refactor Sorter with the usage of
Components.canMove - Update
canMovewith the check of dropping main Symbol inside its own instance.
Answers (3)
This's done.
The idea is to refactor the sorter for improved maintainability and readability. We can implement the following changes:
- Make
Sorterclass depend on an abstract tree structure rather than relying on specific implementations likeComponentorLayers, make theSorterclass work with a more generic, abstract tree structure. This will make it more flexible and reusable. - Split the code to multiple smaller classes each with a single responsibility
ComponentManager.canMove: Use theComponentManager.canMovemethod to determine if aComponentcan be moved. This avoids duplicating code and ensures consistent behavior across different parts of your application.
We will see how we can apply those points one by one
-
To make the
Sorterclass independant of any model implementation, we can add an argument in theSorterOptionsthat’s a function returning whether an element can be dragged into another element.export interface SorterOptions { container?: HTMLElement; canMove: (targetModel: Model<any>, srcModel: Model<any>, index: number) => Boolean; ... }For example the initializing the
Sorterfor theLayersin theStyleManagernew utils.Sorter({ container: this.el, canMove: (targetModel, srcModel, index) => { return targetModel.view === this.el // Allow dropping iff the target is the container }, em, })And for anything depending on
Componentwould benew utils.Sorter({ container: this.el, canMove: editor.Components.canMove, em, }) -
Splitting the functionality of
Sorterclass into multiple classesDropLocationDeterminer- Receives input: Takes mouse movement data, the HTML element being dragged, the tree structure, and the
canMovefunction as input. - Calculates drop location: Analyzes the mouse position, the structure of the tree, and the
canMovefunction to determine the most suitable index for the dropped element.
- Receives input: Takes mouse movement data, the HTML element being dragged, the tree structure, and the
DragAndDropHandler- Creates and manages elements: Creates and manages the placeholder, drag helper, and the actual element being dragged, ensuring their proper appearance and positioning.
Thanks for reporting this, @mohamedsalem401.
Thanks for sharing your report about [Fix] Fix and refactor Sorter.ts. To help the team investigate and prioritize this:
Please provide:
- A minimal reproducible example (CodeSandbox/JSFiddle)
- Your GrapesJS version number
- Browser and OS information
- Any error messages from the browser console
- Steps to reproduce the issue
What helps most:
- Minimal code example (not your full project)
- Screen recording or screenshot showing the issue
- Expected vs actual behavior clearly stated
- GrapesJS configuration you're using
With these details, the maintainers can identify and prioritize a fix much faster. The GrapesJS team is very responsive to well-documented issues.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4897
component:styleUpdate / component:styleUpdate:propertyName not fired
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Edge 109.0.1518.70 Reproducible demo link...
Issue #4601
update() of custom types added to StyleManager is no more triggered when switching between some targets.
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chromium 104 Reproducible demo link https:...
Issue #3137
When I select an particular element I'm not getting its styles. This issue happens only with some elements.
Not getting styles of the selected element Here is my code, const model = editor.StyleManager.getModelToStyle(component); let styleObject =...
Issue #6659
Symbol styles not synced
GrapesJS version [x] I confirm to use the latest version of GrapesJS What browser are you using? Firefox 145.0 (64-bit) Reproducible demo l...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.