-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Allow pasting tables with list content inside the cells. #46775
Open
mpkelly
wants to merge
3
commits into
WordPress:trunk
Choose a base branch
from
mpkelly:add/support-for-pasting-lists-into-tables
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5e8a208
Allow pasting tables with list content inside the cells. This content…
mpkelly aa6ce58
Allow pasting tables with list content inside the cells. This content…
mpkelly 38218df
Merge branch 'trunk' into add/support-for-pasting-lists-into-tables
danielbachhuber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getListItemBullet } from '../utils'; | ||
|
||
describe( 'getListItemBullet', () => { | ||
it( 'UL list item', () => { | ||
expect( getListItemBullet( 'UL', false, 0 ) ).toBe( '-' ); | ||
expect( getListItemBullet( 'UL', false, 100 ) ).toBe( '-' ); | ||
} ); | ||
it( 'OL numeric item', () => { | ||
expect( getListItemBullet( 'OL', true, 0 ) ).toBe( '1.' ); | ||
expect( getListItemBullet( 'OL', true, 26 ) ).toBe( '27.' ); | ||
expect( getListItemBullet( 'OL', true, 53 ) ).toBe( '54.' ); | ||
} ); | ||
it( 'OL character list item', () => { | ||
expect( getListItemBullet( 'OL', false, 0 ) ).toBe( 'a.' ); | ||
expect( getListItemBullet( 'OL', false, 26 ) ).toBe( 'aa.' ); | ||
expect( getListItemBullet( 'OL', false, 53 ) ).toBe( 'bbb.' ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createBlock, getBlockAttributes } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { name } from './block.json'; | ||
import { getListContentSchema } from '../list/transforms'; | ||
import { getListItemBullet } from './utils'; | ||
|
||
const tableContentPasteSchema = ( { phrasingContentSchema } ) => ( { | ||
tr: { | ||
allowEmpty: true, | ||
children: { | ||
th: { | ||
allowEmpty: true, | ||
children: phrasingContentSchema, | ||
children: getListContentSchema( { phrasingContentSchema } ), | ||
attributes: [ 'scope', 'colspan' ], | ||
}, | ||
td: { | ||
allowEmpty: true, | ||
children: phrasingContentSchema, | ||
children: getListContentSchema( { phrasingContentSchema } ), | ||
attributes: [ 'colspan' ], | ||
}, | ||
}, | ||
|
@@ -35,12 +47,101 @@ const tablePasteSchema = ( args ) => ( { | |
}, | ||
} ); | ||
|
||
const indent = ( depth ) => { | ||
return Array.from( { length: depth * 4 } ) | ||
.map( () => ' ' ) | ||
.join( '' ); | ||
}; | ||
|
||
const isList = ( node ) => { | ||
return node.tagName === 'UL' || node.tagName === 'OL'; | ||
}; | ||
|
||
const transformContent = ( cell ) => { | ||
// not available by default in node (jest) env so eslint complains. | ||
// eslint-disable-next-line no-undef | ||
const parser = new DOMParser(); | ||
const document = parser.parseFromString( cell.content, 'text/html' ); | ||
const result = []; | ||
let orderedListUsesNumbers = true; | ||
const processDOMTree = ( node, listDepth = 0 ) => { | ||
if ( isList( node ) ) { | ||
//debugger; | ||
if ( listDepth === 0 ) { | ||
result.push( '<br/>' ); | ||
} | ||
Array.from( node.childNodes ).forEach( | ||
( listItem, listItemIndex ) => { | ||
const children = Array.from( listItem.childNodes ); | ||
let simple = []; | ||
children.forEach( ( child ) => { | ||
if ( isList( child ) ) { | ||
const bullet = getListItemBullet( | ||
node.tagName, | ||
orderedListUsesNumbers, | ||
listItemIndex | ||
); | ||
if ( simple.length ) { | ||
result.push( | ||
`${ indent( | ||
listDepth | ||
) } ${ bullet } ${ simple.join( '' ) }<br/>` | ||
); | ||
simple = []; | ||
} | ||
orderedListUsesNumbers = ! orderedListUsesNumbers; | ||
processDOMTree( child, listDepth + 1 ); | ||
} else { | ||
simple.push( child.outerHTML || child.textContent ); | ||
} | ||
} ); | ||
if ( simple.length ) { | ||
const bullet = getListItemBullet( | ||
node.tagName, | ||
orderedListUsesNumbers, | ||
listItemIndex | ||
); | ||
|
||
result.push( | ||
`${ indent( | ||
listDepth | ||
) } ${ bullet } ${ simple.join( '' ) }<br/>` | ||
); | ||
simple = []; | ||
} | ||
} | ||
); | ||
} else if ( node.nodeName === '#text' ) { | ||
result.push( node.textContent ); | ||
} else { | ||
result.push( node.outerHTML ); | ||
} | ||
}; | ||
|
||
Array.from( document.body.childNodes ).forEach( ( node ) => | ||
processDOMTree( node ) | ||
); | ||
|
||
cell.content = result.join( '' ); | ||
}; | ||
|
||
const transformTableNode = ( node ) => { | ||
const attributes = getBlockAttributes( name, node.outerHTML ); | ||
attributes.body.forEach( ( row ) => { | ||
row.cells.forEach( ( cell ) => { | ||
transformContent( cell ); | ||
} ); | ||
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not transform |
||
return createBlock( name, attributes ); | ||
}; | ||
|
||
const transforms = { | ||
from: [ | ||
{ | ||
type: 'raw', | ||
selector: 'table', | ||
schema: tablePasteSchema, | ||
transform: transformTableNode, | ||
}, | ||
], | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
function intToChar( int ) { | ||
const code = 'a'.charCodeAt( 0 ); | ||
return String.fromCharCode( code + int ); | ||
} | ||
|
||
const NUMBER_OF_CHARS_IN_ALPHABET = 26; | ||
|
||
// returns a, b, c...aa, bb etc. for order lists that are nested in numeric ordered lists. | ||
const getListItemCharCode = ( index ) => { | ||
const code = intToChar( index % NUMBER_OF_CHARS_IN_ALPHABET ); | ||
const multiplier = Math.floor( index / NUMBER_OF_CHARS_IN_ALPHABET ) + 1; | ||
return Array.from( { length: multiplier } ) | ||
.map( () => code ) | ||
.join( '' ); | ||
}; | ||
|
||
// Return the bullet for a list item. | ||
export const getListItemBullet = ( type, orderedListUsesNumbers, index ) => { | ||
if ( type === 'UL' ) { | ||
return '-'; | ||
} | ||
return orderedListUsesNumbers | ||
? `${ index + 1 }.` | ||
: `${ getListItemCharCode( index ) }.`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/integration/fixtures/documents/google-docs-table-with-lists-in.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<meta charset="utf-8"><b style="font-weight:normal;" id="docs-internal-guid-2ff64649-7fff-2807-3386-7b8d2fb0cab6"><div dir="ltr" style="margin-left:0pt;" align="left"><table style="border:none;border-collapse:collapse;"><colgroup><col width="143" /><col width="143" /><col width="339" /></colgroup><tbody><tr style="height:21pt"><td style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Test</span></p></td><td colspan="2" style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Stuff</span></p></td></tr><tr style="height:21pt"><td colspan="3" style="border-left:solid #000000 1pt;border-right:solid #000000 1pt;border-bottom:solid #000000 1pt;border-top:solid #000000 1pt;vertical-align:top;padding:5pt 5pt 5pt 5pt;overflow:hidden;overflow-wrap:break-word;"><br /><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Notes:</span></p><ol style="margin-top:0;margin-bottom:0;padding-inline-start:48px;"><li dir="ltr" style="list-style-type:decimal;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="1"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Line one </span></p></li><li dir="ltr" style="list-style-type:decimal;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="1"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Line </span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">two</span></p></li><ol style="margin-top:0;margin-bottom:0;padding-inline-start:48px;"><li dir="ltr" style="list-style-type:lower-alpha;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="2"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:700;font-style:italic;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Nested</span><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;"> one</span></p></li><ol style="margin-top:0;margin-bottom:0;padding-inline-start:48px;"><li dir="ltr" style="list-style-type:lower-roman;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="3"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Nested two</span></p></li></ol></ol><li dir="ltr" style="list-style-type:decimal;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="1"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Line three</span></p></li></ol><br /><ul style="margin-top:0;margin-bottom:0;padding-inline-start:48px;"><li dir="ltr" style="list-style-type:disc;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="1"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Another list</span></p></li><ul style="margin-top:0;margin-bottom:0;padding-inline-start:48px;"><li dir="ltr" style="list-style-type:disc;font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;" aria-level="2"><p dir="ltr" style="line-height:1.2;margin-top:0pt;margin-bottom:0pt;" role="presentation"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;">Stuff</span></p></li></ul></ul></td></tr></tbody></table></div><br /><br /></b> |
3 changes: 3 additions & 0 deletions
3
test/integration/fixtures/documents/google-docs-table-with-lists-out.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<!-- wp:table --> | ||
<figure class="wp-block-table"><table><tbody><tr><td><strong>Test</strong></td><td colspan="2"><strong>Stuff</strong></td></tr><tr><td colspan="3"><br><strong>Notes:</strong><br/> 1. Line one <br/> 2. Line <strong>two</strong><br/> a. <strong><em>Nested</em></strong> one<br/> 1. Nested two<br/> 3. Line three<br/><br/> - Another list<br/> - Stuff<br/></td></tr></tbody></table></figure> | ||
<!-- /wp:table --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like this. Why not use
'*'
forchildren
and let raw handling parse the cell contents as blocks, then transform that to the markdown syntax. OR instead of parsing as blocks, just convert the HTML to mark down?gutenberg/packages/block-library/src/quote/transforms.js
Lines 38 to 61 in 6517008
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although, when it's parsed as block, it's harder to convert all blocks to something textual (like heading to plain text etc.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't we build this into the paste handler and generally convert lists to plain text if the allowed children is
phrasingContentSchema
? This behaviour could be useful in other places. What if there's a list in a caption? Generalising this would be great.So inside the paste handler, I think we need a filter before
removeInvalidHTML
that converts list that are not allowed in these places (not in schema) to the text version of the list.Let me know if you need help here.