Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable strict mode for workspace-minimap #2078

Merged
merged 13 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
20 changes: 12 additions & 8 deletions plugins/workspace-minimap/src/focus_region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const borderRadius = 6;
* A class that highlights the user's viewport on the minimap.
*/
export class FocusRegion {
private onChangeWrapper: (e: Blockly.Events.Abstract) => void;
private svgGroup: SVGElement;
private rect: SVGElement;
private background: SVGElement;
private onChangeWrapper: ((e: Blockly.Events.Abstract) => void) | null;
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
private svgGroup: SVGElement | null;
private rect: SVGElement | null;
private background: SVGElement | null;
private id: string;
private initialized = false;

Expand All @@ -45,6 +45,10 @@ export class FocusRegion {
private minimapWorkspace: Blockly.WorkspaceSvg,
) {
this.id = String(Math.random()).substring(2);
this.onChangeWrapper = (e: Blockly.Events.Abstract) => {};
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
this.svgGroup = document.createElementNS('http://www.w3.org/2000/svg', 'g');
this.rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
this.background = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -187,9 +191,9 @@ export class FocusRegion {
top += (minimapSvg.height - minimapContent.height) / 2;

// Set the svg attributes.
this.rect.setAttribute('transform', `translate(${left},${top})`);
this.rect.setAttribute('width', width.toString());
this.rect.setAttribute('height', height.toString());
this.rect?.setAttribute('transform', `translate(${left},${top})`);
this.rect?.setAttribute('width', width.toString());
this.rect?.setAttribute('height', height.toString());
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -206,4 +210,4 @@ Blockly.Css.register(`
.blockly-focus-region {
fill: #e6e6e6;
}
`);
`);
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 22 additions & 15 deletions plugins/workspace-minimap/src/minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export class Minimap {
protected primaryWorkspace: Blockly.WorkspaceSvg;
protected minimapWorkspace: Blockly.WorkspaceSvg;
protected focusRegion: FocusRegion;
protected onMouseMoveWrapper: Blockly.browserEvents.Data;
protected onMouseDownWrapper: Blockly.browserEvents.Data;
protected onMouseUpWrapper: Blockly.browserEvents.Data;
protected minimapWrapper: HTMLDivElement;
protected onMouseMoveWrapper: Blockly.browserEvents.Data | null = null;
protected onMouseDownWrapper: Blockly.browserEvents.Data | null = null;
protected onMouseUpWrapper: Blockly.browserEvents.Data | null = null;
protected minimapWrapper: HTMLDivElement | null = null;

/**
* Constructor for a minimap.
Expand All @@ -44,6 +44,9 @@ export class Minimap {
*/
constructor(workspace: Blockly.WorkspaceSvg) {
this.primaryWorkspace = workspace;
this.minimapWorkspace = new Blockly.WorkspaceSvg(new Blockly.Options({}));
this.focusRegion = new FocusRegion(this.primaryWorkspace, this.minimapWorkspace);
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
this.minimapWrapper = null;
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -58,7 +61,7 @@ export class Minimap {
// Make the wrapper a sibling to the primary injection div.
const primaryInjectParentDiv =
this.primaryWorkspace.getInjectionDiv().parentNode;
primaryInjectParentDiv.appendChild(this.minimapWrapper);
primaryInjectParentDiv?.appendChild(this.minimapWrapper);

// Inject the minimap workspace.
this.minimapWorkspace = Blockly.inject(this.minimapWrapper.id, {
Expand All @@ -74,15 +77,15 @@ export class Minimap {
// Remove the scale bounds of the minimap so that it can
// correctly zoomToFit.
zoom: {
maxScale: null,
minScale: null,
maxScale: Infinity,
minScale: 0,
},
readOnly: true,
theme: this.primaryWorkspace.getTheme(),
renderer: this.primaryWorkspace.options.renderer,
});

this.minimapWorkspace.scrollbar.setContainerVisible(false);
this.minimapWorkspace.scrollbar?.setContainerVisible(false);
this.primaryWorkspace.addChangeListener((e) => void this.mirror(e));
window.addEventListener('resize', () => {
this.minimapWorkspace.zoomToFit();
Expand All @@ -97,12 +100,16 @@ export class Minimap {
this,
this.onClickDown,
);
this.onMouseUpWrapper = Blockly.browserEvents.bind(
primaryInjectParentDiv,
'mouseup',
this,
this.onClickUp,
);
if(primaryInjectParentDiv === null) {
throw new Error("primaryInjectParentDiv is null");
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
} else {
this.onMouseUpWrapper = Blockly.browserEvents.bind(
primaryInjectParentDiv,
'mouseup',
this,
this.onClickUp,
);
}

// Initializes the focus region.
this.focusRegion = new FocusRegion(
Expand Down Expand Up @@ -270,4 +277,4 @@ export class Minimap {
isFocusEnabled(): boolean {
return this.focusRegion.isEnabled();
}
}
}
22 changes: 13 additions & 9 deletions plugins/workspace-minimap/src/positioned_minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,23 @@ export class PositionedMinimap
*/
private setAttributes(): void {
const injectDiv = this.minimapWorkspace.getInjectionDiv();
const style = injectDiv.parentElement.style;
style.zIndex = '2';
style.position = 'absolute';
style.width = `${this.width}px`;
style.height = `${this.height}px`;
style.top = `${this.top}px`;
style.left = `${this.left}px`;
Blockly.svgResize(this.minimapWorkspace);
if(injectDiv.parentElement === null) {
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
return;
} else {
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved
const style = injectDiv.parentElement.style;
style.zIndex = '2';
style.position = 'absolute';
style.width = `${this.width}px`;
style.height = `${this.height}px`;
style.top = `${this.top}px`;
style.left = `${this.left}px`;
Blockly.svgResize(this.minimapWorkspace);
}
}
}

Blockly.Css.register(`
.blockly-minimap {
box-shadow: 2px 2px 10px grey;
}
`);
`);
14 changes: 10 additions & 4 deletions plugins/workspace-minimap/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import * as Blockly from 'blockly';
import {toolboxCategories, createPlayground} from '@blockly/dev-tools';
import {PositionedMinimap} from '../src/index';

let minimap = null;
let workspace = null;
let minimap: PositionedMinimap | null = null;

/**
* Create a workspace.
Expand All @@ -30,15 +29,22 @@ function createWorkspace(
if (minimap) {
minimap.dispose();
}
workspace = Blockly.inject(blocklyDiv, options);
const workspace = Blockly.inject(blocklyDiv, options);
minimap = new PositionedMinimap(workspace);
minimap.init();

return workspace;
}


document.addEventListener('DOMContentLoaded', function () {
createPlayground(document.getElementById('root'), createWorkspace, {
const rootElement = document.getElementById('root');
if (rootElement === null) {
console.error("No element with id 'root' found");
return;
}
createPlayground(rootElement, createWorkspace, {
toolbox: toolboxCategories,
});
});

5 changes: 5 additions & 0 deletions plugins/workspace-minimap/test/minimap_tests.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const assert = chai.assert;
const Blockly = require('blockly');
const {Minimap} = require('../src/minimap');
const {PositionedMinimap} = require('../src/positioned_minimap');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;

const { document } = (new JSDOM('')).window;
global.document = document;
Namatuzio marked this conversation as resolved.
Show resolved Hide resolved

suite(
'Converting click coordinates from minimap to primary workspace',
Expand Down
2 changes: 1 addition & 1 deletion plugins/workspace-minimap/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"module": "es2015",
"moduleResolution": "bundler",
"target": "es6",
"strict": false,
"strict": true,
// Point at the local Blockly. See #1934. Remove if we add hoisting.
"paths": {
"blockly/*": ["node_modules/blockly/*"]
Expand Down
Loading