Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.

Commit 91cd9d1

Browse files
authored
feat: RTL support (#469)
* Add support for RTL languages * Add rtl prop description to README.md * Allow basic mixed direction writing * Fix Linting issues * Fix table grips border radii * Change rtl prop to dir in main component * Fix linting issue * Calculate editor direction automatically when dir=auto * Fix block menu position on RTL mode.
1 parent 9d1fd07 commit 91cd9d1

File tree

6 files changed

+117
-43
lines changed

6 files changed

+117
-43
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ Allows overriding the inbuilt copy dictionary, for example to internationalize t
9494

9595
With `dark` set to `true` the editor will use a default dark theme that's included. See the [source here](/src/theme.ts).
9696

97+
#### `dir`
98+
99+
*Default: `auto`*
100+
101+
Controls direction of the document. Possible values are:
102+
- `ltr`: Editor layout is optimized for LTR documents and the content is explicitly marked as LTR.
103+
- `rtl`: Editor layout is optimized for RTL documents and the content is explicitly marked as RTL.
104+
- `auto`: Editor layout is decided by the browser based on document content.
105+
97106
#### `tooltip`
98107

99108
A React component that will be wrapped around items that have an optional tooltip. You can use this to inject your own tooltip library into the editor – the component will be passed the following props:

src/components/BlockMenu.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const defaultPosition = {
2323
};
2424

2525
type Props = {
26+
rtl: boolean;
2627
isActive: boolean;
2728
commands: Record<string, any>;
2829
dictionary: typeof baseDictionary;
@@ -356,19 +357,24 @@ class BlockMenu extends React.Component<Props, State> {
356357
}
357358

358359
const { left } = this.caretPosition;
359-
const { top, bottom } = paragraph.node.getBoundingClientRect();
360+
const { top, bottom, right } = paragraph.node.getBoundingClientRect();
360361
const margin = 24;
361362

363+
let leftPos = left + window.scrollX;
364+
if (props.rtl && ref) {
365+
leftPos = right - ref.scrollWidth;
366+
}
367+
362368
if (startPos.top - offsetHeight > margin) {
363369
return {
364-
left: left + window.scrollX,
370+
left: leftPos,
365371
top: undefined,
366372
bottom: window.innerHeight - top - window.scrollY,
367373
isAbove: false,
368374
};
369375
} else {
370376
return {
371-
left: left + window.scrollX,
377+
left: leftPos,
372378
top: bottom + window.scrollY,
373379
bottom: undefined,
374380
isAbove: true,

src/components/SelectionToolbar.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import baseDictionary from "../dictionary";
2424
type Props = {
2525
dictionary: typeof baseDictionary;
2626
tooltip: typeof React.Component | React.FC<any>;
27+
rtl: boolean;
2728
isTemplate: boolean;
2829
commands: Record<string, any>;
2930
onOpen: () => void;
@@ -121,7 +122,7 @@ export default class SelectionToolbar extends React.Component<Props> {
121122
};
122123

123124
render() {
124-
const { dictionary, onCreateLink, isTemplate, ...rest } = this.props;
125+
const { dictionary, onCreateLink, isTemplate, rtl, ...rest } = this.props;
125126
const { view } = rest;
126127
const { state } = view;
127128
const { selection }: { selection: any } = state;
@@ -145,7 +146,7 @@ export default class SelectionToolbar extends React.Component<Props> {
145146
if (isTableSelection) {
146147
items = getTableMenuItems(dictionary);
147148
} else if (colIndex !== undefined) {
148-
items = getTableColMenuItems(state, colIndex, dictionary);
149+
items = getTableColMenuItems(state, colIndex, rtl, dictionary);
149150
} else if (rowIndex !== undefined) {
150151
items = getTableRowMenuItems(state, rowIndex, dictionary);
151152
} else if (isImageSelection) {

0 commit comments

Comments
 (0)