Reputation: 350
I'm trying to add a menu with different options in TinyMCE4 and when I click one element the text of the option in the editor canvas. I successfully added the menu with one option only with the following code I found:
tinymce.init({
language: 'es',
selector: '#plantillaEditor',
height : '500',
width : '300',
menu : {
file : {title : 'File' , items : 'newdocument'},
edit : {title : 'Edit' , items : 'undo redo | cut copy paste pastetext | selectall'},
insert : {title : 'Insert', items : 'link media | template hr'},
view : {title : 'View' , items : 'visualaid'},
format : {title : 'Format', items : 'bold italic underline strikethrough superscript subscript | formats | removeformat'},
table : {title : 'Table' , items : 'inserttable tableprops deletetable | cell row column'},
tools : {title : 'Tools' , items : 'code'},
tags: {title : 'Etiquetas', items : 'newmenuitem'}
},
menubar: 'file edit format table tags',
setup: function(editor) {
editor.addMenuItem('newmenuitem', {
text: 'new menu',
context: 'tags',
onclick: function () { console.log(this); }
}
);
},
readonly: ((getUrlVars()["mode"]=='view') ? true : false),
plugins:'image imagetools link fullscreen fullpage textcolor colorpicker advlist autolink autosave charmap code contextmenu insertdatetime save print table',
toolbar: "customToolbar undo redo | save | print | styleselect | forecolor backcolor | bold italic | "+
"alignleft aligncenter alignright alignjustify | table bullist numlist outdent indent | "+
" link | image | charmap code | insertdatetime",
insertdatetime_formats: ["%H:%M", "%d-%m-%Y", "%S:%M:%I %p", "Buenos Aires, %d de %B de %Y"],
contextmenu: "copy | cut | paste | link image imageupload | cell row column deletetable",
autosave_interval: "60s",
paste_data_images: true,
save_onsavecallback: function () {
var bodyHtml=$editor.val().match(/(?:.(?!<\s*body[^>]*>))*\>.+/)[0];
var mode='<?php echo $mode?>';
var namePattern;
var namePrefix;
var textAreaName;
if(mode!=='user'){
var request = $.ajax({
type: 'POST',
url: '/editor/processDataHtml',
data: { editorData: bodyHtml,
id_acto_doc_plantilla : '<?php echo $id_acto_doc_plantilla; ?>'
},
success: function(data) {
alert(data);
},
error: function(data) {
alert(data);
}
})
}else{
$htmlInputParent.val(bodyHtml);
}
}
});
I have to solve 2 things:
1)How can I add more than one element to the menu?. There's no documentation on the "setup:" parameter showing and example and I tried adding another "date" element on to the menu:
tags: {title : 'Etiquetas', items : 'newmenuitem date'}
and then the setup parameter with more than one element:
setup: function(editor) {
editor.addMenuItem('newmenuitem', {
text: 'new menu',
context: 'tags',
onclick: function () { console.log(this); }
}),
editor.addMenuItem('date', {
text: 'Date',
context: 'tags',
onclick: function () { console.log(this); }
});
},
But it doesn't seem to work.....
2)The second problem is that I don't know how to retrieve the text value of the option on the menu when the element is clicked. When I log "this" I've searched among the properties of the object and I couldn't find a prop that holds the value of the item.
Does someone know how can I do these 2 things?
EDIT: I solved the issue # 1) ....it was just a typo, the correct code for the setup: parameter is:
tags: {title : 'Etiquetas', items : 'newmenuitem date'},
> setup: function(editor) {
> editor.addMenuItem('newmenuitem', {
> text: 'new menu',
> context: 'tags',
> onclick: function () { console.log(this); }
> });
> editor.addMenuItem('date', {
> text: 'Date',
> context: 'tags',
> onclick: function () { console.log(this); }
> });
> },
The issue #2) is still not working. I tried to log to the console the text content of the menu option using javascript and jquery as well without luck, the object is returned properly but when I filter the properties of the object using javascript console I can't find the attribute.....
Upvotes: 1
Views: 909
Reputation: 13744
I believe that the settings attribute of the object that this
returns will have what you need:
editor.addMenuItem('menuitem1', {
text: 'Text for Menu Item 1',
context: 'tags',
onclick: function () {
console.log(this.settings.text);
}
});
Note that you can even add custom attributes to the object you pass to the addMenuItem
method and access those at runtime:
editor.addMenuItem('menuitem1', {
text: 'Text for Menu Item 1',
customAttrib: 'Colorado Avalanche',
context: 'tags',
onclick: function () {
console.log(this.settings.text);
console.log(this.settings.customAttrib);
}
});
The second console.log
is referencing the customAttrib
attribute which is not part of the required attributes. TinyMCE is just expecting a valid JavaScript object so you can put whatever you like in there as long as you put the things TinyMCE requires.
Upvotes: 2