-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port AnnouncePlugin step 3: Add announce features for list and table (#…
…2592) * Port AnnouncePlugin step 1: refactor list number code * Port AnnouncePlugin step 2 * Port AnnouncePlugin ste 3 * add test
- Loading branch information
1 parent
7e5f1f5
commit af63a38
Showing
24 changed files
with
944 additions
and
215 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
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
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
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
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
80 changes: 80 additions & 0 deletions
80
packages/roosterjs-content-model-api/lib/modelApi/list/getListAnnounceData.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,80 @@ | ||
import { findListItemsInSameThread } from './findListItemsInSameThread'; | ||
import { | ||
getAutoListStyleType, | ||
getClosestAncestorBlockGroupIndex, | ||
getOrderedListNumberStr, | ||
updateListMetadata, | ||
} from 'roosterjs-content-model-dom'; | ||
import type { | ||
AnnounceData, | ||
ContentModelBlockGroup, | ||
ContentModelListItem, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* Get announce data for list item | ||
* @param path Content model path that include the list item | ||
* @returns Announce data of current list item if any, or null | ||
*/ | ||
export function getListAnnounceData(path: ContentModelBlockGroup[]): AnnounceData | null { | ||
const index = getClosestAncestorBlockGroupIndex(path, ['ListItem'], ['TableCell']); | ||
|
||
if (index >= 0) { | ||
const listItem = path[index] as ContentModelListItem; | ||
const level = listItem.levels[listItem.levels.length - 1]; | ||
|
||
if (level.format.displayForDummyItem) { | ||
return null; | ||
} else if (level.listType == 'OL') { | ||
const listNumber = getListNumber(path, listItem); | ||
const metadata = updateListMetadata(level); | ||
const listStyle = getAutoListStyleType( | ||
'OL', | ||
metadata ?? {}, | ||
listItem.levels.length - 1, | ||
level.format.listStyleType | ||
); | ||
|
||
return listStyle === undefined | ||
? null | ||
: { | ||
defaultStrings: 'announceListItemNumbering', | ||
formatStrings: [getOrderedListNumberStr(listStyle, listNumber)], | ||
}; | ||
} else { | ||
return { | ||
defaultStrings: 'announceListItemBullet', | ||
}; | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
function getListNumber(path: ContentModelBlockGroup[], listItem: ContentModelListItem) { | ||
const items = findListItemsInSameThread(path[path.length - 1], listItem); | ||
let listNumber = 0; | ||
|
||
for (let i = 0; i < items.length; i++) { | ||
const item = items[i]; | ||
|
||
if (listNumber == 0 && item.levels.length == listItem.levels.length) { | ||
listNumber = item.levels[item.levels.length - 1]?.format.startNumberOverride ?? 1; | ||
} | ||
|
||
if (item == listItem) { | ||
// Found current item, so break and return | ||
break; | ||
} else if (item.levels.length < listItem.levels.length) { | ||
// Found upper level item, reset list number | ||
listNumber = 0; | ||
} else if (item.levels.length > listItem.levels.length) { | ||
// Found deeper level item, skip | ||
continue; | ||
} else if (!item.levels[item.levels.length - 1].format.displayForDummyItem) { | ||
// Save level, and is not dummy, number plus one | ||
listNumber++; | ||
} | ||
} | ||
return listNumber; | ||
} |
Oops, something went wrong.