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 344c855 commit 07cf5cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
43 changes: 28 additions & 15 deletions packages/core/src/models/TranslateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,34 @@ const calcDynamicSnapLines = (translateHelper: TranslateHelper) => {
translateHelper.cursor,
translateHelper.dragStartCursorOffset
)
const combineLines = calcClosestEdgeLines(
const closestLines = calcClosestEdgeLines(
targetLines,
cursorLines,
TranslateHelper.threshold
)
combineLines.h.forEach((line) => {
translateHelper.dynamicSnapLines.push(
const dynamicSnapLines = translateHelper.dynamicSnapLines
closestLines.h.forEach((line) => {
const gtLineIndex = dynamicSnapLines.findIndex(
(l) => l.direction === 'h' && l.distance != line.distance
)
if (gtLineIndex > -1) {
dynamicSnapLines.splice(gtLineIndex, 1)
}
dynamicSnapLines.push(
new SnapLine(translateHelper, {
refer,
...line,
})
)
})
combineLines.v.forEach((line) => {
translateHelper.dynamicSnapLines.push(
closestLines.v.forEach((line) => {
const gtLineIndex = dynamicSnapLines.findIndex(
(l) => l.direction === 'v' && l.distance != line.distance
)
if (gtLineIndex > -1) {
dynamicSnapLines.splice(gtLineIndex, 1)
}
dynamicSnapLines.push(
new SnapLine(translateHelper, {
refer,
...line,
Expand Down Expand Up @@ -112,6 +125,15 @@ const calcRulerSnapLines = (translateHelper: TranslateHelper) => {
}
})
}

const calcRulerSpaceBlocks = (translateHelper: TranslateHelper) => {
const results: SpaceBlock[] = []
for (let type in translateHelper.aroundSpaceBlocks) {
if (translateHelper.aroundSpaceBlocks[type])
results.push(translateHelper.aroundSpaceBlocks[type])
}
return results
}
export interface ITranslateHelperProps {
operation: Operation
}
Expand Down Expand Up @@ -158,15 +180,6 @@ export class TranslateHelper {
return lines
}

calcRulerSpaceBlocks() {
const results: SpaceBlock[] = []
for (let type in this.aroundSpaceBlocks) {
if (this.aroundSpaceBlocks[type])
results.push(this.aroundSpaceBlocks[type])
}
return results
}

get spaceBlocks(): SpaceBlock[] {
const results = []
for (let type in this.aroundSpaceBlocks) {
Expand Down Expand Up @@ -244,7 +257,7 @@ export class TranslateHelper {
}
handler(translate)
if (snapping) {
this.rulerSpaceBlocks = this.calcRulerSpaceBlocks()
this.rulerSpaceBlocks = calcRulerSpaceBlocks(this)
} else {
this.rulerSpaceBlocks = []
}
Expand Down
1 change: 0 additions & 1 deletion packages/shared/src/coordinate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ export function calcClosestEdgeLines(
): IRectEdgeLines {
const h: ISnapLineSegment[] = []
const v: ISnapLineSegment[] = []

target?.h?.forEach((targetLine) => {
source?.h?.forEach((sourceLine) => {
const distance = Math.abs(targetLine.start.y - sourceLine.start.y)
Expand Down

0 comments on commit 07cf5cc

Please sign in to comment.