Skip to content

Commit

Permalink
Add support to quickcode in the block editor and restrict to v3
Browse files Browse the repository at this point in the history
  • Loading branch information
renatho committed Apr 4, 2020
1 parent 126ae96 commit 53143c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ registerBlockType( 'syntaxhighlighter/code', {
type: 'boolean',
default: syntaxHighlighterData.settings.makeURLsClickable.default,
},

quickCode: {
type: 'boolean',
default: syntaxHighlighterData.settings.quickCode.default,
},
},

supports: {
Expand Down Expand Up @@ -128,7 +133,8 @@ registerBlockType( 'syntaxhighlighter/code', {
firstLineNumber,
highlightLines,
wrapLines,
makeURLsClickable
makeURLsClickable,
quickCode
} = attributes;

let blockSettingRows = [];
Expand Down Expand Up @@ -250,6 +256,24 @@ registerBlockType( 'syntaxhighlighter/code', {
);
}

// Quick code
if ( syntaxHighlighterData.settings.quickCode.supported ) {
blockSettingRows.push(
wp.element.createElement(
PanelRow,
null,
wp.element.createElement(
ToggleControl,
{
label: __( 'Quick code', 'syntaxhighlighter' ),
checked: quickCode,
onChange: ( quickCode ) => setAttributes( { quickCode } ),
}
)
)
);
}

const blockSettings = (
<InspectorControls key="syntaxHighlighterInspectorControls">
<PanelBody title={ __( 'Settings', 'syntaxhighlighter' ) }>
Expand Down
11 changes: 8 additions & 3 deletions syntaxhighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function_exists( 'parse_blocks' ) // WordPress 5.0+
'tabsize' => 4,
'toolbar' => 0,
'wraplines' => 1, // 2.x only
'quickcode' => 1,
'quickcode' => 1, // 3.x only
) );

// Create the settings array by merging the user's settings and the defaults
Expand Down Expand Up @@ -359,6 +359,10 @@ function enqueue_block_editor_assets() {
'supported' => true,
'default' => (bool) $this->settings['autolinks'],
),
'quickCode' => (object) array(
'supported' => ( '3' == $this->settings['shversion'] ),
'default' => (bool) $this->settings['quickcode'],
),
);

wp_add_inline_script(
Expand Down Expand Up @@ -497,6 +501,7 @@ public function render_block( $attributes, $content ) {
'highlightLines' => 'highlight',
'wrapLines' => 'wraplines',
'makeURLsClickable' => 'autolinks',
'quickCode' => 'quickcode',
);

foreach ( $remaps as $from => $to ) {
Expand Down Expand Up @@ -1399,7 +1404,7 @@ function settings_page() { ?>
<label for="syntaxhighlighter-light"><input name="syntaxhighlighter_settings[light]" type="checkbox" id="syntaxhighlighter-light" value="1" <?php checked( $this->settings['light'], 1 ); ?> /> <?php _e( 'Use the light display mode, best for single lines of code', 'syntaxhighlighter' ); ?></label><br />
<label for="syntaxhighlighter-smarttabs"><input name="syntaxhighlighter_settings[smarttabs]" type="checkbox" id="syntaxhighlighter-smarttabs" value="1" <?php checked( $this->settings['smarttabs'], 1 ); ?> /> <?php _e( 'Use smart tabs allowing tabs being used for alignment', 'syntaxhighlighter' ); ?></label><br />
<label for="syntaxhighlighter-wraplines"><input name="syntaxhighlighter_settings[wraplines]" type="checkbox" id="syntaxhighlighter-wraplines" value="1" <?php checked( $this->settings['wraplines'], 1 ); ?> /> <?php _e( 'Wrap long lines (v2.x only, disabling this will make a scrollbar show instead)', 'syntaxhighlighter' ); ?></label><br />
<label for="syntaxhighlighter-quickcode"><input name="syntaxhighlighter_settings[quickcode]" type="checkbox" id="syntaxhighlighter-quickcode" value="1" <?php checked( $this->settings['quickcode'], 1 ); ?> /> <?php _e( 'Enable edit mode on double click', 'syntaxhighlighter' ); ?></label><br />
<label for="syntaxhighlighter-quickcode"><input name="syntaxhighlighter_settings[quickcode]" type="checkbox" id="syntaxhighlighter-quickcode" value="1" <?php checked( $this->settings['quickcode'], 1 ); ?> /> <?php _e( 'Enable edit mode on double click (v3.x only)', 'syntaxhighlighter' ); ?></label><br />
<!--<label for="syntaxhighlighter-htmlscript"><input name="syntaxhighlighter_settings[htmlscript]" type="checkbox" id="syntaxhighlighter-htmlscript" value="1" <?php checked( $this->settings['htmlscript'], 1 ); ?> /> <?php _e( 'Enable &quot;HTML script&quot; mode by default (see the bottom of this page for details). Checking this box is not recommended as this mode only works with certain languages.', 'syntaxhighlighter' ); ?></label>-->
</fieldset>
</td>
Expand Down Expand Up @@ -1562,7 +1567,7 @@ function validate_settings( $settings ) {
$settings['smarttabs'] = ( ! empty($settings['smarttabs']) ) ? 1 : 0;
$settings['toolbar'] = ( ! empty($settings['toolbar']) ) ? 1 : 0; // May be overridden below
$settings['wraplines'] = ( ! empty($settings['wraplines']) ) ? 1 : 0; // 2.x only for now
$settings['quickcode'] = ( ! empty($settings['quickcode']) ) ? 1 : 0;
$settings['quickcode'] = ( ! empty($settings['quickcode']) ) ? 1 : 0; // 3.x only for now

// If the version changed, then force change the toolbar version setting
if ( $settings['shversion'] != $this->settings['shversion'] ) {
Expand Down

0 comments on commit 53143c6

Please sign in to comment.