Skip to content

Commit

Permalink
feature: enabled TS strict mode, resolved TS errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sdthaker committed Nov 19, 2023
1 parent 6d1d5a5 commit d914186
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 57 deletions.
71 changes: 47 additions & 24 deletions plugins/workspace-backpack/src/backpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ export class Backpack
this.initFlyout();
this.createDom();
this.attachListeners();
registerContextMenus(this.options.contextMenu, this.workspace_);
if (this.options.contextMenu) {
registerContextMenus(this.options.contextMenu, this.workspace_);
}
this.initialized_ = true;
this.workspace_.resize();
}
Expand Down Expand Up @@ -167,7 +169,8 @@ export class Backpack
'rtl': this.workspace_.RTL,
'oneBasedIndex': this.workspace_.options.oneBasedIndex,
'renderer': this.workspace_.options.renderer,
'rendererOverrides': this.workspace_.options.rendererOverrides,
'rendererOverrides':
this.workspace_.options.rendererOverrides || undefined,
'move': {
'scrollbars': true,
},
Expand All @@ -183,7 +186,9 @@ export class Backpack
this.workspace_.options,
true,
);
this.flyout_ = new HorizontalFlyout(flyoutWorkspaceOptions);
if (HorizontalFlyout) {
this.flyout_ = new HorizontalFlyout(flyoutWorkspaceOptions);
}
} else {
flyoutWorkspaceOptions.toolboxPosition =
this.workspace_.toolboxPosition === Blockly.utils.toolbox.Position.RIGHT
Expand All @@ -194,10 +199,13 @@ export class Backpack
this.workspace_.options,
true,
);
this.flyout_ = new VerticalFlyout(flyoutWorkspaceOptions);
if (VerticalFlyout) {
this.flyout_ = new VerticalFlyout(flyoutWorkspaceOptions);
}
}
// Add flyout to DOM.
const parentNode = this.workspace_.getParentSvg().parentNode;
if (!parentNode || !this.flyout_) return;
parentNode.appendChild(this.flyout_.createDom(Blockly.utils.Svg.SVG));
this.flyout_.init(this.workspace_);
}
Expand Down Expand Up @@ -253,6 +261,7 @@ export class Backpack
* Attaches event listeners.
*/
protected attachListeners() {
if (!this.svgGroup_) return;
this.addEvent(
this.svgGroup_,
'mousedown',
Expand Down Expand Up @@ -407,10 +416,12 @@ export class Backpack
}
}

this.svgGroup_.setAttribute(
'transform',
`translate(${this.left_},${this.top_})`,
);
if (this.svgGroup_) {
this.svgGroup_.setAttribute(
'transform',
`translate(${this.left_},${this.top_})`,
);
}
}

/**
Expand Down Expand Up @@ -462,20 +473,22 @@ export class Backpack
const keys = ['id', 'height', 'width', 'pinned', 'enabled'];

// Traverse the JSON recursively.
const traverseJson = function (json, keys) {
const traverseJson = function (json: StateWithIndex, keys: string[]) {
for (const key in json) {
if (key) {
if (keys.includes(key)) {
delete json[key];
}
if (json[key] && typeof json[key] === 'object') {
traverseJson(json[key], keys);
traverseJson(json[key] as StateWithIndex, keys);
}
}
}
};

traverseJson(json, keys);
if (json) {
traverseJson(json as StateWithIndex, keys);
}
return JSON.stringify(json);
}

Expand Down Expand Up @@ -619,7 +632,7 @@ export class Backpack
this.maybeRefreshFlyoutContents();
Blockly.Events.fire(new BackpackChange(this.workspace_.id));

if (!this.options.useFilledBackpackImage) return;
if (!this.options.useFilledBackpackImage || !this.svgImg_) return;
if (this.contents_.length > 0) {
this.svgImg_.setAttributeNS(
Blockly.utils.dom.XLINK_NS,
Expand Down Expand Up @@ -665,7 +678,7 @@ export class Backpack
* @returns Whether the backpack is open.
*/
isOpen(): boolean {
return this.flyout_.isVisible();
return this.flyout_ ? this.flyout_.isVisible() : false;
}

/**
Expand All @@ -676,7 +689,7 @@ export class Backpack
return;
}
const jsons = this.contents_.map((text) => JSON.parse(text));
this.flyout_.show(jsons);
if (this.flyout_) this.flyout_.show(jsons);
// TODO: We can remove the setVisible check when updating from ^10.0.0 to
// ^11.
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand All @@ -698,7 +711,7 @@ export class Backpack
return;
}
const json = this.contents_.map((text) => JSON.parse(text));
this.flyout_.show(json);
if (this.flyout_) this.flyout_.show(json);
}

/**
Expand All @@ -708,7 +721,7 @@ export class Backpack
if (!this.isOpen()) {
return;
}
this.flyout_.hide();
if (this.flyout_) this.flyout_.hide();
// TODO: We can remove the setVisible check when updating from ^10.0.0 to
// ^11.
/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down Expand Up @@ -741,8 +754,8 @@ export class Backpack
*
* @param e Mouse event.
*/
protected onClick(e: MouseEvent) {
if (Blockly.browserEvents.isRightButton(e)) {
protected onClick(e: Event) {
if (e instanceof MouseEvent && Blockly.browserEvents.isRightButton(e)) {
return;
}
this.open();
Expand Down Expand Up @@ -799,10 +812,12 @@ export class Backpack
*/
protected updateHoverStying(addClass: boolean) {
const backpackDarken = 'blocklyBackpackDarken';
if (addClass) {
Blockly.utils.dom.addClass(this.svgImg_, backpackDarken);
} else {
Blockly.utils.dom.removeClass(this.svgImg_, backpackDarken);
if (this.svgImg_) {
if (addClass) {
Blockly.utils.dom.addClass(this.svgImg_, backpackDarken);
} else {
Blockly.utils.dom.removeClass(this.svgImg_, backpackDarken);
}
}
}

Expand All @@ -825,8 +840,12 @@ export class Backpack
*
* @param e A mouse down event.
*/
protected blockMouseDownWhenOpenable(e: MouseEvent) {
if (!Blockly.browserEvents.isRightButton(e) && this.isOpenable()) {
protected blockMouseDownWhenOpenable(e: Event) {
if (
e instanceof MouseEvent &&
!Blockly.browserEvents.isRightButton(e) &&
this.isOpenable()
) {
e.stopPropagation(); // Don't start a workspace scroll.
}
}
Expand Down Expand Up @@ -957,3 +976,7 @@ class BackpackSerializer {
backpack?.empty();
}
}

interface StateWithIndex extends Blockly.serialization.blocks.State {
[key: string]: unknown;
}
70 changes: 44 additions & 26 deletions plugins/workspace-backpack/src/backpack_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ import {BackpackContextMenuOptions} from './options';
*/
function registerEmptyBackpack(workspace: Blockly.WorkspaceSvg) {
const prevConfigureContextMenu = workspace.configureContextMenu;
workspace.configureContextMenu = (menuOptions, e: PointerEvent) => {
workspace.configureContextMenu = (menuOptions, e: Event) => {
const backpack = workspace
.getComponentManager()
.getComponent('backpack') as Backpack;
if (!backpack || !backpack.getClientRect().contains(e.clientX, e.clientY)) {
prevConfigureContextMenu &&
prevConfigureContextMenu.call(null, menuOptions, e);
return;
const backpackClientRect = backpack && backpack.getClientRect();
if (e instanceof PointerEvent && backpackClientRect !== null) {
if (!backpack || !backpackClientRect.contains(e.clientX, e.clientY)) {
prevConfigureContextMenu &&
prevConfigureContextMenu.call(null, menuOptions, e);
return;
}
}
menuOptions.length = 0;
const backpackOptions = {
Expand Down Expand Up @@ -58,18 +61,26 @@ function registerRemoveFromBackpack() {
const removeFromBackpack = {
displayText: Blockly.Msg['REMOVE_FROM_BACKPACK'],
preconditionFn: function (scope: Blockly.ContextMenuRegistry.Scope) {
const ws = scope.block.workspace;
if (ws.isFlyout && ws.targetWorkspace) {
const backpack = ws.targetWorkspace
.getComponentManager()
.getComponent('backpack') as Backpack;
if (backpack && backpack.getFlyout().getWorkspace().id === ws.id) {
return 'enabled';
if (scope.block) {
const ws = scope.block.workspace;
if (ws.isFlyout && ws.targetWorkspace) {
const backpack = ws.targetWorkspace
.getComponentManager()
.getComponent('backpack') as Backpack;
const backpackFlyout = backpack && backpack.getFlyout();
if (
backpack &&
backpackFlyout &&
backpackFlyout.getWorkspace().id === ws.id
) {
return 'enabled';
}
}
}
return 'hidden';
},
callback: function (scope: Blockly.ContextMenuRegistry.Scope) {
if (!scope.block || !scope.block.workspace.targetWorkspace) return;
const backpack = scope.block.workspace.targetWorkspace
.getComponentManager()
.getComponent('backpack') as Backpack;
Expand All @@ -96,7 +107,7 @@ function registerCopyToBackpack(disablePreconditionContainsCheck: boolean) {
const copyToBackpack = {
displayText: function (scope: Blockly.ContextMenuRegistry.Scope) {
if (!scope.block) {
return;
return '';
}
const backpack = scope.block.workspace
.getComponentManager()
Expand All @@ -105,21 +116,24 @@ function registerCopyToBackpack(disablePreconditionContainsCheck: boolean) {
return `${Blockly.Msg['COPY_TO_BACKPACK']} (${backpackCount})`;
},
preconditionFn: function (scope: Blockly.ContextMenuRegistry.Scope) {
const ws = scope.block.workspace;
if (!ws.isFlyout) {
const backpack = ws
.getComponentManager()
.getComponent('backpack') as Backpack;
if (backpack) {
if (disablePreconditionContainsCheck) {
return 'enabled';
if (scope.block) {
const ws = scope.block.workspace;
if (!ws.isFlyout) {
const backpack = ws
.getComponentManager()
.getComponent('backpack') as Backpack;
if (backpack) {
if (disablePreconditionContainsCheck) {
return 'enabled';
}
return backpack.containsBlock(scope.block) ? 'disabled' : 'enabled';
}
return backpack.containsBlock(scope.block) ? 'disabled' : 'enabled';
}
}
return 'hidden';
},
callback: function (scope: Blockly.ContextMenuRegistry.Scope) {
if (!scope.block) return;
const backpack = scope.block.workspace
.getComponentManager()
.getComponent('backpack') as Backpack;
Expand All @@ -145,7 +159,7 @@ function registerCopyAllBackpack() {
displayText: Blockly.Msg['COPY_ALL_TO_BACKPACK'],
preconditionFn: function (scope: Blockly.ContextMenuRegistry.Scope) {
const ws = scope.workspace;
if (!ws.isFlyout) {
if (ws && !ws.isFlyout) {
const backpack = ws.getComponentManager().getComponent('backpack');
if (backpack) {
return 'enabled';
Expand All @@ -155,6 +169,7 @@ function registerCopyAllBackpack() {
},
callback: function (scope: Blockly.ContextMenuRegistry.Scope) {
const ws = scope.workspace;
if (!ws) return;
const backpack = ws
.getComponentManager()
.getComponent('backpack') as Backpack;
Expand All @@ -179,7 +194,7 @@ function registerPasteAllBackpack() {
const pasteAllFromBackpack = {
displayText: function (scope: Blockly.ContextMenuRegistry.Scope) {
if (!scope.workspace) {
return;
return '';
}
const backpack = scope.workspace
.getComponentManager()
Expand All @@ -189,7 +204,7 @@ function registerPasteAllBackpack() {
},
preconditionFn: function (scope: Blockly.ContextMenuRegistry.Scope) {
const ws = scope.workspace;
if (!ws.isFlyout) {
if (ws && !ws.isFlyout) {
const backpack = ws.getComponentManager().getComponent('backpack');
if (backpack) {
return 'enabled';
Expand All @@ -199,6 +214,7 @@ function registerPasteAllBackpack() {
},
callback: function (scope: Blockly.ContextMenuRegistry.Scope) {
const ws = scope.workspace;
if (!ws) return;
const backpack = ws
.getComponentManager()
.getComponent('backpack') as Backpack;
Expand Down Expand Up @@ -236,7 +252,9 @@ export function registerContextMenus(
registerRemoveFromBackpack();
}
if (contextMenuOptions.copyToBackpack) {
registerCopyToBackpack(contextMenuOptions.disablePreconditionChecks);
registerCopyToBackpack(
contextMenuOptions.disablePreconditionChecks ?? false,
);
}
if (contextMenuOptions.copyAllToBackpack) {
registerCopyAllBackpack();
Expand Down
2 changes: 1 addition & 1 deletion plugins/workspace-backpack/src/ui_events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class BackpackOpen extends Blockly.Events.UiBase {
* @returns JSON representation.
*/
toJson(): BackpackOpenEventJson {
const json = super.toJson();
const json = super.toJson() as BackpackOpenEventJson;
json['isOpen'] = this.isOpen;
return json;
}
Expand Down
9 changes: 4 additions & 5 deletions plugins/workspace-backpack/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ document.addEventListener('DOMContentLoaded', function () {
const defaultOptions = {
toolbox: toolboxCategories,
};
createPlayground(
document.getElementById('root'),
createWorkspace,
defaultOptions,
);
const rootElement = document.getElementById('root');
if (rootElement instanceof HTMLElement) {
createPlayground(rootElement, createWorkspace, defaultOptions);
}
});
2 changes: 1 addition & 1 deletion plugins/workspace-backpack/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

0 comments on commit d914186

Please sign in to comment.