Skip to content
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
14 changes: 14 additions & 0 deletions features/chomp-blocks/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Extend Wrapped C Blocks",
"description": "Automatically extends C blocks to wrap around blocks that it is being placed over when dragging.",
"credits": [
{
"username": "CST1229",
"url": "https://github.com/CST1229/"
}
],
"tags": [],
"scripts": [{ "file": "script.js", "runOn": "/projects/*" }],
"dynamic": true,
"type": ["Editor"]
}
76 changes: 76 additions & 0 deletions features/chomp-blocks/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export default async function ({ feature, console }) {
await feature.page.waitForElement(".blocklyWorkspace")

const ScratchBlocks = feature.traps.blocks();

// Rerender the dragged block when updating the insertion marker
const ogConnectMarker = ScratchBlocks.InsertionMarkerManager.prototype.connectMarker_;
ScratchBlocks.InsertionMarkerManager.prototype.connectMarker_ = function () {
ogConnectMarker.call(this);
if (!feature.self.disabled && this.firstMarker_) {
const block = this?.workspace_?.currentGesture_?.blockDragger_?.draggingBlock_;
block.noMoveConnection = true;
if (block) block.render(false);
}
};
const ogDisconnectMarker = ScratchBlocks.InsertionMarkerManager.prototype.disconnectMarker_;
ScratchBlocks.InsertionMarkerManager.prototype.disconnectMarker_ = function () {
ogDisconnectMarker.call(this);
if (!feature.self.disabled && this.firstMarker_) {
const block = this?.workspace_?.currentGesture_?.blockDragger_?.draggingBlock_;
block.noMoveConnection = true;
if (block) block.render(false);
}
};

const ogDraw = ScratchBlocks.BlockSvg.prototype.renderDraw_;
const ogMoveConnections = ScratchBlocks.BlockSvg.prototype.renderMoveConnections_;
ScratchBlocks.BlockSvg.prototype.renderDraw_ = function (iconWidth, inputRows) {
if (feature.self.disabled) return ogDraw.call(this, iconWidth, inputRows);

// If the block contains a statement (C) input and has an insertion marker,
// use that to calculate the height of the statement inputs
let computeBlock = this;
if (this?.workspace?.currentGesture_?.blockDragger_?.draggedConnectionManager_) {
const dragger = this.workspace.currentGesture_.blockDragger_;
const manager = dragger.draggedConnectionManager_;
if (
manager.markerConnection_ &&
manager.firstMarker_ &&
dragger.draggingBlock_ == this &&
dragger.draggingBlock_.type == manager.firstMarker_.type
) {
if (inputRows.some((row) => row.some((input) => input.type === ScratchBlocks.NEXT_STATEMENT))) {
computeBlock = manager.firstMarker_;
}
}
}

// Change the height of substacks
// (If we set inputRows to computeBlock.renderCompute_,
// the references to the inputs would be wrong
// so they just won't update properly)
if (computeBlock !== this) {
const _inputRows = computeBlock.renderCompute_(iconWidth);
for (let i = 0; i < inputRows.length; i++) {
const row = inputRows[i];
let update = false;
for (const input of row) {
if (input.type === ScratchBlocks.NEXT_STATEMENT) update = true;
}
if (update) row.height = Math.max(row.height, _inputRows[i].height);
}
}

ogDraw.call(this, iconWidth, inputRows);

// Moving the connections of a block while it's being dragged breaks it,
// so don't
if (computeBlock === this && !this.noMoveConnection) ogMoveConnections.call(this);
this.noMoveConnection = false;
};
ScratchBlocks.BlockSvg.prototype.renderMoveConnections_ = function () {
if (feature.self.disabled) return ogMoveConnections.call(this);
// Do nothing (this function is instead called by renderDraw_)
};
}
5 changes: 5 additions & 0 deletions features/features.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"version": 2,
"id": "chomp-blocks",
"versionAdded": "v4.0.0"
},
{
"version": 2,
"id": "custom-explore",
Expand Down