Skip to content

Commit

Permalink
feat(copy-paste): add wip internal clipboard reuse for copy-paste
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudcolas committed May 20, 2018
1 parent 2f899bc commit 4366190
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/demo/components/DemoEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import type { DraftBlockType } from "draft-js/lib/DraftBlockType.js.flow";
import type { DraftEntityType } from "draft-js/lib/DraftEntityType.js.flow";

import { ListNestingStyles, blockDepthStyleFn } from "../../lib/index";
import {
registerCopySource,
unregisterCopySource,
handleDraftEditorPastedText,
} from "../../lib/copypaste";

import SentryBoundary from "./SentryBoundary";
import Highlight from "./Highlight";
Expand Down Expand Up @@ -98,6 +103,8 @@ type State = {
* Demo editor.
*/
class DemoEditor extends Component<Props, State> {
editorRef: ?Object;

constructor(props: Props) {
super(props);
const { extended } = props;
Expand Down Expand Up @@ -130,6 +137,15 @@ class DemoEditor extends Component<Props, State> {
(this: any).toggleBlock = this.toggleBlock.bind(this);
(this: any).toggleEntity = this.toggleEntity.bind(this);
(this: any).blockRenderer = this.blockRenderer.bind(this);
(this: any).handlePastedText = this.handlePastedText.bind(this);
}

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

componentWillUnmount() {
unregisterCopySource(this.editorRef);
}

onChange(nextState: EditorState) {
Expand Down Expand Up @@ -194,6 +210,13 @@ class DemoEditor extends Component<Props, State> {
}

const entityKey = block.getEntityAt(0);

if (!entityKey) {
return {
editable: false,
};
}

const entity = content.getEntity(entityKey);

if (entity.getType() === "HORIZONTAL_RULE") {
Expand All @@ -209,6 +232,22 @@ class DemoEditor extends Component<Props, State> {
};
}

handlePastedText(text: string, html: ?string, editorState: EditorState) {
let newState = handleDraftEditorPastedText(
this.editorRef,
text,
html,
editorState,
);

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

return false;
}

onTab(event: SyntheticKeyboardEvent<>) {
const { editorState } = this.state;
const newState = RichUtils.onTab(event, editorState, MAX_LIST_NESTING);
Expand Down Expand Up @@ -252,12 +291,16 @@ class DemoEditor extends Component<Props, State> {
))}
</div>
<Editor
ref={(ref) => {
this.editorRef = ref;
}}
editorState={editorState}
onChange={this.onChange}
stripPastedStyles={false}
blockRendererFn={this.blockRenderer}
blockStyleFn={blockDepthStyleFn}
onTab={this.onTab}
handlePastedText={this.handlePastedText}
/>
</SentryBoundary>
<ListNestingStyles max={MAX_LIST_NESTING} />
Expand Down
42 changes: 42 additions & 0 deletions src/lib/copypaste.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// @flow
import type { ElementRef } from "react";
import { Editor, EditorState, Modifier } from "draft-js";

const sourceEditors = {};

export const registerCopySource = (ref: ElementRef<Editor>) => {
sourceEditors[ref.getEditorKey()] = ref;
};

export const unregisterCopySource = (ref: ElementRef<Editor>) => {
delete sourceEditors[ref.getEditorKey()];
};

export const handleDraftEditorPastedText = (
ref: ElementRef<Editor>,
text: string,
html: ?string,
editorState: EditorState,
) => {
const isEditor = html && html.includes("data-editor");
const sourceKey = html && isEditor ? /data-editor="(\w+)"/.exec(html)[1] : "";
const editorKey = ref.getEditorKey();

if (!sourceKey || sourceKey === editorKey || !sourceEditors[sourceKey]) {
return false;
}

const clipboard = sourceEditors[sourceKey].getClipboard();

// TODO Potentially layer this separately.
if (clipboard) {
const newContent = Modifier.replaceWithFragment(
editorState.getCurrentContent(),
editorState.getSelection(),
clipboard,
);
return EditorState.push(editorState, newContent, "insert-fragment");
}

return false;
};

0 comments on commit 4366190

Please sign in to comment.