LoginSignup
2
1

More than 5 years have passed since last update.

CKEditorをガラケー向けにカスタマイズする 続き

Last updated at Posted at 2017-09-15

本ページで記載しているCKEditorのバージョンは
4.6.2
になります。

前回(CKEditorをガラケー向けにカスタマイズする)
の続きで、今回はCKEditorのstylesとpluginに関してですかね。


ではまず

[左寄せ/中央寄せ/右寄せ]ボタン

こっちからいきますか。
先に完成イメージを貼っておきます。ペロッとめくってる[スタイル]のプルダウンがそれです。

ckeditor_styles.png

これは
styles.js
をいじることで対応してます。

では早速。
まずはデフォルトで表示されている項目をコメントアウトしていきます。

styles.jsの36行目付近
  
  { name: 'Italic Title',  element: 'h2', styles: { 'font-style': 'italic' } },
  { name: 'Subtitle',  element: 'h3', styles: { 'color': '#aaa', 'font-style': 'italic' } },
  {
    name: 'Special Container',
    element: 'div',
    styles: {
      padding: '5px 10px',
      background: '#eee',
      border: '1px solid #ccc'
    }
  },
  

これ↑をこう↓変更

styles.jsの36行目付近
  
//デフォルト削除
/*
  { name: 'Italic Title',  element: 'h2', styles: { 'font-style': 'italic' } },
  { name: 'Subtitle',  element: 'h3', styles: { 'color': '#aaa', 'font-style': 'italic' } },
  {
    name: 'Special Container',
    element: 'div',
    styles: {
      padding: '5px 10px',
      background: '#eee',
      border: '1px solid #ccc'
    }
  },
*/
  

同様に
62行目~80行目、84行目~111行目、115行目~134行目
をコメントアウトしていきます。

続いて今度は追加です。
ブロックスタイルのセクションとインラインスタイルのセクションにこんな感じで追記。

styles.js
CKEDITOR.stylesSet.add( 'default', [
  /* Block styles */
  
  // 追加
  { name: '左寄せ',       element: 'div',     attributes: { align: 'left'   } },
  { name: '中央寄せ',     element: 'div',     attributes: { align: 'center' } },
  { name: '右寄せ',       element: 'div',     attributes: { align: 'right'  } },

  /* Inline styles */
  
  // 追加
  { name: 'センター',     element: 'center'                                   

  /* Object styles */
  
},

これでほぼ完成です。
この状態で表示するとこんな感じです。
ckeditor_styles2.png

あとはプルダウンメニューグループの
ブロックスタイル
インラインスタイル
これを書き換えます。

この文字列ですが、
CKEditorフォルダにあるlangフォルダのja.jsに書いてあります。
ブロックスタイル/インラインスタイルでそれぞれ検索して書き換えればOK。

ただこの ja.js ですが・・・
CKEditorの全体定義なのでスタイルプルダウン以外にも使用している場所があると
そっちも変わっちゃう可能性が大きいです。

ただの見た目なので変えなくてもOKですが、
変えないとcenterタグなのになんでインラインの中に?
とか気になっちゃう方は、styles.jsをいじってcenterタグの記述箇所を移動してください


さて残るひとつ

[行背景色]ボタン

これはpluginを作っていくんですが、
CKEditorのpluginは、
CKEditorフォルダにあるpluginsフォルダに適当な名前のフォルダを作成し、
その中に、
plugin.js
というファイルを作成して、ここに処理を書いていきます。
あとは
config.js
に使用するpuginを書く、という感じになります。

ではまず完成したものがコチラです。
ckeditor_bgcolor.png

ちなみに選択してテキスト入力するとこんな感じ。

ckeditor_bgcolor_sample.png

ソースを表示すると・・・

ckeditor_bgcolor_source.png

こんな感じです。

pluginなんで説明いりますかね・・・?
ソースのっけておくのでブラックボックスとして使ってください。

mybgcolor/plugin.js
CKEDITOR.plugins.add('mybgcolor',
{
  requires : ['richcombo'],
  init : function( editor )
  {
    var strings = [];
    strings.push(['<table width="100%" bgcolor="#ffffff"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#ffffff; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;白', '']);
    strings.push(['<table width="100%" bgcolor="#000000"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#000000; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;黒', '']);
    strings.push(['<table width="100%" bgcolor="#a9a9a9"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#a9a9a9; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;グレー', 'グレー']);
    strings.push(['<table width="100%" bgcolor="#ff0000"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#ff0000; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;赤', '']);
    strings.push(['<table width="100%" bgcolor="#ffc0cb"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#ffc0cb; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;ピンク', 'ピンク']);
    strings.push(['<table width="100%" bgcolor="#0000ff"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#0000ff; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;青', '']);
    strings.push(['<table width="100%" bgcolor="#adc3ff"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#adc3ff; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;薄い青', '薄い青']);
    strings.push(['<table width="100%" bgcolor="#800080"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#800080; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;紫', '']);
    strings.push(['<table width="100%" bgcolor="#ffff00"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#ffff00; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;黄', '']);
    strings.push(['<table width="100%" bgcolor="#7cfc00"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#7cfc00; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;緑', '']);
    strings.push(['<table width="100%" bgcolor="#c0c0c0"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#c0c0c0; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;銀', '']);
    strings.push(['<table width="100%" bgcolor="#ffd700"><tr><td>&nbsp;</td></tr></table>', '<span style="background-color:#ffd700; display: inline-block; width:50pt;">&nbsp;</span>&nbsp;金', '']);

    editor.ui.addRichCombo('mybgcolor',
    {
      label:     '行背景色',
      title:     '行背景色',
      voiceLabel: '行背景色',
      className:   'cke_format',
      multiSelect:false,
      panel:
      {
        css: [ editor.config.contentsCss, CKEDITOR.skin.getPath('editor') ],
        voiceLabel: editor.lang.panelVoiceLabel
      },

      init: function()
      {
        this.startGroup( "" );
        for (var i in strings)
        {
          this.add(strings[i][0], strings[i][1], strings[i][2]);
        }
      },

      onClick: function( value )
      {
        editor.focus();
        editor.fire( 'saveSnapshot' );
        editor.insertHtml(value);
        editor.fire( 'saveSnapshot' );
      }
    });
  }
});


pluginの適用を設定する

config.js
CKEDITOR.editorConfig = function( config ) {
  
  config.extraPlugins = 'mybgcolor';
};


ボタンをHTMLに配置する
自分はconfigの方に書いてますが・・・

    <script type="text/javascript">
    CKEDITOR.config.toolbar = [
    
    ,['Styles','Format','Font','FontSize','mybgcolor']
    
    ];
    </script>


以上です
最後に自分のconfigやらを・・・と思ったんですが2点ほど
・プレビューでウィンドウがOPENするけど幅をガラケーサイズに。
・オフィスのワードからクリップボードにコピーして貼りつけるとき、全角スペースが半角スペースになっちゃう!

というのがありまして・・・

これらも対応したのですが
また長くなったので別ページにて。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1