Projet

Général

Profil

Paste
Télécharger (2,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ckeditor / plugins / counter / plugin.js @ c8740e19

1
/**
2
 * @file Plugin to count symbols, symbols without blanks and words
3
 */
4
( function(){
5
  var emptyHtml = '<span class="cke_empty">&nbsp;</span>';
6
  CKEDITOR.plugins.add( 'counter',
7
  {
8
    init : function( editor )
9
    {
10
      var spaceId = 'cke_counter_' + editor.name;
11
      var spaceElement;
12
      var ckeditorEventThemeSpace = 'uiSpace';
13
      var getSpaceElement = function()
14
      {
15
        if ( !spaceElement )
16
          spaceElement = CKEDITOR.document.getById( spaceId );
17
        return spaceElement;
18
      };
19

    
20
      if (Drupal.ckeditor_ver == 3) {
21
        ckeditorEventThemeSpace = 'themeSpace';
22
      }
23

    
24
      editor.on( ckeditorEventThemeSpace, function( event )
25
      {
26
        if ( event.data.space == 'bottom' )
27
        {
28
          event.data.html +=
29
          '<span id="' + spaceId + '_label" class="cke_voice_label">Counter</span>' +
30
          '<div id="' + spaceId + '" class="cke_counter" role="group" aria-labelledby="' + spaceId + '_label">' + emptyHtml + '</div>';
31
        }
32
      });
33

    
34
      function count( ev )
35
      {
36
        var space = getSpaceElement();
37
        var text = ev.editor.getData();
38
        // decode HTML entities; it also removes HTML tags, but works only if jQuery is available
39
        text = jQuery('<div/>').html(text).text();
40
        // remove all redundant blank symbols
41
        text = text.replace(new RegExp('\\s+', 'g'), ' ');
42
        // remove all blank symbols at the start and at the end
43
        text = text.replace(new RegExp('(^\\s+)|(\\s+$)', 'g'), '');
44
        var symbols = text.length;
45
        var words = text.split(' ').length;
46
        //remove all blank symbols
47
        text = text.replace(new RegExp('\\s+', 'g'), '');
48
        var symbols_wo_blanks = text.length;
49

    
50
        space.setHtml( '<span class="cke_counter" style="float: right">' + symbols + ' / ' + symbols_wo_blanks + ' symbols; ' + words + ' words</span>' );
51
      }
52

    
53
      editor.on( 'dataReady', count );
54
      editor.on( 'blur', count );
55
      editor.on( 'focus', count );
56
    // Almost useless
57
    //editor.on( 'saveSnapshot', count );
58
    // Requires too much resources
59
    //editor.on( 'key', count );
60
    }
61
  });
62
})();