-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block library: Extract
deprecated
field to their own files (p.2) (#…
- Loading branch information
Showing
6 changed files
with
350 additions
and
235 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
import { isFinite, omit } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
RawHTML, | ||
} from '@wordpress/element'; | ||
import { | ||
getColorClassName, | ||
RichText, | ||
} from '@wordpress/block-editor'; | ||
|
||
const supports = { | ||
className: false, | ||
}; | ||
|
||
const blockAttributes = { | ||
align: { | ||
type: 'string', | ||
}, | ||
content: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'p', | ||
default: '', | ||
}, | ||
dropCap: { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
placeholder: { | ||
type: 'string', | ||
}, | ||
textColor: { | ||
type: 'string', | ||
}, | ||
customTextColor: { | ||
type: 'string', | ||
}, | ||
backgroundColor: { | ||
type: 'string', | ||
}, | ||
customBackgroundColor: { | ||
type: 'string', | ||
}, | ||
fontSize: { | ||
type: 'string', | ||
}, | ||
customFontSize: { | ||
type: 'number', | ||
}, | ||
direction: { | ||
type: 'string', | ||
enum: [ 'ltr', 'rtl' ], | ||
}, | ||
}; | ||
|
||
const deprecated = [ | ||
{ | ||
supports, | ||
attributes: { | ||
...blockAttributes, | ||
width: { | ||
type: 'string', | ||
}, | ||
}, | ||
save( { attributes } ) { | ||
const { | ||
width, | ||
align, | ||
content, | ||
dropCap, | ||
backgroundColor, | ||
textColor, | ||
customBackgroundColor, | ||
customTextColor, | ||
fontSize, | ||
customFontSize, | ||
} = attributes; | ||
|
||
const textClass = getColorClassName( 'color', textColor ); | ||
const backgroundClass = getColorClassName( 'background-color', backgroundColor ); | ||
const fontSizeClass = fontSize && `is-${ fontSize }-text`; | ||
|
||
const className = classnames( { | ||
[ `align${ width }` ]: width, | ||
'has-background': backgroundColor || customBackgroundColor, | ||
'has-drop-cap': dropCap, | ||
[ fontSizeClass ]: fontSizeClass, | ||
[ textClass ]: textClass, | ||
[ backgroundClass ]: backgroundClass, | ||
} ); | ||
|
||
const styles = { | ||
backgroundColor: backgroundClass ? undefined : customBackgroundColor, | ||
color: textClass ? undefined : customTextColor, | ||
fontSize: fontSizeClass ? undefined : customFontSize, | ||
textAlign: align, | ||
}; | ||
|
||
return ( | ||
<RichText.Content | ||
tagName="p" | ||
style={ styles } | ||
className={ className ? className : undefined } | ||
value={ content } | ||
/> | ||
); | ||
}, | ||
}, | ||
{ | ||
supports, | ||
attributes: omit( { | ||
...blockAttributes, | ||
fontSize: { | ||
type: 'number', | ||
}, | ||
}, 'customFontSize', 'customTextColor', 'customBackgroundColor' ), | ||
save( { attributes } ) { | ||
const { width, align, content, dropCap, backgroundColor, textColor, fontSize } = attributes; | ||
const className = classnames( { | ||
[ `align${ width }` ]: width, | ||
'has-background': backgroundColor, | ||
'has-drop-cap': dropCap, | ||
} ); | ||
const styles = { | ||
backgroundColor, | ||
color: textColor, | ||
fontSize, | ||
textAlign: align, | ||
}; | ||
|
||
return <p style={ styles } className={ className ? className : undefined }>{ content }</p>; | ||
}, | ||
migrate( attributes ) { | ||
return omit( { | ||
...attributes, | ||
customFontSize: isFinite( attributes.fontSize ) ? attributes.fontSize : undefined, | ||
customTextColor: attributes.textColor && '#' === attributes.textColor[ 0 ] ? attributes.textColor : undefined, | ||
customBackgroundColor: attributes.backgroundColor && '#' === attributes.backgroundColor[ 0 ] ? attributes.backgroundColor : undefined, | ||
}, [ 'fontSize', 'textColor', 'backgroundColor' ] ); | ||
}, | ||
}, | ||
{ | ||
supports, | ||
attributes: { | ||
...blockAttributes, | ||
content: { | ||
type: 'string', | ||
source: 'html', | ||
default: '', | ||
}, | ||
}, | ||
save( { attributes } ) { | ||
return <RawHTML>{ attributes.content }</RawHTML>; | ||
}, | ||
migrate( attributes ) { | ||
return attributes; | ||
}, | ||
}, | ||
]; | ||
|
||
export default deprecated; |
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,74 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { RichText } from '@wordpress/block-editor'; | ||
|
||
const blockAttributes = { | ||
value: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'blockquote', | ||
multiline: 'p', | ||
}, | ||
citation: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'cite', | ||
default: '', | ||
}, | ||
mainColor: { | ||
type: 'string', | ||
}, | ||
customMainColor: { | ||
type: 'string', | ||
}, | ||
textColor: { | ||
type: 'string', | ||
}, | ||
customTextColor: { | ||
type: 'string', | ||
}, | ||
}; | ||
|
||
const deprecated = [ | ||
{ | ||
attributes: { | ||
...blockAttributes, | ||
}, | ||
save( { attributes } ) { | ||
const { value, citation } = attributes; | ||
return ( | ||
<blockquote> | ||
<RichText.Content value={ value } multiline /> | ||
{ ! RichText.isEmpty( citation ) && <RichText.Content tagName="cite" value={ citation } /> } | ||
</blockquote> | ||
); | ||
}, | ||
}, { | ||
attributes: { | ||
...blockAttributes, | ||
citation: { | ||
type: 'string', | ||
source: 'html', | ||
selector: 'footer', | ||
}, | ||
align: { | ||
type: 'string', | ||
default: 'none', | ||
}, | ||
}, | ||
|
||
save( { attributes } ) { | ||
const { value, citation, align } = attributes; | ||
|
||
return ( | ||
<blockquote className={ `align${ align }` }> | ||
<RichText.Content value={ value } multiline /> | ||
{ ! RichText.isEmpty( citation ) && <RichText.Content tagName="footer" value={ citation } /> } | ||
</blockquote> | ||
); | ||
}, | ||
}, | ||
]; | ||
|
||
export default deprecated; |
Oops, something went wrong.