mirror of
https://github.com/WordPress/WordPress.git
synced 2025-02-22 23:41:38 +01:00
Editor: restructure word count
* The WordCounter should only do one thing: count words. This makes it also easier to test. * Add some really basic unit tests. * Instead of only refreshing the count on enter and delete, refresh the count when the user stops typing. Also look at paste and content changes in TinyMCE. * Use `match` instead of `replace` when it is appropriate. * More readable code. See #30966. Fixes #26620. Built from https://develop.svn.wordpress.org/trunk@32856 git-svn-id: http://core.svn.wordpress.org/trunk@32827 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
50e32a1969
commit
fd95002b2a
@ -203,7 +203,6 @@ $(document).on( 'heartbeat-send.refresh-lock', function( e, data ) {
|
|||||||
jQuery(document).ready( function($) {
|
jQuery(document).ready( function($) {
|
||||||
var stamp, visibility, $submitButtons, updateVisibility, updateText,
|
var stamp, visibility, $submitButtons, updateVisibility, updateText,
|
||||||
sticky = '',
|
sticky = '',
|
||||||
last = 0,
|
|
||||||
$textarea = $('#content'),
|
$textarea = $('#content'),
|
||||||
$document = $(document),
|
$document = $(document),
|
||||||
$editSlugWrap = $('#edit-slug-box'),
|
$editSlugWrap = $('#edit-slug-box'),
|
||||||
@ -788,24 +787,6 @@ jQuery(document).ready( function($) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// word count
|
|
||||||
if ( typeof(wpWordCount) != 'undefined' ) {
|
|
||||||
$document.triggerHandler('wpcountwords', [ $textarea.val() ]);
|
|
||||||
|
|
||||||
$textarea.keyup( function(e) {
|
|
||||||
var k = e.keyCode || e.charCode;
|
|
||||||
|
|
||||||
if ( k == last )
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if ( 13 == k || 8 == last || 46 == last )
|
|
||||||
$document.triggerHandler('wpcountwords', [ $textarea.val() ]);
|
|
||||||
|
|
||||||
last = k;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
wptitlehint = function(id) {
|
wptitlehint = function(id) {
|
||||||
id = id || 'title';
|
id = id || 'title';
|
||||||
|
|
||||||
@ -935,3 +916,44 @@ jQuery(document).ready( function($) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
( function( $, counter ) {
|
||||||
|
$( function() {
|
||||||
|
var $content = $( '#content' ),
|
||||||
|
$count = $( '#wp-word-count' ).find( '.word-count' ),
|
||||||
|
prevCount = 0,
|
||||||
|
contentEditor;
|
||||||
|
|
||||||
|
function update() {
|
||||||
|
var text, count;
|
||||||
|
|
||||||
|
if ( ! contentEditor || contentEditor.isHidden() ) {
|
||||||
|
text = $content.val();
|
||||||
|
} else {
|
||||||
|
text = contentEditor.getContent( { format: 'raw' } );
|
||||||
|
}
|
||||||
|
|
||||||
|
count = counter.count( text );
|
||||||
|
|
||||||
|
if ( count !== prevCount ) {
|
||||||
|
$count.text( count );
|
||||||
|
}
|
||||||
|
|
||||||
|
prevCount = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
$( document ).on( 'tinymce-editor-init', function( event, editor ) {
|
||||||
|
if ( editor.id !== 'content' ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
contentEditor = editor;
|
||||||
|
|
||||||
|
editor.on( 'nodechange keyup', _.debounce( update, 2000 ) );
|
||||||
|
} );
|
||||||
|
|
||||||
|
$content.on( 'input keyup', _.debounce( update, 2000 ) );
|
||||||
|
|
||||||
|
update();
|
||||||
|
} );
|
||||||
|
} )( jQuery, new wp.utils.WordCounter() );
|
||||||
|
2
wp-admin/js/post.min.js
vendored
2
wp-admin/js/post.min.js
vendored
File diff suppressed because one or more lines are too long
@ -1,44 +1,48 @@
|
|||||||
/* global wordCountL10n */
|
( function() {
|
||||||
var wpWordCount;
|
function WordCounter( settings ) {
|
||||||
(function($,undefined) {
|
var key;
|
||||||
wpWordCount = {
|
|
||||||
|
|
||||||
settings : {
|
if ( settings ) {
|
||||||
strip : /<[a-zA-Z\/][^<>]*>/g, // strip HTML tags
|
for ( key in settings ) {
|
||||||
clean : /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g, // regexp to remove punctuation, etc.
|
if ( settings.hasOwnProperty( key ) ) {
|
||||||
w : /\S\s+/g, // word-counting regexp
|
this.settings[ key ] = settings[ key ];
|
||||||
c : /\S/g // char-counting regexp for asian languages
|
|
||||||
},
|
|
||||||
|
|
||||||
block : 0,
|
|
||||||
|
|
||||||
wc : function(tx, type) {
|
|
||||||
var t = this, w = $('.word-count'), tc = 0;
|
|
||||||
|
|
||||||
if ( type === undefined )
|
|
||||||
type = wordCountL10n.type;
|
|
||||||
if ( type !== 'w' && type !== 'c' )
|
|
||||||
type = 'w';
|
|
||||||
|
|
||||||
if ( t.block )
|
|
||||||
return;
|
|
||||||
|
|
||||||
t.block = 1;
|
|
||||||
|
|
||||||
setTimeout( function() {
|
|
||||||
if ( tx ) {
|
|
||||||
tx = tx.replace( t.settings.strip, ' ' ).replace( / | /gi, ' ' );
|
|
||||||
tx = tx.replace( t.settings.clean, '' );
|
|
||||||
tx.replace( t.settings[type], function(){tc++;} );
|
|
||||||
}
|
}
|
||||||
w.html(tc.toString());
|
}
|
||||||
|
|
||||||
setTimeout( function() { t.block = 0; }, 2000 );
|
|
||||||
}, 1 );
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
WordCounter.prototype.settings = {
|
||||||
|
HTMLRegExp: /<\/?[a-z][^>]*?>/gi,
|
||||||
|
spaceRegExp: / | /gi,
|
||||||
|
removeRegExp: /[0-9.(),;:!?%#$¿'"_+=\\\/-]+/g,
|
||||||
|
wordsRegExp: /\S\s+/g,
|
||||||
|
charactersRegExp: /\S/g,
|
||||||
|
l10n: window.wordCountL10n || {}
|
||||||
};
|
};
|
||||||
|
|
||||||
$(document).bind( 'wpcountwords', function(e, txt) {
|
WordCounter.prototype.count = function( text, type ) {
|
||||||
wpWordCount.wc(txt);
|
var count = 0;
|
||||||
});
|
|
||||||
}(jQuery));
|
type = type || this.settings.l10n.type || 'words';
|
||||||
|
|
||||||
|
if ( text ) {
|
||||||
|
text = ' ' + text + ' ';
|
||||||
|
|
||||||
|
text = text.replace( this.settings.HTMLRegExp, ' ' );
|
||||||
|
text = text.replace( this.settings.spaceRegExp, ' ' );
|
||||||
|
text = text.replace( this.settings.removeRegExp, '' );
|
||||||
|
|
||||||
|
text = text.match( this.settings[ type + 'RegExp' ] );
|
||||||
|
|
||||||
|
if ( text ) {
|
||||||
|
count = text.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
};
|
||||||
|
|
||||||
|
window.wp = window.wp || {};
|
||||||
|
window.wp.utils = window.wp.utils || {};
|
||||||
|
window.wp.utils.WordCounter = WordCounter;
|
||||||
|
} )();
|
||||||
|
2
wp-admin/js/word-count.min.js
vendored
2
wp-admin/js/word-count.min.js
vendored
@ -1 +1 @@
|
|||||||
var wpWordCount;!function(a,b){wpWordCount={settings:{strip:/<[a-zA-Z\/][^<>]*>/g,clean:/[0-9.(),;:!?%#$¿'"_+=\\/-]+/g,w:/\S\s+/g,c:/\S/g},block:0,wc:function(c,d){var e=this,f=a(".word-count"),g=0;d===b&&(d=wordCountL10n.type),"w"!==d&&"c"!==d&&(d="w"),e.block||(e.block=1,setTimeout(function(){c&&(c=c.replace(e.settings.strip," ").replace(/ | /gi," "),c=c.replace(e.settings.clean,""),c.replace(e.settings[d],function(){g++})),f.html(g.toString()),setTimeout(function(){e.block=0},2e3)},1))}},a(document).bind("wpcountwords",function(a,b){wpWordCount.wc(b)})}(jQuery);
|
!function(){function a(a){var b;if(a)for(b in a)a.hasOwnProperty(b)&&(this.settings[b]=a[b])}a.prototype.settings={HTMLRegExp:/<\/?[a-z][^>]*?>/gi,spaceRegExp:/ | /gi,removeRegExp:/[0-9.(),;:!?%#$¿'"_+=\\\/-]+/g,wordsRegExp:/\S\s+/g,charactersRegExp:/\S/g,l10n:window.wordCountL10n||{}},a.prototype.count=function(a,b){var c=0;return b=b||this.settings.l10n.type||"words",a&&(a=" "+a+" ",a=a.replace(this.settings.HTMLRegExp," "),a=a.replace(this.settings.spaceRegExp," "),a=a.replace(this.settings.removeRegExp,""),a=a.match(this.settings[b+"RegExp"]),a&&(c=a.length)),c},window.wp=window.wp||{},window.wp.utils=window.wp.utils||{},window.wp.utils.WordCounter=a}();
|
@ -7,8 +7,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
|
|||||||
var DOM = tinymce.DOM,
|
var DOM = tinymce.DOM,
|
||||||
each = tinymce.each,
|
each = tinymce.each,
|
||||||
__ = editor.editorManager.i18n.translate,
|
__ = editor.editorManager.i18n.translate,
|
||||||
wpAdvButton, style,
|
wpAdvButton, style;
|
||||||
last = 0;
|
|
||||||
|
|
||||||
if ( typeof window.jQuery !== 'undefined' ) {
|
if ( typeof window.jQuery !== 'undefined' ) {
|
||||||
window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
|
window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
|
||||||
@ -363,23 +362,6 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Word count
|
|
||||||
if ( typeof window.jQuery !== 'undefined' ) {
|
|
||||||
editor.on( 'keyup', function( e ) {
|
|
||||||
var key = e.keyCode || e.charCode;
|
|
||||||
|
|
||||||
if ( key === last ) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( 13 === key || 8 === last || 46 === last ) {
|
|
||||||
window.jQuery( document ).triggerHandler( 'wpcountwords', [ editor.getContent({ format : 'raw' }) ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
last = key;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
editor.on( 'SaveContent', function( e ) {
|
editor.on( 'SaveContent', function( e ) {
|
||||||
// If editor is hidden, we just want the textarea's value to be saved
|
// If editor is hidden, we just want the textarea's value to be saved
|
||||||
if ( ! editor.inline && editor.isHidden() ) {
|
if ( ! editor.inline && editor.isHidden() ) {
|
||||||
|
File diff suppressed because one or more lines are too long
Binary file not shown.
@ -371,11 +371,11 @@ function wp_default_scripts( &$scripts ) {
|
|||||||
|
|
||||||
$scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 );
|
$scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 );
|
||||||
|
|
||||||
$scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array( 'jquery' ), false, 1 );
|
$scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 );
|
||||||
did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array(
|
did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array(
|
||||||
/* translators: If your word count is based on single characters (East Asian characters),
|
/* translators: If your word count is based on single characters (East Asian characters),
|
||||||
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
|
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
|
||||||
'type' => 'characters' == _x( 'words', 'word count: words or characters?' ) ? 'c' : 'w',
|
'type' => _x( 'words', 'word count: words or characters?' )
|
||||||
) );
|
) );
|
||||||
|
|
||||||
$scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 );
|
$scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 );
|
||||||
@ -451,7 +451,7 @@ function wp_default_scripts( &$scripts ) {
|
|||||||
'tagDelimiter' => _x( ',', 'tag delimiter' ),
|
'tagDelimiter' => _x( ',', 'tag delimiter' ),
|
||||||
) );
|
) );
|
||||||
|
|
||||||
$scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box' ), false, 1 );
|
$scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count' ), false, 1 );
|
||||||
did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array(
|
did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array(
|
||||||
'ok' => __('OK'),
|
'ok' => __('OK'),
|
||||||
'cancel' => __('Cancel'),
|
'cancel' => __('Cancel'),
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '4.3-alpha-32855';
|
$wp_version = '4.3-alpha-32856';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user