Skip to content

Commit

Permalink
Merge pull request noya-app#385 from noya-app/improve-selection-paste
Browse files Browse the repository at this point in the history
Improve selection & paste behavior
  • Loading branch information
dabbott authored Mar 23, 2023
2 parents ef6287c + 9d6d2d4 commit 51363a0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/noya-canvas/src/interactions/drawing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const createDrawingInteraction =
groups: event[api.platformModKey] ? 'childrenOnly' : 'groupOnly',
artboards: 'childrenOnly',
includeLockedLayers: false,
includeLayersOutsideArtboardBounds: true,
},
);

Expand Down
1 change: 1 addition & 0 deletions packages/noya-canvas/src/interactions/editBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const createEditBlockInteraction = ({
groups: event[api.platformModKey] ? 'childrenOnly' : 'groupOnly',
artboards: 'emptyOrContainedArtboardOrChildren',
includeLockedLayers: false,
includeLayersOutsideArtboardBounds: true,
},
);

Expand Down
3 changes: 3 additions & 0 deletions packages/noya-canvas/src/interactions/marquee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function marqueeInteraction(actions: MarqueeActions) {
artboards: 'childrenOnly',
includeLockedLayers: false,
includePartiallyContainedLayers: origin.x < current.x,
includeLayersOutsideArtboardBounds: true,
});

selectLayer(layerIds);
Expand All @@ -82,6 +83,7 @@ export function marqueeInteraction(actions: MarqueeActions) {
groups: event[api.platformModKey] ? 'childrenOnly' : 'groupOnly',
artboards: 'childrenOnly',
includeLockedLayers: false,
includeLayersOutsideArtboardBounds: true,
});

const layerIds = layerId ? [layerId] : [];
Expand All @@ -96,6 +98,7 @@ export function marqueeInteraction(actions: MarqueeActions) {
artboards: 'childrenOnly',
includeLockedLayers: false,
includePartiallyContainedLayers: origin.x < current.x,
includeLayersOutsideArtboardBounds: true,
});

reset();
Expand Down
1 change: 1 addition & 0 deletions packages/noya-canvas/src/interactions/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const createMoveInteraction = (
? 'childrenOnly'
: 'emptyOrContainedArtboardOrChildren',
includeLockedLayers: false,
includeLayersOutsideArtboardBounds: true,
});

if (layerId) {
Expand Down
2 changes: 2 additions & 0 deletions packages/noya-canvas/src/interactions/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export function selectionInteraction({
? 'childrenOnly'
: 'emptyOrContainedArtboardOrChildren',
includeLockedLayers: false,
includeLayersOutsideArtboardBounds: true,
},
);

Expand All @@ -90,6 +91,7 @@ export function selectionInteraction({
? 'childrenOnly'
: 'emptyOrContainedArtboardOrChildren',
includeLockedLayers: false,
includeLayersOutsideArtboardBounds: true,
},
);

Expand Down
26 changes: 25 additions & 1 deletion packages/noya-state/src/reducers/layerReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
AffineTransform,
createBounds,
Point,
rectContainsRect,
transformRect,
unionRects,
} from 'noya-geometry';
import { SketchModel } from 'noya-sketch-model';
import {
Expand Down Expand Up @@ -504,6 +506,10 @@ export function layerReducer(
)
: undefined;

const newLayersBoundingRect = unionRects(
...layers.map((layer) => layer.frame),
);

return produce(state, (draft) => {
const draftPage = draft.sketch.pages[currentPageIndex];
draft.selectedLayerIds = [];
Expand Down Expand Up @@ -567,8 +573,26 @@ export function layerReducer(
});

newLayer = produce(newLayer, (draftLayer) => {
if (useOriginalDimensions) return;
// TODO: We may not need this flag. But need to test this behavior on noya.design.
if (useOriginalDimensions) {
const newLayersFitWithinParent = rectContainsRect(
selectedLayer.frame,
newLayersBoundingRect,
);

if (newLayersFitWithinParent) return;

// Center all layers within parent
draftLayer.frame.x +=
parentBounds.midX -
createBounds(newLayersBoundingRect).midX;
draftLayer.frame.y +=
parentBounds.midY -
createBounds(newLayersBoundingRect).midY;
return;
}

// Center layers individually at target point or within parent
const targetPoint = point
? point
: { x: parentBounds.midX, y: parentBounds.midY };
Expand Down
18 changes: 16 additions & 2 deletions packages/noya-state/src/selectors/geometrySelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ export type LayerTraversalOptions = {
*/
includePartiallyContainedLayers?: boolean;

/**
* The default is false
*/
includeLayersOutsideArtboardBounds?: boolean;

/**
* The default is `groupOnly`
*/
Expand All @@ -82,6 +87,7 @@ const DEFAULT_TRAVERSAL_OPTIONS: Required<LayerTraversalOptions> = {
includeHiddenLayers: false,
includeLockedLayers: true,
includePartiallyContainedLayers: true,
includeLayersOutsideArtboardBounds: false,
groups: 'groupOnly',
artboards: 'childrenOnly',
};
Expand Down Expand Up @@ -170,7 +176,12 @@ export function getLayersInRect(
// TODO: Handle rotated rectangle collision
const hasIntersect = rectsIntersect(screenRect, transformedFrame);

if (!hasIntersect) return SKIP;
const visitChildrenEvenWhenNotIntersecting =
Layers.isArtboard(layer) && options.includeLayersOutsideArtboardBounds;

if (!hasIntersect && !visitChildrenEvenWhenNotIntersecting) {
return SKIP;
}

const includeSelf =
(Layers.isGroup(layer) && options.groups === 'groupAndChildren') ||
Expand Down Expand Up @@ -251,7 +262,10 @@ export function getLayerAtPoint(
screenPoint,
);

if (!frameContainsPoint) {
const visitChildrenEvenWhenNotIntersecting =
Layers.isArtboard(layer) && options.includeLayersOutsideArtboardBounds;

if (!frameContainsPoint && !visitChildrenEvenWhenNotIntersecting) {
if (
Layers.isSymbolMasterOrArtboard(layer) &&
options.artboards === 'emptyOrContainedArtboardOrChildren' &&
Expand Down

0 comments on commit 51363a0

Please sign in to comment.