Skip to content

Commit

Permalink
Allow override of onInput handler in edit mode (#32)
Browse files Browse the repository at this point in the history
* Adds onInput override for edit mode

* bump version
  • Loading branch information
srubin authored Mar 18, 2024
1 parent d36368a commit a6fc584
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@descript/draft-js",
"description": "A React framework for building text editors.",
"version": "0.11.6-descript.24",
"version": "0.11.6-descript.25",
"keywords": [
"draftjs",
"editor",
Expand Down
34 changes: 13 additions & 21 deletions src/component/base/DraftEditor.react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -578,29 +578,21 @@ export default class DraftEditor extends React.Component<
setMode: (draftEditorModes: DraftEditorModes) => void = (
mode: DraftEditorModes,
): void => {
const {onPaste, onCut, onCopy} = this.props;
const editHandler = {...handlerMap.edit};

if (onPaste) {
/* $FlowFixMe(>=0.117.0 site=www,mobile) This comment suppresses an error found
* when Flow v0.117 was deployed. To see the error delete this comment
* and run Flow. */
editHandler.onPaste = onPaste;
}

if (onCut) {
editHandler.onCut = onCut;
}

if (onCopy) {
editHandler.onCopy = onCopy;
const {onPaste, onCut, onCopy, onInput} = this.props;

if (mode === 'edit') {
const editHandler = handlerMap.edit;
this._handler = {
...editHandler,
onInput: onInput || editHandler.onInput,
onCopy: onCopy || editHandler.onCopy,
onCut: onCut || editHandler.onCut,
onPaste: onPaste || editHandler.onPaste,
};
} else {
this._handler = handlerMap[mode];
}

const handler = {
...handlerMap,
edit: editHandler,
};
this._handler = handler[mode];
this.mode = mode;

if (mode !== 'drag') {
Expand Down
18 changes: 17 additions & 1 deletion src/component/base/DraftEditorProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import {DraftInlineStyle} from '../../model/immutable/DraftInlineStyle';
import {DraftBlockRenderMap} from '../../model/immutable/DraftBlockRenderMap';
import {CSSProperties} from 'react';
import DraftEditor from './DraftEditor.react';
import {SyntheticClipboardEvent} from '../utils/eventTypes';
import {
SyntheticClipboardEvent,
SyntheticInputEvent,
} from '../utils/eventTypes';
import {BlockNode} from '../../model/immutable/BlockNode';

export type DraftEditorProps = {
Expand Down Expand Up @@ -206,6 +209,19 @@ export type DraftEditorProps = {
syntheticClipboardEvent: SyntheticClipboardEvent,
) => void;

/**
* Override the default behavior for the editor's `onInput` handler in edit mode.
* The draft.js implementation is typically an important feature of draft.js
* but if the user is attempting to fully control the input (e.g., via
* overriding `handleBeforeInput` then they might want to override this
* in order to provide error handling in the unexpected event that we do
* end up in this handler).
*/
onInput?: (
draftEditor: DraftEditor,
syntheticClipboardEvent: SyntheticInputEvent,
) => void;

/**
* Enables support for experimental (more performant) strategy for
* updating DOM selection
Expand Down

0 comments on commit a6fc584

Please sign in to comment.