var summernote = {
run: function(id, type) {
if(type && type === 'full') {
this.full(id);
} else {
this.simple(id);
}
},
simple: function(id) {
// todo
},
full: function(id) {
var summernote_params = { height:300,
lang: 'fr-FR',
toolbar: [
['misc', ['undo', 'redo']],
['style', ['bold', 'italic', 'underline', 'clear']],
['font', ['superscript', 'subscript']],
['para', ['ul', 'ol']],
['mybuttons', ['docimage', 'docdocument']],
],
buttons: {
docimage: summernote.docImage,
docdocument: summernote.docDocument,
},
};
$('#'+id).summernote(summernote_params);
},
docImage: function(context) {
var ui = $.summernote.ui;
var button = ui.button({
contents: '<i class="fa fa-file-image-o"></i>',
tooltip: 'Insert image',
click: function() {
documents.run({filter:'image', callback:[summernote.docImageCallback, context]});
}
});
return button.render();
},
docImageCallback: function(context, doc) {
context.invoke('editor.insertImage', doc.url);
},
docDocument: function(context) {
var ui = $.summernote.ui;
var button = ui.button({
contents: '<i class="fa fa-file-o"></i>',
tooltip: 'Insert file',
click: function() {
documents.run({filter:'document', callback:[summernote.docDocumentCallback, context]});
}
});
return button.render();
},
docDocumentCallback: function(context, doc) {
// here i want to get id dynamically
$('#description').summernote('createLink', {
text: doc.name,
url: doc.url,
newWindow: true
});
},
}
Ve çağrı Yani her sorun için bir çözüm buldum.
Init'teki paramlar, bağlam içinde gönderilir.
var summernote = {
...
full: function(id) {
var summernote_params = { id: id, // <-- set id in context, (cutsom data)
height: 300,
lang: 'fr-FR',
toolbar: [
['misc', ['undo', 'redo']],
['style', ['bold', 'italic', 'underline', 'clear']],
['font', ['superscript', 'subscript']],
['para', ['ul', 'ol']],
['mybuttons', ['docimage', 'docdocument']],
],
buttons: {
docimage: summernote.docImage,
docdocument: summernote.docDocument,
},
};
$('#'+id).summernote(summernote_params);
},
...
docDocumentCallback: function(context, doc) {
var id = context.options.id; // <-- get id from context
$('#'+id).summernote('createLink', {
text: doc.name,
url: doc.url,
newWindow: true
});
},
}
Ve dış çağrısı ile imleç konumunu korumak için, sadece pozisyon kaydetmek ve geri arama işlevi ayarlayın: bağlamında id almak için ben sadece init eklemek
var summernote = {
...
docDocument: function(context) {
var ui = $.summernote.ui;
var button = ui.button({
contents: '<i class="fa fa-file-o"></i>',
tooltip: app_i18n.summernote_document,
click: function() {
context.invoke('editor.saveRange'); // <-- save position cursor
documents.run({filter:'document', callback:[summernote.docDocumentCallback, context]});
}
});
return button.render();
},
docDocumentCallback: function(context, doc) {
context.invoke('editor.restoreRange'); // <-- set position cursor to the last save
if(context.options.id) {
$('#'+context.options.id).summernote('createLink', {
text: doc.name,
url: doc.url,
newWindow: true
});
}
},
}