Ticket #30966: 30966.4.patch
File 30966.4.patch, 9.1 KB (added by , 10 years ago) |
---|
-
src/wp-admin/js/post.js
203 203 jQuery(document).ready( function($) { 204 204 var stamp, visibility, $submitButtons, updateVisibility, updateText, 205 205 sticky = '', 206 last = 0,207 co = $('#content'),208 206 $document = $(document), 209 207 $editSlugWrap = $('#edit-slug-box'), 210 208 postId = $('#post_ID').val() || 0, … … 788 786 }); 789 787 } 790 788 791 // word count792 if ( typeof(wpWordCount) != 'undefined' ) {793 $document.triggerHandler('wpcountwords', [ co.val() ]);794 795 co.keyup( function(e) {796 var k = e.keyCode || e.charCode;797 798 if ( k == last )799 return true;800 801 if ( 13 == k || 8 == last || 46 == last )802 $document.triggerHandler('wpcountwords', [ co.val() ]);803 804 last = k;805 return true;806 });807 }808 809 789 wptitlehint = function(id) { 810 790 id = id || 'title'; 811 791 … … 924 904 }); 925 905 } 926 906 }); 907 908 ( function( $, wordCounter ) { 909 $( function() { 910 var counter = new wordCounter(), 911 $content = $( '#content' ), 912 $count = $( '#wp-word-count' ).find( '.word-count' ), 913 prevCount = 0, 914 contentEditor; 915 916 function update() { 917 var text, count; 918 919 if ( ! contentEditor || contentEditor.isHidden() ) { 920 text = $content.val(); 921 } else { 922 text = contentEditor.getContent( { format: 'raw' } ); 923 } 924 925 count = counter.count( text ); 926 927 if ( count !== prevCount ) { 928 $count.text( count ); 929 } 930 931 prevCount = count; 932 } 933 934 $( document ).on( 'tinymce-editor-init', function( event, editor ) { 935 if ( editor.id !== 'content' ) { 936 return; 937 } 938 939 contentEditor = editor; 940 941 editor.on( 'nodechange keyup', _.debounce( update, 500 ) ); 942 } ); 943 944 $content.on( 'input keyup', _.debounce( update, 500 ) ); 945 946 update(); 947 } ); 948 } )( jQuery, wp.utils.wordCounter ); -
src/wp-admin/js/word-count.js
1 /* global wordCountL10n */ 2 var wpWordCount; 3 (function($,undefined) { 4 wpWordCount = { 5 6 settings : { 7 strip : /<[a-zA-Z\/][^<>]*>/g, // strip HTML tags 8 clean : /[0-9.(),;:!?%#$¿'"_+=\\/-]+/g, // regexp to remove punctuation, etc. 9 w : /\S\s+/g, // word-counting regexp 10 c : /\S/g // char-counting regexp for asian languages 11 }, 12 13 block : 0, 14 15 wc : function(tx, type) { 16 var t = this, w = $('.word-count'), tc = 0; 17 18 if ( type === undefined ) 19 type = wordCountL10n.type; 20 if ( type !== 'w' && type !== 'c' ) 21 type = 'w'; 22 23 if ( t.block ) 24 return; 25 26 t.block = 1; 27 28 setTimeout( function() { 29 if ( tx ) { 30 tx = tx.replace( t.settings.strip, ' ' ).replace( / | /gi, ' ' ); 31 tx = tx.replace( t.settings.clean, '' ); 32 tx.replace( t.settings[type], function(){tc++;} ); 1 ( function() { 2 function wordCounter( settings ) { 3 var key; 4 5 if ( settings ) { 6 for ( key in settings ) { 7 if( settings.hasOwnProperty( key ) ) { 8 this.settings[ key ] = settings[ key ]; 33 9 } 34 w.html(tc.toString()); 10 } 11 } 35 12 36 setTimeout( function() { t.block = 0; }, 2000 );37 }, 1);13 if ( this.settings.l10n.shortcodes ) { 14 this.settings.shortcodeRegExp = new RegExp( '\\[\\/?(?:' + this.settings.l10n.shortcodes.join( '|' ) + ')[^\\]]*?\\]', 'gi' ); 38 15 } 16 17 this.settings.excludeHTMLRegExp = new RegExp( '<(' + this.settings.excludeHTML.join( '|' ) + ')[^>]*?>[\\s\\S]*?<\\/\\1>', 'gi' ); 18 this.settings.contractRegExp = new RegExp( '[' + this.settings.contract + ']', 'g' ); 19 this.settings.expandRegExp = new RegExp( '[' + this.settings.expand + ']', 'g' ); 20 } 21 22 wordCounter.prototype.settings = { 23 excludeHTML: [ 'code', 'form', 'noscript', 'script' ], 24 HTMLRegExp: /<[a-z\/][^>]*?>/gi, 25 spaceRegExp: / | /gi, 26 HTMLEntitiesRegExp: /&#?[a-z0-9]+?;/gi, 27 wordsRegExp: /\S\s+/g, 28 charactersRegExp: /\S/g, 29 // Just the apostrophe and hyphen 30 contract: '\'\u2019\\-\u2010\u2011', 31 contractMore: '', 32 expand: [ 33 // Extract form "Basic Latin" 34 '\u0021-\u0040\u005B-\u0060\u007B-\u007E', 35 // Extract from "Latin-1 Supplement" 36 '\u00A1-\u00BF\u00D7\u00F7', 37 // "Combining Diacritical Marks" 38 '\u0300-\u036F', 39 // Punctuation, symbols, operators... 40 '\u2000-\u2BFF', 41 // "Supplemental Punctuation" 42 '\u2E00-\u2E7F' 43 ].join( '' ), 44 expandMore: '', 45 l10n: window.wordCountL10n || {} 46 }; 47 48 wordCounter.prototype.count = function( text, type ) { 49 var count = 0; 50 51 type = type || this.settings.l10n.type || 'words'; 52 53 if ( text ) { 54 text = ' ' + text + ' '; 55 56 text = text.replace( this.settings.excludeHTMLRegExp, ' ' ); 57 text = text.replace( this.settings.HTMLRegExp, ' ' ); 58 text = text.replace( this.settings.shortcodeRegExp, ' ' ); 59 text = text.replace( this.settings.spaceRegExp, ' ' ); 60 text = text.replace( this.settings.HTMLEntitiesRegExp, '' ); 61 // en/em dash shorthand is an exception 62 text = text.replace( /--/g, ' ' ); 63 text = text.replace( this.settings.contractRegExp, '' ); 64 text = text.replace( this.settings.expandRegExp, ' ' ); 65 66 text = text.match( this.settings[ type + 'RegExp' ] ); 67 68 if ( text ) { 69 count = text.length; 70 } 71 } 72 73 return count; 39 74 }; 40 75 41 $(document).bind( 'wpcountwords', function(e, txt) {42 wpWordCount.wc(txt);43 });44 } (jQuery));76 window.wp = window.wp || {}; 77 window.wp.utils = window.wp.utils || {}; 78 window.wp.utils.wordCounter = wordCounter; 79 } )(); -
src/wp-includes/js/tinymce/plugins/wordpress/plugin.js
7 7 var DOM = tinymce.DOM, 8 8 each = tinymce.each, 9 9 __ = editor.editorManager.i18n.translate, 10 wpAdvButton, style, 11 last = 0; 10 wpAdvButton, style; 12 11 13 12 if ( typeof window.jQuery !== 'undefined' ) { 14 13 window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); … … 363 362 } 364 363 }); 365 364 366 // Word count367 if ( typeof window.jQuery !== 'undefined' ) {368 editor.on( 'keyup', function( e ) {369 var key = e.keyCode || e.charCode;370 371 if ( key === last ) {372 return;373 }374 375 if ( 13 === key || 8 === last || 46 === last ) {376 window.jQuery( document ).triggerHandler( 'wpcountwords', [ editor.getContent({ format : 'raw' }) ] );377 }378 379 last = key;380 });381 }382 383 365 editor.on( 'SaveContent', function( e ) { 384 366 // If editor is hidden, we just want the textarea's value to be saved 385 367 if ( ! editor.inline && editor.isHidden() ) { -
src/wp-includes/script-loader.php
372 372 373 373 $scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 ); 374 374 375 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array( 'jquery'), false, 1 );375 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 ); 376 376 did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array( 377 377 /* translators: If your word count is based on single characters (East Asian characters), 378 378 enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ 379 'type' => 'characters' == _x( 'words', 'word count: words or characters?' ) ? 'c' : 'w', 379 'type' => _x( 'words', 'word count: words or characters?' ), 380 'shortcodes' => array_keys( $GLOBALS['shortcode_tags'] ) 380 381 ) ); 381 382 382 383 $scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 ); … … 449 450 'tagDelimiter' => _x( ',', 'tag delimiter' ), 450 451 ) ); 451 452 452 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box' ), false, 1 );453 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count' ), false, 1 ); 453 454 did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array( 454 455 'ok' => __('OK'), 455 456 'cancel' => __('Cancel'), -
tests/qunit/index.html
31 31 <script src="../../src/wp-includes/js/customize-models.js"></script> 32 32 <script src="../../src/wp-includes/js/shortcode.js"></script> 33 33 <script src="../../src/wp-admin/js/customize-controls.js"></script> 34 <script src="../../src/wp-admin/js/word-count.js"></script> 34 35 35 36 <!-- Unit tests --> 36 37 <script src="wp-admin/js/password-strength-meter.js"></script> … … 39 40 <script src="wp-includes/js/shortcode.js"></script> 40 41 <script src="wp-admin/js/customize-controls.js"></script> 41 42 <script src="wp-admin/js/customize-controls-utils.js"></script> 43 <script src="wp-admin/js/word-count.js"></script> 42 44 </body> 43 45 </html>