3

我正在尝试在Redactor编辑器中创建一个下拉列表。最大的问题是使用选定的字体系列围绕选定的文本创建一个容器。

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

$("#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 函数非常相似,它也将选定的文本包装在一个容器中,并为其分配正确的颜色样式属性。

4

3 回答 3

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
        }
    }
});
于 2013-02-06T21:25:51.217 回答
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
于 2013-01-23T17:36:27.357 回答
0

Redactor 为此发布了新插件:

fontcolor.js, fontsize.js and fontfamily.js

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

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

请参阅此存储库: https ://github.com/johnsardine/redactor-plugins

于 2014-04-29T14:11:31.737 回答