Skip to content

Commit

Permalink
Add experimental SimpleRichTextEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
sstur committed Jul 2, 2016
1 parent 06972bc commit 06ed718
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/SimpleRichTextEditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* @flow */
import React, {Component} from 'react';
import RichTextEditor, {createEmptyValue} from './RichTextEditor';
import autobind from 'class-autobind';

import type {EditorValue} from './RichTextEditor';

type Props = {
value: string;
format: string;
onChange: (value: string) => any;
};
type State = {
editorValue: EditorValue;
};

export default class SimpleRichTextEditor extends Component {
props: Props;
state: State;

constructor() {
super(...arguments);
autobind(this);
this.state = {
editorValue: createEmptyValue(),
};
}

componentWillMount() {
this._updateStateFromProps(this.props);
}

componentWillReceiveProps(newProps: Props) {
this._updateStateFromProps(newProps);
}

_updateStateFromProps(props: Props) {
let {value, format} = props;
let {editorValue} = this.state;
this.setState({
editorValue: editorValue.setContentFromString(value, format),
});
}

render(): React.Element {
let {value, format, onChange, ...otherProps} = this.props; // eslint-disable-line no-unused-vars
return (
<RichTextEditor
{...otherProps}
value={this.state.editorValue}
onChange={this._onChange}
/>
);
}

_onChange(editorValue: EditorValue) {

This comment has been minimized.

Copy link
@ForbesLindesay

ForbesLindesay Sep 15, 2016

Collaborator

Doesn't this method also need to call this.setState({editorValue}); before passing on the change? otherwise changes to things like cursor position won't be tracked.

This comment has been minimized.

Copy link
@sstur

sstur Oct 17, 2016

Author Owner

Ahh, yes, you're right!
Edit: Done.

let {format, onChange} = this.props;
if (onChange != null) {
let stringValue = editorValue.toString(format);
onChange(stringValue);

This comment has been minimized.

Copy link
@ForbesLindesay

ForbesLindesay Sep 15, 2016

Collaborator

Shouldn't we check that the stringValue does not equal this.props.value before calling onChange here.

This comment has been minimized.

Copy link
@sstur

sstur Oct 17, 2016

Author Owner

Yes, that would make sense, otherwise the onChange will be called every time the cursor moves.
Edit: Done.

}
}
}

1 comment on commit 06ed718

@sstur
Copy link
Owner Author

@sstur sstur commented on 06ed718 Jul 2, 2016

Choose a reason for hiding this comment

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

This is related to a feature request at #62

Please sign in to comment.