-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Components: Extract a reusable PostTextEditor componeent
- Loading branch information
1 parent
7531665
commit 0d0caf1
Showing
7 changed files
with
184 additions
and
155 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
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 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import Textarea from 'react-autosize-textarea'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { parse } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { getEditedPostContent } from '../../selectors'; | ||
import { editPost, resetBlocks } from '../../actions'; | ||
|
||
class PostTextEditor extends Component { | ||
constructor( props ) { | ||
super( ...arguments ); | ||
|
||
this.onChange = this.onChange.bind( this ); | ||
this.onPersist = this.onPersist.bind( this ); | ||
|
||
this.state = { | ||
initialValue: props.value, | ||
}; | ||
} | ||
|
||
onChange( event ) { | ||
this.props.onChange( event.target.value ); | ||
} | ||
|
||
onPersist( event ) { | ||
const { value } = event.target; | ||
if ( value !== this.state.initialValue ) { | ||
this.props.onPersist( value ); | ||
|
||
this.setState( { | ||
initialValue: value, | ||
} ); | ||
} | ||
} | ||
|
||
render() { | ||
const { value } = this.props; | ||
|
||
return ( | ||
<Textarea | ||
autoComplete="off" | ||
value={ value } | ||
onChange={ this.onChange } | ||
onBlur={ this.onPersist } | ||
className="editor-post-text-editor" | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export default connect( | ||
( state ) => ( { | ||
value: getEditedPostContent( state ), | ||
} ), | ||
{ | ||
onChange( content ) { | ||
return editPost( { content } ); | ||
}, | ||
onPersist( content ) { | ||
return resetBlocks( parse( content ) ); | ||
}, | ||
} | ||
)( PostTextEditor ); |
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,63 @@ | ||
.editor-post-text-editor { | ||
display: block; | ||
margin: 0; | ||
width: 100%; | ||
border: none; | ||
outline: none; | ||
box-shadow: none; | ||
resize: none; | ||
overflow: hidden; | ||
font-family: $editor-html-font; | ||
font-size: $text-editor-font-size; | ||
line-height: 150%; | ||
|
||
&:focus { | ||
box-shadow: none; | ||
} | ||
} | ||
|
||
.editor-post-text-editor__toolbar { | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
|
||
button { | ||
height: 30px; | ||
border: none; | ||
background: none; | ||
padding: 0 8px; | ||
margin: 3px 4px; | ||
text-align: center; | ||
cursor: pointer; | ||
font-family: $editor-html-font; | ||
color: $dark-gray-500; | ||
border: 1px solid transparent; | ||
|
||
&:first-child { | ||
margin-left: 0; | ||
} | ||
|
||
&:hover, | ||
&:focus { | ||
outline: none; | ||
border: 1px solid $dark-gray-500; | ||
} | ||
} | ||
} | ||
|
||
.editor-post-text-editor__bold { | ||
font-weight: bold; | ||
} | ||
|
||
.editor-post-text-editor__italic { | ||
font-style: italic; | ||
} | ||
|
||
.editor-post-text-editor__link { | ||
text-decoration: underline; | ||
color: $blue-medium-500; | ||
} | ||
|
||
.editor-post-text-editor__del { | ||
text-decoration:line-through; | ||
} |
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 @@ | ||
function PostTextEditorToolbar() { | ||
return ( | ||
<div className="editor-post-text-editor__toolbar"> | ||
<button className="editor-post-text-editor__bold">b</button> | ||
<button className="editor-post-text-editor__italic">i</button> | ||
<button className="editor-post-text-editor__link">link</button> | ||
<button>b-quote</button> | ||
<button className="editor-post-text-editor__del">del</button> | ||
<button>ins</button> | ||
<button>img</button> | ||
<button>ul</button> | ||
<button>ol</button> | ||
<button>li</button> | ||
<button>code</button> | ||
<button>more</button> | ||
<button>close tags</button> | ||
</div> | ||
); | ||
} | ||
|
||
export default PostTextEditorToolbar; |
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,97 +1,22 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { connect } from 'react-redux'; | ||
import Textarea from 'react-autosize-textarea'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { parse } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './style.scss'; | ||
import { PostTextEditor, PostTextEditorToolbar } from '../../components'; | ||
import PostTitle from '../../post-title'; | ||
import { getEditedPostContent } from '../../selectors'; | ||
import { editPost, resetBlocks } from '../../actions'; | ||
|
||
class TextEditor extends Component { | ||
constructor( props ) { | ||
super( ...arguments ); | ||
|
||
this.onChange = this.onChange.bind( this ); | ||
this.onPersist = this.onPersist.bind( this ); | ||
|
||
this.state = { | ||
initialValue: props.value, | ||
}; | ||
} | ||
|
||
onChange( event ) { | ||
this.props.onChange( event.target.value ); | ||
} | ||
|
||
onPersist( event ) { | ||
const { value } = event.target; | ||
if ( value !== this.state.initialValue ) { | ||
this.props.onPersist( value ); | ||
|
||
this.setState( { | ||
initialValue: value, | ||
} ); | ||
} | ||
} | ||
|
||
render() { | ||
const { value } = this.props; | ||
|
||
return ( | ||
<div className="editor-text-editor"> | ||
<div className="editor-text-editor__formatting"> | ||
<div className="editor-text-editor__formatting-group"> | ||
<button className="editor-text-editor__bold">b</button> | ||
<button className="editor-text-editor__italic">i</button> | ||
<button className="editor-text-editor__link">link</button> | ||
<button>b-quote</button> | ||
<button className="editor-text-editor__del">del</button> | ||
<button>ins</button> | ||
<button>img</button> | ||
<button>ul</button> | ||
<button>ol</button> | ||
<button>li</button> | ||
<button>code</button> | ||
<button>more</button> | ||
<button>close tags</button> | ||
</div> | ||
</div> | ||
<div className="editor-text-editor__body"> | ||
<PostTitle /> | ||
<Textarea | ||
autoComplete="off" | ||
value={ value } | ||
onChange={ this.onChange } | ||
onBlur={ this.onPersist } | ||
className="editor-text-editor__textarea" | ||
/> | ||
</div> | ||
function TextEditor() { | ||
return ( | ||
<div className="editor-text-editor"> | ||
<div className="editor-text-editor__formatting"> | ||
<PostTextEditorToolbar /> | ||
</div> | ||
<div className="editor-text-editor__body"> | ||
<PostTitle /> | ||
<PostTextEditor /> | ||
</div> | ||
); | ||
} | ||
</div> | ||
); | ||
} | ||
|
||
export default connect( | ||
( state ) => ( { | ||
value: getEditedPostContent( state ), | ||
} ), | ||
{ | ||
onChange( content ) { | ||
return editPost( { content } ); | ||
}, | ||
onPersist( content ) { | ||
return resetBlocks( parse( content ) ); | ||
}, | ||
} | ||
)( TextEditor ); | ||
export default TextEditor; |
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