Insert HTML in NicEdit WYSIWYG

后端 未结 7 1523
忘了有多久
忘了有多久 2020-12-11 06:40

How can I insert text/code at the cursors place in a div created by NicEdit?

I\'ve tried to read the documentation and create my own plugin, but I want it to work wi

相关标签:
7条回答
  • 2020-12-11 07:08

    Change follwoing in NicEdit.js File

    Updated from Reto Aebersold Ans It will handle Null Node exception, if text area is empty

    update: function (A) {
        (this.options.command);
            if (this.options.command == 'InsertBookmark') {
                var editor = nicEditors.findEditor("cpMain_area2");
                var range = editor.getRng();
                var editorField = editor.selElm();
                //  alert(editorField.content);
                if (editorField.nodeValue == null) {
                    //  editorField.setContent('"' + A + '"')
                    var oldStr = A.replace("<<", "").replace(">>", "");
                    editorField.setContent("&lt;&lt;" + oldStr + "&gt;&gt;");
                }
                else {
                    // alert('Not Null');
                    // alert(editorField.nodeValue + '  ' + A);
                    editorField.nodeValue = editorField.nodeValue.substring(0, range.startOffset) + A + editorField.nodeValue.substring(range.endOffset, editorField.nodeValue.length);
                }
            }
            else {
                // alert(A);  
                /* END HERE */
                this.ne.nicCommand(this.options.command, A);
            }
    
    0 讨论(0)
  • 2020-12-11 07:11

    A response to @Reto: This code works, I just needed to add some fix because it doesn't insert anything if text area is empty. Also it adds only plain text. Here is the code if anybody need it:

    if(editorField.nodeValue==null){
      editor.setContent('<strong>Your content</strong>');
    }else{        
      editorField.nodeValue = editorField.nodeValue.substring(0, range.startOffset) +
                              '<strong>Your content</strong>' +
                              editorField.nodeValue.substring(range.endOffset, editorField.nodeValue.length);
      editor.setContent(editorField.nodeValue);
    }
    
    0 讨论(0)
  • 2020-12-11 07:12

    The way I solved this was to make the nicEdit Instance div droppable, using jQuery UI; but to also make all of the elements within the div droppable.

    $('div.nicEdit-main').droppable({
        activeClass: 'dropReady',
        hoverClass: 'dropPending',
        drop: function(event,ui) {
        $(this).find('.cursor').removeClass('cursor');
      },
      over: function(event, ui) {
        if($(this).find('.cursor').length == 0) {
          var insertEl = $('<span class="cursor"/>').append($(ui.draggable).attr('value'));
          $(this).append(insertEl);
        }
      },
      out: function(event, ui) {
        $(this).find('.cursor').remove();
      },
      greedy: true
    });
    
    $('div.nicEdit-main').find('*').droppable({
      activeClass: 'dropReady',
      hoverClass: 'dropPending',
      drop: function(event,ui) {
        $(this).find('.cursor').removeClass('cursor');
      },
      over: function(event, ui) {
        var insertEl = $('<span class="cursor"/>').append($(ui.draggable).attr('value'));
        $(this).append(insertEl);
      },
      out: function(event, ui) {
        $(this).find('.cursor').remove();
      },
      greedy: true
    });
    

    Then make your code or text draggable:

    $('.field').draggable({
                    appendTo: '.content', //This is just a higher level DOM element
                    revert: true,
                    cursor: 'pointer',
                    zIndex: 1500, // Make sure draggable drags above everything else
                    containment: 'DOM',
                    helper: 'clone' //Clone it while dragging (keep original intact)
                });            
    

    Then finally make sure you set the value of the draggable element to what you want to insert, and/or modify the code below to insert the element (span) of your choice.

            $sHTML .= "<div class='field' value='{{".$config[0]."}}'>".$config[1]."</div>";
    
    0 讨论(0)
  • 2020-12-11 07:13

    This function work when nicEdit textarea is empty or cursor is in the blank or new line.

    function addToCursorPosition(textareaId,value) {
                var editor = nicEditors.findEditor(textareaId);
                var range = editor.getRng();
                var editorField = editor.selElm();
                var start = range.startOffset;
                var end = range.endOffset;
                if (editorField.nodeValue != null) {
                    editorField.nodeValue = editorField.nodeValue.substring(0, start) +
                                value +
                                editorField.nodeValue.substring(end, editorField.nodeValue.length);
                }
                else {
                    var content = nicEditors.findEditor(textareaId).getContent().split("<br>");
                    var linesCount = 0;
                    var before = "";
                    var after = "";
                    for (var i = 0; i < content.length; i++) {
                        if (linesCount < start) {
                            before += content[i] + "<br>";
                        }
                        else {
                            after += content[i] + "<br>";
                        }
                        linesCount++;
                        if (content[i]!="") {
                            linesCount++;
                        }
                    }
                    nicEditors.findEditor(textareaId).setContent(before + value + after);
                }
    
            }
    
    0 讨论(0)
  • 2020-12-11 07:16

    This is a quick solution and tested in firefox only. But it works and should be adaptable for IE and other browsers.

    function insertAtCursor(editor, value){
        var editor = nicEditors.findEditor(editor);
        var range = editor.getRng();                    
        var editorField = editor.selElm();
        editorField.nodeValue = editorField.nodeValue.substring(0, range.startOffset) +
                                value +
                                editorField.nodeValue.substring(range.endOffset, editorField.nodeValue.length);
    }
    
    0 讨论(0)
  • 2020-12-11 07:18

    It works for me when I use:

    function neInsertHTML(value){
        $('.nicEdit-main').focus(); // Without focus it wont work!
        // Inserts into first instance, you can replace it by nicEditors.findEditor('ID');
        myNicEditor.nicInstances[0].nicCommand('InsertHTML', value); 
    }
    
    0 讨论(0)
提交回复
热议问题