wordpress - 在 Wordpress 古腾堡编辑器的根容器中添加一个类
问题描述
Wordpress gutenberg 编辑器有一个内容的根容器,用“is-root-container”类标识。我想在预览更改时(在移动设备、平板电脑、桌面设备之间)向该容器添加类,以便我可以设置编辑器内容的样式以模拟相应的响应式 CSS。
我还没有找到一种方法来做到这一点,所以我有一个解决方法来使用 editor.BlockListBlock 过滤器将类添加到每个块的包装器中。但是,这并不是特别有效,尤其是在有大量块的情况下。
这是我目前正在使用的,但它会为每个块运行,效率不高。
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(wp.element.createElement);
const { createHigherOrderComponent } = wp.compose;
const { useSelect } = wp.data;
const { addFilter } = wp.hooks;
const withPreviewDeviceClassname = createHigherOrderComponent(
( BlockListBlock ) => {
return ( props ) => {
const previewMode = useSelect( (select) => select('core/edit-post').__experimentalGetPreviewDeviceType() );
const className = 'tw ' + (previewMode == 'Tablet' ? 'tablet' : (previewMode == 'Desktop' ? 'desktop tablet' : '') );
return (
html`<${BlockListBlock}
...${props}
className=${className}
/>`
);
};
},
'withPreviewDeviceClassname'
);
addFilter(
'editor.BlockListBlock',
'cms/blocklist-plugin',
withPreviewDeviceClassname
);
解决方案
推荐阅读
- css - 为什么 Firefox 打印 css 空白?
- python - 无法在其他文件中导入 mongo 对象
- angular - 如何将段设置为角度 6 路线?
- javascript - MixItUp:请求了操作,但 MixItUp 实例正忙
- c - 使用有限的类 C 库将科学计数法字符串中的数字转换为浮点数
- node.js - 如何仅获取子 Firebase 云功能的一个值?
- c# - 运行带有参数的 C# exe,以启动另一个应用程序并从控制台获取输出
- html - Angular 6 iframe 绑定
- amazon-web-services - AWS Glacier 上传对象参考错误
- java - SAX 解析器不调用 startDTD