Skip to content
Open
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
7 changes: 4 additions & 3 deletions packages/blockly/core/clipboard/block_paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ export class BlockPaster implements IPaster<BlockCopyData, BlockSvg> {
// Sometimes there's a delay before the block is fully created and ready for
// focusing, so wait slightly before focusing the newly pasted block.
const nodeToFocus: IFocusableNode = block;
renderManagement
.finishQueuedRenders()
.then(() => getFocusManager().focusNode(nodeToFocus));
renderManagement.finishQueuedRenders().then(() => {
if (block.isDeadOrDying()) return;
getFocusManager().focusNode(nodeToFocus);
});
return block;
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/blockly/core/field_input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,10 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
const block = this.getSourceBlock();
if (!block) throw new UnattachedFieldError();
const div = WidgetDiv.getDiv();
if (!div) return;
const bBox = this.getScaledBBox();
div!.style.width = bBox.right - bBox.left + 'px';
div!.style.height = bBox.bottom - bBox.top + 'px';
div.style.width = bBox.right - bBox.left + 'px';
div.style.height = bBox.bottom - bBox.top + 'px';

// In RTL mode block fields and LTR input fields the left edge moves,
// whereas the right edge is fixed. Reposition the editor.
Expand All @@ -835,8 +836,8 @@ export abstract class FieldInput<T extends InputTypes> extends Field<
y -= bounds.top + window.scrollY;
}

div!.style.left = `${x}px`;
div!.style.top = `${y}px`;
div.style.left = `${x}px`;
div.style.top = `${y}px`;
});
}

Expand Down
7 changes: 6 additions & 1 deletion packages/blockly/core/workspace_audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class WorkspaceAudio {
const sound = this.sounds.get(name);
if (sound) {
await this.prepareToPlay();
if (this.context.state !== 'running') return;

const source = this.context.createBufferSource();
const gainNode = this.context.createGain();
Expand Down Expand Up @@ -122,6 +123,7 @@ export class WorkspaceAudio {
async beep(tone: number, duration = 0.2) {
if (!this.isPlayingAllowed()) return;
await this.prepareToPlay();
if (this.context.state !== 'running') return;

const oscillator = this.context.createOscillator();
oscillator.type = 'sine';
Expand Down Expand Up @@ -209,7 +211,10 @@ export class WorkspaceAudio {
) {
this.lastSound = new Date();

if (this.context.state === 'suspended') {
if (
this.context.state === 'suspended' &&
navigator.userActivation.hasBeenActive
) {
await this.context.resume();
}
}
Expand Down
13 changes: 10 additions & 3 deletions packages/blockly/tests/mocha/block_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1500,9 +1500,16 @@ suite('Blocks', function () {
teardown(function () {
workspaceTeardown.call(this, this.workspace);

Blockly.icons.registry.unregister(
Blockly.icons.IconType.COMMENT.toString(),
);
if (
Blockly.registry.hasItem(
Blockly.registry.Type.ICON,
Blockly.icons.IconType.COMMENT.toString(),
)
) {
Blockly.icons.registry.unregister(
Blockly.icons.IconType.COMMENT.toString(),
);
}
Blockly.icons.registry.register(
Blockly.icons.IconType.COMMENT,
Blockly.icons.CommentIcon,
Expand Down
4 changes: 3 additions & 1 deletion packages/blockly/tests/mocha/keyboard_movement_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ suite('Keyboard-driven movement', function () {
...DEFAULT_INJECT_OPTIONS,
toolbox: toolbox,
});
Blockly.common.defineBlocks(p5blocks);
if (!('p5_setup' in Blockly.Blocks)) {
Blockly.common.defineBlocks(p5blocks);
}
Blockly.KeyboardMover.mover.setMoveDistance(20);
});

Expand Down
12 changes: 9 additions & 3 deletions packages/blockly/tests/mocha/keyboard_navigation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ suite('Keyboard navigation on Blocks', function () {
toolbox: toolbox,
renderer: 'zelos',
});
Blockly.common.defineBlocks(p5blocks);
if (!('p5_setup' in Blockly.Blocks)) {
Blockly.common.defineBlocks(p5blocks);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally these tests that register blocks should unregister them at the end of their tests

}
Blockly.serialization.workspaces.load(navigationTestBlocks, this.workspace);
for (const block of this.workspace.getAllBlocks()) {
block.initSvg();
Expand Down Expand Up @@ -415,7 +417,9 @@ suite('Keyboard navigation on Fields', function () {
toolbox: toolbox,
renderer: 'zelos',
});
Blockly.common.defineBlocks(p5blocks);
if (!('p5_setup' in Blockly.Blocks)) {
Blockly.common.defineBlocks(p5blocks);
}
Blockly.serialization.workspaces.load(navigationTestBlocks, this.workspace);
});

Expand Down Expand Up @@ -482,7 +486,9 @@ suite('Workspace comment navigation', function () {
toolbox: toolbox,
renderer: 'zelos',
});
Blockly.common.defineBlocks(p5blocks);
if (!('p5_setup' in Blockly.Blocks)) {
Blockly.common.defineBlocks(p5blocks);
}
Blockly.serialization.workspaces.load(navigationTestBlocks, this.workspace);
this.workspace.getTopBlocks(false).forEach((b) => b.queueRender());
Blockly.renderManagement.triggerQueuedRenders(this.workspace);
Expand Down
3 changes: 2 additions & 1 deletion packages/blockly/tests/mocha/render_management_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import {assert} from 'chai';
import {
DEFAULT_INJECT_OPTIONS,
sharedTestSetup,
sharedTestTeardown,
workspaceTeardown,
Expand Down Expand Up @@ -130,7 +131,7 @@ suite('Render Management', function () {

suite('Post-render bumpNeighbours', function () {
setup(function () {
this.workspace = Blockly.inject('blocklyDiv', {});
this.workspace = Blockly.inject('blocklyDiv', DEFAULT_INJECT_OPTIONS);

this.block = this.workspace.newBlock('controls_if');
this.block.initSvg();
Expand Down
19 changes: 11 additions & 8 deletions packages/blockly/tests/mocha/test_helpers/setup_teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const TEST_MEDIA_PATH = '../../media/';
*/
export const DEFAULT_INJECT_OPTIONS = Object.freeze({
media: TEST_MEDIA_PATH,
scrollbars: true,
});

/**
Expand Down Expand Up @@ -186,6 +187,16 @@ export function sharedTestTeardown(workspace = this.workspace) {
console.error('"' + testRef.fullTitle() + '" did not call sharedTestSetup');
}

// Remove the globally registered listener from FocusManager to avoid state
// being shared across test boundaries.
for (const registeredListener of this.globalDocumentEventListeners) {
document.removeEventListener(
registeredListener.type,
registeredListener.listener,
);
}
this.globalDocumentEventListeners = [];

try {
if (workspace) {
workspaceTeardown.call(this, workspace);
Expand Down Expand Up @@ -230,14 +241,6 @@ export function sharedTestTeardown(workspace = this.workspace) {

Blockly.WidgetDiv.testOnly_setDiv(null);

// Remove the globally registered listener from FocusManager to avoid state
// being shared across test boundaries.
for (const registeredListener of this.globalDocumentEventListeners) {
const eventType = registeredListener.type;
const eventListener = registeredListener.listener;
document.removeEventListener(eventType, eventListener);
}
this.globalDocumentEventListeners = [];
FocusManager.getFocusManager = this.oldGetFocusManager;
}
}
Expand Down
Loading