简体   繁体   English

动态生成 TinyMCE 下拉菜单

[英]Generating TinyMCE drop-down menu dynamically

I am trying to create toolbar button in TinyMCE with options that are derived from the array.我正在尝试使用从数组派生的选项在 TinyMCE 中创建工具栏按钮。 I've followed the examples on Tiny's website and the button is getting generated as expected.我已经按照 Tiny 网站上的示例进行操作,并且按钮已按预期生成。 Here is the code:这是代码:

 var mergeFields = {one: "first", two: "second", three: "third"}; tinymce.init({ selector: 'textarea', menubar: false, toolbar: 'mergefields', setup: function (editor) { editor.ui.registry.addMenuButton('mergefields', { text: 'Merge Fields', fetch: function (callback) { var items = []; for (var fieldName in mergeFields) { var menuItem = { type: 'menuitem', text: mergeFields[fieldName], onAction: function() { // The problem: this function always inserts the last element of the array // instead of the expected fieldName associated with this menuItem editor.insertContent(fieldName); }, }; items.push(menuItem); } callback(items); }, }); } });
 <script src="https://cloud.tinymce.com/5/tinymce.min.js?apiKey=XXXXX"></script> <textarea>Editor</textarea>

The problem happens when one of the options is selected and the anonymous function assigned to onAction property is executed -- it always inserts "three" into the document (presumably because after running through the whole array, fieldName is set to "three"). The problem happens when one of the options is selected and the anonymous function assigned to onAction property is executed -- it always inserts "three" into the document (presumably because after running through the whole array, fieldName is set to "three"). How can I make the onAction handler insert the right value into the document?如何让 onAction 处理程序将正确的值插入到文档中?

This needs to work in TinyMCE 5.这需要在 TinyMCE 5 中工作。

I've found a similar question here: Adding custom dropdown menu to tinyMCE and insert dynamic contents , but it was referring to TinyMCE 4 and unfortunately the provided answer does not work for TinyMCE 5.我在这里发现了一个类似的问题:将自定义下拉菜单添加到 tinyMCE 并插入动态内容,但它指的是 TinyMCE 4,不幸的是,提供的答案不适用于 TinyMCE 5。

Thanks for your help!谢谢你的帮助!

I had the same problem.我有同样的问题。 I solved it using value+onSetup我使用 value+onSetup 解决了它

https://jsfiddle.net/stvakis/tjh7k20v/8/ https://jsfiddle.net/stvakis/tjh7k20v/8/

var mergeFields = {
  one: "first",
  two: "second",
  three: "third"
};

tinymce.init({
  selector: 'textarea',
  menubar: false,
  toolbar: 'mergefields',
  setup: function(editor) {
    editor.ui.registry.addMenuButton('mergefields', {
      text: 'Merge Fields',
      fetch: function(callback) {
        var items = [];
        for (var fieldName in mergeFields) {
          var menuItem = {
            type: 'menuitem',
            text: mergeFields[fieldName],
            value:fieldName,
            onSetup: function(buttonApi) {
              var $this = this;
              this.onAction = function() {
                editor.insertContent($this.data.value);
              };
            },
          };
          items.push(menuItem);
        }
        callback(items);
      },
    });
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM