[Fix] 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 (2)
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.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1508
[Question]: Refresh Canvas after changing gjs-html externally
Hi, I've been using GrapesJS with ReactJS (GrapesJS sits inside its own react component) and I'm having an issue uploading an image using m...
Issue #3389
MultiSelect some elements and change styles only work on the last selected element
Hi, when I MultiSelect some elements using shift, and change style in styleManager. Only the last clicked element will update the style. Ca...
Issue #2433
[Bug] Changing layerable in an an event at runtime doesn't immediately update
When I change the layerable option of a custom component in an event, it still shows up in the layers until you drag it once. Is there some...
Issue #3258
QUESTION: Prevent/Disable Custom Blocks from being dropped inside other Custom Blocks
Hello, I have a custom component, let's call it a Section Block. I want to prevent the user from dropping Section Blocks inside other Secti...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.