gpt4 book ai didi

javascript - 当您在方法上使用编辑器时,TinyMCE 更新工具栏(初始化后)

转载 作者:行者123 更新时间:2023-12-03 01:34:21 25 4
gpt4 key购买 nike

我正在为 WordPress 开发 Google 字体插件,并尝试获得与核心 WYSIWYG 编辑器相同的效果。基本上,当您单击具有字体类的元素(在编辑器内)时,我想获取该类,然后基于该类重新加载工具栏中的字体系列/样式列表框。

(我在这里发现了一些黑客攻击,比如 Proper Way Of Modifying Toolbar After Init in TinyMCE,但没有像 WP 核心示例那样有效)

当您点击 P、H1、H3、H3 时,会有相同的功能...他们是如何做到的?你能至少给我指出 WordPress 发行版中的 JS 文件吗?我想如果看到代码我就能弄清楚。

这是演示我正在谈论的内容的 GIF。提前致谢。

enter image description here

最佳答案

我找到了解决方案,因为它不是黑客,就像我在 SO 上找到的其他解决方案一样,我将其发布在这里,希望它能帮助任何其他尝试做类似事情的人。

首先访问按钮/列表框需要使用带有回调函数的onpostrender。

editor.addButton( 'developry_google_font_family_button', {
type : 'listbox',
onpostrender : fontFamilyNodeChange,
value : '',
...

接下来回调函数应如下所示:

function fontFamilyNodeChange() {
var listbox = this;
editor.on('NodeChange', function( e ) {
// This next part is specific for my needs but I will post it as an example.
var selected = [];
if ( $( e.element ).hasClass( 'mce-ga' ) ) { // this a class I add to all elements that have google fonts format
// Then I strip the classes from classList that I don't need and add the rest into an array (e.g ['roboto', '100'])
var gfont_options = $( e.element ).attr('class')
.replace('mce-ga', '')
.replace('mce-family-', '')
.replace('mce-weight-', '')
.trim()
.split(' ');
selected.push( gfont_options );
// At end I add the new value to listbox select[0][0] (e.g. 'roboto')
listbox.value(selected[0][0]);
}
});
}

这是一个例子:

enter image description here

关于javascript - 当您在方法上使用编辑器时,TinyMCE 更新工具栏(初始化后),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51143542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com