Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 213215: set direction for list item #1926

Merged
merged 4 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { findListItemsInSameThread } from '../list/findListItemsInSameThread';
import { getOperationalBlocks } from '../selection/collectSelections';
import { isBlockGroupOfType } from '../common/isBlockGroupOfType';
import {
ContentModelBlockFormat,
ContentModelDocument,
ContentModelListItem,
MarginFormat,
PaddingFormat,
} from 'roosterjs-content-model-types';

/**
* @internal
*/
export function setModelDirection(model: ContentModelDocument, direction: 'ltr' | 'rtl') {
const paragraphOrListItemOrTable = getOperationalBlocks<ContentModelListItem>(
model,
['ListItem'],
['TableCell']
);

paragraphOrListItemOrTable.forEach(({ block }) => {
if (isBlockGroupOfType<ContentModelListItem>(block, 'ListItem')) {
const items = findListItemsInSameThread(model, block);

items.forEach(item => {
item.levels.forEach(level => {
level.direction = direction;
});

item.blocks.forEach(block => internalSetDirection(block.format, direction));
});
} else if (block) {
internalSetDirection(block.format, direction);
}
});

return paragraphOrListItemOrTable.length > 0;
}

function internalSetDirection(format: ContentModelBlockFormat, direction: 'ltr' | 'rtl') {
const wasRtl = format.direction == 'rtl';
const isRtl = direction == 'rtl';

if (wasRtl != isRtl) {
format.direction = direction;

// Adjust margin when change direction
// TODO: make margin and padding direction-aware, like what we did for textAlign. So no need to adjust them here
// TODO: Do we also need to handle border here?
const marginLeft = format.marginLeft;
const paddingLeft = format.paddingLeft;

setProperty(format, 'marginLeft', format.marginRight);
setProperty(format, 'marginRight', marginLeft);
setProperty(format, 'paddingLeft', format.paddingRight);
setProperty(format, 'paddingRight', paddingLeft);
}
}

function setProperty(
format: MarginFormat & PaddingFormat,
key: keyof (MarginFormat & PaddingFormat),
value: string | undefined
) {
if (value) {
format[key] = value;
} else {
delete format[key];
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import { formatParagraphWithContentModel } from '../utils/formatParagraphWithContentModel';
import { formatWithContentModel } from '../utils/formatWithContentModel';
import { IContentModelEditor } from '../../publicTypes/IContentModelEditor';
import { setModelDirection } from '../../modelApi/block/setModelDirection';

/**
* Set text direction of selected paragraphs (Left to right or Right to left)
* @param editor The editor to set alignment
* @param direction Direction value: ltr (Left to right) or rtl (Right to left)
*/
export default function setDirection(editor: IContentModelEditor, direction: 'ltr' | 'rtl') {
formatParagraphWithContentModel(editor, 'setDirection', para => {
const isOldValueRtl = para.format.direction == 'rtl';
const isNewValueRtl = direction == 'rtl';

if (isOldValueRtl != isNewValueRtl) {
para.format.direction = direction;

// Adjust margin when change direction
// TODO: make margin and padding direction-aware, like what we did for textAlign. So no need to adjust them here
// TODO: Do we also need to handle border here?
const marginLeft = para.format.marginLeft;
const paddingLeft = para.format.paddingLeft;

para.format.marginLeft = para.format.marginRight;
para.format.marginRight = marginLeft;

para.format.paddingLeft = para.format.paddingRight;
para.format.paddingRight = paddingLeft;
}
});
formatWithContentModel(editor, 'setDirection', model => setModelDirection(model, direction));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import { ContentModelDocument } from 'roosterjs-content-model-types';
import { setModelDirection } from '../../../lib/modelApi/block/setModelDirection';

describe('setModelDirection', () => {
function runTest(
model: ContentModelDocument,
direction: 'ltr' | 'rtl',
expectedModel: ContentModelDocument,
expectedResult: boolean
) {
const result = setModelDirection(model, direction);

expect(result).toBe(expectedResult);
expect(model).toEqual(expectedModel);
}

it('set direction for paragraph', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {
marginLeft: '10px',
paddingRight: '20px',
},
segments: [
{
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
],
},
],
},
'rtl',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
format: {
direction: 'rtl',
marginRight: '10px',
paddingLeft: '20px',
},
segments: [
{
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
],
},
],
},
true
);
});

it('set direction for divider', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Divider',
format: {},
isSelected: true,
tagName: 'hr',
},
],
},
'rtl',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Divider',
format: {
direction: 'rtl',
},
isSelected: true,
tagName: 'hr',
},
],
},
true
);
});

it('set direction for list item', () => {
runTest(
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
format: {},
formatHolder: {
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
levels: [
{
listType: 'OL',
},
],
blocks: [
{
blockType: 'Paragraph',
segments: [],
format: {},
},
{
blockType: 'Paragraph',
segments: [],
format: {},
},
],
},
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
format: {},
formatHolder: {
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
levels: [
{
listType: 'OL',
},
],
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
],
format: {},
},
],
},
],
},
'rtl',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
format: {},
formatHolder: {
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
levels: [
{
listType: 'OL',
direction: 'rtl',
},
],
blocks: [
{
blockType: 'Paragraph',
segments: [],
format: { direction: 'rtl' },
},
{
blockType: 'Paragraph',
segments: [],
format: { direction: 'rtl' },
},
],
},
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
format: {},
formatHolder: {
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
levels: [
{
listType: 'OL',
direction: 'rtl',
},
],
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'SelectionMarker',
format: {},
isSelected: true,
},
],
format: { direction: 'rtl' },
},
],
},
],
},
true
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ describe('setDirection', () => {
blockType: 'Paragraph',
format: {
direction: 'rtl',
marginLeft: undefined,
marginRight: undefined,
paddingLeft: undefined,
paddingRight: undefined,
},
segments: [
{
Expand Down Expand Up @@ -149,10 +145,6 @@ describe('setDirection', () => {
blockType: 'Paragraph',
format: {
direction: 'rtl',
marginLeft: undefined,
marginRight: undefined,
paddingLeft: undefined,
paddingRight: undefined,
},
segments: [
{
Expand Down Expand Up @@ -262,10 +254,6 @@ describe('setDirection', () => {
blockType: 'Paragraph',
format: {
direction: 'rtl',
marginLeft: undefined,
marginRight: undefined,
paddingLeft: undefined,
paddingRight: undefined,
},
segments: [
{
Expand All @@ -280,10 +268,6 @@ describe('setDirection', () => {
blockType: 'Paragraph',
format: {
direction: 'rtl',
marginLeft: undefined,
marginRight: undefined,
paddingLeft: undefined,
paddingRight: undefined,
},
segments: [
{
Expand Down Expand Up @@ -385,10 +369,6 @@ describe('setDirection', () => {
blockType: 'Paragraph',
format: {
direction: 'rtl',
marginLeft: undefined,
marginRight: undefined,
paddingLeft: undefined,
paddingRight: undefined,
},
segments: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DirectionFormat } from './formatParts/DirectionFormat';
import { LineHeightFormat } from './formatParts/LineHeightFormat';
import { MarginFormat } from './formatParts/MarginFormat';
import { PaddingFormat } from './formatParts/PaddingFormat';
import { TextAlignFormat } from './formatParts/TextAlignFormat';

/**
Expand All @@ -9,4 +10,5 @@ import { TextAlignFormat } from './formatParts/TextAlignFormat';
export type ContentModelListItemFormat = DirectionFormat &
LineHeightFormat &
MarginFormat &
PaddingFormat &
TextAlignFormat;
Loading