Make WordPress Core

Opened 15 years ago

Closed 14 years ago

#12921 closed enhancement (wontfix)

Improve word count

Reported by: shdus's profile shdus Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.0
Component: Editor Keywords: has-patch
Focuses: Cc:

Description

The word count that displays below the textbox when you are writing a post does not change until you hit the enter key (usually). I think I have seem a few instances where it changed without hitting enter, but couldn't find any pattern. I don't know if this matters, but I am using Firefox 3.6.3.

Attachments (2)

12921.1.patch (3.7 KB) - added by koopersmith 14 years ago.
12921.2.patch (24.0 KB) - added by koopersmith 14 years ago.

Download all attachments as: .zip

Change History (12)

#1 follow-up: @koopersmith
14 years ago

Currently the word count updates on enter and the first key pressed after backspace or delete (that isn't backspace or delete). My guess is that this is for performance reasons. If not, it could be easily changed.

#2 in reply to: ↑ 1 @azaozz
14 years ago

  • Resolution set to wontfix
  • Status changed from new to closed

Replying to koopersmith:
You're right, it is for performance reasons and is the intended behaviour. Word count on large posts can be quite slow and interferes with the typing on slower computers if run constantly.

#3 @koopersmith
14 years ago

  • Cc koopersmith added
  • Keywords has-patch added
  • Resolution wontfix deleted
  • Status changed from closed to reopened
  • Summary changed from Word count does not change until you hit enter to Improve word count
  • Type changed from defect (bug) to enhancement

Actually, I've been playing around with the code and wrote a patch that keeps performance about the same, but improves accuracy.

  1. When the wordcount function is blocked, it will estimate the wordcount by adding one to the current wordcount when the spacebar is pressed.
  1. It updates on paste in tinymce and html mode, and a user selection in html mode (tinymce doesn't have an onSelect event for some reason).
  1. It centralizes the functionality between tinymce and html mode by passing the functions into the wcWordCount object.
  1. It times the wc function, and if the execution time is high enough it will increase the timeout threshold accordingly. Currently it's 100ms * execution time, with the default timeout set to 2000ms. This might require some tweaking.

I thought about adding a few more features (like detecting when the user inserts a tag in html mode, and not incrementing), but that requires adding a keypress event. I think this is the best balance of performance and functionality.

#4 @koopersmith
14 years ago

For those having trouble testing:

SCRIPT_DEBUG must be set to true, and for some reason wp-includes/js/tinymce/plugins/wordpress/editor_plugin.js is loaded instead of editor_plugin.dev.js

12921.2.patch includes the compressed versions of editor_plugin.js and word-count.js so you only have to set SCRIPT_DEBUG to make it work.

#5 @azaozz
14 years ago

Running ed.getContent() on each keyup is undesirable (line 165 in editor_plugin.dev.js in the patch). This triggers several large functions in TinyMCE and would interfere with typing in slower browsers. Would suggest testing in IE 6/7 with a 2,000 - 3,000 words post with many HTML tags.

#6 follow-up: @koopersmith
14 years ago

It's not—it's creating (not executing) an anonymous function that is only run in the wc function (word-count.dev.js line 54 in the patch). Haven't tested in IE yet, but I was testing on 80,000 word posts without any issues.

#7 in reply to: ↑ 6 @azaozz
14 years ago

Replying to koopersmith:

it's creating (not executing) an anonymous function...

It's creating a new function on every keyup? Typing a 2,000 words post X average of 5 keystrokes per word = 10,000 new lambda functions, starting to look like memory leak :)

A lot of testing went into making this plugin. It's a "nice to have" (secondary) feature so it shouldn't interfere with the main feature (typing) at all or it becomes pointless.

At first it used to update the word count every 10 sec but if the user was typing fast in a slower browser or on slower computer, sometimes there was a very annoying lag/skipping when it was running. The best time to update the count is when the user pauses typing for a moment. Nearly all users pause when starting a new paragraph or use the backspace key, so running the count then makes any lag virtually unnoticeable.

#8 @jane
14 years ago

  • Milestone changed from Unassigned to 3.1

Setting to 3.1 to make it clear that this came in too late to be considered for 3.0.

#9 @koopersmith
14 years ago

You are a wise man, Andrew Ozz. :)

Given your knowledge of the work and testing that's gone into the current version of word count, I'd love to chat with you about it sometime. It's clearly something that should be changed with great care (or just left the same!).

#10 @nacin
14 years ago

  • Milestone Awaiting Triage deleted
  • Resolution set to wontfix
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.