-
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
WIP: Add inline image resizing UI #13737
Changes from all commits
7f864b9
2bae7d7
846a4e5
38cb03d
dfc532c
f96dbe0
9a68817
b60bd4a
d03c77f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,16 +1,19 @@ | ||||||
/** | ||||||
* WordPress dependencies | ||||||
*/ | ||||||
import { Path, SVG } from '@wordpress/components'; | ||||||
import { Path, SVG, TextControl, Popover, IconButton, PositionedAtSelection } from '@wordpress/components'; | ||||||
import { __ } from '@wordpress/i18n'; | ||||||
import { Component } from '@wordpress/element'; | ||||||
import { insertObject } from '@wordpress/rich-text'; | ||||||
import { MediaUpload, RichTextInserterItem, MediaUploadCheck } from '@wordpress/editor'; | ||||||
import { LEFT, RIGHT, UP, DOWN, BACKSPACE, ENTER } from '@wordpress/keycodes'; | ||||||
|
||||||
const ALLOWED_MEDIA_TYPES = [ 'image' ]; | ||||||
|
||||||
const name = 'core/image'; | ||||||
|
||||||
const stopKeyPropagation = ( event ) => event.stopPropagation(); | ||||||
|
||||||
export const image = { | ||||||
name, | ||||||
title: __( 'Image' ), | ||||||
|
@@ -27,13 +30,46 @@ export const image = { | |||||
edit: class ImageEdit extends Component { | ||||||
constructor() { | ||||||
super( ...arguments ); | ||||||
this.onChange = this.onChange.bind( this ); | ||||||
this.onKeyDown = this.onKeyDown.bind( this ); | ||||||
this.openModal = this.openModal.bind( this ); | ||||||
this.closeModal = this.closeModal.bind( this ); | ||||||
this.state = { | ||||||
modal: false, | ||||||
}; | ||||||
} | ||||||
|
||||||
static getDerivedStateFromProps( props, state ) { | ||||||
const { activeAttributes: { style } } = props; | ||||||
|
||||||
if ( style === state.previousStyle ) { | ||||||
return null; | ||||||
} | ||||||
|
||||||
if ( ! style ) { | ||||||
return { | ||||||
width: undefined, | ||||||
previousStyle: style, | ||||||
}; | ||||||
} | ||||||
|
||||||
return { | ||||||
width: style.replace( /\D/g, '' ), | ||||||
previousStyle: style, | ||||||
}; | ||||||
} | ||||||
|
||||||
onChange( width ) { | ||||||
this.setState( { width } ); | ||||||
} | ||||||
|
||||||
onKeyDown( event ) { | ||||||
if ( [ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].indexOf( event.keyCode ) > -1 ) { | ||||||
// Stop the key event from propagating up to ObserveTyping.startTypingInTextField. | ||||||
event.stopPropagation(); | ||||||
} | ||||||
} | ||||||
|
||||||
openModal() { | ||||||
this.setState( { modal: true } ); | ||||||
} | ||||||
|
@@ -43,7 +79,11 @@ export const image = { | |||||
} | ||||||
|
||||||
render() { | ||||||
const { value, onChange } = this.props; | ||||||
const { value, onChange, isActive, activeAttributes } = this.props; | ||||||
const { style } = activeAttributes; | ||||||
// Rerender PositionedAtSelection when the selection changes or when | ||||||
// the width changes. | ||||||
const key = value.start + style; | ||||||
|
||||||
return ( | ||||||
<MediaUploadCheck> | ||||||
|
@@ -73,6 +113,50 @@ export const image = { | |||||
return null; | ||||||
} } | ||||||
/> } | ||||||
{ isActive && <PositionedAtSelection key={ key }> | ||||||
<Popover | ||||||
position="bottom center" | ||||||
focusOnMount={ false } | ||||||
> | ||||||
{ // Disable reason: KeyPress must be suppressed so the block doesn't hide the toolbar | ||||||
/* eslint-disable jsx-a11y/no-noninteractive-element-interactions */ } | ||||||
<form | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we move some of this markup into its own component? |
||||||
className="editor-format-toolbar__image-container-content" | ||||||
onKeyPress={ stopKeyPropagation } | ||||||
onKeyDown={ this.onKeyDown } | ||||||
onSubmit={ ( event ) => { | ||||||
const newFormats = value.formats.slice( 0 ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: The
Suggested change
|
||||||
|
||||||
newFormats[ value.start ] = [ { | ||||||
type: name, | ||||||
object: true, | ||||||
attributes: { | ||||||
...activeAttributes, | ||||||
style: `width: ${ this.state.width }px;`, | ||||||
}, | ||||||
} ]; | ||||||
|
||||||
onChange( { | ||||||
...value, | ||||||
formats: newFormats, | ||||||
} ); | ||||||
|
||||||
event.preventDefault(); | ||||||
} } | ||||||
> | ||||||
<TextControl | ||||||
className="editor-format-toolbar__image-container-value" | ||||||
type="number" | ||||||
label={ __( 'Width' ) } | ||||||
value={ this.state.width } | ||||||
min={ 1 } | ||||||
onChange={ this.onChange } | ||||||
/> | ||||||
<IconButton icon="editor-break" label={ __( 'Apply' ) } type="submit" /> | ||||||
</form> | ||||||
{ /* eslint-enable jsx-a11y/no-noninteractive-element-interactions */ } | ||||||
</Popover> | ||||||
</PositionedAtSelection> } | ||||||
</MediaUploadCheck> | ||||||
); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.editor-format-toolbar__image-container-content { | ||
display: flex; | ||
|
||
.components-icon-button { | ||
height: $icon-button-size + $grid-size + $grid-size; | ||
align-self: flex-end; | ||
} | ||
} | ||
|
||
.editor-format-toolbar__image-container-value { | ||
margin: $grid-size - $border-width; | ||
flex-grow: 1; | ||
flex-shrink: 1; | ||
white-space: nowrap; | ||
min-width: 150px; | ||
max-width: 500px; | ||
|
||
&.components-base-control .components-base-control__field { | ||
margin-bottom: 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
@import "./image/style.scss"; | ||
@import "./link/style.scss"; |
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.
Should we
export default
to be consistent with the other components inpackages/components/src
?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.
d03c77f
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.
Thanks!