Skip to content

Commit

Permalink
Added option to delete a Mod-block if more than 1 is available in the…
Browse files Browse the repository at this point in the history
… workspace

Removed scrollbar from the context menu
  • Loading branch information
LennardF1989 committed Nov 17, 2021
1 parent 5317082 commit 807dfa0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
67 changes: 60 additions & 7 deletions src/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ const BF2042PortalExtensions = (function () {

const toggleComments = (function () {
function displayText(scope) {
return scope.block.getCommentIcon()
? "Remove Comment"
return scope.block.getCommentIcon()
? "Remove Comment"
: "Add Comment";
}

Expand All @@ -105,8 +105,8 @@ const BF2042PortalExtensions = (function () {

const toggleInputs = (function () {
function displayText(scope) {
return scope.block.getInputsInline()
? "Show Inputs Vertically"
return scope.block.getInputsInline()
? "Show Inputs Vertically"
: "Show Inputs Horizontally";
}

Expand All @@ -130,8 +130,8 @@ const BF2042PortalExtensions = (function () {

const toggleCollapse = (function () {
function displayText(scope) {
return scope.block.isCollapsed()
? "Expand Block"
return scope.block.isCollapsed()
? "Expand Block"
: "Collapse Block";
}

Expand All @@ -153,6 +153,43 @@ const BF2042PortalExtensions = (function () {
};
})();

const deleteModBlock = (function () {
function precondition(scope) {
if(scope.block.type === "modBlock" && getBlocksByType("modBlock").length > 1) {
return "enabled";
}

return "hidden";
}

async function callback(scope) {
scope.block.dispose(false, false);
}

//Based on: https://groups.google.com/g/blockly/c/4mfShJDY6-k
function getBlocksByType(type) {
const blocks = [];
const workspace = _Blockly.getMainWorkspace();

for (const blockID in workspace.blockDB_) {
if (workspace.blockDB_[blockID].type == type) {
blocks.push(workspace.blockDB_[blockID]);
}
}

return blocks;
}

return {
id: "deleteModBlock",
displayText: "Delete Mod Block",
scopeType: _Blockly.ContextMenuRegistry.ScopeType.BLOCK,
weight: 100,
preconditionFn: precondition,
callback: callback
};
})();

const openDocumentation = (function () {
const documentationUrl = "https://bf2042.lennardf1989.com";

Expand All @@ -175,7 +212,7 @@ const BF2042PortalExtensions = (function () {
})();

//Based on: https://groups.google.com/g/blockly/c/LXnMujtEzJY/m/FKQjI4OwAwAJ
document.addEventListener("mousedown", function (event) {
document.addEventListener("mousedown", function (event) {
const mainWorkspace = _Blockly.getMainWorkspace();

// Gets the x and y position of the cursor relative to the workspace's parent svg element.
Expand Down Expand Up @@ -208,7 +245,23 @@ const BF2042PortalExtensions = (function () {
console.log(`[ERROR] ${message}`, error);
}

function cssFixes() {
const styleElement = document.createElement("style");
styleElement.setAttribute("type", "text/css");

styleElement.innerHTML = `
.blocklyMenu {
overflow-y: hidden !important;
}
`;

document.head.appendChild(styleElement);
}

function init() {
cssFixes();

_Blockly.ContextMenuRegistry.registry.register(deleteModBlock);
_Blockly.ContextMenuRegistry.registry.register(toggleComments);
_Blockly.ContextMenuRegistry.registry.register(toggleInputs);
_Blockly.ContextMenuRegistry.registry.register(toggleCollapse);
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "BF2042 Portal Extensions",
"description": "Adds additional functionality to the BF2042 Portal Rules Editor.",
"version": "1.0.2",
"version": "1.0.3",
"manifest_version": 3,
"icons": {
"128": "icon-128.png"
Expand Down

0 comments on commit 807dfa0

Please sign in to comment.