Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Add word count #300

Merged
merged 3 commits into from
Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/lib/posts/post-edit-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ function setRawContent( content ) {
if ( PostEditStore.isDirty() !== isDirty || PostEditStore.hasContent() !== hasContent ) {
PostEditStore.emit( 'change' );
}
PostEditStore.emit( 'rawContentChange' );
}
}

Expand Down
12 changes: 12 additions & 0 deletions client/lib/text-utils/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NODE_BIN := $(shell npm bin)
MOCHA ?= $(NODE_BIN)/mocha
BASE_DIR := $(NODE_BIN)/../..
NODE_PATH := test:$(BASE_DIR)/client:$(BASE_DIR)/shared
COMPILERS ?= js:babel/register
REPORTER ?= spec
UI ?= bdd

test:
@NODE_ENV=test NODE_PATH=$(NODE_PATH) $(MOCHA) --compilers $(COMPILERS) --reporter $(REPORTER) --ui $(UI)

.PHONY: test
26 changes: 26 additions & 0 deletions client/lib/text-utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function countWords( content ) {
// Adapted from TinyMCE wordcount plugin:
// https://github.com/tinymce/tinymce/blob/4.2.6/js/tinymce/plugins/wordcount/plugin.js

if ( content && typeof content === 'string' ) {
content = content.replace( /\.\.\./g, ' ' ); // convert ellipses to spaces
content = content.replace( /<.[^<>]*?>/g, ' ' ); // remove HTML tags
content = content.replace( /&nbsp;|&#160;/gi, ' ' ); // remove space chars
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if make sense add spaces here especially if they aren't added in the below lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't quite sure what to do with that section. I suppose fa276d0 is fine too. Our coding guidelines seem to encourage things like this, though:

Use spaces liberally throughout your code. “When in doubt, space it out.”

and

Spaces may align code within documentation blocks or within a line


// deal with HTML entities
content = content.replace( /(\w+)(&#?[a-z0-9]+;)+(\w+)/i, '$1$3' ); // strip entities inside words
content = content.replace( /&.+?;/g, ' ' ); // turn all other entities into spaces

// remove numbers and punctuation
content = content.replace( /[0-9.(),;:!?%#$?\x27\x22_+=\\\/\-]*/g, '' );

const words = content.match( /[\w\u2019\x27\-\u00C0-\u1FFF]+/g );
if ( words ) {
return words.length;
}
}

return 0;
}

export default { countWords };
64 changes: 64 additions & 0 deletions client/lib/text-utils/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import textUtils from '../';

// Adapted from TinyMCE word count tests:
// https://github.com/tinymce/tinymce/blob/4.2.6/tests/plugins/wordcount.js

describe( 'textUtils', () => {
describe( 'wordCount', () => {
it( 'should return 0 for blank content', () => {
expect( textUtils.countWords(
''
) ).to.equal( 0 );
} );

it( 'should strip HTML tags and count words for a simple sentence', () => {
expect( textUtils.countWords(
'<p>My sentence is this.</p>'
) ).to.equal( 4 );
} );

it( 'should not count dashes', () => {
expect( textUtils.countWords(
'<p>Something -- ok</p>'
) ).to.equal( 2 );
} );

it( 'should not count asterisks or other non-word characters', () => {
expect( textUtils.countWords(
'<p>* something\n\u00b7 something else</p>'
) ).to.equal( 3 );
} );

it( 'should not count numbers', () => {
expect( textUtils.countWords(
'<p>Something 123 ok</p>'
) ).to.equal( 2 );
} );

it( 'should not count HTML entities', () => {
expect( textUtils.countWords(
'<p>It&rsquo;s my life &ndash; &#8211; &#x2013; don\'t you forget.</p>'
) ).to.equal( 6 );
} );

it( 'should count hyphenated words as one word', () => {
expect( textUtils.countWords(
'<p>Hello some-word here.</p>'
) ).to.equal( 3 );
} );

it( 'should count words between blocks as two words', () => {
expect( textUtils.countWords(
'<p>Hello</p><p>world</p>'
) ).to.equal( 2 );
} );
} );
} );
78 changes: 78 additions & 0 deletions client/post-editor/editor-word-count/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* External dependencies
*/
import React from 'react/addons';

/**
* Internal dependencies
*/
import PostEditStore from 'lib/posts/post-edit-store';
import userModule from 'lib/user';
import Count from 'components/count';
import textUtils from 'lib/text-utils';

/**
* Module variables
*/
const user = userModule();

export default React.createClass( {
displayName: 'EditorWordCount',

mixins: [ React.addons.PureRenderMixin ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this component has no props, I don't think this is needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I suppose it does make the component pure/render once, nm.


getInitialState() {
return {
rawContent: ''
};
},

componentWillMount() {
PostEditStore.on( 'rawContentChange', this.onRawContentChange );
},

componentDidMount() {
this.onRawContentChange();
},

componentWillUnmount() {
PostEditStore.removeListener( 'rawContentChange', this.onRawContentChange );
},

onRawContentChange() {
this.setState( {
rawContent: PostEditStore.getRawContent()
} );
},

render() {
const currentUser = user.get();
const localeSlug = currentUser && currentUser.localeSlug || 'en';

switch ( localeSlug ) {
case 'ja':
case 'th':
case 'zh-cn':
case 'zh-hk':
case 'zh-sg':
case 'zh-tw':
// TODO these are character-based languages - count characters instead
return null;

case 'ko':
// TODO Korean is not supported by our current word count regex
return null;
}

return (
<div className="editor-word-count">
{ this.translate( 'Word Count' ) }
<Count count={ this.getCount() } />
</div>
);
},

getCount() {
return textUtils.countWords( this.state.rawContent );
}
} );
4 changes: 4 additions & 0 deletions client/post-editor/post-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var actions = require( 'lib/posts/actions' ),
SimpleNotice = require( 'notices/simple-notice' ),
protectForm = require( 'lib/mixins/protect-form' ),
TinyMCE = require( 'components/tinymce' ),
EditorWordCount = require( 'post-editor/editor-word-count' ),
SegmentedControl = require( 'components/segmented-control' ),
SegmentedControlItem = require( 'components/segmented-control/item' ),
EditorMobileNavigation = require( 'post-editor/editor-mobile-navigation' ),
Expand Down Expand Up @@ -390,6 +391,9 @@ var PostEditor = React.createClass( {
onTextEditorChange={ this.onEditorContentChange }
onTogglePin={ this.onTogglePin } />
</div>
<div className="post-editor__word-count-wrapper">
<EditorWordCount />
</div>
{ this.iframePreviewEnabled() ?
<EditorPreview
showPreview={ this.state.showPreview }
Expand Down
33 changes: 33 additions & 0 deletions client/post-editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,36 @@
max-height: 400px;
}
}

.post-editor__word-count-wrapper {
position: fixed;
bottom: 0;
left: auto;
right: auto;

box-sizing: border-box;
width: 100%;

font-size: 11px;
line-height: 18px;

@include breakpoint( ">660px" ) {
width: 683px; // TODO ???
}

& .editor-word-count {
float: right;
border-top: 1px solid lighten( $gray, 20% );
border-left: 1px solid lighten( $gray, 20% );
border-radius: 4px 0;
background-color: rgba( $white, 0.92 );
color: $gray;
text-transform: uppercase;
padding: 8px;

& .count {
margin-left: 4px;
color: darken( $gray, 10% );
}
}
}