at posts/single.html

下書きプラグイン (draft.js) を IE8 に対応

下書きプラグインを作ってから、日記を書くときの安心感が大幅にアップした。 今のところまだ下書きから復旧したことはないけど、間違ってタブを閉じても、サーバーが落ちていても、日記の内容が消えないって思えるのはいいねぇ。 とはいえ、まだプレビュー前には CTRL+A, CTRL+C を押してクリップボードに保存するクセが抜けないんだけど。

下書きプラグインは Chrome5, 6, Firefox 3.6, Safari5 で動作確認できていたんだけど、 IE8 では動いていなかった。 調べてみたら、 Array オブジェクトに map メソッドが存在しなかったのが原因。 map が追加されたのは JavaScript 1.6 からだから、まだ対応していないブラウザもあるんだな。

ということで、 Mozilla Development Center に載っていたコードを下書きプラグインに取り込んだ。

Compatibility

map is a recent addition to the ECMA-262 standard; as such it may not be present in all browsers. You can work around this by inserting the following code at the beginning of your scripts, allowing use of map in implementations which do not natively support it. This algorithm is exactly the one used in Firefox and SpiderMonkey.

if (!Array.prototype.map)
{
  Array.prototype.map = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array(len);
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        res[i] = fun.call(thisp, this[i], i, this);
    }

    return res;
  };
}

これで IE8 でも下書きプラグインが動くようになった。 tDiary 3.0 のリリースが待ち遠しいね。

関連する日記