scratch-blocks 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +140 -0
- package/dist/main.mjs +1 -1
- package/dist/types/src/blocks/procedures.d.ts +1 -1
- package/dist/types/src/blocks/procedures.d.ts.map +1 -1
- package/dist/types/src/events/events_block_comment_base.d.ts.map +1 -1
- package/dist/types/src/events/events_block_drag_end.d.ts.map +1 -1
- package/dist/types/src/events/events_block_drag_outside.d.ts.map +1 -1
- package/dist/types/src/fields/field_colour_slider.d.ts +4 -4
- package/dist/types/src/fields/field_colour_slider.d.ts.map +1 -1
- package/dist/types/src/fields/field_matrix.d.ts.map +1 -1
- package/dist/types/src/fields/field_note.d.ts.map +1 -1
- package/dist/types/src/fields/scratch_field_number.d.ts.map +1 -1
- package/dist/types/src/fields/scratch_field_variable.d.ts +1 -1
- package/dist/types/src/fields/scratch_field_variable.d.ts.map +1 -1
- package/dist/types/src/flyout_checkbox_icon.d.ts.map +1 -1
- package/dist/types/src/procedures.d.ts.map +1 -1
- package/dist/types/src/renderer/cat/cat_face.d.ts.map +1 -1
- package/dist/types/src/renderer/cat/drawer.d.ts +1 -1
- package/dist/types/src/renderer/cat/drawer.d.ts.map +1 -1
- package/dist/types/src/renderer/cat/render_info.d.ts.map +1 -1
- package/dist/types/src/renderer/cat/renderer.d.ts +1 -1
- package/dist/types/src/renderer/cat/renderer.d.ts.map +1 -1
- package/dist/types/src/renderer/constants.d.ts +41 -0
- package/dist/types/src/renderer/constants.d.ts.map +1 -0
- package/dist/types/src/renderer/drawer.d.ts +2 -2
- package/dist/types/src/renderer/drawer.d.ts.map +1 -1
- package/dist/types/src/renderer/render_info.d.ts.map +1 -1
- package/dist/types/src/renderer/renderer.d.ts +1 -1
- package/dist/types/src/renderer/renderer.d.ts.map +1 -1
- package/dist/types/src/scratch_comment_bubble.d.ts +2 -2
- package/dist/types/src/scratch_comment_bubble.d.ts.map +1 -1
- package/dist/types/src/scratch_comment_icon.d.ts +1 -1
- package/dist/types/src/scratch_comment_icon.d.ts.map +1 -1
- package/dist/types/src/scratch_dragger.d.ts +1 -1
- package/dist/types/src/scratch_variable_model.d.ts +1 -1
- package/dist/types/src/scratch_variable_model.d.ts.map +1 -1
- package/dist/types/tests/jsunit/connection_db_test.d.ts +3 -3
- package/package.json +8 -8
- package/src/block_reporting.ts +2 -2
- package/src/blocks/control.ts +9 -2
- package/src/blocks/data.ts +34 -6
- package/src/blocks/procedures.ts +49 -31
- package/src/context_menu_items.ts +7 -7
- package/src/data_category.ts +4 -4
- package/src/events/events_block_comment_base.ts +8 -5
- package/src/events/events_block_comment_change.ts +4 -4
- package/src/events/events_block_comment_collapse.ts +14 -2
- package/src/events/events_block_comment_create.ts +4 -1
- package/src/events/events_block_comment_delete.ts +4 -2
- package/src/events/events_block_comment_move.ts +4 -4
- package/src/events/events_block_comment_resize.ts +4 -4
- package/src/events/events_block_drag_end.ts +4 -4
- package/src/events/events_block_drag_outside.ts +2 -2
- package/src/events/events_scratch_variable_create.ts +20 -2
- package/src/fields/field_colour_slider.ts +53 -28
- package/src/fields/field_matrix.ts +9 -8
- package/src/fields/field_note.ts +34 -27
- package/src/fields/field_textinput_removable.ts +2 -2
- package/src/fields/field_variable_getter.ts +5 -5
- package/src/fields/field_vertical_separator.ts +1 -1
- package/src/fields/scratch_field_angle.ts +14 -14
- package/src/fields/scratch_field_dropdown.ts +2 -2
- package/src/fields/scratch_field_number.ts +13 -12
- package/src/fields/scratch_field_variable.ts +8 -5
- package/src/flyout_checkbox_icon.ts +1 -1
- package/src/glows.ts +2 -2
- package/src/procedures.ts +25 -17
- package/src/renderer/cat/cat_face.ts +1 -1
- package/src/renderer/cat/drawer.ts +3 -3
- package/src/renderer/cat/render_info.ts +2 -2
- package/src/renderer/cat/renderer.ts +2 -2
- package/src/renderer/constants.ts +8 -8
- package/src/renderer/drawer.ts +2 -2
- package/src/renderer/render_info.ts +7 -4
- package/src/renderer/renderer.ts +2 -2
- package/src/scratch_block_paster.ts +1 -1
- package/src/scratch_comment_bubble.ts +16 -14
- package/src/scratch_comment_icon.ts +1 -1
- package/src/scratch_dragger.ts +2 -2
- package/src/scratch_variable_model.ts +2 -2
- package/src/status_indicator_label.ts +3 -3
- package/src/status_indicator_label_flyout_inflater.ts +1 -1
- package/src/variables.ts +7 -7
- package/src/xml.ts +3 -3
- package/tsconfig.json +1 -1
package/AGENTS.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Agent Guide: scratch-blocks
|
|
2
|
+
|
|
3
|
+
## Agent defaults
|
|
4
|
+
|
|
5
|
+
Use these defaults unless the user asks otherwise:
|
|
6
|
+
|
|
7
|
+
1. Keep changes minimal and scoped to the user request.
|
|
8
|
+
2. Prefer fixing root causes over adding surface-level assertions.
|
|
9
|
+
3. Never edit `node_modules/blockly/`; extend/override from `src/` instead.
|
|
10
|
+
4. When adding runtime guards for states that should never happen, log actionable context unless the path is expected
|
|
11
|
+
control flow.
|
|
12
|
+
5. Run `npm run test:lint` after meaningful code changes.
|
|
13
|
+
|
|
14
|
+
## What this repository is
|
|
15
|
+
|
|
16
|
+
`scratch-blocks` is a TypeScript library that provides the visual block editor for Scratch. It is **not** a fork of
|
|
17
|
+
Blockly — it is a library that **depends on** Blockly as an npm dependency and extends it with Scratch-specific block
|
|
18
|
+
definitions, fields, rendering, events, UI components, and variable/procedure management.
|
|
19
|
+
|
|
20
|
+
The compiled output (`dist/main.mjs`) is consumed by components in the `scratch-editor` mono-repo to render the block
|
|
21
|
+
palette and workspace.
|
|
22
|
+
|
|
23
|
+
## Build and lint
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm run build # Compile TypeScript and bundle with webpack → dist/main.mjs
|
|
27
|
+
npm run test:lint # Run ESLint
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
There are test files under `tests/` but no `npm test` script is wired up yet.
|
|
31
|
+
|
|
32
|
+
## Repository layout
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
src/
|
|
36
|
+
├── blocks/ Block definitions for each Scratch category
|
|
37
|
+
├── events/ Custom Blockly event subclasses
|
|
38
|
+
├── fields/ Custom Blockly field subclasses
|
|
39
|
+
├── renderer/ Custom renderer (ScratchRenderer) + cat blocks variant
|
|
40
|
+
└── index.ts Entry point; registers everything with Blockly
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Key top-level files: `procedures.ts`, `variables.ts`, `data_category.ts`,
|
|
44
|
+
`scratch_continuous_toolbox.ts`, `checkable_continuous_flyout.ts`, `scratch_comment_bubble.ts`.
|
|
45
|
+
|
|
46
|
+
## Blockly is a read-only dependency
|
|
47
|
+
|
|
48
|
+
The scratch-blocks codebase extends Blockly, but we do not control the Blockly source. Treat files under
|
|
49
|
+
`node_modules/blockly/` as read-only.
|
|
50
|
+
|
|
51
|
+
- If your change can be implemented by extending or overriding Blockly without modifying it, do that.
|
|
52
|
+
- If a Blockly change seems necessary, flag it to the human reviewer so they can decide whether to open a Blockly
|
|
53
|
+
issue or PR.
|
|
54
|
+
|
|
55
|
+
## Terminology
|
|
56
|
+
|
|
57
|
+
Blockly and Scratch have some overlapping but not identical terminology. These terms sometimes collide in Scratch
|
|
58
|
+
Blocks, so here are some definitions to clarify:
|
|
59
|
+
|
|
60
|
+
- Blockly's "Theme" matches up with Scratch's "Color Mode" — it defines block/category colors and can also affect
|
|
61
|
+
styling of other Blockly UI components. Examples of Scratch's color modes include "default" and "high contrast".
|
|
62
|
+
- Scratch's "Theme" is an orthogonal concept. Examples of Scratch's themes include "classic" and "cat blocks". To
|
|
63
|
+
disambiguate this from Blockly themes, we refer to this with `scratchTheme` in the `scratch-blocks` codebase.
|
|
64
|
+
- Scratch's block workspace or scripting area is the main Blockly workspace, but it isn't the only Blockly workspace.
|
|
65
|
+
Try to avoid using Blockly's `getMainWorkspace()` method, and instead try to retrieve or pass around a workspace
|
|
66
|
+
reference associated with the object or event in question.
|
|
67
|
+
- Scratch's "block palette" is called the "toolbox" in Blockly, and is implemented with the flyout, which hosts a
|
|
68
|
+
secondary Blockly workspace (a concept that Scratch glosses over).
|
|
69
|
+
|
|
70
|
+
## When managing npm dependencies
|
|
71
|
+
|
|
72
|
+
To install existing dependencies, use `npm ci` rather than `npm install` to ensure you get the exact versions
|
|
73
|
+
specified in `package-lock.json`.
|
|
74
|
+
|
|
75
|
+
When installing a new dependency or updating an existing one, run `npm install some-package@new-version` to update
|
|
76
|
+
both `package.json` and `package-lock.json` in one step.
|
|
77
|
+
|
|
78
|
+
Keep `package.json` in canonical order. In particular, make sure to alphabetize each dependency block (e.g.,
|
|
79
|
+
`dependencies`, `devDependencies`) and the `scripts` block.
|
|
80
|
+
|
|
81
|
+
## When adding runtime safety checks
|
|
82
|
+
|
|
83
|
+
When adding runtime checks for states that should never happen (including guard-driven early returns), avoid silent failure:
|
|
84
|
+
|
|
85
|
+
- Log a warning or error with enough context to debug (function path, block/event id, key flags).
|
|
86
|
+
- Use `console.warn` for recoverable states and `console.error` for invalid required data.
|
|
87
|
+
- Keep logs specific and consistent (e.g., include function or class context in the message).
|
|
88
|
+
- Do not add noisy logs for expected control flow (e.g., optional constructor args used by `fromJson`).
|
|
89
|
+
|
|
90
|
+
## TypeScript guidelines
|
|
91
|
+
|
|
92
|
+
### When using non-null assertions (`!`)
|
|
93
|
+
|
|
94
|
+
Use sparingly and only when you genuinely know — in a way the compiler cannot prove — that a value is not null or
|
|
95
|
+
undefined. Prefer type guards (`if (!x) return`), optional chaining (`?.`), or nullish coalescing (`??`) instead. The
|
|
96
|
+
`!` operator silences the compiler without adding any runtime safety, so a misplaced one becomes a runtime crash.
|
|
97
|
+
|
|
98
|
+
Before adding a new `!`, quickly check these alternatives:
|
|
99
|
+
|
|
100
|
+
- Can you narrow once and reuse a local (`const x = ...; if (!x) return;`)?
|
|
101
|
+
- Can the type carry the uncertainty directly (`prop?: T`, `T | null`) and be assigned conditionally?
|
|
102
|
+
- Can you use optional chaining for best-effort UI updates (`node?.setAttribute(...)`)?
|
|
103
|
+
- If data is required (e.g., mutation XML attrs), can you validate and fail clearly instead of asserting?
|
|
104
|
+
|
|
105
|
+
Prefer narrowing once and reusing a local variable over repeating assertions at each use site.
|
|
106
|
+
|
|
107
|
+
### When using type assertions (`as`)
|
|
108
|
+
|
|
109
|
+
Rely on TypeScript's inference and explicit type annotations first. Use `as SomeType` only when necessary (e.g., when
|
|
110
|
+
narrowing a union that inference can't resolve). Treat `as unknown as Type` as a last resort, and add a comment
|
|
111
|
+
explaining why it is both necessary and safe.
|
|
112
|
+
|
|
113
|
+
## Common patterns
|
|
114
|
+
|
|
115
|
+
- **Block definitions** use `Blockly.Blocks["type_name"] = { init() { this.jsonInit({...}) } }` and are grouped by
|
|
116
|
+
category in `src/blocks/`.
|
|
117
|
+
|
|
118
|
+
- **Custom fields** subclass Blockly field types (e.g., `ScratchFieldVariable extends Blockly.FieldVariable`) and
|
|
119
|
+
override lifecycle methods like `initModel()`, `doValueUpdate_()`, and `showEditor_()`.
|
|
120
|
+
|
|
121
|
+
- **Custom renderer** (`ScratchRenderer`) extends `Blockly.zelos.Renderer` and overrides `makeConstants_()`,
|
|
122
|
+
`makeDrawer_()`, and `makeRenderInfo_()`. When overriding inherited properties to narrow their type (e.g.,
|
|
123
|
+
`constants_: ConstantProvider`), use `declare` rather than re-declaring with an initializer.
|
|
124
|
+
|
|
125
|
+
- **Custom events** extend `BlockCommentBase` or `Blockly.Events.Abstract`. They implement `toJson()` /
|
|
126
|
+
`static fromJson()` for serialization. When constructors take optional parameters (because `fromJson` calls the
|
|
127
|
+
class with no arguments and sets properties directly afterward), use conditional assignment
|
|
128
|
+
(`if (x !== undefined) this.prop = x`) rather than `!` assertions.
|
|
129
|
+
|
|
130
|
+
- **Procedures and variables** are managed in `src/procedures.ts` and `src/variables.ts`. The `ScratchProcedures`
|
|
131
|
+
namespace exposes callback hooks that the host application must set before calling procedure-related
|
|
132
|
+
functionality.
|
|
133
|
+
|
|
134
|
+
## Before submitting changes
|
|
135
|
+
|
|
136
|
+
- Confirm changes are confined to requested scope.
|
|
137
|
+
- Confirm no edits were made under `node_modules/blockly/`.
|
|
138
|
+
- If you added runtime checks for states that should never happen (especially early-outs), confirm diagnostics were
|
|
139
|
+
added where appropriate.
|
|
140
|
+
- Run `npm run test:lint`.
|