Skip to content

Commit

Permalink
chore(core): improve dnd engine
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 13, 2022
1 parent 6e054b5 commit 976eb7b
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 368 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/effects/useDragDropEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const useDragDropEffect = (engine: Engine) => {
const tree = operation.tree
if (!dragNodes.length) return
const touchNode = tree.findById(outlineId || nodeId)
moveHelper.dragging({
moveHelper.dragMove({
point,
touchNode,
})
Expand Down Expand Up @@ -118,7 +118,7 @@ export const useDragDropEffect = (engine: Engine) => {
engine.props.outlineNodeIdAttrName
)
const touchNode = tree.findById(outlineNodeId || nodeId)
moveHelper.dragging({ point, touchNode })
moveHelper.dragMove({ point, touchNode })
})

engine.subscribeTo(DragStopEvent, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/effects/useTranslateEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const useTranslateEffect = (engine: Engine) => {
const dragNodes = translateHelper.dragNodes
const allowTranslate = dragNodes.every((node) => node.allowTranslate())
if (!dragNodes.length || !allowTranslate) return
translateHelper.dragging()
translateHelper.dragMove()
dragNodes.forEach((node) => {
const element = node.getElement()
translateHelper.translate(node, (translate) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/models/MoveHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ export class MoveHelper {
this.operation.engine.cursor.setDragType(CursorDragType.Move)
}

dragging(props: IMoveHelperDraggingProps) {
dragMove(props: IMoveHelperDraggingProps) {
const { point, touchNode } = props
if (this.outline.isPointInViewport(point, false)) {
this.activeViewport = this.outline
Expand Down Expand Up @@ -352,7 +352,7 @@ export class MoveHelper {
viewportClosestOffsetRect: observable.ref,
viewportClosestRect: observable.ref,
dragStart: action,
dragging: action,
dragMove: action,
dragEnd: action,
})
}
Expand Down
12 changes: 9 additions & 3 deletions packages/core/src/models/SnapLine.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import {
calcRectOfAxisLineSegment,
ISnapLineSegment,
ILineSegment,
IPoint,
calcOffsetOfSnapLineSegmentToEdge,
} from '@designable/shared'
import { TreeNode } from './TreeNode'
import { TranslateHelper } from './TranslateHelper'

export type IDynamicSnapLine = ISnapLineSegment & {
export type ISnapLineType = 'ruler' | 'space-block' | 'normal'

export type ISnapLine = ILineSegment & {
type?: ISnapLineType
distance?: number
id?: string
refer?: TreeNode
}

export class SnapLine {
_id: string
type: ISnapLineType
distance: number
refer: TreeNode
start: IPoint
end: IPoint
ctx: TranslateHelper
constructor(ctx: TranslateHelper, line: IDynamicSnapLine) {
constructor(ctx: TranslateHelper, line: ISnapLine) {
this.ctx = ctx
this.type = line.type || 'normal'
this._id = line.id
this.refer = line.refer
this.start = { ...line.start }
Expand Down
67 changes: 64 additions & 3 deletions packages/core/src/models/SpaceBlock.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
isEqualRect,
calcSpaceBlockOfRect,
IRect,
calcExtendsLineSegmentOfRect,
calcDistanceOfSnapLineToEdges,
LineSegment,
} from '@designable/shared'
import { SnapLine } from './SnapLine'
import { TranslateHelper } from './TranslateHelper'
import { TreeNode } from './TreeNode'

Expand Down Expand Up @@ -132,4 +132,65 @@ export class SpaceBlock {
}
return results
}

get snapLine() {
if (!this.isometrics.length) return
const nextRect = this.next.rect
const referRect = this.refer.getValidElementOffsetRect()
let line: LineSegment
if (this.type === 'top') {
line = new LineSegment(
{
x: nextRect.left,
y: referRect.bottom + nextRect.height,
},
{
x: nextRect.right,
y: referRect.bottom + nextRect.height,
}
)
} else if (this.type === 'bottom') {
line = new LineSegment(
{
x: nextRect.left,
y: referRect.top - nextRect.height,
},
{
x: nextRect.right,
y: referRect.top - nextRect.height,
}
)
} else if (this.type === 'left') {
line = new LineSegment(
{
x: referRect.right + nextRect.width,
y: nextRect.top,
},
{
x: referRect.right + nextRect.width,
y: nextRect.bottom,
}
)
} else {
line = new LineSegment(
{
x: referRect.left - nextRect.width,
y: nextRect.top,
},
{
x: referRect.left - nextRect.width,
y: nextRect.bottom,
}
)
}
const distance = calcDistanceOfSnapLineToEdges(
line,
this.ctx.dragNodesEdgeLines
)
return new SnapLine(this.ctx, {
...line,
distance,
type: 'space-block',
})
}
}
Loading

0 comments on commit 976eb7b

Please sign in to comment.