编辑器 App 栏
[尖叫声] 请不要访问或依赖从双下划线开始的功能和变量,因为这些功能和变量无须通知即可更改. 双下划线命名公约
__)作为一个前缀,用于表示某个函数或变量是用于内部使用的,不应被第三方开发者访问或依赖.
使用量
菜单
菜单有5个类型:
mainMenu- 提供对应用程序主要功能和功能的检索。 代表的是一个改变为汉堡的元素标志.toolsMenu- 一个菜单,用户可以访问各种编辑工具(如"添加部件","结构"等),以某种方式来操纵数据或接口. 这个 区域可能包含有限数量的通用工具按钮,以及用于访问额外工具的下拉菜单 这些.utilitiesMenu- 菜单,用户可以访问与主功能不直接相关的各种功能 应用程序的功能。 这可能包括访问查找者、获得帮助或访问其他杂项功能的选项.documentOptionsMenu- 菜单,用户可以访问保存文档的各种选项。 这可能包括: 选项,用于保存为与文档相关的草稿或条件选项(如"显示条件"等).integrationsMenu- 一个子菜单里面mainMenu,用户可以访问各种选项来整合其他服务. 这可能 包括连接第三方服务或访问其他杂项功能的选项.
每个菜单包含一个项目列表,每个项目可以是:
Action- 执行动作的按钮.Link- 连接到另一页的链接.ToggleAction- 一个在两个州之间切换的按钮(检查或不加控制).
让我们延伸 integrationsMenu 以多个实例涵盖所有类型项目:
行动
import { integrationsMenu } from '@elementor/editor-app-bar';
import { PlusIcon } from '@elementor/icons';
import { __ } from '@wordpress/i18n';
integrationsMenu.registerAction( {
id: 'example-action-id',
props: {
title: __( 'My Custom Action', 'elementor' ),
icon: PlusIcon, // A react component that renders an SVG icon
disabled: false, // Optional
onClick: () => alert( 'Custom action triggered!' ), // Optional
}
} );
切换动作
import { integrationsMenu } from '@elementor/editor-app-bar';
import { EyeIcon } from '@elementor/icons';
import { __ } from '@wordpress/i18n';
integrationsMenu.registerToggleAction( {
id: 'my-custom-toggle',
useProps: () => {
const [ isSelected, setIsSelected ] = useState( false );
return {
title: __( 'My Custom Toggle', 'elementor' ),
icon: EyeIcon,
selected: isSelected, // Optional
onClick: () => setIsSelected( ( prev ) => ! prev ), // Optional
disabled: false, // Optional
};
},
} );
链接
import { integrationsMenu } from '@elementor/editor-app-bar';
import { LinkIcon } from '@elementor/icons';
import { __ } from '@wordpress/i18n';
integrationsMenu.registerLink( {
id: 'my-custom-link',
props: {
title: __( 'My Custom Link', 'elementor' ),
icon: LinkIcon,
href: 'https://elementor.com', // Optional
target: '_blank', // Optional
},
} );
[注] 你也可以用这个
props或者说useProps(视需要而定)所有物品类型:
props- 带动作道具的物体.useProps- 一个反应钩子,可以返回动作道具,让你使道具具有反应性.
[注] 这个
icon属性可以是使 SVG 图标成为任何 React 组件,建议使用SVGIcon从@elementor/ui
自定义位置
除了上面的菜单外,少数地点接受完整的自定义 React 组件通过 injectInto* 函数:
injectIntoPageIndication- 在文档标题旁边的应用程序栏中心注入一个组件.injectIntoResponsive- 在应用程序栏中央插入一个响应/断点切换器旁边的组件.injectIntoPrimaryAction- 在应用程序栏的右侧注入初级动作旁边的组件(如"publish"/"Save").
import { injectIntoPageIndication } from '@elementor/editor-app-bar';
injectIntoPageIndication( {
id: 'my-custom-indication',
component: () => <div>Custom content</div>,
options: {
priority: 10, // Optional, lower value means higher priority. Default is 10.
},
} );
[注] 两种菜单(
registerAction编号 :registerLink编号 :registerToggleAction)和自定义位置(injectInto*反应性: 在任何时间(包括编辑器已经装入之后)注册某个项目都会使它立即出现,所以 在编辑器后加载脚本的插件( 如 Pro 插件, 或第三方插件) 不需要注册 在应用栏打开之前.
从此仓库外部注册
第三方插件(如 Elementor Pro,或任何 WordPress 插件)可以将项目注册到应用栏中而不成为
单人建筑的一部分 取决于 elementor-v2-editor-app-bar 脚本处理和使用
elementorV2.editorAppBar global(即从 @elementor/editor-app-bar:
add_action( 'elementor/editor/v2/scripts/enqueue', function () {
wp_enqueue_script(
'my-plugin-editor-app-bar',
plugins_url( 'assets/editor-app-bar.js', __FILE__ ),
[ 'elementor-v2-editor-app-bar', 'elementor-v2-ui', 'elementor-v2-icons' ],
'1.0.0',
true
);
} );
// my-plugin-editor-app-bar.js
if ( window.elementorV2?.editorAppBar ) {
const { utilitiesMenu } = window.elementorV2.editorAppBar;
utilitiesMenu.registerLink( {
id: 'my-plugin-link',
props: {
title: 'My Plugin',
icon: MyIcon,
href: 'https://example.com',
},
} );
}
确保永远使用 id (无) name登记菜单项目时,并用支票保护电话
window.elementorV2.editorAppBar,因为无法保证整个插件的脚本加载顺序.