Ticket #30966: 30966.6.patch
File 30966.6.patch, 7.7 KB (added by , 9 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 206 $textarea = $('#content'), 208 207 $document = $(document), 209 208 $editSlugWrap = $('#edit-slug-box'), … … 788 787 }); 789 788 } 790 789 791 // word count792 if ( typeof(wpWordCount) != 'undefined' ) {793 $document.triggerHandler('wpcountwords', [ $textarea.val() ]);794 795 $textarea.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', [ $textarea.val() ]);803 804 last = k;805 return true;806 });807 }808 809 790 wptitlehint = function(id) { 810 791 id = id || 'title'; 811 792 … … 935 916 } 936 917 }); 937 918 }); 919 920 ( function( $, counter ) { 921 $( function() { 922 var $content = $( '#content' ), 923 $count = $( '#wp-word-count' ).find( '.word-count' ), 924 prevCount = 0, 925 contentEditor; 926 927 function update() { 928 var text, count; 929 930 if ( ! contentEditor || contentEditor.isHidden() ) { 931 text = $content.val(); 932 } else { 933 text = contentEditor.getContent( { format: 'raw' } ); 934 } 935 936 count = counter.count( text ); 937 938 if ( count !== prevCount ) { 939 $count.text( count ); 940 } 941 942 prevCount = count; 943 } 944 945 $( document ).on( 'tinymce-editor-init', function( event, editor ) { 946 if ( editor.id !== 'content' ) { 947 return; 948 } 949 950 contentEditor = editor; 951 952 editor.on( 'nodechange keyup', _.debounce( update, 500 ) ); 953 } ); 954 955 $content.on( 'input keyup', _.debounce( update, 500 ) ); 956 957 update(); 958 } ); 959 } )( jQuery, new 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 } 12 } 13 14 WordCounter.prototype.settings = { 15 HTMLRegExp: /<\/?[a-z][^>]*?>/gi, 16 spaceRegExp: / | /gi, 17 removeRegExp: /[0-9.(),;:!?%#$¿'"_+=\\\/-]+/g, 18 wordsRegExp: /\S\s+/g, 19 charactersRegExp: /\S/g, 20 l10n: window.wordCountL10n || {} 21 }; 22 23 WordCounter.prototype.count = function( text, type ) { 24 var count = 0; 25 26 type = type || this.settings.l10n.type || 'words'; 35 27 36 setTimeout( function() { t.block = 0; }, 2000 ); 37 }, 1 ); 28 if ( text ) { 29 text = ' ' + text + ' '; 30 31 text = text.replace( this.settings.HTMLRegExp, ' ' ); 32 text = text.replace( this.settings.spaceRegExp, ' ' ); 33 text = text.replace( this.settings.removeRegExp, '' ); 34 35 text = text.match( this.settings[ type + 'RegExp' ] ); 36 37 if ( text ) { 38 count = text.length; 39 } 38 40 } 41 42 return count; 39 43 }; 40 44 41 $(document).bind( 'wpcountwords', function(e, txt) {42 wpWordCount.wc(txt);43 });44 } (jQuery));45 window.wp = window.wp || {}; 46 window.wp.utils = window.wp.utils || {}; 47 window.wp.utils.WordCounter = WordCounter; 48 } )(); -
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
371 371 372 372 $scripts->add( 'wpdialogs', "/wp-includes/js/wpdialog$suffix.js", array( 'jquery-ui-dialog' ), false, 1 ); 373 373 374 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array( 'jquery'), false, 1 );374 $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 ); 375 375 did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array( 376 376 /* translators: If your word count is based on single characters (East Asian characters), 377 377 enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ 378 'type' => 'characters' == _x( 'words', 'word count: words or characters?' ) ? 'c' : 'w',378 'type' => _x( 'words', 'word count: words or characters?' ) 379 379 ) ); 380 380 381 381 $scripts->add( 'media-upload', "/wp-admin/js/media-upload$suffix.js", array( 'thickbox', 'shortcode' ), false, 1 ); … … 451 451 'tagDelimiter' => _x( ',', 'tag delimiter' ), 452 452 ) ); 453 453 454 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box' ), false, 1 );454 $scripts->add( 'post', "/wp-admin/js/post$suffix.js", array( 'suggest', 'wp-lists', 'postbox', 'tags-box', 'underscore', 'word-count' ), false, 1 ); 455 455 did_action( 'init' ) && $scripts->localize( 'post', 'postL10n', array( 456 456 'ok' => __('OK'), 457 457 'cancel' => __('Cancel'), -
tests/qunit/index.html
32 32 <script src="../../src/wp-includes/js/customize-models.js"></script> 33 33 <script src="../../src/wp-includes/js/shortcode.js"></script> 34 34 <script src="../../src/wp-admin/js/customize-controls.js"></script> 35 <script src="../../src/wp-admin/js/word-count.js"></script> 35 36 36 37 <!-- Unit tests --> 37 38 <script src="wp-admin/js/password-strength-meter.js"></script> … … 40 41 <script src="wp-includes/js/shortcode.js"></script> 41 42 <script src="wp-admin/js/customize-controls.js"></script> 42 43 <script src="wp-admin/js/customize-controls-utils.js"></script> 44 <script src="wp-admin/js/word-count.js"></script> 43 45 44 46 <!-- Customizer templates for sections --> 45 47 <script type="text/html" id="tmpl-customize-section-default">