Skip to content

Commit

Permalink
Build in own padding and boundary classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 30, 2019
1 parent ed4200d commit 86de3a9
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports[`core/button block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</div>
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`core/heading block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</h2>
<h2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ exports[`core/list block edit matches snapshot 1`] = `
>
<li>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</li>
</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports[`core/paragraph block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</p>
<p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`core/preformatted block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</pre>
<pre
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exports[`core/pullquote block edit matches snapshot 1`] = `
>
<p>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports[`core/quote block edit matches snapshot 1`] = `
>
<p>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</p>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports[`core/text-columns block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</p>
<p
Expand Down Expand Up @@ -59,7 +59,7 @@ exports[`core/text-columns block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</p>
<p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`core/verse block edit matches snapshot 1`] = `
role="textbox"
>
<br
data-mce-bogus="1"
data-rich-text-padding="true"
/>
</pre>
<pre
Expand Down
13 changes: 0 additions & 13 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ export class RichText extends Component {
range,
multilineTag: this.multilineTag,
multilineWrapperTags: this.multilineWrapperTags,
removeNode: ( node ) => node.getAttribute( 'data-mce-bogus' ) === 'all',
unwrapNode: ( node ) => !! node.getAttribute( 'data-mce-bogus' ),
removeAttribute: ( attribute ) => attribute.indexOf( 'data-mce-' ) === 0,
prepareEditableTree: this.props.prepareEditableTree,
} );
}
Expand All @@ -175,11 +172,6 @@ export class RichText extends Component {
current: this.editableRef,
multilineTag: this.multilineTag,
multilineWrapperTags: this.multilineWrapperTags,
createLinePadding( doc ) {
const element = doc.createElement( 'br' );
element.setAttribute( 'data-mce-bogus', '1' );
return element;
},
prepareEditableTree: this.props.prepareEditableTree,
} );
}
Expand Down Expand Up @@ -740,11 +732,6 @@ export class RichText extends Component {
value,
multilineTag: this.multilineTag,
multilineWrapperTags: this.multilineWrapperTags,
createLinePadding( doc ) {
const element = doc.createElement( 'br' );
element.setAttribute( 'data-mce-bogus', '1' );
return element;
},
prepareEditableTree: this.props.prepareEditableTree,
} ).body.innerHTML;
}
Expand Down
86 changes: 16 additions & 70 deletions packages/rich-text/src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ function toFormat( { type, attributes } ) {
* multiline.
* @param {?Array} $1.multilineWrapperTags Tags where lines can be found if
* nesting is possible.
* @param {?Function} $1.removeNode Function to declare whether the
* given node should be removed.
* @param {?Function} $1.unwrapNode Function to declare whether the
* given node should be unwrapped.
* @param {?Function} $1.removeAttribute Wether to remove an attribute
* based on the name.
*
* @return {Object} A rich text value.
*/
Expand All @@ -115,9 +109,6 @@ export function create( {
range,
multilineTag,
multilineWrapperTags,
removeNode,
unwrapNode,
removeAttribute,
} = {} ) {
if ( typeof text === 'string' && text.length > 0 ) {
return {
Expand All @@ -138,9 +129,6 @@ export function create( {
return createFromElement( {
element,
range,
removeNode,
unwrapNode,
removeAttribute,
} );
}

Expand All @@ -149,9 +137,6 @@ export function create( {
range,
multilineTag,
multilineWrapperTags,
removeNode,
unwrapNode,
removeAttribute,
} );
}

Expand Down Expand Up @@ -267,12 +252,6 @@ function filterString( string ) {
* multiline.
* @param {?Array} $1.multilineWrapperTags Tags where lines can be found if
* nesting is possible.
* @param {?Function} $1.removeNode Function to declare whether the
* given node should be removed.
* @param {?Function} $1.unwrapNode Function to declare whether the
* given node should be unwrapped.
* @param {?Function} $1.removeAttribute Wether to remove an attribute
* based on the name.
*
* @return {Object} A rich text value.
*/
Expand All @@ -282,9 +261,6 @@ function createFromElement( {
multilineTag,
multilineWrapperTags,
currentWrapperTags = [],
removeNode,
unwrapNode,
removeAttribute,
} ) {
const accumulator = createEmptyValue();

Expand Down Expand Up @@ -319,10 +295,7 @@ function createFromElement( {
continue;
}

if (
( removeNode && removeNode( node ) ) ||
( unwrapNode && unwrapNode( node ) && ! node.hasChildNodes() )
) {
if ( node.getAttribute( 'data-rich-text-padding' ) ) {
accumulateSelection( accumulator, node, range, createEmptyValue() );
continue;
}
Expand All @@ -336,37 +309,30 @@ function createFromElement( {

const lastFormats = accumulator.formats[ accumulator.formats.length - 1 ];
const lastFormat = lastFormats && lastFormats[ lastFormats.length - 1 ];
let format;
let value;
const newFormat = toFormat( {
type,
attributes: getAttributes( { element: node } ),
} );

if ( ! unwrapNode || ! unwrapNode( node ) ) {
const newFormat = toFormat( {
type,
attributes: getAttributes( {
element: node,
removeAttribute,
} ),
} );
let format;

if ( newFormat ) {
// Reuse the last format if it's equal.
if ( isFormatEqual( newFormat, lastFormat ) ) {
format = lastFormat;
} else {
format = newFormat;
}
if ( newFormat ) {
// Reuse the last format if it's equal.
if ( isFormatEqual( newFormat, lastFormat ) ) {
format = lastFormat;
} else {
format = newFormat;
}
}

let value;

if ( multilineWrapperTags && multilineWrapperTags.indexOf( type ) !== -1 ) {
value = createFromMultilineElement( {
element: node,
range,
multilineTag,
multilineWrapperTags,
removeNode,
unwrapNode,
removeAttribute,
currentWrapperTags: [ ...currentWrapperTags, format ],
} );
format = undefined;
Expand All @@ -376,9 +342,6 @@ function createFromElement( {
range,
multilineTag,
multilineWrapperTags,
removeNode,
unwrapNode,
removeAttribute,
} );
}

Expand Down Expand Up @@ -446,12 +409,6 @@ function createFromElement( {
* multiline.
* @param {?Array} $1.multilineWrapperTags Tags where lines can be found if
* nesting is possible.
* @param {?Function} $1.removeNode Function to declare whether the
* given node should be removed.
* @param {?Function} $1.unwrapNode Function to declare whether the
* given node should be unwrapped.
* @param {?Function} $1.removeAttribute Wether to remove an attribute
* based on the name.
* @param {boolean} $1.currentWrapperTags Whether to prepend a line
* separator.
*
Expand All @@ -462,9 +419,6 @@ function createFromMultilineElement( {
range,
multilineTag,
multilineWrapperTags,
removeNode,
unwrapNode,
removeAttribute,
currentWrapperTags = [],
} ) {
const accumulator = createEmptyValue();
Expand All @@ -489,9 +443,6 @@ function createFromMultilineElement( {
multilineTag,
multilineWrapperTags,
currentWrapperTags,
removeNode,
unwrapNode,
removeAttribute,
} );

// If a line consists of one single line break (invisible), consider the
Expand Down Expand Up @@ -532,16 +483,11 @@ function createFromMultilineElement( {
*
* @param {Object} $1 Named argements.
* @param {Element} $1.element Element to get attributes from.
* @param {?Function} $1.removeAttribute Wether to remove an attribute based on
* the name.
*
* @return {?Object} Attribute object or `undefined` if the element has no
* attributes.
*/
function getAttributes( {
element,
removeAttribute,
} ) {
function getAttributes( { element } ) {
if ( ! element.hasAttributes() ) {
return;
}
Expand All @@ -553,7 +499,7 @@ function getAttributes( {
for ( let i = 0; i < length; i++ ) {
const { name, value } = element.attributes[ i ];

if ( removeAttribute && removeAttribute( name ) ) {
if ( name === 'data-rich-text-format-boundary' ) {
continue;
}

Expand Down
Loading

0 comments on commit 86de3a9

Please sign in to comment.