-
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.
Editor Chrome: Adding the editor mode switcher
- Adding the `onChange` content handler - v1 of the editor header - Adding a v1 the text editor
- Loading branch information
1 parent
ca4111c
commit b4ca382
Showing
11 changed files
with
302 additions
and
67 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,2 @@ | ||
$gray: #575D65; | ||
$light-gray: #e0e5e9; |
This file was deleted.
Oops, something went wrong.
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,67 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ModeSwitcher from './mode-switcher'; | ||
import EditorText from './mode/text'; | ||
import EditorVisual from './mode/visual'; | ||
|
||
class Layout extends wp.element.Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.switchMode = this.switchMode.bind( this ); | ||
this.changeHtml = this.changeHtml.bind( this ); | ||
this.changeBlocks = this.changeBlocks.bind( this ); | ||
this.state = { | ||
mode: 'visual', | ||
html: '', | ||
blocks: [] | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.setState( { | ||
html: this.props.content, | ||
blocks: wp.blocks.parse( this.props.content ) | ||
} ); | ||
} | ||
|
||
switchMode( newMode ) { | ||
// TODO: we need a serializer from blocks here | ||
const html = this.state.html; | ||
const blocks = this.mode === 'visual' ? this.state.blocks : wp.blocks.parse( this.state.html ); | ||
this.setState( { | ||
mode: newMode, | ||
html, | ||
blocks | ||
} ); | ||
} | ||
|
||
changeHtml( html ) { | ||
this.setState( { | ||
html | ||
} ); | ||
} | ||
|
||
changeBlocks( blocks ) { | ||
this.setState( { | ||
blocks | ||
} ); | ||
} | ||
|
||
render() { | ||
const { mode, html, blocks } = this.state; | ||
return ( | ||
<div> | ||
<div className="editor__header"> | ||
<ModeSwitcher mode={ mode } onSwitch={ this.switchMode } /> | ||
</div> | ||
<div className="editor__body"> | ||
{ mode === 'text' && <EditorText html={ html } onChange={ this.changeHtml } /> } | ||
{ mode === 'visual' && <EditorVisual blocks={ blocks } onChange={ this.changeBlocks } /> } | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Layout; |
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,54 @@ | ||
class ModeSwitcher extends wp.element.Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.toggle = this.toggle.bind( this ); | ||
this.state = { | ||
opened: false | ||
}; | ||
} | ||
|
||
toggle( event ) { | ||
event.preventDefault(); | ||
this.setState( { | ||
opened: ! this.state.opened | ||
} ); | ||
} | ||
|
||
render() { | ||
const { opened } = this.state; | ||
const modes = [ | ||
{ value: 'visual', label: 'Visual' }, | ||
{ value: 'text', label: 'Text' }, | ||
]; | ||
const switchMode = ( mode ) => () => { | ||
this.setState( { opened: false } ); | ||
this.props.onSwitch( mode ); | ||
}; | ||
const currentMode = modes.find( ( { value } ) => value === this.props.mode ); | ||
|
||
return ( | ||
<div className="editor-mode-switcher"> | ||
<button | ||
className="editor-mode-switcher__toggle" | ||
onClick={ this.toggle } | ||
aria-label="Switch the editor mode" | ||
> | ||
{ currentMode.label } | ||
<span className="dashicons dashicons-arrow-down" /> | ||
</button> | ||
{ opened && | ||
<div className="editor-mode-switcher__content"> | ||
<div className="editor-mode-switcher__arrow" /> | ||
{ modes.map( ( mode ) => | ||
<button key={ mode.value } onClick={ switchMode( mode.value ) }> | ||
{ mode.label } | ||
</button> | ||
) } | ||
</div> | ||
} | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default ModeSwitcher; |
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,9 @@ | ||
const Text = ( { html, onChange } ) => { | ||
return ( | ||
<div className="editor-mode-text"> | ||
<textarea value={ html } onChange={ onChange } /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Text; |
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,34 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import InserterButton from '../../inserter/button'; | ||
|
||
const Blocks = ( { blocks, onChange } ) => { | ||
const onChangeBlock = ( index ) => ( changes ) => { | ||
const newBlock = { | ||
...blocks[ index ], | ||
changes | ||
}; | ||
|
||
onChange( [ | ||
...blocks.slice( 0, index ), | ||
newBlock, | ||
...blocks.slice( index + 1 ) | ||
] ); | ||
}; | ||
|
||
return ( | ||
<div className="editor-mode-visual"> | ||
<div> | ||
{ blocks.map( ( block, index ) => | ||
<div key={ index }> | ||
{ wp.blocks.getBlockSettings( block.blockType ).edit( block.attributes, onChangeBlock( index ) ) } | ||
</div> | ||
) } | ||
</div> | ||
<InserterButton /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Blocks; |
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,85 @@ | ||
.editor__header { | ||
padding: 10px; | ||
border-bottom: 1px solid $light-gray; | ||
} | ||
|
||
.editor-mode-switcher { | ||
position: relative; | ||
} | ||
|
||
.editor-mode-switcher__toggle { | ||
color: $gray; | ||
padding: 8px; | ||
align-items: center; | ||
cursor: pointer; | ||
border: none; | ||
background: none; | ||
border-right: 1px solid $light-gray; | ||
vertical-align: middle; | ||
line-height: 20px; | ||
outline: none; | ||
} | ||
|
||
.editor-mode-switcher__content { | ||
position: absolute; | ||
top: 40px; | ||
border: 1px solid $light-gray; | ||
color: $gray; | ||
min-width: 100px; | ||
|
||
button { | ||
padding: 8px; | ||
display: block; | ||
border: none; | ||
background: white; | ||
outline: none; | ||
cursor: pointer; | ||
width: 100%; | ||
} | ||
} | ||
|
||
.editor-mode-switcher__arrow { | ||
border: 10px dashed $light-gray; | ||
height: 0; | ||
line-height: 0; | ||
position: absolute; | ||
width: 0; | ||
z-index: 1; | ||
top: -10px; | ||
left: 50%; | ||
margin-left: -10px; | ||
border-bottom-style: solid; | ||
border-top: none; | ||
border-left-color: transparent; | ||
border-right-color: transparent; | ||
|
||
&:before { | ||
top: 2px; | ||
border: 10px solid white; | ||
content: " "; | ||
position: absolute; | ||
left: 50%; | ||
margin-left: -10px; | ||
border-bottom-style: solid; | ||
border-top: none; | ||
border-left-color: transparent; | ||
border-right-color: transparent; | ||
} | ||
} | ||
|
||
.editor-mode-text { | ||
padding: 60px; | ||
width: 700px; | ||
margin: 0 auto; | ||
|
||
textarea { | ||
width: 100%; | ||
min-height: 300px; | ||
} | ||
} | ||
|
||
.editor-mode-visual { | ||
padding: 60px; | ||
width: 700px; | ||
margin: 0 auto; | ||
} |
Oops, something went wrong.