-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2520 from microsoft/u/julairoldi/refactor-auto-fo…
…rmat Auto format fixes
- Loading branch information
Showing
12 changed files
with
415 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/roosterjs-content-model-api/lib/modelApi/list/setModelListStartNumber.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
packages/roosterjs-content-model-api/lib/modelApi/list/setModelListStyle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
packages/roosterjs-content-model-api/test/modelApi/list/setModelListStartNumberTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
137 changes: 137 additions & 0 deletions
137
packages/roosterjs-content-model-api/test/modelApi/list/setModelListStyleTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}); | ||
}); |
Oops, something went wrong.