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

Query content model blocks. #2851

Merged
merged 12 commits into from
Nov 22, 2024
1 change: 1 addition & 0 deletions packages/roosterjs-content-model-api/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ export { setModelIndentation } from './modelApi/block/setModelIndentation';
export { matchLink } from './modelApi/link/matchLink';
export { promoteLink } from './modelApi/link/promoteLink';
export { getListAnnounceData } from './modelApi/list/getListAnnounceData';
export { queryContentModelBlocks } from './modelApi/common/queryContentModelBlocks';
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[] {
Copy link
Collaborator

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?

Copy link
Contributor Author

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

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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

}
Loading
Loading