2015-12-27 16 views

Trả lời

5

Viết plugin của riêng bạn.

Giải pháp sau dựa trên this article. Plugin charactercount đếm các ký tự thực tế mà người dùng nhìn thấy, tất cả các ký tự HTML và ẩn sẽ bị bỏ qua.

Character Đếm Plugin:

tinymce.PluginManager.add('charactercount', function (editor) { 
    var self = this; 

    function update() { 
    editor.theme.panel.find('#charactercount').text(['Characters: {0}', self.getCount()]); 
    } 

    editor.on('init', function() { 
    var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0]; 

    if (statusbar) { 
     window.setTimeout(function() { 
     statusbar.insert({ 
      type: 'label', 
      name: 'charactercount', 
      text: ['Characters: {0}', self.getCount()], 
      classes: 'charactercount', 
      disabled: editor.settings.readonly 
     }, 0); 

     editor.on('setcontent beforeaddundo', update); 

     editor.on('keyup', function (e) { 
      update(); 
     }); 
     }, 0); 
    } 
    }); 

    self.getCount = function() { 
    var tx = editor.getContent({ format: 'raw' }); 
    var decoded = decodeHtml(tx); 
    var decodedStripped = decoded.replace(/(<([^>]+)>)/ig, "").trim(); 
    var tc = decodedStripped.length; 
    return tc; 
    }; 

    function decodeHtml(html) { 
    var txt = document.createElement("textarea"); 
    txt.innerHTML = html; 
    return txt.value; 
    } 
}); 

CSS Tweaks:

/* Optional: Adjust the positioning of the character count text. */ 
label.mce-charactercount { 
margin: 2px 0 2px 2px; 
padding: 8px; 
} 

/* Optional: Remove the html path code from the status bar. */ 
.mce-path { 
    display: none !important; 
} 

TinyMCE Khởi

$('textarea.tinymce').tinymce({ 
    plugins: "charactercount", 
    statusbar: true, 
    init_instance_callback: function (editor) { 
    $('.mce-tinymce').show('fast'); 
    $(editor.getContainer()).find(".mce-path").css("display", "none"); 
    } 
    // ... 
}); 

ps. Sử dụng trình chỉnh sửa JS.

1
init_instance_callback: function (editor) { 
editor.on('change', function (e) { 
       var length = editor.contentDocument.body.innerText.length; 
      }); 
} 

Trên init, hãy thêm điều này. chiều dài là độ dài ký tự của bạn. Bây giờ bạn cần phải ẩn số lượng từ và đính kèm một chuỗi mới với bộ đếm ký tự.

Các vấn đề liên quan