研究调查问卷:请告诉我们您使用 Blockly 的体验
开始调查问卷
插件
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
插件是一段独立的代码,用于为 Blockly 添加功能。例如,它可以添加自定义字段、定义新主题或提供自定义渲染程序。插件通常通过 npm 打包和分发。
如需快速了解插件,请观看我们的“插件概览”讲座 (2021)。
如果您想创建自己的插件,请参阅添加插件。
第一方和第三方插件
第一方插件由 Blockly 团队提供支持,并在 npm 上以 @blockly
作用域发布。它们可用于各种 Blockly 应用。
第三方插件由第三方独立维护和发布。它们可能更复杂、更具实验性,或者仅适用于范围较小的 Blockly 应用。
查找插件
安装插件
我们建议使用 npm 或 yarn 等软件包管理器安装插件。这样您就可以轻松接收更新。
请查看该插件的自述文件,了解是否有其他安装说明。
使用插件
每个插件都不尽相同,因此请参阅插件的自述文件,了解如何使用该插件。以下示例展示了如何使用 @blockly/field-angle
插件:
从插件导入代码。具体操作方法取决于您安装插件的方式。
npm 或 yarn
import Blockly from 'blockly';
import {registerFieldAngle} from '@blockly/field-angle';
克隆的代码库
import {registerFieldAngle} from 'path/to/plugin';
根据需要初始化插件。提供自定义字段的插件通常需要您注册字段:
registerFieldAngle();
使用该插件。
Blockly.common.defineBlocksWithJsonArray([
{
type: "my_angle_block",
message0: "%1 degrees",
args0: [
{
// Use @blockly/field-angle.
type: "field_angle",
name: "FIELDNAME",
value: 45,
},
],
output: null,
style: 'math_blocks'
},
]);
插件版本
blockly-samples
中的插件使用语义版本控制,这要求使用新的主要版本时必须进行破坏性更改。任何通过猴子补丁修补核心的插件都将具有主要版本 0,以表示处于初始开发阶段。
大多数插件将 blockly
软件包作为 peerDependency
(而非 dependency
)包含在内。这是因为我们假设您已安装 Blockly。(如果不使用 Blockly,就没有必要使用插件。)这样,您就可以自行管理 Blockly 版本,但也需要您检查插件的 package.json
,以确定其所需的最低 Blockly 版本。如果插件更新后需要更高版本的 Blockly,则会被视为破坏性更改,其主要版本号将会递增。
将插件添加到应用的 package.json
后,默认情况下,版本前面会添加一个尖括号:
"dependencies": {
"@blockly/field-angle": "^5.0.12"
}
这样,npm 便会安装高于或等于所列版本的任何次要版本,因此版本 5.0.20
或 5.1.0
可以正常运行,但新的主要版本(例如 6.0.1
)则不行。更新到新版 Blockly 后,最好检查您的任何插件是否也可以更新到新的主要版本。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[],[],null,["# Plugins\n\nA plugin is a self-contained piece of code that adds functionality to Blockly.\nFor example, it might add a custom field, define a new theme, or provide a\ncustom renderer. Plugins are generally packaged and distributed through npm.\n| **Note:** The [`BlocklyOptions`\n| object](/blockly/guides/configure/web/configuration_struct#the_options_dictionary) has a `plugins` property for injecting classes that [customize Blockly\n| behavior](/blockly/guides/configure/web/customization). While these classes may be implemented as plugins, this is not required and they are otherwise unrelated.\n\nFor a quick introduction to plugins, see our [Plugins Overview talk\n(2021)](https://www.youtube.com/watch?v=rg-V0w7UZFc&list=PLSIUOFhnxEiCjoIwJ0jAdwpTZET73CK7d&index=3).\n\nIf you want to create your own plugin, see [Add a\nplugin](/blockly/guides/contribute/samples/add_a_plugin).\n\nFirst- and third-party plugins\n------------------------------\n\n*First-party plugins* are supported by the Blockly team and published under the\n`@blockly` scope on npm. They are designed to be usable in a wide range of\nBlockly applications.\n\n*Third-party plugins* are maintained and published independently. They may be\nmore complex, more experimental, or targeted to a narrower range of Blockly\napplications.\n\nFind a plugin\n-------------\n\n- Visit [Blockly Plugins \\&\n Demos](https://google.github.io/blockly-samples/#plugins), which has live\n demos of first-party plugins.\n\n- Search npm for\n [`keyword:blockly-plugin`](https://www.npmjs.com/search?q=keyword%3Ablockly-plugin).\n Plugins with the scope `@blockly` are published by the Blockly team. For\n broader results, search for\n [`keyword:blockly`](https://www.npmjs.com/search?q=keyword%3Ablockly) or\n [`blockly`](https://www.npmjs.com/search?q=blockly).\n\n- See the [`blockly-samples/plugins`\n directory](https://github.com/google/blockly-samples/tree/master/plugins) on\n GitHub, which is the repository for first-party plugins. Each plugin has a\n README that describes its behaviour and intended use.\n\nInstall a plugin\n----------------\n\nWe recommend installing plugins with a package manager like npm or yarn. This\nmakes it easy to receive updates.\n\n- **Install a plugin with a package manager**\n\n ### npm\n\n npm install @blockly/field-angle\n\n ### yarn\n\n yarn add @blockly/field-angle\n\n- **Install a plugin without a package manager**\n\n ### unpkg\n\n \u003cscript src=\"https://unpkg.com/@blockly/field-angle\"\u003e\u003c/script\u003e\n\n You can also clone the GitHub repository that contains the plugin. For\n first-party plugins, this is\n [`blockly-samples`](https://github.com/google/blockly-samples/tree/master).\n\nCheck the plugin's README to see if there are any additional installation\ninstructions.\n\nUse a plugin\n------------\n\nEach plugin is different, so see the plugin's README for information on how to\nuse that plugin. The following example shows how to use the\n[`@blockly/field-angle`\nplugin](https://www.npmjs.com/package/@blockly/field-angle):\n\n1. Import code from the plugin. How you do this depends on how you installed\n the plugin.\n\n ### npm or yarn\n\n import Blockly from 'blockly';\n import {registerFieldAngle} from '@blockly/field-angle';\n\n ### unpkg\n\n You do not need to use an `import` statement.\n\n ### Cloned repository\n\n import {registerFieldAngle} from 'path/to/plugin';\n\n2. Initialize the plugin as needed. Plugins that provide custom fields often\n require you to register the field:\n\n registerFieldAngle();\n\n3. Use the plugin.\n\n Blockly.common.defineBlocksWithJsonArray([\n {\n type: \"my_angle_block\",\n message0: \"%1 degrees\",\n args0: [\n {\n // Use @blockly/field-angle.\n type: \"field_angle\",\n name: \"FIELDNAME\",\n value: 45,\n },\n ],\n output: null,\n style: 'math_blocks'\n },\n ]);\n\nPlugin versions\n---------------\n\nPlugins in `blockly-samples` use [semantic versioning](https://semver.org),\nwhich requires breaking changes to use a new major version. Any new plugin that\nmonkey patches core will have a major version of 0 to [signify initial\ndevelopment](https://semver.org/#spec-item-4).\n\nMost plugins include the `blockly` package as a\n[`peerDependency`](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#peerdependencies)\nrather than a `dependency`. This is because we assume that you have already\ninstalled Blockly. (It doesn't make sense to use a plugin without using\nBlockly.) This lets you manage the Blockly version yourself, but also requires\nyou to check the plugin's `package.json` to determine the minimum version of\nBlockly it requires. If a plugin is updated to need a newer version of Blockly,\nthis is considered a breaking change and its major version will be increased.\n\nWhen you add a plugin to your application's `package.json`, the default is to\ninclude a caret before the version: \n\n \"dependencies\": {\n \"@blockly/field-angle\": \"^5.0.12\"\n }\n\nThis will let npm install any minor version at or above the listed version, so\nversion `5.0.20` or `5.1.0` works, but a new major version such as `6.0.1` does\nnot. When you update to a new version of Blockly, it's a good idea to check if\nany of your plugins can be updated to a new major version as well."]]