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

WIP: On paste, read content from internal clipboard via paste registry #148

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 38 additions & 1 deletion lib/components/DraftailEditor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { Editor, EditorState, RichUtils } from 'draft-js';
import { Editor, EditorState, RichUtils, Modifier } from 'draft-js';
import { ListNestingStyles } from 'draftjs-conductor';

import {
Expand Down Expand Up @@ -48,6 +48,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 +313,32 @@ class DraftailEditor extends Component {
return NOT_HANDLED;
}

handlePastedText(text, html, editorState) {
const editorKey = this.editorRef.getEditorKey();
const isEditor = html.includes('data-editor');
const htmlKey = isEditor ? /data-editor="(\w+)"/.exec(html)[1] : '';

if (!htmlKey || htmlKey === editorKey || !window.editorRefs[htmlKey]) {
return false;
}

const clipboard = window.editorRefs[htmlKey].getClipboard();

if (clipboard) {
const newContent = Modifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
clipboard,
);
this.onChange(
EditorState.push(editorState, newContent, 'insert-fragment'),
);
return true;
}

return false;
}

toggleBlockType(blockType) {
const { editorState } = this.state;
this.onChange(RichUtils.toggleBlockType(editorState, blockType));
Expand Down Expand Up @@ -588,6 +615,15 @@ class DraftailEditor extends Component {
customStyleMap={behavior.getCustomStyleMap(inlineStyles)}
ref={ref => {
this.editorRef = ref;
if (ref) {
window.editorRefs = Object.assign(
{},
window.editorRefs,
{
[ref.getEditorKey()]: ref,
},
);
}
}}
editorState={editorState}
onChange={this.onChange}
Expand All @@ -609,6 +645,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