-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: merge copy paste work into develop #7379
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
68a8a5f
feat: add basic pasters (#7331)
BeksOmega 74c01d2
chore: fix paster exports and registration (#7343)
BeksOmega ce1678e
fix: refactor CopyData interface to have the correct structure (#7344)
BeksOmega 001d9ff
feat: make ICopyable generic and update clipboard APIs (#7348)
BeksOmega a901c62
fix: bumping copied objects (#7349)
BeksOmega e30c4ac
chore: upgrade keyboard shortcuts and context menus to use non-deprec…
BeksOmega File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* @license | ||
* Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {BlockSvg} from '../block_svg.js'; | ||
import * as registry from './registry.js'; | ||
import {ICopyData} from '../interfaces/i_copyable.js'; | ||
import {IPaster} from '../interfaces/i_paster.js'; | ||
import {State, append} from '../serialization/blocks.js'; | ||
import {Coordinate} from '../utils/coordinate.js'; | ||
import {WorkspaceSvg} from '../workspace_svg.js'; | ||
import * as eventUtils from '../events/utils.js'; | ||
import {config} from '../config.js'; | ||
|
||
export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> { | ||
static TYPE = 'block'; | ||
|
||
paste( | ||
copyData: BlockCopyData, | ||
workspace: WorkspaceSvg, | ||
coordinate?: Coordinate, | ||
): BlockSvg | null { | ||
if (!workspace.isCapacityAvailable(copyData.typeCounts!)) return null; | ||
|
||
if (coordinate) { | ||
copyData.blockState['x'] = coordinate.x; | ||
copyData.blockState['y'] = coordinate.y; | ||
} | ||
|
||
eventUtils.disable(); | ||
let block; | ||
try { | ||
block = append(copyData.blockState, workspace) as BlockSvg; | ||
moveBlockToNotConflict(block); | ||
} finally { | ||
eventUtils.enable(); | ||
} | ||
|
||
if (!block) return block; | ||
|
||
if (eventUtils.isEnabled() && !block.isShadow()) { | ||
eventUtils.fire(new (eventUtils.get(eventUtils.BLOCK_CREATE))(block)); | ||
} | ||
block.select(); | ||
return block; | ||
} | ||
} | ||
|
||
/** | ||
* Moves the given block to a location where it does not: (1) overlap exactly | ||
* with any other blocks, or (2) look like it is connected to any other blocks. | ||
* | ||
* Exported for testing. | ||
* | ||
* @param block The block to move to an unambiguous location. | ||
* @internal | ||
*/ | ||
export function moveBlockToNotConflict(block: BlockSvg) { | ||
const workspace = block.workspace; | ||
const snapRadius = config.snapRadius; | ||
const coord = block.getRelativeToSurfaceXY(); | ||
const offset = new Coordinate(0, 0); | ||
// getRelativeToSurfaceXY is really expensive, so we want to cache this. | ||
const otherCoords = workspace | ||
.getAllBlocks(false) | ||
.filter((otherBlock) => otherBlock.id != block.id) | ||
.map((b) => b.getRelativeToSurfaceXY()); | ||
|
||
while ( | ||
blockOverlapsOtherExactly(Coordinate.sum(coord, offset), otherCoords) || | ||
blockIsInSnapRadius(block, offset, snapRadius) | ||
) { | ||
if (workspace.RTL) { | ||
offset.translate(-snapRadius, snapRadius * 2); | ||
} else { | ||
offset.translate(snapRadius, snapRadius * 2); | ||
} | ||
} | ||
|
||
block!.moveTo(Coordinate.sum(coord, offset)); | ||
} | ||
|
||
/** | ||
* @returns true if the given block coordinates are less than a delta of 1 from | ||
* any of the other coordinates. | ||
*/ | ||
function blockOverlapsOtherExactly( | ||
coord: Coordinate, | ||
otherCoords: Coordinate[], | ||
): boolean { | ||
return otherCoords.some( | ||
(otherCoord) => | ||
Math.abs(otherCoord.x - coord.x) <= 1 && | ||
Math.abs(otherCoord.y - coord.y) <= 1, | ||
); | ||
} | ||
|
||
/** | ||
* @returns true if the given block (when offset by the given amount) is close | ||
* enough to any other connections (within the snap radius) that it looks | ||
* like they could connect. | ||
*/ | ||
function blockIsInSnapRadius( | ||
block: BlockSvg, | ||
offset: Coordinate, | ||
snapRadius: number, | ||
): boolean { | ||
return block | ||
.getConnections_(false) | ||
.some((connection) => !!connection.closest(snapRadius, offset).connection); | ||
} | ||
|
||
export interface BlockCopyData extends ICopyData { | ||
blockState: State; | ||
typeCounts: {[key: string]: number}; | ||
} | ||
|
||
registry.register(BlockPaster.TYPE, new BlockPaster()); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was already reviewed in another PR, so this doesn't have to block merging, I'm just curious. does
bumpNeighbors
not work for this?This feels like really similar logic to what's happening in
bumpNeighbors
but you're not taking advantage of some of the methods we already have like we have aconnection.neighbours
method that takes in a snap radius, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bumpNeighbors
always moves whichever block has the inferior connection :/ We want to make sure we're only moving the block that's being pasted.Wrt code reuse: we're using
connection.closest
instead ofconnection.neighbours
. They both take in a snap radius and do similar things.