Making WordPress.org


Ignore:
Timestamp:
08/09/2016 06:06:41 PM (8 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Add a comment preview to the front-end form for user contributed notes.

  • Preview is fetched via AJAX.
  • Preview updates 1.5 seconds after user stops typing.
  • Adds editor toolbar button "preview note".
  • Moves filtering of 'syntaxhighlighter_htmlresult' outside of just handbooks since it can also be applied elsewhere.

Props keesiemeijer.
Fixes #1865.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php

    r3754 r3788  
    4545 */
    4646require __DIR__ . '/inc/user-content.php';
     47
     48/**
     49 * User-submitted content preview.
     50 */
     51require __DIR__ . '/inc/user-content-preview.php';
    4752
    4853/**
     
    112117
    113118    add_filter( 'document_title_separator', __NAMESPACE__ . '\\theme_title_separator', 10, 2 );
     119
     120    add_filter( 'syntaxhighlighter_htmlresult', __NAMESPACE__ . '\\syntaxhighlighter_htmlresult' );
    114121}
    115122
     
    277284    }
    278285}
     286
     287/**
     288 * If a syntax highlighted code block exceeds a given number of lines, wrap the
     289 * markup with other markup to trigger the code expansion/collapse JS handling
     290 * already implemented for the code reference.
     291 *
     292 * @param string  $text The pending result of the syntax highlighting.
     293 * @return string
     294 */
     295function syntaxhighlighter_htmlresult( $text ) {
     296
     297    // is_admin() is true in front end AJAX requests.
     298    if ( is_admin() && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
     299        return $text;
     300    }
     301
     302    $new_text      = '';
     303    // Collapse is handled for >10 lines. But just go ahead and show the full
     304    // code if that is just barely being exceeded (no one wants to expand to
     305    // see one or two more lines).
     306    $lines_to_show = 12;
     307    $do_collapse   = ( substr_count( $text, "\n" ) - 1 ) > $lines_to_show;
     308
     309    if ( $do_collapse )  {
     310        $new_text .= '<section class="source-content">';
     311        $new_text .= '<div class="source-code-container">';
     312    }
     313
     314    $new_text .= $text;
     315
     316    if ( $do_collapse ) {
     317        $new_text .= '</div>';
     318        $new_text .= '<p class="source-code-links"><span>';
     319        $new_text .= '<a href="#" class="show-complete-source">' . __( 'Expand full source code', 'wporg' ) . '</a>';
     320        $new_text .= '<a href="#" class="less-complete-source">' . __( 'Collapse full source code', 'wporg' ) . '</a>';
     321        $new_text .= '</span></p>';
     322        $new_text .= '</section>';
     323    }
     324
     325    return $new_text;
     326}
Note: See TracChangeset for help on using the changeset viewer.