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: preserve indentation on paste #26347

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 3 additions & 0 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ function RichTextWrapper(
HTML: filePasteHandler( files ),
mode: 'BLOCKS',
tagName,
preserveWhiteSpace,
} );

// Allows us to ask for this information when we get a report.
Expand Down Expand Up @@ -457,6 +458,7 @@ function RichTextWrapper(
plainText,
mode,
tagName,
preserveWhiteSpace,
} );

if ( typeof content === 'string' ) {
Expand Down Expand Up @@ -500,6 +502,7 @@ function RichTextWrapper(
splitValue,
__unstableEmbedURLOnPaste,
multiline,
preserveWhiteSpace,
]
);

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/code/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function CodeEdit( { attributes, setAttributes } ) {
onChange={ ( content ) => setAttributes( { content } ) }
placeholder={ __( 'Write code…' ) }
aria-label={ __( 'Code' ) }
preserveWhiteSpace
/>
</pre>
);
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ _Parameters_
- _options.plainText_ `[string]`: Plain text version.
- _options.mode_ `[string]`: Handle content as blocks or inline content. _ 'AUTO': Decide based on the content passed. _ 'INLINE': Always handle as inline content, and return string. \* 'BLOCKS': Always handle as blocks, and return array of blocks.
- _options.tagName_ `[Array]`: The tag into which content will be inserted.
- _options.preserveWhiteSpace_ `[boolean]`: Whether or not to preserve consequent white space.

_Returns_

Expand Down
19 changes: 14 additions & 5 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ const { console } = window;
/**
* Filters HTML to only contain phrasing content.
*
* @param {string} HTML The HTML to filter.
* @param {string} HTML The HTML to filter.
* @param {boolean} preserveWhiteSpace Whether or not to preserve consequent white space.
*
* @return {string} HTML only containing phrasing content.
*/
function filterInlineHTML( HTML ) {
function filterInlineHTML( HTML, preserveWhiteSpace ) {
HTML = deepFilterHTML( HTML, [
googleDocsUIDRemover,
phrasingContentReducer,
Expand All @@ -56,7 +57,12 @@ function filterInlineHTML( HTML ) {
HTML = removeInvalidHTML( HTML, getPhrasingContentSchema( 'paste' ), {
inline: true,
} );
HTML = deepFilterHTML( HTML, [ htmlFormattingRemover, brRemover ] );

console.log( preserveWhiteSpace );
Copy link
Contributor

Choose a reason for hiding this comment

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

Assume you want to ditch this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, sorry, had to push this quick

Copy link
Member Author

Choose a reason for hiding this comment

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

Strange the linter didn't catch it


if ( ! preserveWhiteSpace ) {
HTML = deepFilterHTML( HTML, [ htmlFormattingRemover, brRemover ] );
}

// Allows us to ask for this information when we get a report.
console.log( 'Processed inline HTML:\n\n', HTML );
Expand Down Expand Up @@ -132,6 +138,7 @@ function htmlToBlocks( { html, rawTransforms } ) {
* * 'INLINE': Always handle as inline content, and return string.
* * 'BLOCKS': Always handle as blocks, and return array of blocks.
* @param {Array} [options.tagName] The tag into which content will be inserted.
* @param {boolean} [options.preserveWhiteSpace] Whether or not to preserve consequent white space.
*
* @return {Array|string} A list of blocks or a string, depending on `handlerMode`.
*/
Expand All @@ -140,7 +147,9 @@ export function pasteHandler( {
plainText = '',
mode = 'AUTO',
tagName,
preserveWhiteSpace,
} ) {
console.log( { preserveWhiteSpace } );
Copy link
Contributor

Choose a reason for hiding this comment

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

We'll want to remove this.

// First of all, strip any meta tags.
HTML = HTML.replace( /<meta[^>]+>/g, '' );
// Strip Windows markers.
Expand Down Expand Up @@ -196,7 +205,7 @@ export function pasteHandler( {
}

if ( mode === 'INLINE' ) {
return filterInlineHTML( HTML );
return filterInlineHTML( HTML, preserveWhiteSpace );
}

// An array of HTML strings and block objects. The blocks replace matched
Expand All @@ -213,7 +222,7 @@ export function pasteHandler( {
! hasShortcodes &&
isInlineContent( HTML, tagName )
) {
return filterInlineHTML( HTML );
return filterInlineHTML( HTML, preserveWhiteSpace );
}

const rawTransforms = getRawTransformations();
Expand Down