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

RichText: Fix browser formatting buttons #13833

Merged
merged 3 commits into from
Feb 18, 2019
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
1 change: 1 addition & 0 deletions packages/editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
RichTextShortcut,
RichTextToolbarButton,
RichTextInserterItem,
RichTextInputEvent,
} from './rich-text';
export { default as ServerSideRender } from './server-side-render';
export { default as MediaPlaceholder } from './media-placeholder';
Expand Down
16 changes: 16 additions & 0 deletions packages/editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,21 @@ export class RichText extends Component {
return;
}

if ( event ) {
const { inputType } = event.nativeEvent;

// The browser formatted something or tried to insert a list.
// Overwrite it. It will be handled later by the format library if
// needed.
if (
inputType.indexOf( 'format' ) === 0 ||
( inputType.indexOf( 'insert' ) === 0 && inputType !== 'insertText' )
) {
this.applyRecord( this.getRecord() );
return;
}
}

let { selectedFormat } = this.state;
const { formats, text, start, end } = this.patterns.reduce(
( accumlator, transform ) => transform( accumlator ),
Expand Down Expand Up @@ -1104,3 +1119,4 @@ export default RichTextContainer;
export { RichTextShortcut } from './shortcut';
export { RichTextToolbarButton } from './toolbar-button';
export { RichTextInserterItem } from './inserter-list-item';
export { RichTextInputEvent } from './input-event';
30 changes: 30 additions & 0 deletions packages/editor/src/components/rich-text/input-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';

export class RichTextInputEvent extends Component {
Copy link
Member

Choose a reason for hiding this comment

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

A unit test for this component could be good.

Copy link
Member Author

Choose a reason for hiding this comment

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

How would it work?

Copy link
Member

@noisysocks noisysocks Feb 18, 2019

Choose a reason for hiding this comment

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

Something like this pseudocode:

const onInput = jest.fn();

mount( <RichTextInputEvent inputType="ella-is-cool" onInput={ onInput } /> );

const event = new Event( 'input' );
event.inputType = 'ella-is-cool';
document.dispatchEvent( event );

expect( onInput ).toHaveBeenCalled();

I find these sorts of tests don't hurt and sometimes catch silly regressions e.g. typos.

constructor() {
super( ...arguments );

this.onInput = this.onInput.bind( this );
}

onInput( event ) {
if ( event.inputType === this.props.inputType ) {
this.props.onInput();
Copy link
Member

Choose a reason for hiding this comment

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

Is there a need to preventDefault as well, to ensure it's fully controlled?

Copy link
Member Author

Choose a reason for hiding this comment

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

There's not really anything to prevent. The input event happens after the fact.

Copy link
Member

Choose a reason for hiding this comment

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

There's not really anything to prevent. The input event happens after the fact.

And to that point, an input event is in-fact not cancelable.

https://w3c.github.io/uievents/#event-type-input

}
}

componentDidMount() {
document.addEventListener( 'input', this.onInput, true );
}

componentWillUnmount() {
document.removeEventListener( 'input', this.onInput, true );
}

render() {
return null;
}
}
6 changes: 5 additions & 1 deletion packages/format-library/src/bold/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';
import { toggleFormat } from '@wordpress/rich-text';
import { RichTextToolbarButton, RichTextShortcut } from '@wordpress/editor';
import { RichTextToolbarButton, RichTextShortcut, RichTextInputEvent } from '@wordpress/editor';

const name = 'core/bold';

Expand Down Expand Up @@ -32,6 +32,10 @@ export const bold = {
shortcutType="primary"
shortcutCharacter="b"
/>
<RichTextInputEvent
inputType="formatBold"
onInput={ onToggle }
/>
</Fragment>
);
},
Expand Down
6 changes: 5 additions & 1 deletion packages/format-library/src/italic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';
import { toggleFormat } from '@wordpress/rich-text';
import { RichTextToolbarButton, RichTextShortcut } from '@wordpress/editor';
import { RichTextToolbarButton, RichTextShortcut, RichTextInputEvent } from '@wordpress/editor';

const name = 'core/italic';

Expand Down Expand Up @@ -32,6 +32,10 @@ export const italic = {
shortcutType="primary"
shortcutCharacter="i"
/>
<RichTextInputEvent
inputType="formatItalic"
onInput={ onToggle }
/>
</Fragment>
);
},
Expand Down
6 changes: 5 additions & 1 deletion packages/format-library/src/underline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { __ } from '@wordpress/i18n';
import { Fragment } from '@wordpress/element';
import { toggleFormat } from '@wordpress/rich-text';
import { RichTextShortcut } from '@wordpress/editor';
import { RichTextShortcut, RichTextInputEvent } from '@wordpress/editor';

const name = 'core/underline';

Expand Down Expand Up @@ -34,6 +34,10 @@ export const underline = {
character="u"
onUse={ onToggle }
/>
<RichTextInputEvent
inputType="formatUnderline"
onInput={ onToggle }
/>
</Fragment>
);
},
Expand Down