Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Commit

Permalink
Preserve block style when pasting plain text
Browse files Browse the repository at this point in the history
Summary:
Preserve block style when pasting plain text. That would allow pasting into code blocks and lists and applying respective styles to new blocks.
That should work for any type of block: code, list, quote, heading...

Probably related github issues:

  - #1276
  - #1219

Reviewed By: sophiebits

Differential Revision: D5786405

fbshipit-source-id: 3c6d074b0c3fe200761164ec6c5ccf1a82df5072
  • Loading branch information
bumbu authored and facebook-github-bot committed Sep 7, 2017
1 parent 81b1884 commit e8d1011
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/component/handlers/edit/editOnPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var DataTransfer = require('DataTransfer');
var DraftModifier = require('DraftModifier');
var DraftPasteProcessor = require('DraftPasteProcessor');
var EditorState = require('EditorState');
var RichTextEditorUtil = require('RichTextEditorUtil');

var getEntityKeyForSelection = require('getEntityKeyForSelection');
var getTextContentFromFiles = require('getTextContentFromFiles');
Expand Down Expand Up @@ -64,8 +65,15 @@ function editOnPaste(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
editorState.getSelection(),
),
});
var currentBlockType = RichTextEditorUtil.getCurrentBlockType(
editorState,
);

var text = DraftPasteProcessor.processText(blocks, character);
var text = DraftPasteProcessor.processText(
blocks,
character,
currentBlockType,
);
var fragment = BlockMapBuilder.createFromArray(text);

var withInsertedText = DraftModifier.replaceWithFragment(
Expand Down Expand Up @@ -178,9 +186,12 @@ function editOnPaste(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
),
});

var currentBlockType = RichTextEditorUtil.getCurrentBlockType(editorState);

var textFragment = DraftPasteProcessor.processText(
textBlocks,
character,
currentBlockType,
);

var textMap = BlockMapBuilder.createFromArray(textFragment);
Expand Down
4 changes: 3 additions & 1 deletion src/model/paste/DraftPasteProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'use strict';

import type {DraftBlockRenderMap} from 'DraftBlockRenderMap';
import type {DraftBlockType} from 'DraftBlockType';
import type {EntityMap} from 'EntityMap';

const CharacterMetadata = require('CharacterMetadata');
Expand Down Expand Up @@ -46,13 +47,14 @@ const DraftPasteProcessor = {
processText(
textBlocks: Array<string>,
character: CharacterMetadata,
type: DraftBlockType,
): Array<ContentBlock> {
return textBlocks.map(
textLine => {
textLine = sanitizeDraftText(textLine);
return new ContentBlock({
key: generateRandomKey(),
type: 'unstyled',
type,
text: textLine,
characterList: List(Repeat(character, textLine.length)),
});
Expand Down

0 comments on commit e8d1011

Please sign in to comment.