さくっとつくった FCKeditor マイ設定


FCKeditor のデフォのフォントリストが貧弱だよ

追加するには一つ一つ設定していかなきゃだよ

めんどいからローカルにあるやつ全部つっこもうぜ

というわけで、FCKeditor のフォントリストに、ローカルで対応してるフォントを全て突っ込むスクリプト
参考 Kazuho@Cybozu Labs

考え方
swf オブジェクトからフォントリストを引き出せるので、それを FCKConfig.FontNames に設定してやる。


FCKeditor を表示させる URL

<!-- HEAD の中で -->
<script type="text/javascript" src="fckeditor.helper.js"></script>

<!-- BODY の中で -->
<script type="text/javascript">
  FCKeditorHelper.writeSwf();
</script>

<!-- FCKeditor を表示させたい部分 -->
<div id="fckeditor_field"></div>

fckeditor.helper.js
ヘルパ関数群

var FCKeditorHelper = {};

FCKeditorHelper.writeSwf = function()
{
  var tags =
'<object id="flashy" name="flashy"\
         classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\
         codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"\
         data="./flashy.swf" width="1" height="1">\
   <param name="movie" value="./flashy.swf" />\
 </object>';
  if (!navigator.userAgent.toString().match(/MSIE/)) {
    tags = tags.replace(/codebase=".*?"/, "").replace(/classid=".*?"/, 'type="application/x-shockwave-flash"');
  }
  document.write(tags);
}

FCKeditorHelper.getFontNames = function()
{
  var flash = document.getElementById('flashy'),
      fonts = flash.callStatic('flash.text.Font.enumerateFonts', [true]),
      _fonts = [],
      i = 0, len = fonts.length;
  for ( ; i < len; i++) {
    _fonts[_fonts.length] = fonts[i].fontName;
  }
  return _fonts;
}

FCKeditorHelper.show = function()
{
  var oFCKeditor = new FCKeditor('FCKeditor1');
  oFCKeditor.BasePath = './fckeditor/';
  oFCKeditor.Config['CustomConfigurationsPath'] = '../../fckeditor.config.js';
  document.getElementById('editor_field').innerHTML = oFCKeditor.CreateHtml();
}

if (window.addEventListener) {
  window.addEventListener('load', FCKeditorHelper.show, false);
} else {
  window.attachEvent('onload', FCKeditorHelper.show);
}

fckeditor.config.js
FCKeditor 用のコンフィグファイル

FCKConfig.FontNames = window.parent.FCKeditorHelper.getFontNames().join(';');
// このファイルも fckconfig.js も iframe 先でロードされるので、
// window.parent でトップの window オブジェクトを経由する。

なお、 flashy.swf は
Kazuho@Cybozu Labs: JavaScript から Flash の便利な機能を使う方法
にあったもの。
ほんと助かりました、ありがとうございました。


事前に swf を書き出して、 onload を待ってアクセスするのが汚いな。
swf がロードしきるまで待つような関数作ってもいいけど、それはまた別の機会で。*1

*1:それで動くなら無闇に関数付け足すな