Skip to content

Commit

Permalink
Merge pull request #786 from westonruter/remove/legacy-wp-compat
Browse files Browse the repository at this point in the history
Support exiting Code block with triple andter and remove legacy code for WP<6.1
  • Loading branch information
westonruter authored Nov 21, 2023
2 parents dd87d7b + 5a29511 commit 2a415f3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 50 deletions.
18 changes: 2 additions & 16 deletions inc/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,22 +210,8 @@ function init(): void {

if ( $block instanceof WP_Block_Type ) {
register_editor_assets( $block );

if ( property_exists( $block, 'editor_script_handles' ) ) {
// As of WP>=6.1.
$block->editor_script_handles[] = EDITOR_SCRIPT_HANDLE;
} else {
/* @noinspection PhpUndefinedFieldInspection */
$block->editor_script = EDITOR_SCRIPT_HANDLE;
}

if ( property_exists( $block, 'editor_style_handles' ) ) {
// As of WP>=6.1.
$block->editor_style_handles[] = EDITOR_STYLE_HANDLE;
} else {
/* @noinspection PhpUndefinedFieldInspection */
$block->editor_style = EDITOR_STYLE_HANDLE;
}
$block->editor_script_handles[] = EDITOR_SCRIPT_HANDLE;
$block->editor_style_handles[] = EDITOR_STYLE_HANDLE;
}
}

Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"devDependencies": {
"@wordpress/api-fetch": "6.43.0",
"@wordpress/block-editor": "12.14.0",
"@wordpress/blocks": "12.23.0",
"@wordpress/components": "25.12.0",
"@wordpress/editor": "13.23.0",
"@wordpress/element": "5.23.0",
Expand Down
27 changes: 0 additions & 27 deletions phpstan-baseline.php

This file was deleted.

1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ includes:
# @see https://github.com/phpstan/phpstan-src/blob/b9f62d63f2deaa0a5e97f51073e41a422c48aa01/conf/bleedingEdge.neon
- phar://phpstan.phar/conf/bleedingEdge.neon
- vendor/szepeviktor/phpstan-wordpress/extension.neon
- phpstan-baseline.php
parameters:
level: 9
paths:
Expand Down
18 changes: 15 additions & 3 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
PanelBody,
PanelRow,
} from '@wordpress/components';
import { createBlock, getDefaultBlockName } from '@wordpress/blocks';

/**
* External dependencies
Expand Down Expand Up @@ -110,7 +111,13 @@ const parseHighlightedLines = (highlightedLines) => {
return highlightedLinesSet;
};

export default function CodeEdit({ attributes, setAttributes, onRemove }) {
export default function CodeEdit({
attributes,
setAttributes,
onRemove,
insertBlocksAfter,
mergeBlocks,
}) {
const blockProps = useBlockProps();

const updateLanguage = (language) => {
Expand Down Expand Up @@ -138,16 +145,21 @@ export default function CodeEdit({ attributes, setAttributes, onRemove }) {
);

const richTextProps = {
// These RichText props must mirror core <https://github.com/WordPress/gutenberg/blob/a42fd75/packages/block-library/src/code/edit.js#L12-L19>.
// These RichText props must mirror core <https://github.com/WordPress/gutenberg/blob/e95bb8c9530bbdef1db623eca11b80bd73493197/packages/block-library/src/code/edit.js#L19-L31>.
...{
tagName: 'code',
identifier: 'content',
value: attributes.content,
onChange: (content) => setAttributes({ content }),
onRemove,
onMerge: mergeBlocks,
placeholder: __('Write code…'),
'aria-label': __('Code'),
preserveWhiteSpace: true,
__unstablePastePlainText: true, // See <https://github.com/WordPress/gutenberg/pull/27236>.
__unstableOnSplitAtDoubleLineEnd: () => {
insertBlocksAfter(createBlock(getDefaultBlockName()));
},
},

// Additional props unique to HighlightableTextArea.
Expand Down Expand Up @@ -231,7 +243,7 @@ export default function CodeEdit({ attributes, setAttributes, onRemove }) {
</PanelRow>
</PanelBody>
</InspectorControls>
{/* Keep in sync with https://github.com/WordPress/gutenberg/blob/a42fd75/packages/block-library/src/code/edit.js#L10-L21 */}
{/* Keep in sync with https://github.com/WordPress/gutenberg/blob/e95bb8c9530bbdef1db623eca11b80bd73493197/packages/block-library/src/code/edit.js#L17 */}
<pre {...blockProps}>
<HighlightableTextArea {...richTextProps} />
</pre>
Expand Down

0 comments on commit 2a415f3

Please sign in to comment.