简体   繁体   English

Trumbowyg:在插入符号处插入文本不起作用

[英]Trumbowyg: Insert text at caret not working

I have a button and when clicking on it I want to add some text inside the Editor.我有一个按钮,单击它时我想在编辑器中添加一些文本。

I saw some issues where people gave the solution and I tried it.我看到人们给出了解决方案的一些问题,我尝试了它。 But when I use restoreRange and execCmd with insertHTML , it does not insert at the caret, only if something is input in the editor before, like a character or a space.但是,当我将restoreRangeexecCmdinsertHTML一起使用时,它不会在插入符号处插入,只有在之前在编辑器中输入了某些内容时,例如字符或空格。

In other words, when clicking on the editor, it does not insert where the caret was when I clicked, but when writing something, it does.换句话说,当点击编辑器时,它不会在我点击时插入插入符号的位置,但是在写东西时,它会插入。

Like if restoreRange only worked when writing something.就像restoreRange只在写东西时才有效。

The problem happens with the following code:问题发生在以下代码中:

$('#editor').trumbowyg({
  btnsDef: {
    testButton: {
      fn: function () {
        // Restore the previous position
        $("#editor").trumbowyg('restoreRange');
        // Add text in the current position 
        $("#editor").trumbowyg('execCmd',
        {
          cmd: 'insertHTML',
          param: 'Dummy text',
          forceCss: false
        });
      },
      title: 'test button',
      text: 'insert text',
      hasIcon: false
    }  
  },
  btns: [
    ['testButton'],
  ]
});

Reproduced the problem here: https://jsfiddle.net/95nqv076/在这里重现问题: https://jsfiddle.net/95nqv076/

Am I missing something?我错过了什么吗?

Found the solution: save the range on blur and on focus.找到解决方案:将范围保存在模糊和焦点上。

$('#editor').trumbowyg({
btnsDef: {
  testButton: {
    fn: function () {
      $("#editor").trumbowyg('restoreRange');
      $("#editor").trumbowyg('execCmd',
        {
      cmd: 'insertHTML',
          param: 'Dummy text',
          forceCss: false
        });
      },
      title: 'Button tooltip',
      text: 'Displayed button name',
      hasIcon: false
    }
  },
  btns: [
    ['testButton'],
  ]
}).on('tbwblur', function(){
  $("#editor").trumbowyg('saveRange');
}).on('tbwfocus', function(){
  $("#editor").trumbowyg('saveRange');
});

Hope it helps someone!希望它可以帮助某人!

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

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