Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dragging (absolute dragging mode) #6192

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/core/src/utils/Droppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class Droppable {
dragStop?: DragStop;
draggedNode?: CanvasNewComponentNode;
sorter!: ComponentSorter<CanvasNewComponentNode>;
setAbsoluteDragContent?: (cnt: any) => any;

constructor(em: EditorModel, rootEl?: HTMLElement) {
this.em = em;
Expand Down Expand Up @@ -123,7 +124,6 @@ export default class Droppable {
// any not empty element
let content = dragSourceOrigin?.content || '<br>';
let dragStop: DragStop;
let dragSource;
em.stopDefault();

// Select the right drag provider
Expand All @@ -137,7 +137,7 @@ export default class Droppable {
target,
onEnd: (ev: any, dragger: any, { cancelled }: any) => {
let comp;
if (!cancelled) {
if (!cancelled && !!content) {
comp = wrapper.append(content)[0];
const canvasOffset = canvas.getOffset();
const { top, left, position } = target.getStyle() as ObjectStrings;
Expand All @@ -156,7 +156,7 @@ export default class Droppable {
},
});
dragStop = (cancel?: boolean) => dragger.stop(ev, { cancel });
dragSource = (cnt: any) => (content = cnt);
this.setAbsoluteDragContent = (cnt: any) => (content = cnt);
} else {
const sorter = new utils.ComponentSorter({
em,
Expand Down Expand Up @@ -255,6 +255,7 @@ export default class Droppable {
if (this.draggedNode) {
this.draggedNode.content = content;
}
this.setAbsoluteDragContent?.(content);
this.endDrop(!content, ev);
}

Expand Down
40 changes: 2 additions & 38 deletions packages/core/src/utils/sorter/CanvasNewComponentNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,6 @@ import { isComponent } from '../mixins';
type CanMoveSource = Component | ContentType;

export default class CanvasNewComponentNode extends CanvasComponentNode {
/**
* A cache of shallow editor models stored in the source node.
* This is a map where each content item is associated with its corresponding shallow wrapper model.
*/
private _cachedShallowModels: Map<ContentElement, Component> = new Map();

/**
* Ensures the shallow editor model for the given contentItem is cached in the source node.
* If not cached, retrieves it from the shallow wrapper and stores it in the cache.
* @param contentItem - The content item to retrieve or cache.
* @returns {Component | null} - The shallow wrapper model, either cached or retrieved.
*/
cacheSrcModelForContent(contentItem: ContentElement | Component): Component | undefined {
if (isComponent(contentItem)) {
return contentItem;
}

if (this._cachedShallowModels.has(contentItem)) {
return this._cachedShallowModels.get(contentItem)!;
}

const wrapper = this.model.em.Components.getShallowWrapper();
const srcModel = wrapper?.append(contentItem)[0];
// Replace getEl as the element would be removed in the shallow wrapper after 100ms
const el = srcModel?.getEl();
srcModel!.getEl = () => el;

if (srcModel) {
this._cachedShallowModels.set(contentItem, srcModel);
}

return srcModel;
}

canMove(source: CanvasNewComponentNode, index: number): boolean {
const realIndex = this.getRealIndex(index);
const { model: symbolModel, content, dragDef } = source._dragSource;
Expand All @@ -51,13 +17,11 @@ export default class CanvasNewComponentNode extends CanvasComponentNode {

if (Array.isArray(sourceContent)) {
return (
sourceContent.every((contentItem, i) =>
this.canMoveSingleContent(source.cacheSrcModelForContent(contentItem)!, realIndex + i),
) && canMoveSymbol
canMoveSymbol && sourceContent.every((contentItem, i) => this.canMoveSingleContent(contentItem, realIndex + i))
);
}

return this.canMoveSingleContent(source.cacheSrcModelForContent(sourceContent)!, realIndex) && canMoveSymbol;
return canMoveSymbol && this.canMoveSingleContent(sourceContent, realIndex);
}

private canMoveSingleContent(contentItem: ContentElement | Component, index: number): boolean {
Expand Down