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

Auto format fixes #2520

Merged
merged 9 commits into from
Mar 22, 2024
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
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
Loading