Blockly Samples

repository·main·Indexed 21 days ago

https://github.com/raspberrypifoundation/blockly-samples

A collection of plugins and sample projects designed to extend and demonstrate the capabilities of the Blockly library. Includes demos for real-time collaboration (blockly-rtc), custom tooltip rendering via Blockly.Tooltip.setCustomTooltip, UI/layout configurations, code execution, and a template sample app for building and deploying Blockly applications using Webpack.

Tokens
94.9K
Snippets
377
Records
465
Agent score
71%

What's inside blockly-samples

  1. Overview of Blockly sample demos

    main

    The examples/ directory contains various self-contained projects demonstrating how to include and extend the Blockly library. Key demos include:

    • UI & Layout: fixed-demo (fixed element injection), resizable-demo (resizable element injection), rtl-demo (right-to-left mode), and single-direction-scroll-demo (single-direction scrollbars).
    • Customization: custom-dialogs-demo (custom browser dialogs), custom-tooltips-demo (custom tooltip renderer), pitch-field-demo (custom block fields), and turtle-field-demo (custom block fields).
    • Code Execution: generator-demo (code generation and sandboxed JS execution), interpreter-demo (step-by-step execution), and headless-demo (Python code generation from JSON without graphics).
    • Advanced Features: backpack-demo (shared backpack between instances), mirror-demo (leader-follower instances), blockly-rtc (real-time collaboration), and graph-demo (instant feedback on block changes).
    • Toolbox & Constraints: toolbox-demo (complex category structures) and max-blocks-demo (limiting total block count).
  2. Explore Blockly plugins and examples

    main

    The blockly-samples repository is organized into two primary sections for developers looking to extend or implement Blockly:

    • Plugins: Self-contained code modules designed to add specific new functionality to the core Blockly library.
    • Examples: Complete, self-contained sample projects that demonstrate various techniques for including, configuring, and extending Blockly in an application.

    For interactive, live demonstrations of most available plugins, visit the Blockly Samples GitHub Pages site.

  3. Tooling Overview for Blockly Applications

    main

    The sample app uses a standard modern web development stack to manage the Blockly lifecycle:

    ToolPurpose
    npmDependency management and script execution
    ES6 ModulesHandling imports and exports
    WebpackBundling source code into single files for serving
    webpack-dev-serverLocal development server
    MochaUnit testing
    ESLintCode linting and style enforcement
    No UI FrameworkThe sample is framework-agnostic, but supports integration with React or Angular if needed.
  4. Discover @blockly Plugins

    main
    The @blockly scope on npm contains a variety of plugins designed to extend the core Blockly functionality. These plugins are categorized by their purpose, such as adding new input fields, specialized blocks, themes, workspace behaviors, or development tools. You can find the full list of published packages by searching for the @blockly tag on npm.
  5. How field-dependent dropdowns work

    main

    The FieldDependentDropdown is an extension of Blockly.FieldDropdown. It allows a dropdown's available options to change automatically based on the value of a parent field attached to the same block.

    Key Concepts

    • Parent Field Association: You associate the child field with a parentName. When the parent field's value changes, the child field updates its menu options.
    • Option Mapping: A mapping object where keys correspond to the parent field's possible values, and values are the arrays of options for the child field.
    • Default Options: A fallback set of options used if the parent field's current value does not exist in the optionMapping.
    • Chaining: You can create chains of dependent dropdowns where one dependent field acts as the parent for another.
    • Undo/Redo & Serialization: Changes to dependent options are recorded in the undo history and are preserved during Blockly serialization/deserialization.

    Important Constraints

    • Field Order: The parent field must be attached to the block before the child field. The plugin works by attaching a validator to the parent field to intercept value changes.
    • Custom Validators: If you need to use a custom validator on the parent field, you must use the JavaScript API to define your block and pass your validator to the parent field's constructor. Setting a validator on the parent field after the child field is initialized will overwrite the plugin's internal validator and break the dependency logic.
  6. How to use scroll options with other plugins or custom draggables

    main

    Using with other plugins

    This plugin includes its own MetricsManager. If you are using multiple plugins that provide their own MetricsManager, you must create a custom implementation that combines their features. Your implementation must satisfy the isCacheable type guard in ScrollMetricsManager.ts to ensure content metrics are available during a drag.

    Using with custom draggables

    Custom objects implementing Blockly.IDraggable are compatible with the plugin but do not support autoscrolling by default. To enable autoscrolling for your custom objects, implement the AutoScrollable interface on them.

  7. How shadow-block-converter works

    main

    The @blockly/shadow-block-converter plugin automatically converts shadow blocks into regular blocks when a user edits them.

    Key Behaviors:

    • Persistence: Shadow blocks behave like a persistent default value. If a user moves or deletes the newly created regular block, the original shadow block will automatically reappear.
    • Independence: The converted regular block is a new instance with a unique ID, though it maintains the same properties and shape as the original shadow block.
    • Ancestor Handling: If the shadow block was attached to ancestor blocks that were also shadows, those ancestors will also be recreated as regular blocks.
    • Descendant Handling: If the shadow block had descendant blocks, they will be recreated with new IDs but will remain shadow blocks.
  8. Configure Minimap styling and behavior

    main

    The minimap inherits RTL and theme properties from the provided workspace.

    To customize the appearance, use the following CSS classes:

    • .blockly-minimap: Controls the minimap container (e.g., box-shadow).
    • .blockly-focus-region: Controls the highlight region that shows the current viewport (e.g., fill color).

    To configure keyboard panning distance, use setKeyboardPanStep(stepPixels). The default is 40 workspace pixels per keypress.

    // Set pan distance to 80px per arrow press
    minimap.setKeyboardPanStep(80);
  9. Configure Keyboard Accessibility

    main

    The minimap is a single tab stop. When focused, the arrow keys pan the main workspace. Modifier keys (Ctrl, Alt, Shift, Meta) are passed through without triggering a pan.

    | Key          | Action                  |
    | ------------ | ----------------------- |
    | `ArrowUp`    | Pan the workspace up    |
    | `ArrowDown`  | Pan the workspace down    |
    | `ArrowLeft`  | Pan the workspace left    |
    | `ArrowRight` | Pan the workspace right |
  10. UX/UI patterns for Realtime Collaboration

    main

    The user experience is designed to minimize distraction during collaborative sessions.

    Visual Feedback

    • No Real-time Movement: Users do not see blocks physically moving across the screen in real-time. Instead, they see blocks update their locations/states once the synchronization is complete.
    • Presence Markers: To show where other users are active, the system uses color-coded markers.
    • Marker Behavior: Markers indicate when a remote user has:
      • Selected a specific block.
      • Is currently editing a field.

    Each user is assigned a unique color (e.g., yellow, blue, or green) to distinguish their presence on the workspace.