块编辑器开发文档

💡 云策文档标注

概述

Gutenberg 项目不仅为 WordPress 构建了更好的编辑器,还创建了一个基于 JavaScript 包和工具的开发平台,可用于 Web 应用程序开发。

关键要点

  • 平台提供一系列 npm 包,包括 UI 组件和开发脚本,支持 React 集成。
  • @wordpress/components 包提供可复用的 UI 组件,需安装并引入样式表以确保正确显示。
  • @wordpress/scripts 包提供无需额外配置的构建、格式化和 linting 脚本,简化 JavaScript 开发流程。
  • @wordpress/block-editor 包支持创建独立的块编辑器,适用于自定义开发。

代码示例

// 安装组件包
npm install --save @wordpress/components

// React 中使用组件
import { Button } from '@wordpress/components';

function MyApp() {
    return <Button>Hello Button</Button>;
}

// 安装脚本包
npm install --save-dev @wordpress/scripts

// package.json 脚本配置
"scripts": {
    "build": "wp-scripts build",
    "format": "wp-scripts format",
    "lint:js": "wp-scripts lint-js",
    "start": "wp-scripts start"
}

注意事项

  • 使用 @wordpress/components 时,需从 node_modules/@wordpress/components/build-style/style.css 引入样式表,否则组件可能显示不正确。
  • @wordpress/scripts 包已预配置 webpack 设置,可直接使用 npm run build 等命令,无需额外配置文件。
  • 更多详细信息可参考 Block Editor Handbook 中的 JavaScript 入门教程和 @wordpress/scripts 包文档。

📄 原文内容

The Gutenberg Project is not only building a better editor for WordPress, but also creating a platform to build upon. This platform consists of a set of JavaScript packages and tools that you can use in your web application. View the list of packages available on npm.

UI components

The WordPress Components package contains a set of UI components you can use in your project. See the WordPress Storybook site for an interactive guide to the available components and settings.

Here is a quick example, how to use components in your project.

Install the dependency:

npm install --save @wordpress/components

Usage in React:

import { Button } from '@wordpress/components';

function MyApp() {
    return <Button>Hello Button</Button>;
}

Many components include CSS to add style, you will need to include for the components to appear correctly. The component stylesheet can be found in node_modules/@wordpress/components/build-style/style.css, you can link directly or copy and include it in your project.

Development scripts

The @wordpress/scripts package is a collection of reusable scripts for JavaScript development — includes scripts for building, linting, and testing — all with no additional configuration files.

Here is a quick example, on how to use wp-scripts tool in your project.

Install the dependency:

npm install --save-dev @wordpress/scripts

You can then add a scripts section to your package.json file, for example:

    "scripts": {
        "build": "wp-scripts build",
        "format": "wp-scripts format",
        "lint:js": "wp-scripts lint-js",
        "start": "wp-scripts start"
    }

You can then use npm run build to build your project with all the default webpack settings already configured, likewise for formatting and linting. The start command is used for development mode. See the @wordpress/scripts package for full documentation.

For more info, see the Getting Started with JavaScript tutorial in the Block Editor Handbook.

Block Editor

The @wordpress/block-editor package allows you to create and use standalone block editors.

You can learn more by reading the tutorial “Building a custom block editor”.