Skip to content

Commit

Permalink
Fix toolbar entity edit and remove not working on selection pre first…
Browse files Browse the repository at this point in the history
… char. Fix #109
  • Loading branch information
thibaudcolas committed Nov 29, 2017
1 parent 8b833b8 commit 5a9ab3b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
23 changes: 11 additions & 12 deletions lib/components/DraftailEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,32 +439,31 @@ class DraftailEditor extends Component {
const entity = contentState.getEntity(entityKey);

// TODO It seems strange to update the selection state when requesting an edit.
const entitySelectionState = DraftUtils.getSelectedEntitySelection(
const entitySelection = DraftUtils.getEntitySelection(
editorState,
entityKey,
);
const nextState = EditorState.acceptSelection(
editorState,
entitySelectionState,
entitySelection,
);

this.onChange(nextState);

this.toggleDialog(entity.type, entity);
this.toggleDialog(entity.getType(), entity);
}

onRemoveEntity() {
onRemoveEntity(entityKey) {
const { editorState } = this.state;
const entitySelectionState = DraftUtils.getSelectedEntitySelection(
const entitySelection = DraftUtils.getEntitySelection(
editorState,
entityKey,
);

this.setState({
shouldShowTooltip: false,
});
// TODO Should also set this.tooltip to null.
this.toggleTooltip(false);

this.onChange(
RichUtils.toggleLink(editorState, entitySelectionState, null),
);
this.onChange(RichUtils.toggleLink(editorState, entitySelection, null));
}

addHR() {
Expand Down Expand Up @@ -590,7 +589,7 @@ class DraftailEditor extends Component {
<Portal>
{shouldShowTooltip && entityKey ? (
<Tooltip
onRemove={this.onRemoveEntity}
onRemove={this.onRemoveEntity.bind(this, entityKey)}
onEdit={this.onEditEntity.bind(this, entityKey)}
entityData={contentState.getEntity(entityKey).getData()}
position={{
Expand Down
10 changes: 6 additions & 4 deletions lib/components/DraftailEditor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,14 @@ describe('DraftailEditor', () => {
<DraftailEditor rawContentState={rawContentState} />,
);

jest.spyOn(DraftUtils, 'getSelectedEntitySelection');
jest
.spyOn(DraftUtils, 'getEntitySelection')
.mockImplementation(editorState => editorState.getSelection());
jest.spyOn(wrapper.instance(), 'toggleDialog');
});

afterEach(() => {
DraftUtils.getSelectedEntitySelection.mockRestore();
DraftUtils.getEntitySelection.mockRestore();
});

it('works', () => {
Expand All @@ -484,13 +486,13 @@ describe('DraftailEditor', () => {
wrapper = shallow(<DraftailEditor />);

RichUtils.toggleLink = jest.fn();
DraftUtils.getSelectedEntitySelection = jest.fn();
DraftUtils.getEntitySelection = jest.fn();
wrapper.instance().onChange = jest.fn();
});

afterEach(() => {
RichUtils.toggleLink.mockRestore();
DraftUtils.getSelectedEntitySelection.mockRestore();
DraftUtils.getEntitySelection.mockRestore();
});

it('works', () => {
Expand Down

0 comments on commit 5a9ab3b

Please sign in to comment.