Skip to content
Open
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
176 changes: 97 additions & 79 deletions dist/Draft.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions dist/Draft.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/DraftEditorProps.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ export type DraftEditorProps = {
onLeftArrow?: (e: SyntheticKeyboardEvent<>) => void;

onBlur?: (e: SyntheticEvent<>) => void;
onFocus?: (e: SyntheticEvent<>) => void
onFocus?: (e: SyntheticEvent<>) => void;
onCopy?: (e: SyntheticEvent<>) => void;
onCut?: (e: SyntheticEvent<>) => void

// Provide a map of inline style names corresponding to CSS style objects
// that will be rendered for matching ranges.
Expand Down
7 changes: 4 additions & 3 deletions lib/editOnBlur.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var EditorState = require('./EditorState');
var containsNode = require('fbjs/lib/containsNode');
var getActiveElement = require('fbjs/lib/getActiveElement');

function editOnBlur(editor, e) {
function editOnBlur(editor, event) {
// In a contentEditable element, when you select a range and then click
// another active element, this does trigger a `blur` event but will not
// remove the DOM selection from the contenteditable.
Expand All @@ -42,8 +42,9 @@ function editOnBlur(editor, e) {
}

var selection = currentSelection.set('hasFocus', false);
editor.props.onBlur && editor.props.onBlur(e);
editor.update(EditorState.acceptSelection(editorState, selection));
var updatedEditorState = EditorState.acceptSelection(editorState, selection);
editor.update(updatedEditorState);
editor.props.onBlur && editor.props.onBlur(event);
}

module.exports = editOnBlur;
7 changes: 4 additions & 3 deletions lib/editOnBlur.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EditorState = require('./EditorState');
const containsNode = require('fbjs/lib/containsNode');
const getActiveElement = require('fbjs/lib/getActiveElement');

function editOnBlur(editor: DraftEditor, e: SyntheticEvent<>): void {
function editOnBlur(editor: DraftEditor, event: SyntheticEvent<>): void {
// In a contentEditable element, when you select a range and then click
// another active element, this does trigger a `blur` event but will not
// remove the DOM selection from the contenteditable.
Expand All @@ -44,8 +44,9 @@ function editOnBlur(editor: DraftEditor, e: SyntheticEvent<>): void {
}

var selection = currentSelection.set('hasFocus', false);
editor.props.onBlur && editor.props.onBlur(e);
editor.update(EditorState.acceptSelection(editorState, selection));
var updatedEditorState = EditorState.acceptSelection(editorState, selection);
editor.update(updatedEditorState);
editor.props.onBlur && editor.props.onBlur(event);
}

module.exports = editOnBlur;
11 changes: 9 additions & 2 deletions lib/editOnCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,26 @@
'use strict';

var getFragmentFromSelection = require('./getFragmentFromSelection');
var isEventHandled = require('./isEventHandled');

/**
* If we have a selection, create a ContentState fragment and store
* it in our internal clipboard. Subsequent paste events will use this
* fragment if no external clipboard data is supplied.
*/
function editOnCopy(editor, e) {
function editOnCopy(editor, event) {
var shouldPreventDefault = editor.props.onCopy && isEventHandled(editor.props.onCopy(event));

if (shouldPreventDefault) {
return;
}

var editorState = editor._latestEditorState;
var selection = editorState.getSelection();

// No selection, so there's nothing to copy.
if (selection.isCollapsed()) {
e.preventDefault();
event.preventDefault();
return;
}

Expand Down
14 changes: 10 additions & 4 deletions lib/editOnCopy.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@
'use strict';

import type DraftEditor from './DraftEditor.react';

var getFragmentFromSelection = require('./getFragmentFromSelection');
const getFragmentFromSelection = require('./getFragmentFromSelection');
const isEventHandled = require('./isEventHandled');

/**
* If we have a selection, create a ContentState fragment and store
* it in our internal clipboard. Subsequent paste events will use this
* fragment if no external clipboard data is supplied.
*/
function editOnCopy(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
function editOnCopy(editor: DraftEditor, event: SyntheticClipboardEvent<>): void {
const shouldPreventDefault = editor.props.onCopy && isEventHandled(editor.props.onCopy(event));

if (shouldPreventDefault) {
return;
}

var editorState = editor._latestEditorState;
var selection = editorState.getSelection();

// No selection, so there's nothing to copy.
if (selection.isCollapsed()) {
e.preventDefault();
event.preventDefault();
return;
}

Expand Down
14 changes: 11 additions & 3 deletions lib/editOnCut.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var Style = require('fbjs/lib/Style');

var getFragmentFromSelection = require('./getFragmentFromSelection');
var getScrollPosition = require('fbjs/lib/getScrollPosition');
var isEventHandled = require('./isEventHandled');

/**
* On `cut` events, native behavior is allowed to occur so that the system
Expand All @@ -29,15 +30,21 @@ var getScrollPosition = require('fbjs/lib/getScrollPosition');
* In addition, we can keep a copy of the removed fragment, including all
* styles and entities, for use as an internal paste.
*/
function editOnCut(editor, e) {
function editOnCut(editor, event) {
var shouldPreventDefault = editor.props.onCut && isEventHandled(editor.props.onCut(event));

if (shouldPreventDefault) {
return;
}

var editorState = editor._latestEditorState;
var selection = editorState.getSelection();
var element = e.target;
var element = event.target;
var scrollPosition = void 0;

// No selection, so there's nothing to cut.
if (selection.isCollapsed()) {
e.preventDefault();
event.preventDefault();
return;
}

Expand All @@ -63,6 +70,7 @@ function editOnCut(editor, e) {

function removeFragment(editorState) {
var newContent = DraftModifier.removeRange(editorState.getCurrentContent(), editorState.getSelection(), 'forward');

return EditorState.push(editorState, newContent, 'remove-range');
}

Expand Down
14 changes: 11 additions & 3 deletions lib/editOnCut.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Style = require('fbjs/lib/Style');

const getFragmentFromSelection = require('./getFragmentFromSelection');
const getScrollPosition = require('fbjs/lib/getScrollPosition');
const isEventHandled = require('./isEventHandled');

/**
* On `cut` events, native behavior is allowed to occur so that the system
Expand All @@ -31,15 +32,21 @@ const getScrollPosition = require('fbjs/lib/getScrollPosition');
* In addition, we can keep a copy of the removed fragment, including all
* styles and entities, for use as an internal paste.
*/
function editOnCut(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
function editOnCut(editor: DraftEditor, event: SyntheticClipboardEvent<>): void {
const shouldPreventDefault = editor.props.onCut && isEventHandled(editor.props.onCut(event));

if (shouldPreventDefault) {
return;
}

const editorState = editor._latestEditorState;
const selection = editorState.getSelection();
const element = e.target;
const element = event.target;
let scrollPosition;

// No selection, so there's nothing to cut.
if (selection.isCollapsed()) {
e.preventDefault();
event.preventDefault();
return;
}

Expand All @@ -65,6 +72,7 @@ function editOnCut(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {

function removeFragment(editorState: EditorState): EditorState {
const newContent = DraftModifier.removeRange(editorState.getCurrentContent(), editorState.getSelection(), 'forward');

return EditorState.push(editorState, newContent, 'remove-range');
}

Expand Down
10 changes: 6 additions & 4 deletions lib/editOnPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ var splitTextIntoTextBlocks = require('./splitTextIntoTextBlocks');
/**
* Paste content.
*/
function editOnPaste(editor, e) {
e.preventDefault();
var data = new DataTransfer(e.clipboardData);
function editOnPaste(editor, event) {
event.preventDefault();
var data = new DataTransfer(event.clipboardData);

// Get files, unless this is likely to be a string the user wants inline.
if (!data.isRichText()) {
Expand Down Expand Up @@ -75,7 +75,9 @@ function editOnPaste(editor, e) {
var html = data.getHTML();
var editorState = editor._latestEditorState;

if (editor.props.handlePastedText && isEventHandled(editor.props.handlePastedText(text, html, editorState))) {
var shouldPreventDefault = editor.props.handlePastedText && isEventHandled(editor.props.handlePastedText(text, html, editorState));

if (shouldPreventDefault) {
return;
}

Expand Down
10 changes: 6 additions & 4 deletions lib/editOnPaste.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var splitTextIntoTextBlocks = require('./splitTextIntoTextBlocks');
/**
* Paste content.
*/
function editOnPaste(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
e.preventDefault();
var data = new DataTransfer(e.clipboardData);
function editOnPaste(editor: DraftEditor, event: SyntheticClipboardEvent<>): void {
event.preventDefault();
var data = new DataTransfer(event.clipboardData);

// Get files, unless this is likely to be a string the user wants inline.
if (!data.isRichText()) {
Expand Down Expand Up @@ -79,7 +79,9 @@ function editOnPaste(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
const html = data.getHTML();
const editorState = editor._latestEditorState;

if (editor.props.handlePastedText && isEventHandled(editor.props.handlePastedText(text, html, editorState))) {
const shouldPreventDefault = editor.props.handlePastedText && isEventHandled(editor.props.handlePastedText(text, html, editorState));

if (shouldPreventDefault) {
return;
}

Expand Down
2 changes: 2 additions & 0 deletions src/component/base/DraftEditorProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ export type DraftEditorProps = {

onBlur?: (e: SyntheticEvent<>) => void,
onFocus?: (e: SyntheticEvent<>) => void,
onCopy?: (e: SyntheticEvent<>) => void,
onCut?: (e: SyntheticEvent<>) => void,

// Provide a map of inline style names corresponding to CSS style objects
// that will be rendered for matching ranges.
Expand Down
7 changes: 4 additions & 3 deletions src/component/handlers/edit/editOnBlur.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const EditorState = require('EditorState');
const containsNode = require('containsNode');
const getActiveElement = require('getActiveElement');

function editOnBlur(editor: DraftEditor, e: SyntheticEvent<>): void {
function editOnBlur(editor: DraftEditor, event: SyntheticEvent<>): void {
// In a contentEditable element, when you select a range and then click
// another active element, this does trigger a `blur` event but will not
// remove the DOM selection from the contenteditable.
Expand Down Expand Up @@ -48,8 +48,9 @@ function editOnBlur(editor: DraftEditor, e: SyntheticEvent<>): void {
}

var selection = currentSelection.set('hasFocus', false);
editor.props.onBlur && editor.props.onBlur(e);
editor.update(EditorState.acceptSelection(editorState, selection));
var updatedEditorState = EditorState.acceptSelection(editorState, selection);
editor.update(updatedEditorState);
editor.props.onBlur && editor.props.onBlur(event);
}

module.exports = editOnBlur;
15 changes: 11 additions & 4 deletions src/component/handlers/edit/editOnCopy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,28 @@
'use strict';

import type DraftEditor from 'DraftEditor.react';

var getFragmentFromSelection = require('getFragmentFromSelection');
const getFragmentFromSelection = require('getFragmentFromSelection');
const isEventHandled = require('isEventHandled');

/**
* If we have a selection, create a ContentState fragment and store
* it in our internal clipboard. Subsequent paste events will use this
* fragment if no external clipboard data is supplied.
*/
function editOnCopy(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
function editOnCopy(editor: DraftEditor, event: SyntheticClipboardEvent<>): void {
const shouldPreventDefault = editor.props.onCopy &&
isEventHandled(editor.props.onCopy(event));

if (shouldPreventDefault) {
return;
}

var editorState = editor._latestEditorState;
var selection = editorState.getSelection();

// No selection, so there's nothing to copy.
if (selection.isCollapsed()) {
e.preventDefault();
event.preventDefault();
return;
}

Expand Down
15 changes: 12 additions & 3 deletions src/component/handlers/edit/editOnCut.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Style = require('Style');

const getFragmentFromSelection = require('getFragmentFromSelection');
const getScrollPosition = require('getScrollPosition');
const isEventHandled = require('isEventHandled');

/**
* On `cut` events, native behavior is allowed to occur so that the system
Expand All @@ -31,15 +32,22 @@ const getScrollPosition = require('getScrollPosition');
* In addition, we can keep a copy of the removed fragment, including all
* styles and entities, for use as an internal paste.
*/
function editOnCut(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
function editOnCut(editor: DraftEditor, event: SyntheticClipboardEvent<>): void {
const shouldPreventDefault = editor.props.onCut &&
isEventHandled(editor.props.onCut(event));

if (shouldPreventDefault) {
return;
}

const editorState = editor._latestEditorState;
const selection = editorState.getSelection();
const element = e.target;
const element = event.target;
let scrollPosition;

// No selection, so there's nothing to cut.
if (selection.isCollapsed()) {
e.preventDefault();
event.preventDefault();
return;
}

Expand Down Expand Up @@ -69,6 +77,7 @@ function removeFragment(editorState: EditorState): EditorState {
editorState.getSelection(),
'forward',
);

return EditorState.push(editorState, newContent, 'remove-range');
}

Expand Down
14 changes: 7 additions & 7 deletions src/component/handlers/edit/editOnPaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ var splitTextIntoTextBlocks = require('splitTextIntoTextBlocks');
/**
* Paste content.
*/
function editOnPaste(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
e.preventDefault();
var data = new DataTransfer(e.clipboardData);
function editOnPaste(editor: DraftEditor, event: SyntheticClipboardEvent<>): void {
event.preventDefault();
var data = new DataTransfer(event.clipboardData);

// Get files, unless this is likely to be a string the user wants inline.
if (!data.isRichText()) {
Expand Down Expand Up @@ -97,10 +97,10 @@ function editOnPaste(editor: DraftEditor, e: SyntheticClipboardEvent<>): void {
const html = data.getHTML();
const editorState = editor._latestEditorState;

if (
editor.props.handlePastedText &&
isEventHandled(editor.props.handlePastedText(text, html, editorState))
) {
const shouldPreventDefault = editor.props.handlePastedText &&
isEventHandled(editor.props.handlePastedText(text, html, editorState));

if (shouldPreventDefault) {
return;
}

Expand Down