Making WordPress.org

Changeset 10702


Ignore:
Timestamp:
02/18/2021 04:18:58 AM (3 years ago)
Author:
dd32
Message:

Support: Try wrapping long pastes in core blocks.

See #2107.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-support/js/forums.js

    r10669 r10702  
    2222        } );
    2323    }
     24
     25    // Wrap long pastes in code tags.
     26    $( '#new-post textarea' ).on( 'paste', function( e ) {
     27        var $this = $(this),
     28            $val  = $this.val(),
     29            paste = ( e.originalEvent.clipboardData || window.clipboardData ).getData('text');
     30
     31        // If no pasted text, or no textarea value, skip.
     32        if ( ! paste.length || ! $val.length ) {
     33            return;
     34        }
     35
     36        if (
     37            paste.length < 1000 &&        // Super long pastes get code wrapped
     38            paste.split("\n").length < 10 // in addition to many-lines pastes.
     39        ) {
     40            return;
     41        }
     42   
     43        $this.val(
     44            $val.substring( 0, $this.prop('selectionStart') ) +       // Text before cusor/selection
     45            "`" + paste.trim().replace(/^`|`$/g, '') + "`" +          // The pasted text, trimming ` off it and wrapping with `
     46            $val.substring( $this.prop('selectionEnd'), $val.length ) // Text after cursor position/selection
     47        );
     48
     49        e.preventDefault();
     50    } );
    2451
    2552    if ( $( 'body' ).is( '.bbp-view' ) ) {
Note: See TracChangeset for help on using the changeset viewer.