2016-09-14 58 views
0

使用tinyMCE 4.2,每当用户点击编辑器内的任何地方时,我需要更改(自定义)工具栏按钮的文本。TinyMCE 4更改编辑器上的工具栏按钮文本点击

这是相关代码:

tinymce.init({ 

    //code ommitted... 

    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image jbimages navigationbutton glossary", 

    setup: function(editor){ 

     //add "Glossary" button 
     editor.addButton('glossary', { 
      name: 'glossary', 
      text: 'Glossary', 
      onclick: function(){ 
       /* 
       //commented on purpose 
       var button = this; 
       var updatedButtonText = "Some updated button text"; 
       button.text = updatedButtonText; 
       */ 
      } 
     });//end addButton 

     editor.on('click', function(){ 
      var updatedButtonText = "Some updated button text"; 

      //update the button text: 
      editor.buttons.glossary.text = updatedButtonText; //doesn't work 
      window.parent.tinyMCE.activeEditor.buttons.glossary.text = updatedButtonText; //doesn't work either 

      //confirm changes: 
      console.log(editor.buttons.glossary.text); //correctly prints "Some updated button text" 
      console.log(window.parent.tinyMCE.activeEditor.buttons.glossary.text); //correctly prints "Some updated button text" as well 
     }); 

    }//end setup 
});//end tinymce.init 

所以,真正的问题是,虽然按钮对象的text属性是变化的,这种变化不仅仅体现在编辑器,按钮上的文字仍然是“词汇表” 。 有趣的是,如果我通过按钮的onclick函数完成同样的操作(所以如果我取消注释注释代码块),那么它可以按预期完美工作 - 按钮文本在编辑器中更新。

我花了几个小时在TinyMCE 4的文档中试图找到一些相关信息,但显然是徒劳的。有任何想法吗?

回答

0

加载编辑器的工具栏后,TinyMCE不支持更改按钮的图标/文本。如果将按钮切换为“打开”或“关闭”(例如,将光标放在粗体或不粗体的文本上时粗体按钮所做的操作),但无法更改实际的文本/图标,则可以更改该按钮。

在编辑器完全加载后,用于定义词汇表按钮的JavaScript对象仍在内存中,因此您可以对该对象执行操作,例如更改属性值或console.log该值,但TinyMCE不会去返回并查看该按钮对象并在加载工具栏后更新该按钮。

+0

哇,我并不期待那样。所以如果在**按钮的''onclick'函数中'text'属性被改变,它确实会更新按钮,但如果在**编辑器中**时更改了'text'属性,它将不会更新它'onclick'功能?因为如果是这样的话,它对我来说没有什么意义,为什么它会这样设计...... – pazof

+0

即使我将按钮的代码放在按钮自己的onClick中,我也无法获取按钮的文本以在UI中进行更改事件。 (它会在JavaScript对象中更改,但不会在工具栏上直观显示)您可以让JS小提琴显示工作吗? –

相关问题