Skip to content

Commit a022736

Browse files
ellatrixgziolo
authored andcommitted
Writing Flow/Quote: allow splitting (#17121)
* Writing Flow/Quote: allow splitting * Add extra merge e2e test
1 parent 9d9136d commit a022736

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

packages/block-library/src/quote/edit.js

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import classnames from 'classnames';
99
import { __ } from '@wordpress/i18n';
1010
import { AlignmentToolbar, BlockControls, RichText } from '@wordpress/block-editor';
1111
import { BlockQuotation } from '@wordpress/components';
12+
import { createBlock } from '@wordpress/blocks';
1213

1314
export default function QuoteEdit( { attributes, setAttributes, isSelected, mergeBlocks, onReplace, className } ) {
1415
const { align, value, citation } = attributes;
@@ -48,6 +49,16 @@ export default function QuoteEdit( { attributes, setAttributes, isSelected, merg
4849
// translators: placeholder text used for the quote
4950
__( 'Write quote…' )
5051
}
52+
onReplace={ onReplace }
53+
onSplit={ ( piece ) =>
54+
createBlock( 'core/quote', {
55+
...attributes,
56+
value: piece,
57+
} )
58+
}
59+
__unstableOnSplitMiddle={ () =>
60+
createBlock( 'core/paragraph' )
61+
}
5162
/>
5263
{ ( ! RichText.isEmpty( citation ) || isSelected ) && (
5364
<RichText

packages/block-library/src/quote/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@ export const settings = {
3030
edit,
3131
save,
3232
merge( attributes, { value, citation } ) {
33+
// Quote citations cannot be merged. Pick the second one unless it's
34+
// empty.
35+
if ( ! citation ) {
36+
citation = attributes.citation;
37+
}
38+
3339
if ( ! value || value === '<p></p>' ) {
3440
return {
3541
...attributes,
36-
citation: attributes.citation + citation,
42+
citation,
3743
};
3844
}
3945

4046
return {
4147
...attributes,
4248
value: attributes.value + value,
43-
citation: attributes.citation + citation,
49+
citation,
4450
};
4551
},
4652
deprecated,

packages/e2e-tests/specs/blocks/__snapshots__/quote.test.js.snap

+62
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,68 @@ exports[`Quote can be merged into from a paragraph 1`] = `
8080
<!-- /wp:quote -->"
8181
`;
8282
83+
exports[`Quote can be split at the end and merged back 1`] = `
84+
"<!-- wp:quote -->
85+
<blockquote class=\\"wp-block-quote\\"><p>1</p></blockquote>
86+
<!-- /wp:quote -->
87+
88+
<!-- wp:paragraph -->
89+
<p></p>
90+
<!-- /wp:paragraph -->"
91+
`;
92+
93+
exports[`Quote can be split at the end and merged back 2`] = `
94+
"<!-- wp:quote -->
95+
<blockquote class=\\"wp-block-quote\\"><p>1</p><p></p></blockquote>
96+
<!-- /wp:quote -->"
97+
`;
98+
99+
exports[`Quote can be split at the end and merged back 3`] = `
100+
"<!-- wp:quote -->
101+
<blockquote class=\\"wp-block-quote\\"><p>1</p></blockquote>
102+
<!-- /wp:quote -->"
103+
`;
104+
105+
exports[`Quote can be split in the middle and merged back 1`] = `
106+
"<!-- wp:quote -->
107+
<blockquote class=\\"wp-block-quote\\"><p>1</p><cite>c</cite></blockquote>
108+
<!-- /wp:quote -->
109+
110+
<!-- wp:paragraph -->
111+
<p></p>
112+
<!-- /wp:paragraph -->
113+
114+
<!-- wp:quote -->
115+
<blockquote class=\\"wp-block-quote\\"><p>2</p><cite>c</cite></blockquote>
116+
<!-- /wp:quote -->"
117+
`;
118+
119+
exports[`Quote can be split in the middle and merged back 2`] = `
120+
"<!-- wp:quote -->
121+
<blockquote class=\\"wp-block-quote\\"><p>1</p><p></p><cite>c</cite></blockquote>
122+
<!-- /wp:quote -->
123+
124+
<!-- wp:quote -->
125+
<blockquote class=\\"wp-block-quote\\"><p>2</p><cite>c</cite></blockquote>
126+
<!-- /wp:quote -->"
127+
`;
128+
129+
exports[`Quote can be split in the middle and merged back 3`] = `
130+
"<!-- wp:quote -->
131+
<blockquote class=\\"wp-block-quote\\"><p>1</p><cite>c</cite></blockquote>
132+
<!-- /wp:quote -->
133+
134+
<!-- wp:quote -->
135+
<blockquote class=\\"wp-block-quote\\"><p>2</p><cite>c</cite></blockquote>
136+
<!-- /wp:quote -->"
137+
`;
138+
139+
exports[`Quote can be split in the middle and merged back 4`] = `
140+
"<!-- wp:quote -->
141+
<blockquote class=\\"wp-block-quote\\"><p>1</p><p>2</p><cite>c</cite></blockquote>
142+
<!-- /wp:quote -->"
143+
`;
144+
83145
exports[`Quote is transformed to a heading and a quote if the quote contains a citation 1`] = `
84146
"<!-- wp:heading -->
85147
<h2>one</h2>

packages/e2e-tests/specs/blocks/quote.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,57 @@ describe( 'Quote', () => {
185185
await page.keyboard.press( 'Backspace' );
186186
expect( await getEditedPostContent() ).toMatchSnapshot();
187187
} );
188+
189+
it( 'can be split at the end and merged back', async () => {
190+
await insertBlock( 'Quote' );
191+
await page.keyboard.type( '1' );
192+
await page.keyboard.press( 'Enter' );
193+
await page.keyboard.press( 'Enter' );
194+
195+
// Expect empty paragraph outside quote block.
196+
expect( await getEditedPostContent() ).toMatchSnapshot();
197+
198+
await page.keyboard.press( 'Backspace' );
199+
200+
// Expect empty paragraph inside quote block.
201+
expect( await getEditedPostContent() ).toMatchSnapshot();
202+
203+
await page.keyboard.press( 'Backspace' );
204+
205+
// Expect quote without empty paragraphs.
206+
expect( await getEditedPostContent() ).toMatchSnapshot();
207+
} );
208+
209+
it( 'can be split in the middle and merged back', async () => {
210+
await insertBlock( 'Quote' );
211+
await page.keyboard.type( '1' );
212+
await page.keyboard.press( 'Enter' );
213+
await page.keyboard.type( '2' );
214+
await page.keyboard.press( 'Tab' );
215+
await page.keyboard.type( 'c' );
216+
await page.keyboard.press( 'ArrowUp' );
217+
await page.keyboard.press( 'ArrowUp' );
218+
await page.keyboard.press( 'Enter' );
219+
await page.keyboard.press( 'Enter' );
220+
221+
// Expect two quote blocks and empty paragraph in the middle.
222+
expect( await getEditedPostContent() ).toMatchSnapshot();
223+
224+
await page.keyboard.press( 'Backspace' );
225+
226+
// Expect two quote blocks and empty paragraph in the first quote.
227+
expect( await getEditedPostContent() ).toMatchSnapshot();
228+
229+
await page.keyboard.press( 'Backspace' );
230+
231+
// Expect two quote blocks.
232+
expect( await getEditedPostContent() ).toMatchSnapshot();
233+
234+
await page.keyboard.press( 'ArrowLeft' );
235+
await page.keyboard.press( 'ArrowDown' );
236+
await page.keyboard.press( 'ArrowDown' );
237+
await page.keyboard.press( 'Backspace' );
238+
239+
expect( await getEditedPostContent() ).toMatchSnapshot();
240+
} );
188241
} );

0 commit comments

Comments
 (0)