Skip to content
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

fix: dynamic blocks finalizing connections when children are deleted #2192

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion plugins/block-dynamic-connection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ npm install @blockly/block-dynamic-connection --save
import * as Blockly from 'blockly';
import * as BlockDynamicConnection from '@blockly/block-dynamic-connection';

const ws = Blockly.inject({
const myWorkspace = Blockly.inject({
// options...
plugins: {
connectionPreviewer:
Expand All @@ -26,6 +26,9 @@ const ws = Blockly.inject({
),
},
};

// Add the change listener so connections will be finalized on deletion.
workspace.addChangeListener(BlockDynamicConnection.finalizeConnections);
```

## API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
finalizeConnections(): void;
}

function blockIsDynamic(block: Blockly.BlockSvg): block is DynamicBlock {
/**

Check warning on line 18 in plugins/block-dynamic-connection/src/connection_previewer.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "block" declaration
* A type guard that checks if the given block fulfills the DynamicBlock
* interface.
*/
export function blockIsDynamic(block: Blockly.BlockSvg): block is DynamicBlock {
return (
(block as DynamicBlock)['onPendingConnection'] !== undefined &&
(block as DynamicBlock)['finalizeConnections'] !== undefined
Expand Down
20 changes: 18 additions & 2 deletions plugins/block-dynamic-connection/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,28 @@
import './dynamic_if';
import './dynamic_text_join';
import './dynamic_list_create';
import {decoratePreviewer} from './connection_previewer';
import {decoratePreviewer, blockIsDynamic} from './connection_previewer';

export {decoratePreviewer};
export {decoratePreviewer, blockIsDynamic};

export const overrideOldBlockDefinitions = function (): void {
Blockly.Blocks['lists_create_with'] = Blockly.Blocks['dynamic_list_create'];
Blockly.Blocks['text_join'] = Blockly.Blocks['dynamic_text_join'];
Blockly.Blocks['controls_if'] = Blockly.Blocks['dynamic_if'];
};

/**

Check warning on line 26 in plugins/block-dynamic-connection/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing JSDoc @param "e" declaration
* Finalizes connections when certain events (such as block deletion) are
* detected.
*/
export function finalizeConnections(e: Blockly.Events.Abstract) {
if (e.type === Blockly.Events.BLOCK_DELETE) {
const ws = Blockly.Workspace.getById(e.workspaceId ?? '');
if (!ws) return;
for (const block of ws.getAllBlocks() as Blockly.BlockSvg[]) {
if (blockIsDynamic(block)) {
block.finalizeConnections();
}
}
}
}
4 changes: 3 additions & 1 deletion plugins/block-dynamic-connection/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ function createWorkspace(
blocklyDiv: HTMLElement,
options: Blockly.BlocklyOptions,
): Blockly.WorkspaceSvg {
return Blockly.inject(blocklyDiv, options);
const ws = Blockly.inject(blocklyDiv, options);
ws.addChangeListener(BlockDynamicConnection.finalizeConnections);
return ws;
}

document.addEventListener('DOMContentLoaded', function () {
Expand Down
Loading