Skip to content

Commit

Permalink
RichText: don't use DOM to add line padding
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Feb 20, 2019
1 parent 9f5f1c3 commit 4116973
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports[`adding blocks should create valid paragraph blocks when rapidly pressin
exports[`adding blocks should insert line break at end 1`] = `
"<!-- wp:paragraph -->
<p>a<br><br></p>
<p>a<br></p>
<!-- /wp:paragraph -->"
`;
Expand All @@ -90,7 +90,7 @@ exports[`adding blocks should insert line break at start 1`] = `
exports[`adding blocks should insert line break in empty container 1`] = `
"<!-- wp:paragraph -->
<p><br><br></p>
<p><br></p>
<!-- /wp:paragraph -->"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ exports[`List should indent and outdent level 2 3`] = `
exports[`List should insert a line break on shift+enter 1`] = `
"<!-- wp:list -->
<ul><li>a<br><br></li></ul>
<ul><li>a<br></li></ul>
<!-- /wp:list -->"
`;
exports[`List should insert a line break on shift+enter in a non trailing list item 1`] = `
"<!-- wp:list -->
<ul><li>a</li><li>b<br><br></li><li>c</li></ul>
<ul><li>a</li><li>b<br></li><li>c</li></ul>
<!-- /wp:list -->"
`;
Expand Down
24 changes: 3 additions & 21 deletions packages/rich-text/src/insert-line-break.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,14 @@
*/

import { insert } from './insert';
import { LINE_SEPARATOR } from './special-characters';

/**
* Inserts a line break at the given or selected position. Inserts two line
* breaks if at the end of a line.
* Inserts a line break at the given or selected position.
*
* @param {Object} value Value to modify.
*
* @return {Object} The value with the line break(s) inserted.
* @return {Object} The value with the line break inserted.
*/
export function insertLineBreak( value ) {
const { text, end } = value;
const length = text.length;

let toInsert = '\n';

// If the caret is at the end of the text, and there is no
// trailing line break or no text at all, we have to insert two
// line breaks in order to create a new line visually and place
// the caret there.
if (
( end === length || text[ end ] === LINE_SEPARATOR ) &&
( text[ end - 1 ] !== '\n' || length === 0 )
) {
toInsert = '\n\n';
}

return insert( value, toInsert );
return insert( value, '\n' );
}
9 changes: 9 additions & 0 deletions packages/rich-text/src/test/__snapshots__/to-dom.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ exports[`recordToDom should handle br 1`] = `
<br />
<br
data-rich-text-padding="true"
/>
</body>
`;

Expand All @@ -147,6 +150,9 @@ exports[`recordToDom should handle br with formatting 1`] = `
</em>
<br
data-rich-text-padding="true"
/>
</body>
`;

Expand Down Expand Up @@ -197,12 +203,14 @@ exports[`recordToDom should handle middle empty list value 1`] = `
<br
data-rich-text-padding="true"
/>
</li>
<li>
<br
data-rich-text-padding="true"
/>
</li>
<li>
Expand Down Expand Up @@ -276,6 +284,7 @@ exports[`recordToDom should handle multiline value with empty 1`] = `
exports[`recordToDom should handle nested empty list value 1`] = `
<body>
<li>
<br
data-rich-text-padding="true"
/>
Expand Down
8 changes: 4 additions & 4 deletions packages/rich-text/src/test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ export const spec = [
endOffset: 0,
endContainer: element.querySelector( 'ul > li' ),
} ),
startPath: [ 0, 0, 0, 0, 0 ],
endPath: [ 0, 0, 0, 0, 0 ],
startPath: [ 0, 2, 0, 0, 0 ],
endPath: [ 0, 2, 0, 0, 0 ],
record: {
start: 1,
end: 1,
Expand All @@ -479,8 +479,8 @@ export const spec = [
endOffset: 0,
endContainer: element.firstChild.nextSibling,
} ),
startPath: [ 1, 0, 0 ],
endPath: [ 1, 0, 0 ],
startPath: [ 1, 2, 0 ],
endPath: [ 1, 2, 0 ],
record: {
start: 1,
end: 1,
Expand Down
37 changes: 0 additions & 37 deletions packages/rich-text/src/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,39 +113,6 @@ function remove( node ) {
return node.parentNode.removeChild( node );
}

function createLinePadding( doc ) {
const element = doc.createElement( 'br' );
element.setAttribute( 'data-rich-text-padding', 'true' );
return element;
}

function padEmptyLines( { element, multilineWrapperTags } ) {
const length = element.childNodes.length;
const doc = element.ownerDocument;

for ( let index = 0; index < length; index++ ) {
const child = element.childNodes[ index ];

if ( child.nodeType === TEXT_NODE ) {
if ( length === 1 && ! child.nodeValue ) {
// Pad if the only child is an empty text node.
element.appendChild( createLinePadding( doc ) );
}
} else {
if (
multilineWrapperTags &&
! child.previousSibling &&
multilineWrapperTags.indexOf( child.nodeName.toLowerCase() ) !== -1
) {
// Pad the line if there is no content before a nested wrapper.
element.insertBefore( createLinePadding( doc ), child );
}

padEmptyLines( { element: child, multilineWrapperTags } );
}
}
}

function prepareFormats( prepareEditableTree = [], value ) {
return prepareEditableTree.reduce( ( accumlator, fn ) => {
return fn( accumlator, value.text );
Expand Down Expand Up @@ -186,10 +153,6 @@ export function toDom( {
isEditableTree,
} );

if ( isEditableTree ) {
padEmptyLines( { element: tree, multilineWrapperTags } );
}

return {
body: tree,
selection: { startPath, endPath },
Expand Down
33 changes: 33 additions & 0 deletions packages/rich-text/src/to-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ function getDeepestActiveFormat( value ) {
return activeFormats[ selectedFormat - 1 ];
}

const padding = {
type: 'br',
attributes: {
'data-rich-text-padding': 'true',
},
object: true,
};

export function toTree( {
value,
multilineTag,
Expand Down Expand Up @@ -136,6 +144,23 @@ export function toTree( {

let pointer = getLastChild( tree );

if ( isEditableTree && character === LINE_SEPARATOR ) {
let node = pointer;

while ( ! isText( node ) ) {
node = getLastChild( node );
}

if (
! lastCharacter ||
lastCharacter === LINE_SEPARATOR ||
lastCharacter === '\n'
) {
append( getParent( node ), padding );
append( getParent( node ), '' );
}
}

// Set selection for the start of line.
if ( lastCharacter === LINE_SEPARATOR ) {
let node = pointer;
Expand Down Expand Up @@ -232,6 +257,14 @@ export function toTree( {
onEndIndex( tree, pointer );
}

if ( i === text.length && isEditableTree && (
! lastCharacter ||
lastCharacter === LINE_SEPARATOR ||
lastCharacter === '\n'
) ) {
append( getParent( pointer ), padding );
}

lastCharacterFormats = characterFormats;
lastCharacter = character;
}
Expand Down

0 comments on commit 4116973

Please sign in to comment.