【问题标题】:How can I increment and decrement the fontsize via shortcut? (tinymce)如何通过快捷方式增加和减少字体大小? (小)
【发布时间】:2014-10-14 17:09:51
【问题描述】:

我想像 microsoft word 中那样增加和减少字体大小。 我需要一个快捷方式来增加和一个减少。 目前我有这个:

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>

有没有人可以为我提供解决方案或其他方式来做到这一点?

谢谢菲利克斯

【问题讨论】:

    标签: javascript html tinymce font-size


    【解决方案1】:

    我有办法! :) 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);
        }
    }); 
    });
    

    我希望它对某人有用。 :) 问候菲利克斯

    【讨论】:

      猜你喜欢
      • 2013-08-04
      • 1970-01-01
      • 2015-06-11
      • 2021-03-13
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      相关资源
      最近更新 更多