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

refactor: migrate scratch-blocks to Typescript #224

Merged
merged 125 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
125 commits
Select commit Hold shift + click to select a range
dd0bbcd
chore: rename checkbox_buble.js to checkbox_bubble.ts
gonfunko Oct 15, 2024
dcfc592
refactor: convert CheckboxBubble to TS
gonfunko Oct 15, 2024
e8ddbaf
chore: rename status_indicator_label.js to status_indicator_label.ts
gonfunko Oct 15, 2024
040b035
refactor: convert StatusIndicatorLabel to Typescript
gonfunko Oct 15, 2024
65ce0dd
chore: rename status_indicator_label_flyout_inflater.js to status_ind…
gonfunko Oct 15, 2024
8a137e2
refactor: convert StatusIndicatorLabelFlyoutInflater to TS
gonfunko Oct 15, 2024
a0b00af
chore: rename recyclable_block_flyout_inflater.js to recyclable_block…
gonfunko Oct 15, 2024
48331a6
refactor: convert RecyclableBlockFlyoutInflater to TS
gonfunko Oct 15, 2024
6551ad2
chore: rename procedures.js to procedures.ts
gonfunko Oct 15, 2024
f8be7ca
refactor: modernize and convert procedures.ts to Typescript
gonfunko Oct 15, 2024
2730eb1
chore: rename variables.js to variables.ts
gonfunko Oct 16, 2024
c33e8a6
refactor: convert variables.ts to Typescript
gonfunko Oct 16, 2024
eea99ef
refactor: modernize variables.ts
gonfunko Oct 16, 2024
fd1bec2
chore: rename shadows.js to shadows.ts
gonfunko Oct 16, 2024
1c54664
refactor: convert shadows.ts to Typescript
gonfunko Oct 16, 2024
9d2e956
chore: rename scratch_variable_model.js to scratch_variable_model.ts
gonfunko Oct 16, 2024
f967c8a
refactor: convert ScratchVariableModel to Typescript
gonfunko Oct 16, 2024
1d069bb
chore: rename scratch_variable_map.js to scratch_variable_map.ts
gonfunko Oct 16, 2024
12e3d9b
refactor: convert ScratchVariableMap to Typescript
gonfunko Oct 16, 2024
108c429
chore: rename scratch_dragger.js to scratch_dragger.ts
gonfunko Oct 16, 2024
8dc70a6
refactor: convert ScratchDragger to Typescript
gonfunko Oct 16, 2024
5ec7e6f
chore: add TSDoc to ScratchDragger
gonfunko Oct 16, 2024
2fac9a9
chore: rename scratch_continuous_toolbox.js to scratch_continuous_too…
gonfunko Oct 16, 2024
144dbe8
refactor: convert ScratchContinuousToolbox to Typescript
gonfunko Oct 16, 2024
01c021b
chore: rename scratch_continuous_category.js to scratch_continuous_ca…
gonfunko Oct 16, 2024
a468113
refactor: convert ScratchContinuousCategory to Typescript
gonfunko Oct 16, 2024
5ec527c
chore: rename scratch_connection_checker.js to scratch_connection_che…
gonfunko Oct 16, 2024
ea66345
refactor: convert ScratchConnectionChecker to Typescript
gonfunko Oct 16, 2024
724f096
chore: rename scratch_comment_icon.js to scratch_comment_icon.ts
gonfunko Oct 16, 2024
f2b09b6
refactor: convert ScratchCommentIcon to Typescript
gonfunko Oct 16, 2024
e4b2378
chore: rename data_category.js to data_category.ts
gonfunko Oct 17, 2024
5158b1c
refactor: convert data_category.ts to Typescript
gonfunko Oct 17, 2024
2ed676c
refactor: modernize data_category.ts
gonfunko Oct 17, 2024
9ad6310
chore: rename scratch_block_paster.js to scratch_block_paster.ts
gonfunko Oct 17, 2024
443e2e2
refactor: convert ScratchBlockPaster to Typescript
gonfunko Oct 17, 2024
fbe6276
chore: rename glows.js to glows.ts
gonfunko Oct 17, 2024
123db04
refactor: convert glows.ts to Typescript
gonfunko Oct 17, 2024
24a0edb
chore: rename flyout_checkbox_icon.js to flyout_checkbox_icon.ts
gonfunko Oct 17, 2024
1102585
refactor: convert FlyoutCheckboxIcon to Typescript
gonfunko Oct 17, 2024
16376fa
refactor: convert css.js to Typescript
gonfunko Oct 17, 2024
caa1057
chore: rename context_menu_items.js to context_menu_items.ts
gonfunko Oct 17, 2024
a4eca72
refactor: convert context_menu_items.ts to Typescript
gonfunko Oct 17, 2024
0dba894
refactor: convert constants.js to Typescript
gonfunko Oct 17, 2024
09a742f
chore: add license to constants.ts
gonfunko Oct 17, 2024
c6e7b83
refactor: convert colours.js to Typescript
gonfunko Oct 17, 2024
b95d02a
chore: remove the unneded categories.js
gonfunko Oct 17, 2024
647b0f0
chore: rename block_reporting.js to block_reporting.ts
gonfunko Oct 17, 2024
eca4687
refactor: convert block_reporting.ts to Typescript
gonfunko Oct 17, 2024
1c31a47
chore: rename colour.js to colour.ts
gonfunko Oct 17, 2024
a458e31
refactor: convert colour.ts to Typescript
gonfunko Oct 17, 2024
4568d98
refactor: convert control.js to Typescript
gonfunko Oct 17, 2024
4146c9d
refactor: convert event.js to Typescript
gonfunko Oct 17, 2024
3792aff
refactor: convert looks.js to Typescript
gonfunko Oct 17, 2024
518bfda
refactor: convert math.js to Typescript
gonfunko Oct 17, 2024
af53553
refactor: convert matrix.js to Typescript
gonfunko Oct 17, 2024
68841c5
refactor: convert motion.js to Typescript
gonfunko Oct 17, 2024
9dc8b71
refactor: convert note.js to Typescript
gonfunko Oct 17, 2024
2af4cce
refactor: convert operators.js to Typescript
gonfunko Oct 17, 2024
28d62fe
refactor: convert sensing.js to Typescript
gonfunko Oct 17, 2024
c1d58b7
refactor: convert sound.js to Typescript
gonfunko Oct 17, 2024
bd5cccd
refactor: convert text.js to Typescript
gonfunko Oct 17, 2024
6d4b595
chore: rename data.js to data.ts
gonfunko Oct 17, 2024
429486c
refactor: convert data.ts to Typescript
gonfunko Oct 17, 2024
cefd8c8
chore: rename procedures.js to procedures.ts
gonfunko Oct 17, 2024
8fd8cac
refactor: convert procedures.ts to Typescript
gonfunko Oct 17, 2024
53e3171
refactor: modernize procedures.ts
gonfunko Oct 17, 2024
a848483
chore: rename vertical_extensions.js to vertical_extensions.ts
gonfunko Oct 17, 2024
2d7cb78
refactor: convert vertical_extensions.ts to Typescript
gonfunko Oct 17, 2024
c7c0ce4
chore: rename field_angle.js to scratch_field_angle.ts
gonfunko Oct 17, 2024
5bb1c0f
refactor: convert ScratchFieldAngle to Typescript
gonfunko Oct 18, 2024
93b2f13
refactor: clean up ScratchFieldAngle
gonfunko Oct 18, 2024
4615ac4
refactor: add typings for `this` to block definitions
gonfunko Oct 18, 2024
1d000c5
chore: rename field_variable.js to scratch_field_variable.ts
gonfunko Oct 18, 2024
da0697e
refactor: convert ScratchFieldVariable to Typescript
gonfunko Oct 18, 2024
51011a4
chore: rename field_vertical_separator.js to field_vertical_separator.ts
gonfunko Oct 18, 2024
cc51da9
refactor: convert FieldVerticalSeparator to Typescript
gonfunko Oct 18, 2024
1838212
chore: rename field_variable_getter.js to field_variable_getter.ts
gonfunko Oct 18, 2024
2fc751b
refactor: convert FieldVariableGetter to Typescript
gonfunko Oct 18, 2024
9004bd7
chore: rename field_textinput_removable.js to field_textinput_removab…
gonfunko Oct 18, 2024
9f1000d
refactor: convert FieldTextInputRemovable to Typescript
gonfunko Oct 18, 2024
3a7fcf8
chore: rename field_number.js to scratch_field_number.ts
gonfunko Oct 18, 2024
498966a
refactor: convert ScratchFieldNumber to Typescript
gonfunko Oct 18, 2024
316fa0a
chore: rename field_note.js to field_note.ts
gonfunko Oct 18, 2024
e749f38
refactor: convert FieldNote to Typescript
gonfunko Oct 18, 2024
6da5a73
chore: rename field_matrix.js to field_matrix.ts
gonfunko Oct 18, 2024
51e64ee
refactor: convert FieldMatrix to Typescript
gonfunko Oct 18, 2024
463912a
chore: rename field_dropdown.js to scratch_field_dropdown.ts
gonfunko Oct 18, 2024
2ee27f9
refactor: convert ScratchFieldDropdown to Typescript
gonfunko Oct 18, 2024
71c71ae
chore: rename field_colour_slider.js to field_colour_slider.ts
gonfunko Oct 18, 2024
205303a
refactor: convert FieldColourSlider to Typescript
gonfunko Oct 18, 2024
434bf84
chore: rename events_block_comment_base.js to events_block_comment_ba…
gonfunko Oct 21, 2024
1b16f7d
refactor: convert BlockCommentBase to Typescript
gonfunko Oct 21, 2024
6456eb8
chore: rename events_block_comment_change.js to events_block_comment_…
gonfunko Oct 21, 2024
be51907
refactor: convert BlockCommentChange to Typescript
gonfunko Oct 21, 2024
7396437
chore: rename events_block_comment_collapse.js to events_block_commen…
gonfunko Oct 21, 2024
64c83cd
refactor: convert BlockCommentCollapse to Typescript
gonfunko Oct 21, 2024
8159dfe
chore: rename events_block_comment_create.js to events_block_comment_…
gonfunko Oct 21, 2024
00c4bec
refactor: convert BlockCommentCreate to Typescript
gonfunko Oct 21, 2024
cad3478
chore: rename events_block_comment_delete.js to events_block_comment_…
gonfunko Oct 21, 2024
6a439e5
refactor: convert BlockCommentDelete to Typescript
gonfunko Oct 21, 2024
0f34fcb
chore: rename events_block_comment_move.js to events_block_comment_mo…
gonfunko Oct 21, 2024
ddf5365
refactor: convert BlockCommentMove to Typescript
gonfunko Oct 21, 2024
2d52b1e
chore: rename events_block_comment_resize.js to events_block_comment_…
gonfunko Oct 21, 2024
915fd0d
refactor: convert BlockCommentResize to Typescript
gonfunko Oct 21, 2024
d696249
chore: rename events_block_drag_end.js to events_block_drag_end.ts
gonfunko Oct 21, 2024
5107a5a
refactor: convert BlockDragEnd to Typescript
gonfunko Oct 21, 2024
a02f099
chore: rename events_block_drag_outside.js to events_block_drag_outsi…
gonfunko Oct 21, 2024
8f74c6f
refactor: convert BlockDragOutside to Typescript
gonfunko Oct 21, 2024
370a63b
chore: rename events_scratch_variable_create.js to events_scratch_var…
gonfunko Oct 21, 2024
fd2a021
refactor: convert ScratchVariableCreate to Typescript
gonfunko Oct 21, 2024
d12b9a0
chore: rename bowler_hat.js to bowler_hat.ts
gonfunko Oct 21, 2024
5abf7c9
refactor: convert BowlerHat to Typescript
gonfunko Oct 21, 2024
12a46ac
chore: rename constants.js to constants.ts
gonfunko Oct 21, 2024
25dcdda
refactor: convert ConstantProvider to Typescript
gonfunko Oct 21, 2024
f2e9c10
chore: rename drawer.js to drawer.ts
gonfunko Oct 21, 2024
7f30272
refactor: convert Drawer to Typescript
gonfunko Oct 21, 2024
cc54d89
chore: rename path_object.js to path_object.ts
gonfunko Oct 21, 2024
512717e
refactor: convert PathObject to Typescript
gonfunko Oct 21, 2024
c2693e5
chore: rename render_info.js to render_info.ts
gonfunko Oct 21, 2024
266b47b
refactor: convert RenderInfo to Typescript
gonfunko Oct 21, 2024
dfdfd94
chore: rename renderer.js to renderer.ts
gonfunko Oct 21, 2024
ed7e030
refactor: convert Renderer to Typescript
gonfunko Oct 21, 2024
9f83f5f
chore: rename scratch_blocks_utils.js to scratch_blocks_utils.ts
gonfunko Oct 22, 2024
0020746
refactor: remove unused functions
gonfunko Oct 22, 2024
ae9f848
refactor: convert scratch_blocks_utils.ts to Typescript
gonfunko Oct 22, 2024
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
2 changes: 1 addition & 1 deletion src/checkable_continuous_flyout.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import * as Blockly from "blockly/core";
import { ContinuousFlyout } from "@blockly/continuous-toolbox";
import { RecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater.js";
import { RecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater";
import { StatusIndicatorLabel } from "./status_indicator_label";

export class CheckableContinuousFlyout extends ContinuousFlyout {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { registerFieldTextInputRemovable } from "./fields/field_textinput_remova
import { registerFieldVariableGetter } from "./fields/field_variable_getter.js";
import { registerFieldVariable } from "./fields/field_variable.js";
import { registerFieldVerticalSeparator } from "./fields/field_vertical_separator.js";
import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater.js";
import { registerRecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater";
import { registerScratchBlockPaster } from "./scratch_block_paster.js";
import { registerStatusIndicatorLabelFlyoutInflater } from "./status_indicator_label_flyout_inflater";
import { registerScratchContinuousCategory } from "./scratch_continuous_category.js";
Expand Down
68 changes: 34 additions & 34 deletions src/recyclable_block_flyout_inflater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ import { CheckboxBubble } from "./checkbox_bubble";
export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
/**
* Whether or not block recycling is enabled.
* @type {boolean}
*/
recyclingEnabled = true;

/**
* Map from block type to block instance.
* @type {Map<string, !Blockly.BlockSvg>}
*/
recycledBlocks = new Map();
recycledBlocks = new Map<string, Blockly.BlockSvg>();

/**
* Creates a block on the flyout workspace from the given block definition.
*
* @param {!Object} state A JSON representation of a block to load.
* @param {!Blockly.WorkspaceSvg} flyoutWorkspace The flyout's workspace.
* @returns {!Blockly.BlockSvg} The newly created block.
* @param state A JSON representation of a block to load.
* @param flyoutWorkspace The flyout's workspace.
* @returns The newly created block.
*/
load(state, flyoutWorkspace) {
load(
state: Blockly.utils.toolbox.BlockInfo,
flyoutWorkspace: Blockly.WorkspaceSvg
): Blockly.IBoundedElement {
const block = super.load(state, flyoutWorkspace);
if (block.checkboxInFlyout) {
if ("checkboxInFlyout" in block && block.checkboxInFlyout) {
gonfunko marked this conversation as resolved.
Show resolved Hide resolved
block.moveBy(
CheckboxBubble.CHECKBOX_SIZE + CheckboxBubble.CHECKBOX_MARGIN,
0
Expand All @@ -45,19 +46,21 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
/**
* Toggles whether or not recycling is enabled.
*
* @param {boolean} enabled True if recycling should be enabled.
* @param enabled True if recycling should be enabled.
*/
setRecyclingEnabled(enabled) {
setRecyclingEnabled(enabled: boolean) {
this.recyclingEnabled = enabled;
}

/**
* Creates a new block from the given block definition.
*
* @param {!Object} blockDefinition The definition to create a block from.
* @returns {!Blockly.BlockSvg} The newly created block.
* @param blockDefinition The definition to create a block from.
* @returns The newly created block.
*/
createBlock(blockDefinition) {
createBlock(
blockDefinition: Blockly.utils.toolbox.BlockInfo
): Blockly.BlockSvg {
const blockType = this.getTypeFromDefinition(blockDefinition);
return (
this.getRecycledBlock(blockType) ??
Expand All @@ -68,15 +71,17 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
/**
* Returns the type of a block from an XML or JSON block definition.
*
* @param blockDefinition {!Object} The block definition to parse.
* @returns {string} The block type.
* @param blockDefinition The block definition to parse.
* @returns The block type.
*/
getTypeFromDefinition(blockDefinition) {
getTypeFromDefinition(
blockDefinition: Blockly.utils.toolbox.BlockInfo
): string {
if (blockDefinition["blockxml"]) {
const xml =
typeof blockDefinition["blockxml"] === "string"
? Blockly.utils.xml.textToDom(blockDefinition["blockxml"])
: blockDefinition["blockxml"];
: (blockDefinition["blockxml"] as Element);
return xml.getAttribute("type");
} else {
return blockDefinition["type"];
Expand All @@ -88,9 +93,9 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
* top of the workspace. Used during large workspace swaps to limit the number
* of new DOM elements we need to create.
*
* @param {!Blockly.BlockSvg} block The block to recycle.
* @param block The block to recycle.
*/
recycleBlock(block) {
recycleBlock(block: Blockly.BlockSvg) {
const xy = block.getRelativeToSurfaceXY();
block.moveBy(-xy.x, -xy.y);
this.recycledBlocks.set(block.type, block);
Expand All @@ -100,11 +105,10 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
* Returns a block from the cache of recycled blocks with the given type, or
* undefined if one cannot be found.
*
* @param {string} blockType The type of the block to try to recycle.
* @returns {?Blockly.BlockSvg} The recycled block, or undefined if
* one could not be recycled.
* @param blockType The type of the block to try to recycle.
* @returns The recycled block, or undefined if one could not be recycled.
*/
getRecycledBlock(blockType) {
getRecycledBlock(blockType: string): Blockly.BlockSvg | undefined {
const block = this.recycledBlocks.get(blockType);
this.recycledBlocks.delete(blockType);
return block;
Expand All @@ -113,10 +117,10 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
/**
* Returns whether the given block can be recycled or not.
*
* @param {!Blockly.BlockSvg} block The block to check for recyclability.
* @returns {boolean} True if the block can be recycled. False otherwise.
* @param block The block to check for recyclability.
* @returns True if the block can be recycled. False otherwise.
*/
blockIsRecyclable(block) {
blockIsRecyclable(block: Blockly.Block): boolean {
if (!this.recyclingEnabled) {
return false;
}
Expand Down Expand Up @@ -144,9 +148,7 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
}
// Check children.
if (input.connection) {
const targetBlock =
/** @type {Blockly.BlockSvg} */
(input.connection.targetBlock());
const targetBlock = input.connection.targetBlock();
if (targetBlock && !this.blockIsRecyclable(targetBlock)) {
return false;
}
Expand All @@ -158,9 +160,9 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
/**
* Disposes of the provided block.
*
* @param {!Blockly.BlockSvg} element The block to dispose of.
* @param element The block to dispose of.
*/
disposeElement(element) {
disposeElement(element: Blockly.BlockSvg) {
if (this.blockIsRecyclable(element)) {
this.removeListeners(element.id);
this.recycleBlock(element);
Expand All @@ -173,9 +175,7 @@ export class RecyclableBlockFlyoutInflater extends Blockly.BlockFlyoutInflater {
* Clears the cache of recycled blocks.
*/
emptyRecycledBlocks() {
this.recycledBlocks
.values()
.forEach((block) => block.dispose(false, false));
this.recycledBlocks.forEach((block) => block.dispose(false, false));
this.recycledBlocks.clear();
}
}
Expand Down