Making WordPress.org

Changeset 10876


Ignore:
Timestamp:
04/07/2021 03:51:17 AM (6 years ago)
Author:
dd32
Message:

Support Forums: Don't wrap pastes in code blocks when the paste cursor previous character is a backtick.

This also doesn't affect pastes when they begin OR end with a backtick, allowing for predefs which end with a code block but do not start with one.

See #2107.

File:
1 edited

Legend:

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

    r10702 r10876  
    2626        $( '#new-post textarea' ).on( 'paste', function( e ) {
    2727                var $this = $(this),
    28                         $val  = $this.val(),
    29                         paste = ( e.originalEvent.clipboardData || window.clipboardData ).getData('text');
     28                        val  = $this.val(),
     29                        paste = ( e.originalEvent.clipboardData || window.clipboardData ).getData('text').trim();
    3030
    3131                // If no pasted text, or no textarea value, skip.
    32                 if ( ! paste.length || ! $val.length ) {
     32                if ( ! paste.length || ! val.length ) {
    3333                        return;
    3434                }
    3535
    3636                if (
    37                         paste.length < 1000 &&        // Super long pastes get code wrapped
    38                         paste.split("\n").length < 10 // in addition to many-lines pastes.
     37                        paste.length < 500 &&        // Super long pastes get code wrapped
     38                        paste.split("\n").length < 5 // in addition to many-lines pastes.
    3939                ) {
     40                        return;
     41                }
     42
     43                // See if the author is pasting into a code block already
     44                if ( '`' === val.substr( $this.prop('selectionStart') - 1 , 1 ) ) {
     45                        return;
     46                }
     47
     48                // If the code being pasted is already wrapped in backticks (well, starts with OR ends with), skip.
     49                if (
     50                        '`' === paste.substr( 0, 1 ) ||
     51                        '`' === paste.substr( -1, 1 )
     52                ) {
    4053                        return;
    4154                }
    4255       
    4356                $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
     57                        val.substr( 0, $this.prop('selectionStart') ) +      // Text before cusor/selection
     58                        "`" + paste + "`" +                                  // The pasted text, wrapping with `
     59                        val.substr( $this.prop('selectionEnd'), val.length ) // Text after cursor position/selection
    4760                );
    4861
Note: See TracChangeset for help on using the changeset viewer.