使用一个快捷方式将字体大小从 `10px` 更改为 `14px` 到 `18px`

changing fontsize from `10px` to `14px` to `18px` with a single shortcut

我正在为 TinyMCE5 创建一个插件,它可以在 10px14px18px 之间切换所选文本的字体大小。 (字体大小默认为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

试试这个,

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