Reputation:
I want to increment and decrement the fontsize like in microsoft word. I need one shortcut to increment and one to decrement. Currently I have this:
tinymce.PluginManager.add('ds_fontsize', function (editor, url) {
editor.addMenuItem('fontsize_up', {
text: 'fontsize_up',
icon: false,
onclick: function () {
editor.execCommand('fontsize_up');
}
});
editor.addMenuItem('fontsize_down', {
text: 'fontsize-down',
icon: false,
onclick: function () {
editor.execCommand('fontsize_down');
}
});
editor.addCommand('fontsize_down', function () {
var content = tinymce.activeEditor.selection.getContent();
var node = tinymce.activeEditor.selection.getNode();
var fontsize = tinymce.activeEditor.dom.getStyle(node, 'font-size', true);
fontsize = fontsize.split("px", 1)
fontsize--;
//remove old span
tinyMCE.activeEditor.execCommand('mceReplaceContent', false, '', 'span');
tinyMCE.activeEditor.selection.setNode(tinyMCE.activeEditor.dom.create('span', { style: 'font-size: 15px' }, content));
});
editor.addCommand('fontsize_up', function () {
var content = tinymce.activeEditor.selection.getContent();
var node = tinymce.activeEditor.selection.getNode();
var fontsize = tinymce.activeEditor.dom.getStyle(node, 'font-size', true);
fontsize = fontsize.split("px", 1)
fontsize++;
//remove old span
tinyMCE.activeEditor.execCommand('mceReplaceContent', false, '', 'span');
tinyMCE.activeEditor.selection.setNode(tinyMCE.activeEditor.dom.create('span', { style: 'font-size:' + fontsize + 'px' }, content));
});
});
Ok this works mainly, but if I increment or decrement more then one size the old span wouldn't remove. I get HTML-Code like this:
<p><span style="font-size:12px"><span style="font-size:13px">Hello World</span></span></p>
Does anyone have a solution for me or an other way to do this?
Thanks Felix
Upvotes: 2
Views: 563
Reputation:
I have a solution! :) The tinymce editor have an implemented function to set the fontsize, but you can`t find in the documentation. Search here "fontsize": https://github.com/tinymce/tinymce/blob/master/js/tinymce/classes/EditorCommands.js#L253 -> Line 253
And here is my code:
tinymce.init({
...
setup: function (editor) {
//initialize shortcuts
editor.on('keydown', function (e) {
//decrement fontsize
if (e.ctrlKey && !e.shiftKey && e.keyCode == 226) {
e.preventDefault();
editor.execCommand("fontsize_down");
}
//increment fontsize
if (e.ctrlKey && e.shiftKey && e.keyCode == 226) {
e.preventDefault();
editor.execCommand("fontsize_up");
}
});
}
});
And here is my plugin:
tinymce.PluginManager.add('ds_fontsize', function (editor, url) {
editor.addMenuItem('fontsize_up', {
text: 'fontsize_up',
icon: false,
onclick: function () {
editor.execCommand('fontsize_up');
}
});
editor.addMenuItem('fontsize_down', {
text: 'fontsize-down',
icon: false,
onclick: function () {
editor.execCommand('fontsize_down');
}
});
editor.addCommand('fontsize_down', function () {
var content = tinymce.activeEditor.selection.getContent();
var node = tinymce.activeEditor.selection.getNode();
var fontsize = tinymce.activeEditor.dom.getStyle(node, 'font-size', true);
fontsize = fontsize.split("px", 1)
fontsize--;
if (fontsize > 7 && fontsize <=72) {
switch (fontsize) {
case 35:
fontsize = 28;
break;
case 47:
fontsize = 36;
break;
case 71:
fontsize = 48;
break;
default:
if (fontsize > 12) fontsize--;
}
fontsize = fontsize + "px";
alert(fontsize);
tinymce.activeEditor.execCommand('fontsize', false, fontsize);
}
});
editor.addCommand('fontsize_up', function () {
var content = tinymce.activeEditor.selection.getContent();
var node = tinymce.activeEditor.selection.getNode();
var fontsize = tinymce.activeEditor.dom.getStyle(node, 'font-size', true);
fontsize = fontsize.split("px", 1)
fontsize++;
if (fontsize > 7 && fontsize <= 72) {
switch (fontsize) {
case 29:
fontsize = 36;
break;
case 37:
fontsize = 48;
break;
case 49:
fontsize = 72;
break;
default:
if (fontsize > 12) fontsize++;
}
fontsize = fontsize + "px";
alert(fontsize);
tinymce.activeEditor.execCommand('fontsize', false, fontsize);
}
});
});
I hope it is useful for someone. :) Greetings Felix
Upvotes: 3