@@ -5,15 +5,17 @@ import {EditorUiContext} from "../../framework/core";
55import alignCenterIcon from "@icons/editor/align-center.svg" ;
66import alignRightIcon from "@icons/editor/align-right.svg" ;
77import alignJustifyIcon from "@icons/editor/align-justify.svg" ;
8+ import ltrIcon from "@icons/editor/direction-ltr.svg" ;
9+ import rtlIcon from "@icons/editor/direction-rtl.svg" ;
810import {
911 $getBlockElementNodesInSelection ,
10- $selectionContainsAlignment , $selectSingleNode , $toggleSelection , getLastSelection
12+ $selectionContainsAlignment , $selectionContainsDirection , $ selectSingleNode, $toggleSelection , getLastSelection
1113} from "../../../utils/selection" ;
1214import { CommonBlockAlignment } from "../../../nodes/_common" ;
1315import { nodeHasAlignment } from "../../../utils/nodes" ;
1416
1517
16- function setAlignmentForSection ( editor : LexicalEditor , alignment : CommonBlockAlignment ) : void {
18+ function setAlignmentForSelection ( editor : LexicalEditor , alignment : CommonBlockAlignment ) : void {
1719 const selection = getLastSelection ( editor ) ;
1820 const selectionNodes = selection ?. getNodes ( ) || [ ] ;
1921
@@ -35,11 +37,21 @@ function setAlignmentForSection(editor: LexicalEditor, alignment: CommonBlockAli
3537 $toggleSelection ( editor ) ;
3638}
3739
40+ function setDirectionForSelection ( editor : LexicalEditor , direction : 'ltr' | 'rtl' ) : void {
41+ const selection = getLastSelection ( editor ) ;
42+
43+ const elements = $getBlockElementNodesInSelection ( selection ) ;
44+ for ( const node of elements ) {
45+ console . log ( 'setting direction' , node ) ;
46+ node . setDirection ( direction ) ;
47+ }
48+ }
49+
3850export const alignLeft : EditorButtonDefinition = {
3951 label : 'Align left' ,
4052 icon : alignLeftIcon ,
4153 action ( context : EditorUiContext ) {
42- context . editor . update ( ( ) => setAlignmentForSection ( context . editor , 'left' ) ) ;
54+ context . editor . update ( ( ) => setAlignmentForSelection ( context . editor , 'left' ) ) ;
4355 } ,
4456 isActive ( selection : BaseSelection | null ) {
4557 return $selectionContainsAlignment ( selection , 'left' ) ;
@@ -50,7 +62,7 @@ export const alignCenter: EditorButtonDefinition = {
5062 label : 'Align center' ,
5163 icon : alignCenterIcon ,
5264 action ( context : EditorUiContext ) {
53- context . editor . update ( ( ) => setAlignmentForSection ( context . editor , 'center' ) ) ;
65+ context . editor . update ( ( ) => setAlignmentForSelection ( context . editor , 'center' ) ) ;
5466 } ,
5567 isActive ( selection : BaseSelection | null ) {
5668 return $selectionContainsAlignment ( selection , 'center' ) ;
@@ -61,7 +73,7 @@ export const alignRight: EditorButtonDefinition = {
6173 label : 'Align right' ,
6274 icon : alignRightIcon ,
6375 action ( context : EditorUiContext ) {
64- context . editor . update ( ( ) => setAlignmentForSection ( context . editor , 'right' ) ) ;
76+ context . editor . update ( ( ) => setAlignmentForSelection ( context . editor , 'right' ) ) ;
6577 } ,
6678 isActive ( selection : BaseSelection | null ) {
6779 return $selectionContainsAlignment ( selection , 'right' ) ;
@@ -72,9 +84,31 @@ export const alignJustify: EditorButtonDefinition = {
7284 label : 'Align justify' ,
7385 icon : alignJustifyIcon ,
7486 action ( context : EditorUiContext ) {
75- context . editor . update ( ( ) => setAlignmentForSection ( context . editor , 'justify' ) ) ;
87+ context . editor . update ( ( ) => setAlignmentForSelection ( context . editor , 'justify' ) ) ;
7688 } ,
7789 isActive ( selection : BaseSelection | null ) {
7890 return $selectionContainsAlignment ( selection , 'justify' ) ;
7991 }
8092} ;
93+
94+ export const directionLTR : EditorButtonDefinition = {
95+ label : 'Left to right' ,
96+ icon : ltrIcon ,
97+ action ( context : EditorUiContext ) {
98+ context . editor . update ( ( ) => setDirectionForSelection ( context . editor , 'ltr' ) ) ;
99+ } ,
100+ isActive ( selection : BaseSelection | null ) {
101+ return $selectionContainsDirection ( selection , 'ltr' ) ;
102+ }
103+ } ;
104+
105+ export const directionRTL : EditorButtonDefinition = {
106+ label : 'Right to left' ,
107+ icon : rtlIcon ,
108+ action ( context : EditorUiContext ) {
109+ context . editor . update ( ( ) => setDirectionForSelection ( context . editor , 'rtl' ) ) ;
110+ } ,
111+ isActive ( selection : BaseSelection | null ) {
112+ return $selectionContainsDirection ( selection , 'rtl' ) ;
113+ }
114+ } ;
0 commit comments