/
setDirection.ts
29 lines (27 loc) · 1.14 KB
/
setDirection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import collapseSelectedBlocks from '../utils/collapseSelectedBlocks';
import formatUndoSnapshot from '../utils/formatUndoSnapshot';
import { Direction } from 'roosterjs-editor-types';
import type { IEditor } from 'roosterjs-editor-types';
import type { CompatibleDirection } from 'roosterjs-editor-types/lib/compatibleTypes';
/**
* Change direction for the blocks/paragraph at selection
* @param editor The editor instance
* @param direction The direction option:
* Direction.LeftToRight refers to 'ltr', Direction.RightToLeft refers to 'rtl'
*/
export default function setDirection(editor: IEditor, direction: Direction | CompatibleDirection) {
editor.focus();
formatUndoSnapshot(
editor,
(start, end) => {
collapseSelectedBlocks(editor, element => {
element.setAttribute('dir', direction == Direction.LeftToRight ? 'ltr' : 'rtl');
element.style.textAlign = direction == Direction.LeftToRight ? 'left' : 'right';
});
if (start && end) {
editor.select(start, end);
}
},
'setDirection'
);
}