Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 自动将两个破折号转换为&;mdash;在TinyMCE_Javascript_Regex_Events_Tinymce_Keyboard Events - Fatal编程技术网

Javascript 自动将两个破折号转换为&;mdash;在TinyMCE

Javascript 自动将两个破折号转换为&;mdash;在TinyMCE,javascript,regex,events,tinymce,keyboard-events,Javascript,Regex,Events,Tinymce,Keyboard Events,我正在寻找一个TinyMCE插件或其他自定义解决方案,它将自动转换为。理想情况下,解决方案不需要在每个按键/keyUp事件上处理整个TinyMCE内容,而是只检查用户是否剪切、粘贴或键入了-。现在,我使用的是如下内容,当TinyMCE内容较大时,速度有点慢: tinyMCE.init({ //... setup: function (ed) { ed.onKeyUp.add(function(ed) { //find

我正在寻找一个TinyMCE插件或其他自定义解决方案,它将自动转换为。理想情况下,解决方案不需要在每个按键/keyUp事件上处理整个TinyMCE内容,而是只检查用户是否剪切、粘贴或键入了-。现在,我使用的是如下内容,当TinyMCE内容较大时,速度有点慢:

tinyMCE.init({
    //...            
    setup: function (ed) {
        ed.onKeyUp.add(function(ed) {
            //find and replace two dashes with emdash
            //if there was a change, update tinymce/textarea contents
        });
    }
});

更一般地说,我对TinyMCE的快速文本处理解决方案感到好奇。我知道也许没有什么比我现在使用的方法更好的了,但我只是想知道你们中是否有人找到了更好的解决方案。谢谢

关于剪切和粘贴(以及拖放),您可以检查相应的快捷方式或使用onPaste事件。要将内容拖放到编辑器中,可以将onDrop处理程序绑定到tinymce编辑器实例iframe主体(即使用jQuery)

如果你想检查用户输入的每个字符,你已经得到了你想要的解决方案——除了速度。但是对于这种情况,我看不到更快的解决方案-您必须检查每个新字符,除非您可以检查输入的字符(删除、退格等)。

在安装程序中创建自定义处理函数 你在正确的轨道上,你不需要一个插件

在最基本的级别上,只需在事件回调中执行
.replace()

tinyMCE.init({
    //...            
    setup: function (editor) {
        editor.onKeyUp.add(function (editor) {
            var 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' });
        });
    }
});
但是您还需要监听
onChange
事件,因为您要用一个字符替换两个字符,上述方法将导致光标位置不同步

这里是更好的方法:

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);
    }
});

编辑:

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);
    }
});
现在我知道您正在寻找
onKeyUp
事件的替代方案。
onChange
事件可能是您的最佳选择。我不知道还有其他快速处理解决方案