-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: use new connection previewer in block-dynamic-connection (#2204)
* fix: change the block-dynamic-connections plugin to use a connection previewer instead of monkey patching (#2190) * feat: add pass through previewer * fix: don't remove valid connections on saveExtraState * fix: have connection previewer handle pending connections * fix: have onPendingConnection ignore insertion markers * chore: delete insertion marker monkey patch * fixup * chore: bump versions * chore: fixup naming and docs * chore: format * chore: fix build * chore: fixup type * feat: make BasePreviewerConstructor optional * chore: fix readme * chore: PR nits * feat!: update block-dynamic-connection to use blockly v10.4.1 --------- Co-authored-by: Beka Westberg <[email protected]>
- Loading branch information
Showing
10 changed files
with
206 additions
and
116 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
96 changes: 96 additions & 0 deletions
96
plugins/block-dynamic-connection/src/connection_previewer.ts
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,96 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import * as Blockly from 'blockly/core'; | ||
|
||
interface ConnectionPreviewerConstructor { | ||
new (draggedBlock: Blockly.BlockSvg): Blockly.IConnectionPreviewer; | ||
} | ||
|
||
interface DynamicBlock extends Blockly.BlockSvg { | ||
onPendingConnection(connection: Blockly.RenderedConnection): void; | ||
finalizeConnections(): void; | ||
} | ||
|
||
function blockIsDynamic(block: Blockly.BlockSvg): block is DynamicBlock { | ||
return ( | ||
(block as DynamicBlock)['onPendingConnection'] !== undefined && | ||
(block as DynamicBlock)['finalizeConnections'] !== undefined | ||
); | ||
} | ||
|
||
/** | ||
* Returns a connection previewer constructor that decorates the passed | ||
* constructor to add connection previewing. | ||
* | ||
* @param BasePreviewerConstructor The constructor for the base connection | ||
* previewer class being decorated. If not provided, the default | ||
* InsertionMarkerPreviewer will be used. | ||
* @return A decorated connection previewer constructor. | ||
*/ | ||
export function decoratePreviewer( | ||
BasePreviewerConstructor?: ConnectionPreviewerConstructor, | ||
): ConnectionPreviewerConstructor { | ||
return class implements Blockly.IConnectionPreviewer { | ||
private basePreviewer: Blockly.IConnectionPreviewer; | ||
|
||
private pendingBlocks: Set<DynamicBlock> = new Set(); | ||
|
||
constructor(draggedBlock: Blockly.BlockSvg) { | ||
const BaseConstructor = | ||
BasePreviewerConstructor ?? Blockly.InsertionMarkerPreviewer; | ||
this.basePreviewer = new BaseConstructor(draggedBlock); | ||
} | ||
|
||
previewReplacement( | ||
draggedConn: Blockly.RenderedConnection, | ||
staticConn: Blockly.RenderedConnection, | ||
replacedBlock: Blockly.BlockSvg, | ||
): void { | ||
this.previewDynamism(staticConn); | ||
this.basePreviewer.previewReplacement( | ||
draggedConn, | ||
staticConn, | ||
replacedBlock, | ||
); | ||
} | ||
|
||
previewConnection( | ||
draggedConn: Blockly.RenderedConnection, | ||
staticConn: Blockly.RenderedConnection, | ||
): void { | ||
this.previewDynamism(staticConn); | ||
this.basePreviewer.previewConnection(draggedConn, staticConn); | ||
} | ||
|
||
hidePreview(): void { | ||
this.basePreviewer.hidePreview(); | ||
} | ||
|
||
dispose(): void { | ||
for (const block of this.pendingBlocks) { | ||
if (block.isDeadOrDying()) return; | ||
block.finalizeConnections(); | ||
} | ||
this.pendingBlocks.clear(); | ||
this.basePreviewer.dispose(); | ||
} | ||
|
||
/** | ||
* If the block is a dynamic block, calls onPendingConnection and | ||
* stores the block to be finalized later. | ||
* | ||
* @param conn The block to trigger onPendingConnection on. | ||
*/ | ||
private previewDynamism(conn: Blockly.RenderedConnection) { | ||
const block = conn.getSourceBlock(); | ||
if (blockIsDynamic(block)) { | ||
block.onPendingConnection(conn); | ||
this.pendingBlocks.add(block); | ||
} | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.