Skip to content

Commit

Permalink
Merge pull request #2520 from microsoft/u/julairoldi/refactor-auto-fo…
Browse files Browse the repository at this point in the history
…rmat

Auto format fixes
  • Loading branch information
juliaroldi authored Mar 22, 2024
2 parents ef0a216 + 2c22ca8 commit c6d3934
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 121 deletions.
2 changes: 2 additions & 0 deletions packages/roosterjs-content-model-api/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export { formatParagraphWithContentModel } from './publicApi/utils/formatParagra
export { formatSegmentWithContentModel } from './publicApi/utils/formatSegmentWithContentModel';

export { setListType } from './modelApi/list/setListType';
export { setModelListStyle } from './modelApi/list/setModelListStyle';
export { setModelListStartNumber } from './modelApi/list/setModelListStartNumber';
export { findListItemsInSameThread } from './modelApi/list/findListItemsInSameThread';
export { setModelIndentation } from './modelApi/block/setModelIndentation';
export { matchLink } from './modelApi/link/matchLink';
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getFirstSelectedListItem } from 'roosterjs-content-model-dom';
import type { ContentModelDocument } from 'roosterjs-content-model-types';

/**
* Set start number of a list item
* @param model The model document
* @param value The number to set to, must be equal or greater than 1
*/
export function setModelListStartNumber(model: ContentModelDocument, value: number) {
const listItem = getFirstSelectedListItem(model);
const level = listItem?.levels[listItem?.levels.length - 1];

if (level) {
level.format.startNumberOverride = value;

return true;
} else {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { findListItemsInSameThread } from './findListItemsInSameThread';
import { getFirstSelectedListItem, updateListMetadata } from 'roosterjs-content-model-dom';
import type { ContentModelDocument, ListMetadataFormat } from 'roosterjs-content-model-types';

/**
* Set style of list items with in same thread of current item
* @param model The model document
* @param style The style to set
*/
export function setModelListStyle(model: ContentModelDocument, style: ListMetadataFormat) {
const listItem = getFirstSelectedListItem(model);

if (listItem) {
const listItems = findListItemsInSameThread(model, listItem);
const levelIndex = listItem.levels.length - 1;

listItems.forEach(listItem => {
const level = listItem.levels[levelIndex];

if (level) {
updateListMetadata(level, metadata => Object.assign({}, metadata, style));
}
});
}
return !!listItem;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getFirstSelectedListItem } from 'roosterjs-content-model-dom';
import { setModelListStartNumber } from '../../modelApi/list/setModelListStartNumber';
import type { IEditor } from 'roosterjs-content-model-types';

/**
Expand All @@ -11,16 +11,7 @@ export function setListStartNumber(editor: IEditor, value: number) {

editor.formatContentModel(
model => {
const listItem = getFirstSelectedListItem(model);
const level = listItem?.levels[listItem?.levels.length - 1];

if (level) {
level.format.startNumberOverride = value;

return true;
} else {
return false;
}
return setModelListStartNumber(model, value);
},
{
apiName: 'setListStartNumber',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { findListItemsInSameThread } from '../../modelApi/list/findListItemsInSameThread';
import { getFirstSelectedListItem, updateListMetadata } from 'roosterjs-content-model-dom';
import { setModelListStyle } from '../../modelApi/list/setModelListStyle';
import type { IEditor, ListMetadataFormat } from 'roosterjs-content-model-types';

/**
Expand All @@ -12,24 +11,7 @@ export function setListStyle(editor: IEditor, style: ListMetadataFormat) {

editor.formatContentModel(
model => {
const listItem = getFirstSelectedListItem(model);

if (listItem) {
const listItems = findListItemsInSameThread(model, listItem);
const levelIndex = listItem.levels.length - 1;

listItems.forEach(listItem => {
const level = listItem.levels[levelIndex];

if (level) {
updateListMetadata(level, metadata => Object.assign({}, metadata, style));
}
});

return true;
} else {
return false;
}
return setModelListStyle(model, style);
},
{
apiName: 'setListStyle',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { ContentModelDocument } from 'roosterjs-content-model-types';
import { setModelListStartNumber } from '../../../lib/modelApi/list/setModelListStartNumber';

describe('setModelListStartNumber', () => {
function runTest(model: ContentModelDocument, value: number, expected: boolean) {
// Act
const actual = setModelListStartNumber(model, value);

// Assert
expect(actual).toBe(expected);
}

it('set start number to 1', () => {
// Arrange
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
},
],
format: {},
},
],
levels: [
{
listType: 'OL',
format: {
listStyleType: 'decimal',
},
dataset: {
editingInfo: '{"orderedStyleType":1}',
},
},
],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
format: {},
},
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
},
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
],
format: {},
},
],
levels: [
{
listType: 'OL',
format: {
listStyleType: 'decimal',
},
dataset: {
editingInfo: '{"orderedStyleType":1}',
},
},
],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
format: {},
},
],
format: {},
};

runTest(model, 1, true);
});

it('no list', () => {
// Arrange
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
},
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
],
format: {},
},
],
format: {},
};

runTest(model, 1, false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { ContentModelDocument, ListMetadataFormat } from 'roosterjs-content-model-types';
import { setModelListStyle } from '../../../lib/modelApi/list/setModelListStyle';

describe('setModelListStyle', () => {
function runTest(model: ContentModelDocument, style: ListMetadataFormat, expected: boolean) {
// Act
const actual = setModelListStyle(model, style);

// Assert
expect(actual).toBe(expected);
}

it('set start number to 1', () => {
// Arrange
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
},
],
format: {},
},
],
levels: [
{
listType: 'OL',
format: {
listStyleType: 'decimal',
},
dataset: {
editingInfo: '{"orderedStyleType":1}',
},
},
],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
format: {},
},
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
},
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
],
format: {},
},
],
levels: [
{
listType: 'OL',
format: {
listStyleType: 'decimal',
},
dataset: {
editingInfo: '{"orderedStyleType":1}',
},
},
],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
format: {},
},
],
format: {},
};

runTest(
model,
{
unorderedStyleType: 2,
},
true
);
});

it('no list', () => {
// Arrange
const model: ContentModelDocument = {
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'test',
format: {},
},
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
],
format: {},
},
],
format: {},
};

runTest(
model,
{
unorderedStyleType: 2,
},
false
);
});
});
Loading

0 comments on commit c6d3934

Please sign in to comment.