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: use dom-align to position dnd container (#3978) #3979

Merged
merged 2 commits into from
Nov 3, 2023
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
3 changes: 3 additions & 0 deletions packages/x6-plugin-dnd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"dependencies": {
"dom-align": "^1.12.4"
}
}
37 changes: 14 additions & 23 deletions packages/x6-plugin-dnd/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Graph,
EventArgs,
} from '@antv/x6'
import { alignPoint } from 'dom-align'
import { content } from './style/raw'

export class Dnd extends View implements Graph.Plugin {
Expand All @@ -26,7 +27,6 @@ export class Dnd extends View implements Graph.Plugin {
protected delta: Point | null
protected padding: number | null
protected snapOffset: Point.PointLike | null
protected originOffset: null | { left: number; top: number }

public options: Dnd.Options
public draggingGraph: Graph
Expand Down Expand Up @@ -165,38 +165,30 @@ export class Dnd extends View implements Graph.Plugin {
this.draggingView = delegateView
this.draggingBBox = draggingNode.getBBox()
this.padding = padding
this.originOffset = this.updateGraphPosition(clientX, clientY)
this.updateGraphPosition(clientX, clientY)
}

protected updateGraphPosition(clientX: number, clientY: number) {
const scrollTop =
document.body.scrollTop || document.documentElement.scrollTop
const scrollLeft =
document.body.scrollLeft || document.documentElement.scrollLeft
const delta = this.delta!
const nodeBBox = this.geometryBBox
const padding = this.padding || 5
const offset = {
left: clientX - delta.x - nodeBBox.width / 2 - padding + scrollLeft,
top: clientY - delta.y - nodeBBox.height / 2 - padding + scrollTop,
left: clientX - delta.x - nodeBBox.width / 2 - padding,
top: clientY - delta.y - nodeBBox.height / 2 - padding,
}

if (this.draggingGraph) {
if (this.options.draggingContainer) {
const { top, left } = Dom.offset(this.options.draggingContainer)
Dom.css(this.container, {
left: `${offset.left - left}px`,
top: `${offset.top - top}px`,
})
} else {
Dom.css(this.container, {
left: `${offset.left}px`,
top: `${offset.top}px`,
})
}
alignPoint(
this.container,
{
clientX: offset.left,
clientY: offset.top,
},
{
points: ['tl'],
},
)
}

return offset
}

protected updateNodePosition(x: number, y: number) {
Expand Down Expand Up @@ -333,7 +325,6 @@ export class Dnd extends View implements Graph.Plugin {
this.delta = null
this.padding = null
this.snapOffset = null
this.originOffset = null
this.undelegateDocumentEvents()
}
}
Expand Down
34 changes: 14 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading