为什么我的函数调用在 TinyMCE 中不起作用

Why Are My Function Calls Not Working in TinyMCE

所以,我一直在尝试让 TinyMCE 自动用 &mdash 替换双破折号。根据我的发现,setup 函数在大多数情况下工作正常,但调用该函数的部分会导致 TinyMCE 编辑器中断。我正在寻找的是一种使用“editor.onKeyUp.add”和“editor.onChange.add”来调用函数 customProcess 而不会破坏编辑器的方法。

tinyMCE.init({
//...            
setup: function (editor) {
        var customProcess = function (editor) {
            var lastSelection = editor.selection.getBookmark(2, true), // Store last selection for later restoration
                content       = editor.getContent({ format: 'html'});  // Get content in HTML entity encoded format

            // RegEx replacements
            content = content.replace(/--/g, '—'); // Convert two dashes into —

            // Set content of iframe editor with modified string
            editor.setContent(content, { format: 'html' });

            // Restore selection
            editor.selection.moveToBookmark(lastSelection);
        };

        // Listen for change event
        editor.onChange.add(customProcess);

        // Listen for key up event
        editor.onKeyUp.add(customProcess);
    }
});

textpattern 插件将以零编码为您做到这一点:

https://www.tiny.cloud/docs/plugins/opensource/textpattern/#replacementspatterns

这是一个使用 TinyMCE Fiddle 的 运行 示例:https://fiddle.tiny.cloud/M5haab

如果您输入 2 个连续的破折号并按 space 栏,您将得到 emdash