From 06971f9e5791e1cff6c79adf2ef129df132ee7f9 Mon Sep 17 00:00:00 2001 From: rgantzos <86856959+rgantzos@users.noreply.github.com> Date: Wed, 3 Jul 2024 17:29:17 -0700 Subject: [PATCH] Extend C Blocks --- features/chomp-blocks/data.json | 14 ++++++ features/chomp-blocks/script.js | 76 +++++++++++++++++++++++++++++++++ features/features.json | 5 +++ 3 files changed, 95 insertions(+) create mode 100644 features/chomp-blocks/data.json create mode 100644 features/chomp-blocks/script.js diff --git a/features/chomp-blocks/data.json b/features/chomp-blocks/data.json new file mode 100644 index 00000000..a138facd --- /dev/null +++ b/features/chomp-blocks/data.json @@ -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"] +} diff --git a/features/chomp-blocks/script.js b/features/chomp-blocks/script.js new file mode 100644 index 00000000..f58df53a --- /dev/null +++ b/features/chomp-blocks/script.js @@ -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_) + }; +} \ No newline at end of file diff --git a/features/features.json b/features/features.json index d0889cb5..bf6c47f3 100644 --- a/features/features.json +++ b/features/features.json @@ -1,4 +1,9 @@ [ + { + "version": 2, + "id": "chomp-blocks", + "versionAdded": "v4.0.0" + }, { "version": 2, "id": "custom-explore",