MediaWiki:Gadget-Prosesize.js: Difference between revisions
Content deleted Content added
fix |
fix |
||
(27 intermediate revisions by 2 users not shown) | |||
Line 1:
/* _____________________________________________________________________________
// rewrite of [[User:Dr_pda/prosesize.js]]▼
* | |
* | === WARNING: GLOBAL GADGET FILE === |
* | Changes to this page affect many users. |
* | Please discuss changes on the talk page or on [[WT:Gadget]] before editing. |
* |_____________________________________________________________________________|
*
*/
/**
* Prosesize
* Documentation at en.wikipedia.org/wiki/Wikipedia:Prosesize
*/
'use strict';
( function () {
function sizeFormatter( size ) {
var nbsp = "\xA0"; // Equivalent to
if ( size > 10240 ) {
return ( Math.round( size / 1024 )
} else {
return ( size +
}
}
Line 13 ⟶ 26:
return $( '<li>' )
.prop( 'id', id )
.append(
$( '<b>' ).text( text ) document.
}
Line 26 ⟶ 41:
// Get size of text in edit box
// eslint-disable-next-line no-jquery/no-global-selector
appendResult( $( '#wpTextbox1' ).
} else if ( mw.config.get( 'wgIsArticle' ) ) {
// Get revision size from API
Line 42 ⟶ 57:
function getFileSize( proseHtmlValue ) {
//
if ( mw.config.get( 'wgAction' ) !== 'submit' ) {
$.get( ___location ).then( function ( result ) {
var fsize = sizeElement( 'total-size', '
proseHtmlValue.before( fsize );
} );
Line 64 ⟶ 79:
// Exclude from length, and don't set background yellow
id.childNodes[ i ].className += ' prosesize-special-template';
} else if (id.childNodes[ i ].tagName !== 'STYLE') {
} else {▼
// Exclude style tags
textLength += getLength( id.childNodes[ i ] );
}
Line 88 ⟶ 104:
function main() {
var prosePromise, proseValue, refValue, refHtmlValue, proseHtmlValue;
// eslint-disable-next-line no-jquery/no-global-selector
var
// eslint-disable-next-line no-jquery/no-global-selector
var prevStats = $( '#document-size-stats' );
Line 104 ⟶ 120:
var header = $( '<span>' )
.prop( 'id', 'document-size-header' )
.html( 'Document statistics
var output = $( '<ul>' )
.prop( 'id', 'document-size-stats' );
Line 110 ⟶ 126:
.prop( 'id', 'document-size' )
.append( header, output );
if (
return;
}
Line 117 ⟶ 133:
prevStats.remove();
prevHeader.remove();
} else {
// Use prosesize API to get a more accurate prose size account
// The calculations below are left in for the highlighting
prosePromise = $.getJSON( 'https://prosesize.toolforge.org/api/' + mw.config.get( 'wgServerName' ) + '/'
+ encodeURIComponent( mw.config.get( 'wgPageName' ) ) + '?revision=' + mw.config.get( 'wgRevisionId' ) );
// Calculate prose size and size of reference markers ([1] etc)
$( this ).addClass( 'prosesize-highlight' );
proseSize += getLength( this );
Line 130 ⟶ 151:
// Calculate size of references (i.e. output of <references/>)
refSize = getLength( this );
refSizeHtml = this.innerHTML.length;
} );
proseSize -= refmarksize;
function show_output() { proseValue = sizeElement( 'prose-size', 'Prose size (text only):', proseSize refValue = sizeElement( 'ref-size', 'References (text only):', refSize + refmarksize );
refHtmlValue = sizeElement( 'ref-size-html', 'References (including all HTML code):', refSizeHtml + refmarkSizeHtml );
proseHtmlValue = sizeElement( 'prose-size-html', 'Prose size (including all HTML code):', proseSizeHtml - refmarkSizeHtml );
output.append( proseHtmlValue, refHtmlValue, proseValue, refValue );
getFileSize( proseHtmlValue );
getRevisionSize( proseValue );
}
// Add the relevant outputs once we have fetched the prose size.
prosePromise.then(
function( data ) {
if ( mw.config.get( 'wgIsArticle' ) ) {
// Tool doesn't work on previews
proseSize = data.prose_size;
wordCount = data.word_count;
}
show_output();
},
// If tool is down fallback to our prose count
show_output
);
}
}
Line 149 ⟶ 188:
!mw.config.get( 'wgCanonicalSpecialPageName' )
) {
$.ready.then( function () {
/**
* Depending on whether in edit mode or preview/view mode,
* show the approppiate response upon clicking the portlet link
*/
var func, $portlet, notEnabled = false;
if (
if ( mw.config.get( 'wgAction' ) === 'edit' || ( mw.config.get( 'wgAction' ) === 'submit' && document.getElementById( 'wikiDiff' ) ) ) {▼
mw.config.get( 'wgAction' ) === 'edit' ||
▲
) {
notEnabled = true;
func = function () {
mw.notify( 'You need to preview the text for the prose size script to work in edit mode.' );
};
$portlet.addClass( 'prosesize-portlet-link-edit-mode' );▼
} else if ( [ 'view', 'submit', 'historysubmit', 'purge' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
func = main;
Line 166 ⟶ 207:
if ( func ) {
$portlet = $( mw.util.addPortletLink( 'p-tb', '#', 'Page size', 't-page-size', 'Calculate page and prose size' ) );
if ( notEnabled ) {
▲ $portlet.addClass( 'prosesize-portlet-link-edit-mode' );
}
$portlet.on( 'click', function ( e ) {
e.preventDefault();
if ( window.ve && ve.init && ve.init.target && ve.init.target.active ) {
▲ func();
mw.notify( 'Prosesize does not work with the Visual Editor.' );
▲ } else {
func();
}
} );
}
|