-
Notifications
You must be signed in to change notification settings - Fork 166
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
Query content model blocks. #2851
Changes from all commits
978a819
2a56e9a
b5d60cd
5945ba5
6370fde
c172ca4
1f3422c
3a00c66
d274484
7efa7de
a91d593
5a1957f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import type { | ||
ContentModelBlockType, | ||
ReadonlyContentModelBlock, | ||
ReadonlyContentModelBlockBase, | ||
ReadonlyContentModelBlockGroup, | ||
} from 'roosterjs-content-model-types'; | ||
|
||
/** | ||
* Query content model blocks | ||
* @param group The block group to query | ||
* @param type The type of block to query | ||
* @param filter Optional selector to filter the blocks | ||
* @param findFirstOnly True to return the first block only, false to return all blocks | ||
*/ | ||
export function queryContentModelBlocks<T extends ReadonlyContentModelBlock>( | ||
group: ReadonlyContentModelBlockGroup, | ||
type: T extends ReadonlyContentModelBlockBase<infer U> ? U : never, | ||
filter?: (element: T) => element is T, | ||
findFirstOnly?: boolean | ||
): T[] { | ||
const elements: T[] = []; | ||
for (let i = 0; i < group.blocks.length; i++) { | ||
if (findFirstOnly && elements.length > 0) { | ||
return elements; | ||
} | ||
const block = group.blocks[i]; | ||
|
||
switch (block.blockType) { | ||
case 'Paragraph': | ||
case 'Divider': | ||
case 'Entity': | ||
if (isExpectedBlockType(block, type, filter)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @JiuqingSong, I tried not verifying this in every, but not do it is causing build errors |
||
elements.push(block); | ||
} | ||
break; | ||
case 'BlockGroup': | ||
if (isExpectedBlockType(block, type, filter)) { | ||
elements.push(block); | ||
} | ||
const results = queryContentModelBlocks<T>(block, type, filter, findFirstOnly); | ||
elements.push(...results); | ||
break; | ||
case 'Table': | ||
if (isExpectedBlockType(block, type, filter)) { | ||
elements.push(block); | ||
} | ||
for (const row of block.rows) { | ||
for (const cell of row.cells) { | ||
const results = queryContentModelBlocks<T>( | ||
cell, | ||
type, | ||
filter, | ||
findFirstOnly | ||
); | ||
elements.push(...results); | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
return elements; | ||
} | ||
|
||
function isExpectedBlockType<T extends ReadonlyContentModelBlock>( | ||
block: ReadonlyContentModelBlock, | ||
type: ContentModelBlockType, | ||
filter?: (element: T) => element is T | ||
): block is T { | ||
return isBlockType<T>(block, type) && (!filter || filter(block)); | ||
} | ||
|
||
function isBlockType<T extends ReadonlyContentModelBlock>( | ||
block: ReadonlyContentModelBlock, | ||
type: ContentModelBlockType | ||
): block is T { | ||
return block.blockType == type; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to have this function. Just directly check blockType == type should work |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you also want to return the path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, all the elements of type or filtered by the filter function