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 Draft.js copy-paste handling overrides from draftjs-conductor. Fix #147 #155

Merged
merged 4 commits into from
Jun 4, 2018
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

## 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)).

### Changed

* Update to `[email protected]`. This does not include any functional changes, but will cause a duplicated dependency for projects having both `draftail` and `draftjs-filters` as deps if they don’t also update `draftjs-filters`.

## [[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