在Redactor WYSIWYG中创建font-family下拉列表

时间:2012-11-07 00:35:38

标签: jquery drop-down-menu wysiwyg text-editor rich-text-editor

我正在尝试在Redactor编辑器中创建一个下拉列表。最大的问题是使用选定的font-family在所选文本周围创建一个容器。

到目前为止,我已经有了自定义下拉菜单的基本设置:

$("#text_edit").redactor({
    buttons: ['html', '|', 'bold', 'italic', 'deleted', '|', 'table', 'link', '|', 'fontcolor', 'backcolor', '|', 'fontfamily'],
    buttonsCustom: {
        fontfamily: {
            title: "Select Font",
            dropdown: {
                Arial: {
                    title: 'Arial',
                    callback: insertFont
                },
                Georgia: {
                    title: 'Georgia',
                    callback: insertFont
                }
            }
        }
    }
});

function insertFont(obj, e, key)
{
    // wrap selected text in <span> container with style attribute and selected font
}

实际上,所需的方法与内置的fontcolor函数非常相似,它还将所选文本包装在容器中,并为其指定正确的颜色样式属性。

3 个答案:

答案 0 :(得分:6)

改进之前的响应,这是JQuery中的一个实现,它将在下拉列表中很好地列出所有可能的Web安全字体:

var oFontMap: {
arial: ["Arial", "Arial, Helvetica, sans-serif"],
arialblack: ["Arial Black", '"Arial Black", Gadget, sans-serif'],
comicsans: ["Courier New", '"Courier New", Courier, monospace'],
courier: ["Comic Sans", '"Comic Sans MS", cursive, sans-serif'],
impact: ["Impact", 'Impact, Charcoal, sans-serif'],
lucida: ["Lucida", '"Lucida Sans Unicode", "Lucida Grande", sans-serif'],
lucidaconsole: ["Lucida Console", '"Lucida Console", Monaco, monospace'],
georgia: ["Georgia", "Georgia, serif"],
palatino: ["Palatino Linotype", '"Palatino Linotype", "Book Antiqua", Palatino, serif'],
tahoma: ["Tahoma", "Tahoma, Geneva, sans-serif"],
times: ["Times New Roman", "Times, serif"],
trebuchet: ["Trebuchet", '"Trebuchet MS", Helvetica, sans-serif'],
verdana: ["Verdana", "Verdana, Geneva, sans-serif"] };

//Create the font dropdown
var oFontDropdown = {}
$.each(oFontMap, function(iIndex, oFont){
    var sFontName = oFont[0];
    var sFontFace = oFont[1];
    oFontDropdown[iIndex] = {
        title: "<font face='"+sFontFace+"'>"+sFontName+"</font>",
        callback: function(obj, e, sFont){
            obj.execCommand("fontname", sFontFace);
        }
    }
});

return $(".redactor").redactor({
    focus: true,
    buttonsAdd: ["|", "font"],
    buttonsCustom: {
        font: {
            title: "Advanced Font List",
            dropdown: oFontDropdown
        }
    }
});

答案 1 :(得分:2)

你可以试试这个,我已经用这种方式解决了同样的要求,而且在这里运行良好。

var setFontFamily;

setFontFamily = function(obj, e, key) {
  if (key === "serif") {
    obj.execCommand("fontname", "Times New Roman");
  }
  if (key === "sans") {
    return obj.execCommand("fontname", "Helvetica");
  }
};

$(document).ready(function() {
  return $(".redactor").redactor({
    focus: true,
    buttonsAdd: ["|", "font"],
    buttonsCustom: {
      font: {
        title: "Advanced List",
        dropdown: {
          serif: {
            title: "Times",
            callback: setFontFamily
          },
          sans: {
            title: "Helvetica",
            callback: setFontFamily
          }
        }
      }
    }
  });
});

setFontFamily函数不是世界上最美丽的函数,但它是概念证明,然后你可以看到它如何定义字体系列。

我是一个rails编码器,所以我会留下相同的代码,但coffescript版本(如果有人需要它)

setFontFamily = (obj, e, key) ->
  obj.execCommand "fontname", "Times New Roman"  if key is "serif"
  obj.execCommand "fontname", "Helvetica"  if key is "sans"

$(document).ready ->
  $(".redactor").redactor
    focus: true
    buttonsAdd: ["|", "font"]
    buttonsCustom:
      font:
        title: "Advanced List"
        dropdown:
          serif:
            title: "Times"
            callback: setFontFamily
          sans:
            title: "Helvetica"
            callback: setFontFamily

答案 2 :(得分:0)

Redactor为此目的发布了新的插件:

fontcolor.js, fontsize.js and fontfamily.js

如果包含,您可以像这样使用它们:

elem.redactor({
      plugins: ['fontcolor', 'fontsize', 'fontfamily']
});

查看此存储库: https://github.com/johnsardine/redactor-plugins