Skip to content

Commit

Permalink
Code block: allow HTML editing & rich text (#24689)
Browse files Browse the repository at this point in the history
* Code block: allow HTML editing

* Change to RichText

* Fix integration test
  • Loading branch information
ellatrix authored Sep 24, 2020
1 parent b18a2ff commit f5c32f8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 18 deletions.
3 changes: 1 addition & 2 deletions packages/block-library/src/code/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
"attributes": {
"content": {
"type": "string",
"source": "text",
"source": "html",
"selector": "code"
}
},
"supports": {
"anchor": true,
"html": false,
"lightBlockWrapper": true
}
}
9 changes: 2 additions & 7 deletions packages/block-library/src/code/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import {
PlainText,
RichText,
__experimentalUseBlockWrapperProps as useBlockWrapperProps,
} from '@wordpress/block-editor';

export default function CodeEdit( { attributes, setAttributes } ) {
const blockWrapperProps = useBlockWrapperProps();
return (
<pre { ...blockWrapperProps }>
<PlainText
__experimentalVersion={ 2 }
<RichText
tagName="code"
value={ attributes.content }
onChange={ ( content ) => setAttributes( { content } ) }
Expand Down
10 changes: 9 additions & 1 deletion packages/block-library/src/code/save.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
Expand All @@ -6,7 +11,10 @@ import { escape } from './utils';
export default function save( { attributes } ) {
return (
<pre>
<code>{ escape( attributes.content ) }</code>
<RichText.Content
tagName="code"
value={ escape( attributes.content ) }
/>
</pre>
);
}
6 changes: 0 additions & 6 deletions packages/block-library/src/code/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
*/
import { flow } from 'lodash';

/**
* WordPress dependencies
*/
import { escapeEditableHTML } from '@wordpress/escape-html';

/**
* Escapes ampersands, shortcodes, and links.
*
Expand All @@ -16,7 +11,6 @@ import { escapeEditableHTML } from '@wordpress/escape-html';
*/
export function escape( content ) {
return flow(
escapeEditableHTML,
escapeOpeningSquareBrackets,
escapeProtocolInIsolatedUrls
)( content || '' );
Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/fixtures/blocks/core__code.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "core/code",
"isValid": true,
"attributes": {
"content": "export default function MyButton() {\n\treturn <Button>Click Me!</Button>;\n}"
"content": "export default function MyButton() {\n\treturn &lt;Button&gt;Click Me!&lt;/Button&gt;;\n}"
},
"innerBlocks": [],
"originalContent": "<pre class=\"wp-block-code\"><code>export default function MyButton() {\n\treturn &lt;Button&gt;Click Me!&lt;/Button&gt;;\n}</code></pre>"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:code -->
<pre class="wp-block-code"><code>export default function MyButton() {
return &lt;Button>Click Me!&lt;/Button>;
return &lt;Button&gt;Click Me!&lt;/Button&gt;;
}</code></pre>
<!-- /wp:code -->

0 comments on commit f5c32f8

Please sign in to comment.