From fbc21541962e822f507458577a923465e658811c Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Wed, 28 Jun 2017 16:47:40 -0400 Subject: [PATCH 1/9] WIP commit for wordcount --- editor/sidebar/post-settings/index.js | 2 + editor/sidebar/word-count/index.js | 25 +++ lib/client-assets.php | 4 +- utils/index.js | 3 +- utils/wordcount.js | 219 ++++++++++++++++++++++++++ 5 files changed, 250 insertions(+), 3 deletions(-) create mode 100644 editor/sidebar/word-count/index.js create mode 100644 utils/wordcount.js diff --git a/editor/sidebar/post-settings/index.js b/editor/sidebar/post-settings/index.js index 7d939a44e4217..e749684d507a2 100644 --- a/editor/sidebar/post-settings/index.js +++ b/editor/sidebar/post-settings/index.js @@ -19,6 +19,7 @@ import PostTaxonomies from '../post-taxonomies'; import FeaturedImage from '../featured-image'; import DiscussionPanel from '../discussion-panel'; import LastRevision from '../last-revision'; +import WordCount from '../word-count'; const PostSettings = ( { toggleSidebar } ) => { return ( @@ -42,6 +43,7 @@ const PostSettings = ( { toggleSidebar } ) => { + ); }; diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js new file mode 100644 index 0000000000000..25c22c40182d5 --- /dev/null +++ b/editor/sidebar/word-count/index.js @@ -0,0 +1,25 @@ +/** + * External dependencies + */ +import { connect } from 'react-redux'; + +/** + * WordPress dependencies + */ +import { __ } from 'i18n'; +import { WordCounter } from 'utils'; +import { PanelHeader, FormToggle, withInstanceId } from 'components'; + +function WordCount() { + + console.log( WordCounter.bind(this) ); + return ( + +
Count
+
+ ); +} +export default connect( + undefined, + undefined +)( WordCount ); diff --git a/lib/client-assets.php b/lib/client-assets.php index d13492e66f005..833f61ec1b27b 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -47,7 +47,7 @@ function gutenberg_register_scripts_and_styles() { wp_register_script( 'wp-utils', gutenberg_url( 'utils/build/index.js' ), - array(), + array('word-count'), filemtime( gutenberg_dir_path() . 'utils/build/index.js' ) ); wp_register_script( @@ -387,7 +387,7 @@ function gutenberg_scripts_and_styles( $hook ) { wp_enqueue_script( 'wp-editor', gutenberg_url( 'editor/build/index.js' ), - array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils' ), + array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils', 'word-count' ), filemtime( gutenberg_dir_path() . 'editor/build/index.js' ), true // enqueue in the footer. ); diff --git a/utils/index.js b/utils/index.js index 6400ec8e4489f..14beca5b426be 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,3 +1,4 @@ import * as keycodes from './keycodes'; +import WordCounter from './wordcount'; -export { keycodes }; +export { keycodes, WordCounter }; diff --git a/utils/wordcount.js b/utils/wordcount.js new file mode 100644 index 0000000000000..7800244b2064d --- /dev/null +++ b/utils/wordcount.js @@ -0,0 +1,219 @@ +/** + * Created by ryanwelcher on 2017-06-28. + */ +/** + * Word or character counting functionality. Count words or characters in a provided text string. + * + * @summary Count words or characters in a text. + * + * @namespace wp.utils + * @since 2.6.0 + */ + + + /** + * Word counting utility + * + * @namespace wp.utils.wordcounter + * @memberof wp.utils + * + * @class + * + * @param {Object} settings Optional. Key-value object containing overrides for + * settings. + * @param {RegExp} settings.HTMLRegExp Optional. Regular expression to find HTML elements. + * @param {RegExp} settings.HTMLcommentRegExp Optional. Regular expression to find HTML comments. + * @param {RegExp} settings.spaceRegExp Optional. Regular expression to find irregular space + * characters. + * @param {RegExp} settings.HTMLEntityRegExp Optional. Regular expression to find HTML entities. + * @param {RegExp} settings.connectorRegExp Optional. Regular expression to find connectors that + * split words. + * @param {RegExp} settings.removeRegExp Optional. Regular expression to find remove unwanted + * characters to reduce false-positives. + * @param {RegExp} settings.astralRegExp Optional. Regular expression to find unwanted + * characters when searching for non-words. + * @param {RegExp} settings.wordsRegExp Optional. Regular expression to find words by spaces. + * @param {RegExp} settings.characters_excluding_spacesRegExp Optional. Regular expression to find characters which + * are non-spaces. + * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters + * including spaces. + * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes. + * @param {Object} settings.l10n Optional. Localization object containing specific + * configuration for the current localization. + * @param {String} settings.l10n.type Optional. Method of finding words to count. + * @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed + * from the text. + * + * @return void + */ + function WordCounter( settings ) { + var key, + shortcodes; + + // Apply provided settings to object settings. + if ( settings ) { + for ( key in settings ) { + + // Only apply valid settings. + if ( settings.hasOwnProperty( key ) ) { + this.settings[ key ] = settings[ key ]; + } + } + } + + shortcodes = this.settings.l10n.shortcodes; + + // If there are any localization shortcodes, add this as type in the settings. + if ( shortcodes && shortcodes.length ) { + this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' ); + } + } + + // Default settings. + WordCounter.prototype.settings = { + HTMLRegExp: /<\/?[a-z][^>]*?>/gi, + HTMLcommentRegExp: //g, + spaceRegExp: / | /gi, + HTMLEntityRegExp: /&\S+?;/g, + + // \u2014 = em-dash + connectorRegExp: /--|\u2014/g, + + // Characters to be removed from input text. + removeRegExp: new RegExp( [ + '[', + + // Basic Latin (extract) + '\u0021-\u0040\u005B-\u0060\u007B-\u007E', + + // Latin-1 Supplement (extract) + '\u0080-\u00BF\u00D7\u00F7', + + /* + * The following range consists of: + * General Punctuation + * Superscripts and Subscripts + * Currency Symbols + * Combining Diacritical Marks for Symbols + * Letterlike Symbols + * Number Forms + * Arrows + * Mathematical Operators + * Miscellaneous Technical + * Control Pictures + * Optical Character Recognition + * Enclosed Alphanumerics + * Box Drawing + * Block Elements + * Geometric Shapes + * Miscellaneous Symbols + * Dingbats + * Miscellaneous Mathematical Symbols-A + * Supplemental Arrows-A + * Braille Patterns + * Supplemental Arrows-B + * Miscellaneous Mathematical Symbols-B + * Supplemental Mathematical Operators + * Miscellaneous Symbols and Arrows + */ + '\u2000-\u2BFF', + + // Supplemental Punctuation + '\u2E00-\u2E7F', + ']' + ].join( '' ), 'g' ), + + // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF + astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, + wordsRegExp: /\S\s+/g, + characters_excluding_spacesRegExp: /\S/g, + + /* + * Match anything that is not a formatting character, excluding: + * \f = form feed + * \n = new line + * \r = carriage return + * \t = tab + * \v = vertical tab + * \u00AD = soft hyphen + * \u2028 = line separator + * \u2029 = paragraph separator + */ + characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, + l10n: window.wordCountL10n || {} + }; + + /** + * Counts the number of words (or other specified type) in the specified text. + * + * @summary Count the number of elements in a text. + * + * @since 2.6.0 + * @memberof wp.utils.wordcounter + * + * @param {String} text Text to count elements in. + * @param {String} type Optional. Specify type to use. + * + * @return {Number} The number of items counted. + */ + WordCounter.prototype.count = function( text, type ) { + var count = 0; + + // Use default type if none was provided. + type = type || this.settings.l10n.type; + + // Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'. + if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) { + type = 'words'; + } + + // If we have any text at all. + if ( text ) { + text = text + '\n'; + + // Replace all HTML with a new-line. + text = text.replace( this.settings.HTMLRegExp, '\n' ); + + // Remove all HTML comments. + text = text.replace( this.settings.HTMLcommentRegExp, '' ); + + // If a shortcode regular expression has been provided use it to remove shortcodes. + if ( this.settings.shortcodesRegExp ) { + text = text.replace( this.settings.shortcodesRegExp, '\n' ); + } + + // Normalize non-breaking space to a normal space. + text = text.replace( this.settings.spaceRegExp, ' ' ); + + if ( type === 'words' ) { + + // Remove HTML Entities. + text = text.replace( this.settings.HTMLEntityRegExp, '' ); + + // Convert connectors to spaces to count attached text as words. + text = text.replace( this.settings.connectorRegExp, ' ' ); + + // Remove unwanted characters. + text = text.replace( this.settings.removeRegExp, '' ); + } else { + + // Convert HTML Entities to "a". + text = text.replace( this.settings.HTMLEntityRegExp, 'a' ); + + // Remove surrogate points. + text = text.replace( this.settings.astralRegExp, 'a' ); + } + + // Match with the selected type regular expression to count the items. + text = text.match( this.settings[ type + 'RegExp' ] ); + + // If we have any matches, set the count to the number of items found. + if ( text ) { + count = text.length; + } + } + + return count; + }; + +export const WordCount = WordCounter( this ); From 785f19cee862f47e68c21e7835b8e80a93f5af81 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 08:17:58 -0400 Subject: [PATCH 2/9] Resetting changes that were addressed in master --- editor/sidebar/word-count/index.js | 39 +++-- utils/wordcount.js | 219 ----------------------------- 2 files changed, 28 insertions(+), 230 deletions(-) delete mode 100644 utils/wordcount.js diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js index 25c22c40182d5..f27537dffdf5a 100644 --- a/editor/sidebar/word-count/index.js +++ b/editor/sidebar/word-count/index.js @@ -7,19 +7,36 @@ import { connect } from 'react-redux'; * WordPress dependencies */ import { __ } from 'i18n'; -import { WordCounter } from 'utils'; -import { PanelHeader, FormToggle, withInstanceId } from 'components'; +import { Component } from 'element'; +import { PanelHeader } from 'components'; -function WordCount() { - console.log( WordCounter.bind(this) ); - return ( - -
Count
-
- ); +class WordCount extends Component { + + componentDidMount() { + console.log( window.wp.utils.WordCounter ); + } + + render() { + return ( + +
{ this.props.wordcount }
+
+ ); + } } + export default connect( - undefined, - undefined + ( state ) => { + return { + wordcount: 200, + }; + }, + ( dispatch ) => { + return { + onUpdateExcerpt( excerpt ) { + dispatch( editPost( { excerpt } ) ); + }, + }; + } )( WordCount ); diff --git a/utils/wordcount.js b/utils/wordcount.js deleted file mode 100644 index 7800244b2064d..0000000000000 --- a/utils/wordcount.js +++ /dev/null @@ -1,219 +0,0 @@ -/** - * Created by ryanwelcher on 2017-06-28. - */ -/** - * Word or character counting functionality. Count words or characters in a provided text string. - * - * @summary Count words or characters in a text. - * - * @namespace wp.utils - * @since 2.6.0 - */ - - - /** - * Word counting utility - * - * @namespace wp.utils.wordcounter - * @memberof wp.utils - * - * @class - * - * @param {Object} settings Optional. Key-value object containing overrides for - * settings. - * @param {RegExp} settings.HTMLRegExp Optional. Regular expression to find HTML elements. - * @param {RegExp} settings.HTMLcommentRegExp Optional. Regular expression to find HTML comments. - * @param {RegExp} settings.spaceRegExp Optional. Regular expression to find irregular space - * characters. - * @param {RegExp} settings.HTMLEntityRegExp Optional. Regular expression to find HTML entities. - * @param {RegExp} settings.connectorRegExp Optional. Regular expression to find connectors that - * split words. - * @param {RegExp} settings.removeRegExp Optional. Regular expression to find remove unwanted - * characters to reduce false-positives. - * @param {RegExp} settings.astralRegExp Optional. Regular expression to find unwanted - * characters when searching for non-words. - * @param {RegExp} settings.wordsRegExp Optional. Regular expression to find words by spaces. - * @param {RegExp} settings.characters_excluding_spacesRegExp Optional. Regular expression to find characters which - * are non-spaces. - * @param {RegExp} settings.characters_including_spacesRegExp Optional. Regular expression to find characters - * including spaces. - * @param {RegExp} settings.shortcodesRegExp Optional. Regular expression to find shortcodes. - * @param {Object} settings.l10n Optional. Localization object containing specific - * configuration for the current localization. - * @param {String} settings.l10n.type Optional. Method of finding words to count. - * @param {Array} settings.l10n.shortcodes Optional. Array of shortcodes that should be removed - * from the text. - * - * @return void - */ - function WordCounter( settings ) { - var key, - shortcodes; - - // Apply provided settings to object settings. - if ( settings ) { - for ( key in settings ) { - - // Only apply valid settings. - if ( settings.hasOwnProperty( key ) ) { - this.settings[ key ] = settings[ key ]; - } - } - } - - shortcodes = this.settings.l10n.shortcodes; - - // If there are any localization shortcodes, add this as type in the settings. - if ( shortcodes && shortcodes.length ) { - this.settings.shortcodesRegExp = new RegExp( '\\[\\/?(?:' + shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'g' ); - } - } - - // Default settings. - WordCounter.prototype.settings = { - HTMLRegExp: /<\/?[a-z][^>]*?>/gi, - HTMLcommentRegExp: //g, - spaceRegExp: / | /gi, - HTMLEntityRegExp: /&\S+?;/g, - - // \u2014 = em-dash - connectorRegExp: /--|\u2014/g, - - // Characters to be removed from input text. - removeRegExp: new RegExp( [ - '[', - - // Basic Latin (extract) - '\u0021-\u0040\u005B-\u0060\u007B-\u007E', - - // Latin-1 Supplement (extract) - '\u0080-\u00BF\u00D7\u00F7', - - /* - * The following range consists of: - * General Punctuation - * Superscripts and Subscripts - * Currency Symbols - * Combining Diacritical Marks for Symbols - * Letterlike Symbols - * Number Forms - * Arrows - * Mathematical Operators - * Miscellaneous Technical - * Control Pictures - * Optical Character Recognition - * Enclosed Alphanumerics - * Box Drawing - * Block Elements - * Geometric Shapes - * Miscellaneous Symbols - * Dingbats - * Miscellaneous Mathematical Symbols-A - * Supplemental Arrows-A - * Braille Patterns - * Supplemental Arrows-B - * Miscellaneous Mathematical Symbols-B - * Supplemental Mathematical Operators - * Miscellaneous Symbols and Arrows - */ - '\u2000-\u2BFF', - - // Supplemental Punctuation - '\u2E00-\u2E7F', - ']' - ].join( '' ), 'g' ), - - // Remove UTF-16 surrogate points, see https://en.wikipedia.org/wiki/UTF-16#U.2BD800_to_U.2BDFFF - astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, - wordsRegExp: /\S\s+/g, - characters_excluding_spacesRegExp: /\S/g, - - /* - * Match anything that is not a formatting character, excluding: - * \f = form feed - * \n = new line - * \r = carriage return - * \t = tab - * \v = vertical tab - * \u00AD = soft hyphen - * \u2028 = line separator - * \u2029 = paragraph separator - */ - characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, - l10n: window.wordCountL10n || {} - }; - - /** - * Counts the number of words (or other specified type) in the specified text. - * - * @summary Count the number of elements in a text. - * - * @since 2.6.0 - * @memberof wp.utils.wordcounter - * - * @param {String} text Text to count elements in. - * @param {String} type Optional. Specify type to use. - * - * @return {Number} The number of items counted. - */ - WordCounter.prototype.count = function( text, type ) { - var count = 0; - - // Use default type if none was provided. - type = type || this.settings.l10n.type; - - // Sanitize type to one of three possibilities: 'words', 'characters_excluding_spaces' or 'characters_including_spaces'. - if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) { - type = 'words'; - } - - // If we have any text at all. - if ( text ) { - text = text + '\n'; - - // Replace all HTML with a new-line. - text = text.replace( this.settings.HTMLRegExp, '\n' ); - - // Remove all HTML comments. - text = text.replace( this.settings.HTMLcommentRegExp, '' ); - - // If a shortcode regular expression has been provided use it to remove shortcodes. - if ( this.settings.shortcodesRegExp ) { - text = text.replace( this.settings.shortcodesRegExp, '\n' ); - } - - // Normalize non-breaking space to a normal space. - text = text.replace( this.settings.spaceRegExp, ' ' ); - - if ( type === 'words' ) { - - // Remove HTML Entities. - text = text.replace( this.settings.HTMLEntityRegExp, '' ); - - // Convert connectors to spaces to count attached text as words. - text = text.replace( this.settings.connectorRegExp, ' ' ); - - // Remove unwanted characters. - text = text.replace( this.settings.removeRegExp, '' ); - } else { - - // Convert HTML Entities to "a". - text = text.replace( this.settings.HTMLEntityRegExp, 'a' ); - - // Remove surrogate points. - text = text.replace( this.settings.astralRegExp, 'a' ); - } - - // Match with the selected type regular expression to count the items. - text = text.match( this.settings[ type + 'RegExp' ] ); - - // If we have any matches, set the count to the number of items found. - if ( text ) { - count = text.length; - } - } - - return count; - }; - -export const WordCount = WordCounter( this ); From 907af410572a0f93d821166233dd64fa6edffa80 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 08:20:21 -0400 Subject: [PATCH 3/9] reset file --- lib/client-assets.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/client-assets.php b/lib/client-assets.php index 833f61ec1b27b..a4cf88d15bf84 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -47,7 +47,7 @@ function gutenberg_register_scripts_and_styles() { wp_register_script( 'wp-utils', gutenberg_url( 'utils/build/index.js' ), - array('word-count'), + array(), filemtime( gutenberg_dir_path() . 'utils/build/index.js' ) ); wp_register_script( @@ -387,18 +387,11 @@ function gutenberg_scripts_and_styles( $hook ) { wp_enqueue_script( 'wp-editor', gutenberg_url( 'editor/build/index.js' ), - array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils', 'word-count' ), + array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils' ), filemtime( gutenberg_dir_path() . 'editor/build/index.js' ), true // enqueue in the footer. ); - // Register `wp-utils` as a dependency of `word-count` to ensure that - // `wp-utils` doesn't clobbber `word-count`. See WordPress/gutenberg#1569. - $word_count_script = wp_scripts()->query( 'word-count' ); - array_push( $word_count_script->deps, 'wp-utils' ); - // Now load the `word-count` script from core. - wp_enqueue_script( 'word-count' ); - $post_id = null; if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) { $post_id = (int) $_GET['post_id']; From 86380f9bee5c11d7c2f10f4ce9708ae279ee69e4 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 15:34:06 -0400 Subject: [PATCH 4/9] Getting there --- blocks/api/serializer.js | 1 + editor/sidebar/word-count/index.js | 24 ++++++++---------------- utils/index.js | 3 +-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/blocks/api/serializer.js b/blocks/api/serializer.js index 66b7421979064..7adb7b7e176e0 100644 --- a/blocks/api/serializer.js +++ b/blocks/api/serializer.js @@ -143,5 +143,6 @@ export function serializeBlock( block ) { * @return {String} The post content */ export default function serialize( blocks ) { + console.log( 'Saving', blocks ); return blocks.map( serializeBlock ).join( '\n\n' ); } diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js index f27537dffdf5a..9a5ccad82fe03 100644 --- a/editor/sidebar/word-count/index.js +++ b/editor/sidebar/word-count/index.js @@ -7,20 +7,17 @@ import { connect } from 'react-redux'; * WordPress dependencies */ import { __ } from 'i18n'; -import { Component } from 'element'; import { PanelHeader } from 'components'; - +import { Component } from 'element'; +import { getBlocks, getBlockCount } from '../../selectors'; +import { serialize } from '../../../blocks/api'; class WordCount extends Component { - - componentDidMount() { - console.log( window.wp.utils.WordCounter ); - } - render() { + console.log( wp.blocks.getBlockTypes() ); return ( -
{ this.props.wordcount }
+
{ this.props.blocks.length }
); } @@ -29,14 +26,9 @@ class WordCount extends Component { export default connect( ( state ) => { return { - wordcount: 200, + blocks: getBlocks( state ), + blockCount: getBlockCount( state ), + //content: serialize( getBlocks( state ) ), }; }, - ( dispatch ) => { - return { - onUpdateExcerpt( excerpt ) { - dispatch( editPost( { excerpt } ) ); - }, - }; - } )( WordCount ); diff --git a/utils/index.js b/utils/index.js index 14beca5b426be..6400ec8e4489f 100644 --- a/utils/index.js +++ b/utils/index.js @@ -1,4 +1,3 @@ import * as keycodes from './keycodes'; -import WordCounter from './wordcount'; -export { keycodes, WordCounter }; +export { keycodes }; From becc7dd0be7a32970a78f360ff34540a939147a9 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 15:39:11 -0400 Subject: [PATCH 5/9] Missing commit master --- lib/client-assets.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/client-assets.php b/lib/client-assets.php index a4cf88d15bf84..d13492e66f005 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -392,6 +392,13 @@ function gutenberg_scripts_and_styles( $hook ) { true // enqueue in the footer. ); + // Register `wp-utils` as a dependency of `word-count` to ensure that + // `wp-utils` doesn't clobbber `word-count`. See WordPress/gutenberg#1569. + $word_count_script = wp_scripts()->query( 'word-count' ); + array_push( $word_count_script->deps, 'wp-utils' ); + // Now load the `word-count` script from core. + wp_enqueue_script( 'word-count' ); + $post_id = null; if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) { $post_id = (int) $_GET['post_id']; From c9e9f855f12809d4ba5fbff8ed9c7a368643cb70 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 16:45:17 -0400 Subject: [PATCH 6/9] Introduce WordCount component --- blocks/api/serializer.js | 1 - editor/sidebar/post-settings/index.js | 2 +- editor/sidebar/word-count/index.js | 26 ++++++++++---------------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/blocks/api/serializer.js b/blocks/api/serializer.js index 7adb7b7e176e0..66b7421979064 100644 --- a/blocks/api/serializer.js +++ b/blocks/api/serializer.js @@ -143,6 +143,5 @@ export function serializeBlock( block ) { * @return {String} The post content */ export default function serialize( blocks ) { - console.log( 'Saving', blocks ); return blocks.map( serializeBlock ).join( '\n\n' ); } diff --git a/editor/sidebar/post-settings/index.js b/editor/sidebar/post-settings/index.js index e749684d507a2..cf9ee483a3f7a 100644 --- a/editor/sidebar/post-settings/index.js +++ b/editor/sidebar/post-settings/index.js @@ -38,12 +38,12 @@ const PostSettings = ( { toggleSidebar } ) => { + - ); }; diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js index 9a5ccad82fe03..c3c4f64d62c53 100644 --- a/editor/sidebar/word-count/index.js +++ b/editor/sidebar/word-count/index.js @@ -7,28 +7,22 @@ import { connect } from 'react-redux'; * WordPress dependencies */ import { __ } from 'i18n'; -import { PanelHeader } from 'components'; -import { Component } from 'element'; -import { getBlocks, getBlockCount } from '../../selectors'; -import { serialize } from '../../../blocks/api'; +import PanelBody from 'components/panel/body'; +import { getBlocks } from '../../selectors'; +import { serialize } from 'blocks'; -class WordCount extends Component { - render() { - console.log( wp.blocks.getBlockTypes() ); - return ( - -
{ this.props.blocks.length }
-
- ); - } +function WordCount( { content, stopRender } ) { + let wordcount = ( stopRender ) ? __( 'Calculating...' ) : wp.utils.WordCounter.prototype.count( content ); + return ( + { __( 'Word Count' ) }:{ wordcount } + ); } export default connect( ( state ) => { return { - blocks: getBlocks( state ), - blockCount: getBlockCount( state ), - //content: serialize( getBlocks( state ) ), + content: serialize( getBlocks( state ) ), + stopRender: ( 'undefined' === typeof wp.utils.WordCounter ), }; }, )( WordCount ); From a6222c4b8d008d75f2f516de79b5adebe8343920 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 17:05:45 -0400 Subject: [PATCH 7/9] Changing let to const --- editor/sidebar/word-count/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js index c3c4f64d62c53..f11da6dc1f67c 100644 --- a/editor/sidebar/word-count/index.js +++ b/editor/sidebar/word-count/index.js @@ -12,7 +12,7 @@ import { getBlocks } from '../../selectors'; import { serialize } from 'blocks'; function WordCount( { content, stopRender } ) { - let wordcount = ( stopRender ) ? __( 'Calculating...' ) : wp.utils.WordCounter.prototype.count( content ); + const wordcount = ( stopRender ) ? __( 'Calculating...' ) : wp.utils.WordCounter.prototype.count( content ); return ( { __( 'Word Count' ) }:{ wordcount } ); From b058a49cbbefd2158694f63ca504feeaf2006764 Mon Sep 17 00:00:00 2001 From: Ryan Welcher Date: Fri, 30 Jun 2017 17:09:43 -0400 Subject: [PATCH 8/9] Adding a space before the count --- editor/sidebar/word-count/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js index f11da6dc1f67c..7628bfffb8ea6 100644 --- a/editor/sidebar/word-count/index.js +++ b/editor/sidebar/word-count/index.js @@ -14,7 +14,7 @@ import { serialize } from 'blocks'; function WordCount( { content, stopRender } ) { const wordcount = ( stopRender ) ? __( 'Calculating...' ) : wp.utils.WordCounter.prototype.count( content ); return ( - { __( 'Word Count' ) }:{ wordcount } + { __( 'Word Count' ) }: { wordcount } ); } From 8b1a7bfc5376ae1dbd164d5d87e48b24b3b98f22 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Sat, 1 Jul 2017 11:08:03 +0200 Subject: [PATCH 9/9] Properly load `word-count` dependency --- editor/sidebar/word-count/index.js | 9 ++++----- lib/client-assets.php | 13 ++++++++++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/editor/sidebar/word-count/index.js b/editor/sidebar/word-count/index.js index 7628bfffb8ea6..594ceb61e78d2 100644 --- a/editor/sidebar/word-count/index.js +++ b/editor/sidebar/word-count/index.js @@ -11,10 +11,10 @@ import PanelBody from 'components/panel/body'; import { getBlocks } from '../../selectors'; import { serialize } from 'blocks'; -function WordCount( { content, stopRender } ) { - const wordcount = ( stopRender ) ? __( 'Calculating...' ) : wp.utils.WordCounter.prototype.count( content ); +function WordCount( { content } ) { + const wordCount = wp.utils.WordCounter.prototype.count( content ); return ( - { __( 'Word Count' ) }: { wordcount } + { __( 'Word Count' ) }: { wordCount } ); } @@ -22,7 +22,6 @@ export default connect( ( state ) => { return { content: serialize( getBlocks( state ) ), - stopRender: ( 'undefined' === typeof wp.utils.WordCounter ), }; - }, + } )( WordCount ); diff --git a/lib/client-assets.php b/lib/client-assets.php index d13492e66f005..f8c826b916bb2 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -387,7 +387,16 @@ function gutenberg_scripts_and_styles( $hook ) { wp_enqueue_script( 'wp-editor', gutenberg_url( 'editor/build/index.js' ), - array( 'wp-api', 'wp-date', 'wp-i18n', 'wp-blocks', 'wp-element', 'wp-components', 'wp-utils' ), + array( + 'wp-api', + 'wp-date', + 'wp-i18n', + 'wp-blocks', + 'wp-element', + 'wp-components', + 'wp-utils', + 'word-count', + ), filemtime( gutenberg_dir_path() . 'editor/build/index.js' ), true // enqueue in the footer. ); @@ -396,8 +405,6 @@ function gutenberg_scripts_and_styles( $hook ) { // `wp-utils` doesn't clobbber `word-count`. See WordPress/gutenberg#1569. $word_count_script = wp_scripts()->query( 'word-count' ); array_push( $word_count_script->deps, 'wp-utils' ); - // Now load the `word-count` script from core. - wp_enqueue_script( 'word-count' ); $post_id = null; if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) {