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

Add block transform to transform preformatted block into code block #22634

Merged
merged 1 commit into from
Jun 8, 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
5 changes: 5 additions & 0 deletions packages/block-library/src/preformatted/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const transforms = {
transform: ( attributes ) =>
createBlock( 'core/paragraph', attributes ),
},
{
type: 'block',
blocks: [ 'core/code' ],
transform: ( attributes ) => createBlock( 'core/code', attributes ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that the "preformatted" content attribute has this property on it "__unstablePreserveWhiteSpace": true
I wonder if that means that we should sanitize the content when transforming cc @ellatrix
Also, do we want to support the opposite direction too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, do we want to support the opposite direction too?

Yes, but this is already implemented, see my gif in the Screenshot section.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine, because a textarea doesn't need anything special to preserve whitespace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I believe we use characters for line breaks when __unstablePreserveWhiteSpace is enabled, so I don't think there's any HTML to strip. Worth noting that pre can contain HTML though. Can have bold, links etc.

},
],
};

Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/fixtures/block-transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const EXPECTED_TRANSFORMS = {
},
core__preformatted: {
originalBlock: 'Preformatted',
availableTransforms: [ 'Group', 'Paragraph' ],
availableTransforms: [ 'Group', 'Paragraph', 'Code' ],
},
core__pullquote: {
originalBlock: 'Pullquote',
Expand Down