kindeditorはバイトの計算方法を追加して、中国語のバイト数の問題を解決します

1604 ワード

前述:プロジェクトではkindeditorをリッチテキストエディタとして使用するが、中国語はデータベースで3バイト、英語で1バイト、pタグ改行ラベルもそれぞれ対応するコード長バイトを占有する.
kindeditorバージョン@version 4.1.2 (2012-07-21)
ページは次のコードカウントを使用します.
js:
afterChange : function() {K('.word_count').html(this.count());}

 html:
<p>       <span class="word_count">0</span>    。</p>   

質問:kindeditorが持参したカウンタは文字で計算されるので、提供した文字数でデータベースにコミットすると
だからGoogleの後フィルタ文字Unicodeを見つけてカウントする方法を見つけて、少し修正してkindeditorに追加します.jsのcount(5111行)メソッドの後にcountCodeと名付けられる. 
	countCode : function() {
	var self = this,
	total = 0,
        i,
        str = _removeBookmarkTag(_removeTempTag(self.html())),
        len;
	for(i = 0, len = str.length; i < len; i++){
            charCode = str.charCodeAt(i);
            if(charCode <= 0x007f) {
                total += 1;
            }else if(charCode <= 0x07ff){
                total += 2;
            }else if(charCode <= 0xffff){
                total += 3;
            }else{
                total += 4;
            }
        }
	return total;
	},

対応するページが呼び出すjsも変更する.
afterChange : function() {K('.word_count').html(this.countCode());}

事後追加:
kindeditor-min.jsの変更
「count:」の検索後に追加
countCode:function(){var a=0,i,b=V(ab(this.html())),c;for(i=0,c=b.length;i<c;i++){d=b.charCodeAt(i);if(d<=0x007f){a+=1;}else if(d<=0x07ff){a+=2;}else if(d<=0xffff){a+=3;}else{a+=4;}}return a;},

これでkindeditor-min.jsを再ページで使用できます.