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

以前の応答を改善して、ドロップダウンにすべての可能な Web セーフ フォントを一覧表示する JQuery の実装を次に示します。

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 に答える