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
1 change: 1 addition & 0 deletions pxtblocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export * from "./diff";
export * from "./legacyMutations";
export * from "./blockDragger";
export * from "./workspaceSearch";
export * from "./monkeyPatches";

import * as contextMenu from "./contextMenu";
import * as external from "./external";
Expand Down
3 changes: 0 additions & 3 deletions pxtblocks/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { initVariables } from "./builtins/variables";
import { initOnStart } from "./builtins/misc";
import { initContextMenu } from "./contextMenu";
import { renderCodeCard } from "./codecardRenderer";
import { applyMonkeyPatches } from "./monkeyPatches";
import { FieldDropdown } from "./fields/field_dropdown";
import { setDraggableShadowBlocks, setDuplicateOnDragStrategy } from "./plugins/duplicateOnDrag";

Expand Down Expand Up @@ -588,8 +587,6 @@ function init(blockInfo: pxtc.BlocksInfo) {
if (blocklyInitialized) return;
blocklyInitialized = true;

applyMonkeyPatches();

initFieldEditors();
initContextMenu();
initOnStart();
Expand Down
73 changes: 73 additions & 0 deletions pxtblocks/monkeyPatches/grid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as Blockly from "blockly";

interface ExtendedGridOptions extends Blockly.Options.GridOptions {
image?: {
path: string;
width: string;
height: string;
opacity: number;
}
}

export function monkeyPatchGrid() {
const options = pxt.appTarget.appTheme.blocklyOptions?.grid as ExtendedGridOptions;

if (!options?.image?.path) return;

const gridPatternIds: string[] = [];

Blockly.Grid.createDom = function (rnd: string, gridOptions: Blockly.Options.GridOptions, defs: SVGElement) {
const id = "blocklyGridPattern" + rnd;

const gridPattern = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.PATTERN,
{
id,
patternUnits: "userSpaceOnUse",
width: options.image.width,
height: options.image.height
},
defs,
);

gridPatternIds.push(id)

const image = Blockly.utils.dom.createSvgElement(
Blockly.utils.Svg.IMAGE,
{
width: options.image.width,
height: options.image.height,
opacity: options.image.opacity
},
gridPattern
);

image.setAttributeNS("http://www.w3.org/1999/xlink", "xlink:href", options.image.path)

return gridPattern;
}

const oldGridUpdate = Blockly.Grid.prototype.update;

Blockly.Grid.prototype.update = function (this: Blockly.Grid, scale: number) {
oldGridUpdate.call(this, scale);

const patternsToRemove: string[] = [];
for (const patternId of gridPatternIds) {
const imagePattern = document.getElementById(patternId) as unknown as SVGPatternElement;

if (!imagePattern) {
patternsToRemove.push(patternId);
continue;
}

imagePattern.setAttribute("width", options.image.width);
imagePattern.setAttribute("height", options.image.height);
imagePattern.setAttribute('patternTransform', 'scale(' + scale + ')');
}

for (const patternId of patternsToRemove) {
gridPatternIds.splice(gridPatternIds.indexOf(patternId), 1);
}
}
}
2 changes: 2 additions & 0 deletions pxtblocks/monkeyPatches/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { monkeyPatchBlockSvg } from "./blockSvg";
import { monkeyPatchGrid } from "./grid";

export function applyMonkeyPatches() {
monkeyPatchBlockSvg();
monkeyPatchGrid();
}
1 change: 1 addition & 0 deletions webapp/src/blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,7 @@ export class Editor extends toolboxeditor.ToolboxEditor {
loadBlocklyAsync() {
if (!this._loadBlocklyPromise) {
pxt.perf.measureStart("loadBlockly")
pxtblockly.applyMonkeyPatches();
this._loadBlocklyPromise = pxt.BrowserUtils.loadBlocklyAsync()
.then(() => {
// Initialize the "Make a function" button
Expand Down