Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#613 closed defect (worksforme)

Character Counter that shows file size

Reported by: mharrisonline Owned by: gogo
Priority: normal Milestone:
Component: Plugin_Other Version:
Severity: trivial Keywords:
Cc:

Description

Just a thought, but what if the character counter showed how many k the HTML was? This adds the additional option of showing size, it divides and the number of HTML characters by 1024.

// Charcounter for HTMLArea-3.0
// (c) Udo Schmal & L.N.Schaffrath NeueMedien 
// Distributed under the same terms as HTMLArea itself.
// This notice MUST stay intact for use (see license.txt).

function CharCounter(editor) {
    this.editor = editor;
}

HTMLArea.Config.prototype.CharCounter =
{
  'showChar': true, // show the characters count,
  'showWord': true, // show the words count,
  'showHtml': true, // show the exact html count
  'showSize': true, // show the file size in k
  'separator': ' | ' // separator used to join informations
};

CharCounter._pluginInfo = {
    name          : "CharCounter",
    version       : "1.1",
    developer     : "Udo Schmal",
    developer_url : "http://www.schaffrath-neuemedien.de",
    sponsor       : "L.N.Schaffrath NeueMedien",
    sponsor_url   : "http://www.schaffrath-neuemedien.de",
    c_owner       : "Udo Schmal & L.N.Schaffrath NeueMedien",
    license       : "htmlArea"
};

CharCounter.prototype._lc = function(string) {
    return HTMLArea._lc(string, "CharCounter");
};


CharCounter.prototype.onGenerate = function() {
  var self = this;
  if (this.charCount==null) {
    var charCount = document.createElement("span");
    charCount.style.padding = "2px 5px";
    if(HTMLArea.is_ie) {
      charCount.style.styleFloat = "right";
    } else {
      charCount.style.cssFloat = "right";
    }
    var brk = document.createElement('div');
    brk.style.height =
    brk.style.width =
    brk.style.lineHeight =
    brk.style.fontSize = '1px';
    brk.style.clear = 'both';
    if(HTMLArea.is_ie) {
      this.editor._statusBarTree.style.styleFloat = "left";
    } else {
      this.editor._statusBarTree.style.cssFloat = "left";
    }
    this.editor._statusBar.appendChild(charCount);
    this.editor._statusBar.appendChild(brk);
    this.charCount = charCount;
  }
};

CharCounter.prototype.onUpdateToolbar = function() {
    this.updateCharCount();
};

CharCounter.prototype.onMode = function (mode)
{
  //Hide Chars in statusbar when switching into textmode
  switch (mode)
  {
    case "textmode":
      this.charCount.style.display = "none";
      break;
    case "wysiwyg":
      this.charCount.style.display = "";
      break;
    default:
      alert("Mode <" + mode + "> not defined!");
      return false;
  }
};

CharCounter.prototype.updateCharCount = function(ev) {
  var editor = this.editor;
  var cfg = editor.config;
    var contents = editor.getHTML();
	var string = new Array();
	var htmlcount = contents.length;

  if (cfg.CharCounter.showWord || cfg.CharCounter.showChar) {
    contents = contents.replace(/<\/?\s*!--[^-->]*-->/gi, "" );
    contents = contents.replace(/<(.+?)>/g, '');//Don't count HTML tags
    contents = contents.replace(/&nbsp;/gi, ' ');
    contents = contents.replace(/([\n\r\t])/g, ' ');//convert newlines and tabs into space
    contents = contents.replace(/(  +)/g, ' ');//count spaces only once
    contents = contents.replace(/&(.*);/g, ' ');//Count htmlentities as one keystroke
    contents = contents.replace(/^\s*|\s*$/g, '');//trim
  }
  if (cfg.CharCounter.showWord) {
    var words=0;
    for (var x=0;x<contents.length;x++) 
    {
      if (contents.charAt(x) == " " ) {words++;}
    }
    if (words>=1) { words++; }
    string[string.length] = this._lc("Words") + ": " + words;
  }
  if (cfg.CharCounter.showSize) {
     string[string.length] = this._lc("Size")+ ": " + Math.round((htmlcount*100)/1024)/100 + "k";
  }
  if (cfg.CharCounter.showChar) {
    string[string.length] = this._lc("Chars") + ": " + contents.length;
  }
    if (cfg.CharCounter.showHtml) {
    string[string.length] = this._lc("HTML") + ": " + htmlcount;
  }
  this.charCount.innerHTML = string.join(cfg.CharCounter.separator);
  

};

Attachments (1)

char-counter.js (3.8 KB) - added by mharrisonline 12 years ago.
Just for fun, this character counter shows how many K the HTML takes up.

Download all attachments as: .zip

Change History (3)

Changed 12 years ago by mharrisonline

Just for fun, this character counter shows how many K the HTML takes up.

comment:1 Changed 12 years ago by mharrisonline

  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.