NicEditによって作成されたdivのカーソル位置にテキスト/コードを挿入するにはどうすればよいですか?
ドキュメントを読んで独自のプラグインを作成しようとしましたが、ツールバーなしで機能させたい(モーダルウィンドウ)
NicEditによって作成されたdivのカーソル位置にテキスト/コードを挿入するにはどうすればよいですか?
ドキュメントを読んで独自のプラグインを作成しようとしましたが、ツールバーなしで機能させたい(モーダルウィンドウ)
これは迅速な解決策であり、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);
}
これが役立つかどうかはわかりませんが、これはカーソル位置に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に貼り付けて、nicEditorIcons.gifと同じフォルダーにgifとして保存しました。
私が使用するときそれは私のために働きます:
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);
}
私がこれを解決した方法は、jQuery UIを使用して、nicEditインスタンスの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>";
@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);
}
NicEdit.jsファイルの以下を変更します
Reto Aebersold Ansから更新テキスト領域が空の場合、Nullノード例外を処理します
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);
}
この関数は、nicEdit textareaが空の場合、またはカーソルが空白行または改行にある場合に機能します。
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);
}
}