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

最后都变了- 提交于 2021-01-27 06:43:38

问题


I have a simple codemirror text editor I am working on with bootstrap. I can click on my bold and code buttons OK and it wraps the selected / highlighted text corrctly.

Question 1: When text is wrapped in a tag lets say <b>something</b> If I go and select / highlight it again and click same bold button how can I undo it in codemirror?

Here's CodePen Full View

Here's Codepen Code View

Script

<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>

回答1:


I tried with this and it worked:

    $('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;
        }

    });

It just checks your string if it has <b> tag, and if it does just removes them.

CodePen link




回答2:


for me this works perfect

mac

cmd + u

windows

ctrl + u

similiar to editor.undoSelection

manual

according to the other answer i didnt get the point



来源:https://stackoverflow.com/questions/34699463/how-to-undo-selected-highlighted-text-when-click-on-button-with-codemirror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!