Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code block: allow HTML editing & rich text #24689

Merged
merged 3 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 -->