Skip to content

Commit

Permalink
Add Draft.js copy-paste handling overrides from draftjs-conductor. Fix
Browse files Browse the repository at this point in the history
…#147

This makes Draftail always preserve the full content as-is when copy-pasting between editors. See also thibaudcolas/draftjs-conductor#2.
  • Loading branch information
thibaudcolas committed Jun 3, 2018
1 parent da0f470 commit d3ee6f0
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
## Unreleased

### Added

* Add Draft.js copy-paste handling overrides from `draftjs-conductor`. This makes Draftail always preserve the full content as-is when copy-pasting between editors. Fix [#147](https://github.com/springload/draftail/issues/147) ([thibaudcolas/draftjs-conductor#2](https://github.com/thibaudcolas/draftjs-conductor/pull/2)).

## [[v0.17.1]](https://github.com/springload/draftail/releases/tag/v0.17.1)

### Changed
Expand Down
34 changes: 33 additions & 1 deletion lib/components/DraftailEditor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { Editor, EditorState, RichUtils } from 'draft-js';
import { ListNestingStyles } from 'draftjs-conductor';
import {
ListNestingStyles,
registerCopySource,
handleDraftEditorPastedText,
} from 'draftjs-conductor';

import {
ENTITY_TYPE,
Expand Down Expand Up @@ -48,6 +52,7 @@ class DraftailEditor extends Component {
this.onTab = this.onTab.bind(this);
this.handleKeyCommand = this.handleKeyCommand.bind(this);
this.handleBeforeInput = this.handleBeforeInput.bind(this);
this.handlePastedText = this.handlePastedText.bind(this);

this.toggleBlockType = this.toggleBlockType.bind(this);
this.toggleInlineStyle = this.toggleInlineStyle.bind(this);
Expand Down Expand Up @@ -312,6 +317,24 @@ class DraftailEditor extends Component {
return NOT_HANDLED;
}

handlePastedText(text, html, editorState) {
const { stripPastedStyles } = this.props;

// Leave paste handling to Draft.js when stripping styles is desirable.
if (stripPastedStyles) {
return false;
}

const pastedState = handleDraftEditorPastedText(html, editorState);

if (pastedState) {
this.onChange(pastedState);
return true;
}

return false;
}

toggleBlockType(blockType) {
const { editorState } = this.state;
this.onChange(RichUtils.toggleBlockType(editorState, blockType));
Expand Down Expand Up @@ -528,6 +551,14 @@ class DraftailEditor extends Component {
return null;
}

componentDidMount() {
this.copySource = registerCopySource(this.editorRef);
}

componentWillUnmount() {
this.copySource.unregister();
}

render() {
const {
placeholder,
Expand Down Expand Up @@ -609,6 +640,7 @@ class DraftailEditor extends Component {
)}
handleKeyCommand={this.handleKeyCommand}
handleBeforeInput={this.handleBeforeInput}
handlePastedText={this.handlePastedText}
onFocus={this.onFocus}
onBlur={this.onBlur}
onTab={this.onTab}
Expand Down
Loading

0 comments on commit d3ee6f0

Please sign in to comment.