首页 > 解决方案 > 使用单个快捷方式将字体大小从 `10px` 更改为 `14px` 到 `18px`

问题描述

我正在为 TinyMCE5 创建一个插件,用于在 和 之间切换所选文本的10px字体14px大小18px。(默认字体大小为14px

这是我的尝试:

editor.addCommand('customfontsize_command', 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("p", 1)
fontsize--;

if (fontsize > 0 && fontsize <= 100) { // only work for first time
    switch (fontsize) {
        case 18:
            fontsize = 14;
            break;
        case 10:
            fontsize = 8;
            break;
        case 8:
            fontsize = 18;
            break;
        default:
            fontsize = 14;
    }

    fontsize = fontsize + "px";
    tinymce.activeEditor.execCommand('fontsize', false, fontsize);
}
}); // end customfontsize_command
editor.addShortcut('alt+a', 'customfontsize_command_desc', 'customfontsize_command');

但它只适用于我第一次做我的快捷方式。

我也试过这个,但结果相同:

if (fontsize > 10 && fontsize <= 14) { 
    fontsize = 10;
} else if (fontsize <= 10) {
    fontsize = 18;
} else {
    fontsize = 14;
}

我可以使用像这样的 2 种不同的快捷方式来完成这项工作,但我更喜欢在 3 种尺寸之间切换的单个快捷方式:

editor.addCommand('small_size', 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("p", 1)
    fontsize = 10
    fontsize = fontsize + "px";
    tinymce.activeEditor.execCommand('fontsize', false, fontsize);

}); // end customfontsize_command


editor.addCommand('big_size', 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("p", 1)
    fontsize = 18
    fontsize = fontsize + "px";
    tinymce.activeEditor.execCommand('fontsize', false, fontsize);

}); // end customfontsize_command

标签: javascripttinymce

解决方案


试试这个,

fontsize = (fontsize <= 10) ? 18 : ((fontsize > 10 && fontsize <= 14) ? 10 : 14);

推荐阅读