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

Commit

Permalink
Add text directionality override prop to DraftEditor
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
flarnie committed Feb 27, 2017
1 parent d6cad2e commit 1557524
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
13 changes: 13 additions & 0 deletions docs/APIReference-Editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ to fit it within your UI design.
If this value is not set, text alignment will be based on the characters within
the editor, on a per-block basis.

#### textDirectionality
```
textDirectionality?: DraftTextDirectionality
```
Optionally set the overriding text directionality for this editor. The values
include 'RTL' for right-to-left text, like Hebrew or Arabic, and 'LTR' for
left-to-right text, like English or Spanish. This directionality will apply to
the entire contents, regardless of default text direction for input text.

If this value is not set, text directionality will be based on the characters
within the editor, on a per-block basis.


#### blockRendererFn
```
blockRendererFn?: (block: ContentBlock) => ?Object
Expand Down
5 changes: 3 additions & 2 deletions src/component/base/DraftEditor.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,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 Expand Up @@ -355,8 +356,8 @@ class DraftEditor extends React.Component {
this.update(
EditorState.forceSelection(
editorState,
editorState.getSelection()
)
editorState.getSelection(),
),
);
}
}
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 @@ -17,6 +17,7 @@ import type {DraftBlockRenderMap} from 'DraftBlockRenderMap';
import type {DraftDragType} from 'DraftDragType';
import type {DraftEditorCommand} from 'DraftEditorCommand';
import type {DraftTextAlignment} from 'DraftTextAlignment';
import type {DraftTextDirectionality} from 'DraftTextDirectionality';
import type {DraftInlineStyle} from 'DraftInlineStyle';
import type {DraftHandleValue} from 'DraftHandleValue';
import type EditorState from 'EditorState';
Expand All @@ -42,6 +43,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?: DraftTextDirectionality,

// 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
16 changes: 16 additions & 0 deletions src/component/base/DraftTextDirectionality.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule DraftTextDirectionality
* @flow
*/

'use strict';

// TODO: (t16333390) import this type from 'fbjs'
export type DraftTextDirectionality = 'LTR' | 'RTL' | 'NEUTRAL';
12 changes: 8 additions & 4 deletions 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 Expand Up @@ -161,7 +165,7 @@ class DraftEditorContents extends React.Component {
);
className = joinClasses(
className,
getListItemClasses(blockType, depth, shouldResetCount, direction)
getListItemClasses(blockType, depth, shouldResetCount, direction),
);
}

Expand Down Expand Up @@ -221,7 +225,7 @@ class DraftEditorContents extends React.Component {
key: info.key + '-wrap',
'data-offset-key': info.offsetKey,
},
blocks
blocks,
);
outputBlocks.push(wrapperElement);
} else {
Expand All @@ -244,7 +248,7 @@ function getListItemClasses(
type: string,
depth: number,
shouldResetCount: boolean,
direction: BidiDirection
direction: BidiDirection,
): string {
return cx({
'public/DraftStyleDefault/unorderedListItem':
Expand Down

0 comments on commit 1557524

Please sign in to comment.