Skip to content

Commit

Permalink
Add text directionality override prop to DraftEditor (facebookarchive…
Browse files Browse the repository at this point in the history
…#1034)

* Add text directionality override prop to DraftEditor

Credit for this change goes to @mmmoussa

 - Added an optional textDirectionality prop of type
   DraftTextDirectionality to the DraftEditor component which allows
   overriding the directionality of all blocks in the editor.
 - Fixed lint errors in the modified Draft.js files.

Note that we will be updating the DraftTextDirectionality type
definition to move it to the `fbjs` shared utils, possibly along with
related text directionality utilities.

* Hold off on updating docs with textDirectionality

Since we haven't released this new prop yet let's not add it to the
documentation until we do.

* Use BidiDirection instead of own directionality type

Thanks to @zpao for pointing out that fbjs has BidiDirction.
  • Loading branch information
flarnie authored and ouchxp committed Apr 7, 2017
1 parent 9d85ced commit 24b35ce
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/component/base/DraftEditor.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class DraftEditor extends React.Component {
editorKey={this._editorKey}
editorState={this.props.editorState}
key={'contents' + this.state.contentsKey}
textDirectionality={this.props.textDirectionality}
/>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/component/base/DraftEditorProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

'use strict';

import type {BidiDirection} from 'UnicodeBidiDirection';
import type ContentBlock from 'ContentBlock';
import type {DraftBlockRenderMap} from 'DraftBlockRenderMap';
import type {DraftDragType} from 'DraftDragType';
Expand Down Expand Up @@ -51,6 +52,10 @@ export type DraftEditorProps = {
// regardless of input characters.
textAlignment?: DraftTextAlignment,

// Specify whether text directionality should be forced in a direction
// regardless of input characters.
textDirectionality?: BidiDirection,

// For a given `ContentBlock` object, return an object that specifies
// a custom block component and/or props. If no object is returned,
// the default `TextEditorBlock` is used.
Expand Down
6 changes: 5 additions & 1 deletion src/component/contents/DraftEditorContents.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Props = {
blockRendererFn: Function,
blockStyleFn: (block: ContentBlock) => string,
editorState: EditorState,
textDirectionality?: BidiDirection,
};

/**
Expand Down Expand Up @@ -123,7 +124,10 @@ class DraftEditorContents extends React.Component {
customEditable = customRenderer.editable;
}

const direction = directionMap.get(key);
const {textDirectionality} = this.props;
const direction = textDirectionality
? textDirectionality
: directionMap.get(key);
const offsetKey = DraftOffsetKey.encode(key, 0, 0);
const componentProps = {
contentState: content,
Expand Down

0 comments on commit 24b35ce

Please sign in to comment.