[英]How can I increment and decrement the fontsize via shortcut? (tinymce)
我想像Microsoft Word中那樣增加和減少fontsize。 我需要一種快捷方式來增加,另一種要減少。 目前我有這個:
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));
});
});
好的,這主要起作用,但是如果我增加或減少的大小超過一個大小,舊的跨度將不會刪除。 我得到這樣的HTML代碼:
<p><span style="font-size:12px"><span style="font-size:13px">Hello World</span></span></p>
有人對我有解決方案或其他解決方法嗎?
謝謝菲利克斯
我有解決辦法! :) tinymce編輯器具有實現字體大小的已實現功能,但是您無法在文檔中找到。 在此處搜索“字體大小”: https : //github.com/tinymce/tinymce/blob/master/js/tinymce/classes/EditorCommands.js#L253- >第253行
這是我的代碼:
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");
}
});
}
});
這是我的插件:
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);
}
});
});
我希望它對某人有用。 :)問候費利克斯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.