wordpress - 古腾堡向编辑器添加富文本按钮,按钮不出现
问题描述
我在这里学习教程 - https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/format-api/
第 1 步 - 注册一个新格式,这工作正常,我可以在控制台中看到我的新格式类型
第 2 步 - 我已将代码添加到 my-custom-format.js 以使按钮出现,并按照说明将相应的 wp-element 和 wp-editor 包添加到 PHP 文件中的依赖项数组中。
.php 文件
<?php
/**
* Plugin Name: My custom format
*/
function my_custom_format_script_register() {
wp_register_script(
'my-custom-format-js',
plugins_url( 'my-custom-format.js', __FILE__ ),
array( 'wp-element', 'wp-editor', 'wp-rich-text', )
);
}
add_action( 'init', 'my_custom_format_script_register' );
function my_custom_format_enqueue_assets_editor() {
wp_enqueue_script( 'my-custom-format-js' );
}
add_action( 'enqueue_block_editor_assets', 'my_custom_format_enqueue_assets_editor' );
JS文件
( function( wp ) {
wp.richText.registerFormatType(
'my-custom-format/sample-output', {
title: 'Sample output',
tagName: 'samp',
className: null,
}
);
var MyCustomButton = function( props ) {
return wp.element.createElement(
wp.editor.RichTextToolbarButton, {
icon: 'editor-code',
title: 'Sample output',
onClick: function() {
props.onChange( wp.richText.toggleFormat(
props.value,
{ type: 'my-custom-format/sample-output' }
) );
},
isActive: props.isActive,
}
);
}
wp.richText.registerFormatType(
'my-custom-format/sample-output', {
title: 'Sample output',
tagName: 'samp',
className: null,
edit: MyCustomButton,
}
);
} )( window.wp );
我是否需要安装 @wordpress/rich-text 模块才能使其正常工作?
解决方案
推荐阅读
- javascript - 仅在附加时才关注输入
- python - Python 2.7 pip install 失败缺少 pyconfig.h,可能安装的源分发错误?
- mysql - mysql symfony中左连接时如何确定排序字段?
- javascript - 从字典列表中提取值并填充组件
- docker - docker 中的 java 应用程序如何在 ubuntu 18.4 的挂载点中定位文件
- c++ - C++,程序认为我没有 main 函数,当我这样做并给出“_main”时,引用自:implicit entry/start for main executable error
- ios - Xcode 12.0 中是否有任何更改将 ButtonType 从 roundedRect 更改为 system?
- ruby-on-rails - Rails slim 将 html 渲染为文本
- jquery - 如何在 jQuery 中使用嵌套的 foreach 循环?
- android - 将 TextView 锚定到 ImageView