From cb58a6c749087044fa424ebdac48f5041efbf5cc Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Fri, 3 Sep 2021 12:07:34 +0100 Subject: [PATCH] brush selection when pressing meta tweak --- .../tldraw/src/components/tldraw/tldraw.tsx | 3 +- packages/tldraw/src/state/tlstate.ts | 59 +++++++++---------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/packages/tldraw/src/components/tldraw/tldraw.tsx b/packages/tldraw/src/components/tldraw/tldraw.tsx index 0efa9313c..8a6a3ef01 100644 --- a/packages/tldraw/src/components/tldraw/tldraw.tsx +++ b/packages/tldraw/src/components/tldraw/tldraw.tsx @@ -104,7 +104,8 @@ function InnerTldraw() { const isInSession = tlstate.session !== undefined // Hide bounds when not using the select tool, or when the only selected shape has handles - const hideBounds = isInSession || !isSelecting || isSelectedHandlesShape + const hideBounds = + (tlstate.session && tlstate.session.id !== 'brush') || !isSelecting || isSelectedHandlesShape // Hide bounds when not using the select tool, or when in session const hideHandles = isInSession || !isSelecting diff --git a/packages/tldraw/src/state/tlstate.ts b/packages/tldraw/src/state/tlstate.ts index 1999c44ad..174b87187 100644 --- a/packages/tldraw/src/state/tlstate.ts +++ b/packages/tldraw/src/state/tlstate.ts @@ -729,15 +729,17 @@ export class TLDrawState extends StateManager { * @returns this */ copy = (ids = this.selectedIds): this => { - this.clipboard = ids.map((id) => { - const shape = this.getShape(id, this.currentPageId) + this.clipboard = ids + .flatMap((id) => TLDR.getDocumentBranch(this.state, id, this.currentPageId)) + .map((id) => { + const shape = this.getShape(id, this.currentPageId) - return { - ...shape, - id: Utils.uniqueId(), - childIndex: TLDR.getChildIndexAbove(this.state, id, this.currentPageId), - } - }) + return { + ...shape, + id: Utils.uniqueId(), + childIndex: TLDR.getChildIndexAbove(this.state, id, this.currentPageId), + } + }) return this } @@ -794,10 +796,12 @@ export class TLDrawState extends StateManager { if (!this.clipboard) return this + const idsMap = Object.fromEntries(this.clipboard.map((shape) => [shape.id, Utils.uniqueId()])) + const shapesToPaste = this.clipboard.map((shape) => ({ ...shape, - id: Utils.uniqueId(), - parentId: this.currentPageId, + id: idsMap[shape.id], + parentId: idsMap[shape.parentId] || this.currentPageId, })) const commonBounds = Utils.getCommonBounds(shapesToPaste.map(TLDR.getBounds)) @@ -2308,6 +2312,9 @@ export class TLDrawState extends StateManager { switch (activeTool) { case 'select': { if (info.metaKey) { + if (!info.shiftKey) { + this.deselectAll() + } // While holding just command key, start a brush session this.startBrushSession(this.getPagePoint(info.point)) return @@ -2356,27 +2363,6 @@ export class TLDrawState extends StateManager { break } case TLDrawStatus.PointingBounds: { - // If we've clicked on a shape that is inside of a group, - // then select the group rather than the shape. - - // if (info.metaKey && info.shiftKey) { - // const targetId = this.pageState.hoveredId - // if (targetId) { - // this.pointedId = targetId - // const shape = this.getShape(targetId) - // if (this.selectedIds.includes(targetId)) { - // this.select(...this.selectedIds.filter((id) => id !== targetId)) - // } else { - // if (this.selectedIds.includes(shape.parentId)) { - // this.select(targetId) - // } else { - // this.select(...this.selectedIds, targetId) - // } - // } - // return - // } - // } - const { parentId } = this.getShape(info.target) this.pointedId = parentId === this.currentPageId ? info.target : parentId @@ -2459,7 +2445,16 @@ export class TLDrawState extends StateManager { } // Bounds (bounding box background) - onPointBounds: TLBoundsEventHandler = () => { + onPointBounds: TLBoundsEventHandler = (info) => { + if (info.metaKey) { + if (!info.shiftKey) { + this.deselectAll() + } + // While holding just command key, start a brush session + this.startBrushSession(this.getPagePoint(info.point)) + return + } + this.setStatus(TLDrawStatus.PointingBounds) }