-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editor Chrome: Adding the editor mode switcher #339
Merged
+293
−72
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4897fae
Editor Chrome: Adding the editor mode switcher
youknowriad 28f3721
- Renaming content prop to initialContent and prefilling the state in…
youknowriad 7f086d4
Using function instead of arrow functions for module level functions
youknowriad 337fcb5
Fix linting errors
youknowriad 52461da
Fix running the unit tests locally
youknowriad 08a46f4
Consolidate onChange content logic
youknowriad 9f3e225
Fix Text Mode onChange
youknowriad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,50 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import ModeSwitcher from './mode-switcher'; | ||
import EditorText from './mode/text'; | ||
import EditorVisual from './mode/visual'; | ||
|
||
class Layout extends wp.element.Component { | ||
constructor( props ) { | ||
super( ...arguments ); | ||
this.switchMode = this.switchMode.bind( this ); | ||
this.state = { | ||
mode: 'visual', | ||
html: props.initialContent, | ||
blocks: wp.blocks.parse( this.props.initialContent ) | ||
}; | ||
} | ||
|
||
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 | ||
} ); | ||
} | ||
|
||
createChangeHandler( type ) { | ||
return ( value ) => this.setState( { [ type ]: value } ); | ||
} | ||
|
||
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.createChangeHandler( 'html' ) } /> } | ||
{ mode === 'visual' && <EditorVisual blocks={ blocks } onChange={ this.createChangeHandler( 'blocks' ) } /> } | ||
</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,53 @@ | ||
class ModeSwitcher extends wp.element.Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.toggle = this.toggle.bind( this ); | ||
this.state = { | ||
opened: false | ||
}; | ||
} | ||
|
||
toggle() { | ||
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 } type="button" 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,13 @@ | ||
function Text( { html, onChange } ) { | ||
const changeValue = ( event ) => { | ||
onChange( event.target.value ); | ||
}; | ||
|
||
return ( | ||
<div className="editor-mode-text"> | ||
<textarea value={ html } onChange={ changeValue } /> | ||
</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'; | ||
|
||
function 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; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably start thinking about localization sooner than later. I might focus on this today (for a separate pull request).