javascript - 使用单个快捷方式将字体大小从 `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
解决方案
试试这个,
fontsize = (fontsize <= 10) ? 18 : ((fontsize > 10 && fontsize <= 14) ? 10 : 14);
推荐阅读
- django - Django:通过表单向另一台服务器发送发布请求
- java - 如何在我的 lambda 函数中传递多个 get 参数?
- python - 是否有可以对有限状态机进行时间逻辑模型检查的 Python 包?
- android - 监听应用程序通知设置的变化
- json - 期望一个大小为 2 但集合大小为 0 的集合?
- android - body_loc_args 中的 FCM 格式日期
- node.js - 如何卸载间接依赖
- python - 如何将程序的 STDOUT 重定向到 GDB 调试程序的 STDIN?
- c# - this[] 索引器:类型“SheetsDecoratorImpl”已经保留了一个名为“get_Item”的成员,具有相同的参数类型
- r - 如何创建一个新变量,然后用多个条件更改它?