-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(copy-paste): add wip internal clipboard reuse for copy-paste
- Loading branch information
1 parent
d38022d
commit 3b24a10
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |