Skip to content
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

Add Editable placeholders #756

Merged
merged 1 commit into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions blocks/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function createElement( type, props, ...children ) {
}

export default class Editable extends wp.element.Component {
constructor() {
constructor( props ) {
super( ...arguments );

this.onInit = this.onInit.bind( this );
Expand All @@ -73,10 +73,13 @@ export default class Editable extends wp.element.Component {
this.onNodeChange = this.onNodeChange.bind( this );
this.onKeyDown = this.onKeyDown.bind( this );
this.changeFormats = this.changeFormats.bind( this );
this.onSelectionChange = this.onSelectionChange.bind( this );

this.state = {
formats: {},
alignment: null,
bookmark: null
bookmark: null,
empty: ! props.value || ! props.value.length
};
}

Expand All @@ -88,6 +91,7 @@ export default class Editable extends wp.element.Component {
editor.on( 'focusin', this.onFocus );
editor.on( 'nodechange', this.onNodeChange );
editor.on( 'keydown', this.onKeyDown );
editor.on( 'selectionChange', this.onSelectionChange );
}

onInit() {
Expand All @@ -103,6 +107,23 @@ export default class Editable extends wp.element.Component {
this.props.onFocus();
}

isActive() {
return document.activeElement === this.editor.getBody();
}

onSelectionChange() {
// We must check this because selectionChange is a global event.
if ( ! this.isActive() ) {
return;
}

const content = this.getContent();

this.setState( {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably shouldn't be calling this all the time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this would trigger a re-render if the value is the same?

This performs a shallow merge of stateChange into the new state.
https://facebook.github.io/react/docs/react-component.html#setstate

I do notice a lot of re-renders form node change though, not from this one.

Copy link
Member

@aduth aduth May 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this would trigger a re-render if the value is the same?

Yes it does: https://codepen.io/aduth/pen/mmXgxQ

We'd need to expose and use React.PureComponent for it to not: https://facebook.github.io/react/docs/react-api.html#react.purecomponent

Alternatively just compare against state here and return; early if it would end up being the same.

empty: ! content || ! content.length
} );
}

onChange() {
if ( ! this.editor.isDirty() ) {
return;
Expand Down Expand Up @@ -349,7 +370,8 @@ export default class Editable extends wp.element.Component {
showAlignments = false,
inlineToolbar = false,
inline,
formattingControls
formattingControls,
placeholder
} = this.props;

// Generating a key that includes `tagName` ensures that if the tag
Expand Down Expand Up @@ -397,6 +419,8 @@ export default class Editable extends wp.element.Component {
settings={ {
forced_root_block: inline ? false : 'p'
} }
isEmpty={ this.state.empty }
placeholder={ placeholder }
key={ key } />
</div>
);
Expand Down
15 changes: 15 additions & 0 deletions blocks/editable/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@
background: $light-gray-400;
}

&[data-is-empty="true"] {
position: relative;
}

&[data-is-empty="true"]:before {
content: attr( data-placeholder );
opacity: 0.5;
pointer-events: none;
position: absolute;
}
}

.block-editable__inline-toolbar {
Expand All @@ -57,6 +67,11 @@ figcaption.blocks-editable__tinymce {
color: $dark-gray-100;
text-align: center;
font-size: $default-font-size;

&:before {
left: 50%;
transform: translateX( -50% );
}
}


Expand Down
13 changes: 11 additions & 2 deletions blocks/editable/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ export default class TinyMCE extends wp.element.Component {
return false;
}

componentWillReceiveProps( nextProps ) {
const isEmpty = String( nextProps.isEmpty );

if ( this.editorNode.getAttribute( 'data-is-empty' ) !== isEmpty ) {
this.editorNode.setAttribute( 'data-is-empty', isEmpty );
}
}

componentWillUnmount() {
if ( ! this.editor ) {
return;
Expand Down Expand Up @@ -47,7 +55,7 @@ export default class TinyMCE extends wp.element.Component {
}

render() {
const { tagName = 'div', style, defaultValue } = this.props;
const { tagName = 'div', style, defaultValue, placeholder } = this.props;

// If a default value is provided, render it into the DOM even before
// TinyMCE finishes initializing. This avoids a short delay by allowing
Expand All @@ -62,7 +70,8 @@ export default class TinyMCE extends wp.element.Component {
contentEditable: true,
suppressContentEditableWarning: true,
className: 'blocks-editable__tinymce',
style
style,
'data-placeholder': placeholder
}, children );
}
}