Skip to content

Commit

Permalink
handle cloning and positioning
Browse files Browse the repository at this point in the history
  • Loading branch information
sprocketc authored and logseq-cldwalker committed Dec 14, 2023
1 parent 0869a10 commit 844fa15
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions tldraw/packages/core/src/lib/TLApi/TLApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vec from '@tldraw/vec'
import type { TLAsset, TLBinding, TLEventMap, TLCloneDirection } from '../../types'
import type { TLAsset, TLBinding, TLEventMap } from '../../types'
import { TLCloneDirection, TLTargetType } from '../../types'
import { BoundsUtils, isNonNullable, uniqueId } from '../../utils'
import type { TLShape, TLShapeModel } from '../shapes'
import type { TLApp } from '../TLApp'
Expand Down Expand Up @@ -230,8 +231,47 @@ export class TLApi<S extends TLShape = TLShape, K extends TLEventMap = TLEventMa
return this.app.createNewLineBinding(source, target)
}

clone = (direction: TLCloneDirection) => {
// TODO: Clone to direction, clear label, create binding, select shape
cloneTo = (direction: TLCloneDirection) => {
const shape = this.app.allSelectedShapesArray[0]
const ShapeClass = this.app.getShapeClass(shape.type)

const bounds = shape.bounds
const spacing = 100
let point = [0, 0]

switch(direction) {
case TLCloneDirection.Bottom: {
point = [bounds.minX, bounds.maxY + spacing]
break
}
case TLCloneDirection.Top: {
point = [bounds.minX, bounds.minY - spacing - bounds.height]
break
}
case TLCloneDirection.Left: {
point = [bounds.minX - spacing - bounds.width, bounds.minY]
break
}
case TLCloneDirection.Right: {
point = [bounds.maxX + spacing, bounds.minY]
break
}
}

const clone = new ShapeClass({
...shape.serialized,
id: uniqueId(),
type: shape.type,
label: '',
point: point,
})

this.app.history.pause()
this.app.currentPage.addShapes(clone)
this.app.createNewLineBinding(shape, clone)
this.app.history.resume()
this.app.persist();
setTimeout(() => this.editShape(clone))
}

/** Clone shapes with given context */
Expand Down

0 comments on commit 844fa15

Please sign in to comment.