在 TinyMCE 中以编程方式插入新行

技术男孩

我正在使用 tinyMCE,我想在按下快捷键后以编程方式添加一个新行。

我有以下代码,要测试该示例,请单击此处

<!DOCTYPE html>
<html>
<head>
  <script src="jquery.min.js"></script>
  <script src="tinymce/tinymce.min.js"></script>
</head>
  <body>
    <textarea></textarea>
  </body>
</html>
<script>
  tinymce.init({
    selector:'textarea',
    plugins: 'table',
    menubar: 'table',
    toolbar: 'table',
    table_grid: false,
    height: 500,

    setup: function(editor) {
      editor.shortcuts.add('ctrl+alt+a', "description of the shortcut", function() {
        alert('test successful'); //i want to insert a new row here
      });
   }
  });
</script>
迈克尔·弗罗明

TinyMCE 使用命令来执行它的许多功能。如果您查看在上方和下方插入行的现有功能,它们依赖于命令。您可以setup()在配置中的函数中使用一个简单的函数来查看 TinyMCE 何时使用命令:

editor.on('ExecCommand', function (e) {
  console.log('ExecCommand:');
  //console.log(e);
  console.log(e.command);
});

从中您将看到,有两个命令与这些活动相关联:

  • mceTableInsertRowAfter
  • mceTableInsertRowBefore

因此,您可以将自己的击键与这些命令相关联。例如,您可以在 setup() 函数中执行此操作:

editor.shortcuts.add('ctrl+alt+a', "description of the shortcut", function() {
  editor.execCommand('mceTableInsertRowAfter', false);
});

然后,您可以设置一键插入前,一键插入后。这是一个 TinyMCE 小提琴,显示了所有这些:https : //fiddle.tiny.cloud/lyhaab/3

我设置了两个按键快捷键:

  • CTRL+ ALT+AmceTableInsertRowAfter
  • CTRL+ ALT+BmceTableInsertRowBefore

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章