块编辑器开发文档

@wordpress/keyboard-shortcuts

💡 云策文档标注

概述

@wordpress/keyboard-shortcuts 是一个用于注册和修改键盘快捷键的通用包,适用于 WordPress 开发环境。它提供 API 来管理上下文快捷键,并依赖于 ES2015+ 环境。

关键要点

  • 安装方式:通过 npm install @wordpress/keyboard-shortcuts --save 安装,需确保运行在 ES2015+ 环境,否则需包含 polyfill。
  • 核心 API:包括 ShortcutProvider(用于处理上下文快捷键)、store(键盘快捷键命名空间的存储定义)和 useShortcut(用于附加键盘快捷键处理程序)。
  • 贡献指南:此包是 Gutenberg 项目的一部分,作为 monorepo 中的独立包,贡献需参考项目的主要贡献者指南。

📄 原文内容

Keyboard shortcuts is a generic package that allows registering and modifying shortcuts.

Installation

Install the module

npm install @wordpress/keyboard-shortcuts --save

This package assumes that your code will run in an ES2015+ environment. If you’re using an environment that has limited or no support for such language features and APIs, you should include the polyfill shipped in @wordpress/babel-preset-default in your code.

API

ShortcutProvider

Handles callbacks added to context by useShortcut. Adding a provider allows to register contextual shortcuts that are only active when a certain part of the UI is focused.

Parameters

  • props ShortcutProviderProps: Props to pass to div.

Returns

  • Component.

store

Store definition for the keyboard shortcuts namespace.

Related

useShortcut

Attach a keyboard shortcut handler.

Parameters

  • name string: Shortcut name.
  • callback ( event: KeyboardEvent ) => void: Shortcut callback.
  • options UseShortcutOptions: Shortcut options.
  • options.isDisabled UseShortcutOptions[ 'isDisabled' ]: Whether to disable the shortcut.

Contributing to this package

This is an individual package that’s part of the Gutenberg project. The project is organized as a monorepo. It’s made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to npm and used by WordPress as well as other software projects.

To find out more about contributing to this package or Gutenberg as a whole, please read the project’s main contributor guide.