Skip to content
Draft
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
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions packages/plugins/lesson-authoring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# @blockly/plugin-lesson-authoring [![Built on Blockly](https://tinyurl.com/built-on-blockly)](https://github.com/RaspberryPiFoundation/blockly)

Tools for authoring Blockly lessons

> **Prototype.** This package is private and its API and artifact format will change
> without notice. Lesson files written by earlier versions may not load.

## Installation

### Yarn

```
yarn add @blockly/plugin-lesson-authoring
```

### npm

```
npm install @blockly/plugin-lesson-authoring --save
```

## Usage

```js
import * as Blockly from 'blockly';
import {findMissingRequirements} from '@blockly/plugin-lesson-authoring';

const artifact = JSON.parse(lessonJson);
const missing = findMissingRequirements(artifact, {
blockTypes: Object.keys(Blockly.Blocks),
});
if (missing.blockTypes.length) {
throw new Error(`Missing blocks: ${missing.blockTypes.join(', ')}`);
}
```

## API

### `LessonArtifact`

A lesson as portable JSON, currently:

- `title`: the lesson title.
- `toolbox`: a standard JSON toolbox definition (`ToolboxInfo`).
- `initialWorkspaceState`: the starter workspace, as saved by
`Blockly.serialization.workspaces.save`.

### `getRequirements(artifact)`

Returns the `LessonRequirements` an artifact depends on: currently `blockTypes`,
a sorted list of every block type used in the toolbox and initial workspace state.
Throws if the toolbox contains `blockxml` entries; only JSON block state is supported.

### `findMissingRequirements(artifact, supported)`

Returns the requirements of `artifact` that are not in `supported`. Hosts typically
pass `{blockTypes: Object.keys(Blockly.Blocks)}` (or a smaller subset of types).

## License

Apache 2.0
54 changes: 54 additions & 0 deletions packages/plugins/lesson-authoring/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@blockly/plugin-lesson-authoring",
"version": "13.4.0",
"description": "A set of tools for authoring Blockly lessons",
"scripts": {
"audit-fix": "blockly-scripts auditFix",
"build": "blockly-scripts build",
"clean": "blockly-scripts clean",
"lint": "eslint .",
"predeploy": "blockly-scripts predeploy",
"start": "blockly-scripts start",
"test": "blockly-scripts test"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"unpkg": "./dist/index.js",
"private": true,
"author": "Blockly Team",
"keywords": [
"blockly",
"blockly-plugin",
"lesson-authoring"
],
"homepage": "https://github.com/mikeharv/blockly/tree/lesson-editor-prototype/packages/plugins/lesson-authoring#readme",
"bugs": {
"url": "https://github.com/mikeharv/blockly/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/mikeharv/blockly.git",
"directory": "packages/plugins/lesson-authoring"
},
"license": "Apache-2.0",
"directories": {
"dist": "dist",
"src": "src"
},
"files": [
"dist",
"src"
],
"devDependencies": {
"@blockly/dev-scripts": "^13.4.0",
"@blockly/dev-tools": "^13.4.0",
"sinon": "^22.1.0",
"typescript": "^6.0.3"
},
"peerDependencies": {
"blockly": "^13.4.0"
},
"publishConfig": {
"access": "public"
}
}
26 changes: 26 additions & 0 deletions packages/plugins/lesson-authoring/src/artifact_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @license
* Copyright 2026 Raspberry Pi Foundation
* SPDX-License-Identifier: Apache-2.0
*/

import type * as Blockly from 'blockly/core';

export type WorkspaceState = ReturnType<
typeof Blockly.serialization.workspaces.save
>;

/** Names a lesson refers to that the host must provide. */
export interface LessonRequirements {
blockTypes: string[];
// Later, needed:
// dynamicCategories?: string[]; // toolbox `custom` values
// categoryStyles?: string[]; // `categorystyle` names
// buttonCallbacks?: string[]; // button `callbackKey`s
}

export interface LessonArtifact {
title: string;
toolbox: Blockly.utils.toolbox.ToolboxInfo;
initialWorkspaceState: WorkspaceState;
}
170 changes: 170 additions & 0 deletions packages/plugins/lesson-authoring/src/block_state_change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
/**
* @license
* Copyright 2026 Raspberry Pi Foundation
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview An event for changes to a block's deletable, editable or
* movable state, which core does not fire one for, so that those changes can
* be undone.
*/

import * as Blockly from 'blockly/core';
import {refreshBlockStateIcon} from './block_state_icon';

const BLOCK_STATE_CHANGE = 'lesson_block_state_change';

/**
* Something a block allows, which core turns on and off without firing an
* event.
*/
export type BlockPermission = 'deletable' | 'editable' | 'movable';

interface BlockStateChangeJson extends Blockly.Events.BlockBaseJson {
element: BlockPermission;
oldValue: boolean;
newValue: boolean;
}

/**
* Notifies listeners that a block's deletable, editable or movable state has
* changed.
*/
class BlockStateChange extends Blockly.Events.BlockBase {
override type = BLOCK_STATE_CHANGE;

/** The state that changed. Undefined for a blank event. */
element?: BlockPermission;

/** The value before the change. */
oldValue: boolean;

/** The value after the change. */
newValue: boolean;

/**
* @param block The changed block. Undefined for a blank event.
* @param element The state that changed.
* @param oldValue The value before the change.
* @param newValue The value after the change.
*/
constructor(
block?: Blockly.Block,
element?: BlockPermission,
oldValue = false,
newValue = false,
) {
super(block);
this.element = element;
this.oldValue = oldValue;
this.newValue = newValue;
}

/**
* Encodes this event as JSON.
*
* @returns JSON representation.
*/
override toJson(): BlockStateChangeJson {
if (!this.element) {
throw new Error(
'The changed element is undefined. Either pass an element to the ' +
'constructor, or call fromJson',
);
}
const json = super.toJson() as BlockStateChangeJson;
json.element = this.element;
json.oldValue = this.oldValue;
json.newValue = this.newValue;
return json;
}

/**
* Deserializes the JSON event.
*
* @param json The JSON representation of the event.
* @param workspace The workspace the event belongs to.
* @param event The event to populate, if any.
* @returns The deserialized event.
*/
static fromJson(
json: BlockStateChangeJson,
workspace: Blockly.Workspace,
event?: BlockStateChange,
): BlockStateChange {
const newEvent = super.fromJson(
json,
workspace,
event ?? new BlockStateChange(),
) as BlockStateChange;
newEvent.element = json.element;
newEvent.oldValue = json.oldValue;
newEvent.newValue = json.newValue;
return newEvent;
}

/**
* Does this event record any change of state?
*
* @returns False if something changed.
*/
override isNull(): boolean {
return this.oldValue === this.newValue;
}

/**
* Applies the old or new value to the block.
*
* @param forward True to apply the new value, false to restore the old one.
*/
override run(forward: boolean) {
const block = this.blockId
? this.getEventWorkspace_().getBlockById(this.blockId)
: null;
if (!block || !this.element) {
console.warn("Can't change state of non-existent block");
return;
}
// Fires a new event, as core's events do when run, so that listeners
// hear about undo and redo. Undo isn't recorded while undoing.
setBlockState(block, this.element, forward ? this.newValue : this.oldValue);
}
}

Blockly.registry.register(
Blockly.registry.Type.EVENT,
BLOCK_STATE_CHANGE,
BlockStateChange,
);

/**
* Sets a block's deletable, editable or movable state and updates its state
* icon, firing an event so the change can be undone.
*
* @param block The block to change.
* @param state The state to set.
* @param value The new value of the state.
*/
export function setBlockState(
block: Blockly.Block,
state: BlockPermission,
value: boolean,
) {
let oldValue: boolean;
switch (state) {
case 'deletable':
oldValue = block.isOwnDeletable();
block.setDeletable(value);
break;
case 'editable':
oldValue = block.isOwnEditable();
block.setEditable(value);
break;
case 'movable':
oldValue = block.isOwnMovable();
block.setMovable(value);
break;
}
Blockly.Events.fire(new BlockStateChange(block, state, oldValue, value));
refreshBlockStateIcon(block);
}
Loading
Loading