在 NicEdit 中插入 HTML 所见即所得

发布于 2024-08-30 10:03:53 字数 90 浏览 5 评论 0原文

如何在 NicEdit 创建的 div 中的光标处插入文本/代码?

我尝试阅读文档并创建自己的插件,但我希望它能够在没有工具栏(模态窗口)的情况下工作

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 without the tool bar (Modal Window)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

骑趴 2024-09-06 10:03:54

这是一个快速解决方案,仅在 Firefox 中进行了测试。但它可以工作并且应该适用于 IE 和其他浏览器。

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

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);
}
昔梦 2024-09-06 10:03:54

插入 Html 插件

不知道这是否有帮助,但这是我为在光标位置插入 Html 而创建的插件。该按钮会打开一个内容窗格,我只需粘贴所需的 html 并提交即可。对我有用!

var nicMyhtmlOptions = {
    buttons : {
      'html' : {name : 'Insert Html', type : 'nicMyhtmlButton'}
    },iconFiles : {'html' : '/nicedit/html_add.gif'}

};

var nicMyhtmlButton=nicEditorAdvancedButton.extend({
      addPane: function () {
      this.addForm({
        '': { type: 'title', txt: 'Insert Html' },
        'code' : {type : 'content', 'value' : '', style : {width: '340px', height : '200px'}}
      });
    },

    submit : function(e) {
      var mycode = this.inputs['code'].value;
      this.removePane();
      this.ne.nicCommand('insertHTML', mycode );
    }

});
nicEditors.registerPlugin(nicPlugin,nicMyhtmlOptions);

我使用了 Silk Icons 中的 html_add 图标,粘贴到透明的 18 x 18 上并保存作为 gif 与 nicEditorIcons.gif 位于同一文件夹中。

Insert Html Plugin

Don't know if this will help or not, but this is the plugin I created for inserting Html at the cursor position. The button opens a content pane and I just paste in the html I want and submit. Works for me!

var nicMyhtmlOptions = {
    buttons : {
      'html' : {name : 'Insert Html', type : 'nicMyhtmlButton'}
    },iconFiles : {'html' : '/nicedit/html_add.gif'}

};

var nicMyhtmlButton=nicEditorAdvancedButton.extend({
      addPane: function () {
      this.addForm({
        '': { type: 'title', txt: 'Insert Html' },
        'code' : {type : 'content', 'value' : '', style : {width: '340px', height : '200px'}}
      });
    },

    submit : function(e) {
      var mycode = this.inputs['code'].value;
      this.removePane();
      this.ne.nicCommand('insertHTML', mycode );
    }

});
nicEditors.registerPlugin(nicPlugin,nicMyhtmlOptions);

I used the html_add icon from Silk Icons, pasted onto a transparent 18 x 18 and saved as gif in the same folder as nicEditorIcons.gif.

爱你是孤单的心事 2024-09-06 10:03:54

当我使用时它对我有用:

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

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); 
}
离不开的别离 2024-09-06 10:03:54

我解决这个问题的方法是使用 jQuery UI 使 nicEdit Instance div 可放置;但也使 div 中的所有元素都可放置。

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

然后使您的代码或文本可拖动:

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

最后确保将可拖动元素的值设置为您想要插入的内容,和/或修改下面的代码以插入您选择的元素(跨度)。

        $sHTML .= "<div class='field' value='{{".$config[0]."}}'>".$config[1]."</div>";

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>";
厌味 2024-09-06 10:03:54

对@Reto的回应:这段代码有效,我只需要添加一些修复,因为如果文本区域为空,它不会插入任何内容。它还仅添加纯文本。如果有人需要的话,这是代码:

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

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);
}
盛夏尉蓝 2024-09-06 10:03:54

在 NicEdit.js 文件中更改以下内容

从 Reto Aebersold Ans 更新 如果文本区域为空,它将处理 Null Node 异常

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("<<" + oldStr + ">>");
            }
            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);
        }

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("<<" + oldStr + ">>");
            }
            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);
        }
行雁书 2024-09-06 10:03:54

当 nicEdit 文本区域为空或光标位于空白行或新行时,此功能有效。

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

        }

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

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文