如何在使用代码镜像单击按钮时撤消选定/突出显示的文本

How to undo selected / highlighted text when click on button with codemirror

本文关键字:文本 显示 撤消 代码 按钮 单击 镜像      更新时间:2023-09-26

我有一个简单的codemirror文本编辑器,我正在使用bootstrap进行操作。我可以点击我的粗体和代码按钮OK,它可以正确地包装所选/突出显示的文本。

问题1:当文本被包装在标签中时,比如<b>something</b>如果我再次选择/突出显示它并单击相同的粗体按钮,我如何在代码镜像中撤销它?

这是CodePen全视图

这是Codepen代码视图

编写脚本

<script type="text/javascript">
$(document).ready(function() {
// tooltips on hover
$('[data-toggle=''tooltip'']').tooltip({container: 'body', html: true});
// Makes tooltips work on ajax generated content
$(document).ajaxStop(function() {
    $('[data-toggle=''tooltip'']').tooltip({container: 'body'});
});
$('[data-toggle=''tooltip'']').on('remove', function() {
    $(this).tooltip('destroy');
});

var editor = document.getElementById('text-editor');
$("#text-editor").each(function (i) {
    editor = CodeMirror.fromTextArea(this, {
        lineNumbers: true,
        mode: 'html'
    });
    editor.on("change", function() {
        document.getElementById('question-preview').innerHTML = editor.getValue('<br>')
        .replace('<?','&lt;?')
        .replace('?>', '?&gt;')
        .replace('<script>', '&lt;script&gt;')
        .replace('<script>', '&lt;/script&gt;')
        .replace('<div>', '&lt;div&gt;')
        .replace('</div>', '&lt;/div&gt;')
    });
    //$('#hr').append('<hr />');
    $('a[role="button"]').click(function(){
        var val = $(this).data('val');
        var string = editor.getSelection();
        switch(val){
            case 'bold': 
                editor.replaceSelection('<b>' + string + '</b>');
            break;
            case 'italic': 
                editor.replaceSelection('<i>' + string + '</i>');
            break;
            case 'quote': 
                editor.replaceSelection('<blockquote><p>' + string + '</p></blockquote>');
            break;
            case 'code': 
                editor.replaceSelection('<pre><code>' + string + '</code></pre>');
            break;
            case 'hr': 
                editor.replaceSelection('<hr/>');
            break;
        }
    });
    $(".dropdown-menu li a[class='btn-heading']").click(function(){
        var val = $(this).data('val');
        var string = editor.getSelection();
        switch(val){
            case 'h1': 
                editor.replaceSelection('<h1>' + string + '</h1>');
            break;
            case 'h2': 
                editor.replaceSelection('<h2>' + string + '</h2>');
            break;
            case 'h3': 
                editor.replaceSelection('<h3>' + string + '</h3>');
            break;
            case 'h4': 
                editor.replaceSelection('<h4>' + string + '</h4>');
            break;
            case 'h5': 
                editor.replaceSelection('<h5>' + string + '</h5>');
            break;
            case 'h6': 
                editor.replaceSelection('<h6>' + string + '</h6>');
            break;
        }
    });
});
});
</script>

HTML

<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="panel panel-default"  onLoad="text_editor();">
<div class="panel-heading">
<ul class="nav nav-pills" id="heading">
    <li><a role="button" data-val="bold">Bold</a></li>
    <li><a role="button" data-val="italic">Italic</a></li>
    <li><a role="button" data-val="link">Hyperlink</a></li>
    <li><a role="button" data-val="quote">Quote</a></li>
    <li><a role="button" data-val="code">Code</a></li>
    <li><a role="button" data-val="image">Image</a></li>
    <li class="dropdown">
        <a class="dropdown-toggle" aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown">Heading <span class="caret"></span></a>
        <ul class="dropdown-menu"> 
            <li><a class="btn-heading" data-val="h1">H1</a></li> 
            <li><a class="btn-heading" data-val="h2">H2</a></li> 
            <li><a class="btn-heading" data-val="h3">H3</a></li> 
            <li><a class="btn-heading" data-val="h4">H4</a></li>
            <li><a class="btn-heading" data-val="h5">H5</a></li> 
            <li><a class="btn-heading" data-val="h6">H6</a></li>
        </ul>
    </li>
</ul>
</div>
<div class="panel-body">
<textarea id="text-editor" name="text-editor" class="form-control"></textarea>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div id="question-preview"></div>
</div>
</div>
</div>

我试过这个,它成功了:

    $('a[role="button"]').click(function(){
        var val = $(this).data('val');
        var string = editor.getSelection();
        switch(val){
            case 'bold':
              if(string.indexOf('<b>') > -1){
                editor.replaceSelection(string.replace('<b>', '').replace('</b>', ''));
              } else{
                editor.replaceSelection('<b>' + string + '</b>'); 
              }
            break;
            case 'italic': 
                editor.replaceSelection('<i>' + string + '</i>');
            break;
            case 'quote': 
                editor.replaceSelection('<blockquote><p>' + string + '</p></blockquote>');
            break;
            case 'code': 
                editor.replaceSelection('<pre><code>' + string + '</code></pre>');
            break;
            case 'hr': 
                editor.replaceSelection('<hr/>');
            break;
        }
    });

它只是检查你的字符串是否有<b>标记,如果有,它只是删除它们。

CodePen链接

对我来说,这是完美的

mac

cmd+u

windows

ctrl+u

类似于editor.undoSelection

手动

根据另一个答案,我没有得到的分数